Blender V4.5
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")
18 .implicit_field(NODE_DEFAULT_INPUT_INDEX_FIELD)
19 .description("The edge to retrieve data from. Defaults to the edge from the context");
20 b.add_input<decl::Float>("Weights").supports_field().hide_value().description(
21 "Values that sort the corners attached to the edge");
22 b.add_input<decl::Int>("Sort Index")
23 .min(0)
24 .supports_field()
25 .description("Which of the sorted corners to output");
26 b.add_output<decl::Int>("Corner Index")
27 .field_source_reference_all()
29 "A corner of the input edge in its face's winding order, chosen by the sort index");
30 b.add_output<decl::Int>("Total").field_source().reference_pass({0}).description(
31 "The number of faces or corners connected to each edge");
32}
33
35 const Field<int> edge_index_;
36 const Field<int> sort_index_;
37 const Field<float> sort_weight_;
38
39 public:
40 CornersOfEdgeInput(Field<int> edge_index, Field<int> sort_index, Field<float> sort_weight)
41 : bke::MeshFieldInput(CPPType::get<int>(), "Corner of Edge"),
42 edge_index_(std::move(edge_index)),
43 sort_index_(std::move(sort_index)),
44 sort_weight_(std::move(sort_weight))
45 {
47 }
48
50 const AttrDomain domain,
51 const IndexMask &mask) const final
52 {
53 const IndexRange edge_range(mesh.edges_num);
54 Array<int> map_offsets;
55 Array<int> map_indices;
56 const Span<int> corner_edges = mesh.corner_edges();
58 mesh.corner_edges(), mesh.edges_num, map_offsets, map_indices);
59
60 const bke::MeshFieldContext context{mesh, domain};
61 fn::FieldEvaluator evaluator{context, &mask};
62 evaluator.add(edge_index_);
63 evaluator.add(sort_index_);
64 evaluator.evaluate();
65 const VArray<int> edge_indices = evaluator.get_evaluated<int>(0);
66 const VArray<int> indices_in_sort = evaluator.get_evaluated<int>(1);
67
68 const bke::MeshFieldContext corner_context{mesh, AttrDomain::Corner};
69 fn::FieldEvaluator corner_evaluator{corner_context, corner_edges.size()};
70 corner_evaluator.add(sort_weight_);
71 corner_evaluator.evaluate();
72 const VArray<float> all_sort_weights = corner_evaluator.get_evaluated<float>(0);
73 const bool use_sorting = !all_sort_weights.is_single();
74
75 Array<int> corner_of_edge(mask.min_array_size());
76 mask.foreach_segment(GrainSize(1024), [&](const IndexMaskSegment segment) {
77 /* Reuse arrays to avoid allocation. */
78 Array<int64_t> corner_indices;
79 Array<float> sort_weights;
80 Array<int> sort_indices;
81
82 for (const int selection_i : segment) {
83 const int edge_i = edge_indices[selection_i];
84 const int index_in_sort = indices_in_sort[selection_i];
85 if (!edge_range.contains(edge_i)) {
86 corner_of_edge[selection_i] = 0;
87 continue;
88 }
89
90 const Span<int> corners = edge_to_corner_map[edge_i];
91 if (corners.is_empty()) {
92 corner_of_edge[selection_i] = 0;
93 continue;
94 }
95
96 const int index_in_sort_wrapped = mod_i(index_in_sort, corners.size());
97 if (use_sorting) {
98 /* Retrieve a compressed array of weights for each edge. */
99 sort_weights.reinitialize(corners.size());
100 IndexMaskMemory memory;
101 all_sort_weights.materialize_compressed(IndexMask::from_indices<int>(corners, memory),
102 sort_weights.as_mutable_span());
103
104 /* Sort a separate array of compressed indices corresponding to the compressed weights.
105 * This allows using `materialize_compressed` to avoid virtual function call overhead
106 * when accessing values in the sort weights. However, it means a separate array of
107 * indices within the compressed array is necessary for sorting. */
108 sort_indices.reinitialize(corners.size());
110 std::stable_sort(sort_indices.begin(), sort_indices.end(), [&](int a, int b) {
111 return sort_weights[a] < sort_weights[b];
112 });
113 corner_of_edge[selection_i] = corners[sort_indices[index_in_sort_wrapped]];
114 }
115 else {
116 corner_of_edge[selection_i] = corners[index_in_sort_wrapped];
117 }
118 }
119 });
120
121 return VArray<int>::ForContainer(std::move(corner_of_edge));
122 }
123
124 void for_each_field_input_recursive(FunctionRef<void(const FieldInput &)> fn) const override
125 {
126 edge_index_.node().for_each_field_input_recursive(fn);
127 sort_index_.node().for_each_field_input_recursive(fn);
128 sort_weight_.node().for_each_field_input_recursive(fn);
129 }
130
131 std::optional<AttrDomain> preferred_domain(const Mesh & /*mesh*/) const final
132 {
133 return AttrDomain::Edge;
134 }
135};
136
138 public:
139 CornersOfEdgeCountInput() : bke::MeshFieldInput(CPPType::get<int>(), "Edge Corner Count")
140 {
142 }
143
145 const AttrDomain domain,
146 const IndexMask & /*mask*/) const final
147 {
148 if (domain != AttrDomain::Edge) {
149 return {};
150 }
151 Array<int> counts(mesh.edges_num, 0);
152 array_utils::count_indices(mesh.corner_edges(), counts);
153 return VArray<int>::ForContainer(std::move(counts));
154 }
155
157 {
158 return 2345897985577;
159 }
160
161 bool is_equal_to(const fn::FieldNode &other) const final
162 {
163 return dynamic_cast<const CornersOfEdgeCountInput *>(&other) != nullptr;
164 }
165
166 std::optional<AttrDomain> preferred_domain(const Mesh & /*mesh*/) const final
167 {
168 return AttrDomain::Edge;
169 }
170};
171
173{
174 const Field<int> edge_index = params.extract_input<Field<int>>("Edge Index");
175 if (params.output_is_required("Total")) {
176 params.set_output("Total",
177 Field<int>(std::make_shared<bke::EvaluateAtIndexInput>(
178 edge_index,
179 Field<int>(std::make_shared<CornersOfEdgeCountInput>()),
180 AttrDomain::Edge)));
181 }
182 if (params.output_is_required("Corner Index")) {
183 params.set_output("Corner Index",
184 Field<int>(std::make_shared<CornersOfEdgeInput>(
185 edge_index,
186 params.extract_input<Field<int>>("Sort Index"),
187 params.extract_input<Field<float>>("Weights"))));
188 }
189}
190
191static void node_register()
192{
193 static blender::bke::bNodeType ntype;
194 geo_node_type_base(&ntype, "GeometryNodeCornersOfEdge", GEO_NODE_MESH_TOPOLOGY_CORNERS_OF_EDGE);
195 ntype.ui_name = "Corners of Edge";
196 ntype.ui_description = "Retrieve face corners connected to edges";
197 ntype.enum_name_legacy = "CORNERS_OF_EDGE";
198 ntype.nclass = NODE_CLASS_INPUT;
200 ntype.declare = node_declare;
202}
204
205} // namespace blender::nodes::node_geo_mesh_topology_corners_of_edge_cc
#define NODE_CLASS_INPUT
Definition BKE_node.hh:433
#define GEO_NODE_MESH_TOPOLOGY_CORNERS_OF_EDGE
#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
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
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 for_each_field_input_recursive(FunctionRef< void(const FieldInput &)> fn) const override
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_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: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