Blender  V2.93
abc_reader_nurbs.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 
21 #include "abc_reader_nurbs.h"
22 #include "abc_axis_conversion.h"
23 #include "abc_reader_transform.h"
24 #include "abc_util.h"
25 
26 #include "MEM_guardedalloc.h"
27 
28 #include "DNA_curve_types.h"
29 #include "DNA_object_types.h"
30 
31 #include "BLI_listbase.h"
32 #include "BLI_string.h"
33 
34 #include "BKE_curve.h"
35 #include "BKE_object.h"
36 
37 using Alembic::AbcGeom::FloatArraySamplePtr;
38 using Alembic::AbcGeom::kWrapExisting;
39 using Alembic::AbcGeom::MetaData;
40 using Alembic::AbcGeom::P3fArraySamplePtr;
41 
42 using Alembic::AbcGeom::ICompoundProperty;
43 using Alembic::AbcGeom::INuPatch;
44 using Alembic::AbcGeom::INuPatchSchema;
45 using Alembic::AbcGeom::IObject;
46 
47 namespace blender::io::alembic {
48 
49 AbcNurbsReader::AbcNurbsReader(const IObject &object, ImportSettings &settings)
50  : AbcObjectReader(object, settings)
51 {
52  getNurbsPatches(m_iobject);
53  get_min_max_time(m_iobject, m_schemas[0].first, m_min_time, m_max_time);
54 }
55 
57 {
58  if (m_schemas.empty()) {
59  return false;
60  }
61 
62  std::vector<std::pair<INuPatchSchema, IObject>>::const_iterator it;
63  for (it = m_schemas.begin(); it != m_schemas.end(); ++it) {
64  const INuPatchSchema &schema = it->first;
65 
66  if (!schema.valid()) {
67  return false;
68  }
69  }
70 
71  return true;
72 }
73 
74 static bool set_knots(const FloatArraySamplePtr &knots, float *&nu_knots)
75 {
76  if (!knots || knots->size() < 2) {
77  return false;
78  }
79 
80  /* Skip first and last knots, as they are used for padding. */
81  const size_t num_knots = knots->size() - 2;
82  nu_knots = static_cast<float *>(MEM_callocN(num_knots * sizeof(float), "abc_setsplineknotsu"));
83 
84  for (size_t i = 0; i < num_knots; i++) {
85  nu_knots[i] = (*knots)[i + 1];
86  }
87 
88  return true;
89 }
90 
91 void AbcNurbsReader::readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel)
92 {
93  Curve *cu = static_cast<Curve *>(BKE_curve_add(bmain, "abc_curve", OB_SURF));
94  cu->actvert = CU_ACT_NONE;
95 
96  std::vector<std::pair<INuPatchSchema, IObject>>::iterator it;
97 
98  for (it = m_schemas.begin(); it != m_schemas.end(); ++it) {
99  Nurb *nu = static_cast<Nurb *>(MEM_callocN(sizeof(Nurb), "abc_getnurb"));
100  nu->flag = CU_SMOOTH;
101  nu->type = CU_NURBS;
102  nu->resolu = cu->resolu;
103  nu->resolv = cu->resolv;
104 
105  const INuPatchSchema &schema = it->first;
106  INuPatchSchema::Sample smp;
107  try {
108  smp = schema.getValue(sample_sel);
109  }
110  catch (Alembic::Util::Exception &ex) {
111  printf("Alembic: error reading nurbs sample for '%s/%s' at time %f: %s\n",
112  m_iobject.getFullName().c_str(),
113  schema.getName().c_str(),
114  sample_sel.getRequestedTime(),
115  ex.what());
116  return;
117  }
118 
119  nu->orderu = smp.getUOrder() - 1;
120  nu->orderv = smp.getVOrder() - 1;
121  nu->pntsu = smp.getNumU();
122  nu->pntsv = smp.getNumV();
123 
124  /* Read positions and weights. */
125 
126  const P3fArraySamplePtr positions = smp.getPositions();
127  const FloatArraySamplePtr weights = smp.getPositionWeights();
128 
129  const size_t num_points = positions->size();
130 
131  nu->bp = static_cast<BPoint *>(MEM_callocN(num_points * sizeof(BPoint), "abc_setsplinetype"));
132 
133  BPoint *bp = nu->bp;
134  float posw_in = 1.0f;
135 
136  for (int i = 0; i < num_points; i++, bp++) {
137  const Imath::V3f &pos_in = (*positions)[i];
138 
139  if (weights) {
140  posw_in = (*weights)[i];
141  }
142 
143  copy_zup_from_yup(bp->vec, pos_in.getValue());
144  bp->vec[3] = posw_in;
145  bp->f1 = SELECT;
146  bp->radius = 1.0f;
147  bp->weight = 1.0f;
148  }
149 
150  /* Read knots. */
151 
152  if (!set_knots(smp.getUKnot(), nu->knotsu)) {
154  }
155 
156  if (!set_knots(smp.getVKnot(), nu->knotsv)) {
158  }
159 
160  /* Read flags. */
161 
162  ICompoundProperty user_props = schema.getUserProperties();
163 
164  if (has_property(user_props, "enpoint_u")) {
165  nu->flagu |= CU_NURB_ENDPOINT;
166  }
167 
168  if (has_property(user_props, "enpoint_v")) {
169  nu->flagv |= CU_NURB_ENDPOINT;
170  }
171 
172  if (has_property(user_props, "cyclic_u")) {
173  nu->flagu |= CU_NURB_CYCLIC;
174  }
175 
176  if (has_property(user_props, "cyclic_v")) {
177  nu->flagv |= CU_NURB_CYCLIC;
178  }
179 
181  }
182 
183  BLI_strncpy(cu->id.name + 2, m_data_name.c_str(), m_data_name.size() + 1);
184 
186  m_object->data = cu;
187 }
188 
189 void AbcNurbsReader::getNurbsPatches(const IObject &obj)
190 {
191  if (!obj.valid()) {
192  return;
193  }
194 
195  const int num_children = obj.getNumChildren();
196 
197  if (num_children == 0) {
198  INuPatch abc_nurb(obj, kWrapExisting);
199  INuPatchSchema schem = abc_nurb.getSchema();
200  m_schemas.emplace_back(schem, obj);
201  return;
202  }
203 
204  for (int i = 0; i < num_children; i++) {
205  bool ok = true;
206  IObject child(obj, obj.getChildHeader(i).getName());
207 
208  if (!m_name.empty() && child.valid() && !begins_with(child.getFullName(), m_name)) {
209  ok = false;
210  }
211 
212  if (!child.valid()) {
213  continue;
214  }
215 
216  const MetaData &md = child.getMetaData();
217 
218  if (INuPatch::matches(md) && ok) {
219  INuPatch abc_nurb(child, kWrapExisting);
220  INuPatchSchema schem = abc_nurb.getSchema();
221  m_schemas.emplace_back(schem, child);
222  }
223 
224  getNurbsPatches(child);
225  }
226 }
227 
228 } // namespace blender::io::alembic
void BKE_nurb_knot_calc_v(struct Nurb *nu)
Definition: curve.c:1308
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
ListBase * BKE_curve_nurbs_get(struct Curve *cu)
Definition: curve.c:5079
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
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:110
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL()
Definition: string.c:108
@ CU_NURBS
@ CU_SMOOTH
#define CU_ACT_NONE
@ CU_NURB_CYCLIC
@ CU_NURB_ENDPOINT
Object is a sort of wrapper for general info.
@ OB_SURF
Read Guarded memory(de)allocation.
AbcNurbsReader(const Alembic::Abc::IObject &object, ImportSettings &settings)
void readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel)
#define SELECT
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
bool begins_with(const TContainer &input, const TContainer &match)
Definition: abc_util.h:56
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])
bool has_property(const Alembic::Abc::ICompoundProperty &prop, const std::string &name)
Definition: abc_util.cc:129
static bool set_knots(const FloatArraySamplePtr &knots, float *&nu_knots)
float weight
uint8_t f1
float vec[4]
float radius
short resolv
short resolu
char name[66]
Definition: DNA_ID.h:283
Definition: BKE_main.h:116
short flagu
short orderu
short orderv
float * knotsu
short flag
short type
float * knotsv
BPoint * bp
short resolu
short resolv
short flagv
void * data