Blender V5.3
abc_reader_points.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2016 Kévin Dietrich. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "abc_reader_points.h"
10#include "abc_axis_conversion.h"
11#include "abc_util.h"
12
13#include "DNA_object_types.h"
15
16#include "BLT_translation.hh"
17
18#include "BKE_geometry_set.hh"
19#include "BKE_object.hh"
20#include "BKE_pointcloud.hh"
21
22#include "BLI_color_types.hh"
23
24#include "IO_validate.hh"
25
26#include "CLG_log.h"
27
28#include <algorithm>
29
30namespace blender {
31
32using namespace Alembic::AbcGeom;
33
34namespace io::alembic {
35
36static CLG_LogRef LOG = {"io.alembic"};
37
38/* Specialization of #has_animations() as defined in abc_reader_object.h. */
39template<> bool has_animations(Alembic::AbcGeom::IPointsSchema &schema, ImportSettings *settings)
40{
41 if (settings->is_sequence || !schema.isConstant()) {
42 return true;
43 }
44
45 IFloatGeomParam widths_param = schema.getWidthsParam();
46 if (widths_param.valid() && !widths_param.isConstant()) {
47 return true;
48 }
49
50 IV3fArrayProperty velocities_prop = schema.getVelocitiesProperty();
51 if (velocities_prop.valid() && !velocities_prop.isConstant()) {
52 return true;
53 }
54
55 return false;
56}
57
59{
60 IPoints ipoints(m_iobject, kWrapExisting);
61 m_schema = ipoints.getSchema();
62}
63
65{
66 return m_schema.valid();
67}
68
70 const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header,
71 const Object *const ob,
72 const char **r_err_str) const
73{
74 if (!Alembic::AbcGeom::IPoints::matches(alembic_header)) {
75 *r_err_str = RPT_(
76 "Object type mismatch, Alembic object path pointed to Points when importing, but not any "
77 "more");
78 return false;
79 }
80
81 if (ob->type != OB_POINTCLOUD) {
82 *r_err_str = RPT_("Object type mismatch, Alembic object path points to Points.");
83 return false;
84 }
85
86 return true;
87}
88
89void AbcPointsReader::readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel)
90{
91 PointCloud *pointcloud = BKE_pointcloud_add(bmain, m_data_name.c_str());
92
95 AbcReadGeometryParams read_params{};
96 read_geometry(geometry_set, sample_sel, read_params, nullptr);
97
98 PointCloud *read_pointcloud =
99 geometry_set.get_component_for_write<bke::PointCloudComponent>().release();
100
101 if (read_pointcloud != pointcloud) {
102 BKE_pointcloud_nomain_to_pointcloud(read_pointcloud, pointcloud);
103 }
104
106 m_object->data = id_cast<ID *>(pointcloud);
107
110 }
111}
112
113static void read_points(const P3fArraySamplePtr positions, MutableSpan<float3> r_points)
114{
115 for (size_t i = 0; i < positions->size(); i++) {
116 copy_zup_from_yup(r_points[i], (*positions)[i].getValue());
117 }
118}
119
120static void read_points_sample(const IPointsSchema &schema,
121 const ISampleSelector &selector,
122 MutableSpan<float3> r_points)
123{
124 Alembic::AbcGeom::IPointsSchema::Sample sample = schema.getValue(selector);
125
126 const P3fArraySamplePtr &positions = sample.getPositions();
127 read_points(positions, r_points);
128}
129
130template<typename TOut, typename TIn> static TOut convert_abc_value(const TIn &in)
131{
132 static_assert(std::is_same_v<TIn, TOut>,
133 "convert_abc_value needs to be explicitly specialized for each pair of types");
134 return in;
135}
136
137template<> float3 convert_abc_value(const V3f &in)
138{
139 float3 out;
140 copy_zup_from_yup(out, in.getValue());
141 return out;
142}
143
145{
146 return ColorGeometry4f(in[0], in[1], in[2], 1.0f);
147}
148
149template<> float2 convert_abc_value(const V2f &in)
150{
151 return in.getValue();
152}
153
154template<typename TArrayProperty, typename TWriteValue>
155static void read_typed_property_sample(const ICompoundProperty &parent,
156 const ISampleSelector &selector,
157 const std::string &name,
158 bke::MutableAttributeAccessor &attribute_accessor)
159{
160 const TArrayProperty &array_prop = TArrayProperty(parent, name);
161 if (array_prop) {
162 using SamplePtr = typename TArrayProperty::sample_ptr_type;
163 using ValueType = typename TArrayProperty::value_type;
164
165 const SamplePtr sample_ptr = array_prop.getValue(selector);
167 attribute_accessor.lookup_or_add_for_write_span<TWriteValue>(name, bke::AttrDomain::Point);
168 MutableSpan<TWriteValue> span = writer.span;
169 for (const int64_t i : IndexRange(std::min(span.size(), int64_t(sample_ptr->size())))) {
170 ValueType value = (*sample_ptr)[i];
171 span[i] = convert_abc_value<TWriteValue>(value);
172 }
173 writer.finish();
174 }
175}
176
177static void read_point_arb_geom_params(const IPointsSchema &schema,
178 const ISampleSelector &selector,
179 bke::MutableAttributeAccessor &attribute_accessor)
180{
181 const ICompoundProperty prop = schema.getArbGeomParams();
182 if (!prop.valid()) {
183 return;
184 }
185
186 for (size_t i = 0; i < prop.getNumProperties(); i++) {
187 const PropertyHeader header = prop.getPropertyHeader(i);
188 const PropertyType property_type = header.getPropertyType();
189 if (property_type != kArrayProperty) {
190 // currently unsupported
191 continue;
192 }
193
194 const DataType data_type = header.getDataType();
195 const MetaData metadata = header.getMetaData();
196 const std::string interpretation = metadata.get("interpretation");
197 const std::string name = header.getName();
198
199 if (data_type == DataType(kFloat32POD, 3)) {
200 if (interpretation == C3fTPTraits::interpretation()) {
202 prop, selector, name, attribute_accessor);
203 }
204 else if (interpretation == N3fTPTraits::interpretation()) {
206 prop, selector, name, attribute_accessor);
207 }
208 else {
210 prop, selector, name, attribute_accessor);
211 }
212 }
213 else if (data_type == DataType(kFloat32POD, 2)) {
215 prop, selector, name, attribute_accessor);
216 }
217 else if (data_type == DataType(kFloat32POD, 1)) {
219 prop, selector, name, attribute_accessor);
220 }
221 }
222}
223
225 const Alembic::Abc::ISampleSelector &sample_sel,
226 const AbcReadGeometryParams &read_params,
227 const char **r_err_str)
228{
229 BLI_assert(geometry_set.has_pointcloud());
230
231 IPointsSchema::Sample sample;
232 try {
233 sample = m_schema.getValue(sample_sel);
234 }
235 catch (Alembic::Util::Exception &ex) {
236 *r_err_str = RPT_("Error reading points sample; more detail on the console");
237 CLOG_WARN(&LOG,
238 "Error reading points sample for '%s/%s' at time %f: %s",
239 m_iobject.getFullName().c_str(),
240 m_schema.getName().c_str(),
241 sample_sel.getRequestedTime(),
242 ex.what());
243 return;
244 }
245
246 PointCloud *existing_pointcloud = geometry_set.get_pointcloud_for_write();
247 PointCloud *pointcloud = existing_pointcloud;
248
249 const P3fArraySamplePtr &positions = sample.getPositions();
250
251 const IFloatGeomParam widths_param = m_schema.getWidthsParam();
252 FloatArraySamplePtr widths;
253
254 if (widths_param.valid()) {
255 IFloatGeomParam::Sample wsample = widths_param.getExpandedValue(sample_sel);
256 widths = wsample.getVals();
257 }
258
259 if (!validate::size_fits_in_int(positions->size())) {
260 CLOG_WARN(&LOG,
261 "Point cloud too large to import for '%s/%s' at time %f, exceeds max int size",
262 m_iobject.getFullName().c_str(),
263 m_schema.getName().c_str(),
264 sample_sel.getRequestedTime());
265 return;
266 }
267
268 if (pointcloud->totpoint != positions->size()) {
269 pointcloud = BKE_pointcloud_new_nomain(PointCloudType::Points, positions->size());
270 }
271
272 bke::MutableAttributeAccessor attribute_accessor = pointcloud->attributes_for_write();
273
274 MutableSpan<float3> point_positions = pointcloud->positions_for_write();
275 read_points_sample(m_schema, sample_sel, point_positions);
276
277 bool widths_handled = false;
278 if (widths) {
279 switch (widths_param.getScope()) {
280 case Alembic::AbcGeom::kConstantScope: {
281 if (widths->size() >= 1) {
282 const float radius = (*widths)[0] / 2.0f;
283 attribute_accessor.remove("radius");
284 attribute_accessor.add<float>(
286 widths_handled = true;
287 }
288 break;
289 }
290 case Alembic::AbcGeom::kUniformScope:
291 case Alembic::AbcGeom::kVaryingScope:
292 case Alembic::AbcGeom::kVertexScope:
293 case Alembic::AbcGeom::kFacevaryingScope: {
294 MutableSpan<float> point_radii = pointcloud->radius_for_write();
295 for (const int64_t i : IndexRange(std::min(point_radii.size(), int64_t(widths->size())))) {
296 point_radii[i] = (*widths)[i] / 2.0f;
297 }
298 widths_handled = true;
299 break;
300 }
301 case Alembic::AbcGeom::kUnknownScope:
302 break;
303 }
304 }
305
306 if (!widths_handled) {
307 attribute_accessor.remove("radius");
308 attribute_accessor.add<float>(
310 }
311
312 read_point_arb_geom_params(m_schema, sample_sel, attribute_accessor);
313
314 if (!read_params.velocity_name.empty() && read_params.velocity_scale != 0.0f) {
315 V3fArraySamplePtr velocities = get_velocity_prop(
316 m_schema, sample_sel, read_params.velocity_name);
317 if (velocities && pointcloud->totpoint == int(velocities->size())) {
318 bke::SpanAttributeWriter<float3> velocity_writer =
319 attribute_accessor.lookup_or_add_for_write_span<float3>("velocity",
321 MutableSpan<float3> point_velocity = velocity_writer.span;
322 for (const int64_t i :
323 IndexRange(std::min(point_velocity.size(), int64_t(velocities->size()))))
324 {
325 const Imath::V3f &vel_in = (*velocities)[i];
326 copy_zup_from_yup(point_velocity[i], vel_in.getValue());
327 point_velocity[i] *= read_params.velocity_scale;
328 }
329 velocity_writer.finish();
330 }
331 }
332
333 geometry_set.replace_pointcloud(pointcloud);
334}
335
336} // namespace io::alembic
337} // namespace blender
General operations, lookup, etc. for blender objects.
General operations for point clouds.
#define RPT_(msgid)
#define CLOG_WARN(clg_ref,...)
Definition CLG_log.h:189
Object is a sort of wrapper for general info.
constexpr IndexRange()=default
constexpr int64_t size() const
Definition BLI_span.hh:499
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
GSpanAttributeWriter lookup_or_add_for_write_span(StringRef name, AttrDomain domain, AttrType data_type, const AttributeInit &initializer=AttributeInitDefaultValue())
AbcObjectReader(const AbcReaderConstructorArgs &args)
void read_geometry(bke::GeometrySet &geometry_set, const Alembic::Abc::ISampleSelector &sample_sel, const AbcReadGeometryParams &read_params, const char **r_err_str) override
bool accepts_object_type(const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header, const Object *const ob, const char **r_err_str) const override
AbcPointsReader(const AbcReaderConstructorArgs &args)
void readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel) override
static void read_typed_property_sample(const ICompoundProperty &parent, const ISampleSelector &selector, const std::string &name, bke::MutableAttributeAccessor &attribute_accessor)
static void read_points(const P3fArraySamplePtr positions, MutableSpan< float3 > r_points)
BLI_INLINE void copy_zup_from_yup(float zup[3], const float yup[3])
V3fArraySamplePtr get_velocity_prop(const Alembic::Abc::ICompoundProperty &schema, const Alembic::AbcGeom::ISampleSelector &selector, const std::string &name)
Definition abc_util.cc:91
static void read_point_arb_geom_params(const IPointsSchema &schema, const ISampleSelector &selector, bke::MutableAttributeAccessor &attribute_accessor)
bool has_animations(Alembic::AbcGeom::ICurvesSchema &schema, ImportSettings *settings)
static void read_points_sample(const IPointsSchema &schema, const ISampleSelector &selector, MutableSpan< float3 > r_points)
static TOut convert_abc_value(const TIn &in)
static CLG_LogRef LOG
bool size_fits_in_int(const int64_t size)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
void BKE_pointcloud_nomain_to_pointcloud(PointCloud *pointcloud_src, PointCloud *pointcloud_dst)
VecBase< float, 2 > float2
PointCloud * BKE_pointcloud_add(Main *bmain, const char *name)
static CLG_LogRef LOG
PointCloud * BKE_pointcloud_new_nomain(PointCloudType type, int totpoint)
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
VecBase< float, 3 > float3
Object * BKE_object_add_only_object(Main *bmain, ObjectType type, const char *name) ATTR_RETURNS_NONNULL
Definition object.cc:2265
GeometryComponent & get_component_for_write(GeometryComponent::Type component_type)
PointCloud * get_pointcloud_for_write()
void replace_pointcloud(PointCloud *pointcloud, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
static GeometrySet from_pointcloud(PointCloud *pointcloud, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)