Blender V5.3
node_geo_sample_grid.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 "GEO_grid_samplers.hh"
12
13#include "NOD_rna_define.hh"
15
17#include "UI_resources.hh"
18
19#include "RNA_enum_types.hh"
20
21#include "node_geometry_util.hh"
22
24
31
33 {int(InterpolationMode::Nearest), "NEAREST", 0, N_("Nearest Neighbor"), ""},
34 {int(InterpolationMode::TriLinear), "TRILINEAR", 0, N_("Trilinear"), ""},
35 {int(InterpolationMode::TriQuadratic), "TRIQUADRATIC", 0, N_("Triquadratic"), ""},
36 {int(InterpolationMode::TriCubic), "TRICUBIC", 0, N_("Tricubic"), ""},
37 {0, nullptr, 0, nullptr, nullptr},
38};
39
41{
42 const bNode *node = b.node_or_null();
43 if (!node) {
44 return;
45 }
46 const eNodeSocketDatatype data_type = eNodeSocketDatatype(node->custom1);
47
48 b.add_input(data_type, "Grid"_ustr).hide_value().structure_type(StructureType::Grid);
49 auto &position = b.add_input<decl::Vector>("Position"_ustr)
50 .default_input_type(NODE_DEFAULT_INPUT_POSITION_FIELD)
51 .structure_type(StructureType::Dynamic);
52 b.add_input<decl::Menu>("Interpolation"_ustr)
53 .static_items(interpolation_mode_items)
55 .optional_label()
56 .description("How to interpolate the values between neighboring voxels");
57
58 const std::array<int, 1> dynamic_inputs = {position.index()};
59 b.add_output(data_type, "Value"_ustr)
60 .inferred_structure_type(dynamic_inputs)
61 .propagate_references(dynamic_inputs);
62}
63
64static std::optional<eNodeSocketDatatype> node_type_for_socket_type(const bNodeSocket &socket)
65{
66 switch (socket.type) {
67 case SOCK_FLOAT:
68 return SOCK_FLOAT;
69 case SOCK_BOOLEAN:
70 return SOCK_BOOLEAN;
71 case SOCK_INT:
72 return SOCK_INT;
73 case SOCK_VECTOR:
74 case SOCK_RGBA:
75 return SOCK_VECTOR;
76 default:
77 return std::nullopt;
78 }
79}
80
82{
83 const std::optional<eNodeSocketDatatype> node_type = node_type_for_socket_type(
84 params.other_socket());
85 if (!node_type) {
86 return;
87 }
88 if (params.in_out() == SOCK_IN) {
89 params.add_item(IFACE_("Grid"), [node_type](LinkSearchOpParams &params) {
90 bNode &node = params.add_node("GeometryNodeSampleGrid"_ustr);
91 node.custom1 = *node_type;
92 params.update_and_connect_available_socket(node, "Grid"_ustr);
93 });
94 const eNodeSocketDatatype other_type = params.other_socket().type;
95 if (params.node_tree().typeinfo->validate_link(other_type, SOCK_VECTOR)) {
96 params.add_item(IFACE_("Position"), [](LinkSearchOpParams &params) {
97 bNode &node = params.add_node("GeometryNodeSampleGrid"_ustr);
98 params.update_and_connect_available_socket(node, "Position"_ustr);
99 });
100 }
101 }
102 else {
103 params.add_item(IFACE_("Value"), [node_type](LinkSearchOpParams &params) {
104 bNode &node = params.add_node("GeometryNodeSampleGrid"_ustr);
105 node.custom1 = *node_type;
106 params.update_and_connect_available_socket(node, "Value"_ustr);
107 });
108 }
109}
110
111static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
112{
113 layout.prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
114}
115
116#ifdef WITH_OPENVDB
117
118template<typename T>
119void sample_grid(const bke::OpenvdbGridType<T> &grid,
120 const InterpolationMode interpolation,
121 const Span<float3> positions,
122 const IndexMask &mask,
123 MutableSpan<T> dst)
124{
125 using GridType = bke::OpenvdbGridType<T>;
126 using GridValueT = typename GridType::ValueType;
127 using AccessorT = typename GridType::ConstUnsafeAccessor;
128 using TraitsT = typename bke::VolumeGridTraits<T>;
129 AccessorT accessor = grid.getConstUnsafeAccessor();
130
131 auto sample_data = [&]<typename Sampler>() {
132 mask.foreach_index([&](const int64_t i) {
133 const float3 &pos = positions[i];
134 const openvdb::Vec3R world_pos(pos.x, pos.y, pos.z);
135 const openvdb::Vec3R index_pos = grid.transform().worldToIndex(world_pos);
136 GridValueT value;
137 Sampler::sample(accessor, index_pos, value);
138 dst[i] = TraitsT::to_blender(value);
139 });
140 };
141
142 /* Use to the Nearest Neighbor sampler for Bool grids (no interpolation). */
143 InterpolationMode real_interpolation = interpolation;
144 if constexpr (std::is_same_v<T, bool>) {
145 real_interpolation = InterpolationMode::Nearest;
146 }
147 switch (real_interpolation) {
149 sample_data.template operator()<geometry::NearestPointSampler>();
150 break;
151 }
153 sample_data.template operator()<geometry::LinearSampler>();
154 break;
155 }
157 sample_data.template operator()<geometry::QuadraticBSplineSampler>();
158 break;
159 }
161 sample_data.template operator()<geometry::CubicBSplineSampler>();
162 break;
163 }
164 }
165}
166
167class SampleGridFunction : public mf::MultiFunction {
168 bke::GVolumeGrid grid_;
169 InterpolationMode interpolation_;
170 mf::Signature signature_;
171 VolumeGridType grid_type_;
173 bke::VolumeTreeAccessToken tree_token_;
174 const openvdb::GridBase *grid_base_ = nullptr;
175
176 public:
177 SampleGridFunction(bke::GVolumeGrid grid, InterpolationMode interpolation)
178 : grid_(std::move(grid)), interpolation_(interpolation)
179 {
180 BLI_assert(grid_);
181
182 const std::optional<eNodeSocketDatatype> data_type = bke::grid_type_to_socket_type(
183 grid_->grid_type());
184 const CPPType *cpp_type = bke::socket_type_to_geo_nodes_base_cpp_type(*data_type);
185 mf::SignatureBuilder builder{"Sample Grid", signature_};
186 builder.single_input<float3>("Position");
187 builder.single_output("Value", *cpp_type);
188 this->set_signature(&signature_);
189
190 grid_base_ = &grid_->grid(tree_token_);
191 grid_type_ = grid_->grid_type();
192 }
193
194 void call(const IndexMask &mask, mf::Params params, mf::Context /*context*/) const override
195 {
196 const VArraySpan<float3> positions = params.readonly_single_input<float3>(0, "Position");
197 GMutableSpan dst = params.uninitialized_single_output(1, "Value");
198
199 BKE_volume_grid_type_to_blender_value_type(grid_type_, [&]<typename T>() {
200 if constexpr (is_same_any_v<T, bool, float, int, float3>) {
201 sample_grid<T>(static_cast<const bke::OpenvdbGridType<T> &>(*grid_base_),
202 interpolation_,
203 positions,
204 mask,
205 dst.typed<T>());
206 }
207 });
208 }
209};
210
211#endif /* WITH_OPENVDB */
212
214{
215#ifdef WITH_OPENVDB
216 bke::GVolumeGrid grid = params.extract_input<bke::GVolumeGrid>("Grid"_ustr);
217 if (!grid) {
219 return;
220 }
221
222 const auto interpolation = params.get_input<InterpolationMode>("Interpolation"_ustr);
224 "Position"_ustr);
225
226 std::string error_message;
227 bke::SocketValueVariant output_value;
229 std::make_shared<SampleGridFunction>(std::move(grid), interpolation),
230 {&position},
231 {&output_value},
232 params.user_data(),
233 error_message))
234 {
236 params.error_message_add(NodeWarningType::Error, std::move(error_message));
237 return;
238 }
239
240 params.set_output("Value"_ustr, std::move(output_value));
241#else
243#endif
244}
245
246static void node_init(bNodeTree * /*tree*/, bNode *node)
247{
248 node->custom1 = SOCK_FLOAT;
249}
250
251static void node_rna(StructRNA *srna)
252{
254 "data_type",
255 "Data Type",
256 "Node socket data type",
261}
262
263static void node_register()
264{
265 static bke::bNodeType ntype;
266
267 geo_node_type_base(&ntype, "GeometryNodeSampleGrid"_ustr, GEO_NODE_SAMPLE_GRID);
268 ntype.ui_name = "Sample Grid";
269 ntype.ui_description = "Retrieve values from the specified volume grid";
270 ntype.enum_name_legacy = "SAMPLE_GRID";
272 ntype.initfunc = node_init;
273 ntype.declare = node_declare;
279
280 node_rna(ntype.rna_ext.srna);
281}
283
284} // namespace blender::nodes::node_geo_sample_grid_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_SAMPLE_GRID
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
#define BLI_assert(a)
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)
T get_input(const UString identifier) const
GeoNodesUserData * user_data() const
void set_output(const UString identifier, T &&value)
void update_and_connect_available_socket(bNode &new_node, UString socket_name)
#define position
VecBase< float, 3 > float3
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
#define T
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
static std::optional< eNodeSocketDatatype > node_type_for_socket_type(const bNodeSocket &socket)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static const EnumPropertyItem interpolation_mode_items[]
static void node_geo_exec(GeoNodeExecParams params)
static void node_declare(NodeDeclarationBuilder &b)
static void node_init(bNodeTree *, bNode *node)
static void node_gather_link_search_ops(GatherLinkSearchOpParams &params)
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)
__int64 int64_t
Definition BKE_text.h:90
const EnumPropertyItem rna_enum_node_socket_data_type_items[]
VecBase< float, 3 > float3
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)
#define N_(msgid)