Blender  V2.93
abc_reader_curves.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2016 Kévin Dietrich.
17  * All rights reserved.
18  */
19 
24 #include "abc_reader_curves.h"
25 #include "abc_axis_conversion.h"
26 #include "abc_reader_transform.h"
27 #include "abc_util.h"
28 
29 #include <cstdio>
30 
31 #include "MEM_guardedalloc.h"
32 
33 #include "DNA_curve_types.h"
34 #include "DNA_object_types.h"
35 
36 #include "BLI_listbase.h"
37 
38 #include "BKE_curve.h"
39 #include "BKE_mesh.h"
40 #include "BKE_object.h"
41 
42 using Alembic::Abc::FloatArraySamplePtr;
43 using Alembic::Abc::Int32ArraySamplePtr;
44 using Alembic::Abc::P3fArraySamplePtr;
45 using Alembic::Abc::PropertyHeader;
46 using Alembic::Abc::UcharArraySamplePtr;
47 
48 using Alembic::AbcGeom::CurvePeriodicity;
49 using Alembic::AbcGeom::ICompoundProperty;
50 using Alembic::AbcGeom::ICurves;
51 using Alembic::AbcGeom::ICurvesSchema;
52 using Alembic::AbcGeom::IFloatGeomParam;
53 using Alembic::AbcGeom::IInt16Property;
54 using Alembic::AbcGeom::ISampleSelector;
55 using Alembic::AbcGeom::kWrapExisting;
56 
57 namespace blender::io::alembic {
58 
59 AbcCurveReader::AbcCurveReader(const Alembic::Abc::IObject &object, ImportSettings &settings)
60  : AbcObjectReader(object, settings)
61 {
62  ICurves abc_curves(object, kWrapExisting);
63  m_curves_schema = abc_curves.getSchema();
64 
65  get_min_max_time(m_iobject, m_curves_schema, m_min_time, m_max_time);
66 }
67 
69 {
70  return m_curves_schema.valid();
71 }
72 
74  const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header,
75  const Object *const ob,
76  const char **err_str) const
77 {
78  if (!Alembic::AbcGeom::ICurves::matches(alembic_header)) {
79  *err_str =
80  "Object type mismatch, Alembic object path pointed to Curves when importing, but not any "
81  "more.";
82  return false;
83  }
84 
85  if (ob->type != OB_CURVE) {
86  *err_str = "Object type mismatch, Alembic object path points to Curves.";
87  return false;
88  }
89 
90  return true;
91 }
92 
93 void AbcCurveReader::readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel)
94 {
95  Curve *cu = BKE_curve_add(bmain, m_data_name.c_str(), OB_CURVE);
96 
97  cu->flag |= CU_DEFORM_FILL | CU_3D;
98  cu->actvert = CU_ACT_NONE;
99  cu->resolu = 1;
100 
101  ICompoundProperty user_props = m_curves_schema.getUserProperties();
102  if (user_props) {
103  const PropertyHeader *header = user_props.getPropertyHeader(ABC_CURVE_RESOLUTION_U_PROPNAME);
104  if (header != nullptr && header->isScalar() && IInt16Property::matches(*header)) {
105  IInt16Property resolu(user_props, header->getName());
106  cu->resolu = resolu.getValue(sample_sel);
107  }
108  }
109 
111  m_object->data = cu;
112 
113  read_curve_sample(cu, m_curves_schema, sample_sel);
114 
115  if (has_animations(m_curves_schema, m_settings)) {
117  }
118 }
119 
121  const ICurvesSchema &schema,
122  const ISampleSelector &sample_sel)
123 {
124  ICurvesSchema::Sample smp;
125  try {
126  smp = schema.getValue(sample_sel);
127  }
128  catch (Alembic::Util::Exception &ex) {
129  printf("Alembic: error reading curve sample for '%s/%s' at time %f: %s\n",
130  m_iobject.getFullName().c_str(),
131  schema.getName().c_str(),
132  sample_sel.getRequestedTime(),
133  ex.what());
134  return;
135  }
136 
137  const Int32ArraySamplePtr num_vertices = smp.getCurvesNumVertices();
138  const P3fArraySamplePtr positions = smp.getPositions();
139  const FloatArraySamplePtr weights = smp.getPositionWeights();
140  const FloatArraySamplePtr knots = smp.getKnots();
141  const CurvePeriodicity periodicity = smp.getWrap();
142  const UcharArraySamplePtr orders = smp.getOrders();
143 
144  const IFloatGeomParam widths_param = schema.getWidthsParam();
145  FloatArraySamplePtr radiuses;
146 
147  if (widths_param.valid()) {
148  IFloatGeomParam::Sample wsample = widths_param.getExpandedValue(sample_sel);
149  radiuses = wsample.getVals();
150  }
151 
152  int knot_offset = 0;
153 
154  size_t idx = 0;
155  for (size_t i = 0; i < num_vertices->size(); i++) {
156  const int num_verts = (*num_vertices)[i];
157 
158  Nurb *nu = static_cast<Nurb *>(MEM_callocN(sizeof(Nurb), "abc_getnurb"));
159  nu->resolu = cu->resolu;
160  nu->resolv = cu->resolv;
161  nu->pntsu = num_verts;
162  nu->pntsv = 1;
163  nu->flag |= CU_SMOOTH;
164 
165  switch (smp.getType()) {
166  case Alembic::AbcGeom::kCubic:
167  nu->orderu = 4;
168  break;
169  case Alembic::AbcGeom::kVariableOrder:
170  if (orders && orders->size() > i) {
171  nu->orderu = static_cast<short>((*orders)[i]);
172  break;
173  }
175  case Alembic::AbcGeom::kLinear:
176  default:
177  nu->orderu = 2;
178  }
179 
180  if (periodicity == Alembic::AbcGeom::kNonPeriodic) {
181  nu->flagu |= CU_NURB_ENDPOINT;
182  }
183  else if (periodicity == Alembic::AbcGeom::kPeriodic) {
184  nu->flagu |= CU_NURB_CYCLIC;
185 
186  /* Check the number of points which overlap, we don't have
187  * overlapping points in Blender, but other software do use them to
188  * indicate that a curve is actually cyclic. Usually the number of
189  * overlapping points is equal to the order/degree of the curve.
190  */
191 
192  const int start = idx;
193  const int end = idx + num_verts;
194  int overlap = 0;
195 
196  for (int j = start, k = end - nu->orderu; j < nu->orderu; j++, k++) {
197  const Imath::V3f &p1 = (*positions)[j];
198  const Imath::V3f &p2 = (*positions)[k];
199 
200  if (p1 != p2) {
201  break;
202  }
203 
204  overlap++;
205  }
206 
207  /* TODO: Special case, need to figure out how it coincides with knots. */
208  if (overlap == 0 && num_verts > 2 && (*positions)[start] == (*positions)[end - 1]) {
209  overlap = 1;
210  }
211 
212  /* There is no real cycles. */
213  if (overlap == 0) {
214  nu->flagu &= ~CU_NURB_CYCLIC;
215  nu->flagu |= CU_NURB_ENDPOINT;
216  }
217 
218  nu->pntsu -= overlap;
219  }
220 
221  const bool do_weights = (weights != nullptr) && (weights->size() > 1);
222  float weight = 1.0f;
223 
224  const bool do_radius = (radiuses != nullptr) && (radiuses->size() > 1);
225  float radius = (radiuses && radiuses->size() == 1) ? (*radiuses)[0] : 1.0f;
226 
227  nu->type = CU_NURBS;
228 
229  nu->bp = static_cast<BPoint *>(MEM_callocN(sizeof(BPoint) * nu->pntsu, "abc_getnurb"));
230  BPoint *bp = nu->bp;
231 
232  for (int j = 0; j < nu->pntsu; j++, bp++, idx++) {
233  const Imath::V3f &pos = (*positions)[idx];
234 
235  if (do_radius) {
236  radius = (*radiuses)[idx];
237  }
238 
239  if (do_weights) {
240  weight = (*weights)[idx];
241  }
242 
243  copy_zup_from_yup(bp->vec, pos.getValue());
244  bp->vec[3] = weight;
245  bp->f1 = SELECT;
246  bp->radius = radius;
247  bp->weight = 1.0f;
248  }
249 
250  if (knots && knots->size() != 0) {
251  nu->knotsu = static_cast<float *>(
252  MEM_callocN(KNOTSU(nu) * sizeof(float), "abc_setsplineknotsu"));
253 
254  /* TODO: second check is temporary, for until the check for cycles is rock solid. */
255  if (periodicity == Alembic::AbcGeom::kPeriodic && (KNOTSU(nu) == knots->size() - 2)) {
256  /* Skip first and last knots. */
257  for (size_t i = 1; i < knots->size() - 1; i++) {
258  nu->knotsu[i - 1] = (*knots)[knot_offset + i];
259  }
260  }
261  else {
262  /* TODO: figure out how to use the knots array from other
263  * software in this case. */
265  }
266 
267  knot_offset += knots->size();
268  }
269  else {
271  }
272 
274  }
275 }
276 
277 /* NOTE: Alembic only stores data about control points, but the Mesh
278  * passed from the cache modifier contains the displist, which has more data
279  * than the control points, so to avoid corrupting the displist we modify the
280  * object directly and create a new Mesh from that. Also we might need to
281  * create new or delete existing NURBS in the curve.
282  */
284  const ISampleSelector &sample_sel,
285  int /*read_flag*/,
286  const char **err_str)
287 {
288  ICurvesSchema::Sample sample;
289 
290  try {
291  sample = m_curves_schema.getValue(sample_sel);
292  }
293  catch (Alembic::Util::Exception &ex) {
294  *err_str = "Error reading curve sample; more detail on the console";
295  printf("Alembic: error reading curve sample for '%s/%s' at time %f: %s\n",
296  m_iobject.getFullName().c_str(),
297  m_curves_schema.getName().c_str(),
298  sample_sel.getRequestedTime(),
299  ex.what());
300  return existing_mesh;
301  }
302 
303  const P3fArraySamplePtr &positions = sample.getPositions();
304  const Int32ArraySamplePtr num_vertices = sample.getCurvesNumVertices();
305 
306  int vertex_idx = 0;
307  int curve_idx;
308  Curve *curve = static_cast<Curve *>(m_object->data);
309 
310  const int curve_count = BLI_listbase_count(&curve->nurb);
311  bool same_topology = curve_count == num_vertices->size();
312 
313  if (same_topology) {
314  Nurb *nurbs = static_cast<Nurb *>(curve->nurb.first);
315  for (curve_idx = 0; nurbs; nurbs = nurbs->next, curve_idx++) {
316  const int num_in_alembic = (*num_vertices)[curve_idx];
317  const int num_in_blender = nurbs->pntsu;
318 
319  if (num_in_alembic != num_in_blender) {
320  same_topology = false;
321  break;
322  }
323  }
324  }
325 
326  if (!same_topology) {
328  read_curve_sample(curve, m_curves_schema, sample_sel);
329  }
330  else {
331  Nurb *nurbs = static_cast<Nurb *>(curve->nurb.first);
332  for (curve_idx = 0; nurbs; nurbs = nurbs->next, curve_idx++) {
333  const int totpoint = (*num_vertices)[curve_idx];
334 
335  if (nurbs->bp) {
336  BPoint *point = nurbs->bp;
337 
338  for (int i = 0; i < totpoint; i++, point++, vertex_idx++) {
339  const Imath::V3f &pos = (*positions)[vertex_idx];
340  copy_zup_from_yup(point->vec, pos.getValue());
341  }
342  }
343  else if (nurbs->bezt) {
344  BezTriple *bezier = nurbs->bezt;
345 
346  for (int i = 0; i < totpoint; i++, bezier++, vertex_idx++) {
347  const Imath::V3f &pos = (*positions)[vertex_idx];
348  copy_zup_from_yup(bezier->vec[1], pos.getValue());
349  }
350  }
351  }
352  }
353 
355 }
356 
357 } // namespace blender::io::alembic
#define KNOTSU(nu)
Definition: BKE_curve.h:68
struct Curve * BKE_curve_add(struct Main *bmain, const char *name, int type)
Definition: curve.c:424
void BKE_nurb_knot_calc_u(struct Nurb *nu)
Definition: curve.c:1303
void BKE_nurbList_free(struct ListBase *lb)
Definition: curve.c:660
ListBase * BKE_curve_nurbs_get(struct Curve *cu)
Definition: curve.c:5079
struct Mesh * BKE_mesh_new_nomain_from_curve(struct Object *ob)
Definition: mesh_convert.c:572
General operations, lookup, etc. for blender objects.
struct Object * BKE_object_add_only_object(struct Main *bmain, int type, const char *name) ATTR_NONNULL(1) ATTR_RETURNS_NONNULL
Definition: object.c:2193
#define ATTR_FALLTHROUGH
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:110
int BLI_listbase_count(const struct ListBase *listbase) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
@ CU_NURBS
@ CU_SMOOTH
#define CU_ACT_NONE
@ CU_NURB_CYCLIC
@ CU_NURB_ENDPOINT
@ CU_3D
@ CU_DEFORM_FILL
Object is a sort of wrapper for general info.
@ OB_CURVE
Read Guarded memory(de)allocation.
void readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel)
void read_curve_sample(Curve *cu, const Alembic::AbcGeom::ICurvesSchema &schema, const Alembic::Abc::ISampleSelector &sample_selector)
bool accepts_object_type(const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header, const Object *const ob, const char **err_str) const
struct Mesh * read_mesh(struct Mesh *existing_mesh, const Alembic::Abc::ISampleSelector &sample_sel, int read_flag, const char **err_str)
AbcCurveReader(const Alembic::Abc::IObject &object, ImportSettings &settings)
#define SELECT
Curve curve
uint pos
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
static void sample(SocketReader *reader, int x, int y, float color[4])
void get_min_max_time(const Alembic::AbcGeom::IObject &object, const Schema &schema, chrono_t &min, chrono_t &max)
Definition: abc_util.h:80
BLI_INLINE void copy_zup_from_yup(float zup[3], const float yup[3])
const std::string ABC_CURVE_RESOLUTION_U_PROPNAME
bool has_animations(Alembic::AbcGeom::IPolyMeshSchema &schema, ImportSettings *settings)
float weight
uint8_t f1
float vec[4]
float radius
float vec[3][3]
short resolv
short resolu
ListBase nurb
void * first
Definition: DNA_listBase.h:47
Definition: BKE_main.h:116
short flagu
short orderu
struct Nurb * next
float * knotsu
short flag
short type
BezTriple * bezt
BPoint * bp
short resolu
short resolv
void * data