Blender V5.3
node_geo_mesh_topology_corners_of_face.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_array_utils.hh"
6
7#include "DNA_mesh_types.h"
8
10
12
14{
15 b.add_input<decl::Int>("Face Index"_ustr)
16 .default_input_type(NODE_DEFAULT_INPUT_INDEX_FIELD)
17 .description("The face to retrieve data from. Defaults to the face from the context")
18 .structure_type(StructureType::Field);
19 b.add_input<decl::Float>("Weights"_ustr)
20 .structure_type(StructureType::Field)
21 .hide_value()
22 .description("Values used to sort the face's corners. Uses indices by default");
23 b.add_input<decl::Int>("Sort Index"_ustr)
24 .structure_type(StructureType::Field)
25 .description("Which of the sorted corners to output. Negative indexing is supported");
26 b.add_output<decl::Int>("Corner Index"_ustr)
27 .structure_type(StructureType::Field)
28 .propagate_references()
29 .description("A corner of the face, chosen by the sort index");
30 b.add_output<decl::Int>("Total"_ustr)
31 .structure_type(StructureType::Field)
32 .propagate_references({0})
33 .description("The number of corners in the face");
34}
35
37 const Field<int> face_index_;
38 const Field<int> sort_index_;
39 const Field<float> sort_weight_;
40
41 public:
42 CornersOfFaceInput(Field<int> face_index, Field<int> sort_index, Field<float> sort_weight)
43 : bke::MeshFieldInput(CPPType::get<int>(), "Corner of Face"),
44 face_index_(std::move(face_index)),
45 sort_index_(std::move(sort_index)),
46 sort_weight_(std::move(sort_weight))
47 {
48 }
49
51 const AttrDomain domain,
52 const IndexMask &mask) const final
53 {
54 const OffsetIndices faces = mesh.faces();
55
56 const bke::MeshFieldContext context{mesh, domain};
57 fn::FieldEvaluator evaluator{context, &mask};
58 evaluator.add(face_index_);
59 evaluator.add(sort_index_);
60 evaluator.evaluate();
61 const VArray<int> face_indices = evaluator.get_evaluated<int>(0);
62 const VArray<int> indices_in_sort = evaluator.get_evaluated<int>(1);
63
64 const bke::MeshFieldContext corner_context{mesh, AttrDomain::Corner};
65 fn::FieldEvaluator corner_evaluator{corner_context, mesh.corners_num};
66 corner_evaluator.add(sort_weight_);
67 corner_evaluator.evaluate();
68 const VArray<float> all_sort_weights = corner_evaluator.get_evaluated<float>(0);
69 const bool use_sorting = !all_sort_weights.is_single();
70
71 Array<int> corner_of_face(mask.min_array_size());
72 mask.foreach_segment(
73 [&](const IndexMaskSegment segment) {
74 /* Reuse arrays to avoid allocation. */
75 Array<float> sort_weights;
76 Array<int> sort_indices;
77
78 for (const int selection_i : segment) {
79 const int face_i = face_indices[selection_i];
80 const int index_in_sort = indices_in_sort[selection_i];
81 if (!faces.index_range().contains(face_i)) {
82 corner_of_face[selection_i] = 0;
83 continue;
84 }
85
86 const IndexRange corners = faces[face_i];
87
88 const int index_in_sort_wrapped = mod_i(index_in_sort, corners.size());
89 if (use_sorting) {
90 /* Retrieve the weights for each corner. */
91 sort_weights.reinitialize(corners.size());
92 all_sort_weights.materialize_compressed(IndexMask(corners),
93 sort_weights.as_mutable_span());
94
95 /* Sort a separate array of compressed indices corresponding to the compressed
96 * weights. This allows using `materialize_compressed` to avoid virtual function call
97 * overhead when accessing values in the sort weights. However, it means a separate
98 * array of indices within the compressed array is necessary for sorting. */
99 sort_indices.reinitialize(corners.size());
101 std::stable_sort(sort_indices.begin(), sort_indices.end(), [&](int a, int b) {
102 return sort_weights[a] < sort_weights[b];
103 });
104 corner_of_face[selection_i] = corners[sort_indices[index_in_sort_wrapped]];
105 }
106 else {
107 corner_of_face[selection_i] = corners[index_in_sort_wrapped];
108 }
109 }
110 },
112
113 return VArray<int>::from_container(std::move(corner_of_face));
114 }
115
116 void foreach_recursive_field(FunctionRef<void(const GField &)> fn) const override
117 {
118 fn(face_index_);
119 fn(sort_index_);
120 fn(sort_weight_);
121 }
122
123 void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &deep_hash_cache) const final
124 {
125 static constexpr int8_t id = 0;
126 hash.add(&id);
127 hash.add(deep_hash_cache.ensure(face_index_));
128 hash.add(deep_hash_cache.ensure(sort_index_));
129 hash.add(deep_hash_cache.ensure(sort_weight_));
130 }
131
132 std::optional<AttrDomain> preferred_domain(const Mesh & /*mesh*/) const final
133 {
134 return AttrDomain::Face;
135 }
136};
137
139 public:
140 CornersOfFaceCountInput() : bke::MeshFieldInput(CPPType::get<int>(), "Face Corner Count") {}
141
143 const AttrDomain domain,
144 const IndexMask & /*mask*/) const final
145 {
146 if (domain != AttrDomain::Face) {
147 return {};
148 }
149 const OffsetIndices faces = mesh.faces();
150 return VArray<int>::from_func(mesh.faces_num,
151 [faces](const int64_t i) { return faces[i].size(); });
152 }
153
154 void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep & /*deep_hash_cache*/) const override
155 {
156 static constexpr int8_t id = 0;
157 hash.add(&id);
158 }
159
160 std::optional<AttrDomain> preferred_domain(const Mesh & /*mesh*/) const final
161 {
162 return AttrDomain::Face;
163 }
164};
165
167{
168 const Field<int> face_index = params.extract_input<Field<int>>("Face Index"_ustr);
169 if (params.output_is_required("Total"_ustr)) {
170 params.set_output(
171 "Total"_ustr,
173 face_index, Field<int>::from_input<CornersOfFaceCountInput>(), AttrDomain::Face));
174 }
175 if (params.output_is_required("Corner Index"_ustr)) {
176 params.set_output("Corner Index"_ustr,
178 face_index,
179 params.extract_input<Field<int>>("Sort Index"_ustr),
180 params.extract_input<Field<float>>("Weights"_ustr)));
181 }
182}
183
184static void node_register()
185{
186 static bke::bNodeType ntype;
188 &ntype, "GeometryNodeCornersOfFace"_ustr, GEO_NODE_MESH_TOPOLOGY_CORNERS_OF_FACE);
189 ntype.ui_name = "Corners of Face";
190 ntype.ui_description = "Retrieve corners that make up a face";
191 ntype.enum_name_legacy = "CORNERS_OF_FACE";
192 ntype.nclass = NODE_CLASS_INPUT;
194 ntype.declare = node_declare;
196}
198
199} // namespace blender::nodes::node_geo_mesh_topology_corners_of_face_cc
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define GEO_NODE_MESH_TOPOLOGY_CORNERS_OF_FACE
#define final(a, b, c)
Definition BLI_hash_c.hh:21
#define NOD_REGISTER_NODE(REGISTER_FUNC)
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
const T * end() const
Definition BLI_array.hh:336
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:430
const T * begin() const
Definition BLI_array.hh:332
constexpr bool contains(int64_t value) const
void materialize_compressed(const IndexMask &mask, MutableSpan< T > r_span) const
static VArray from_func(const int64_t size, GetFunc get_func)
static VArray from_container(ContainerT container)
int add(GField field, GVArray *varray_ptr)
const GVArray & get_evaluated(const int field_index) const
uint64_t hash() const
Definition field.cc:185
static Field from_input(Args &&...args)
T extract_input(const UString identifier)
bool output_is_required(const UString identifier) const
void set_output(const UString identifier, T &&value)
GVArray get_varray_for_context(const Mesh &mesh, const AttrDomain domain, const IndexMask &) const final
CornersOfFaceInput(Field< int > face_index, Field< int > sort_index, Field< float > sort_weight)
void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &deep_hash_cache) const final
GVArray get_varray_for_context(const Mesh &mesh, const AttrDomain domain, const IndexMask &mask) const final
void fill_index_range(MutableSpan< T > span, const T start=0)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
constexpr ParallelGrainSize grain_size(int grain_size)
MINLINE int mod_i(int i, int n)
__int64 int64_t
Definition BKE_text.h:90
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
Defines a node type.
Definition BKE_node.hh:265
std::string ui_description
Definition BKE_node.hh:271
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:385
const char * enum_name_legacy
Definition BKE_node.hh:274
NodeDeclareFunction declare
Definition BKE_node.hh:393