Blender V5.0
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")
16 .implicit_field(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").supports_field().hide_value().description(
20 "Values used to sort the face's corners. Uses indices by default");
21 b.add_input<decl::Int>("Sort Index")
22 .min(0)
23 .supports_field()
24 .description("Which of the sorted corners to output");
25 b.add_output<decl::Int>("Corner Index")
26 .field_source_reference_all()
27 .description("A corner of the face, chosen by the sort index");
28 b.add_output<decl::Int>("Total").field_source().reference_pass({0}).description(
29 "The number of corners in the face");
30}
31
33 const Field<int> face_index_;
34 const Field<int> sort_index_;
35 const Field<float> sort_weight_;
36
37 public:
38 CornersOfFaceInput(Field<int> face_index, Field<int> sort_index, Field<float> sort_weight)
39 : bke::MeshFieldInput(CPPType::get<int>(), "Corner of Face"),
40 face_index_(std::move(face_index)),
41 sort_index_(std::move(sort_index)),
42 sort_weight_(std::move(sort_weight))
43 {
45 }
46
48 const AttrDomain domain,
49 const IndexMask &mask) const final
50 {
51 const OffsetIndices faces = mesh.faces();
52
53 const bke::MeshFieldContext context{mesh, domain};
54 fn::FieldEvaluator evaluator{context, &mask};
55 evaluator.add(face_index_);
56 evaluator.add(sort_index_);
57 evaluator.evaluate();
58 const VArray<int> face_indices = evaluator.get_evaluated<int>(0);
59 const VArray<int> indices_in_sort = evaluator.get_evaluated<int>(1);
60
61 const bke::MeshFieldContext corner_context{mesh, AttrDomain::Corner};
62 fn::FieldEvaluator corner_evaluator{corner_context, mesh.corners_num};
63 corner_evaluator.add(sort_weight_);
64 corner_evaluator.evaluate();
65 const VArray<float> all_sort_weights = corner_evaluator.get_evaluated<float>(0);
66 const bool use_sorting = !all_sort_weights.is_single();
67
68 Array<int> corner_of_face(mask.min_array_size());
69 mask.foreach_segment(GrainSize(1024), [&](const IndexMaskSegment segment) {
70 /* Reuse arrays to avoid allocation. */
71 Array<float> sort_weights;
72 Array<int> sort_indices;
73
74 for (const int selection_i : segment) {
75 const int face_i = face_indices[selection_i];
76 const int index_in_sort = indices_in_sort[selection_i];
77 if (!faces.index_range().contains(face_i)) {
78 corner_of_face[selection_i] = 0;
79 continue;
80 }
81
82 const IndexRange corners = faces[face_i];
83
84 const int index_in_sort_wrapped = mod_i(index_in_sort, corners.size());
85 if (use_sorting) {
86 /* Retrieve the weights for each corner. */
87 sort_weights.reinitialize(corners.size());
88 all_sort_weights.materialize_compressed(IndexMask(corners),
89 sort_weights.as_mutable_span());
90
91 /* Sort a separate array of compressed indices corresponding to the compressed weights.
92 * This allows using `materialize_compressed` to avoid virtual function call overhead
93 * when accessing values in the sort weights. However, it means a separate array of
94 * indices within the compressed array is necessary for sorting. */
95 sort_indices.reinitialize(corners.size());
97 std::stable_sort(sort_indices.begin(), sort_indices.end(), [&](int a, int b) {
98 return sort_weights[a] < sort_weights[b];
99 });
100 corner_of_face[selection_i] = corners[sort_indices[index_in_sort_wrapped]];
101 }
102 else {
103 corner_of_face[selection_i] = corners[index_in_sort_wrapped];
104 }
105 }
106 });
107
108 return VArray<int>::from_container(std::move(corner_of_face));
109 }
110
111 void for_each_field_input_recursive(FunctionRef<void(const FieldInput &)> fn) const override
112 {
113 face_index_.node().for_each_field_input_recursive(fn);
114 sort_index_.node().for_each_field_input_recursive(fn);
115 sort_weight_.node().for_each_field_input_recursive(fn);
116 }
117
119 {
120 return 6927982716657;
121 }
122
123 bool is_equal_to(const fn::FieldNode &other) const final
124 {
125 if (const auto *typed = dynamic_cast<const CornersOfFaceInput *>(&other)) {
126 return typed->face_index_ == face_index_ && typed->sort_index_ == sort_index_ &&
127 typed->sort_weight_ == sort_weight_;
128 }
129 return false;
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 }
144
146 const AttrDomain domain,
147 const IndexMask & /*mask*/) const final
148 {
149 if (domain != AttrDomain::Face) {
150 return {};
151 }
152 const OffsetIndices faces = mesh.faces();
153 return VArray<int>::from_func(mesh.faces_num,
154 [faces](const int64_t i) { return faces[i].size(); });
155 }
156
158 {
159 return 8345908765432698;
160 }
161
162 bool is_equal_to(const fn::FieldNode &other) const final
163 {
164 return dynamic_cast<const CornersOfFaceCountInput *>(&other) != nullptr;
165 }
166
167 std::optional<AttrDomain> preferred_domain(const Mesh & /*mesh*/) const final
168 {
169 return AttrDomain::Face;
170 }
171};
172
174{
175 const Field<int> face_index = params.extract_input<Field<int>>("Face Index");
176 if (params.output_is_required("Total")) {
177 params.set_output("Total",
178 Field<int>(std::make_shared<bke::EvaluateAtIndexInput>(
179 face_index,
180 Field<int>(std::make_shared<CornersOfFaceCountInput>()),
181 AttrDomain::Face)));
182 }
183 if (params.output_is_required("Corner Index")) {
184 params.set_output("Corner Index",
185 Field<int>(std::make_shared<CornersOfFaceInput>(
186 face_index,
187 params.extract_input<Field<int>>("Sort Index"),
188 params.extract_input<Field<float>>("Weights"))));
189 }
190}
191
192static void node_register()
193{
194 static blender::bke::bNodeType ntype;
195 geo_node_type_base(&ntype, "GeometryNodeCornersOfFace", GEO_NODE_MESH_TOPOLOGY_CORNERS_OF_FACE);
196 ntype.ui_name = "Corners of Face";
197 ntype.ui_description = "Retrieve corners that make up a face";
198 ntype.enum_name_legacy = "CORNERS_OF_FACE";
199 ntype.nclass = NODE_CLASS_INPUT;
201 ntype.declare = node_declare;
203}
205
206} // namespace blender::nodes::node_geo_mesh_topology_corners_of_face_cc
#define NODE_CLASS_INPUT
Definition BKE_node.hh:447
#define GEO_NODE_MESH_TOPOLOGY_CORNERS_OF_FACE
#define final(a, b, c)
Definition BLI_hash.h:19
MINLINE int mod_i(int i, int n)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
long long int int64_t
unsigned long long int uint64_t
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:248
const T * end() const
Definition BLI_array.hh:325
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:419
const T * begin() const
Definition BLI_array.hh:321
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)
FieldInput(const CPPType &type, std::string debug_name="")
Definition field.cc:677
int add(GField field, GVArray *varray_ptr)
Definition field.cc:751
const GVArray & get_evaluated(const int field_index) const
Definition FN_field.hh:448
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 for_each_field_input_recursive(FunctionRef< void(const FieldInput &)> fn) const override
GVArray get_varray_for_context(const Mesh &mesh, const AttrDomain domain, const IndexMask &mask) const final
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
static char faces[256]
void fill_index_range(MutableSpan< T > span, const T start=0)
void node_register_type(bNodeType &ntype)
Definition node.cc:2416
void geo_node_type_base(blender::bke::bNodeType *ntype, std::string idname, const std::optional< int16_t > legacy_type)
#define min(a, b)
Definition sort.cc:36
Defines a node type.
Definition BKE_node.hh:238
std::string ui_description
Definition BKE_node.hh:244
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:354
const char * enum_name_legacy
Definition BKE_node.hh:247
NodeDeclareFunction declare
Definition BKE_node.hh:362
i
Definition text_draw.cc:230