44 "When combined with each voxel's value, determines the number of points "
45 "to sample per unit volume")
52 .description(
"Seed used by the random number generator to generate random points")
55 .default_value({0.3, 0.3, 0.3})
58 .description(
"Spacing between grid points")
61 auto &threshold =
b.add_input<
decl::Float>(
"Threshold"_ustr)
65 .
description(
"Minimum density of a voxel to contain a grid point")
71 if (
node !=
nullptr) {
92class PositionsVDBWrapper {
98 PositionsVDBWrapper(
const PositionsVDBWrapper &wrapper) =
default;
100 void add(
const openvdb::Vec3R &pos)
102 vector_.
append(
float3(
float(pos[0]),
float(pos[1]),
float(pos[2])));
108using RNGType = std::mt19937;
110using NonUniformPointScatterVDB =
111 openvdb::tools::NonUniformPointScatter<PositionsVDBWrapper, RNGType>;
113static void point_scatter_density_random(
const openvdb::FloatGrid &grid,
116 Vector<float3> &r_positions)
119 PositionsVDBWrapper vdb_position_wrapper(r_positions);
120 RNGType random_generator(seed);
121 NonUniformPointScatterVDB point_scatter(vdb_position_wrapper, density, random_generator);
125static void point_scatter_density_grid(
const openvdb::FloatGrid &grid,
127 const float threshold,
128 Vector<float3> &r_positions)
130 const openvdb::Vec3d half_voxel(0.5, 0.5, 0.5);
131 const openvdb::Vec3d voxel_spacing(
double(spacing.
x) / grid.voxelSize().x(),
132 double(spacing.
y) / grid.voxelSize().y(),
133 double(spacing.
z) / grid.voxelSize().z());
136 const double min_spacing = std::min({voxel_spacing.x(), voxel_spacing.y(), voxel_spacing.z()});
137 if (std::abs(min_spacing) < 0.0001) {
142 for (openvdb::FloatGrid::ValueOnCIter cell = grid.cbeginValueOn(); cell; ++cell) {
144 if (cell.getValue() < threshold) {
148 const openvdb::CoordBBox bbox = cell.getBoundingBox();
149 const openvdb::Vec3d box_min = bbox.min().asVec3d() - half_voxel;
150 const openvdb::Vec3d box_max = bbox.max().asVec3d() + half_voxel;
153 double abs_spacing_x = std::abs(voxel_spacing.x());
154 double abs_spacing_y = std::abs(voxel_spacing.y());
155 double abs_spacing_z = std::abs(voxel_spacing.z());
156 const openvdb::Vec3d start(
ceil(box_min.x() / abs_spacing_x) * abs_spacing_x,
157 ceil(box_min.y() / abs_spacing_y) * abs_spacing_y,
158 ceil(box_min.z() / abs_spacing_z) * abs_spacing_z);
161 for (
double x = start.x();
x < box_max.x();
x += abs_spacing_x) {
162 for (
double y = start.y();
y < box_max.y();
y += abs_spacing_y) {
163 for (
double z = start.z();
z < box_max.z();
z += abs_spacing_z) {
165 const openvdb::Vec3d idx_pos(
x,
y,
z);
166 const openvdb::Vec3d local_pos = grid.indexToWorld(idx_pos);
167 r_positions.append({
float(local_pos.x()),
float(local_pos.y()),
float(local_pos.z())});
186 bke::VolumeTreeAccessToken tree_token;
187 const openvdb::GridBase &base_grid =
volume_grid.grid(tree_token);
188 if (!base_grid.isType<openvdb::FloatGrid>()) {
192 const openvdb::FloatGrid &grid =
static_cast<const openvdb::FloatGrid &
>(base_grid);
212 point_scatter_density_random(grid, density, seed, positions);
215 point_scatter_density_grid(grid, spacing, threshold, positions);
220 pointcloud->positions_for_write().copy_from(positions);
237 "Distribute points randomly inside of the volume"},
242 "Distribute the points in a grid pattern inside of the volume"},
243 {0,
nullptr, 0,
nullptr,
nullptr},
248 "Distribution Method",
249 "Method to use for scattering points",
260 ntype.
ui_name =
"Distribute Points in Grid";
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< StringRef > name_opt, int icon, std::optional< StringRef > placeholder=std::nullopt)