9#ifdef MAYAFLUX_PLATFORM_WINDOWS
13#include <Alembic/Abc/All.h>
14#include <Alembic/AbcCoreOgawa/All.h>
15#include <Alembic/AbcGeom/All.h>
17#ifdef MAYAFLUX_PLATFORM_WINDOWS
27 using namespace Alembic::AbcGeom;
34 return kConstantScope;
40 return kConstantScope;
52 return topology == PrimitiveTopology::LINE_LIST
53 || topology == PrimitiveTopology::LINE_STRIP;
57 struct OFloat4GeomParam {
58 OFloatGeomParam param;
61 using GeomParamVariant = std::variant<
62 OFloatGeomParam, OV2fGeomParam, OV3fGeomParam, OC3fGeomParam, ON3fGeomParam, OUInt32GeomParam,
63 OC4fGeomParam, OFloat4GeomParam>;
67 std::unordered_map<std::string, GeomParamVariant> geom_params;
72 std::unordered_map<std::string, GeomParamVariant> geom_params;
97 std::unordered_map<std::string, GeomParamVariant>& params,
98 const OCompoundProperty& arb_params,
100 size_t expected_count)
102 const auto scope = to_alembic_scope(attr.scope);
103 auto it = params.find(attr.name);
105 if (
const auto* floats = std::get_if<std::vector<float>>(&attr.values)) {
106 if (floats->size() != expected_count) {
109 if (it == params.end()) {
110 it = params.emplace(attr.name, OFloatGeomParam(arb_params, attr.name,
false, scope, 1)).first;
112 auto* param = std::get_if<OFloatGeomParam>(&it->second);
116 param->set(OFloatGeomParam::Sample(FloatArraySample(floats->data(), floats->size()), scope));
119 if (
const auto* uvs = std::get_if<std::vector<glm::vec2>>(&attr.values)) {
120 if (uvs->size() != expected_count) {
123 if (it == params.end()) {
124 it = params.emplace(attr.name, OV2fGeomParam(arb_params, attr.name,
false, scope, 1)).first;
126 auto* param = std::get_if<OV2fGeomParam>(&it->second);
130 param->set(OV2fGeomParam::Sample(
131 V2fArraySample(
reinterpret_cast<const V2f*
>(uvs->data()), uvs->size()), scope));
134 if (
const auto* vecs = std::get_if<std::vector<glm::vec3>>(&attr.values)) {
135 if (vecs->size() != expected_count) {
139 if (it == params.end()) {
140 if (attr.name ==
"color") {
141 it = params.emplace(attr.name, OC3fGeomParam(arb_params, attr.name,
false, scope, 1)).first;
142 }
else if (attr.name ==
"normal") {
143 it = params.emplace(attr.name, ON3fGeomParam(arb_params, attr.name,
false, scope, 1)).first;
145 it = params.emplace(attr.name, OV3fGeomParam(arb_params, attr.name,
false, scope, 1)).first;
149 if (
auto* color_param = std::get_if<OC3fGeomParam>(&it->second)) {
150 color_param->set(OC3fGeomParam::Sample(
151 C3fArraySample(
reinterpret_cast<const C3f*
>(vecs->data()), vecs->size()), scope));
154 if (
auto* normal_param = std::get_if<ON3fGeomParam>(&it->second)) {
155 normal_param->set(ON3fGeomParam::Sample(
156 N3fArraySample(
reinterpret_cast<const N3f*
>(vecs->data()), vecs->size()), scope));
159 if (
auto* vector_param = std::get_if<OV3fGeomParam>(&it->second)) {
160 vector_param->set(OV3fGeomParam::Sample(
161 V3fArraySample(
reinterpret_cast<const V3f*
>(vecs->data()), vecs->size()), scope));
166 if (
const auto* quads = std::get_if<std::vector<glm::vec4>>(&attr.values)) {
167 if (quads->size() != expected_count) {
171 if (it == params.end()) {
172 if (attr.name ==
"color") {
173 it = params.emplace(attr.name, OC4fGeomParam(arb_params, attr.name,
false, scope, 1)).first;
175 it = params.emplace(attr.name,
176 OFloat4GeomParam { OFloatGeomParam(arb_params, attr.name, false, scope, 4) })
181 if (
auto* color_param = std::get_if<OC4fGeomParam>(&it->second)) {
182 color_param->set(OC4fGeomParam::Sample(
183 C4fArraySample(
reinterpret_cast<const C4f*
>(quads->data()), quads->size()), scope));
186 if (
auto* vector_param = std::get_if<OFloat4GeomParam>(&it->second)) {
187 vector_param->param.set(OFloatGeomParam::Sample(
188 FloatArraySample(
reinterpret_cast<const float*
>(quads->data()), quads->size() * 4), scope));
193 if (
const auto* uints = std::get_if<std::vector<uint32_t>>(&attr.values)) {
194 if (uints->size() != expected_count) {
197 if (it == params.end()) {
198 it = params.emplace(attr.name, OUInt32GeomParam(arb_params, attr.name,
false, scope, 1)).first;
200 auto* param = std::get_if<OUInt32GeomParam>(&it->second);
204 param->set(OUInt32GeomParam::Sample(UInt32ArraySample(uints->data(), uints->size()), scope));
210 size_t vertex_attribute_count(
SpatialScope scope,
size_t point_count)
217 size_t curves_attribute_count(
SpatialScope scope,
size_t vertex_count,
size_t curve_count)
260 m_impl->archive.emplace(Alembic::AbcCoreOgawa::WriteArchive(), resolved);
261 }
catch (
const std::exception& e) {
262 set_error(std::string(
"open: ") + e.what());
264 "SpatialCache::open: failed for '{}': {}", resolved, e.what());
269 "SpatialCache: opened '{}'", resolved);
275 auto it =
m_impl->vertex_streams.find(stream_name);
276 if (it ==
m_impl->vertex_streams.end()) {
278 auto mit =
m_impl->pending_metadata.find(stream_name);
279 if (mit !=
m_impl->pending_metadata.end()) {
281 m_impl->pending_metadata.erase(mit);
283 auto stream = std::make_unique<VertexStream>();
284 stream->object = OPoints(
m_impl->archive->getTop(), stream_name, md);
285 it =
m_impl->vertex_streams.emplace(stream_name, std::move(stream)).first;
287 auto& stream = *it->second;
288 auto& schema = stream.object.getSchema();
290 const P3fArraySample pos_sample(
292 const V3fArraySample vel_sample = sample.
velocities.empty()
294 : V3fArraySample(
reinterpret_cast<const V3f*
>(sample.
velocities.data()), sample.
velocities.size());
296 if (sample.
ids.empty()) {
297 schema.set(OPointsSchema::Sample(pos_sample, vel_sample));
299 const UInt64ArraySample id_sample(sample.
ids.data(), sample.
ids.size());
300 schema.set(OPointsSchema::Sample(pos_sample, id_sample, vel_sample));
304 const size_t expected = vertex_attribute_count(attr.scope, sample.
positions.size());
305 if (!set_attribute(stream.geom_params, schema.getArbGeomParams(), attr, expected)) {
306 set_error(
"write: attribute '" + attr.name +
"' has an unsupported type, wrong "
307 +
"element count, or was redeclared with a different type");
319 const size_t expected_vertex_count = std::accumulate(
321 [](
size_t acc, int32_t n) { return acc + static_cast<size_t>(n); });
322 if (expected_vertex_count != sample.
positions.size()) {
323 set_error(
"write: vertex_counts_per_curve sum does not match positions.size()");
329 auto it =
m_impl->curves_streams.find(stream_name);
330 if (it ==
m_impl->curves_streams.end()) {
332 auto mit =
m_impl->pending_metadata.find(stream_name);
333 if (mit !=
m_impl->pending_metadata.end()) {
335 m_impl->pending_metadata.erase(mit);
337 auto stream = std::make_unique<CurvesStream>();
338 stream->object = OCurves(
m_impl->archive->getTop(), stream_name, md);
339 it =
m_impl->curves_streams.emplace(stream_name, std::move(stream)).first;
341 auto& stream = *it->second;
342 auto& schema = stream.object.getSchema();
344 const P3fArraySample pos_sample(
346 const Int32ArraySample nverts_sample(
349 schema.set(OCurvesSchema::Sample(pos_sample, nverts_sample, kLinear, kNonPeriodic));
352 const size_t expected = curves_attribute_count(
354 if (!set_attribute(stream.geom_params, schema.getArbGeomParams(), attr, expected)) {
355 set_error(
"write: attribute '" + attr.name +
"' has an unsupported type, wrong "
356 +
"element count, or was redeclared with a different type");
368 std::lock_guard lock(
m_impl->mutex);
379 if (uses_curve_schema(sample.
topology)) {
381 set_error(
"write: ids/velocities must be empty for a curve topology");
385 set_error(
"write: vertex_counts_per_curve is required for a curve topology");
391 if (sample.
topology == PrimitiveTopology::POINT_LIST) {
393 set_error(
"write: vertex_counts_per_curve must be empty for POINT_LIST");
399 set_error(
"write: unsupported topology; SpatialCache only writes "
400 "POINT_LIST, LINE_LIST, LINE_STRIP");
406 const std::string& stream_name,
407 const std::unordered_map<std::string, std::string>& tags)
409 std::lock_guard lock(
m_impl->mutex);
411 if (
m_impl->vertex_streams.contains(stream_name) ||
m_impl->curves_streams.contains(stream_name)) {
412 set_error(
"write_metadata: stream '" + stream_name
413 +
"' already created; call write_metadata before its first write() call");
418 auto& md =
m_impl->pending_metadata[stream_name];
419 for (
const auto& [key, value] : tags) {
427 std::lock_guard lock(
m_impl->mutex);
433 m_impl->vertex_streams.clear();
434 m_impl->curves_streams.clear();
435 m_impl->pending_metadata.clear();
#define MF_INFO(comp, ctx,...)
#define MF_ERROR(comp, ctx,...)
std::unique_ptr< Impl > m_impl
bool write_metadata(const std::string &stream_name, const std::unordered_map< std::string, std::string > &tags)
Queue object-level metadata tags for a named stream.
bool write_curves_sample(const std::string &stream_name, const SpatialSample &sample)
bool write_vertex_sample(const std::string &stream_name, const SpatialSample &sample)
std::string get_last_error() const
bool open(const std::string &filepath)
Open an Alembic archive for writing, Ogawa backend.
void set_error(std::string msg) const
bool write(const std::string &stream_name, const SpatialSample &sample)
Append one sample to a named stream.
void close()
Finalize and close the archive.
Alembic-backed writer for time-sampled spatial entity state: particle systems, point clouds,...
SpatialScope
How many elements one attribute contributes per sample.
PrimitiveTopology
Vertex assembly primitive topology.
std::string resolve_write_path(const std::string &filepath, std::string_view default_directory=Config::OUT_DIR)
Resolve a writable path against the configured output directory.
@ FileIO
Filesystem I/O operations.
@ IO
Networking, file handling, streaming.
PrimitiveTopology
Vertex assembly primitive topology.
One named, scoped channel of already-typed values.
std::unordered_map< std::string, MetaData > pending_metadata
std::unordered_map< std::string, std::unique_ptr< CurvesStream > > curves_streams
std::mutex mutex
Guards the members above: write() and close() can run on different threads.
std::optional< OArchive > archive
std::unordered_map< std::string, std::unique_ptr< VertexStream > > vertex_streams
std::span< const glm::vec3 > positions
std::span< const glm::vec3 > velocities
std::span< const SpatialAttribute > attributes
Portal::Graphics::PrimitiveTopology topology
std::span< const int32_t > vertex_counts_per_curve
std::span< const uint64_t > ids
One sample's worth of data for one named stream.