[Bf-blender-cvs] [16abe9343a5] master: Geometry Nodes: add Volume to Mesh node

Jacques Lucke noreply at git.blender.org
Fri Feb 5 16:20:25 CET 2021


Commit: 16abe9343a5e01426e85e4bb9d0e95c3217e35ea
Author: Jacques Lucke
Date:   Fri Feb 5 16:20:14 2021 +0100
Branches: master
https://developer.blender.org/rB16abe9343a5e01426e85e4bb9d0e95c3217e35ea

Geometry Nodes: add Volume to Mesh node

This node takes a volume and generates a mesh on it's "surface".
The surface is defined by a threshold value.

Currently, the node only works on volumes generated by the
Points to Volume node. This limitation will be resolved soonish.

Ref T84605.

Differential Revision: https://developer.blender.org/D10243

===================================================================

M	release/scripts/startup/nodeitems_builtins.py
M	source/blender/blenkernel/BKE_node.h
A	source/blender/blenkernel/BKE_volume_to_mesh.hh
M	source/blender/blenkernel/CMakeLists.txt
M	source/blender/blenkernel/intern/node.cc
A	source/blender/blenkernel/intern/volume_to_mesh.cc
M	source/blender/editors/space_node/drawnode.c
M	source/blender/makesdna/DNA_node_types.h
M	source/blender/makesrna/intern/rna_nodetree.c
M	source/blender/modifiers/intern/MOD_volume_to_mesh.cc
M	source/blender/nodes/CMakeLists.txt
M	source/blender/nodes/NOD_geometry.h
M	source/blender/nodes/NOD_static_types.h
A	source/blender/nodes/geometry/nodes/node_geo_volume_to_mesh.cc

===================================================================

