Lagrange
Loading...
Searching...
No Matches
utils.h
1/*
2 * Copyright 2026 Adobe. All rights reserved.
3 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License. You may obtain a copy
5 * of the License at http://www.apache.org/licenses/LICENSE-2.0
6 *
7 * Unless required by applicable law or agreed to in writing, software distributed under
8 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9 * OF ANY KIND, either express or implied. See the License for the specific language
10 * governing permissions and limitations under the License.
11 */
12#pragma once
13
14#include <lagrange/volume/types.h>
15
16// clang-format off
17#include <lagrange/utils/warnoff.h>
18#include <openvdb/tools/FastSweeping.h>
19#include <openvdb/tools/GridTransformer.h>
20#include <openvdb/tools/LevelSetFilter.h>
21#include <openvdb/tools/Dense.h>
22#include <lagrange/utils/warnon.h>
23// clang-format on
24
25namespace lagrange::volume::internal {
26
27template <typename GridScalar>
28typename Grid<GridScalar>::Ptr resample_grid(const Grid<GridScalar>& grid, double voxel_size)
29{
30 if (voxel_size <= 0.0) {
31 auto vs = grid.voxelSize();
32 voxel_size = (vs.x() + vs.y() + vs.z()) / 3.0 * (-voxel_size);
33 }
34 logger().info("Resampling grid to voxel size {}", voxel_size);
35 auto dest = Grid<GridScalar>::create();
36
37 const openvdb::Vec3d offset(voxel_size / 2.0, voxel_size / 2.0, voxel_size / 2.0);
38 auto transform = openvdb::math::Transform::createLinearTransform(voxel_size);
39 transform->postTranslate(offset);
40
41 dest->setTransform(transform);
42 openvdb::tools::resampleToMatch<openvdb::tools::BoxSampler>(grid, *dest);
43
44 return dest;
45}
46
47template <typename GridScalar>
48typename Grid<GridScalar>::Ptr densify_grid(const Grid<GridScalar>& grid)
49{
50 openvdb::tools::Dense<GridScalar> dense(grid.evalActiveVoxelBoundingBox());
51 openvdb::tools::copyToDense(grid, dense);
52 auto tmp = grid.deepCopy();
53 openvdb::tools::copyFromDense(dense, *tmp, -1);
54 return tmp;
55}
56
57template <typename GridScalar>
58typename Grid<GridScalar>::Ptr redistance_grid(const Grid<GridScalar>& grid)
59{
60 return openvdb::tools::sdfToSdf(grid);
61}
62
63template <typename GridScalar>
64void offset_grid_in_place(Grid<GridScalar>& grid, double offset_radius, bool relative)
65{
66 if (grid.getGridClass() != openvdb::GRID_LEVEL_SET) {
67 logger().warn("Offset can only be applied to signed distance fields.");
68 } else {
69 if (relative) {
70 auto vs = grid.voxelSize();
71 double voxel_size = (vs.x() + vs.y() + vs.z()) / 3.0;
72 offset_radius = offset_radius * voxel_size;
73 }
74 openvdb::tools::LevelSetFilter filter(grid);
75 filter.offset(offset_radius);
76 logger().trace("Number of active voxels after offset: {}", grid.activeVoxelCount());
77 filter.prune();
78 logger().trace("Number of active voxels after pruning: {}", grid.activeVoxelCount());
79 }
80}
81
82} // namespace lagrange::volume::internal
LA_CORE_API spdlog::logger & logger()
Retrieves the current logger.
Definition Logger.cpp:40