Blender V5.3
node_geo_cube_grid_topology.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BKE_volume.hh"
6#include "BKE_volume_grid.hh"
7
9
10#include "node_geometry_util.hh"
11#include <cstdint>
12
13#ifdef WITH_OPENVDB
14# include <openvdb/openvdb.h>
15#endif
16
18
20{
21 b.use_custom_socket_order();
22 b.add_output<decl::Bool>("Topology"_ustr)
23 .structure_type(StructureType::Grid)
24 .description("Boolean grid defining the topology/active regions");
25 b.add_input<decl::Vector>("Bounds Min"_ustr)
26 .default_value(float3(-1.0f))
27 .description("Minimum boundary of the grid (world space)");
28 b.add_input<decl::Vector>("Bounds Max"_ustr)
29 .default_value(float3(1.0f))
30 .description("Maximum boundary of the grid (world space)");
31
32 b.add_input<decl::Int>("Resolution X"_ustr)
33 .default_value(32)
34 .min(1)
35 .description("Number of voxels in the X axis");
36 b.add_input<decl::Int>("Resolution Y"_ustr)
37 .default_value(32)
38 .min(1)
39 .description("Number of voxels in the Y axis");
40 b.add_input<decl::Int>("Resolution Z"_ustr)
41 .default_value(32)
42 .min(1)
43 .description("Number of voxels in the Z axis");
44
45 PanelDeclarationBuilder &min_panel = b.add_panel("Min"_ustr).default_closed(true);
46 min_panel.add_input<decl::Int>("Min X"_ustr)
47 .default_value(0)
48 .description("Minimum coordinate in X axis (grid index space)");
49 min_panel.add_input<decl::Int>("Min Y"_ustr)
50 .default_value(0)
51 .description("Minimum coordinate in Y axis (grid index space)");
52 min_panel.add_input<decl::Int>("Min Z"_ustr)
53 .default_value(0)
54 .description("Minimum coordinate in Z axis (grid index space)");
55}
56
58{
59#ifdef WITH_OPENVDB
60 using type_traits = typename bke::VolumeGridTraits<bool>;
61 using TreeType = typename type_traits::TreeType;
62 using GridType = openvdb::Grid<TreeType>;
63
64 auto openvdb_grid = GridType::create(false /* background */);
65
66 const int3 grid_min_inclusive = int3(params.extract_input<int>("Min X"_ustr),
67 params.extract_input<int>("Min Y"_ustr),
68 params.extract_input<int>("Min Z"_ustr));
69 const int3 resolution = int3(params.extract_input<int>("Resolution X"_ustr),
70 params.extract_input<int>("Resolution Y"_ustr),
71 params.extract_input<int>("Resolution Z"_ustr));
72
73 const float3 bounds_min = params.extract_input<float3>("Bounds Min"_ustr);
74 const float3 bounds_max = params.extract_input<float3>("Bounds Max"_ustr);
75
76 if (resolution.x <= 0 || resolution.y <= 0 || resolution.z <= 0) {
77 params.error_message_add(NodeWarningType::Warning, TIP_("Resolution must be positive"));
79 return;
80 }
81
82 if (bounds_min.x >= bounds_max.x || bounds_min.y >= bounds_max.y || bounds_min.z >= bounds_max.z)
83 {
85 TIP_("Bounding box volume must be greater than 0"));
87 return;
88 }
89
90 const int3 grid_max_inclusive = grid_min_inclusive + resolution - 1;
91 const openvdb::math::CoordBBox bbox(
92 {grid_min_inclusive.x, grid_min_inclusive.y, grid_min_inclusive.z},
93 {grid_max_inclusive.x, grid_max_inclusive.y, grid_max_inclusive.z});
94 openvdb_grid->tree().denseFill(bbox, true, /*active=*/true);
95
96 const double3 scale_fac = double3(bounds_max - bounds_min) / double3(resolution);
97 if (!BKE_volume_voxel_size_valid(float3(scale_fac))) {
99 TIP_("Volume scale is lower than permitted by OpenVDB"));
101 return;
102 }
103
104 openvdb_grid->transform().postScale(openvdb::math::Vec3d(scale_fac.x, scale_fac.y, scale_fac.z));
105 const double3 translation = double3(bounds_min) - scale_fac * double3(grid_min_inclusive);
106 openvdb_grid->transform().postTranslate(
107 openvdb::math::Vec3d(translation.x, translation.y, translation.z));
108
109 bke::VolumeGrid<bool> topology_grid(std::move(openvdb_grid));
110 params.set_output("Topology"_ustr, bke::GVolumeGrid(std::move(topology_grid)));
111#else
113#endif
114}
115
116static void node_register()
117{
118 static bke::bNodeType ntype;
119
120 geo_node_type_base(&ntype, "GeometryNodeCubeGridTopology"_ustr);
121 ntype.ui_name = "Cube Grid Topology";
122 ntype.ui_description =
123 "Create a boolean grid topology with the given dimensions, for use with the Field to Grid "
124 "node";
126 ntype.declare = node_declare;
130}
132
133} // namespace blender::nodes::node_geo_cube_grid_topology_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
Volume data-block.
#define TIP_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
DeclType::Builder & add_input(UString name, UString identifier=""_ustr)
void error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
static void node_declare(NodeDeclarationBuilder &b)
void node_geo_exec_with_missing_openvdb(GeoNodeExecParams &params)
VecBase< int32_t, 3 > int3
VecBase< double, 3 > double3
VecBase< float, 3 > float3
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
bool BKE_volume_voxel_size_valid(const float3 &voxel_size)
static constexpr int _160
Definition BKE_node.hh:246
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
NodeDeclareFunction declare
Definition BKE_node.hh:393