44 "When combined with each voxel's value, determines the number of points "
45 "to sample per unit volume");
47 "Seed used by the random number generator to generate random points");
49 .default_value({0.3, 0.3, 0.3})
52 .description(
"Spacing between grid points");
57 .
description(
"Minimum density of a voxel to contain a grid point");
60 const bNode *node =
b.node_or_null();
61 if (node !=
nullptr) {
83class PositionsVDBWrapper {
90 : offset_fix_(offset_fix), vector_(
vector)
93 PositionsVDBWrapper(
const PositionsVDBWrapper &wrapper) =
default;
95 void add(
const openvdb::Vec3R &
pos)
103using RNGType = std::mt19937;
105using NonUniformPointScatterVDB =
106 openvdb::tools::NonUniformPointScatter<PositionsVDBWrapper, RNGType>;
108static void point_scatter_density_random(
const openvdb::FloatGrid &grid,
114 const float3 offset_fix = {0.5f * float(grid.voxelSize().x()),
115 0.5f * float(grid.voxelSize().y()),
116 0.5f * float(grid.voxelSize().z())};
118 PositionsVDBWrapper vdb_position_wrapper(r_positions, offset_fix);
119 RNGType random_generator(
seed);
120 NonUniformPointScatterVDB point_scatter(vdb_position_wrapper, density, random_generator);
124static void point_scatter_density_grid(
const openvdb::FloatGrid &grid,
126 const float threshold,
129 const openvdb::Vec3d half_voxel(0.5, 0.5, 0.5);
130 const openvdb::Vec3d voxel_spacing(
double(spacing.
x) / grid.voxelSize().x(),
131 double(spacing.
y) / grid.voxelSize().y(),
132 double(spacing.
z) / grid.voxelSize().z());
135 const double min_spacing = std::min({voxel_spacing.x(), voxel_spacing.y(), voxel_spacing.z()});
136 if (std::abs(min_spacing) < 0.0001) {
141 for (openvdb::FloatGrid::ValueOnCIter cell = grid.cbeginValueOn(); cell; ++cell) {
143 if (cell.getValue() < threshold) {
147 const openvdb::CoordBBox bbox = cell.getBoundingBox();
148 const openvdb::Vec3d box_min = bbox.min().asVec3d() - half_voxel;
149 const openvdb::Vec3d box_max = bbox.max().asVec3d() + half_voxel;
152 double abs_spacing_x = std::abs(voxel_spacing.x());
153 double abs_spacing_y = std::abs(voxel_spacing.y());
154 double abs_spacing_z = std::abs(voxel_spacing.z());
155 const openvdb::Vec3d start(
ceil(box_min.x() / abs_spacing_x) * abs_spacing_x,
156 ceil(box_min.y() / abs_spacing_y) * abs_spacing_y,
157 ceil(box_min.z() / abs_spacing_z) * abs_spacing_z);
160 for (
double x = start.x();
x < box_max.x();
x += abs_spacing_x) {
161 for (
double y = start.y();
y < box_max.y();
y += abs_spacing_y) {
162 for (
double z = start.z();
z < box_max.z();
z += abs_spacing_z) {
164 const openvdb::Vec3d idx_pos(
x,
y,
z);
165 const openvdb::Vec3d local_pos = grid.indexToWorld(idx_pos + half_voxel);
166 r_positions.
append({float(local_pos.x()), float(local_pos.y()), float(local_pos.z())});
180 params.set_default_remaining_outputs();
184 bke::VolumeTreeAccessToken tree_token;
185 const openvdb::GridBase &base_grid = volume_grid.grid(tree_token);
186 if (!base_grid.isType<openvdb::FloatGrid>()) {
187 params.set_default_remaining_outputs();
190 const openvdb::FloatGrid &grid =
static_cast<const openvdb::FloatGrid &
>(base_grid);
199 density =
params.extract_input<
float>(
"Density");
204 threshold =
params.extract_input<
float>(
"Threshold");
210 point_scatter_density_random(grid, density,
seed, positions);
213 point_scatter_density_grid(grid, spacing, threshold, positions);
218 pointcloud->positions_for_write().copy_from(positions);
235 "Distribute points randomly inside of the volume"},
240 "Distribute the points in a grid pattern inside of the volume"},
241 {0,
nullptr, 0,
nullptr,
nullptr},
246 "Distribution Method",
247 "Method to use for scattering points",
258 ntype.
ui_name =
"Distribute Points in Grid";
static unsigned long seed
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 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)