Blender V5.3
node_geo_sample_index.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_task.hh"
6
8
10#include "UI_resources.hh"
11
13
14#include "node_geometry_util.hh"
15
17
19
21{
22 const bNode *node = b.node_or_null();
23 b.add_input<decl::Geometry>("Geometry"_ustr)
24 .supported_type({GeometryComponent::Type::Mesh,
29 .description("Geometry to sample a value on");
30 if (node != nullptr) {
31 const eCustomDataType data_type = eCustomDataType(node_storage(*node).data_type);
32 b.add_input(data_type, "Value"_ustr).hide_value().evaluated_geometry_field();
33 }
34 auto &index = b.add_input<decl::Int>("Index"_ustr)
35 .description("Which element to retrieve a value from on the geometry")
36 .structure_type(StructureType::Dynamic);
37
38 if (node != nullptr) {
39 const eCustomDataType data_type = eCustomDataType(node_storage(*node).data_type);
40 std::array<int, 1> dynamic_inputs = {index.index()};
41 b.add_output(data_type, "Value"_ustr)
42 .inferred_structure_type(dynamic_inputs)
43 .propagate_references(dynamic_inputs);
44 }
45}
46
47static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
48{
49 layout.prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
50 layout.prop(ptr, "domain", UI_ITEM_NONE, "", ICON_NONE);
51 layout.prop(ptr, "clamp", UI_ITEM_NONE, std::nullopt, ICON_NONE);
52}
53
54static void node_init(bNodeTree * /*tree*/, bNode *node)
55{
56 NodeGeometrySampleIndex *data = MEM_new<NodeGeometrySampleIndex>(__func__);
57 data->data_type = CD_PROP_FLOAT;
58 data->domain = int8_t(AttrDomain::Point);
59 data->clamp = 0;
60 node->storage = data;
61}
62
64{
65 const NodeDeclaration &declaration = *params.node_type().static_declaration;
66 search_link_ops_for_declarations(params, declaration.inputs);
67
68 const std::optional<eCustomDataType> type = bke::socket_type_to_custom_data_type(
69 params.other_socket().type);
70 if (type && *type != CD_PROP_STRING) {
71 /* The input and output sockets have the same name. */
72 params.add_item(IFACE_("Value"), [type](LinkSearchOpParams &params) {
73 bNode &node = params.add_node("GeometryNodeSampleIndex"_ustr);
74 node_storage(node).data_type = *type;
75 params.update_and_connect_available_socket(node, "Value"_ustr);
76 });
77 }
78}
79
81{
82 GeometrySet geometry = params.extract_input<GeometrySet>("Geometry"_ustr);
83 const NodeGeometrySampleIndex &storage = node_storage(params.node());
84 const AttrDomain domain = AttrDomain(storage.domain);
85 const bool use_clamp = bool(storage.clamp);
86
87 GField value_field = params.extract_input<GField>("Value"_ustr);
88 SocketValueVariant index_value_variant = params.extract_input<SocketValueVariant>("Index"_ustr);
89 const CPPType &cpp_type = value_field.cpp_type();
90
92 domain);
93 if (!component) {
95 return;
96 }
97 if (index_value_variant.is_single()) {
98 /* Optimization for the case when the index is a single value. Here only that one index has to
99 * be evaluated. */
100 const int domain_size = component->attribute_domain_size(domain);
101 int index = index_value_variant.extract<int>();
102 if (use_clamp) {
103 index = std::clamp(index, 0, domain_size - 1);
104 }
105 const eNodeSocketDatatype socket_type = params.node().output_socket(0).typeinfo->type;
106 const CPPType &output_cpp_type = *bke::socket_type_to_geo_nodes_base_cpp_type(socket_type);
107 SocketValueVariant output_value;
108 void *buffer = output_value.allocate_single(output_cpp_type);
109 if (index >= 0 && index < domain_size) {
110 const IndexMask mask = IndexRange(index, 1);
111 const bke::GeometryFieldContext geometry_context(*component, domain);
112 FieldEvaluator evaluator(geometry_context, &mask);
113 evaluator.add(value_field);
114 evaluator.evaluate();
115 const GVArray &data = evaluator.get_evaluated(0);
116 data.get_to_uninitialized(index, buffer);
117 }
118 else {
119 cpp_type.copy_construct(cpp_type.default_value(), buffer);
120 }
121 params.set_output("Value"_ustr, std::move(output_value));
122 return;
123 }
124
125 std::string error_message;
126
127 if (use_clamp) {
128 bke::SocketValueVariant index_value_variant_copy = index_value_variant;
129 static auto clamp_fn = mf::build::SI3_SO<int, int, int, int>(
130 "Clamp",
131 [](int value, int min, int max) { return std::clamp(value, min, max); },
132 mf::build::exec_presets::SomeSpanOrSingle<0>());
133 const int domain_size = component->attribute_domain_size(domain);
135 bke::SocketValueVariant max_value = bke::SocketValueVariant::from(domain_size - 1);
137 clamp_fn,
138 {&index_value_variant_copy, &min_value, &max_value},
139 {&index_value_variant},
140 params.user_data(),
141 error_message))
142 {
144 params.error_message_add(NodeWarningType::Error, std::move(error_message));
145 return;
146 }
147 }
148
149 bke::SocketValueVariant output_value;
151 std::make_shared<bke::SampleIndexFunction>(
152 std::move(geometry), std::move(value_field), domain),
153 {&index_value_variant},
154 {&output_value},
155 params.user_data(),
156 error_message))
157 {
159 params.error_message_add(NodeWarningType::Error, std::move(error_message));
160 return;
161 }
162
163 params.set_output("Value"_ustr, std::move(output_value));
164}
165
166static void node_register()
167{
168 static bke::bNodeType ntype;
169
170 geo_node_type_base(&ntype, "GeometryNodeSampleIndex"_ustr, GEO_NODE_SAMPLE_INDEX);
171 ntype.ui_name = "Sample Index";
172 ntype.ui_description = "Retrieve values from specific geometry elements";
173 ntype.enum_name_legacy = "SAMPLE_INDEX";
175 ntype.initfunc = node_init;
176 ntype.declare = node_declare;
178 ntype, "NodeGeometrySampleIndex", node_free_standard_storage, node_copy_standard_storage);
183}
185
186} // namespace blender::nodes::node_geo_sample_index_cc
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_SAMPLE_INDEX
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define UI_ITEM_NONE
btScalar max
constexpr IndexRange()=default
int attribute_domain_size(AttrDomain domain) const
static const GeometryComponent * find_source_component(const GeometrySet &geometry, AttrDomain domain)
void * allocate_single(const CPPType &type)
static SocketValueVariant from(T &&value)
int add(GField field, GVArray *varray_ptr)
const GVArray & get_evaluated(const int field_index) const
const CPPType & cpp_type() const
Definition FN_field.hh:518
const bke::bNodeType & node_type() const
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)
Vector< SocketDeclaration * > inputs
#define storage(slot, qualifiers)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
const CPPType * socket_type_to_geo_nodes_base_cpp_type(eNodeSocketDatatype type)
Definition node.cc:5969
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
std::optional< eCustomDataType > socket_type_to_custom_data_type(eNodeSocketDatatype type)
Definition node.cc:5911
static void node_geo_exec(GeoNodeExecParams params)
static void node_declare(NodeDeclarationBuilder &b)
NODE_STORAGE_FUNCS(NodeGeometrySampleIndex)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_init(bNodeTree *, bNode *node)
void search_link_ops_for_declarations(GatherLinkSearchOpParams &params, Span< SocketDeclaration * > declarations)
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)
BMesh const char void * data
void node_copy_standard_storage(bNodeTree *, bNode *dest_node, const bNode *src_node)
Definition node_util.cc:57
void node_free_standard_storage(bNode *node)
Definition node_util.cc:45
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
signed char int8_t
Definition BKE_text.h:76
#define min(a, b)
Definition sort.cc:38
eNodeSocketDatatype type
bke::bNodeType * typeinfo
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
nodes::NodeDeclaration * static_declaration
Definition BKE_node.hh:402
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)