Fix T57078: Alembic curve import: better check for topology similarity
[blender.git] / source / blender / alembic / intern / abc_curves.cc
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software  Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2016 K√©vin Dietrich.
19  * All rights reserved.
20  *
21  * ***** END GPL LICENSE BLOCK *****
22  *
23  */
24
25 #include "abc_curves.h"
26
27 #include <cstdio>
28
29 #include "abc_transform.h"
30 #include "abc_util.h"
31
32 extern "C" {
33 #include "MEM_guardedalloc.h"
34
35 #include "DNA_curve_types.h"
36 #include "DNA_object_types.h"
37
38 #include "BLI_listbase.h"
39
40 #include "BKE_curve.h"
41 #include "BKE_mesh.h"
42 #include "BKE_object.h"
43
44 #include "ED_curve.h"
45 }
46
47 using Alembic::Abc::IInt32ArrayProperty;
48 using Alembic::Abc::Int32ArraySamplePtr;
49 using Alembic::Abc::FloatArraySamplePtr;
50 using Alembic::Abc::P3fArraySamplePtr;
51 using Alembic::Abc::UcharArraySamplePtr;
52 using Alembic::Abc::PropertyHeader;
53
54 using Alembic::AbcGeom::ICompoundProperty;
55 using Alembic::AbcGeom::ICurves;
56 using Alembic::AbcGeom::ICurvesSchema;
57 using Alembic::AbcGeom::IFloatGeomParam;
58 using Alembic::AbcGeom::IInt16Property;
59 using Alembic::AbcGeom::ISampleSelector;
60 using Alembic::AbcGeom::kWrapExisting;
61 using Alembic::AbcGeom::CurvePeriodicity;
62
63 using Alembic::AbcGeom::OCompoundProperty;
64 using Alembic::AbcGeom::OCurves;
65 using Alembic::AbcGeom::OCurvesSchema;
66 using Alembic::AbcGeom::OInt16Property;
67 using Alembic::AbcGeom::ON3fGeomParam;
68 using Alembic::AbcGeom::OV2fGeomParam;
69
70 #define ABC_CURVE_RESOLUTION_U_PROPNAME "blender:resolution"
71
72 /* ************************************************************************** */
73
74 AbcCurveWriter::AbcCurveWriter(Object *ob,
75                                AbcTransformWriter *parent,
76                                uint32_t time_sampling,
77                                ExportSettings &settings)
78     : AbcObjectWriter(ob, time_sampling, settings, parent)
79 {
80         OCurves curves(parent->alembicXform(), m_name, m_time_sampling);
81         m_schema = curves.getSchema();
82
83         Curve *cu = static_cast<Curve *>(m_object->data);
84         OCompoundProperty user_props = m_schema.getUserProperties();
85         OInt16Property user_prop_resolu(user_props, ABC_CURVE_RESOLUTION_U_PROPNAME);
86         user_prop_resolu.set(cu->resolu);
87 }
88
89 void AbcCurveWriter::do_write()
90 {
91         Curve *curve = static_cast<Curve *>(m_object->data);
92
93         std::vector<Imath::V3f> verts;
94         std::vector<int32_t> vert_counts;
95         std::vector<float> widths;
96         std::vector<float> weights;
97         std::vector<float> knots;
98         std::vector<uint8_t> orders;
99         Imath::V3f temp_vert;
100
101         Alembic::AbcGeom::BasisType curve_basis;
102         Alembic::AbcGeom::CurveType curve_type;
103         Alembic::AbcGeom::CurvePeriodicity periodicity;
104
105         Nurb *nurbs = static_cast<Nurb *>(curve->nurb.first);
106         for (; nurbs; nurbs = nurbs->next) {
107                 if (nurbs->bp) {
108                         curve_basis = Alembic::AbcGeom::kNoBasis;
109                         curve_type = Alembic::AbcGeom::kVariableOrder;
110
111                         const int totpoint = nurbs->pntsu * nurbs->pntsv;
112
113                         const BPoint *point = nurbs->bp;
114
115                         for (int i = 0; i < totpoint; ++i, ++point) {
116                                 copy_yup_from_zup(temp_vert.getValue(), point->vec);
117                                 verts.push_back(temp_vert);
118                                 weights.push_back(point->vec[3]);
119                                 widths.push_back(point->radius);
120                         }
121                 }
122                 else if (nurbs->bezt) {
123                         curve_basis = Alembic::AbcGeom::kBezierBasis;
124                         curve_type = Alembic::AbcGeom::kCubic;
125
126                         const int totpoint = nurbs->pntsu;
127
128                         const BezTriple *bezier = nurbs->bezt;
129
130                         /* TODO(kevin): store info about handles, Alembic doesn't have this. */
131                         for (int i = 0; i < totpoint; ++i, ++bezier) {
132                                 copy_yup_from_zup(temp_vert.getValue(), bezier->vec[1]);
133                                 verts.push_back(temp_vert);
134                                 widths.push_back(bezier->radius);
135                         }
136                 }
137
138                 if ((nurbs->flagu & CU_NURB_ENDPOINT) != 0) {
139                         periodicity = Alembic::AbcGeom::kNonPeriodic;
140                 }
141                 else if ((nurbs->flagu & CU_NURB_CYCLIC) != 0) {
142                         periodicity = Alembic::AbcGeom::kPeriodic;
143
144                         /* Duplicate the start points to indicate that the curve is actually
145                          * cyclic since other software need those.
146                          */
147
148                         for (int i = 0; i < nurbs->orderu; ++i) {
149                                 verts.push_back(verts[i]);
150                         }
151                 }
152
153                 if (nurbs->knotsu != NULL) {
154                         const size_t num_knots = KNOTSU(nurbs);
155
156                         /* Add an extra knot at the beginning and end of the array since most apps
157                          * require/expect them. */
158                         knots.resize(num_knots + 2);
159
160                         for (int i = 0; i < num_knots; ++i) {
161                                 knots[i + 1] = nurbs->knotsu[i];
162                         }
163
164                         if ((nurbs->flagu & CU_NURB_CYCLIC) != 0) {
165                                 knots[0] = nurbs->knotsu[0];
166                                 knots[num_knots - 1] = nurbs->knotsu[num_knots - 1];
167                         }
168                         else {
169                                 knots[0] = (2.0f * nurbs->knotsu[0] - nurbs->knotsu[1]);
170                                 knots[num_knots - 1] = (2.0f * nurbs->knotsu[num_knots - 1] - nurbs->knotsu[num_knots - 2]);
171                         }
172                 }
173
174                 orders.push_back(nurbs->orderu);
175                 vert_counts.push_back(verts.size());
176         }
177
178         Alembic::AbcGeom::OFloatGeomParam::Sample width_sample;
179         width_sample.setVals(widths);
180
181         m_sample = OCurvesSchema::Sample(verts,
182                                          vert_counts,
183                                          curve_type,
184                                          periodicity,
185                                          width_sample,
186                                          OV2fGeomParam::Sample(),  /* UVs */
187                                          ON3fGeomParam::Sample(),  /* normals */
188                                          curve_basis,
189                                          weights,
190                                          orders,
191                                          knots);
192
193         m_sample.setSelfBounds(bounds());
194         m_schema.set(m_sample);
195 }
196
197 /* ************************************************************************** */
198
199 AbcCurveReader::AbcCurveReader(const Alembic::Abc::IObject &object, ImportSettings &settings)
200     : AbcObjectReader(object, settings)
201 {
202         ICurves abc_curves(object, kWrapExisting);
203         m_curves_schema = abc_curves.getSchema();
204
205         get_min_max_time(m_iobject, m_curves_schema, m_min_time, m_max_time);
206 }
207
208 bool AbcCurveReader::valid() const
209 {
210         return m_curves_schema.valid();
211 }
212
213 bool AbcCurveReader::accepts_object_type(const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header,
214                                          const Object *const ob,
215                                          const char **err_str) const
216 {
217         if (!Alembic::AbcGeom::ICurves::matches(alembic_header)) {
218                 *err_str = "Object type mismatch, Alembic object path pointed to Curves when importing, but not any more.";
219                 return false;
220         }
221
222         if (ob->type != OB_CURVE) {
223                 *err_str = "Object type mismatch, Alembic object path points to Curves.";
224                 return false;
225         }
226
227         return true;
228 }
229
230 void AbcCurveReader::readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel)
231 {
232         Curve *cu = BKE_curve_add(bmain, m_data_name.c_str(), OB_CURVE);
233
234         cu->flag |= CU_DEFORM_FILL | CU_3D;
235         cu->actvert = CU_ACT_NONE;
236         cu->resolu = 1;
237
238         ICompoundProperty user_props = m_curves_schema.getUserProperties();
239         if (user_props) {
240                 const PropertyHeader *header = user_props.getPropertyHeader(ABC_CURVE_RESOLUTION_U_PROPNAME);
241                 if (header != NULL && header->isScalar() && IInt16Property::matches(*header)) {
242                         IInt16Property resolu(user_props, header->getName());
243                         cu->resolu = resolu.getValue(sample_sel);
244                 }
245         }
246
247         m_object = BKE_object_add_only_object(bmain, OB_CURVE, m_object_name.c_str());
248         m_object->data = cu;
249
250         read_curve_sample(cu, m_curves_schema, sample_sel);
251
252         if (has_animations(m_curves_schema, m_settings)) {
253                 addCacheModifier();
254         }
255 }
256
257 /* ************************************************************************** */
258
259 void AbcCurveReader::read_curve_sample(Curve *cu, const ICurvesSchema &schema, const ISampleSelector &sample_sel)
260 {
261         ICurvesSchema::Sample smp;
262         try {
263                 smp = schema.getValue(sample_sel);
264         }
265         catch(Alembic::Util::Exception &ex) {
266                 printf("Alembic: error reading curve sample for '%s/%s' at time %f: %s\n",
267                        m_iobject.getFullName().c_str(),
268                        schema.getName().c_str(),
269                        sample_sel.getRequestedTime(),
270                        ex.what());
271                 return;
272         }
273
274         const Int32ArraySamplePtr num_vertices = smp.getCurvesNumVertices();
275         const P3fArraySamplePtr positions = smp.getPositions();
276         const FloatArraySamplePtr weights = smp.getPositionWeights();
277         const FloatArraySamplePtr knots = smp.getKnots();
278         const CurvePeriodicity periodicity = smp.getWrap();
279         const UcharArraySamplePtr orders = smp.getOrders();
280
281         const IFloatGeomParam widths_param = schema.getWidthsParam();
282         FloatArraySamplePtr radiuses;
283
284         if (widths_param.valid()) {
285                 IFloatGeomParam::Sample wsample = widths_param.getExpandedValue(sample_sel);
286                 radiuses = wsample.getVals();
287         }
288
289         int knot_offset = 0;
290
291         size_t idx = 0;
292         for (size_t i = 0; i < num_vertices->size(); ++i) {
293                 const int num_verts = (*num_vertices)[i];
294
295                 Nurb *nu = static_cast<Nurb *>(MEM_callocN(sizeof(Nurb), "abc_getnurb"));
296                 nu->resolu = cu->resolu;
297                 nu->resolv = cu->resolv;
298                 nu->pntsu = num_verts;
299                 nu->pntsv = 1;
300                 nu->flag |= CU_SMOOTH;
301
302                 switch (smp.getType()) {
303                         case Alembic::AbcGeom::kCubic:
304                                 nu->orderu = 4;
305                                 break;
306                         case Alembic::AbcGeom::kVariableOrder:
307                                 if (orders && orders->size() > i) {
308                                         nu->orderu = static_cast<short>((*orders)[i]);
309                                         break;
310                                 }
311                                 ATTR_FALLTHROUGH;
312                         case Alembic::AbcGeom::kLinear:
313                         default:
314                                 nu->orderu = 2;
315                 }
316
317                 if (periodicity == Alembic::AbcGeom::kNonPeriodic) {
318                         nu->flagu |= CU_NURB_ENDPOINT;
319                 }
320                 else if (periodicity == Alembic::AbcGeom::kPeriodic) {
321                         nu->flagu |= CU_NURB_CYCLIC;
322
323                         /* Check the number of points which overlap, we don't have
324                          * overlapping points in Blender, but other software do use them to
325                          * indicate that a curve is actually cyclic. Usually the number of
326                          * overlapping points is equal to the order/degree of the curve.
327                          */
328
329                         const int start = idx;
330                         const int end = idx + num_verts;
331                         int overlap = 0;
332
333                         for (int j = start, k = end - nu->orderu; j < nu->orderu; ++j, ++k) {
334                                 const Imath::V3f &p1 = (*positions)[j];
335                                 const Imath::V3f &p2 = (*positions)[k];
336
337                                 if (p1 != p2) {
338                                         break;
339                                 }
340
341                                 ++overlap;
342                         }
343
344                         /* TODO: Special case, need to figure out how it coincides with knots. */
345                         if (overlap == 0 && num_verts > 2 && (*positions)[start] == (*positions)[end - 1]) {
346                                 overlap = 1;
347                         }
348
349                         /* There is no real cycles. */
350                         if (overlap == 0) {
351                                 nu->flagu &= ~CU_NURB_CYCLIC;
352                                 nu->flagu |= CU_NURB_ENDPOINT;
353                         }
354
355                         nu->pntsu -= overlap;
356                 }
357
358                 const bool do_weights = (weights != NULL) && (weights->size() > 1);
359                 float weight = 1.0f;
360
361                 const bool do_radius = (radiuses != NULL) && (radiuses->size() > 1);
362                 float radius = (radiuses && radiuses->size() == 1) ? (*radiuses)[0] : 1.0f;
363
364                 nu->type = CU_NURBS;
365
366                 nu->bp = static_cast<BPoint *>(MEM_callocN(sizeof(BPoint) * nu->pntsu, "abc_getnurb"));
367                 BPoint *bp = nu->bp;
368
369                 for (int j = 0; j < nu->pntsu; ++j, ++bp, ++idx) {
370                         const Imath::V3f &pos = (*positions)[idx];
371
372                         if (do_radius) {
373                                 radius = (*radiuses)[idx];
374                         }
375
376                         if (do_weights) {
377                                 weight = (*weights)[idx];
378                         }
379
380                         copy_zup_from_yup(bp->vec, pos.getValue());
381                         bp->vec[3] = weight;
382                         bp->f1 = SELECT;
383                         bp->radius = radius;
384                         bp->weight = 1.0f;
385                 }
386
387                 if (knots && knots->size() != 0) {
388                         nu->knotsu = static_cast<float *>(MEM_callocN(KNOTSU(nu) * sizeof(float), "abc_setsplineknotsu"));
389
390                         /* TODO: second check is temporary, for until the check for cycles is rock solid. */
391                         if (periodicity == Alembic::AbcGeom::kPeriodic && (KNOTSU(nu) == knots->size() - 2)) {
392                                 /* Skip first and last knots. */
393                                 for (size_t i = 1; i < knots->size() - 1; ++i) {
394                                         nu->knotsu[i - 1] = (*knots)[knot_offset + i];
395                                 }
396                         }
397                         else {
398                                 /* TODO: figure out how to use the knots array from other
399                                  * software in this case. */
400                                 BKE_nurb_knot_calc_u(nu);
401                         }
402
403                         knot_offset += knots->size();
404                 }
405                 else {
406                         BKE_nurb_knot_calc_u(nu);
407                 }
408
409                 BLI_addtail(BKE_curve_nurbs_get(cu), nu);
410         }
411 }
412
413 /* NOTE: Alembic only stores data about control points, but the Mesh
414  * passed from the cache modifier contains the displist, which has more data
415  * than the control points, so to avoid corrupting the displist we modify the
416  * object directly and create a new Mesh from that. Also we might need to
417  * create new or delete existing NURBS in the curve.
418  */
419 Mesh *AbcCurveReader::read_mesh(Mesh *existing_mesh,
420                                 const ISampleSelector &sample_sel,
421                                 int /*read_flag*/,
422                                 const char **err_str)
423 {
424         ICurvesSchema::Sample sample;
425
426         try {
427                 sample = m_curves_schema.getValue(sample_sel);
428         }
429         catch(Alembic::Util::Exception &ex) {
430                 *err_str = "Error reading curve sample; more detail on the console";
431                 printf("Alembic: error reading curve sample for '%s/%s' at time %f: %s\n",
432                            m_iobject.getFullName().c_str(),
433                            m_curves_schema.getName().c_str(),
434                            sample_sel.getRequestedTime(),
435                            ex.what());
436                 return existing_mesh;
437         }
438
439         const P3fArraySamplePtr &positions = sample.getPositions();
440         const Int32ArraySamplePtr num_vertices = sample.getCurvesNumVertices();
441
442         int vertex_idx = 0;
443         int curve_idx;
444         Curve *curve = static_cast<Curve *>(m_object->data);
445
446         const int curve_count = BLI_listbase_count(&curve->nurb);
447         bool same_topology = curve_count == num_vertices->size();
448
449         if (same_topology) {
450                 Nurb *nurbs = static_cast<Nurb *>(curve->nurb.first);
451                 for (curve_idx=0; nurbs; nurbs = nurbs->next, ++curve_idx) {
452                         const int num_in_alembic = (*num_vertices)[curve_idx];
453                         const int num_in_blender = nurbs->pntsu;
454
455                         if (num_in_alembic != num_in_blender) {
456                                 same_topology = false;
457                                 break;
458                         }
459                 }
460         }
461
462         if (!same_topology) {
463                 BKE_nurbList_free(&curve->nurb);
464                 read_curve_sample(curve, m_curves_schema, sample_sel);
465         }
466         else {
467                 Nurb *nurbs = static_cast<Nurb *>(curve->nurb.first);
468                 for (curve_idx=0; nurbs; nurbs = nurbs->next, ++curve_idx) {
469                         const int totpoint = (*num_vertices)[curve_idx];
470
471                         if (nurbs->bp) {
472                                 BPoint *point = nurbs->bp;
473
474                                 for (int i = 0; i < totpoint; ++i, ++point, ++vertex_idx) {
475                                         const Imath::V3f &pos = (*positions)[vertex_idx];
476                                         copy_zup_from_yup(point->vec, pos.getValue());
477                                 }
478                         }
479                         else if (nurbs->bezt) {
480                                 BezTriple *bezier = nurbs->bezt;
481
482                                 for (int i = 0; i < totpoint; ++i, ++bezier, ++vertex_idx) {
483                                         const Imath::V3f &pos = (*positions)[vertex_idx];
484                                         copy_zup_from_yup(bezier->vec[1], pos.getValue());
485                                 }
486                         }
487                 }
488         }
489
490         return BKE_mesh_new_nomain_from_curve(m_object);
491 }