Blender V5.3
node_geo_field_min_and_max.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
6
7#include "BLI_array.hh"
10
11#include "NOD_rna_define.hh"
13
14#include "RNA_enum_types.hh"
15
16#include "node_geometry_util.hh"
17
19#include "UI_resources.hh"
20
22
24{
25 const bNode *node = b.node_or_null();
26
27 if (node != nullptr) {
28 const eCustomDataType data_type = eCustomDataType(node->custom1);
29 b.add_input(data_type, "Value"_ustr)
30 .structure_type(StructureType::Field)
31 .description("The values the minimum and maximum will be calculated from");
32 }
33
34 b.add_input<decl::Int>("Group ID"_ustr, "Group Index"_ustr)
35 .structure_type(StructureType::Field)
36 .hide_value()
37 .description("An index used to group values together for multiple separate operations");
38
39 if (node != nullptr) {
40 const eCustomDataType data_type = eCustomDataType(node->custom1);
41 b.add_output(data_type, "Min"_ustr)
42 .structure_type(StructureType::Field)
43 .propagate_references()
44 .description("The lowest value in each group");
45 b.add_output(data_type, "Max"_ustr)
46 .structure_type(StructureType::Field)
47 .propagate_references()
48 .description("The highest value in each group");
49 }
50}
51
52static void node_layout(ui::Layout &layout, bContext * /*C*/, PointerRNA *ptr)
53{
54 layout.prop(ptr, "data_type", UI_ITEM_NONE, "", ICON_NONE);
55 layout.prop(ptr, "domain", UI_ITEM_NONE, "", ICON_NONE);
56}
57
58static void node_init(bNodeTree * /*tree*/, bNode *node)
59{
60 node->custom1 = CD_PROP_FLOAT;
61 node->custom2 = int16_t(AttrDomain::Point);
62}
63
64enum class Operation { Min = 0, Max = 1 };
65
66static std::optional<eCustomDataType> node_type_from_other_socket(const bNodeSocket &socket)
67{
68 switch (socket.type) {
69 case SOCK_FLOAT:
70 return CD_PROP_FLOAT;
71 case SOCK_BOOLEAN:
72 case SOCK_INT:
73 return CD_PROP_INT32;
74 case SOCK_VECTOR:
75 case SOCK_RGBA:
76 case SOCK_ROTATION:
77 return CD_PROP_FLOAT3;
78 default:
79 return std::nullopt;
80 }
81}
82
84{
85 const NodeDeclaration &declaration = *params.node_type().static_declaration;
86 search_link_ops_for_declarations(params, declaration.inputs);
87
88 const std::optional<eCustomDataType> type = node_type_from_other_socket(params.other_socket());
89 if (!type) {
90 return;
91 }
92 if (params.in_out() == SOCK_OUT) {
93 params.add_item(
94 IFACE_("Min"),
95 [type](LinkSearchOpParams &params) {
96 bNode &node = params.add_node("GeometryNodeFieldMinAndMax"_ustr);
97 node.custom1 = *type;
98 params.update_and_connect_available_socket(node, "Min"_ustr);
99 },
100 0);
101 params.add_item(
102 IFACE_("Max"),
103 [type](LinkSearchOpParams &params) {
104 bNode &node = params.add_node("GeometryNodeFieldMinAndMax"_ustr);
105 node.custom1 = *type;
106 params.update_and_connect_available_socket(node, "Max"_ustr);
107 },
108 -1);
109 }
110 else {
111 params.add_item(
112 IFACE_("Value"),
113 [type](LinkSearchOpParams &params) {
114 bNode &node = params.add_node("GeometryNodeFieldMinAndMax"_ustr);
115 node.custom1 = *type;
116 params.update_and_connect_available_socket(node, "Value"_ustr);
117 },
118 0);
119 }
120}
121
122template<typename T> struct MinMaxInfo {
123 static inline const T min_initial_value = []() {
124 if constexpr (std::is_same_v<T, float3>) {
125 return float3(std::numeric_limits<float>::max());
126 }
127 else {
128 return std::numeric_limits<T>::max();
129 }
130 }();
131
132 static inline const T max_initial_value = []() {
133 if constexpr (std::is_same_v<T, float3>) {
134 return float3(std::numeric_limits<float>::lowest());
135 }
136 else {
137 return std::numeric_limits<T>::lowest();
138 }
139 }();
140};
141
143 private:
144 GField input_;
145 Field<int> group_index_;
146 AttrDomain source_domain_;
147 Operation operation_;
148
149 public:
150 FieldMinMaxInput(const AttrDomain source_domain,
151 GField input,
152 Field<int> group_index,
153 Operation operation)
154 : bke::GeometryFieldInput(input.cpp_type(), "Calculation"),
155 input_(std::move(input)),
156 group_index_(std::move(group_index)),
157 source_domain_(source_domain),
158 operation_(operation)
159 {
160 }
161
163 const IndexMask & /*mask*/) const final
164 {
165 const AttributeAccessor attributes = *context.attributes();
166 const int64_t domain_size = attributes.domain_size(source_domain_);
167 if (domain_size == 0) {
168 return {};
169 }
170
171 const bke::GeometryFieldContext source_context{context, source_domain_};
172 fn::FieldEvaluator evaluator{source_context, domain_size};
173 evaluator.add(input_);
174 evaluator.add(group_index_);
175 evaluator.evaluate();
176 const GVArray g_values = evaluator.get_evaluated(0);
177 const VArray<int> group_indices = evaluator.get_evaluated<int>(1);
178
179 GVArray g_outputs;
180
181 g_values.type().to_static_type<int, float, float3>([&]<typename T>() {
182 const VArray<T> values = g_values.typed<T>();
183
184 if (operation_ == Operation::Min) {
185 if (group_indices.is_single()) {
187 for (const int i : values.index_range()) {
188 result = math::min(result, values[i]);
189 }
190 g_outputs = VArray<T>::from_single(result, domain_size);
191 }
192 else {
193 Map<int, T> results;
194 for (const int i : values.index_range()) {
195 T &value = results.lookup_or_add(group_indices[i], MinMaxInfo<T>::min_initial_value);
196 value = math::min(value, values[i]);
197 }
198 Array<T> outputs(domain_size);
199 for (const int i : values.index_range()) {
200 outputs[i] = results.lookup(group_indices[i]);
201 }
202 g_outputs = VArray<T>::from_container(std::move(outputs));
203 }
204 }
205 else {
206 if (group_indices.is_single()) {
208 for (const int i : values.index_range()) {
209 result = math::max(result, values[i]);
210 }
211 g_outputs = VArray<T>::from_single(result, domain_size);
212 }
213 else {
214 Map<int, T> results;
215 for (const int i : values.index_range()) {
216 T &value = results.lookup_or_add(group_indices[i], MinMaxInfo<T>::max_initial_value);
217 value = math::max(value, values[i]);
218 }
219 Array<T> outputs(domain_size);
220 for (const int i : values.index_range()) {
221 outputs[i] = results.lookup(group_indices[i]);
222 }
223 g_outputs = VArray<T>::from_container(std::move(outputs));
224 }
225 }
226 });
227
228 return attributes.adapt_domain(std::move(g_outputs), source_domain_, context.domain());
229 }
230
231 void foreach_recursive_field(FunctionRef<void(const GField &)> fn) const final
232 {
233 fn(input_);
234 fn(group_index_);
235 }
236
237 void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &deep_hash_cache) const override
238 {
239 static constexpr int8_t id = 0;
240 hash.add(&id);
241 hash.add(deep_hash_cache.ensure(input_));
242 hash.add(deep_hash_cache.ensure(group_index_));
243 hash.add(source_domain_);
244 hash.add(operation_);
245 }
246
247 std::optional<AttrDomain> preferred_domain(
248 const GeometryComponent & /*component*/) const override
249 {
250 return source_domain_;
251 }
252};
253
255{
256 const AttrDomain source_domain = AttrDomain(params.node().custom2);
257
258 const Field<int> group_index_field = params.extract_input<Field<int>>("Group Index"_ustr);
259 const GField input_field = params.extract_input<GField>("Value"_ustr);
260 if (params.output_is_required("Min"_ustr)) {
261 params.set_output<GField>("Min"_ustr,
263 source_domain, input_field, group_index_field, Operation::Min));
264 }
265 if (params.output_is_required("Max"_ustr)) {
266 params.set_output<GField>("Max"_ustr,
268 source_domain, input_field, group_index_field, Operation::Max));
269 }
270}
271
272static void node_rna(StructRNA *srna)
273{
274 static EnumPropertyItem items[] = {
275 {CD_PROP_FLOAT, "FLOAT", ICON_NODE_SOCKET_FLOAT, "Float", "Floating-point value"},
276 {CD_PROP_INT32, "INT", ICON_NODE_SOCKET_INT, "Integer", "32-bit integer"},
278 "FLOAT_VECTOR",
279 ICON_NODE_SOCKET_VECTOR,
280 "Vector",
281 "3D vector with floating-point values"},
282 {0, nullptr, 0, nullptr, nullptr},
283 };
284
286 "data_type",
287 "Data Type",
288 "Type of data the outputs are calculated from",
289 items,
292
294 "domain",
295 "Domain",
296 "",
299 int(AttrDomain::Point),
300 nullptr,
301 true);
302}
303
304static void node_register()
305{
306 static bke::bNodeType ntype;
307
308 geo_node_type_base(&ntype, "GeometryNodeFieldMinAndMax"_ustr);
309 ntype.ui_name = "Field Min & Max";
310 ntype.ui_description = "Calculate the minimum and maximum of a given field";
313 ntype.initfunc = node_init;
315 ntype.declare = node_declare;
318 node_rna(ntype.rna_ext.srna);
319}
321
322} // namespace blender::nodes::node_geo_field_min_and_max_cc
#define NODE_CLASS_CONVERTER
Definition BKE_node.hh:484
#define final(a, b, c)
Definition BLI_hash_c.hh:21
#define IFACE_(msgid)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define NOD_inline_enum_accessors(member)
#define UI_ITEM_NONE
void to_static_type(Fn &&fn) const
const Value & lookup(const Key &key) const
Definition BLI_map.hh:545
Value & lookup_or_add(const Key &key, const Value &value)
Definition BLI_map.hh:588
IndexRange index_range() const
static VArray from_single(T value, const int64_t size)
static VArray from_container(ContainerT container)
GVArray adapt_domain(const GVArray &varray, const AttrDomain from_domain, const AttrDomain to_domain) const
int domain_size(const AttrDomain domain) const
int add(GField field, GVArray *varray_ptr)
const GVArray & get_evaluated(const int field_index) const
uint64_t hash() const
Definition field.cc:185
const CPPType & cpp_type() const
Definition FN_field.hh:559
static GField from_input(Args &&...args)
Definition FN_field.hh:482
const bke::bNodeType & node_type() const
void add_item(std::string socket_name, SocketLinkOperation::LinkSocketFn fn, int weight=0)
T extract_input(const UString identifier)
bool output_is_required(const UString identifier) const
void set_output(const UString identifier, T &&value)
void update_and_connect_available_socket(bNode &new_node, UString socket_name)
Vector< SocketDeclaration * > inputs
GVArray get_varray_for_context(const bke::GeometryFieldContext &context, const IndexMask &) const final
void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &deep_hash_cache) const override
void foreach_recursive_field(FunctionRef< void(const GField &)> fn) const final
FieldMinMaxInput(const AttrDomain source_domain, GField input, Field< int > group_index, Operation operation)
std::optional< AttrDomain > preferred_domain(const GeometryComponent &) const override
#define T
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
T min(const T &a, const T &b)
T max(const T &a, const T &b)
static std::optional< eCustomDataType > node_type_from_other_socket(const bNodeSocket &socket)
static void node_gather_link_searches(GatherLinkSearchOpParams &params)
static void node_declare(NodeDeclarationBuilder &b)
static void node_layout(ui::Layout &layout, bContext *, PointerRNA *ptr)
static void node_geo_exec(GeoNodeExecParams params)
void search_link_ops_for_declarations(GatherLinkSearchOpParams &params, Span< SocketDeclaration * > declarations)
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)
__int64 int64_t
Definition BKE_text.h:90
VecBase< float, 3 > float3
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
const EnumPropertyItem rna_enum_attribute_domain_items[]
signed char int8_t
Definition BKE_text.h:76
signed short int16_t
Definition BKE_text.h:77
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
eNodeSocketDatatype type
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
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
UniqueHash ensure(const GFieldRef &field)
Definition field.cc:113
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)