14#include <lagrange/Logger.h>
15#include <lagrange/utils/assert.h>
16#include <lagrange/volume/types.h>
19#include <lagrange/utils/warnoff.h>
20#include <polyscope/polyscope.h>
21#include <polyscope/volume_grid.h>
22#include <openvdb/tools/FastSweeping.h>
23#include <openvdb/tools/Dense.h>
24#include <openvdb/tools/Interpolation.h>
25#include <tbb/parallel_for.h>
26#include <tbb/blocked_range3d.h>
27#include <tbb/enumerable_thread_specific.h>
28#include <lagrange/utils/warnon.h>
31using FloatGrid = lagrange::volume::Grid<float>;
33void register_grid(std::string_view name,
const FloatGrid& grid)
35 auto bbox_index = grid.evalActiveVoxelBoundingBox();
37 auto bbox_world = grid.transform().indexToWorld(bbox_index);
39 uint32_t dimX = bbox_index.dim().x();
40 uint32_t dimY = bbox_index.dim().y();
41 uint32_t dimZ = bbox_index.dim().z();
43 static_cast<float>(bbox_world.min().x()),
44 static_cast<float>(bbox_world.min().y()),
45 static_cast<float>(bbox_world.min().z()));
47 static_cast<float>(bbox_world.max().x()),
48 static_cast<float>(bbox_world.max().y()),
49 static_cast<float>(bbox_world.max().z()));
52 polyscope::VolumeGrid* ps_grid =
53 polyscope::registerVolumeGrid(std::string(name), {dimX, dimY, dimZ}, bbox_min, bbox_max);
56 uint32_t num_voxels = dimX * dimY * dimZ;
57 std::vector<float> values(num_voxels, 0.f);
58 auto o = bbox_index.min();
61 FloatGrid::ConstAccessor accessor;
63 tbb::enumerable_thread_specific<Data> data([&]() {
return Data{grid.getConstAccessor()}; });
64 using Range3d = tbb::blocked_range3d<int32_t>;
65 const Range3d voxel_range(0, dimZ, 0, dimY, 0, dimX);
66 tbb::parallel_for(voxel_range, [&](
const Range3d& range) {
67 auto rz =
range.pages();
68 auto ry =
range.rows();
69 auto rx =
range.cols();
70 auto& accessor = data.local().accessor;
71 for (int32_t k = rz.begin(); k != rz.end(); ++k) {
72 for (int32_t j = ry.begin(); j != ry.end(); ++j) {
73 for (int32_t i = rx.begin(); i != rx.end(); ++i) {
74 openvdb::Vec3R ijk_is(i + o.x(), j + o.y(), k + o.z());
75 float value = openvdb::tools::BoxSampler::sample(accessor, ijk_is);
76 values[k * dimY * dimX + j * dimX + i] = value;
82 "Registered {} voxels. Min corner: {}, {}, {}",
88 polyscope::VolumeGridNodeScalarQuantity* ps_scalars =
89 ps_grid->addNodeScalarQuantity(
"values", std::make_tuple(values.data(), num_voxels));
90 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