Blender V4.5
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
11#include "NOD_rna_define.hh"
13
14#include "UI_interface.hh"
15#include "UI_resources.hh"
16
17#include "RNA_enum_types.hh"
18
19#include "node_geometry_util.hh"
20
22
24{
25 const bNode *node = b.node_or_null();
26 if (!node) {
27 return;
28 }
29 const eNodeSocketDatatype data_type = eNodeSocketDatatype(node->custom1);
30
31 b.add_input(data_type, "Grid").hide_value().structure_type(StructureType::Grid);
32 b.add_input<decl::Int>("X").supports_field();
33 b.add_input<decl::Int>("Y").supports_field();
34 b.add_input<decl::Int>("Z").supports_field();
35
36 b.add_output(data_type, "Value").dependent_field({1, 2, 3});
37}
38
39static std::optional<eNodeSocketDatatype> node_type_for_socket_type(const bNodeSocket &socket)
40{
41 switch (socket.type) {
42 case SOCK_FLOAT:
43 return SOCK_FLOAT;
44 case SOCK_BOOLEAN:
45 return SOCK_BOOLEAN;
46 case SOCK_INT:
47 return SOCK_INT;
48 case SOCK_VECTOR:
49 case SOCK_RGBA:
50 return SOCK_VECTOR;
51 default:
52 return std::nullopt;
53 }
54}
55
57{
58 if (!USER_EXPERIMENTAL_TEST(&U, use_new_volume_nodes)) {
59 return;
60 }
61 const std::optional<eNodeSocketDatatype> node_type = node_type_for_socket_type(
62 params.other_socket());
63 if (!node_type) {
64 return;
65 }
66 if (params.in_out() == SOCK_IN) {
67 params.add_item(IFACE_("Grid"), [node_type](LinkSearchOpParams &params) {
68 bNode &node = params.add_node("GeometryNodeSampleGridIndex");
69 node.custom1 = *node_type;
70 params.update_and_connect_available_socket(node, "Grid");
71 });
72 const eNodeSocketDatatype other_type = eNodeSocketDatatype(params.other_socket().type);
73 if (params.node_tree().typeinfo->validate_link(other_type, SOCK_INT)) {
74 params.add_item(IFACE_("X"), [](LinkSearchOpParams &params) {
75 bNode &node = params.add_node("GeometryNodeSampleGridIndex");
76 params.update_and_connect_available_socket(node, "X");
77 });
78 params.add_item(IFACE_("Y"), [](LinkSearchOpParams &params) {
79 bNode &node = params.add_node("GeometryNodeSampleGridIndex");
80 params.update_and_connect_available_socket(node, "Y");
81 });
82 params.add_item(IFACE_("Z"), [](LinkSearchOpParams &params) {
83 bNode &node = params.add_node("GeometryNodeSampleGridIndex");
84 params.update_and_connect_available_socket(node, "Z");
85 });
86 }
87 }
88 else {
89 params.add_item(IFACE_("Value"), [node_type](LinkSearchOpParams &params) {
90 bNode &node = params.add_node("GeometryNodeSampleGridIndex");
91 node.custom1 = *node_type;
92 params.update_and_connect_available_socket(node, "Value");
93 });
94 }
95}
96
97static void node_layout(uiLayout *layout, bContext * /*C*/, PointerRNA *ptr)
98{
99 layout->prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
100}
101
102static void node_init(bNodeTree * /*tree*/, bNode *node)
103{
104 node->custom1 = SOCK_FLOAT;
105}
106
107#ifdef WITH_OPENVDB
108
109template<typename T>
110void sample_grid(const bke::OpenvdbGridType<T> &grid,
111 const Span<int> x,
112 const Span<int> y,
113 const Span<int> z,
114 const IndexMask &mask,
115 MutableSpan<T> dst)
116{
117 using GridType = bke::OpenvdbGridType<T>;
118 using GridValueT = typename GridType::ValueType;
119 using AccessorT = typename GridType::ConstAccessor;
120 using TraitsT = typename bke::VolumeGridTraits<T>;
121 AccessorT accessor = grid.getConstAccessor();
122
123 mask.foreach_index([&](const int64_t i) {
124 GridValueT value = accessor.getValue(openvdb::Coord(x[i], y[i], z[i]));
125 dst[i] = TraitsT::to_blender(value);
126 });
127}
128
129template<typename Fn> void convert_to_static_type(const VolumeGridType type, const Fn &fn)
130{
131 switch (type) {
133 fn(bool());
134 break;
136 fn(float());
137 break;
138 case VOLUME_GRID_INT:
139 fn(int());
140 break;
141 case VOLUME_GRID_MASK:
142 fn(bool());
143 break;
145 fn(float3());
146 break;
147 default:
148 break;
149 }
150}
151
152class SampleGridIndexFunction : public mf::MultiFunction {
153 bke::GVolumeGrid grid_;
154 mf::Signature signature_;
155
156 public:
157 SampleGridIndexFunction(bke::GVolumeGrid grid) : grid_(std::move(grid))
158 {
159 BLI_assert(grid_);
160
161 const std::optional<eNodeSocketDatatype> data_type = bke::grid_type_to_socket_type(
162 grid_->grid_type());
163 const CPPType *cpp_type = bke::socket_type_to_geo_nodes_base_cpp_type(*data_type);
164 mf::SignatureBuilder builder{"Sample Grid Index", signature_};
165 builder.single_input<int>("X");
166 builder.single_input<int>("Y");
167 builder.single_input<int>("Z");
168 builder.single_output("Value", *cpp_type);
169 this->set_signature(&signature_);
170 }
171
172 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const final
173 {
174 const VArraySpan<int> x = params.readonly_single_input<int>(0, "X");
175 const VArraySpan<int> y = params.readonly_single_input<int>(1, "Y");
176 const VArraySpan<int> z = params.readonly_single_input<int>(2, "Z");
177 GMutableSpan dst = params.uninitialized_single_output(3, "Value");
178
179 bke::VolumeTreeAccessToken tree_token;
180 convert_to_static_type(grid_->grid_type(), [&](auto dummy) {
181 using T = decltype(dummy);
182 sample_grid<T>(grid_.typed<T>().grid(tree_token), x, y, z, mask, dst.typed<T>());
183 });
184 }
185};
186
187#endif /* WITH_OPENVDB */
188
190{
191#ifdef WITH_OPENVDB
192 const bNode &node = params.node();
193 const eNodeSocketDatatype data_type = eNodeSocketDatatype(node.custom1);
194
195 bke::GVolumeGrid grid = params.extract_input<bke::GVolumeGrid>("Grid");
196 if (!grid) {
197 params.set_default_remaining_outputs();
198 return;
199 }
200
201 auto fn = std::make_shared<SampleGridIndexFunction>(std::move(grid));
202 auto op = FieldOperation::Create(std::move(fn),
203 {params.extract_input<Field<int>>("X"),
204 params.extract_input<Field<int>>("Y"),
205 params.extract_input<Field<int>>("Z")});
206
208 const CPPType &output_type = *bke::socket_type_to_geo_nodes_base_cpp_type(data_type);
209 const GField output_field = conversions.try_convert(fn::GField(std::move(op)), output_type);
210 params.set_output("Value", std::move(output_field));
211
212#else
214#endif
215}
216
218 PointerRNA * /*ptr*/,
219 PropertyRNA * /*prop*/,
220 bool *r_free)
221{
222 *r_free = true;
223 return enum_items_filter(
226 });
227}
228
229static void node_rna(StructRNA *srna)
230{
232 "data_type",
233 "Data Type",
234 "Node socket data type",
239}
240
241static void node_register()
242{
243 static blender::bke::bNodeType ntype;
244
245 geo_node_type_base(&ntype, "GeometryNodeSampleGridIndex", GEO_NODE_SAMPLE_GRID_INDEX);
246 ntype.ui_name = "Sample Grid Index";
247 ntype.ui_description = "Retrieve volume grid values at specific voxels";
248 ntype.enum_name_legacy = "SAMPLE_GRID_INDEX";
250 ntype.initfunc = node_init;
251 ntype.declare = node_declare;
257
258 node_rna(ntype.rna_ext.srna);
259}
261
262} // namespace blender::nodes::node_geo_sample_grid_index_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:439
#define GEO_NODE_SAMPLE_GRID_INDEX
VolumeGridType
@ VOLUME_GRID_VECTOR_FLOAT
@ VOLUME_GRID_MASK
@ VOLUME_GRID_BOOLEAN
@ VOLUME_GRID_INT
@ VOLUME_GRID_FLOAT
#define BLI_assert(a)
Definition BLI_assert.h:46
#define ELEM(...)
#define IFACE_(msgid)
@ SOCK_IN
eNodeSocketDatatype
@ SOCK_INT
@ SOCK_VECTOR
@ SOCK_BOOLEAN
@ SOCK_FLOAT
@ SOCK_RGBA
#define USER_EXPERIMENTAL_TEST(userdef, member)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
#define U
long long int int64_t
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
static std::shared_ptr< FieldOperation > Create(std::shared_ptr< const mf::MultiFunction > function, Vector< GField > inputs={})
Definition FN_field.hh:242
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
void convert_to_static_type(const CPPType &cpp_type, const Func &func)
const DataTypeConversions & get_implicit_type_conversions()
void node_register_type(bNodeType &ntype)
Definition node.cc:2748
const CPPType * socket_type_to_geo_nodes_base_cpp_type(eNodeSocketDatatype type)
Definition node.cc:5413
static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
static void node_gather_link_search_ops(GatherLinkSearchOpParams &params)
static const EnumPropertyItem * data_type_filter_fn(bContext *, PointerRNA *, PropertyRNA *, bool *r_free)
static void node_layout(uiLayout *layout, bContext *, PointerRNA *ptr)
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)
void node_geo_exec_with_missing_openvdb(GeoNodeExecParams &params)
const EnumPropertyItem * enum_items_filter(const EnumPropertyItem *original_item_array, FunctionRef< bool(const EnumPropertyItem &item)> fn)
VecBase< float, 3 > float3
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
const EnumPropertyItem rna_enum_node_socket_data_type_items[]
StructRNA * srna
Definition RNA_types.hh:909
int16_t custom1
Defines a node type.
Definition BKE_node.hh:226
std::string ui_description
Definition BKE_node.hh:232
void(* initfunc)(bNodeTree *ntree, bNode *node)
Definition BKE_node.hh:277
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:347
const char * enum_name_legacy
Definition BKE_node.hh:235
void(* draw_buttons)(uiLayout *, bContext *C, PointerRNA *ptr)
Definition BKE_node.hh:247
NodeGatherSocketLinkOperationsFunction gather_link_search_ops
Definition BKE_node.hh:371
NodeDeclareFunction declare
Definition BKE_node.hh:355
void prop(PointerRNA *ptr, PropertyRNA *prop, int index, int value, eUI_Item_Flag flag, std::optional< blender::StringRef > name_opt, int icon, std::optional< blender::StringRef > placeholder=std::nullopt)
i
Definition text_draw.cc:230
PointerRNA * ptr
Definition wm_files.cc:4226