Blender V5.3
node_geo_mesh_topology_edges_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 "BKE_mesh_mapping.hh"
8
9#include "BLI_array_utils.hh"
10
11#include "node_geometry_util.hh"
12
14
16{
17 b.add_input<decl::Int>("Vertex Index"_ustr)
18 .default_input_type(NODE_DEFAULT_INPUT_INDEX_FIELD)
19 .description("The vertex to retrieve data from. Defaults to the vertex from the context")
20 .structure_type(StructureType::Field);
21 b.add_input<decl::Float>("Weights"_ustr)
22 .structure_type(StructureType::Field)
23 .hide_value()
24 .description(
25 "Values used to sort the edges connected to the vertex. Uses indices by default");
26 b.add_input<decl::Int>("Sort Index"_ustr)
27 .structure_type(StructureType::Field)
28 .description("Which of the sorted edges to output. Negative indexing is supported");
29 b.add_output<decl::Int>("Edge Index"_ustr)
30 .structure_type(StructureType::Field)
31 .propagate_references()
32 .description("An edge connected to the face, chosen by the sort index");
33 b.add_output<decl::Int>("Total"_ustr)
34 .structure_type(StructureType::Field)
35 .propagate_references({0})
36 .description("The number of edges connected to each vertex");
37}
38
40 const Field<int> vert_index_;
41 const Field<int> sort_index_;
42 const Field<float> sort_weight_;
43
44 public:
45 EdgesOfVertInput(Field<int> vert_index, Field<int> sort_index, Field<float> sort_weight)
46 : bke::MeshFieldInput(CPPType::get<int>(), "Edge of Vertex"),
47 vert_index_(std::move(vert_index)),
48 sort_index_(std::move(sort_index)),
49 sort_weight_(std::move(sort_weight))
50 {
51 }
52
54 const AttrDomain domain,
55 const IndexMask &mask) const final
56 {
57 const IndexRange vert_range(mesh.verts_num);
58 const Span<int2> edges = mesh.edges();
59 Array<int> map_offsets;
60 Array<int> map_indices;
62 edges, mesh.verts_num, map_offsets, map_indices);
63
64 const bke::MeshFieldContext context{mesh, domain};
65 fn::FieldEvaluator evaluator{context, &mask};
66 evaluator.add(vert_index_);
67 evaluator.add(sort_index_);
68 evaluator.evaluate();
69 const VArray<int> vert_indices = evaluator.get_evaluated<int>(0);
70 const VArray<int> indices_in_sort = evaluator.get_evaluated<int>(1);
71
72 const bke::MeshFieldContext edge_context{mesh, AttrDomain::Edge};
73 fn::FieldEvaluator edge_evaluator{edge_context, mesh.edges_num};
74 edge_evaluator.add(sort_weight_);
75 edge_evaluator.evaluate();
76 const VArray<float> all_sort_weights = edge_evaluator.get_evaluated<float>(0);
77 const bool use_sorting = !all_sort_weights.is_single();
78
79 Array<int> edge_of_vertex(mask.min_array_size());
80 mask.foreach_segment(
81 [&](const IndexMaskSegment segment) {
82 /* Reuse arrays to avoid allocation. */
83 Array<float> sort_weights;
84 Array<int> sort_indices;
85
86 for (const int selection_i : segment) {
87 const int vert_i = vert_indices[selection_i];
88 const int index_in_sort = indices_in_sort[selection_i];
89 if (!vert_range.contains(vert_i)) {
90 edge_of_vertex[selection_i] = 0;
91 continue;
92 }
93
94 const Span<int> edges = vert_to_edge_map[vert_i];
95 if (edges.is_empty()) {
96 edge_of_vertex[selection_i] = 0;
97 continue;
98 }
99
100 const int index_in_sort_wrapped = mod_i(index_in_sort, edges.size());
101 if (use_sorting) {
102 /* Retrieve a compressed array of weights for each edge. */
103 sort_weights.reinitialize(edges.size());
104 IndexMaskMemory memory;
105 all_sort_weights.materialize_compressed(IndexMask::from_indices<int>(edges, memory),
106 sort_weights.as_mutable_span());
107
108 /* Sort a separate array of compressed indices corresponding to the compressed
109 * weights. This allows using `materialize_compressed` to avoid virtual function call
110 * overhead when accessing values in the sort weights. However, it means a separate
111 * array of indices within the compressed array is necessary for sorting. */
112 sort_indices.reinitialize(edges.size());
114 std::stable_sort(sort_indices.begin(), sort_indices.end(), [&](int a, int b) {
115 return sort_weights[a] < sort_weights[b];
116 });
117
118 edge_of_vertex[selection_i] = edges[sort_indices[index_in_sort_wrapped]];
119 }
120 else {
121 edge_of_vertex[selection_i] = edges[index_in_sort_wrapped];
122 }
123 }
124 },
126
127 return VArray<int>::from_container(std::move(edge_of_vertex));
128 }
129
130 void foreach_recursive_field(FunctionRef<void(const GField &)> fn) const override
131 {
132 fn(vert_index_);
133 fn(sort_index_);
134 fn(sort_weight_);
135 }
136
137 void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &deep_hash_cache) const final
138 {
139 static constexpr int8_t id = 0;
140 hash.add(&id);
141 hash.add(deep_hash_cache.ensure(vert_index_));
142 hash.add(deep_hash_cache.ensure(sort_index_));
143 hash.add(deep_hash_cache.ensure(sort_weight_));
144 }
145
146 std::optional<AttrDomain> preferred_domain(const Mesh & /*mesh*/) const final
147 {
148 return AttrDomain::Point;
149 }
150};
151
153 public:
154 EdgesOfVertCountInput() : bke::MeshFieldInput(CPPType::get<int>(), "Corner Face Index") {}
155
157 const AttrDomain domain,
158 const IndexMask & /*mask*/) const final
159 {
160 if (domain != AttrDomain::Point) {
161 return {};
162 }
163 Array<int> counts(mesh.verts_num, 0);
164 array_utils::count_indices(mesh.edges().cast<int>(), counts);
165 return VArray<int>::from_container(std::move(counts));
166 }
167
168 void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep & /*deep_hash_cache*/) const override
169 {
170 static constexpr int8_t id = 0;
171 hash.add(&id);
172 }
173
174 std::optional<AttrDomain> preferred_domain(const Mesh & /*mesh*/) const final
175 {
176 return AttrDomain::Point;
177 }
178};
179
181{
182 const Field<int> vert_index = params.extract_input<Field<int>>("Vertex Index"_ustr);
183 if (params.output_is_required("Total"_ustr)) {
184 params.set_output(
185 "Total"_ustr,
187 vert_index, Field<int>::from_input<EdgesOfVertCountInput>(), AttrDomain::Point));
188 }
189 if (params.output_is_required("Edge Index"_ustr)) {
190 params.set_output("Edge Index"_ustr,
192 vert_index,
193 params.extract_input<Field<int>>("Sort Index"_ustr),
194 params.extract_input<Field<float>>("Weights"_ustr)));
195 }
196}
197
198static void node_register()
199{
200 static bke::bNodeType ntype;
202 &ntype, "GeometryNodeEdgesOfVertex"_ustr, GEO_NODE_MESH_TOPOLOGY_EDGES_OF_VERTEX);
203 ntype.ui_name = "Edges of Vertex";
204 ntype.ui_description = "Retrieve the edges connected to each vertex";
205 ntype.enum_name_legacy = "EDGES_OF_VERTEX";
206 ntype.nclass = NODE_CLASS_INPUT;
208 ntype.declare = node_declare;
210}
212
213} // namespace blender::nodes::node_geo_mesh_topology_edges_of_vertex_cc
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define GEO_NODE_MESH_TOPOLOGY_EDGES_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
constexpr int64_t size() const
Definition BLI_span.hh:256
constexpr bool is_empty() const
Definition BLI_span.hh:264
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
void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &deep_hash_cache) const final
EdgesOfVertInput(Field< int > vert_index, Field< int > sort_index, Field< float > sort_weight)
void count_indices(Span< int > indices, MutableSpan< int > counts)
void fill_index_range(MutableSpan< T > span, const T start=0)
GroupedSpan< int > build_vert_to_edge_map(Span< int2 > edges, int verts_num, Array< int > &r_offsets, Array< int > &r_indices)
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