15#include <lagrange/Logger.h>
16#include <lagrange/utils/assert.h>
17#include <lagrange/volume/types.h>
20#include <lagrange/utils/warnoff.h>
21#include <polyscope/polyscope.h>
22#include <polyscope/volume_grid.h>
23#include <openvdb/tools/FastSweeping.h>
24#include <openvdb/tools/Dense.h>
25#include <openvdb/tools/Interpolation.h>
26#include <tbb/parallel_for.h>
27#include <tbb/blocked_range3d.h>
28#include <tbb/enumerable_thread_specific.h>
29#include <lagrange/utils/warnon.h>
32using FloatGrid = lagrange::volume::Grid<float>;
34void register_grid(std::string_view name,
const FloatGrid& grid)
36 auto bbox_index = grid.evalActiveVoxelBoundingBox();
38 auto bbox_world = grid.transform().indexToWorld(bbox_index);
40 uint32_t dimX = bbox_index.dim().x();
41 uint32_t dimY = bbox_index.dim().y();
42 uint32_t dimZ = bbox_index.dim().z();
44 static_cast<float>(bbox_world.min().x()),
45 static_cast<float>(bbox_world.min().y()),
46 static_cast<float>(bbox_world.min().z()));
48 static_cast<float>(bbox_world.max().x()),
49 static_cast<float>(bbox_world.max().y()),
50 static_cast<float>(bbox_world.max().z()));
53 polyscope::VolumeGrid* ps_grid =
54 polyscope::registerVolumeGrid(std::string(name), {dimX, dimY, dimZ}, bbox_min, bbox_max);
57 uint32_t num_voxels = dimX * dimY * dimZ;
58 std::vector<float> values(num_voxels, 0.f);
59 auto o = bbox_index.min();
62 FloatGrid::ConstAccessor accessor;
64 tbb::enumerable_thread_specific<Data> data([&]() {
return Data{grid.getConstAccessor()}; });
65 using Range3d = tbb::blocked_range3d<int32_t>;
66 const Range3d voxel_range(0, dimZ, 0, dimY, 0, dimX);
67 tbb::parallel_for(voxel_range, [&](
const Range3d& range) {
68 auto rz =
range.pages();
69 auto ry =
range.rows();
70 auto rx =
range.cols();
71 auto& accessor = data.local().accessor;
72 for (int32_t k = rz.begin(); k != rz.end(); ++k) {
73 for (int32_t j = ry.begin(); j != ry.end(); ++j) {
74 for (int32_t i = rx.begin(); i != rx.end(); ++i) {
75 openvdb::Vec3R ijk_is(i + o.x(), j + o.y(), k + o.z());
76 float value = openvdb::tools::BoxSampler::sample(accessor, ijk_is);
77 values[k * dimY * dimX + j * dimX + i] = value;
83 "Registered {} voxels. Min corner: {}, {}, {}",
89 polyscope::VolumeGridNodeScalarQuantity* ps_scalars =
90 ps_grid->addNodeScalarQuantity(
"values", std::make_tuple(values.data(), num_voxels));
91 ps_scalars->setEnabled(
true);
LA_CORE_API spdlog::logger & logger()
Retrieves the current logger.
Definition Logger.cpp:40
#define la_runtime_assert(...)
Runtime assertion check.
Definition assert.h:174
internal::Range< Index > range(Index end)
Returns an iterable object representing the range [0, end).
Definition range.h:176