diff --git a/release/scripts/startup/nodeitems_builtins.py b/release/scripts/startup/nodeitems_builtins.py
index dac027d649e..40b57fd10de 100644
--- a/release/scripts/startup/nodeitems_builtins.py
+++ b/release/scripts/startup/nodeitems_builtins.py
@@ -526,6 +526,7 @@ geometry_node_categories = [
     ]),
     GeometryNodeCategory("GEO_VOLUME", "Volume", items=[
         NodeItem("GeometryNodePointsToVolume"),
+        NodeItem("GeometryNodeVolumeToMesh"),
     ]),
     GeometryNodeCategory("GEO_UTILITIES", "Utilities", items=[
         NodeItem("ShaderNodeMapRange"),
diff --git a/source/blender/blenkernel/BKE_node.h b/source/blender/blenkernel/BKE_node.h
index 0b8ef70de2a..f4f753c4084 100644
--- a/source/blender/blenkernel/BKE_node.h
+++ b/source/blender/blenkernel/BKE_node.h
@@ -1369,6 +1369,7 @@ int ntreeTexExecTree(struct bNodeTree *ntree,
 #define GEO_NODE_COLLECTION_INFO 1023
 #define GEO_NODE_IS_VIEWPORT 1024
 #define GEO_NODE_ATTRIBUTE_PROXIMITY 1025
+#define GEO_NODE_VOLUME_TO_MESH 1026
 
 /** \} */
 
diff --git a/source/blender/blenkernel/BKE_volume_to_mesh.hh b/source/blender/blenkernel/BKE_volume_to_mesh.hh
new file mode 100644
index 00000000000..1ec8a8e84cd
--- /dev/null
+++ b/source/blender/blenkernel/BKE_volume_to_mesh.hh
@@ -0,0 +1,43 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "DNA_modifier_types.h"
+
+#ifdef WITH_OPENVDB
+#  include <openvdb/openvdb.h>
+#endif
+
+struct Mesh;
+struct VolumeGrid;
+
+namespace blender::bke {
+
+struct VolumeToMeshResolution {
+  VolumeToMeshResolutionMode mode;
+  union {
+    float voxel_size;
+    float voxel_amount;
+  } settings;
+};
+
+#ifdef WITH_OPENVDB
+struct Mesh *volume_to_mesh(const openvdb::GridBase &grid,
+                            const VolumeToMeshResolution &resolution,
+                            const float threshold,
+                            const float adaptivity);
+#endif
+
+}  // namespace blender::bke
diff --git a/source/blender/blenkernel/CMakeLists.txt b/source/blender/blenkernel/CMakeLists.txt
index 0f0ef926a52..0da9598f0ad 100644
--- a/source/blender/blenkernel/CMakeLists.txt
+++ b/source/blender/blenkernel/CMakeLists.txt
@@ -259,6 +259,7 @@ set(SRC
   intern/unit.c
   intern/volume.cc
   intern/volume_render.cc
+  intern/volume_to_mesh.cc
   intern/workspace.c
   intern/world.c
   intern/writeavi.c
@@ -413,6 +414,7 @@ set(SRC
   BKE_unit.h
   BKE_volume.h
   BKE_volume_render.h
+  BKE_volume_to_mesh.hh
   BKE_workspace.h
   BKE_world.h
   BKE_writeavi.h
diff --git a/source/blender/blenkernel/intern/node.cc b/source/blender/blenkernel/intern/node.cc
index 44efbe64202..434875820c3 100644
--- a/source/blender/blenkernel/intern/node.cc
+++ b/source/blender/blenkernel/intern/node.cc
@@ -4777,6 +4777,7 @@ static void registerGeometryNodes()
   register_node_type_geo_points_to_volume();
   register_node_type_geo_collection_info();
   register_node_type_geo_is_viewport();
+  register_node_type_geo_volume_to_mesh();
 }
 
 static void registerFunctionNodes()
diff --git a/source/blender/blenkernel/intern/volume_to_mesh.cc b/source/blender/blenkernel/intern/volume_to_mesh.cc
new file mode 100644
index 00000000000..7ab67516242
--- /dev/null
+++ b/source/blender/blenkernel/intern/volume_to_mesh.cc
@@ -0,0 +1,183 @@
+/*
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include <vector>
+
+#include "BLI_float3.hh"
+#include "BLI_span.hh"
+#include "BLI_utildefines.h"
+
+#include "DNA_mesh_types.h"
+#include "DNA_meshdata_types.h"
+#include "DNA_volume_types.h"
+
+#include "BKE_mesh.h"
+#include "BKE_volume.h"
+
+#ifdef WITH_OPENVDB
+#  include <openvdb/tools/GridTransformer.h>
+#  include <openvdb/tools/VolumeToMesh.h>
+#endif
+
+#include "BKE_volume_to_mesh.hh"
+
+namespace blender::bke {
+
+#ifdef WITH_OPENVDB
+
+struct VolumeToMeshOp {
+  const openvdb::GridBase &base_grid;
+  const VolumeToMeshResolution resolution;
+  const float threshold;
+  const float adaptivity;
+  std::vector<openvdb::Vec3s> verts;
+  std::vector<openvdb::Vec3I> tris;
+  std::vector<openvdb::Vec4I> quads;
+
+  template<typename GridType> bool operator()()
+  {
+    if constexpr (std::is_scalar_v<typename GridType::ValueType>) {
+      this->generate_mesh_data<GridType>();
+      return true;
+    }
+    return false;
+  }
+
+  template<typename GridType> void generate_mesh_data()
+  {
+    const GridType &grid = static_cast<const GridType &>(base_grid);
+
+    if (this->resolution.mode == VOLUME_TO_MESH_RESOLUTION_MODE_GRID) {
+      this->grid_to_mesh(grid);
+      return;
+    }
+
+    const float resolution_factor = this->compute_resolution_factor(base_grid);
+    typename GridType::Ptr temp_grid = this->create_grid_with_changed_resolution(
+        grid, resolution_factor);
+    this->grid_to_mesh(*temp_grid);
+  }
+
+  template<typename GridType>
+  typename GridType::Ptr create_grid_with_changed_resolution(const GridType &old_grid,
+                                                             const float resolution_factor)
+  {
+    BLI_assert(resolution_factor > 0.0f);
+
+    openvdb::Mat4R xform;
+    xform.setToScale(openvdb::Vec3d(resolution_factor));
+    openvdb::tools::GridTransformer transformer{xform};
+
+    typename GridType::Ptr new_grid = GridType::create();
+    transformer.transformGrid<openvdb::tools::BoxSampler>(old_grid, *new_grid);
+    new_grid->transform() = old_grid.transform();
+    new_grid->transform().preScale(1.0f / resolution_factor);
+    return new_grid;
+  }
+
+  float compute_resolution_factor(const openvdb::GridBase &grid) const
+  {
+    const openvdb::Vec3s voxel_size{grid.voxelSize()};
+    const float current_voxel_size = std::max({voxel_size[0], voxel_size[1], voxel_size[2]});
+    const float desired_voxel_size = this->compute_desired_voxel_size(grid);
+    return current_voxel_size / desired_voxel_size;
+  }
+
+  float compute_desired_voxel_size(const openvdb::GridBase &grid) const
+  {
+    if (this->resolution.mode == VOLUME_TO_MESH_RESOLUTION_MODE_VOXEL_SIZE) {
+      return this->resolution.settings.voxel_size;
+    }
+    const openvdb::CoordBBox coord_bbox = base_grid.evalActiveVoxelBoundingBox();
+    const openvdb::BBoxd bbox = grid.transform().indexToWorld(coord_bbox);
+    const float max_extent = bbox.extents()[bbox.maxExtent()];
+    const float voxel_size = max_extent / this->resolution.settings.voxel_amount;
+    return voxel_size;
+  }
+
+  template<typename GridType> void grid_to_mesh(const GridType &grid)
+  {
+    openvdb::tools::volumeToMesh(
+        grid, this->verts, this->tris, this->quads, this->threshold, this->adaptivity);
+
+    /* Better align generated mesh with volume (see T85312). */
+    openvdb::Vec3s offset = grid.voxelSize() / 2.0f;
+    for (openvdb::Vec3s &position : this->verts) {
+      position += offset;
+    }
+  }
+};
+
+static Mesh *new_mesh_from_openvdb_data(Span<openvdb::Vec3s> verts,
+                                        Span<openvdb::Vec3I> tris,
+                                        Span<openvdb::Vec4I> quads)
+{
+  const int tot_loops = 3 * tris.size() + 4 * quads.size();
+  const int tot_polys = tris.size() + quads.size();
+
+  Mesh *mesh = BKE_mesh_new_nomain(verts.size(), 0, 0, tot_loops, tot_polys);
+
+  /* Write vertices. */
+  for (const int i : verts.index_range()) {
+    const blender::float3 co = blender::float3(verts[i].asV());
+    copy_v3_v3(mesh->mvert[i].co, co);
+  }
+
+  /* Write triangles. */
+  for (const int i : tris.index_range()) {
+    mesh->mpoly[i].loopstart = 3 * i;
+    mesh->mpoly[i].totloop = 3;
+    for (int j = 0; j < 3; j++) {
+      /* Reverse vertex order to get correct normals. */
+      mesh->mloop[3 * i + j].v = tris[i][2 - j];
+    }
+  }
+
+  /* Write quads. */
+  const int poly_offset = tris.size();
+  const int loop_offset = tris.size() * 3;
+  for (const int i : quads.index_range()) {
+    mesh->mpoly[poly_offset + i].loopstart = loop_offset + 4 * i;
+    mesh->mpoly[poly_offset + i].totloop = 4;
+    for (int j = 0; j < 4; j++) {
+      /* Reverse vertex order to get correct normals. */
+      mesh->mloop[loop_offset + 4 * i + j].v = quads[i][3 - j];
+    }
+  }
+
+  BKE_mesh_calc_edges(mesh, false, false);
+  BKE_mesh_calc_normals(mesh);
+  return mesh;
+}
+
+Mesh *volume_to_mesh(const openvdb::GridBase &grid,
+                     const VolumeToMeshResolution &resolution,
+                     const float threshold,
+                     const float adaptivity)
+{
+  const VolumeGridType grid_type = BKE_volume_grid_type_openvdb(grid);
+
+  VolumeToMeshOp to_mesh_op{grid, resolution, threshold, adaptivity};
+  if (!BKE_volume_grid_type_operation(grid_type, to_mesh_op)) {
+    return nullptr;
+  }
+
+  return new_mesh_from_openvdb_data(to_mesh_op.verts, to_mesh_op.tris, to_mesh_op.quads);
+}
+
+#endif /* WITH_OPENVDB */
+
+}  // namespace blender::bke
diff --git a/source/blender/edit

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list