MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ add_grid()

bool MayaFlux::IO::Detail::VDBArchive::add_grid ( const Kinesis::Lattice3D & lattice,
const VDBGridSpec & spec )

Build one grid from active cells and retain it for saving.

The lattice becomes a per-axis scale-translate transform mapping index to world as cell_size() times index plus bounds.min plus half a cell, which reproduces Lattice3D::cell_center exactly.

Returns
False if the spec is inconsistent or the tree build failed. Call last_error() for detail. A failure leaves previously added grids intact.

Definition at line 507 of file VDBArchive.cpp.

509{
510 const size_t width = element_width(spec.is_vector);
511 if (spec.values.size() != spec.coords.size() * width) {
512 m_last_error = "grid '" + std::string(spec.name)
513 + "': value bytes do not match coordinate count";
514 return false;
515 }
516 if (spec.background.size() != width) {
517 m_last_error = "grid '" + std::string(spec.name)
518 + "': background is the wrong width";
519 return false;
520 }
521
522 static_assert(sizeof(glm::ivec3) == sizeof(tvdb_vec3i),
523 "glm::ivec3 and tvdb_vec3i must be layout compatible for the "
524 "coordinate span to be passed through without a copy");
525
526 auto& type_token = m_state->type_tokens.emplace_back();
527 tvdb_grid_t tmpl = make_template(lattice, spec.is_vector, type_token.data());
528
529 const std::string name(spec.name);
530
531 tvdb_grid_t grid;
532 const bool ok = tvdb_grid_from_sparse_typed_using_template(
533 &tmpl,
534 reinterpret_cast<const tvdb_vec3i*>(spec.coords.data()),
535 spec.values.data(),
536 spec.coords.size(),
537 spec.is_vector ? TVDB_VALUE_VEC3F : TVDB_VALUE_FLOAT,
538 spec.background.data(),
539 name.c_str(),
540 &grid);
541
542 if (!ok) {
543 m_last_error = "grid '" + name + "': tree build failed";
544 return false;
545 }
546
547 auto& list = m_state->entries.emplace_back();
548 list.reserve(spec.metadata.size() + 1);
549
550 auto add_entry = [&](std::string_view key, std::string_view value) {
551 std::string& k = m_state->strings.emplace_back(key);
552 std::string& t = m_state->strings.emplace_back("string");
553 std::string& v = m_state->strings.emplace_back(value);
554
555 tvdb_meta_entry_t entry;
556 std::memset(&entry, 0, sizeof(entry));
557 entry.name = k.data();
558 entry.type_name = t.data();
559 entry.value.type = TVDB_VALUE_STRING;
560 entry.value.u.s.str = v.data();
561 entry.value.u.s.len = v.size();
562 list.push_back(entry);
563 };
564
565 // OpenVDB reads a grid's name from metadata, not from the archive's grid
566 // descriptor. Without this entry vdb_print and every DCC show it unnamed.
567 add_entry("name", spec.name);
568
569 for (const auto& [key, value] : spec.metadata) {
570 add_entry(key, value);
571 }
572
573 grid.metadata.entries = list.data();
574 grid.metadata.count = list.size();
575 grid.metadata.capacity = list.size();
576 grid.metadata.alloc = nullptr;
577
578 m_state->grids.push_back(grid);
579 return true;
580}
std::string m_last_error
Set from the const read-path accessors too.
std::unique_ptr< State > m_state

References MayaFlux::IO::Detail::VDBGridSpec::background, MayaFlux::IO::Detail::VDBGridSpec::coords, MayaFlux::IO::Detail::VDBGridSpec::is_vector, m_last_error, m_state, MayaFlux::IO::Detail::VDBGridSpec::metadata, MayaFlux::IO::Detail::VDBGridSpec::name, and MayaFlux::IO::Detail::VDBGridSpec::values.

Referenced by operator=(), and MayaFlux::IO::VDBWriter::write().

Here is the caller graph for this function: