Blender V5.3
node_geo_mesh_topology_corners_of_edge.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>("Edge Index"_ustr)
18 .default_input_type(NODE_DEFAULT_INPUT_INDEX_FIELD)
19 .description("The edge to retrieve data from. Defaults to the edge 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("Values that sort the corners attached to the edge");
25 b.add_input<decl::Int>("Sort Index"_ustr)
26 .structure_type(StructureType::Field)
27 .description("Which of the sorted corners to output. Negative indexing is supported");
28 b.add_output<decl::Int>("Corner Index"_ustr)
29 .structure_type(StructureType::Field)
30 .propagate_references()
32 "A corner of the input edge in its face's winding order, 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 faces or corners connected to each edge");
37}
38
40 const Field<int> edge_index_;
41 const Field<int> sort_index_;
42 const Field<float> sort_weight_;
43
44 public:
45 CornersOfEdgeInput(Field<int> edge_index, Field<int> sort_index, Field<float> sort_weight)
46 : bke::MeshFieldInput(CPPType::get<int>(), "Corner of Edge"),
47 edge_index_(std::move(edge_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 edge_range(mesh.edges_num);
58 Array<int> map_offsets;
59 Array<int> map_indices;
60 const Span<int> corner_edges = mesh.corner_edges();
62 mesh.corner_edges(), mesh.edges_num, map_offsets, map_indices);
63
64 const bke::MeshFieldContext context{mesh, domain};
65 fn::FieldEvaluator evaluator{context, &mask};
66 evaluator.add(edge_index_);
67 evaluator.add(sort_index_);
68 evaluator.evaluate();
69 const VArray<int> edge_indices = evaluator.get_evaluated<int>(0);
70 const VArray<int> indices_in_sort = evaluator.get_evaluated<int>(1);
71
72 const bke::MeshFieldContext corner_context{mesh, AttrDomain::Corner};
73 fn::FieldEvaluator corner_evaluator{corner_context, corner_edges.size()};
74 corner_evaluator.add(sort_weight_);
75 corner_evaluator.evaluate();
76 const VArray<float> all_sort_weights = corner_evaluator.get_evaluated<float>(0);
77 const bool use_sorting = !all_sort_weights.is_single();
78
79 Array<int> corner_of_edge(mask.min_array_size());
80 mask.foreach_segment(
81 [&](const IndexMaskSegment segment) {
82 /* Reuse arrays to avoid allocation. */
83 Array<int64_t> corner_indices;
84 Array<float> sort_weights;
85 Array<int> sort_indices;
86
87 for (const int selection_i : segment) {
88 const int edge_i = edge_indices[selection_i];
89 const int index_in_sort = indices_in_sort[selection_i];
90 if (!edge_range.contains(edge_i)) {
91 corner_of_edge[selection_i] = 0;
92 continue;
93 }
94
95 const Span<int> corners = edge_to_corner_map[edge_i];
96 if (corners.is_empty()) {
97 corner_of_edge[selection_i] = 0;
98 continue;
99 }
100
101 const int index_in_sort_wrapped = mod_i(index_in_sort, corners.size());
102 if (use_sorting) {
103 /* Retrieve a compressed array of weights for each edge. */
104 sort_weights.reinitialize(corners.size());
105 IndexMaskMemory memory;
106 all_sort_weights.materialize_compressed(
107 IndexMask::from_indices<int>(corners, memory), sort_weights.as_mutable_span());
108
109 /* Sort a separate array of compressed indices corresponding to the compressed
110 * weights. This allows using `materialize_compressed` to avoid virtual function call
111 * overhead when accessing values in the sort weights. However, it means a separate
112 * array of indices within the compressed array is necessary for sorting. */
113 sort_indices.reinitialize(corners.size());
115 std::stable_sort(sort_indices.begin(), sort_indices.end(), [&](int a, int b) {
116 return sort_weights[a] < sort_weights[b];
117 });
118 corner_of_edge[selection_i] = corners[sort_indices[index_in_sort_wrapped]];
119 }
120 else {
121 corner_of_edge[selection_i] = corners[index_in_sort_wrapped];
122 }
123 }
124 },
126
127 return VArray<int>::from_container(std::move(corner_of_edge));
128 }
129
130 void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &deep_hash_cache) const override
131 {
132 static constexpr int8_t id = 0;
133 hash.add(&id);
134 hash.add(deep_hash_cache.ensure(edge_index_));
135 hash.add(deep_hash_cache.ensure(sort_index_));
136 hash.add(deep_hash_cache.ensure(sort_weight_));
137 }
138
139 void foreach_recursive_field(FunctionRef<void(const GField &)> fn) const override
140 {
141 fn(edge_index_);
142 fn(sort_index_);
143 fn(sort_weight_);
144 }
145
146 std::optional<AttrDomain> preferred_domain(const Mesh & /*mesh*/) const final
147 {
148 return AttrDomain::Edge;
149 }
150};
151
153 public:
154 CornersOfEdgeCountInput() : bke::MeshFieldInput(CPPType::get<int>(), "Edge Corner Count") {}
155
157 const AttrDomain domain,
158 const IndexMask & /*mask*/) const final
159 {
160 if (domain != AttrDomain::Edge) {
161 return {};
162 }
163 Array<int> counts(mesh.edges_num, 0);
164 array_utils::count_indices(mesh.corner_edges(), 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::Edge;
177 }
178};
179
181{
182 const Field<int> edge_index = params.extract_input<Field<int>>("Edge Index"_ustr);
183 if (params.output_is_required("Total"_ustr)) {
184 params.set_output(
185 "Total"_ustr,
187 edge_index, Field<int>::from_input<CornersOfEdgeCountInput>(), AttrDomain::Edge));
188 }
189 if (params.output_is_required("Corner Index"_ustr)) {
190 params.set_output("Corner Index"_ustr,
192 edge_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, "GeometryNodeCornersOfEdge"_ustr, GEO_NODE_MESH_TOPOLOGY_CORNERS_OF_EDGE);
203 ntype.ui_name = "Corners of Edge";
204 ntype.ui_description = "Retrieve face corners connected to edges";
205 ntype.enum_name_legacy = "CORNERS_OF_EDGE";
206 ntype.nclass = NODE_CLASS_INPUT;
208 ntype.declare = node_declare;
210}
212
213} // namespace blender::nodes::node_geo_mesh_topology_corners_of_edge_cc
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define GEO_NODE_MESH_TOPOLOGY_CORNERS_OF_EDGE
#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
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
void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &deep_hash_cache) const override
GVArray get_varray_for_context(const Mesh &mesh, const AttrDomain domain, const IndexMask &mask) const final
CornersOfEdgeInput(Field< int > edge_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_edge_to_corner_map(Span< int > corner_edges, int edges_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
UniqueHash ensure(const GFieldRef &field)
Definition field.cc:113