Basic Alembic support
[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_object.h"
42
43 #include "ED_curve.h"
44 }
45
46 using Alembic::Abc::IInt32ArrayProperty;
47 using Alembic::Abc::Int32ArraySamplePtr;
48 using Alembic::Abc::FloatArraySamplePtr;
49 using Alembic::Abc::P3fArraySamplePtr;
50 using Alembic::Abc::UcharArraySamplePtr;
51
52 using Alembic::AbcGeom::ICurves;
53 using Alembic::AbcGeom::ICurvesSchema;
54 using Alembic::AbcGeom::IFloatGeomParam;
55 using Alembic::AbcGeom::ISampleSelector;
56 using Alembic::AbcGeom::kWrapExisting;
57 using Alembic::AbcGeom::CurvePeriodicity;
58
59 using Alembic::AbcGeom::OCurves;
60 using Alembic::AbcGeom::OCurvesSchema;
61 using Alembic::AbcGeom::ON3fGeomParam;
62 using Alembic::AbcGeom::OV2fGeomParam;
63
64 /* ************************************************************************** */
65
66 AbcCurveWriter::AbcCurveWriter(Scene *scene,
67                                Object *ob,
68                                AbcTransformWriter *parent,
69                                uint32_t time_sampling,
70                                ExportSettings &settings)
71     : AbcObjectWriter(scene, ob, time_sampling, settings, parent)
72 {
73         OCurves curves(parent->alembicXform(), m_name, m_time_sampling);
74         m_schema = curves.getSchema();
75 }
76
77 void AbcCurveWriter::do_write()
78 {
79         Curve *curve = static_cast<Curve *>(m_object->data);
80
81         std::vector<Imath::V3f> verts;
82         std::vector<int32_t> vert_counts;
83         std::vector<float> widths;
84         std::vector<float> weights;
85         std::vector<float> knots;
86         std::vector<uint8_t> orders;
87         Imath::V3f temp_vert;
88
89         Alembic::AbcGeom::BasisType curve_basis;
90         Alembic::AbcGeom::CurveType curve_type;
91         Alembic::AbcGeom::CurvePeriodicity periodicity;
92
93         Nurb *nurbs = static_cast<Nurb *>(curve->nurb.first);
94         for (; nurbs; nurbs = nurbs->next) {
95                 if (nurbs->bp) {
96                         curve_basis = Alembic::AbcGeom::kNoBasis;
97                         curve_type = Alembic::AbcGeom::kLinear;
98
99                         const int totpoint = nurbs->pntsu * nurbs->pntsv;
100
101                         const BPoint *point = nurbs->bp;
102
103                         for (int i = 0; i < totpoint; ++i, ++point) {
104                                 copy_zup_yup(temp_vert.getValue(), point->vec);
105                                 verts.push_back(temp_vert);
106                                 weights.push_back(point->vec[3]);
107                                 widths.push_back(point->radius);
108                         }
109                 }
110                 else if (nurbs->bezt) {
111                         curve_basis = Alembic::AbcGeom::kBezierBasis;
112                         curve_type = Alembic::AbcGeom::kCubic;
113
114                         const int totpoint = nurbs->pntsu;
115
116                         const BezTriple *bezier = nurbs->bezt;
117
118                         /* TODO(kevin): store info about handles, Alembic doesn't have this. */
119                         for (int i = 0; i < totpoint; ++i, ++bezier) {
120                                 copy_zup_yup(temp_vert.getValue(), bezier->vec[1]);
121                                 verts.push_back(temp_vert);
122                                 widths.push_back(bezier->radius);
123                         }
124                 }
125
126                 if ((nurbs->flagu & CU_NURB_ENDPOINT) != 0) {
127                         periodicity = Alembic::AbcGeom::kNonPeriodic;
128                 }
129                 else if ((nurbs->flagu & CU_NURB_CYCLIC) != 0) {
130                         periodicity = Alembic::AbcGeom::kPeriodic;
131
132                         /* Duplicate the start points to indicate that the curve is actually
133                          * cyclic since other software need those.
134                          */
135
136                         for (int i = 0; i < nurbs->orderu; ++i) {
137                                 verts.push_back(verts[i]);
138                         }
139                 }
140
141                 if (nurbs->knotsu != NULL) {
142                         const size_t num_knots = KNOTSU(nurbs);
143
144                         /* Add an extra knot at the beggining and end of the array since most apps
145                          * require/expect them. */
146                         knots.resize(num_knots + 2);
147
148                         for (int i = 0; i < num_knots; ++i) {
149                                 knots[i + 1] = nurbs->knotsu[i];
150                         }
151
152                         if ((nurbs->flagu & CU_NURB_CYCLIC) != 0) {
153                                 knots[0] = nurbs->knotsu[0];
154                                 knots[num_knots - 1] = nurbs->knotsu[num_knots - 1];
155                         }
156                         else {
157                                 knots[0] = (2.0f * nurbs->knotsu[0] - nurbs->knotsu[1]);
158                                 knots[num_knots - 1] = (2.0f * nurbs->knotsu[num_knots - 1] - nurbs->knotsu[num_knots - 2]);
159                         }
160                 }
161
162                 orders.push_back(nurbs->orderu + 1);
163                 vert_counts.push_back(verts.size());
164         }
165
166         Alembic::AbcGeom::OFloatGeomParam::Sample width_sample;
167         width_sample.setVals(widths);
168
169         m_sample = OCurvesSchema::Sample(verts,
170                                          vert_counts,
171                                          curve_type,
172                                          periodicity,
173                                          width_sample,
174                                          OV2fGeomParam::Sample(),  /* UVs */
175                                          ON3fGeomParam::Sample(),  /* normals */
176                                          curve_basis,
177                                          weights,
178                                          orders,
179                                          knots);
180
181         m_sample.setSelfBounds(bounds());
182         m_schema.set(m_sample);
183 }
184
185 /* ************************************************************************** */
186
187 AbcCurveReader::AbcCurveReader(const Alembic::Abc::IObject &object, ImportSettings &settings)
188     : AbcObjectReader(object, settings)
189 {
190         ICurves abc_curves(object, kWrapExisting);
191         m_curves_schema = abc_curves.getSchema();
192
193         get_min_max_time(m_curves_schema, m_min_time, m_max_time);
194 }
195
196 bool AbcCurveReader::valid() const
197 {
198         return m_curves_schema.valid();
199 }
200
201 void AbcCurveReader::readObjectData(Main *bmain, float time)
202 {
203         Curve *cu = BKE_curve_add(bmain, m_data_name.c_str(), OB_CURVE);
204
205         cu->flag |= CU_DEFORM_FILL | CU_3D;
206         cu->actvert = CU_ACT_NONE;
207
208         m_object = BKE_object_add_only_object(bmain, OB_CURVE, m_object_name.c_str());
209         m_object->data = cu;
210
211         read_curve_sample(cu, m_curves_schema, time);
212
213         if (has_animations(m_curves_schema, m_settings)) {
214                 addCacheModifier();
215         }
216 }
217
218 /* ************************************************************************** */
219
220 void read_curve_sample(Curve *cu, const ICurvesSchema &schema, const float time)
221 {
222         const ISampleSelector sample_sel(time);
223         ICurvesSchema::Sample smp = schema.getValue(sample_sel);
224         const Int32ArraySamplePtr num_vertices = smp.getCurvesNumVertices();
225         const P3fArraySamplePtr positions = smp.getPositions();
226         const FloatArraySamplePtr weights = smp.getPositionWeights();
227         const FloatArraySamplePtr knots = smp.getKnots();
228         const CurvePeriodicity periodicity = smp.getWrap();
229         const UcharArraySamplePtr orders = smp.getOrders();
230
231         const IFloatGeomParam widths_param = schema.getWidthsParam();
232         FloatArraySamplePtr radiuses;
233
234         if (widths_param.valid()) {
235                 IFloatGeomParam::Sample wsample = widths_param.getExpandedValue(sample_sel);
236                 radiuses = wsample.getVals();
237         }
238
239         int knot_offset = 0;
240
241         size_t idx = 0;
242         for (size_t i = 0; i < num_vertices->size(); ++i) {
243                 const int num_verts = (*num_vertices)[i];
244
245                 Nurb *nu = static_cast<Nurb *>(MEM_callocN(sizeof(Nurb), "abc_getnurb"));
246                 nu->resolu = cu->resolu;
247                 nu->resolv = cu->resolv;
248                 nu->pntsu = num_verts;
249                 nu->pntsv = 1;
250                 nu->flag |= CU_SMOOTH;
251
252                 nu->orderu = num_verts;
253
254                 if (smp.getType() == Alembic::AbcGeom::kCubic) {
255                         nu->orderu = 3;
256                 }
257                 else if (orders && orders->size() > i) {
258                         nu->orderu = static_cast<short>((*orders)[i] - 1);
259                 }
260
261                 if (periodicity == Alembic::AbcGeom::kNonPeriodic) {
262                         nu->flagu |= CU_NURB_ENDPOINT;
263                 }
264                 else if (periodicity == Alembic::AbcGeom::kPeriodic) {
265                         nu->flagu |= CU_NURB_CYCLIC;
266
267                         /* Check the number of points which overlap, we don't have
268                          * overlapping points in Blender, but other software do use them to
269                          * indicate that a curve is actually cyclic. Usually the number of
270                          * overlapping points is equal to the order/degree of the curve.
271                          */
272
273                         const int start = idx;
274                         const int end = idx + num_verts;
275                         int overlap = 0;
276
277                         for (int j = start, k = end - nu->orderu; j < nu->orderu; ++j, ++k) {
278                                 const Imath::V3f &p1 = (*positions)[j];
279                                 const Imath::V3f &p2 = (*positions)[k];
280
281                                 if (p1 != p2) {
282                                         break;
283                                 }
284
285                                 ++overlap;
286                         }
287
288                         /* TODO: Special case, need to figure out how it coincides with knots. */
289                         if (overlap == 0 && num_verts > 2 && (*positions)[start] == (*positions)[end - 1]) {
290                                 overlap = 1;
291                         }
292
293                         /* There is no real cycles. */
294                         if (overlap == 0) {
295                                 nu->flagu &= ~CU_NURB_CYCLIC;
296                                 nu->flagu |= CU_NURB_ENDPOINT;
297                         }
298
299                         nu->pntsu -= overlap;
300                 }
301
302                 const bool do_weights = (weights != NULL) && (weights->size() > 1);
303                 float weight = 1.0f;
304
305                 const bool do_radius = (radiuses != NULL) && (radiuses->size() > 1);
306                 float radius = (radiuses && radiuses->size() == 1) ? (*radiuses)[0] : 1.0f;
307
308                 nu->type = CU_NURBS;
309
310                 nu->bp = static_cast<BPoint *>(MEM_callocN(sizeof(BPoint) * nu->pntsu, "abc_getnurb"));
311                 BPoint *bp = nu->bp;
312
313                 for (int j = 0; j < nu->pntsu; ++j, ++bp, ++idx) {
314                         const Imath::V3f &pos = (*positions)[idx];
315
316                         if (do_radius) {
317                                 radius = (*radiuses)[idx];
318                         }
319
320                         if (do_weights) {
321                                 weight = (*weights)[idx];
322                         }
323
324                         copy_yup_zup(bp->vec, pos.getValue());
325                         bp->vec[3] = weight;
326                         bp->f1 = SELECT;
327                         bp->radius = radius;
328                         bp->weight = 1.0f;
329                 }
330
331                 if (knots && knots->size() != 0) {
332                         nu->knotsu = static_cast<float *>(MEM_callocN(KNOTSU(nu) * sizeof(float), "abc_setsplineknotsu"));
333
334                         /* TODO: second check is temporary, for until the check for cycles is rock solid. */
335                         if (periodicity == Alembic::AbcGeom::kPeriodic && (KNOTSU(nu) == knots->size() - 2)) {
336                                 /* Skip first and last knots. */
337                                 for (size_t i = 1; i < knots->size() - 1; ++i) {
338                                         nu->knotsu[i - 1] = (*knots)[knot_offset + i];
339                                 }
340                         }
341                         else {
342                                 /* TODO: figure out how to use the knots array from other
343                                  * software in this case. */
344                                 BKE_nurb_knot_calc_u(nu);
345                         }
346
347                         knot_offset += knots->size();
348                 }
349                 else {
350                         BKE_nurb_knot_calc_u(nu);
351                 }
352
353                 BLI_addtail(BKE_curve_nurbs_get(cu), nu);
354         }
355 }