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},
46 .supported_type(GeometryComponent::Type::Volume)
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")
88class PositionsVDBWrapper {
94 PositionsVDBWrapper(
const PositionsVDBWrapper &wrapper) =
default;
96 void add(
const openvdb::Vec3R &
pos)
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);
184 density =
params.extract_input<
float>(
"Density");
189 threshold =
params.extract_input<
float>(
"Threshold");
194 geometry_set.
keep_only({GeometryComponent::Type::Edit});
205 if (volume_grid ==
nullptr) {
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);
232 point_radii.
span.fill(0.05f);
238 geometry_set.
keep_only({GeometryComponent::Type::PointCloud, GeometryComponent::Type::Edit});
241 params.set_output(
"Points", std::move(geometry_set));
253 ntype.
ui_name =
"Distribute Points in Volume";
258 "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)
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, AttrType data_type)
const Volume * get() const
std::optional< std::string > translation_context
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 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)
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 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
MutableVArraySpan< T > span
std::string ui_description
void(* initfunc)(bNodeTree *ntree, bNode *node)
NodeGeometryExecFunction geometry_node_execute
const char * enum_name_legacy
NodeDeclareFunction declare