6# include <openvdb/openvdb.h>
7# include <openvdb/tools/Interpolation.h>
8# include <openvdb/tools/PointScatter.h>
34 N_(
"Distribute points randomly inside of the volume")},
39 N_(
"Distribute the points in a grid pattern inside of the volume")},
40 {0,
nullptr, 0,
nullptr,
nullptr},
48 .description(
"Volume with fog grids that points are scattered in");
52 .description(
"Method to use for scattering points");
58 .description(
"Number of points to sample per unit volume")
63 .description(
"Seed used by the random number generator to generate random points")
66 .default_value({0.3, 0.3, 0.3})
69 .description(
"Spacing between grid points")
75 .
description(
"Minimum density of a volume cell to contain a grid point")
83 node->storage = MEM_new<NodeGeometryDistributePointsInVolume>(__func__);
88class PositionsVDBWrapper {
94 PositionsVDBWrapper(
const PositionsVDBWrapper &wrapper) =
default;
96 void add(
const openvdb::Vec3R &pos)
98 vector_.
append(
float3(
float(pos[0]),
float(pos[1]),
float(pos[2])));
105using RNGType = std::mt19937;
107using NonUniformPointScatterVDB =
108 openvdb::tools::NonUniformPointScatter<PositionsVDBWrapper, RNGType>;
110static void point_scatter_density_random(
const openvdb::FloatGrid &grid,
113 Vector<float3> &r_positions)
116 PositionsVDBWrapper vdb_position_wrapper = PositionsVDBWrapper(r_positions);
117 RNGType random_generator(seed);
118 NonUniformPointScatterVDB point_scatter(vdb_position_wrapper, density, random_generator);
122static void point_scatter_density_grid(
const openvdb::FloatGrid &grid,
124 const float threshold,
125 Vector<float3> &r_positions)
127 const openvdb::Vec3d half_voxel(0.5, 0.5, 0.5);
128 const openvdb::Vec3d voxel_spacing(
double(spacing.
x) / grid.voxelSize().x(),
129 double(spacing.
y) / grid.voxelSize().y(),
130 double(spacing.
z) / grid.voxelSize().z());
133 const double min_spacing = std::min({voxel_spacing.x(), voxel_spacing.y(), voxel_spacing.z()});
134 if (std::abs(min_spacing) < 0.0001) {
139 for (openvdb::FloatGrid::ValueOnCIter cell = grid.cbeginValueOn(); cell; ++cell) {
141 if (cell.getValue() < threshold) {
145 const openvdb::CoordBBox bbox = cell.getBoundingBox();
146 const openvdb::Vec3d box_min = bbox.min().asVec3d() - half_voxel;
147 const openvdb::Vec3d box_max = bbox.max().asVec3d() + half_voxel;
150 double abs_spacing_x = std::abs(voxel_spacing.x());
151 double abs_spacing_y = std::abs(voxel_spacing.y());
152 double abs_spacing_z = std::abs(voxel_spacing.z());
153 const openvdb::Vec3d start(
ceil(box_min.x() / abs_spacing_x) * abs_spacing_x,
154 ceil(box_min.y() / abs_spacing_y) * abs_spacing_y,
155 ceil(box_min.z() / abs_spacing_z) * abs_spacing_z);
158 for (
double x = start.x();
x < box_max.x();
x += abs_spacing_x) {
159 for (
double y = start.y();
y < box_max.y();
y += abs_spacing_y) {
160 for (
double z = start.z();
z < box_max.z();
z += abs_spacing_z) {
162 const openvdb::Vec3d idx_pos(
x,
y,
z);
163 const openvdb::Vec3d local_pos = grid.indexToWorld(idx_pos);
164 r_positions.append({
float(local_pos.x()),
float(local_pos.y()),
float(local_pos.z())});
209 bke::VolumeTreeAccessToken tree_token;
210 const openvdb::GridBase &base_grid =
volume_grid->grid(tree_token);
212 if (!base_grid.isType<openvdb::FloatGrid>()) {
216 const openvdb::FloatGrid &grid =
static_cast<const openvdb::FloatGrid &
>(base_grid);
219 point_scatter_density_random(grid, density, seed, positions);
222 point_scatter_density_grid(grid, spacing, threshold, positions);
228 pointcloud->positions_for_write().copy_from(positions);
237 params.
set_output(
"Points"_ustr, std::move(geometry_set));
249 ntype.
ui_name =
"Distribute Points in Volume";
254 "NodeGeometryDistributePointsInVolume",
#define NODE_STORAGE_FUNCS(StorageT)
#define NODE_CLASS_GEOMETRY
#define GEO_NODE_DISTRIBUTE_POINTS_IN_VOLUME
General operations for point clouds.
#define BLT_I18NCONTEXT_ID_ID
#define NOD_REGISTER_NODE(REGISTER_FUNC)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
constexpr IndexRange()=default
void append(const T &value)
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
const Volume * get() const
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
std::optional< std::string > translation_context
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 foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
void debug_randomize_point_order(PointCloud *pointcloud)
static const EnumPropertyItem mode_items[]
static void node_init(bNodeTree *, bNode *node)
static void node_geo_exec(GeoNodeExecParams params)
static void node_register()
static void node_declare(NodeDeclarationBuilder &b)
void node_geo_exec_with_missing_openvdb(GeoNodeExecParams ¶ms)
int BKE_volume_num_grids(const Volume *volume)
const bke::VolumeGridData * BKE_volume_grid_get(const Volume *volume, int grid_index)
bool BKE_volume_load(const Volume *volume, const Main *bmain)
GeometryNodeDistributePointsInVolumeMode
@ GEO_NODE_DISTRIBUTE_POINTS_IN_VOLUME_DENSITY_RANDOM
@ GEO_NODE_DISTRIBUTE_POINTS_IN_VOLUME_DENSITY_GRID
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
PointCloud * BKE_pointcloud_new_nomain(PointCloudType type, int totpoint)
void node_free_standard_storage(bNode *node)
VecBase< float, 3 > float3
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
void keep_only(Span< GeometryComponent::Type > component_types)
void replace_pointcloud(PointCloud *pointcloud, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
const GeometryComponent * get_component(GeometryComponent::Type component_type) const
static constexpr int _180
std::string ui_description
NodeGeometryExecFunction geometry_node_execute
void(*) initfunc(bNodeTree *ntree, bNode *node)
const char * enum_name_legacy
NodeDeclareFunction declare