Blender V5.3
node_geo_rasterize_points.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2024 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <fmt/format.h>
6
7#include "BKE_volume.hh"
8#include "BKE_volume_grid.hh"
9
10#include "BLI_array_utils.hh"
11#include "BLI_generic_array.hh"
12#include "BLI_math_matrix.hh"
13
14#include "BLO_read_write.hh"
15
17
19#include "NOD_socket.hh"
24
25#include "UI_interface.hh"
26#include "UI_resources.hh"
27
28#include "RNA_enum_types.hh"
29#include "RNA_prototypes.hh"
30
31#include "node_geometry_util.hh"
32
34
36
38 /* Uniform voxel scale with no translation and rotation. */
40 /* Full grid matrix input. */
42};
43
46 "VOXEL_SIZE",
47 0,
48 N_("Voxel Size"),
49 N_("Use uniform voxel scale with no translation or rotation")},
51 "MATRIX",
52 0,
53 N_("Matrix"),
54 N_("Define grid transform with a matrix")},
55 {0, nullptr, 0, nullptr, nullptr},
56};
57
60 "CONSTANT",
61 0,
62 N_("Constant"),
63 N_("Assign points to the closest voxel")},
65 "LINEAR",
66 0,
67 N_("Linear"),
68 N_("Linear falloff over the voxel range")},
70 "QUADRATIC",
71 0,
72 N_("Quadratic B-Spline"),
73 N_("Quadratic b-spline kernel over 1.5 voxels")},
75 "CONSTANT",
76 0,
77 N_("Cubic B-Spline"),
78 N_("Cube b-spline kernel over 2 voxels")},
79 {0, nullptr, 0, nullptr, nullptr},
80};
81
94
96{
97 b.use_custom_socket_order();
98 b.allow_any_socket_order();
99
100 b.add_input<decl::Geometry>("Points"_ustr);
101 b.add_input<decl::Menu>("Grid Transform Mode"_ustr)
102 .static_items(grid_transform_mode_items)
104 .expanded()
105 .optional_label()
106 .description("Method of defining the grid transform");
107 b.add_input<decl::Float>("Voxel Size"_ustr)
108 .default_value(0.3f)
109 .min(0.01f)
111 .usage_by_menu("Grid Transform Mode"_ustr, int(GridTransformMode::VoxelSize));
112 b.add_input<decl::Matrix>("Matrix"_ustr)
113 .usage_by_menu("Grid Transform Mode"_ustr, int(GridTransformMode::Matrix));
114 b.add_input<decl::Menu>("Kernel Type"_ustr)
115 .static_items(kernel_type_items)
117 .optional_label()
118 .description("Kernel function for computing weights at each voxel");
119
120 b.add_input<decl::Vector>("Position"_ustr)
121 .default_input_type(NODE_DEFAULT_INPUT_POSITION_FIELD)
122 .structure_type(StructureType::Field);
123
124 b.add_separator();
125
126 const bNode *node = b.node_or_null();
127 const bNodeTree *tree = b.tree_or_null();
128 if (node && tree) {
129 const NodeGeometryRasterizePoints &storage = node_storage(*node);
130 for (const int i : IndexRange(storage.items_num)) {
131 const NodeGeometryRasterizePointsItem &item = storage.items[i];
132 const StringRef name = item.name ? item.name : "";
134 item);
137 const CPPType &attribute_type = geometry::points_rasterize_attribute_type(rasterize_type);
138 const CPPType &grid_type = geometry::points_rasterize_grid_type(rasterize_type);
140 attribute_type);
142 grid_type);
143
144 auto &input_decl = b.add_input(input_type, UString(name), UString(identifier));
145 input_decl.socket_name_ptr(
146 &tree->id, *RasterizePointsItemsAccessor::item_srna, &item, "name");
147 input_decl.evaluated_geometry_field();
148
149 b.add_output(output_type, UString(name), UString(identifier))
150 .structure_type(StructureType::Grid)
151 .align_with_previous();
152 }
153 }
154 b.add_input<decl::Extend>(""_ustr, "__extend__"_ustr).structure_type(StructureType::Field);
155 b.add_output<decl::Extend>(""_ustr, "__extend__"_ustr)
156 .structure_type(StructureType::Field)
157 .align_with_previous();
158}
159
160static void node_init(bNodeTree * /*tree*/, bNode *node)
161{
162 NodeGeometryRasterizePoints *data = MEM_new<NodeGeometryRasterizePoints>(__func__);
163 data->next_identifier = 0;
164
165 data->items = nullptr;
166 data->items_num = 0;
167
168 node->storage = data;
169}
170
172{
174 MEM_delete(reinterpret_cast<NodeGeometryRasterizePoints *>(node->storage));
175}
176
177static void node_copy_storage(bNodeTree * /*dst_tree*/, bNode *dst_node, const bNode *src_node)
178{
179 const NodeGeometryRasterizePoints &src_storage = node_storage(*src_node);
180 auto *dst_storage = MEM_new<NodeGeometryRasterizePoints>(__func__,
181 dna::shallow_copy(src_storage));
182 dst_node->storage = dst_storage;
183
185}
186
192
197
198static void node_layout_ex(ui::Layout &layout, bContext *C, PointerRNA *ptr)
199{
200 bNodeTree &ntree = *reinterpret_cast<bNodeTree *>(ptr->owner_id);
201 bNode &node = *static_cast<bNode *>(ptr->data);
202
203 if (ui::Layout *panel = layout.panel(C, "rasterize_items", false, IFACE_("Items"))) {
204 socket_items::ui::draw_items_list_with_operators<RasterizePointsItemsAccessor>(
205 C, panel, ntree, node);
206 socket_items::ui::draw_active_item_props<RasterizePointsItemsAccessor>(
207 ntree, node, [&](PointerRNA *item_ptr) {
208 panel->use_property_split_set(true);
209 panel->use_property_decorate_set(false);
210 panel->prop(item_ptr, "type", UI_ITEM_NONE, std::nullopt, ICON_NONE);
211 });
212 }
213}
214
216{
217#ifdef WITH_OPENVDB
218 const NodeGeometryRasterizePoints &storage = node_storage(params.node());
219
220 /* Same transform is used for the intermediate point data grid and all output grids. */
221 const GridTransformMode grid_transform_mode = params.extract_input<GridTransformMode>(
222 "Grid Transform Mode"_ustr);
223 float4x4 grid_transform;
224 switch (grid_transform_mode) {
226 grid_transform = math::from_scale<float4x4>(
227 float3(params.extract_input<float>("Voxel Size"_ustr)));
228 break;
230 grid_transform = params.extract_input<float4x4>("Matrix"_ustr);
231 break;
232 }
233 const double determinant = math::determinant(grid_transform);
236 return;
237 }
238
239 const geometry::KernelType kernel_type = params.get_input<geometry::KernelType>(
240 "Kernel Type"_ustr);
241 const GeometrySet geometry_set = params.extract_input<GeometrySet>("Points"_ustr);
242 const Field<float3> position_field = params.extract_input<Field<float3>>("Position"_ustr);
243 Vector<GField> fields_by_item;
244 for (const int i : IndexRange(storage.items_num)) {
245 const NodeGeometryRasterizePointsItem &item = storage.items[i];
246 const std::string identifier = RasterizePointsItemsAccessor::socket_identifier_for_item(item);
247
248 fields_by_item.append(params.extract_input<GField>(UString(identifier)));
249 }
250
254 Vector<int> points_offsets;
255 for (const GeometryComponent::Type type : component_types) {
256 const GeometryComponent *component = geometry_set.get_component(type);
257 points_offsets.append(component ? component->attribute_domain_size(AttrDomain::Point) : 0);
258 }
259 points_offsets.append(0);
261 points_offsets);
262 const int points_num = points_by_component.total_size();
263 if (points_num == 0) {
265 return;
266 }
267
268 Array<float3> positions(points_num);
269 Array<GArray<>> value_buffers(storage.items_num);
270 Vector<geometry::PointDataGridAttributeInfo> point_data_grid_attributes;
271 Vector<geometry::PointRasterizeAttributeInfo> point_rasterize_attributes;
272 for (const int i : IndexRange(storage.items_num)) {
273 const NodeGeometryRasterizePointsItem &item = storage.items[i];
276 const CPPType &attribute_type = geometry::points_rasterize_attribute_type(rasterize_type);
277
278 value_buffers[i] = GArray<>(attribute_type, points_num);
279 /* Note: Item name is unique and can be used as an attribute identifier. */
280 point_data_grid_attributes.append({item.name, value_buffers[i].as_span()});
281 point_rasterize_attributes.append({item.name, rasterize_type});
282 }
283
284 for (const int component_i : component_types.index_range()) {
285 const GeometryComponent *component = geometry_set.get_component(component_types[component_i]);
286 const IndexRange points = points_by_component[component_i];
287 if (points.is_empty()) {
288 continue;
289 }
290 BLI_assert(component != nullptr);
291
292 const bke::GeometryFieldContext field_context{*component, AttrDomain::Point};
293 fn::FieldEvaluator evaluator{field_context, points.size()};
294 evaluator.add_with_destination(position_field, positions.as_mutable_span().slice(points));
295 for (const int i : IndexRange(storage.items_num)) {
296 const GField &field = fields_by_item[i];
297 evaluator.add_with_destination(field, value_buffers[i].as_mutable_span().slice(points));
298 }
299 evaluator.evaluate();
300 }
301
302 geometry::MappedPointDataGrid point_data_grid = geometry::points_to_point_data_grid(
303 positions, point_data_grid_attributes, grid_transform);
304
305 // TODO only generate output grids that are actually needed.
306 Array<bke::GVolumeGrid> output_attribute_grids(storage.items_num);
307 geometry::points_rasterize(point_data_grid,
308 kernel_type,
309 point_rasterize_attributes,
310 grid_transform,
311 output_attribute_grids);
312 for (const int i : IndexRange(storage.items_num)) {
313 const NodeGeometryRasterizePointsItem &item = storage.items[i];
314 const std::string identifier = RasterizePointsItemsAccessor::socket_identifier_for_item(item);
315 if (output_attribute_grids[i]) {
316 params.set_output(UString(identifier), std::move(output_attribute_grids[i]));
317 }
318 else {
319 const std::string message = fmt::format(
320 fmt::runtime(TIP_("Could not generate grid for \"{}\"")), item.name);
322 }
323 }
325
326#else
328#endif
329}
330
331static void node_blend_write(const bNodeTree & /*tree*/, const bNode &node, BlendWriter &writer)
332{
334}
335
340
341static void node_register()
342{
343 static blender::bke::bNodeType ntype;
344
345 geo_node_type_base(&ntype, "GeometryNodeRasterizePoints"_ustr);
346 ntype.ui_name = "Rasterize Points";
347 ntype.ui_description = "Create volume grids from points with a weighted sum";
350 ntype.declare = node_declare;
351 ntype.initfunc = node_init;
355 ntype, "NodeGeometryRasterizePoints", node_free_storage, node_copy_storage);
357 // ntype.gather_link_search_ops = search_link_ops_for_volume_grid_node;
362}
364
365} // namespace blender::nodes::node_geo_rasterize_points
366
367namespace blender::nodes {
368
369StructRNA **RasterizePointsItemsAccessor::item_srna = &RNA_NodeGeometryRasterizePointsItem;
370
372{
373 writer->write_string(item.name);
374}
375
380
381} // namespace blender::nodes
382
383namespace blender {
384
385blender::Span<NodeGeometryRasterizePointsItem> NodeGeometryRasterizePoints::items_span() const
386{
388}
389
390blender::MutableSpan<NodeGeometryRasterizePointsItem> NodeGeometryRasterizePoints::items_span()
391{
393}
394
395} // namespace blender
#define NODE_STORAGE_FUNCS(StorageT)
Definition BKE_node.hh:1353
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
Volume data-block.
#define TIP_(msgid)
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define C
Definition RandGen.cpp:29
#define UI_ITEM_NONE
#define BLI_assert_unreachable()
Span< T > as_span() const
Definition BLI_array.hh:254
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
IndexRange index_range() const
Definition BLI_array.hh:371
GArray(Allocator allocator={}) noexcept
constexpr int64_t size() const
constexpr bool is_empty() const
constexpr IndexRange()=default
UString()=default
void append(const T &value)
int attribute_domain_size(AttrDomain domain) const
int add_with_destination(GField field, GVMutableArray dst)
void error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
T get_input(const UString identifier) const
void set_output(const UString identifier, T &&value)
#define storage(slot, qualifiers)
b[1] b &&[1] b &&[1] b &&[1] b &&[1] b[3] b &&[1] b[3] float determinant(MatBase< C, R >) RET
std::optional< eNodeSocketDatatype > geo_nodes_base_cpp_type_to_socket_type(const CPPType &type)
Definition node.cc:6008
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
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))
Definition node.cc:6166
internal::ShallowDataConstRef< T > shallow_copy(const T &other)
Definition DNA_defs.h:147
const CPPType & points_rasterize_grid_type(const PointRasterizeType rasterize_type)
const CPPType & points_rasterize_attribute_type(const PointRasterizeType rasterize_type)
MatT from_scale(const VecBase< typename MatT::base_type, ScaleDim > &scale)
T determinant(const MatBase< T, Size, Size > &mat)
static void node_layout_ex(ui::Layout &layout, bContext *C, PointerRNA *ptr)
static void node_declare(NodeDeclarationBuilder &b)
static void node_init(bNodeTree *, bNode *node)
static void node_geo_exec(GeoNodeExecParams params)
static void node_blend_read(bNodeTree &, bNode &node, BlendDataReader &reader)
static geometry::PointRasterizeType get_rasterize_item_type(const NodeGeometryRasterizePointsItemType type)
static void node_blend_write(const bNodeTree &, const bNode &node, BlendWriter &writer)
static void node_copy_storage(bNodeTree *, bNode *dst_node, const bNode *src_node)
static bool node_insert_link(bke::NodeInsertLinkParams &params)
void blend_write(BlendWriter *writer, const bNode &node)
void blend_read_data(BlendDataReader *reader, bNode &node)
void copy_array(const bNode &src_node, bNode &dst_node)
bool try_add_item_via_any_extend_socket(bNodeTree &ntree, bNode &extend_node, bNode &storage_node, bNodeLink &link, const std::optional< StringRef > socket_identifier=std::nullopt, typename Accessor::ItemT **r_new_item=nullptr)
void node_geo_exec_with_missing_openvdb(GeoNodeExecParams &params)
OffsetIndices< int > accumulate_counts_to_offsets(MutableSpan< int > counts_to_offsets, int start_offset=0)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
MatBase< float, 4, 4 > float4x4
BMesh const char void * data
bool BKE_volume_grid_determinant_valid(double determinant)
NodeGeometryRasterizePointsItemType
@ GEO_NODE_RASTERIZE_POINTS_ITEM_TYPE_SCALAR
@ GEO_NODE_RASTERIZE_POINTS_ITEM_TYPE_VECTOR
VecBase< float, 3 > float3
@ PROP_DISTANCE
Definition RNA_types.hh:313
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
void BLO_read_string(BlendDataReader *reader, char **ptr_p)
Definition readfile.cc:6653
void write_string(const char *data)
NodeGeometryRasterizePointsItem * items
static constexpr int _200
Definition BKE_node.hh:248
Defines a node type.
Definition BKE_node.hh:265
NodeBlendWriteFunction blend_write_storage_content
Definition BKE_node.hh:421
void(*) register_operators()
Definition BKE_node.hh:448
std::string ui_description
Definition BKE_node.hh:271
NodeBlendDataReadFunction blend_data_read_storage_content
Definition BKE_node.hh:422
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
void(*) draw_buttons_ex(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:292
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
NodeDeclareFunction declare
Definition BKE_node.hh:393
bool(*) insert_link(NodeInsertLinkParams &params)
Definition BKE_node.hh:364
static void blend_read_data_item(BlendDataReader *reader, ItemT &item)
static std::string socket_identifier_for_item(const NodeGeometryRasterizePointsItem &item)
static void blend_write_item(BlendWriter *writer, const ItemT &item)
PanelLayout panel(const bContext *C, StringRef idname, bool default_closed)
#define N_(msgid)