31using Alembic::Abc::FloatArraySamplePtr;
32using Alembic::Abc::Int32ArraySamplePtr;
33using Alembic::Abc::P3fArraySamplePtr;
34using Alembic::Abc::PropertyHeader;
35using Alembic::Abc::UcharArraySamplePtr;
37using Alembic::AbcGeom::CurvePeriodicity;
38using Alembic::AbcGeom::ICompoundProperty;
39using Alembic::AbcGeom::ICurves;
40using Alembic::AbcGeom::ICurvesSchema;
41using Alembic::AbcGeom::IFloatGeomParam;
42using Alembic::AbcGeom::IInt16Property;
43using Alembic::AbcGeom::ISampleSelector;
44using Alembic::AbcGeom::IV3fArrayProperty;
45using Alembic::AbcGeom::kWrapExisting;
59 if (settings->
is_sequence || !schema.isConstant()) {
67 IFloatGeomParam widths_param = schema.getWidthsParam();
68 if (widths_param.valid() && !widths_param.isConstant()) {
72 IV3fArrayProperty velocities_prop = schema.getVelocitiesProperty();
73 if (velocities_prop.valid() && !velocities_prop.isConstant()) {
81 const Alembic::Abc::ISampleSelector &sample_sel)
83 ICompoundProperty user_props = schema.getUserProperties();
88 const PropertyHeader *header = user_props.getPropertyHeader(ABC_CURVE_RESOLUTION_U_PROPNAME);
89 if (!header || !header->isScalar() || !IInt16Property::matches(*header)) {
93 IInt16Property resolu(user_props, header->getName());
94 return resolu.getValue(sample_sel);
98 const UcharArraySamplePtr orders,
99 const size_t curve_index)
101 switch (abc_curve_type) {
102 case Alembic::AbcGeom::kCubic:
104 case Alembic::AbcGeom::kVariableOrder:
105 if (orders && orders->size() > curve_index) {
106 return int16_t((*orders)[curve_index]);
109 case Alembic::AbcGeom::kLinear:
117 if (abc_curve_type == Alembic::AbcGeom::kCubic) {
134 const int start = idx;
135 const int end = idx + num_verts;
138 const int safe_order = order <= num_verts ? order : num_verts;
139 for (
int j = start, k = end - safe_order; j < (start + safe_order); j++, k++) {
140 const Imath::V3f &p1 = (*positions)[j];
141 const Imath::V3f &p2 = (*positions)[k];
151 if (overlap == 0 && num_verts > 2 && (*positions)[start] == (*positions)[end - 1]) {
161 case Alembic::AbcGeom::kNoBasis:
163 case Alembic::AbcGeom::kBezierBasis:
165 case Alembic::AbcGeom::kBsplineBasis:
167 case Alembic::AbcGeom::kCatmullromBasis:
169 case Alembic::AbcGeom::kHermiteBasis:
170 case Alembic::AbcGeom::kPowerBasis:
179 return is_cyclic ? (alembic_count / 3) : ((alembic_count / 3) + 1);
192 if (
curves.offsets() != preprocessed_offsets) {
198template<
typename SampleType>
201 const P3fArraySamplePtr positions =
sample.getPositions();
202 const Int32ArraySamplePtr per_curve_vertices_count =
sample.getCurvesNumVertices();
204 const P3fArraySamplePtr ceil_positions = ceil_sample.getPositions();
205 const Int32ArraySamplePtr ceil_per_curve_vertices_count = ceil_sample.getCurvesNumVertices();
208 const bool different_counters = positions->size() != ceil_positions->size() ||
209 per_curve_vertices_count->size() !=
210 ceil_per_curve_vertices_count->size();
211 if (different_counters) {
216 if (memcmp(per_curve_vertices_count->get(),
217 ceil_per_curve_vertices_count->get(),
218 per_curve_vertices_count->size() *
sizeof(
int)) != 0)
258 FloatArraySamplePtr
radii =
nullptr;
270 const ICurvesSchema &schema,
271 const ISampleSelector sample_sel)
274 ICurvesSchema::Sample smp;
276 smp = schema.getValue(sample_sel);
278 catch (Alembic::Util::Exception &ex) {
280 "Error reading curve sample for '%s/%s' at time %f: %s",
281 iobject_name.
c_str(),
282 schema.getName().c_str(),
283 sample_sel.getRequestedTime(),
290 const Int32ArraySamplePtr per_curve_vertices_count = smp.getCurvesNumVertices();
291 const P3fArraySamplePtr positions = smp.getPositions();
292 const FloatArraySamplePtr weights = smp.getPositionWeights();
293 const CurvePeriodicity periodicity = smp.getWrap();
294 const UcharArraySamplePtr orders = smp.getOrders();
296 if (positions->size() == 0) {
304 "Curves too large to import for '%s/%s' at time %f, exceeds max int size",
305 iobject_name.
c_str(),
306 schema.getName().c_str(),
307 sample_sel.getRequestedTime());
311 const IFloatGeomParam widths_param = schema.getWidthsParam();
312 FloatArraySamplePtr radii;
313 if (widths_param.valid()) {
314 IFloatGeomParam::Sample wsample = widths_param.getExpandedValue(sample_sel);
315 radii = wsample.getVals();
318 const int curve_count = per_curve_vertices_count->size();
322 data.offset_in_blender.resize(curve_count + 1);
323 data.offset_in_alembic.resize(curve_count + 1);
324 data.curves_cyclic.resize(curve_count);
327 data.do_cyclic = periodicity == Alembic::AbcGeom::kPeriodic;
332 if (smp.getType() == Alembic::AbcGeom::kVariableOrder && !orders) {
335 data.do_cyclic =
false;
339 data.curves_orders.resize(curve_count);
344 const int positions_size = positions->size();
345 int blender_offset = 0;
346 int alembic_offset = 0;
347 for (
size_t i = 0; i < curve_count; i++) {
348 int vertices_count = (*per_curve_vertices_count)[i];
351 if (vertices_count < 0 || vertices_count > positions_size - alembic_offset) {
352 vertices_count = std::max(0, positions_size - alembic_offset);
357 data.offset_in_blender[i] = blender_offset;
358 data.offset_in_alembic[i] = alembic_offset;
359 data.curves_cyclic[i] =
data.do_cyclic;
362 data.curves_orders[i] = curve_order;
367 const int overlap =
data.do_cyclic ?
369 positions, alembic_offset, vertices_count, curve_order) :
376 blender_offset += (overlap >= vertices_count) ? vertices_count : (vertices_count - overlap);
379 alembic_offset += vertices_count;
381 data.offset_in_blender[curve_count] = blender_offset;
382 data.offset_in_alembic[curve_count] = alembic_offset;
386 data.positions = positions;
388 if (weights && weights->size() > 1) {
389 data.weights = weights;
392 if (radii && radii->size() >= 1) {
393 data.radii_scope = widths_param.getScope();
397 const std::optional<SampleInterpolationSettings> interpolation_settings =
399 sample_sel, schema.getTimeSampling(), schema.getNumSamples());
402 if (use_interpolation && interpolation_settings.has_value()) {
403 Alembic::AbcGeom::ICurvesSchema::Sample ceil_smp;
404 schema.get(ceil_smp, Alembic::Abc::ISampleSelector(interpolation_settings->ceil_index));
406 data.ceil_positions = ceil_smp.getPositions();
407 data.interpolation_settings = interpolation_settings;
420 ICurves abc_curves(
m_iobject, kWrapExisting);
421 m_curves_schema = abc_curves.getSchema();
426 return m_curves_schema.valid();
430 const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header,
432 const char **r_err_str)
const
434 if (!Alembic::AbcGeom::ICurves::matches(alembic_header)) {
436 "Object type mismatch, Alembic object path pointed to Curves when importing, but not "
442 *r_err_str =
RPT_(
"Object type mismatch, Alembic object path points to Curves.");
470 const Imath::V3f &floor_pos = floor_positions[i];
471 const Imath::V3f &ceil_pos = ceil_positions[i];
473 interp_v3_v3v3(p, floor_pos.getValue(), ceil_pos.getValue(), weight);
489 handles_right[cp] =
interpolate_to_zup(floor_positions, ceil_positions, offset + 1, weight);
490 handles_left[cp] = 2.0f * positions[cp] - handles_right[cp];
492 else if (offset == floor_positions.
size() - 1) {
493 handles_left[cp] =
interpolate_to_zup(floor_positions, ceil_positions, offset - 1, weight);
494 handles_right[cp] = 2.0f * positions[cp] - handles_left[cp];
497 handles_left[cp] =
interpolate_to_zup(floor_positions, ceil_positions, offset - 1, weight);
498 handles_right[cp] =
interpolate_to_zup(floor_positions, ceil_positions, offset + 1, weight);
504 const ICurvesSchema &schema,
505 const ISampleSelector &sample_sel)
508 m_iobject.getFullName(), read_params, schema, sample_sel);
509 if (!opt_preprocess) {
515 const int point_count =
data.offset_in_blender.last();
516 const int curve_count =
data.offset_in_blender.size() - 1;
521 curves.resize(point_count, curve_count);
522 curves.offsets_for_write().copy_from(
data.offset_in_blender);
529 if (curve_resolution > 0) {
530 curves.resolution_for_write().fill(curve_resolution);
538 float interp_weight = 0.0f;
539 if (
data.interpolation_settings.has_value()) {
540 alembic_points_ceil = {&(*
data.ceil_positions)[0],
int64_t((*
data.ceil_positions).size())};
541 interp_weight =
data.interpolation_settings->weight;
544 alembic_points_ceil = alembic_points;
554 int point_offset = 0;
555 for (
const int i_curve :
curves.curves_range()) {
556 const int alembic_point_offset =
data.offset_in_alembic[i_curve];
557 const int alembic_point_count =
data.offset_in_alembic[i_curve + 1] - alembic_point_offset;
558 const int cp_count =
data.offset_in_blender[i_curve + 1] -
data.offset_in_blender[i_curve];
565 alembic_points.
slice(alembic_point_offset, alembic_point_count),
566 alembic_points_ceil.
slice(alembic_point_offset, alembic_point_count),
567 curves_positions.
slice(point_offset, point_count),
568 handles_left.
slice(point_offset, point_count),
569 handles_right.
slice(point_offset, point_count),
574 point_offset += cp_count;
578 for (
const int i_curve :
curves.curves_range()) {
579 int position_offset =
data.offset_in_alembic[i_curve];
580 for (
const int i_point :
curves.points_by_curve()[i_curve]) {
581 if (
data.interpolation_settings.has_value()) {
583 alembic_points, alembic_points_ceil, position_offset++, interp_weight);
586 curves_positions[i_point] =
to_zup_float3(alembic_points[position_offset++]);
592 if (
data.do_cyclic) {
593 curves.cyclic_for_write().copy_from(
data.curves_cyclic);
597 switch (
data.radii_scope) {
598 case Alembic::AbcGeom::kConstantScope: {
599 const float radius = (*
data.radii)[0] / 2.0f;
601 attribute_accessor.
remove(
"radius");
602 attribute_accessor.
add<
float>(
606 case Alembic::AbcGeom::kVertexScope:
607 case Alembic::AbcGeom::kVaryingScope:
608 case Alembic::AbcGeom::kFacevaryingScope: {
611 Alembic::Abc::FloatArraySample alembic_widths = *
data.radii;
613 radii[i_point] = alembic_widths[i_point] / 2.0f;
617 case Alembic::AbcGeom::kUniformScope: {
620 Alembic::Abc::FloatArraySample alembic_widths = *
data.radii;
623 for (
const int i_point :
curves.points_by_curve()[i_curve]) {
624 radii[i_point] = alembic_widths[i_curve] / 2.0f;
629 case Alembic::AbcGeom::kUnknownScope:
635 curves.nurbs_orders_for_write().copy_from(
data.curves_orders);
636 curves.nurbs_knots_modes_for_write().fill(
data.knot_mode);
641 for (
const int i_curve :
curves.curves_range()) {
642 const int alembic_offset =
data.offset_in_alembic[i_curve];
645 data_weights_span.
slice(alembic_offset, points.
size()));
650 if (
data.velocities &&
data.velocities->size() ==
curves.point_num) {
656 const Imath::V3f &vel_in = (*
data.velocities)[i];
665 const Alembic::Abc::ISampleSelector &sample_sel,
Low-level operations for curves.
General operations, lookup, etc. for blender objects.
#define CLOG_WARN(clg_ref,...)
Object is a sort of wrapper for general info.
constexpr int64_t size() const
constexpr IndexRange()=default
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
constexpr void copy_from(Span< T > values) const
constexpr Span slice(int64_t start, int64_t size) const
constexpr int64_t size() const
constexpr const char * c_str() const
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())
bool remove(const StringRef name)
void readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel) override
AbcCurveReader(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 valid() const override
bool accepts_object_type(const Alembic::AbcCoreAbstract::ObjectHeader &alembic_header, const Object *const ob, const char **r_err_str) const override
void read_curves_sample(Curves *curves_id, const AbcReadGeometryParams &read_params, const Alembic::AbcGeom::ICurvesSchema &schema, const Alembic::Abc::ISampleSelector &sample_selector)
std::string m_object_name
Alembic::Abc::IObject m_iobject
AbcObjectReader(const AbcReaderConstructorArgs &args)
ImportSettings * m_settings
static float3 to_zup_float3(Imath::V3f v)
static int16_t get_curve_resolution(const ICurvesSchema &schema, const Alembic::Abc::ISampleSelector &sample_sel)
static void add_bezier_control_point(int cp, int offset, const Span< Imath::V3f > floor_positions, const Span< Imath::V3f > ceil_positions, MutableSpan< float3 > positions, MutableSpan< float3 > handles_left, MutableSpan< float3 > handles_right, float weight)
static bool curves_topology_changed(const bke::CurvesGeometry &curves, Span< int > preprocessed_offsets)
static bool samples_have_same_topology(const SampleType &sample, const SampleType &ceil_sample)
BLI_INLINE void copy_zup_from_yup(float zup[3], const float yup[3])
static IndexRange get_sane_range(IndexRange range, size_t alembic_size)
static int8_t get_knot_mode(const Alembic::AbcGeom::CurveType abc_curve_type)
V3fArraySamplePtr get_velocity_prop(const Alembic::Abc::ICompoundProperty &schema, const Alembic::AbcGeom::ISampleSelector &selector, const std::string &name)
static std::optional< PreprocessedSampleData > preprocess_sample(StringRefNull iobject_name, const AbcReadGeometryParams &read_params, const ICurvesSchema &schema, const ISampleSelector sample_sel)
BLI_INLINE float3 interpolate_to_zup(const Span< Imath::V3f > &floor_positions, const Span< Imath::V3f > &ceil_positions, int i, float weight)
bool has_animations(Alembic::AbcGeom::ICurvesSchema &schema, ImportSettings *settings)
static CurveType get_curve_type(const Alembic::AbcGeom::BasisType basis)
static int16_t get_curve_order(const Alembic::AbcGeom::CurveType abc_curve_type, const UcharArraySamplePtr orders, const size_t curve_index)
std::optional< SampleInterpolationSettings > get_sample_interpolation_settings(const Alembic::AbcGeom::ISampleSelector &selector, const Alembic::AbcCoreAbstract::TimeSamplingPtr &time_sampling, size_t samples_number)
static int bezier_point_count(int alembic_count, bool is_cyclic)
static int get_curve_overlap(const P3fArraySamplePtr positions, const int idx, const int num_verts, const int16_t order)
bool size_fits_in_int(const int64_t size)
struct Curves * BKE_curves_add(struct Main *bmain, const char *name)
ATTR_WARN_UNUSED_RESULT const BMVert * v
MINLINE void mul_v3_fl(float r[3], float f)
@ NURBS_KNOT_MODE_ENDPOINT
BMesh const char void * data
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
@ MOD_MESHSEQ_INTERPOLATE_VERTICES
VecBase< float, 3 > float3
Object * BKE_object_add_only_object(Main *bmain, ObjectType type, const char *name) ATTR_RETURNS_NONNULL
Curves * get_curves_for_write()
MutableVArraySpan< T > span
std::string velocity_name
bool always_add_cache_reader
Vector< int > offset_in_blender
Vector< int > offset_in_alembic
std::optional< SampleInterpolationSettings > interpolation_settings
V3fArraySamplePtr velocities
FloatArraySamplePtr radii
Alembic::AbcGeom::GeometryScope radii_scope
Vector< bool > curves_cyclic
FloatArraySamplePtr weights
P3fArraySamplePtr positions
Vector< int8_t > curves_orders
P3fArraySamplePtr ceil_positions