Blender V5.3
node_geo_sample_grid_index.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 "DNA_userdef_types.h"
6
8#include "BKE_volume_grid.hh"
10#include "BKE_volume_openvdb.hh"
11
12#include "NOD_rna_define.hh"
14
16#include "UI_resources.hh"
17
18#include "RNA_enum_types.hh"
19
20#include "node_geometry_util.hh"
21
23
25{
26 const bNode *node = b.node_or_null();
27 if (!node) {
28 return;
29 }
30 const eNodeSocketDatatype data_type = eNodeSocketDatatype(node->custom1);
31
32 b.add_input(data_type, "Grid"_ustr).hide_value().structure_type(StructureType::Grid);
33 auto &x = b.add_input<decl::Int>("X"_ustr).structure_type(StructureType::Dynamic);
34 auto &y = b.add_input<decl::Int>("Y"_ustr).structure_type(StructureType::Dynamic);
35 auto &z = b.add_input<decl::Int>("Z"_ustr).structure_type(StructureType::Dynamic);
36
37 const std::array<int, 3> dynamic_inputs = {x.index(), y.index(), z.index()};
38 b.add_output(data_type, "Value"_ustr)
39 .propagate_references(dynamic_inputs)
40 .inferred_structure_type(dynamic_inputs);
41}
42
43static std::optional<eNodeSocketDatatype> node_type_for_socket_type(const bNodeSocket &socket)
44{
45 switch (socket.type) {
46 case SOCK_FLOAT:
47 return SOCK_FLOAT;
48 case SOCK_BOOLEAN:
49 return SOCK_BOOLEAN;
50 case SOCK_INT:
51 return SOCK_INT;
52 case SOCK_VECTOR:
53 case SOCK_RGBA:
54 return SOCK_VECTOR;
55 default:
56 return std::nullopt;
57 }
58}
59
61{
62 const std::optional<eNodeSocketDatatype> node_type = node_type_for_socket_type(
63 params.other_socket());
64 if (!node_type) {
65 return;
66 }
67 if (params.in_out() == SOCK_IN) {
68 params.add_item(IFACE_("Grid"), [node_type](LinkSearchOpParams &params) {
69 bNode &node = params.add_node("GeometryNodeSampleGridIndex"_ustr);
70 node.custom1 = *node_type;
71 params.update_and_connect_available_socket(node, "Grid"_ustr);
72 });
73 const eNodeSocketDatatype other_type = params.other_socket().type;
74 if (params.node_tree().typeinfo->validate_link(other_type, SOCK_INT)) {
75 params.add_item(IFACE_("X"), [](LinkSearchOpParams &params) {
76 bNode &node = params.add_node("GeometryNodeSampleGridIndex"_ustr);
78 });
79 params.add_item(IFACE_("Y"), [](LinkSearchOpParams &params) {
80 bNode &node = params.add_node("GeometryNodeSampleGridIndex"_ustr);
82 });
83 params.add_item(IFACE_("Z"), [](LinkSearchOpParams &params) {
84 bNode &node = params.add_node("GeometryNodeSampleGridIndex"_ustr);
86 });
87 }
88 }
89 else {
90 params.add_item(IFACE_("Value"), [node_type](LinkSearchOpParams &params) {
91 bNode &node = params.add_node("GeometryNodeSampleGridIndex"_ustr);
92 node.custom1 = *node_type;
93 params.update_and_connect_available_socket(node, "Value"_ustr);
94 });
95 }
96}
97
98static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
99{
100 layout.prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
101}
102
103#ifdef WITH_OPENVDB
104
105class SampleGridIndexFunction : public mf::MultiFunction {
106 bke::GVolumeGrid grid_;
107 mf::Signature signature_;
108 VolumeGridType grid_type_;
110 bke::VolumeTreeAccessToken tree_token_;
111 const openvdb::GridBase *grid_base_ = nullptr;
112
113 public:
114 SampleGridIndexFunction(bke::GVolumeGrid grid) : grid_(std::move(grid))
115 {
116 BLI_assert(grid_);
117
118 const std::optional<eNodeSocketDatatype> data_type = bke::grid_type_to_socket_type(
119 grid_->grid_type());
120 const CPPType *cpp_type = bke::socket_type_to_geo_nodes_base_cpp_type(*data_type);
121 mf::SignatureBuilder builder{"Sample Grid Index", signature_};
122 builder.single_input<int>("X");
123 builder.single_input<int>("Y");
124 builder.single_input<int>("Z");
125 builder.single_output("Value", *cpp_type);
126 this->set_signature(&signature_);
127
128 grid_base_ = &grid_->grid(tree_token_);
129 grid_type_ = grid_->grid_type();
130 }
131
132 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const final
133 {
134 const VArraySpan<int> x = params.readonly_single_input<int>(0, "X");
135 const VArraySpan<int> y = params.readonly_single_input<int>(1, "Y");
136 const VArraySpan<int> z = params.readonly_single_input<int>(2, "Z");
137 GMutableSpan dst = params.uninitialized_single_output(3, "Value");
138
139 bke::volume_grid::sample_tree_indices(grid_type_, grid_base_->baseTree(), x, y, z, mask, dst);
140 }
141};
142
143#endif /* WITH_OPENVDB */
144
146{
147#ifdef WITH_OPENVDB
148 bke::GVolumeGrid grid = params.extract_input<bke::GVolumeGrid>("Grid"_ustr);
149 if (!grid) {
151 return;
152 }
153
154 auto x = params.extract_input<bke::SocketValueVariant>("X"_ustr);
155 auto y = params.extract_input<bke::SocketValueVariant>("Y"_ustr);
156 auto z = params.extract_input<bke::SocketValueVariant>("Z"_ustr);
157
158 std::string error_message;
159 bke::SocketValueVariant output_value;
161 std::make_shared<SampleGridIndexFunction>(std::move(grid)),
162 {&x, &y, &z},
163 {&output_value},
164 params.user_data(),
165 error_message))
166 {
168 params.error_message_add(NodeWarningType::Error, std::move(error_message));
169 return;
170 }
171
172 params.set_output("Value"_ustr, std::move(output_value));
173#else
175#endif
176}
177
178static void node_init(bNodeTree * /*tree*/, bNode *node)
179{
180 node->custom1 = SOCK_FLOAT;
181}
182
183static void node_rna(StructRNA *srna)
184{
186 "data_type",
187 "Data Type",
188 "Node socket data type",
193}
194
195static void node_register()
196{
197 static bke::bNodeType ntype;
198
199 geo_node_type_base(&ntype, "GeometryNodeSampleGridIndex"_ustr, GEO_NODE_SAMPLE_GRID_INDEX);
200 ntype.ui_name = "Sample Grid Index";
201 ntype.ui_description = "Retrieve volume grid values at specific voxels";
202 ntype.enum_name_legacy = "SAMPLE_GRID_INDEX";
204 ntype.initfunc = node_init;
205 ntype.declare = node_declare;
211
212 node_rna(ntype.rna_ext.srna);
213}
215
216} // namespace blender::nodes::node_geo_sample_grid_index_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_SAMPLE_GRID_INDEX
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
void error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
GeoNodesUserData * user_data() const
void set_output(const UString identifier, T &&value)
void update_and_connect_available_socket(bNode &new_node, UString socket_name)
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
std::optional< eNodeSocketDatatype > grid_type_to_socket_type(VolumeGridType type)
Definition node.cc:6096
const CPPType * socket_type_to_geo_nodes_base_cpp_type(eNodeSocketDatatype type)
Definition node.cc:5969
static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_gather_link_search_ops(GatherLinkSearchOpParams &params)
static std::optional< eNodeSocketDatatype > node_type_for_socket_type(const bNodeSocket &socket)
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)
bool execute_multi_function_on_value_variant(const MultiFunction &fn, const std::shared_ptr< MultiFunction > &owned_fn, const Span< SocketValueVariant * > input_values, const Span< SocketValueVariant * > output_values, GeoNodesUserData *user_data, std::string &r_error_message)
const EnumPropertyItem * grid_socket_type_items_filter_fn(bContext *, PointerRNA *, PropertyRNA *, bool *r_free)
void node_geo_exec_with_missing_openvdb(GeoNodeExecParams &params)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
const EnumPropertyItem rna_enum_node_socket_data_type_items[]
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
eNodeSocketDatatype type
bke::bNodeTreeType * typeinfo
bool(*) validate_link(eNodeSocketDatatype from, eNodeSocketDatatype to)
Definition BKE_node.hh:566
Defines a node type.
Definition BKE_node.hh:265
void(*) draw_buttons(ui::Layout &, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:290
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
void(*) initfunc(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:320
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:409
NodeDeclareFunction declare
Definition BKE_node.hh:393
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)