Blender V5.0
node_geo_mesh_topology_corners_of_vertex.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 "DNA_mesh_types.h"
6
7#include "BLI_array_utils.hh"
8
10
12
14{
15 b.add_input<decl::Int>("Vertex Index")
16 .implicit_field(NODE_DEFAULT_INPUT_INDEX_FIELD)
17 .description("The vertex to retrieve data from. Defaults to the vertex 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 corners attached to the vertex. 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 connected to the face, chosen by the sort index");
28 b.add_output<decl::Int>("Total").field_source().reference_pass({0}).description(
29 "The number of faces or corners connected to each vertex");
30}
31
33 const Field<int> vert_index_;
34 const Field<int> sort_index_;
35 const Field<float> sort_weight_;
36
37 public:
38 CornersOfVertInput(Field<int> vert_index, Field<int> sort_index, Field<float> sort_weight)
39 : bke::MeshFieldInput(CPPType::get<int>(), "Corner of Vertex"),
40 vert_index_(std::move(vert_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 IndexRange vert_range(mesh.verts_num);
52 const GroupedSpan<int> vert_to_corner_map = mesh.vert_to_corner_map();
53
54 const bke::MeshFieldContext context{mesh, domain};
55 fn::FieldEvaluator evaluator{context, &mask};
56 evaluator.add(vert_index_);
57 evaluator.add(sort_index_);
58 evaluator.evaluate();
59 const VArray<int> vert_indices = evaluator.get_evaluated<int>(0);
60 const VArray<int> indices_in_sort = evaluator.get_evaluated<int>(1);
61
62 const bke::MeshFieldContext corner_context{mesh, AttrDomain::Corner};
63 fn::FieldEvaluator corner_evaluator{corner_context, mesh.corners_num};
64 corner_evaluator.add(sort_weight_);
65 corner_evaluator.evaluate();
66 const VArray<float> all_sort_weights = corner_evaluator.get_evaluated<float>(0);
67 const bool use_sorting = !all_sort_weights.is_single();
68
69 Array<int> corner_of_vertex(mask.min_array_size());
70 mask.foreach_segment(GrainSize(1024), [&](const IndexMaskSegment segment) {
71 /* Reuse arrays to avoid allocation. */
72 Array<float> sort_weights;
73 Array<int> sort_indices;
74
75 for (const int selection_i : segment) {
76 const int vert_i = vert_indices[selection_i];
77 const int index_in_sort = indices_in_sort[selection_i];
78 if (!vert_range.contains(vert_i)) {
79 corner_of_vertex[selection_i] = 0;
80 continue;
81 }
82
83 const Span<int> corners = vert_to_corner_map[vert_i];
84 if (corners.is_empty()) {
85 corner_of_vertex[selection_i] = 0;
86 continue;
87 }
88
89 const int index_in_sort_wrapped = mod_i(index_in_sort, corners.size());
90 if (use_sorting) {
91 /* Retrieve a compressed array of weights for each edge. */
92 sort_weights.reinitialize(corners.size());
93 IndexMaskMemory memory;
94 all_sort_weights.materialize_compressed(IndexMask::from_indices<int>(corners, memory),
95 sort_weights.as_mutable_span());
96
97 /* Sort a separate array of compressed indices corresponding to the compressed weights.
98 * This allows using `materialize_compressed` to avoid virtual function call overhead
99 * when accessing values in the sort weights. However, it means a separate array of
100 * indices within the compressed array is necessary for sorting. */
101 sort_indices.reinitialize(corners.size());
103 std::stable_sort(sort_indices.begin(), sort_indices.end(), [&](int a, int b) {
104 return sort_weights[a] < sort_weights[b];
105 });
106 corner_of_vertex[selection_i] = corners[sort_indices[index_in_sort_wrapped]];
107 }
108 else {
109 corner_of_vertex[selection_i] = corners[index_in_sort_wrapped];
110 }
111 }
112 });
113
114 return VArray<int>::from_container(std::move(corner_of_vertex));
115 }
116
117 void for_each_field_input_recursive(FunctionRef<void(const FieldInput &)> fn) const override
118 {
119 vert_index_.node().for_each_field_input_recursive(fn);
120 sort_index_.node().for_each_field_input_recursive(fn);
121 sort_weight_.node().for_each_field_input_recursive(fn);
122 }
123
125 {
126 return 3541871368173645;
127 }
128
129 bool is_equal_to(const fn::FieldNode &other) const final
130 {
131 if (const auto *typed = dynamic_cast<const CornersOfVertInput *>(&other)) {
132 return typed->vert_index_ == vert_index_ && typed->sort_index_ == sort_index_ &&
133 typed->sort_weight_ == sort_weight_;
134 }
135 return false;
136 }
137
138 std::optional<AttrDomain> preferred_domain(const Mesh & /*mesh*/) const final
139 {
140 return AttrDomain::Point;
141 }
142};
143
145 public:
146 CornersOfVertCountInput() : bke::MeshFieldInput(CPPType::get<int>(), "Vertex Corner Count")
147 {
149 }
150
152 const AttrDomain domain,
153 const IndexMask & /*mask*/) const final
154 {
155 if (domain != AttrDomain::Point) {
156 return {};
157 }
158 Array<int> counts(mesh.verts_num, 0);
159 array_utils::count_indices(mesh.corner_verts(), counts);
160 return VArray<int>::from_container(std::move(counts));
161 }
162
164 {
165 return 253098745374645;
166 }
167
168 bool is_equal_to(const fn::FieldNode &other) const final
169 {
170 return dynamic_cast<const CornersOfVertCountInput *>(&other) != nullptr;
171 }
172
173 std::optional<AttrDomain> preferred_domain(const Mesh & /*mesh*/) const final
174 {
175 return AttrDomain::Point;
176 }
177};
178
180{
181 const Field<int> vert_index = params.extract_input<Field<int>>("Vertex Index");
182 if (params.output_is_required("Total")) {
183 params.set_output("Total",
184 Field<int>(std::make_shared<bke::EvaluateAtIndexInput>(
185 vert_index,
186 Field<int>(std::make_shared<CornersOfVertCountInput>()),
187 AttrDomain::Point)));
188 }
189 if (params.output_is_required("Corner Index")) {
190 params.set_output("Corner Index",
191 Field<int>(std::make_shared<CornersOfVertInput>(
192 vert_index,
193 params.extract_input<Field<int>>("Sort Index"),
194 params.extract_input<Field<float>>("Weights"))));
195 }
196}
197
198static void node_register()
199{
200 static blender::bke::bNodeType ntype;
202 &ntype, "GeometryNodeCornersOfVertex", GEO_NODE_MESH_TOPOLOGY_CORNERS_OF_VERTEX);
203 ntype.ui_name = "Corners of Vertex";
204 ntype.ui_description = "Retrieve face corners connected to vertices";
205 ntype.enum_name_legacy = "CORNERS_OF_VERTEX";
206 ntype.nclass = NODE_CLASS_INPUT;
208 ntype.declare = node_declare;
210}
212
213} // namespace blender::nodes::node_geo_mesh_topology_corners_of_vertex_cc
#define NODE_CLASS_INPUT
Definition BKE_node.hh:447
#define GEO_NODE_MESH_TOPOLOGY_CORNERS_OF_VERTEX
#define final(a, b, c)
Definition BLI_hash.h:19
MINLINE int mod_i(int i, int n)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
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
static IndexMask from_indices(Span< T > indices, IndexMaskMemory &memory)
constexpr bool contains(int64_t value) const
void materialize_compressed(const IndexMask &mask, MutableSpan< T > r_span) const
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
GVArray get_varray_for_context(const Mesh &mesh, const AttrDomain domain, const IndexMask &mask) const final
CornersOfVertInput(Field< int > vert_index, Field< int > sort_index, Field< float > sort_weight)
uiWidgetBaseParameters params[MAX_WIDGET_BASE_BATCH]
ccl_device_inline float2 mask(const MaskType mask, const float2 a)
void count_indices(Span< int > indices, MutableSpan< int > counts)
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