Blender  V2.93
volume_to_mesh.cc
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #include <vector>
18 
19 #include "BLI_float3.hh"
20 #include "BLI_span.hh"
21 #include "BLI_utildefines.h"
22 
23 #include "DNA_mesh_types.h"
24 #include "DNA_meshdata_types.h"
25 #include "DNA_volume_types.h"
26 
27 #include "BKE_mesh.h"
28 #include "BKE_volume.h"
29 
30 #ifdef WITH_OPENVDB
31 # include <openvdb/tools/GridTransformer.h>
32 # include <openvdb/tools/VolumeToMesh.h>
33 #endif
34 
35 #include "BKE_volume_to_mesh.hh"
36 
37 namespace blender::bke {
38 
39 #ifdef WITH_OPENVDB
40 
41 struct VolumeToMeshOp {
42  const openvdb::GridBase &base_grid;
43  const VolumeToMeshResolution resolution;
44  const float threshold;
45  const float adaptivity;
46  std::vector<openvdb::Vec3s> verts;
47  std::vector<openvdb::Vec3I> tris;
48  std::vector<openvdb::Vec4I> quads;
49 
50  template<typename GridType> bool operator()()
51  {
52  if constexpr (std::is_scalar_v<typename GridType::ValueType>) {
53  this->generate_mesh_data<GridType>();
54  return true;
55  }
56  return false;
57  }
58 
59  template<typename GridType> void generate_mesh_data()
60  {
61  const GridType &grid = static_cast<const GridType &>(base_grid);
62 
63  if (this->resolution.mode == VOLUME_TO_MESH_RESOLUTION_MODE_GRID) {
64  this->grid_to_mesh(grid);
65  return;
66  }
67 
68  const float resolution_factor = this->compute_resolution_factor(base_grid);
69  typename GridType::Ptr temp_grid = this->create_grid_with_changed_resolution(
70  grid, resolution_factor);
71  this->grid_to_mesh(*temp_grid);
72  }
73 
74  template<typename GridType>
75  typename GridType::Ptr create_grid_with_changed_resolution(const GridType &old_grid,
76  const float resolution_factor)
77  {
78  BLI_assert(resolution_factor > 0.0f);
79 
80  openvdb::Mat4R xform;
81  xform.setToScale(openvdb::Vec3d(resolution_factor));
82  openvdb::tools::GridTransformer transformer{xform};
83 
84  typename GridType::Ptr new_grid = GridType::create();
85  transformer.transformGrid<openvdb::tools::BoxSampler>(old_grid, *new_grid);
86  new_grid->transform() = old_grid.transform();
87  new_grid->transform().preScale(1.0f / resolution_factor);
88  return new_grid;
89  }
90 
91  float compute_resolution_factor(const openvdb::GridBase &grid) const
92  {
93  const openvdb::Vec3s voxel_size{grid.voxelSize()};
94  const float current_voxel_size = std::max({voxel_size[0], voxel_size[1], voxel_size[2]});
95  const float desired_voxel_size = this->compute_desired_voxel_size(grid);
96  return current_voxel_size / desired_voxel_size;
97  }
98 
99  float compute_desired_voxel_size(const openvdb::GridBase &grid) const
100  {
101  if (this->resolution.mode == VOLUME_TO_MESH_RESOLUTION_MODE_VOXEL_SIZE) {
102  return this->resolution.settings.voxel_size;
103  }
104  const openvdb::CoordBBox coord_bbox = base_grid.evalActiveVoxelBoundingBox();
105  const openvdb::BBoxd bbox = grid.transform().indexToWorld(coord_bbox);
106  const float max_extent = bbox.extents()[bbox.maxExtent()];
107  const float voxel_size = max_extent / this->resolution.settings.voxel_amount;
108  return voxel_size;
109  }
110 
111  template<typename GridType> void grid_to_mesh(const GridType &grid)
112  {
113  openvdb::tools::volumeToMesh(
114  grid, this->verts, this->tris, this->quads, this->threshold, this->adaptivity);
115 
116  /* Better align generated mesh with volume (see T85312). */
117  openvdb::Vec3s offset = grid.voxelSize() / 2.0f;
118  for (openvdb::Vec3s &position : this->verts) {
119  position += offset;
120  }
121  }
122 };
123 
124 static Mesh *new_mesh_from_openvdb_data(Span<openvdb::Vec3s> verts,
125  Span<openvdb::Vec3I> tris,
126  Span<openvdb::Vec4I> quads)
127 {
128  const int tot_loops = 3 * tris.size() + 4 * quads.size();
129  const int tot_polys = tris.size() + quads.size();
130 
131  Mesh *mesh = BKE_mesh_new_nomain(verts.size(), 0, 0, tot_loops, tot_polys);
132 
133  /* Write vertices. */
134  for (const int i : verts.index_range()) {
135  const blender::float3 co = blender::float3(verts[i].asV());
136  copy_v3_v3(mesh->mvert[i].co, co);
137  }
138 
139  /* Write triangles. */
140  for (const int i : tris.index_range()) {
141  mesh->mpoly[i].loopstart = 3 * i;
142  mesh->mpoly[i].totloop = 3;
143  for (int j = 0; j < 3; j++) {
144  /* Reverse vertex order to get correct normals. */
145  mesh->mloop[3 * i + j].v = tris[i][2 - j];
146  }
147  }
148 
149  /* Write quads. */
150  const int poly_offset = tris.size();
151  const int loop_offset = tris.size() * 3;
152  for (const int i : quads.index_range()) {
153  mesh->mpoly[poly_offset + i].loopstart = loop_offset + 4 * i;
154  mesh->mpoly[poly_offset + i].totloop = 4;
155  for (int j = 0; j < 4; j++) {
156  /* Reverse vertex order to get correct normals. */
157  mesh->mloop[loop_offset + 4 * i + j].v = quads[i][3 - j];
158  }
159  }
160 
161  BKE_mesh_calc_edges(mesh, false, false);
163  return mesh;
164 }
165 
166 Mesh *volume_to_mesh(const openvdb::GridBase &grid,
167  const VolumeToMeshResolution &resolution,
168  const float threshold,
169  const float adaptivity)
170 {
171  const VolumeGridType grid_type = BKE_volume_grid_type_openvdb(grid);
172 
173  VolumeToMeshOp to_mesh_op{grid, resolution, threshold, adaptivity};
174  if (!BKE_volume_grid_type_operation(grid_type, to_mesh_op)) {
175  return nullptr;
176  }
177 
178  return new_mesh_from_openvdb_data(to_mesh_op.verts, to_mesh_op.tris, to_mesh_op.quads);
179 }
180 
181 #endif /* WITH_OPENVDB */
182 
183 } // namespace blender::bke
struct Mesh * BKE_mesh_new_nomain(int verts_len, int edges_len, int tessface_len, int loops_len, int polys_len)
Definition: mesh.c:877
void BKE_mesh_calc_normals(struct Mesh *me)
void BKE_mesh_calc_edges(struct Mesh *mesh, bool keep_existing_edges, const bool select_new_edges)
Volume datablock.
VolumeGridType
Definition: BKE_volume.h:98
#define BLI_assert(a)
Definition: BLI_assert.h:58
MINLINE void copy_v3_v3(float r[3], const float a[3])
@ VOLUME_TO_MESH_RESOLUTION_MODE_VOXEL_SIZE
@ VOLUME_TO_MESH_RESOLUTION_MODE_GRID
SIMD_FORCE_INLINE btVector3 operator()(const btVector3 &x) const
Return the transform of the vector.
Definition: btTransform.h:90
static float verts[][3]
VecMat::Vec3< double > Vec3d
Definition: Geom.h:41
unsigned int v
float co[3]
struct MVert * mvert
float size[3]
struct MLoop * mloop
struct MPoly * mpoly
float max