Blender V5.3
node_geo_curve_topology_points_of_curve.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 "BKE_curves.hh"
6
7#include "BLI_array_utils.hh"
8
10
12
14{
15 b.add_input<decl::Int>("Curve Index"_ustr)
16 .default_input_type(NODE_DEFAULT_INPUT_INDEX_FIELD)
17 .description("The curve to retrieve data from. Defaults to the curve 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 the curve's points. Uses indices by default");
23 b.add_input<decl::Int>("Sort Index"_ustr)
24 .structure_type(StructureType::Field)
25 .description("Which of the sorted points to output. Negative indexing is supported");
26 b.add_output<decl::Int>("Point Index"_ustr)
27 .structure_type(StructureType::Field)
28 .propagate_references()
29 .description("A point of the curve, 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 points in the curve");
34}
35
41static bool use_start_point_special_case(const Field<int> &curve_index,
42 const Field<int> &sort_index,
43 const Field<float> &sort_weights)
44{
45 if (!curve_index.get_input_if<fn::IndexFieldInput>()) {
46 return false;
47 }
48 if (sort_index.depends_on_input() || sort_weights.depends_on_input()) {
49 return false;
50 }
51 return fn::evaluate_constant_field(sort_index) == 0;
52}
53
55 const Field<int> curve_index_;
56 const Field<int> sort_index_;
57 const Field<float> sort_weight_;
58
59 public:
60 PointsOfCurveInput(Field<int> curve_index, Field<int> sort_index, Field<float> sort_weight)
61 : bke::GeometryFieldInput(CPPType::get<int>(), "Point of Curve"),
62 curve_index_(std::move(curve_index)),
63 sort_index_(std::move(sort_index)),
64 sort_weight_(std::move(sort_weight))
65 {
66 }
67
69 const IndexMask &mask) const final
70 {
71 const bke::CurvesGeometry *curves_ptr = context.curves_or_strokes();
72 if (!curves_ptr) {
73 return {};
74 }
75 const bke::CurvesGeometry &curves = *curves_ptr;
76 const OffsetIndices points_by_curve = curves.points_by_curve();
77
78 if (context.domain() == AttrDomain::Curve) {
79 if (use_start_point_special_case(curve_index_, sort_index_, sort_weight_)) {
80 return VArray<int>::from_span(points_by_curve.data());
81 }
82 }
83
84 fn::FieldEvaluator evaluator{context, &mask};
85 evaluator.add(curve_index_);
86 evaluator.add(sort_index_);
87 evaluator.evaluate();
88 const VArray<int> curve_indices = evaluator.get_evaluated<int>(0);
89 const VArray<int> indices_in_sort = evaluator.get_evaluated<int>(1);
90
91 const bke::GeometryFieldContext point_context{context, AttrDomain::Point};
92 fn::FieldEvaluator point_evaluator{point_context, curves.points_num()};
93 point_evaluator.add(sort_weight_);
94 point_evaluator.evaluate();
95 const VArray<float> all_sort_weights = point_evaluator.get_evaluated<float>(0);
96 const bool use_sorting = !all_sort_weights.is_single();
97
98 Array<int> point_of_curve(mask.min_array_size());
99 mask.foreach_segment(
100 [&](const IndexMaskSegment segment) {
101 /* Reuse arrays to avoid allocation. */
102 Array<float> sort_weights;
103 Array<int> sort_indices;
104
105 for (const int selection_i : segment) {
106 const int curve_i = curve_indices[selection_i];
107 const int index_in_sort = indices_in_sort[selection_i];
108 if (!curves.curves_range().contains(curve_i)) {
109 point_of_curve[selection_i] = 0;
110 continue;
111 }
112 const IndexRange points = points_by_curve[curve_i];
113
114 const int index_in_sort_wrapped = mod_i(index_in_sort, points.size());
115 if (use_sorting) {
116 /* Retrieve the weights for each point. */
117 sort_weights.reinitialize(points.size());
118 all_sort_weights.materialize_compressed(IndexMask(points),
119 sort_weights.as_mutable_span());
120
121 /* Sort a separate array of compressed indices corresponding to the compressed
122 * weights. This allows using `materialize_compressed` to avoid virtual function call
123 * overhead when accessing values in the sort weights. However, it means a separate
124 * array of indices within the compressed array is necessary for sorting. */
125 sort_indices.reinitialize(points.size());
127 std::stable_sort(sort_indices.begin(), sort_indices.end(), [&](int a, int b) {
128 return sort_weights[a] < sort_weights[b];
129 });
130 point_of_curve[selection_i] = points[sort_indices[index_in_sort_wrapped]];
131 }
132 else {
133 point_of_curve[selection_i] = points[index_in_sort_wrapped];
134 }
135 }
136 },
138
139 return VArray<int>::from_container(std::move(point_of_curve));
140 }
141
142 void foreach_recursive_field(FunctionRef<void(const GField &)> fn) const override
143 {
144 fn(curve_index_);
145 fn(sort_index_);
146 fn(sort_weight_);
147 }
148
149 void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &deep_hash_cache) const override
150 {
151 static constexpr int8_t id = 0;
152 hash.add(&id);
153 hash.add(deep_hash_cache.ensure(curve_index_));
154 hash.add(deep_hash_cache.ensure(sort_index_));
155 hash.add(deep_hash_cache.ensure(sort_weight_));
156 }
157
158 std::optional<AttrDomain> preferred_domain(const GeometryComponent & /*component*/) const final
159 {
160 return AttrDomain::Curve;
161 }
162};
163
165 public:
166 CurvePointCountInput() : bke::CurvesFieldInput(CPPType::get<int>(), "Curve Point Count") {}
167
169 const AttrDomain domain,
170 const IndexMask & /*mask*/) const final
171 {
172 if (domain != AttrDomain::Curve) {
173 return {};
174 }
175 const OffsetIndices points_by_curve = curves.points_by_curve();
176 return VArray<int>::from_func(curves.curves_num(), [points_by_curve](const int64_t curve_i) {
177 return points_by_curve[curve_i].size();
178 });
179 }
180
181 void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep & /*deep_hash_cache*/) const override
182 {
183 static constexpr int8_t id = 0;
184 hash.add(&id);
185 }
186
187 std::optional<AttrDomain> preferred_domain(const bke::CurvesGeometry & /*curves*/) const final
188 {
189 return AttrDomain::Curve;
190 }
191};
192
194{
195 const Field<int> curve_index = params.extract_input<Field<int>>("Curve Index"_ustr);
196 if (params.output_is_required("Total"_ustr)) {
197 params.set_output(
198 "Total"_ustr,
200 curve_index, Field<int>::from_input<CurvePointCountInput>(), AttrDomain::Curve));
201 }
202 if (params.output_is_required("Point Index"_ustr)) {
203 params.set_output("Point Index"_ustr,
205 curve_index,
206 params.extract_input<Field<int>>("Sort Index"_ustr),
207 params.extract_input<Field<float>>("Weights"_ustr)));
208 }
209}
210
211static void node_register()
212{
213 static bke::bNodeType ntype;
215 &ntype, "GeometryNodePointsOfCurve"_ustr, GEO_NODE_CURVE_TOPOLOGY_POINTS_OF_CURVE);
216 ntype.ui_name = "Points of Curve";
217 ntype.ui_description = "Retrieve a point index within a curve";
218 ntype.enum_name_legacy = "POINTS_OF_CURVE";
219 ntype.nclass = NODE_CLASS_INPUT;
221 ntype.declare = node_declare;
223}
225
226} // namespace blender::nodes::node_geo_curve_topology_points_of_curve_cc
Low-level operations for curves.
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define GEO_NODE_CURVE_TOPOLOGY_POINTS_OF_CURVE
#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
constexpr int64_t size() const
void materialize_compressed(const IndexMask &mask, MutableSpan< T > r_span) const
static VArray from_func(const int64_t size, GetFunc get_func)
static VArray from_span(Span< T > values)
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
bool depends_on_input() const
Definition FN_field.hh:513
const InputT * get_input_if() const
Definition FN_field.hh:649
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 bke::CurvesGeometry &curves, const AttrDomain domain, const IndexMask &) const final
std::optional< AttrDomain > preferred_domain(const bke::CurvesGeometry &) const final
GVArray get_varray_for_context(const bke::GeometryFieldContext &context, const IndexMask &mask) const final
PointsOfCurveInput(Field< int > curve_index, Field< int > sort_index, Field< float > sort_weight)
void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &deep_hash_cache) const override
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)
void evaluate_constant_field(const GField &field, void *r_value)
static bool use_start_point_special_case(const Field< int > &curve_index, const Field< int > &sort_index, const Field< float > &sort_weights)
MINLINE int mod_i(int i, int n)
__int64 int64_t
Definition BKE_text.h:90
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