6# include <openvdb/openvdb.h>
7# include <openvdb/tools/Interpolation.h>
8# include <openvdb/tools/PointScatter.h>
36 .supported_type(GeometryComponent::Type::Volume)
43 .description(
"Number of points to sample per unit volume");
45 "Seed used by the random number generator to generate random points");
50 .description(
"Spacing between grid points");
55 .description(
"Minimum density of a volume cell to contain a grid point");
58 const bNode *node =
b.node_or_null();
59 if (node !=
nullptr) {
86class PositionsVDBWrapper {
93 : offset_fix_(offset_fix), vector_(
vector)
96 PositionsVDBWrapper(
const PositionsVDBWrapper &wrapper) =
default;
98 void add(
const openvdb::Vec3R &
pos)
107using RNGType = std::mt19937;
109using NonUniformPointScatterVDB =
110 openvdb::tools::NonUniformPointScatter<PositionsVDBWrapper, RNGType>;
112static void point_scatter_density_random(
const openvdb::FloatGrid &grid,
118 const float3 offset_fix = {0.5f * float(grid.voxelSize().x()),
119 0.5f * float(grid.voxelSize().y()),
120 0.5f * float(grid.voxelSize().z())};
122 PositionsVDBWrapper vdb_position_wrapper = PositionsVDBWrapper(r_positions, offset_fix);
123 RNGType random_generator(
seed);
124 NonUniformPointScatterVDB point_scatter(vdb_position_wrapper, density, random_generator);
128static void point_scatter_density_grid(
const openvdb::FloatGrid &grid,
130 const float threshold,
133 const openvdb::Vec3d half_voxel(0.5, 0.5, 0.5);
134 const openvdb::Vec3d voxel_spacing(
double(spacing.
x) / grid.voxelSize().x(),
135 double(spacing.
y) / grid.voxelSize().y(),
136 double(spacing.
z) / grid.voxelSize().z());
139 const double min_spacing = std::min({voxel_spacing.x(), voxel_spacing.y(), voxel_spacing.z()});
140 if (std::abs(min_spacing) < 0.0001) {
145 for (openvdb::FloatGrid::ValueOnCIter cell = grid.cbeginValueOn(); cell; ++cell) {
147 if (cell.getValue() < threshold) {
151 const openvdb::CoordBBox bbox = cell.getBoundingBox();
152 const openvdb::Vec3d box_min = bbox.min().asVec3d() - half_voxel;
153 const openvdb::Vec3d box_max = bbox.max().asVec3d() + half_voxel;
156 double abs_spacing_x = std::abs(voxel_spacing.x());
157 double abs_spacing_y = std::abs(voxel_spacing.y());
158 double abs_spacing_z = std::abs(voxel_spacing.z());
159 const openvdb::Vec3d start(
ceil(box_min.x() / abs_spacing_x) * abs_spacing_x,
160 ceil(box_min.y() / abs_spacing_y) * abs_spacing_y,
161 ceil(box_min.z() / abs_spacing_z) * abs_spacing_z);
164 for (
double x = start.x();
x < box_max.x();
x += abs_spacing_x) {
165 for (
double y = start.y();
y < box_max.y();
y += abs_spacing_y) {
166 for (
double z = start.z();
z < box_max.z();
z += abs_spacing_z) {
168 const openvdb::Vec3d idx_pos(
x,
y,
z);
169 const openvdb::Vec3d local_pos = grid.indexToWorld(idx_pos + half_voxel);
170 r_positions.
append({float(local_pos.x()), float(local_pos.y()), float(local_pos.z())});
193 density =
params.extract_input<
float>(
"Density");
198 threshold =
params.extract_input<
float>(
"Threshold");
214 if (volume_grid ==
nullptr) {
218 bke::VolumeTreeAccessToken tree_token;
219 const openvdb::GridBase &base_grid = volume_grid->grid(tree_token);
221 if (!base_grid.isType<openvdb::FloatGrid>()) {
225 const openvdb::FloatGrid &grid =
static_cast<const openvdb::FloatGrid &
>(base_grid);
228 point_scatter_density_random(grid, density,
seed, positions);
231 point_scatter_density_grid(grid, spacing, threshold, positions);
237 pointcloud->positions_for_write().copy_from(positions);
241 point_radii.
span.fill(0.05f);
250 params.set_output(
"Points", std::move(geometry_set));
264 "Distribute points randomly inside of the volume"},
269 "Distribute the points in a grid pattern inside of the volume"},
270 {0,
nullptr, 0,
nullptr,
nullptr},
275 "Distribution Method",
276 "Method to use for scattering points",
287 ntype.
ui_name =
"Distribute Points in Volume";
292 "NodeGeometryDistributePointsInVolume",
#define NODE_STORAGE_FUNCS(StorageT)
#define NODE_CLASS_GEOMETRY
#define GEO_NODE_DISTRIBUTE_POINTS_IN_VOLUME
General operations for point clouds.
PointCloud * BKE_pointcloud_new_nomain(int totpoint)
int BKE_volume_num_grids(const Volume *volume)
bool BKE_volume_load(const Volume *volume, const Main *bmain)
const blender::bke::VolumeGridData * BKE_volume_grid_get(const Volume *volume, int grid_index)
#define BLT_I18NCONTEXT_ID_ID
GeometryNodeDistributePointsInVolumeMode
@ GEO_NODE_DISTRIBUTE_POINTS_IN_VOLUME_DENSITY_GRID
@ GEO_NODE_DISTRIBUTE_POINTS_IN_VOLUME_DENSITY_RANDOM
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_storage_enum_accessors(member)
BMesh const char void * data
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
static unsigned long seed
void append(const T &value)
void append(const T &value)
GSpanAttributeWriter lookup_or_add_for_write_only_span(StringRef attribute_id, AttrDomain domain, eCustomDataType data_type)
const Volume * get() const
VecBase< float, 3 > float3
void * MEM_callocN(size_t len, const char *str)
static void add(blender::Map< std::string, std::string > &messages, Message &msg)
void node_type_size(bNodeType &ntype, int width, int minwidth, int maxwidth)
void node_register_type(bNodeType &ntype)
void node_type_storage(bNodeType &ntype, std::optional< StringRefNull > storagename, void(*freefunc)(bNode *node), void(*copyfunc)(bNodeTree *dest_ntree, bNode *dest_node, const bNode *src_node))
void debug_randomize_point_order(PointCloud *pointcloud)
static void node_init(bNodeTree *, bNode *node)
static void node_geo_exec(GeoNodeExecParams params)
static void node_rna(StructRNA *srna)
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
static void node_register()
static void node_declare(NodeDeclarationBuilder &b)
PropertyRNA * RNA_def_node_enum(StructRNA *srna, const char *identifier, const char *ui_name, const char *ui_description, const EnumPropertyItem *static_items, const EnumRNAAccessors accessors, std::optional< int > default_value, const EnumPropertyItemFunc item_func, const bool allow_animation)
void node_geo_exec_with_missing_openvdb(GeoNodeExecParams ¶ms)
VecBase< float, 3 > float3
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
void node_free_standard_storage(bNode *node)
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
void replace_pointcloud(PointCloud *pointcloud, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
void keep_only_during_modify(Span< GeometryComponent::Type > component_types)
const GeometryComponent * get_component(GeometryComponent::Type component_type) const
void modify_geometry_sets(ForeachSubGeometryCallback callback)
MutableVArraySpan< T > span
std::string ui_description
void(* initfunc)(bNodeTree *ntree, bNode *node)
NodeGeometryExecFunction geometry_node_execute
const char * enum_name_legacy
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
NodeDeclareFunction declare
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)