Blender V5.0
volume_to_mesh.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <fmt/format.h>
6#include <vector>
7
9#include "BLI_span.hh"
10
11#include "BKE_mesh.hh"
12#include "BKE_volume_grid.hh"
13#include "BKE_volume_openvdb.hh"
14
15#ifdef WITH_OPENVDB
16# include <openvdb/tools/GridTransformer.h>
17# include <openvdb/tools/VolumeToMesh.h>
18#endif
19
20#include "BKE_volume_to_mesh.hh"
21
22#include "BLT_translation.hh"
23
24namespace blender::bke {
25
26#ifdef WITH_OPENVDB
27
28struct VolumeToMeshOp {
29 const openvdb::GridBase &base_grid;
30 const VolumeToMeshResolution resolution;
31 const float threshold;
32 const float adaptivity;
33 std::vector<openvdb::Vec3s> verts;
34 std::vector<openvdb::Vec3I> tris;
35 std::vector<openvdb::Vec4I> quads;
36 std::string error;
37
38 template<typename GridType> bool operator()()
39 {
40 if constexpr (std::is_scalar_v<typename GridType::ValueType>) {
41 this->generate_mesh_data<GridType>();
42 return true;
43 }
44 return false;
45 }
46
47 template<typename GridType> void generate_mesh_data()
48 {
49 const GridType &grid = static_cast<const GridType &>(base_grid);
50
51 if (this->resolution.mode == VOLUME_TO_MESH_RESOLUTION_MODE_GRID) {
52 this->grid_to_mesh(grid);
53 return;
54 }
55
56 const float resolution_factor = this->compute_resolution_factor(base_grid);
57 typename GridType::Ptr temp_grid = this->create_grid_with_changed_resolution(
58 grid, resolution_factor);
59 this->grid_to_mesh(*temp_grid);
60 }
61
62 template<typename GridType>
63 typename GridType::Ptr create_grid_with_changed_resolution(const GridType &old_grid,
64 const float resolution_factor)
65 {
66 BLI_assert(resolution_factor > 0.0f);
67
68 openvdb::Mat4R xform;
69 xform.setToScale(openvdb::Vec3d(resolution_factor));
70 openvdb::tools::GridTransformer transformer{xform};
71
72 typename GridType::Ptr new_grid = GridType::create();
73 transformer.transformGrid<openvdb::tools::BoxSampler>(old_grid, *new_grid);
74 new_grid->transform() = old_grid.transform();
75 new_grid->transform().preScale(1.0f / resolution_factor);
76 return new_grid;
77 }
78
79 float compute_resolution_factor(const openvdb::GridBase &grid) const
80 {
81 const openvdb::Vec3s voxel_size{grid.voxelSize()};
82 const float current_voxel_size = std::max({voxel_size[0], voxel_size[1], voxel_size[2]});
83 const float desired_voxel_size = this->compute_desired_voxel_size(grid);
84 return current_voxel_size / desired_voxel_size;
85 }
86
87 float compute_desired_voxel_size(const openvdb::GridBase &grid) const
88 {
89 if (this->resolution.mode == VOLUME_TO_MESH_RESOLUTION_MODE_VOXEL_SIZE) {
90 return this->resolution.settings.voxel_size;
91 }
92 const openvdb::CoordBBox coord_bbox = base_grid.evalActiveVoxelBoundingBox();
93 const openvdb::BBoxd bbox = grid.transform().indexToWorld(coord_bbox);
94 const float max_extent = bbox.extents()[bbox.maxExtent()];
95 const float voxel_size = max_extent / this->resolution.settings.voxel_amount;
96 return voxel_size;
97 }
98
99 template<typename GridType> void grid_to_mesh(const GridType &grid)
100 {
101 try {
102 openvdb::tools::volumeToMesh(
103 grid, this->verts, this->tris, this->quads, this->threshold, this->adaptivity);
104 }
105 catch (const std::exception &e) {
106 this->error = fmt::format(fmt::runtime(TIP_("OpenVDB error: {}")), e.what());
107 this->verts.clear();
108 this->tris.clear();
109 this->quads.clear();
110 }
111 }
112};
113
114void fill_mesh_from_openvdb_data(const Span<openvdb::Vec3s> vdb_verts,
115 const Span<openvdb::Vec3I> vdb_tris,
116 const Span<openvdb::Vec4I> vdb_quads,
117 const int vert_offset,
118 const int face_offset,
119 const int loop_offset,
120 MutableSpan<float3> vert_positions,
121 MutableSpan<int> face_offsets,
122 MutableSpan<int> corner_verts)
123{
124 /* Write vertices. */
125 vert_positions.slice(vert_offset, vdb_verts.size()).copy_from(vdb_verts.cast<float3>());
126
127 /* Write triangles. */
128 for (const int i : vdb_tris.index_range()) {
129 face_offsets[face_offset + i] = loop_offset + 3 * i;
130 for (int j = 0; j < 3; j++) {
131 /* Reverse vertex order to get correct normals. */
132 corner_verts[loop_offset + 3 * i + j] = vert_offset + vdb_tris[i][2 - j];
133 }
134 }
135
136 /* Write quads. */
137 const int quad_offset = face_offset + vdb_tris.size();
138 const int quad_loop_offset = loop_offset + vdb_tris.size() * 3;
139 for (const int i : vdb_quads.index_range()) {
140 face_offsets[quad_offset + i] = quad_loop_offset + 4 * i;
141 for (int j = 0; j < 4; j++) {
142 /* Reverse vertex order to get correct normals. */
143 corner_verts[quad_loop_offset + 4 * i + j] = vert_offset + vdb_quads[i][3 - j];
144 }
145 }
146}
147
148bke::VolumeToMeshDataResult volume_to_mesh_data(const openvdb::GridBase &grid,
149 const VolumeToMeshResolution &resolution,
150 const float threshold,
151 const float adaptivity)
152{
153 const VolumeGridType grid_type = bke::volume_grid::get_type(grid);
154
155 VolumeToMeshOp to_mesh_op{grid, resolution, threshold, adaptivity};
156 if (!BKE_volume_grid_type_operation(grid_type, to_mesh_op)) {
157 return {};
158 }
159 return {{std::move(to_mesh_op.verts), std::move(to_mesh_op.tris), std::move(to_mesh_op.quads)},
160 to_mesh_op.error};
161}
162
163Mesh *volume_to_mesh(const openvdb::GridBase &grid,
164 const VolumeToMeshResolution &resolution,
165 const float threshold,
166 const float adaptivity)
167{
168 using namespace blender::bke;
169 const OpenVDBMeshData mesh_data =
170 volume_to_mesh_data(grid, resolution, threshold, adaptivity).data;
171
172 const int tot_loops = 3 * mesh_data.tris.size() + 4 * mesh_data.quads.size();
173 const int tot_faces = mesh_data.tris.size() + mesh_data.quads.size();
174 Mesh *mesh = BKE_mesh_new_nomain(mesh_data.verts.size(), 0, tot_faces, tot_loops);
175
176 fill_mesh_from_openvdb_data(mesh_data.verts,
177 mesh_data.tris,
178 mesh_data.quads,
179 0,
180 0,
181 0,
182 mesh->vert_positions_for_write(),
183 mesh->face_offsets_for_write(),
184 mesh->corner_verts_for_write());
185
186 mesh_calc_edges(*mesh, false, false);
187 mesh_smooth_set(*mesh, false);
188
189 mesh->tag_overlapping_none();
190
191 return mesh;
192}
193
194Mesh *volume_grid_to_mesh(const openvdb::GridBase &grid,
195 const float threshold,
196 const float adaptivity)
197{
198 return volume_to_mesh(grid, {VOLUME_TO_MESH_RESOLUTION_MODE_GRID}, threshold, adaptivity);
199}
200
201#endif /* WITH_OPENVDB */
202
203} // namespace blender::bke
Mesh * BKE_mesh_new_nomain(int verts_num, int edges_num, int faces_num, int corners_num)
VolumeGridType
#define BLI_assert(a)
Definition BLI_assert.h:46
#define TIP_(msgid)
struct Mesh Mesh
@ VOLUME_TO_MESH_RESOLUTION_MODE_VOXEL_SIZE
@ VOLUME_TO_MESH_RESOLUTION_MODE_GRID
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
SIMD_FORCE_INLINE btVector3 operator()(const btVector3 &x) const
Return the transform of the vector.
Definition btTransform.h:90
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:573
Span< NewT > constexpr cast() const
Definition BLI_span.hh:418
constexpr int64_t size() const
Definition BLI_span.hh:252
constexpr IndexRange index_range() const
Definition BLI_span.hh:401
constexpr void copy_from(Span< T > values) const
Definition BLI_span.hh:739
static float verts[][3]
static void error(const char *str)
VolumeGridType get_type(const VolumeGridData &grid)
void mesh_smooth_set(Mesh &mesh, bool use_smooth, bool keep_sharp_edges=false)
void mesh_calc_edges(Mesh &mesh, bool keep_existing_edges, bool select_new_edges)
VecBase< float, 3 > float3
i
Definition text_draw.cc:230