Blender  V2.93
node_geo_points_to_volume.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 #ifdef WITH_OPENVDB
18 # include <openvdb/openvdb.h>
19 # include <openvdb/tools/LevelSetUtil.h>
20 # include <openvdb/tools/ParticlesToLevelSet.h>
21 #endif
22 
23 #include "node_geometry_util.hh"
24 
25 #include "BKE_lib_id.h"
26 #include "BKE_volume.h"
27 
28 #include "UI_interface.h"
29 #include "UI_resources.h"
30 
32  {SOCK_GEOMETRY, N_("Geometry")},
33  {SOCK_FLOAT, N_("Density"), 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, FLT_MAX},
34  {SOCK_FLOAT, N_("Voxel Size"), 0.3f, 0.0f, 0.0f, 0.0f, 0.01f, FLT_MAX, PROP_DISTANCE},
35  {SOCK_FLOAT, N_("Voxel Amount"), 64.0f, 0.0f, 0.0f, 0.0f, 0.0f, FLT_MAX},
36  {SOCK_STRING, N_("Radius")},
37  {SOCK_FLOAT, N_("Radius"), 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, FLT_MAX},
38  {-1, ""},
39 };
40 
42  {SOCK_GEOMETRY, N_("Geometry")},
43  {-1, ""},
44 };
45 
47  bContext *UNUSED(C),
48  PointerRNA *ptr)
49 {
50  uiLayoutSetPropSep(layout, true);
51  uiLayoutSetPropDecorate(layout, false);
52  uiItemR(layout, ptr, "resolution_mode", 0, IFACE_("Resolution"), ICON_NONE);
53  uiItemR(layout, ptr, "input_type_radius", 0, IFACE_("Radius"), ICON_NONE);
54 }
55 
56 namespace blender::nodes {
57 
58 #ifdef WITH_OPENVDB
59 namespace {
60 /* Implements the interface required by #openvdb::tools::ParticlesToLevelSet. */
61 struct ParticleList {
62  using PosType = openvdb::Vec3R;
63 
64  Span<float3> positions;
65  Span<float> radii;
66 
67  size_t size() const
68  {
69  return (size_t)positions.size();
70  }
71 
72  void getPos(size_t n, openvdb::Vec3R &xyz) const
73  {
74  xyz = &positions[n].x;
75  }
76 
77  void getPosRad(size_t n, openvdb::Vec3R &xyz, openvdb::Real &radius) const
78  {
79  xyz = &positions[n].x;
80  radius = radii[n];
81  }
82 };
83 } // namespace
84 
85 static openvdb::FloatGrid::Ptr generate_volume_from_points(const Span<float3> positions,
86  const Span<float> radii,
87  const float density)
88 {
89  /* Create a new grid that will be filled. #ParticlesToLevelSet requires the background value to
90  * be positive. It will be set to zero later on. */
91  openvdb::FloatGrid::Ptr new_grid = openvdb::FloatGrid::create(1.0f);
92 
93  /* Create a narrow-band level set grid based on the positions and radii. */
94  openvdb::tools::ParticlesToLevelSet op{*new_grid};
95  /* Don't ignore particles based on their radius. */
96  op.setRmin(0.0f);
97  op.setRmax(FLT_MAX);
98  ParticleList particles{positions, radii};
99  op.rasterizeSpheres(particles);
100  op.finalize();
101 
102  /* Convert the level set to a fog volume. This also sets the background value to zero. Inside the
103  * fog there will be a density of 1. */
104  openvdb::tools::sdfToFogVolume(*new_grid);
105 
106  /* Take the desired density into account. */
107  openvdb::tools::foreach (new_grid->beginValueOn(),
108  [&](const openvdb::FloatGrid::ValueOnIter &iter) {
109  iter.modifyValue([&](float &value) { value *= density; });
110  });
111  return new_grid;
112 }
113 
114 static float compute_voxel_size(const GeoNodeExecParams &params,
115  Span<float3> positions,
116  const float radius)
117 {
118  const NodeGeometryPointsToVolume &storage =
119  *(const NodeGeometryPointsToVolume *)params.node().storage;
120 
122  return params.get_input<float>("Voxel Size");
123  }
124 
125  if (positions.is_empty()) {
126  return 0.0f;
127  }
128 
129  float3 min, max;
130  INIT_MINMAX(min, max);
131  minmax_v3v3_v3_array(min, max, (float(*)[3])positions.data(), positions.size());
132 
133  const float voxel_amount = params.get_input<float>("Voxel Amount");
134  if (voxel_amount <= 1) {
135  return 0.0f;
136  }
137 
138  /* The voxel size adapts to the final size of the volume. */
139  const float diagonal = float3::distance(min, max);
140  const float extended_diagonal = diagonal + 2.0f * radius;
141  const float voxel_size = extended_diagonal / voxel_amount;
142  return voxel_size;
143 }
144 
145 static void gather_point_data_from_component(const GeoNodeExecParams &params,
147  Vector<float3> &r_positions,
148  Vector<float> &r_radii)
149 {
150  Float3ReadAttribute positions = component.attribute_get_for_read<float3>(
151  "position", ATTR_DOMAIN_POINT, {0, 0, 0});
152  FloatReadAttribute radii = params.get_input_attribute<float>(
153  "Radius", component, ATTR_DOMAIN_POINT, 0.0f);
154 
155  r_positions.extend(positions.get_span());
156  r_radii.extend(radii.get_span());
157 }
158 
159 static void convert_to_grid_index_space(const float voxel_size,
160  MutableSpan<float3> positions,
161  MutableSpan<float> radii)
162 {
163  const float voxel_size_inv = 1.0f / voxel_size;
164  for (const int i : positions.index_range()) {
165  positions[i] *= voxel_size_inv;
166  /* Better align generated grid with source points. */
167  positions[i] -= float3(0.5f);
168  radii[i] *= voxel_size_inv;
169  }
170 }
171 
172 static void initialize_volume_component_from_points(const GeometrySet &geometry_set_in,
173  GeometrySet &geometry_set_out,
174  const GeoNodeExecParams &params)
175 {
176  Vector<float3> positions;
177  Vector<float> radii;
178 
179  if (geometry_set_in.has<MeshComponent>()) {
180  gather_point_data_from_component(
181  params, *geometry_set_in.get_component_for_read<MeshComponent>(), positions, radii);
182  }
183  if (geometry_set_in.has<PointCloudComponent>()) {
184  gather_point_data_from_component(
185  params, *geometry_set_in.get_component_for_read<PointCloudComponent>(), positions, radii);
186  }
187 
188  const float max_radius = *std::max_element(radii.begin(), radii.end());
189  const float voxel_size = compute_voxel_size(params, positions, max_radius);
190  if (voxel_size == 0.0f || positions.is_empty()) {
191  return;
192  }
193 
194  Volume *volume = (Volume *)BKE_id_new_nomain(ID_VO, nullptr);
195  BKE_volume_init_grids(volume);
196 
197  VolumeGrid *c_density_grid = BKE_volume_grid_add(volume, "density", VOLUME_GRID_FLOAT);
198  openvdb::FloatGrid::Ptr density_grid = openvdb::gridPtrCast<openvdb::FloatGrid>(
199  BKE_volume_grid_openvdb_for_write(volume, c_density_grid, false));
200 
201  const float density = params.get_input<float>("Density");
202  convert_to_grid_index_space(voxel_size, positions, radii);
203  openvdb::FloatGrid::Ptr new_grid = generate_volume_from_points(positions, radii, density);
204  /* This merge is cheap, because the #density_grid is empty. */
205  density_grid->merge(*new_grid);
206  density_grid->transform().postScale(voxel_size);
207 
208  VolumeComponent &volume_component = geometry_set_out.get_component_for_write<VolumeComponent>();
209  volume_component.replace(volume);
210 }
211 #endif
212 
214 {
215  GeometrySet geometry_set_in = params.extract_input<GeometrySet>("Geometry");
216  GeometrySet geometry_set_out;
217 
218  /* TODO: Read-only access to instances should be supported here, for now they are made real. */
219  geometry_set_in = geometry_set_realize_instances(geometry_set_in);
220 
221 #ifdef WITH_OPENVDB
222  initialize_volume_component_from_points(geometry_set_in, geometry_set_out, params);
223 #endif
224 
225  params.set_output("Geometry", std::move(geometry_set_out));
226 }
227 
229 {
231  sizeof(NodeGeometryPointsToVolume), __func__);
233  data->input_type_radius = GEO_NODE_ATTRIBUTE_INPUT_FLOAT;
234  node->storage = data;
235 
236  bNodeSocket *radius_attribute_socket = nodeFindSocket(node, SOCK_IN, "Radius");
237  bNodeSocketValueString *radius_attribute_socket_value =
238  (bNodeSocketValueString *)radius_attribute_socket->default_value;
239  STRNCPY(radius_attribute_socket_value->value, "radius");
240 }
241 
243 {
245  bNodeSocket *voxel_size_socket = nodeFindSocket(node, SOCK_IN, "Voxel Size");
246  bNodeSocket *voxel_amount_socket = nodeFindSocket(node, SOCK_IN, "Voxel Amount");
247  nodeSetSocketAvailability(voxel_amount_socket,
248  data->resolution_mode ==
251  voxel_size_socket, data->resolution_mode == GEO_NODE_POINTS_TO_VOLUME_RESOLUTION_MODE_SIZE);
252 
254  *node, "Radius", (GeometryNodeAttributeInputMode)data->input_type_radius);
255 }
256 
257 } // namespace blender::nodes
258 
260 {
261  static bNodeType ntype;
262 
264  &ntype, GEO_NODE_POINTS_TO_VOLUME, "Points to Volume", NODE_CLASS_GEOMETRY, 0);
266  node_type_storage(&ntype,
267  "NodeGeometryPointsToVolume",
270  node_type_size(&ntype, 170, 120, 700);
275  nodeRegisterType(&ntype);
276 }
@ ATTR_DOMAIN_POINT
Definition: BKE_attribute.h:43
void * BKE_id_new_nomain(const short type, const char *name)
Definition: lib_id.c:1196
void nodeSetSocketAvailability(struct bNodeSocket *sock, bool is_available)
Definition: node.cc:3726
void node_type_size(struct bNodeType *ntype, int width, int minwidth, int maxwidth)
Definition: node.cc:4565
void node_type_socket_templates(struct bNodeType *ntype, struct bNodeSocketTemplate *inputs, struct bNodeSocketTemplate *outputs)
Definition: node.cc:4527
void node_type_update(struct bNodeType *ntype, void(*updatefunc)(struct bNodeTree *ntree, struct bNode *node))
Definition: node.cc:4623
void node_type_init(struct bNodeType *ntype, void(*initfunc)(struct bNodeTree *ntree, struct bNode *node))
Definition: node.cc:4559
#define NODE_CLASS_GEOMETRY
Definition: BKE_node.h:359
void node_type_storage(struct bNodeType *ntype, const char *storagename, void(*freefunc)(struct bNode *node), void(*copyfunc)(struct bNodeTree *dest_ntree, struct bNode *dest_node, const struct bNode *src_node))
Definition: node.cc:4599
struct bNodeSocket * nodeFindSocket(const struct bNode *node, eNodeSocketInOut in_out, const char *identifier)
#define GEO_NODE_POINTS_TO_VOLUME
Definition: BKE_node.h:1398
void nodeRegisterType(struct bNodeType *ntype)
Definition: node.cc:1298
Volume datablock.
void BKE_volume_init_grids(struct Volume *volume)
Definition: volume.cc:660
struct VolumeGrid * BKE_volume_grid_add(struct Volume *volume, const char *name, VolumeGridType type)
Definition: volume.cc:1417
@ VOLUME_GRID_FLOAT
Definition: BKE_volume.h:101
void minmax_v3v3_v3_array(float r_min[3], float r_max[3], const float(*vec_arr)[3], int nbr)
Definition: math_vector.c:1060
#define STRNCPY(dst, src)
Definition: BLI_string.h:163
#define INIT_MINMAX(min, max)
#define UNUSED(x)
#define IFACE_(msgid)
#define N_(msgid)
static uint8 component(Color32 c, uint i)
Definition: ColorBlock.cpp:126
@ ID_VO
Definition: DNA_ID_enums.h:95
@ SOCK_IN
@ SOCK_FLOAT
@ SOCK_GEOMETRY
@ SOCK_STRING
@ GEO_NODE_POINTS_TO_VOLUME_RESOLUTION_MODE_AMOUNT
@ GEO_NODE_POINTS_TO_VOLUME_RESOLUTION_MODE_SIZE
GeometryNodeAttributeInputMode
@ GEO_NODE_ATTRIBUTE_INPUT_FLOAT
@ PROP_DISTANCE
Definition: RNA_types.h:135
#define C
Definition: RandGen.cpp:39
void uiLayoutSetPropSep(uiLayout *layout, bool is_sep)
void uiItemR(uiLayout *layout, struct PointerRNA *ptr, const char *propname, int flag, const char *name, int icon)
void uiLayoutSetPropDecorate(uiLayout *layout, bool is_sep)
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
void replace(Volume *volume, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
OperationNode * node
bNodeTree * ntree
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
void *(* MEM_callocN)(size_t len, const char *str)
Definition: mallocn.c:45
TypedReadAttribute< float > FloatReadAttribute
TypedReadAttribute< float3 > Float3ReadAttribute
GeometrySet geometry_set_realize_instances(const GeometrySet &geometry_set)
static void geo_node_points_to_volume_update(bNodeTree *UNUSED(ntree), bNode *node)
void update_attribute_input_socket_availabilities(bNode &node, const StringRef name, const GeometryNodeAttributeInputMode mode, const bool name_is_available)
static void geo_node_points_to_volume_init(bNodeTree *UNUSED(ntree), bNode *node)
static void geo_node_points_to_volume_exec(GeoNodeExecParams params)
static bNodeSocketTemplate geo_node_points_to_volume_out[]
static bNodeSocketTemplate geo_node_points_to_volume_in[]
void register_node_type_geo_points_to_volume()
static void geo_node_points_to_volume_layout(uiLayout *layout, bContext *UNUSED(C), PointerRNA *ptr)
void geo_node_type_base(bNodeType *ntype, int type, const char *name, short nclass, short flag)
void node_copy_standard_storage(bNodeTree *UNUSED(dest_ntree), bNode *dest_node, const bNode *src_node)
Definition: node_util.c:67
void node_free_standard_storage(bNode *node)
Definition: node_util.c:55
#define min(a, b)
Definition: sort.c:51
GeometryComponent & get_component_for_write(GeometryComponentType component_type)
bool has(const GeometryComponentType component_type) const
const GeometryComponent * get_component_for_read(GeometryComponentType component_type) const
Compact definition of a node socket.
Definition: BKE_node.h:95
void * default_value
Defines a node type.
Definition: BKE_node.h:221
NodeGeometryExecFunction geometry_node_execute
Definition: BKE_node.h:327
void(* draw_buttons)(struct uiLayout *, struct bContext *C, struct PointerRNA *ptr)
Definition: BKE_node.h:253
static float distance(const float3 &a, const float3 &b)
Definition: sky_float3.h:99
float max
#define foreach(x, y)
Definition: util_foreach.h:22
PointerRNA * ptr
Definition: wm_files.c:3157