Blender V5.3
abc_util.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "abc_util.h"
10
11#include "abc_reader_camera.h"
12#include "abc_reader_curves.h"
13#include "abc_reader_mesh.h"
14#include "abc_reader_points.h"
16
17#include <Alembic/AbcGeom/ILight.h>
18#include <Alembic/AbcGeom/INuPatch.h>
19#include <Alembic/AbcMaterial/IMaterial.h>
20
21#include <algorithm>
22
23namespace blender {
24
25using Alembic::Abc::IV3fArrayProperty;
26using Alembic::Abc::PropertyHeader;
27using Alembic::Abc::V3fArraySamplePtr;
28
29namespace io::alembic {
30
32{
33 return name == propNameOriginalCoordinates;
34}
35
36std::string get_valid_abc_name(const char *name)
37{
38 std::string abc_name(name);
39 std::replace(abc_name.begin(), abc_name.end(), ' ', '_');
40 std::replace(abc_name.begin(), abc_name.end(), '.', '_');
41 std::replace(abc_name.begin(), abc_name.end(), ':', '_');
42 std::replace(abc_name.begin(), abc_name.end(), '/', '_');
43 return abc_name;
44}
45
46Imath::M44d convert_matrix_datatype(const float mat[4][4])
47{
48 Imath::M44d m;
49
50 for (int i = 0; i < 4; i++) {
51 for (int j = 0; j < 4; j++) {
52 m[i][j] = double(mat[i][j]);
53 }
54 }
55
56 return m;
57}
58
59void convert_matrix_datatype(const Imath::M44d &xform, float r_mat[4][4])
60{
61 for (int i = 0; i < 4; i++) {
62 for (int j = 0; j < 4; j++) {
63 r_mat[i][j] = float(xform[i][j]);
64 }
65 }
66}
67
68void split(const std::string &s, const char delim, std::vector<std::string> &tokens)
69{
70 tokens.clear();
71
72 std::stringstream ss(s);
73 std::string item;
74
75 while (std::getline(ss, item, delim)) {
76 if (!item.empty()) {
77 tokens.push_back(item);
78 }
79 }
80}
81
82bool has_property(const Alembic::Abc::ICompoundProperty &prop, const std::string &name)
83{
84 if (!prop.valid()) {
85 return false;
86 }
87
88 return prop.getPropertyHeader(name) != nullptr;
89}
90
91V3fArraySamplePtr get_velocity_prop(const Alembic::Abc::ICompoundProperty &schema,
92 const Alembic::AbcGeom::ISampleSelector &selector,
93 const std::string &name)
94{
95 for (size_t i = 0; i < schema.getNumProperties(); i++) {
96 const PropertyHeader &header = schema.getPropertyHeader(i);
97
98 if (header.isCompound()) {
99 const Alembic::Abc::ICompoundProperty &prop = Alembic::Abc::ICompoundProperty(
100 schema, header.getName());
101
102 if (has_property(prop, name)) {
103 /* Header cannot be null here, as its presence is checked via has_property, so it is safe
104 * to dereference. */
105 const PropertyHeader *header = prop.getPropertyHeader(name);
106 if (!IV3fArrayProperty::matches(*header)) {
107 continue;
108 }
109
110 const IV3fArrayProperty &velocity_prop = IV3fArrayProperty(prop, name, 0);
111 if (velocity_prop) {
112 return velocity_prop.getValue(selector);
113 }
114 }
115 }
116 else if (header.isArray()) {
117 if (header.getName() == name && IV3fArrayProperty::matches(header)) {
118 const IV3fArrayProperty &velocity_prop = IV3fArrayProperty(schema, name, 0);
119 return velocity_prop.getValue(selector);
120 }
121 }
122 }
123
124 return V3fArraySamplePtr();
125}
126
127using index_time_pair_t = std::pair<Alembic::AbcCoreAbstract::index_t, Alembic::AbcGeom::chrono_t>;
128
129std::optional<SampleInterpolationSettings> get_sample_interpolation_settings(
130 const Alembic::AbcGeom::ISampleSelector &selector,
131 const Alembic::AbcCoreAbstract::TimeSamplingPtr &time_sampling,
132 size_t samples_number)
133{
134 const chrono_t time = selector.getRequestedTime();
135 samples_number = std::max(samples_number, size_t(1));
136
137 index_time_pair_t t0 = time_sampling->getFloorIndex(time, samples_number);
138 Alembic::AbcCoreAbstract::index_t i0 = t0.first;
139
140 if (samples_number == 1 || (fabs(time - t0.second) < 0.0001)) {
141 return {};
142 }
143
144 index_time_pair_t t1 = time_sampling->getCeilIndex(time, samples_number);
145 Alembic::AbcCoreAbstract::index_t i1 = t1.first;
146
147 if (i0 == i1) {
148 return {};
149 }
150
151 const double bias = (time - t0.second) / (t1.second - t0.second);
152
153 if (fabs(1.0 - bias) < 0.0001) {
154 return {};
155 }
156
157 return SampleInterpolationSettings{i0, i1, bias};
158}
159
160// #define USE_NURBS
161
163{
164 AbcObjectReader *reader = nullptr;
165
166 const Alembic::AbcGeom::MetaData &md = args.object.getMetaData();
167
168 if (Alembic::AbcGeom::IXform::matches(md)) {
169 reader = new AbcEmptyReader(args);
170 }
171 else if (Alembic::AbcGeom::IPolyMesh::matches(md)) {
172 reader = new AbcMeshReader(args);
173 }
174 else if (Alembic::AbcGeom::ISubD::matches(md)) {
175 reader = new AbcSubDReader(args);
176 }
177 else if (Alembic::AbcGeom::INuPatch::matches(md)) {
178#ifdef USE_NURBS
179 /* TODO(kevin): importing cyclic NURBS from other software crashes
180 * at the moment. This is due to the fact that NURBS in other
181 * software have duplicated points which causes buffer overflows in
182 * Blender. Need to figure out exactly how these points are
183 * duplicated, in all cases (cyclic U, cyclic V, and cyclic UV).
184 * Until this is fixed, disabling NURBS reading. */
185 reader = new AbcNurbsReader(args);
186#endif
187 }
188 else if (Alembic::AbcGeom::ICamera::matches(md)) {
189 reader = new AbcCameraReader(args);
190 }
191 else if (Alembic::AbcGeom::IPoints::matches(md)) {
192 reader = new AbcPointsReader(args);
193 }
194 else if (Alembic::AbcMaterial::IMaterial::matches(md)) {
195 /* Pass for now. */
196 }
197 else if (Alembic::AbcGeom::ILight::matches(md)) {
198 /* Pass for now. */
199 }
200 else if (Alembic::AbcGeom::IFaceSet::matches(md)) {
201 /* Pass, those are handled in the mesh reader. */
202 }
203 else if (Alembic::AbcGeom::ICurves::matches(md)) {
204 reader = new AbcCurveReader(args);
205 }
206 else {
207 std::cerr << "Alembic: unknown how to handle objects of schema '" << md.get("schemaObjTitle")
208 << "', skipping object '" << args.object.getFullName() << "'" << std::endl;
209 }
210
211 return reader;
212}
213
214} // namespace io::alembic
215} // namespace blender
AbcCameraReader(const AbcReaderConstructorArgs &args)
AbcCurveReader(const AbcReaderConstructorArgs &args)
AbcEmptyReader(const AbcReaderConstructorArgs &args)
AbcMeshReader(const AbcReaderConstructorArgs &args)
AbcNurbsReader(const AbcReaderConstructorArgs &args)
AbcPointsReader(const AbcReaderConstructorArgs &args)
AbcSubDReader(const AbcReaderConstructorArgs &args)
ccl_device_inline dual3 fabs(const ccl_private dual3 &a)
Definition math_dual.h:241
std::string get_valid_abc_name(const char *name)
Definition abc_util.cc:36
V3fArraySamplePtr get_velocity_prop(const Alembic::Abc::ICompoundProperty &schema, const Alembic::AbcGeom::ISampleSelector &selector, const std::string &name)
Definition abc_util.cc:91
AbcObjectReader * create_reader(const AbcReaderConstructorArgs &args)
Definition abc_util.cc:162
bool is_reserved_attribute_name(const StringRefNull name)
Definition abc_util.cc:31
Imath::M44d convert_matrix_datatype(const float mat[4][4])
Definition abc_util.cc:46
std::pair< Alembic::AbcCoreAbstract::index_t, Alembic::AbcGeom::chrono_t > index_time_pair_t
Definition abc_util.cc:127
bool has_property(const Alembic::Abc::ICompoundProperty &prop, const std::string &name)
Definition abc_util.cc:82
std::optional< SampleInterpolationSettings > get_sample_interpolation_settings(const Alembic::AbcGeom::ISampleSelector &selector, const Alembic::AbcCoreAbstract::TimeSamplingPtr &time_sampling, size_t samples_number)
Definition abc_util.cc:129
void split(const std::string &s, const char delim, std::vector< std::string > &tokens)
Definition abc_util.cc:68
BMesh Iterators.
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float