MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
SpatialCache.cpp
Go to the documentation of this file.
1#include "SpatialCache.hpp"
2
4
6
8
9#ifdef MAYAFLUX_PLATFORM_WINDOWS
10#include <windows.h>
11#endif
12
13#include <Alembic/Abc/All.h>
14#include <Alembic/AbcCoreOgawa/All.h>
15#include <Alembic/AbcGeom/All.h>
16
17#ifdef MAYAFLUX_PLATFORM_WINDOWS
18#ifdef ERROR
19#undef ERROR
20#endif
21#endif // MAYAFLUX_PLATFORM_WINDOWS
22
23namespace MayaFlux::IO {
24
25namespace {
26
27 using namespace Alembic::AbcGeom;
29
30 GeometryScope to_alembic_scope(SpatialScope scope)
31 {
32 switch (scope) {
34 return kConstantScope;
36 return kUniformScope;
38 return kVaryingScope;
39 }
40 return kConstantScope;
41 }
42
43 /**
44 * @brief True for a topology written through Alembic's Curves schema
45 * (grouped into curves via vertex_counts_per_curve). The
46 * remaining supported case, PrimitiveTopology::POINT_LIST, goes
47 * through the flat Points schema instead; every other topology
48 * is rejected by write().
49 */
50 bool uses_curve_schema(PrimitiveTopology topology)
51 {
52 return topology == PrimitiveTopology::LINE_LIST
53 || topology == PrimitiveTopology::LINE_STRIP;
54 }
55
56 /** @brief A float GeomParam declared with array extent 4, kept apart from a scalar float param. */
57 struct OFloat4GeomParam {
58 OFloatGeomParam param;
59 };
60
61 using GeomParamVariant = std::variant<
62 OFloatGeomParam, OV2fGeomParam, OV3fGeomParam, OC3fGeomParam, ON3fGeomParam, OUInt32GeomParam,
63 OC4fGeomParam, OFloat4GeomParam>;
64
65 struct VertexStream {
66 OPoints object;
67 std::unordered_map<std::string, GeomParamVariant> geom_params;
68 };
69
70 struct CurvesStream {
71 OCurves object;
72 std::unordered_map<std::string, GeomParamVariant> geom_params;
73 };
74
75 /**
76 * @brief Find or declare @p attr's OGeomParam, then set this sample's values.
77 *
78 * A vec3 attribute named "color" or "normal" is declared through
79 * Alembic's dedicated OC3fGeomParam/ON3fGeomParam rather than a plain
80 * OV3fGeomParam: those are MayaFlux's own fixed vertex field names
81 * (PointVertex/LineVertex/MeshVertex all use them for these exact
82 * roles), and the dedicated type, not the value's shape (three floats
83 * either way), is what tells an importer this is actually color or
84 * normal data rather than an arbitrary vector. Any other vec3 name
85 * (e.g. "tangent") stays a generic vector, matching what Alembic
86 * itself offers no dedicated type for.
87 *
88 * A vec4 "color" is declared as OC4fGeomParam (RGBA). Any other vec4
89 * name is a float GeomParam with array extent 4.
90 *
91 * @return False if @p attr.values holds a DataVariant alternative with no
92 * Alembic GeomParam counterpart, its size doesn't match
93 * @p expected_count, or attr.name was already declared with a
94 * different type.
95 */
96 bool set_attribute(
97 std::unordered_map<std::string, GeomParamVariant>& params,
98 const OCompoundProperty& arb_params,
99 const SpatialAttribute& attr,
100 size_t expected_count)
101 {
102 const auto scope = to_alembic_scope(attr.scope);
103 auto it = params.find(attr.name);
104
105 if (const auto* floats = std::get_if<std::vector<float>>(&attr.values)) {
106 if (floats->size() != expected_count) {
107 return false;
108 }
109 if (it == params.end()) {
110 it = params.emplace(attr.name, OFloatGeomParam(arb_params, attr.name, false, scope, 1)).first;
111 }
112 auto* param = std::get_if<OFloatGeomParam>(&it->second);
113 if (!param) {
114 return false;
115 }
116 param->set(OFloatGeomParam::Sample(FloatArraySample(floats->data(), floats->size()), scope));
117 return true;
118 }
119 if (const auto* uvs = std::get_if<std::vector<glm::vec2>>(&attr.values)) {
120 if (uvs->size() != expected_count) {
121 return false;
122 }
123 if (it == params.end()) {
124 it = params.emplace(attr.name, OV2fGeomParam(arb_params, attr.name, false, scope, 1)).first;
125 }
126 auto* param = std::get_if<OV2fGeomParam>(&it->second);
127 if (!param) {
128 return false;
129 }
130 param->set(OV2fGeomParam::Sample(
131 V2fArraySample(reinterpret_cast<const V2f*>(uvs->data()), uvs->size()), scope));
132 return true;
133 }
134 if (const auto* vecs = std::get_if<std::vector<glm::vec3>>(&attr.values)) {
135 if (vecs->size() != expected_count) {
136 return false;
137 }
138
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;
144 } else {
145 it = params.emplace(attr.name, OV3fGeomParam(arb_params, attr.name, false, scope, 1)).first;
146 }
147 }
148
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));
152 return true;
153 }
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));
157 return true;
158 }
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));
162 return true;
163 }
164 return false;
165 }
166 if (const auto* quads = std::get_if<std::vector<glm::vec4>>(&attr.values)) {
167 if (quads->size() != expected_count) {
168 return false;
169 }
170
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;
174 } else {
175 it = params.emplace(attr.name,
176 OFloat4GeomParam { OFloatGeomParam(arb_params, attr.name, false, scope, 4) })
177 .first;
178 }
179 }
180
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));
184 return true;
185 }
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));
189 return true;
190 }
191 return false;
192 }
193 if (const auto* uints = std::get_if<std::vector<uint32_t>>(&attr.values)) {
194 if (uints->size() != expected_count) {
195 return false;
196 }
197 if (it == params.end()) {
198 it = params.emplace(attr.name, OUInt32GeomParam(arb_params, attr.name, false, scope, 1)).first;
199 }
200 auto* param = std::get_if<OUInt32GeomParam>(&it->second);
201 if (!param) {
202 return false;
203 }
204 param->set(OUInt32GeomParam::Sample(UInt32ArraySample(uints->data(), uints->size()), scope));
205 return true;
206 }
207 return false;
208 }
209
210 size_t vertex_attribute_count(SpatialScope scope, size_t point_count)
211 {
212 return scope == SpatialScope::Constant || scope == SpatialScope::Uniform
213 ? 1
214 : point_count;
215 }
216
217 size_t curves_attribute_count(SpatialScope scope, size_t vertex_count, size_t curve_count)
218 {
219 switch (scope) {
221 return 1;
223 return curve_count;
225 return vertex_count;
226 }
227 return 1;
228 }
229
230} // namespace
231
233 std::optional<OArchive> archive;
234 std::unordered_map<std::string, std::unique_ptr<VertexStream>> vertex_streams;
235 std::unordered_map<std::string, std::unique_ptr<CurvesStream>> curves_streams;
236 std::unordered_map<std::string, MetaData> pending_metadata;
237
238 /**
239 * @brief Guards the members above: write() and close() can run on
240 * different threads. In Impl rather than on SpatialCache
241 * directly so its defaulted move constructor still works.
242 */
243 std::mutex mutex;
244};
245
247 : m_impl(std::make_unique<Impl>())
248{
249}
250
252SpatialCache::SpatialCache(SpatialCache&&) noexcept = default;
253SpatialCache& SpatialCache::operator=(SpatialCache&&) noexcept = default;
254
255bool SpatialCache::open(const std::string& filepath)
256{
257 const auto resolved = resolve_write_path(filepath);
258
259 try {
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());
265 return false;
266 }
267
269 "SpatialCache: opened '{}'", resolved);
270 return true;
271}
272
273bool SpatialCache::write_vertex_sample(const std::string& stream_name, const SpatialSample& sample)
274{
275 auto it = m_impl->vertex_streams.find(stream_name);
276 if (it == m_impl->vertex_streams.end()) {
277 MetaData md;
278 auto mit = m_impl->pending_metadata.find(stream_name);
279 if (mit != m_impl->pending_metadata.end()) {
280 md = mit->second;
281 m_impl->pending_metadata.erase(mit);
282 }
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;
286 }
287 auto& stream = *it->second;
288 auto& schema = stream.object.getSchema();
289
290 const P3fArraySample pos_sample(
291 reinterpret_cast<const V3f*>(sample.positions.data()), sample.positions.size());
292 const V3fArraySample vel_sample = sample.velocities.empty()
293 ? V3fArraySample()
294 : V3fArraySample(reinterpret_cast<const V3f*>(sample.velocities.data()), sample.velocities.size());
295
296 if (sample.ids.empty()) {
297 schema.set(OPointsSchema::Sample(pos_sample, vel_sample));
298 } else {
299 const UInt64ArraySample id_sample(sample.ids.data(), sample.ids.size());
300 schema.set(OPointsSchema::Sample(pos_sample, id_sample, vel_sample));
301 }
302
303 for (const auto& attr : sample.attributes) {
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");
309 "SpatialCache::write: '{}': {}", stream_name, get_last_error());
310 return false;
311 }
312 }
313
314 return true;
315}
316
317bool SpatialCache::write_curves_sample(const std::string& stream_name, const SpatialSample& sample)
318{
319 const size_t expected_vertex_count = std::accumulate(
320 sample.vertex_counts_per_curve.begin(), sample.vertex_counts_per_curve.end(), size_t { 0 },
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()");
325 "SpatialCache::write: '{}': {}", stream_name, get_last_error());
326 return false;
327 }
328
329 auto it = m_impl->curves_streams.find(stream_name);
330 if (it == m_impl->curves_streams.end()) {
331 MetaData md;
332 auto mit = m_impl->pending_metadata.find(stream_name);
333 if (mit != m_impl->pending_metadata.end()) {
334 md = mit->second;
335 m_impl->pending_metadata.erase(mit);
336 }
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;
340 }
341 auto& stream = *it->second;
342 auto& schema = stream.object.getSchema();
343
344 const P3fArraySample pos_sample(
345 reinterpret_cast<const V3f*>(sample.positions.data()), sample.positions.size());
346 const Int32ArraySample nverts_sample(
347 sample.vertex_counts_per_curve.data(), sample.vertex_counts_per_curve.size());
348
349 schema.set(OCurvesSchema::Sample(pos_sample, nverts_sample, kLinear, kNonPeriodic));
350
351 for (const auto& attr : sample.attributes) {
352 const size_t expected = curves_attribute_count(
353 attr.scope, sample.positions.size(), sample.vertex_counts_per_curve.size());
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");
358 "SpatialCache::write: '{}': {}", stream_name, get_last_error());
359 return false;
360 }
361 }
362
363 return true;
364}
365
366bool SpatialCache::write(const std::string& stream_name, const SpatialSample& sample)
367{
368 std::lock_guard lock(m_impl->mutex);
369
370 if (!m_impl->archive) {
371 set_error("write: archive not open");
372 return false;
373 }
374 if (sample.positions.empty()) {
375 set_error("write: no positions");
376 return false;
377 }
378
379 if (uses_curve_schema(sample.topology)) {
380 if (!sample.ids.empty() || !sample.velocities.empty()) {
381 set_error("write: ids/velocities must be empty for a curve topology");
382 return false;
383 }
384 if (sample.vertex_counts_per_curve.empty()) {
385 set_error("write: vertex_counts_per_curve is required for a curve topology");
386 return false;
387 }
388 return write_curves_sample(stream_name, sample);
389 }
390
391 if (sample.topology == PrimitiveTopology::POINT_LIST) {
392 if (!sample.vertex_counts_per_curve.empty()) {
393 set_error("write: vertex_counts_per_curve must be empty for POINT_LIST");
394 return false;
395 }
396 return write_vertex_sample(stream_name, sample);
397 }
398
399 set_error("write: unsupported topology; SpatialCache only writes "
400 "POINT_LIST, LINE_LIST, LINE_STRIP");
402 return false;
403}
404
406 const std::string& stream_name,
407 const std::unordered_map<std::string, std::string>& tags)
408{
409 std::lock_guard lock(m_impl->mutex);
410
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");
415 return false;
416 }
417
418 auto& md = m_impl->pending_metadata[stream_name];
419 for (const auto& [key, value] : tags) {
420 md.set(key, value);
421 }
422 return true;
423}
424
426{
427 std::lock_guard lock(m_impl->mutex);
428
429 if (!m_impl->archive) {
430 return;
431 }
432
433 m_impl->vertex_streams.clear();
434 m_impl->curves_streams.clear();
435 m_impl->pending_metadata.clear();
436 m_impl->archive.reset();
437
439}
440
441} // namespace MayaFlux::IO
#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.