Blender V4.5
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")
18 .implicit_field(NODE_DEFAULT_INPUT_INDEX_FIELD)
19 .description("The vertex to retrieve data from. Defaults to the vertex from the context");
20 b.add_input<decl::Float>("Weights").supports_field().hide_value().description(
21 "Values used to sort the edges connected to the vertex. Uses indices by default");
22 b.add_input<decl::Int>("Sort Index")
23 .min(0)
24 .supports_field()
25 .description("Which of the sorted edges to output");
26 b.add_output<decl::Int>("Edge Index")
27 .field_source_reference_all()
28 .description("An edge connected to the face, chosen by the sort index");
29 b.add_output<decl::Int>("Total").field_source().reference_pass({0}).description(
30 "The number of edges connected to each vertex");
31}
32
34 const Field<int> vert_index_;
35 const Field<int> sort_index_;
36 const Field<float> sort_weight_;
37
38 public:
39 EdgesOfVertInput(Field<int> vert_index, Field<int> sort_index, Field<float> sort_weight)
40 : bke::MeshFieldInput(CPPType::get<int>(), "Edge of Vertex"),
41 vert_index_(std::move(vert_index)),
42 sort_index_(std::move(sort_index)),
43 sort_weight_(std::move(sort_weight))
44 {
46 }
47
49 const AttrDomain domain,
50 const IndexMask &mask) const final
51 {
52 const IndexRange vert_range(mesh.verts_num);
53 const Span<int2> edges = mesh.edges();
54 Array<int> map_offsets;
55 Array<int> map_indices;
57 edges, mesh.verts_num, map_offsets, map_indices);
58
59 const bke::MeshFieldContext context{mesh, domain};
60 fn::FieldEvaluator evaluator{context, &mask};
61 evaluator.add(vert_index_);
62 evaluator.add(sort_index_);
63 evaluator.evaluate();
64 const VArray<int> vert_indices = evaluator.get_evaluated<int>(0);
65 const VArray<int> indices_in_sort = evaluator.get_evaluated<int>(1);
66
67 const bke::MeshFieldContext edge_context{mesh, AttrDomain::Edge};
68 fn::FieldEvaluator edge_evaluator{edge_context, mesh.edges_num};
69 edge_evaluator.add(sort_weight_);
70 edge_evaluator.evaluate();
71 const VArray<float> all_sort_weights = edge_evaluator.get_evaluated<float>(0);
72 const bool use_sorting = !all_sort_weights.is_single();
73
74 Array<int> edge_of_vertex(mask.min_array_size());
75 mask.foreach_segment(GrainSize(1024), [&](const IndexMaskSegment segment) {
76 /* Reuse arrays to avoid allocation. */
77 Array<float> sort_weights;
78 Array<int> sort_indices;
79
80 for (const int selection_i : segment) {
81 const int vert_i = vert_indices[selection_i];
82 const int index_in_sort = indices_in_sort[selection_i];
83 if (!vert_range.contains(vert_i)) {
84 edge_of_vertex[selection_i] = 0;
85 continue;
86 }
87
88 const Span<int> edges = vert_to_edge_map[vert_i];
89 if (edges.is_empty()) {
90 edge_of_vertex[selection_i] = 0;
91 continue;
92 }
93
94 const int index_in_sort_wrapped = mod_i(index_in_sort, edges.size());
95 if (use_sorting) {
96 /* Retrieve a compressed array of weights for each edge. */
97 sort_weights.reinitialize(edges.size());
98 IndexMaskMemory memory;
99 all_sort_weights.materialize_compressed(IndexMask::from_indices<int>(edges, memory),
100 sort_weights.as_mutable_span());
101
102 /* Sort a separate array of compressed indices corresponding to the compressed weights.
103 * This allows using `materialize_compressed` to avoid virtual function call overhead
104 * when accessing values in the sort weights. However, it means a separate array of
105 * indices within the compressed array is necessary for sorting. */
106 sort_indices.reinitialize(edges.size());
108 std::stable_sort(sort_indices.begin(), sort_indices.end(), [&](int a, int b) {
109 return sort_weights[a] < sort_weights[b];
110 });
111
112 edge_of_vertex[selection_i] = edges[sort_indices[index_in_sort_wrapped]];
113 }
114 else {
115 edge_of_vertex[selection_i] = edges[index_in_sort_wrapped];
116 }
117 }
118 });
119
120 return VArray<int>::ForContainer(std::move(edge_of_vertex));
121 }
122
123 void for_each_field_input_recursive(FunctionRef<void(const FieldInput &)> fn) const override
124 {
125 vert_index_.node().for_each_field_input_recursive(fn);
126 sort_index_.node().for_each_field_input_recursive(fn);
127 sort_weight_.node().for_each_field_input_recursive(fn);
128 }
129
131 {
132 return 98762349875636;
133 }
134
135 bool is_equal_to(const fn::FieldNode &other) const final
136 {
137 if (const auto *typed = dynamic_cast<const EdgesOfVertInput *>(&other)) {
138 return typed->vert_index_ == vert_index_ && typed->sort_index_ == sort_index_ &&
139 typed->sort_weight_ == sort_weight_;
140 }
141 return false;
142 }
143
144 std::optional<AttrDomain> preferred_domain(const Mesh & /*mesh*/) const final
145 {
146 return AttrDomain::Point;
147 }
148};
149
151 public:
152 EdgesOfVertCountInput() : bke::MeshFieldInput(CPPType::get<int>(), "Corner Face Index")
153 {
155 }
156
158 const AttrDomain domain,
159 const IndexMask & /*mask*/) const final
160 {
161 if (domain != AttrDomain::Point) {
162 return {};
163 }
164 Array<int> counts(mesh.verts_num, 0);
165 array_utils::count_indices(mesh.edges().cast<int>(), counts);
166 return VArray<int>::ForContainer(std::move(counts));
167 }
168
170 {
171 return 436758278618374;
172 }
173
174 bool is_equal_to(const fn::FieldNode &other) const final
175 {
176 return dynamic_cast<const EdgesOfVertCountInput *>(&other) != nullptr;
177 }
178
179 std::optional<AttrDomain> preferred_domain(const Mesh & /*mesh*/) const final
180 {
181 return AttrDomain::Point;
182 }
183};
184
186{
187 const Field<int> vert_index = params.extract_input<Field<int>>("Vertex Index");
188 if (params.output_is_required("Total")) {
189 params.set_output("Total",
190 Field<int>(std::make_shared<bke::EvaluateAtIndexInput>(
191 vert_index,
192 Field<int>(std::make_shared<EdgesOfVertCountInput>()),
193 AttrDomain::Point)));
194 }
195 if (params.output_is_required("Edge Index")) {
196 params.set_output("Edge Index",
197 Field<int>(std::make_shared<EdgesOfVertInput>(
198 vert_index,
199 params.extract_input<Field<int>>("Sort Index"),
200 params.extract_input<Field<float>>("Weights"))));
201 }
202}
203
204static void node_register()
205{
206 static blender::bke::bNodeType ntype;
207 geo_node_type_base(&ntype, "GeometryNodeEdgesOfVertex", GEO_NODE_MESH_TOPOLOGY_EDGES_OF_VERTEX);
208 ntype.ui_name = "Edges of Vertex";
209 ntype.ui_description = "Retrieve the edges connected to each vertex";
210 ntype.enum_name_legacy = "EDGES_OF_VERTEX";
211 ntype.nclass = NODE_CLASS_INPUT;
213 ntype.declare = node_declare;
215}
217
218} // namespace blender::nodes::node_geo_mesh_topology_edges_of_vertex_cc
#define NODE_CLASS_INPUT
Definition BKE_node.hh:433
#define GEO_NODE_MESH_TOPOLOGY_EDGES_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:237
const T * end() const
Definition BLI_array.hh:314
void reinitialize(const int64_t new_size)
Definition BLI_array.hh:398
const T * begin() const
Definition BLI_array.hh:310
static IndexMask from_indices(Span< T > indices, IndexMaskMemory &memory)
constexpr bool contains(int64_t value) const
constexpr int64_t size() const
Definition BLI_span.hh:252
constexpr bool is_empty() const
Definition BLI_span.hh:260
void materialize_compressed(const IndexMask &mask, MutableSpan< T > r_span) const
static VArray ForContainer(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
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
EdgesOfVertInput(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)
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:2748
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:226
std::string ui_description
Definition BKE_node.hh:232
NodeGeometryExecFunction geometry_node_execute
Definition BKE_node.hh:347
const char * enum_name_legacy
Definition BKE_node.hh:235
NodeDeclareFunction declare
Definition BKE_node.hh:355