Blender V5.3
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"_ustr)
16 .default_input_type(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"_ustr)
20 .structure_type(StructureType::Field)
21 .hide_value()
22 .description("Values used to sort corners attached to the vertex. 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 connected to 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 faces or corners connected to each vertex");
34}
35
37 const Field<int> vert_index_;
38 const Field<int> sort_index_;
39 const Field<float> sort_weight_;
40
41 public:
42 CornersOfVertInput(Field<int> vert_index, Field<int> sort_index, Field<float> sort_weight)
43 : bke::MeshFieldInput(CPPType::get<int>(), "Corner of Vertex"),
44 vert_index_(std::move(vert_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 IndexRange vert_range(mesh.verts_num);
55 const GroupedSpan<int> vert_to_corner_map = mesh.vert_to_corner_map();
56
57 const bke::MeshFieldContext context{mesh, domain};
58 fn::FieldEvaluator evaluator{context, &mask};
59 evaluator.add(vert_index_);
60 evaluator.add(sort_index_);
61 evaluator.evaluate();
62 const VArray<int> vert_indices = evaluator.get_evaluated<int>(0);
63 const VArray<int> indices_in_sort = evaluator.get_evaluated<int>(1);
64
65 const bke::MeshFieldContext corner_context{mesh, AttrDomain::Corner};
66 fn::FieldEvaluator corner_evaluator{corner_context, mesh.corners_num};
67 corner_evaluator.add(sort_weight_);
68 corner_evaluator.evaluate();
69 const VArray<float> all_sort_weights = corner_evaluator.get_evaluated<float>(0);
70 const bool use_sorting = !all_sort_weights.is_single();
71
72 Array<int> corner_of_vertex(mask.min_array_size());
73 mask.foreach_segment(
74 [&](const IndexMaskSegment segment) {
75 /* Reuse arrays to avoid allocation. */
76 Array<float> sort_weights;
77 Array<int> sort_indices;
78
79 for (const int selection_i : segment) {
80 const int vert_i = vert_indices[selection_i];
81 const int index_in_sort = indices_in_sort[selection_i];
82 if (!vert_range.contains(vert_i)) {
83 corner_of_vertex[selection_i] = 0;
84 continue;
85 }
86
87 const Span<int> corners = vert_to_corner_map[vert_i];
88 if (corners.is_empty()) {
89 corner_of_vertex[selection_i] = 0;
90 continue;
91 }
92
93 const int index_in_sort_wrapped = mod_i(index_in_sort, corners.size());
94 if (use_sorting) {
95 /* Retrieve a compressed array of weights for each edge. */
96 sort_weights.reinitialize(corners.size());
97 IndexMaskMemory memory;
98 all_sort_weights.materialize_compressed(
99 IndexMask::from_indices<int>(corners, memory), sort_weights.as_mutable_span());
100
101 /* Sort a separate array of compressed indices corresponding to the compressed
102 * weights. This allows using `materialize_compressed` to avoid virtual function call
103 * overhead when accessing values in the sort weights. However, it means a separate
104 * array of indices within the compressed array is necessary for sorting. */
105 sort_indices.reinitialize(corners.size());
107 std::stable_sort(sort_indices.begin(), sort_indices.end(), [&](int a, int b) {
108 return sort_weights[a] < sort_weights[b];
109 });
110 corner_of_vertex[selection_i] = corners[sort_indices[index_in_sort_wrapped]];
111 }
112 else {
113 corner_of_vertex[selection_i] = corners[index_in_sort_wrapped];
114 }
115 }
116 },
118
119 return VArray<int>::from_container(std::move(corner_of_vertex));
120 }
121
122 void foreach_recursive_field(FunctionRef<void(const GField &)> fn) const override
123 {
124 fn(vert_index_);
125 fn(sort_index_);
126 fn(sort_weight_);
127 }
128
129 void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &deep_hash_cache) const override
130 {
131 static constexpr int8_t id = 0;
132 hash.add(&id);
133 hash.add(deep_hash_cache.ensure(vert_index_));
134 hash.add(deep_hash_cache.ensure(sort_index_));
135 hash.add(deep_hash_cache.ensure(sort_weight_));
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 const AttrDomain domain,
150 const IndexMask & /*mask*/) const final
151 {
152 if (domain != AttrDomain::Point) {
153 return {};
154 }
155 Array<int> counts(mesh.verts_num, 0);
156 array_utils::count_indices(mesh.corner_verts(), counts);
157 return VArray<int>::from_container(std::move(counts));
158 }
159
160 void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep & /*deep_hash_cache*/) const override
161 {
162 static constexpr int8_t id = 0;
163 hash.add(&id);
164 }
165
166 std::optional<AttrDomain> preferred_domain(const Mesh & /*mesh*/) const final
167 {
168 return AttrDomain::Point;
169 }
170};
171
173{
174 const Field<int> vert_index = params.extract_input<Field<int>>("Vertex Index"_ustr);
175 if (params.output_is_required("Total"_ustr)) {
176 params.set_output(
177 "Total"_ustr,
179 vert_index, Field<int>::from_input<CornersOfVertCountInput>(), AttrDomain::Point));
180 }
181 if (params.output_is_required("Corner Index"_ustr)) {
182 params.set_output("Corner Index"_ustr,
184 vert_index,
185 params.extract_input<Field<int>>("Sort Index"_ustr),
186 params.extract_input<Field<float>>("Weights"_ustr)));
187 }
188}
189
190static void node_register()
191{
192 static bke::bNodeType ntype;
194 &ntype, "GeometryNodeCornersOfVertex"_ustr, GEO_NODE_MESH_TOPOLOGY_CORNERS_OF_VERTEX);
195 ntype.ui_name = "Corners of Vertex";
196 ntype.ui_description = "Retrieve face corners connected to vertices";
197 ntype.enum_name_legacy = "CORNERS_OF_VERTEX";
198 ntype.nclass = NODE_CLASS_INPUT;
200 ntype.declare = node_declare;
202}
204
205} // namespace blender::nodes::node_geo_mesh_topology_corners_of_vertex_cc
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define GEO_NODE_MESH_TOPOLOGY_CORNERS_OF_VERTEX
#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
static IndexMask from_indices(Span< T > indices, LinearAllocator<> &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)
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
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)
void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &deep_hash_cache) const override
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:2920
constexpr ParallelGrainSize grain_size(int grain_size)
MINLINE int mod_i(int i, int n)
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
UniqueHash ensure(const GFieldRef &field)
Definition field.cc:113