bf485c7ef61f85889174bdaebc8bb9fe83e236c9
[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_cdderivedmesh.h"
41 #include "BKE_curve.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(Scene *scene,
75                                Object *ob,
76                                AbcTransformWriter *parent,
77                                uint32_t time_sampling,
78                                ExportSettings &settings)
79     : AbcObjectWriter(scene, ob, time_sampling, settings, parent)
80 {
81         OCurves curves(parent->alembicXform(), m_name, m_time_sampling);
82         m_schema = curves.getSchema();
83
84         Curve *cu = static_cast<Curve *>(m_object->data);
85         OCompoundProperty user_props = m_schema.getUserProperties();
86         OInt16Property user_prop_resolu(user_props, ABC_CURVE_RESOLUTION_U_PROPNAME);
87         user_prop_resolu.set(cu->resolu);
88 }
89
90 void AbcCurveWriter::do_write()
91 {
92         Curve *curve = static_cast<Curve *>(m_object->data);
93
94         std::vector<Imath::V3f> verts;
95         std::vector<int32_t> vert_counts;
96         std::vector<float> widths;
97         std::vector<float> weights;
98         std::vector<float> knots;
99         std::vector<uint8_t> orders;
100         Imath::V3f temp_vert;
101
102         Alembic::AbcGeom::BasisType curve_basis;
103         Alembic::AbcGeom::CurveType curve_type;
104         Alembic::AbcGeom::CurvePeriodicity periodicity;
105
106         Nurb *nurbs = static_cast<Nurb *>(curve->nurb.first);
107         for (; nurbs; nurbs = nurbs->next) {
108                 if (nurbs->bp) {
109                         curve_basis = Alembic::AbcGeom::kNoBasis;
110                         curve_type = Alembic::AbcGeom::kVariableOrder;
111
112                         const int totpoint = nurbs->pntsu * nurbs->pntsv;
113
114                         const BPoint *point = nurbs->bp;
115
116                         for (int i = 0; i < totpoint; ++i, ++point) {
117                                 copy_yup_from_zup(temp_vert.getValue(), point->vec);
118                                 verts.push_back(temp_vert);
119                                 weights.push_back(point->vec[3]);
120                                 widths.push_back(point->radius);
121                         }
122                 }
123                 else if (nurbs->bezt) {
124                         curve_basis = Alembic::AbcGeom::kBezierBasis;
125                         curve_type = Alembic::AbcGeom::kCubic;
126
127                         const int totpoint = nurbs->pntsu;
128
129                         const BezTriple *bezier = nurbs->bezt;
130
131                         /* TODO(kevin): store info about handles, Alembic doesn't have this. */
132                         for (int i = 0; i < totpoint; ++i, ++bezier) {
133                                 copy_yup_from_zup(temp_vert.getValue(), bezier->vec[1]);
134                                 verts.push_back(temp_vert);
135                                 widths.push_back(bezier->radius);
136                         }
137                 }
138
139                 if ((nurbs->flagu & CU_NURB_ENDPOINT) != 0) {
140                         periodicity = Alembic::AbcGeom::kNonPeriodic;
141                 }
142                 else if ((nurbs->flagu & CU_NURB_CYCLIC) != 0) {
143                         periodicity = Alembic::AbcGeom::kPeriodic;
144
145                         /* Duplicate the start points to indicate that the curve is actually
146                          * cyclic since other software need those.
147                          */
148
149                         for (int i = 0; i < nurbs->orderu; ++i) {
150                                 verts.push_back(verts[i]);
151                         }
152                 }
153
154                 if (nurbs->knotsu != NULL) {
155                         const size_t num_knots = KNOTSU(nurbs);
156
157                         /* Add an extra knot at the beggining and end of the array since most apps
158                          * require/expect them. */
159                         knots.resize(num_knots + 2);
160
161                         for (int i = 0; i < num_knots; ++i) {
162                                 knots[i + 1] = nurbs->knotsu[i];
163                         }
164
165                         if ((nurbs->flagu & CU_NURB_CYCLIC) != 0) {
166                                 knots[0] = nurbs->knotsu[0];
167                                 knots[num_knots - 1] = nurbs->knotsu[num_knots - 1];
168                         }
169                         else {
170                                 knots[0] = (2.0f * nurbs->knotsu[0] - nurbs->knotsu[1]);
171                                 knots[num_knots - 1] = (2.0f * nurbs->knotsu[num_knots - 1] - nurbs->knotsu[num_knots - 2]);
172                         }
173                 }
174
175                 orders.push_back(nurbs->orderu);
176                 vert_counts.push_back(verts.size());
177         }
178
179         Alembic::AbcGeom::OFloatGeomParam::Sample width_sample;
180         width_sample.setVals(widths);
181
182         m_sample = OCurvesSchema::Sample(verts,
183                                          vert_counts,
184                                          curve_type,
185                                          periodicity,
186                                          width_sample,
187                                          OV2fGeomParam::Sample(),  /* UVs */
188                                          ON3fGeomParam::Sample(),  /* normals */
189                                          curve_basis,
190                                          weights,
191                                          orders,
192                                          knots);
193
194         m_sample.setSelfBounds(bounds());
195         m_schema.set(m_sample);
196 }
197
198 /* ************************************************************************** */
199
200 AbcCurveReader::AbcCurveReader(const Alembic::Abc::IObject &object, ImportSettings &settings)
201     : AbcObjectReader(object, settings)
202 {
203         ICurves abc_curves(object, kWrapExisting);
204         m_curves_schema = abc_curves.getSchema();
205
206         get_min_max_time(m_iobject, m_curves_schema, m_min_time, m_max_time);
207 }
208
209 bool AbcCurveReader::valid() const
210 {
211         return m_curves_schema.valid();
212 }
213
214 void AbcCurveReader::readObjectData(Main *bmain, float time)
215 {
216         Curve *cu = BKE_curve_add(bmain, m_data_name.c_str(), OB_CURVE);
217
218         cu->flag |= CU_DEFORM_FILL | CU_3D;
219         cu->actvert = CU_ACT_NONE;
220         cu->resolu = 1;
221
222         const ISampleSelector sample_sel(time);
223
224         ICompoundProperty user_props = m_curves_schema.getUserProperties();
225         if (user_props) {
226                 const PropertyHeader *header = user_props.getPropertyHeader(ABC_CURVE_RESOLUTION_U_PROPNAME);
227                 if (header != NULL && header->isScalar() && IInt16Property::matches(*header)) {
228                         IInt16Property resolu(user_props, header->getName());
229                         cu->resolu = resolu.getValue(sample_sel);
230                 }
231         }
232
233         m_object = BKE_object_add_only_object(bmain, OB_CURVE, m_object_name.c_str());
234         m_object->data = cu;
235
236         read_curve_sample(cu, m_curves_schema, sample_sel);
237
238         if (has_animations(m_curves_schema, m_settings)) {
239                 addCacheModifier();
240         }
241 }
242
243 /* ************************************************************************** */
244
245 void read_curve_sample(Curve *cu, const ICurvesSchema &schema, const ISampleSelector &sample_sel)
246 {
247         ICurvesSchema::Sample smp = schema.getValue(sample_sel);
248         const Int32ArraySamplePtr num_vertices = smp.getCurvesNumVertices();
249         const P3fArraySamplePtr positions = smp.getPositions();
250         const FloatArraySamplePtr weights = smp.getPositionWeights();
251         const FloatArraySamplePtr knots = smp.getKnots();
252         const CurvePeriodicity periodicity = smp.getWrap();
253         const UcharArraySamplePtr orders = smp.getOrders();
254
255         const IFloatGeomParam widths_param = schema.getWidthsParam();
256         FloatArraySamplePtr radiuses;
257
258         if (widths_param.valid()) {
259                 IFloatGeomParam::Sample wsample = widths_param.getExpandedValue(sample_sel);
260                 radiuses = wsample.getVals();
261         }
262
263         int knot_offset = 0;
264
265         size_t idx = 0;
266         for (size_t i = 0; i < num_vertices->size(); ++i) {
267                 const int num_verts = (*num_vertices)[i];
268
269                 Nurb *nu = static_cast<Nurb *>(MEM_callocN(sizeof(Nurb), "abc_getnurb"));
270                 nu->resolu = cu->resolu;
271                 nu->resolv = cu->resolv;
272                 nu->pntsu = num_verts;
273                 nu->pntsv = 1;
274                 nu->flag |= CU_SMOOTH;
275
276                 switch (smp.getType()) {
277                         case Alembic::AbcGeom::kCubic:
278                                 nu->orderu = 4;
279                                 break;
280                         case Alembic::AbcGeom::kVariableOrder:
281                                 if (orders && orders->size() > i) {
282                                         nu->orderu = static_cast<short>((*orders)[i]);
283                                         break;
284                                 }
285                         case Alembic::AbcGeom::kLinear:
286                         default:
287                                 nu->orderu = 2;
288                 }
289
290                 if (periodicity == Alembic::AbcGeom::kNonPeriodic) {
291                         nu->flagu |= CU_NURB_ENDPOINT;
292                 }
293                 else if (periodicity == Alembic::AbcGeom::kPeriodic) {
294                         nu->flagu |= CU_NURB_CYCLIC;
295
296                         /* Check the number of points which overlap, we don't have
297                          * overlapping points in Blender, but other software do use them to
298                          * indicate that a curve is actually cyclic. Usually the number of
299                          * overlapping points is equal to the order/degree of the curve.
300                          */
301
302                         const int start = idx;
303                         const int end = idx + num_verts;
304                         int overlap = 0;
305
306                         for (int j = start, k = end - nu->orderu; j < nu->orderu; ++j, ++k) {
307                                 const Imath::V3f &p1 = (*positions)[j];
308                                 const Imath::V3f &p2 = (*positions)[k];
309
310                                 if (p1 != p2) {
311                                         break;
312                                 }
313
314                                 ++overlap;
315                         }
316
317                         /* TODO: Special case, need to figure out how it coincides with knots. */
318                         if (overlap == 0 && num_verts > 2 && (*positions)[start] == (*positions)[end - 1]) {
319                                 overlap = 1;
320                         }
321
322                         /* There is no real cycles. */
323                         if (overlap == 0) {
324                                 nu->flagu &= ~CU_NURB_CYCLIC;
325                                 nu->flagu |= CU_NURB_ENDPOINT;
326                         }
327
328                         nu->pntsu -= overlap;
329                 }
330
331                 const bool do_weights = (weights != NULL) && (weights->size() > 1);
332                 float weight = 1.0f;
333
334                 const bool do_radius = (radiuses != NULL) && (radiuses->size() > 1);
335                 float radius = (radiuses && radiuses->size() == 1) ? (*radiuses)[0] : 1.0f;
336
337                 nu->type = CU_NURBS;
338
339                 nu->bp = static_cast<BPoint *>(MEM_callocN(sizeof(BPoint) * nu->pntsu, "abc_getnurb"));
340                 BPoint *bp = nu->bp;
341
342                 for (int j = 0; j < nu->pntsu; ++j, ++bp, ++idx) {
343                         const Imath::V3f &pos = (*positions)[idx];
344
345                         if (do_radius) {
346                                 radius = (*radiuses)[idx];
347                         }
348
349                         if (do_weights) {
350                                 weight = (*weights)[idx];
351                         }
352
353                         copy_zup_from_yup(bp->vec, pos.getValue());
354                         bp->vec[3] = weight;
355                         bp->f1 = SELECT;
356                         bp->radius = radius;
357                         bp->weight = 1.0f;
358                 }
359
360                 if (knots && knots->size() != 0) {
361                         nu->knotsu = static_cast<float *>(MEM_callocN(KNOTSU(nu) * sizeof(float), "abc_setsplineknotsu"));
362
363                         /* TODO: second check is temporary, for until the check for cycles is rock solid. */
364                         if (periodicity == Alembic::AbcGeom::kPeriodic && (KNOTSU(nu) == knots->size() - 2)) {
365                                 /* Skip first and last knots. */
366                                 for (size_t i = 1; i < knots->size() - 1; ++i) {
367                                         nu->knotsu[i - 1] = (*knots)[knot_offset + i];
368                                 }
369                         }
370                         else {
371                                 /* TODO: figure out how to use the knots array from other
372                                  * software in this case. */
373                                 BKE_nurb_knot_calc_u(nu);
374                         }
375
376                         knot_offset += knots->size();
377                 }
378                 else {
379                         BKE_nurb_knot_calc_u(nu);
380                 }
381
382                 BLI_addtail(BKE_curve_nurbs_get(cu), nu);
383         }
384 }
385
386 /* NOTE: Alembic only stores data about control points, but the DerivedMesh
387  * passed from the cache modifier contains the displist, which has more data
388  * than the control points, so to avoid corrupting the displist we modify the
389  * object directly and create a new DerivedMesh from that. Also we might need to
390  * create new or delete existing NURBS in the curve.
391  */
392 DerivedMesh *AbcCurveReader::read_derivedmesh(DerivedMesh * /*dm*/, const float time, int /*read_flag*/, const char ** /*err_str*/)
393 {
394         ISampleSelector sample_sel(time);
395         const ICurvesSchema::Sample sample = m_curves_schema.getValue(sample_sel);
396
397         const P3fArraySamplePtr &positions = sample.getPositions();
398         const Int32ArraySamplePtr num_vertices = sample.getCurvesNumVertices();
399
400         int vertex_idx = 0;
401         int curve_idx = 0;
402         Curve *curve = static_cast<Curve *>(m_object->data);
403
404         const int curve_count = BLI_listbase_count(&curve->nurb);
405
406         if (curve_count != num_vertices->size()) {
407                 BKE_nurbList_free(&curve->nurb);
408                 read_curve_sample(curve, m_curves_schema, sample_sel);
409         }
410         else {
411                 Nurb *nurbs = static_cast<Nurb *>(curve->nurb.first);
412                 for (; nurbs; nurbs = nurbs->next, ++curve_idx) {
413                         const int totpoint = (*num_vertices)[curve_idx];
414
415                         if (nurbs->bp) {
416                                 BPoint *point = nurbs->bp;
417
418                                 for (int i = 0; i < totpoint; ++i, ++point, ++vertex_idx) {
419                                         const Imath::V3f &pos = (*positions)[vertex_idx];
420                                         copy_zup_from_yup(point->vec, pos.getValue());
421                                 }
422                         }
423                         else if (nurbs->bezt) {
424                                 BezTriple *bezier = nurbs->bezt;
425
426                                 for (int i = 0; i < totpoint; ++i, ++bezier, ++vertex_idx) {
427                                         const Imath::V3f &pos = (*positions)[vertex_idx];
428                                         copy_zup_from_yup(bezier->vec[1], pos.getValue());
429                                 }
430                         }
431                 }
432         }
433
434         return CDDM_from_curve(m_object);
435 }