Blender V4.5
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")
16 .implicit_field(NODE_DEFAULT_INPUT_INDEX_FIELD)
17 .description("The curve to retrieve data from. Defaults to the curve from the context");
18 b.add_input<decl::Float>("Weights").supports_field().hide_value().description(
19 "Values used to sort the curve's points. Uses indices by default");
20 b.add_input<decl::Int>("Sort Index")
21 .min(0)
22 .supports_field()
23 .description("Which of the sorted points to output");
24 b.add_output<decl::Int>("Point Index")
25 .field_source_reference_all()
26 .description("A point of the curve, chosen by the sort index");
27 b.add_output<decl::Int>("Total").field_source().reference_pass({0}).description(
28 "The number of points in the curve");
29}
30
36static bool use_start_point_special_case(const Field<int> &curve_index,
37 const Field<int> &sort_index,
38 const Field<float> &sort_weights)
39{
40 if (!dynamic_cast<const fn::IndexFieldInput *>(&curve_index.node())) {
41 return false;
42 }
43 if (sort_index.node().depends_on_input() || sort_weights.node().depends_on_input()) {
44 return false;
45 }
46 return fn::evaluate_constant_field(sort_index) == 0;
47}
48
50 const Field<int> curve_index_;
51 const Field<int> sort_index_;
52 const Field<float> sort_weight_;
53
54 public:
55 PointsOfCurveInput(Field<int> curve_index, Field<int> sort_index, Field<float> sort_weight)
56 : bke::GeometryFieldInput(CPPType::get<int>(), "Point of Curve"),
57 curve_index_(std::move(curve_index)),
58 sort_index_(std::move(sort_index)),
59 sort_weight_(std::move(sort_weight))
60 {
62 }
63
65 const IndexMask &mask) const final
66 {
67 const bke::CurvesGeometry *curves_ptr = context.curves_or_strokes();
68 if (!curves_ptr) {
69 return {};
70 }
71 const bke::CurvesGeometry &curves = *curves_ptr;
72 const OffsetIndices points_by_curve = curves.points_by_curve();
73
74 if (context.domain() == AttrDomain::Curve) {
75 if (use_start_point_special_case(curve_index_, sort_index_, sort_weight_)) {
76 return VArray<int>::ForSpan(points_by_curve.data());
77 }
78 }
79
80 fn::FieldEvaluator evaluator{context, &mask};
81 evaluator.add(curve_index_);
82 evaluator.add(sort_index_);
83 evaluator.evaluate();
84 const VArray<int> curve_indices = evaluator.get_evaluated<int>(0);
85 const VArray<int> indices_in_sort = evaluator.get_evaluated<int>(1);
86
87 const bke::GeometryFieldContext point_context{context, AttrDomain::Point};
88 fn::FieldEvaluator point_evaluator{point_context, curves.points_num()};
89 point_evaluator.add(sort_weight_);
90 point_evaluator.evaluate();
91 const VArray<float> all_sort_weights = point_evaluator.get_evaluated<float>(0);
92 const bool use_sorting = !all_sort_weights.is_single();
93
94 Array<int> point_of_curve(mask.min_array_size());
95 mask.foreach_segment(GrainSize(256), [&](const IndexMaskSegment segment) {
96 /* Reuse arrays to avoid allocation. */
97 Array<float> sort_weights;
98 Array<int> sort_indices;
99
100 for (const int selection_i : segment) {
101 const int curve_i = curve_indices[selection_i];
102 const int index_in_sort = indices_in_sort[selection_i];
103 if (!curves.curves_range().contains(curve_i)) {
104 point_of_curve[selection_i] = 0;
105 continue;
106 }
107 const IndexRange points = points_by_curve[curve_i];
108
109 const int index_in_sort_wrapped = mod_i(index_in_sort, points.size());
110 if (use_sorting) {
111 /* Retrieve the weights for each point. */
112 sort_weights.reinitialize(points.size());
113 all_sort_weights.materialize_compressed(IndexMask(points),
114 sort_weights.as_mutable_span());
115
116 /* Sort a separate array of compressed indices corresponding to the compressed weights.
117 * This allows using `materialize_compressed` to avoid virtual function call overhead
118 * when accessing values in the sort weights. However, it means a separate array of
119 * indices within the compressed array is necessary for sorting. */
120 sort_indices.reinitialize(points.size());
122 std::stable_sort(sort_indices.begin(), sort_indices.end(), [&](int a, int b) {
123 return sort_weights[a] < sort_weights[b];
124 });
125 point_of_curve[selection_i] = points[sort_indices[index_in_sort_wrapped]];
126 }
127 else {
128 point_of_curve[selection_i] = points[index_in_sort_wrapped];
129 }
130 }
131 });
132
133 return VArray<int>::ForContainer(std::move(point_of_curve));
134 }
135
136 void for_each_field_input_recursive(FunctionRef<void(const FieldInput &)> fn) const override
137 {
138 curve_index_.node().for_each_field_input_recursive(fn);
139 sort_index_.node().for_each_field_input_recursive(fn);
140 sort_weight_.node().for_each_field_input_recursive(fn);
141 }
142
143 uint64_t hash() const override
144 {
145 return 26978695677882;
146 }
147
148 bool is_equal_to(const fn::FieldNode &other) const override
149 {
150 if (const auto *typed = dynamic_cast<const PointsOfCurveInput *>(&other)) {
151 return typed->curve_index_ == curve_index_ && typed->sort_index_ == sort_index_ &&
152 typed->sort_weight_ == sort_weight_;
153 }
154 return false;
155 }
156
157 std::optional<AttrDomain> preferred_domain(const GeometryComponent & /*component*/) const final
158 {
159 return AttrDomain::Curve;
160 }
161};
162
164 public:
165 CurvePointCountInput() : bke::CurvesFieldInput(CPPType::get<int>(), "Curve Point Count")
166 {
168 }
169
171 const AttrDomain domain,
172 const IndexMask & /*mask*/) const final
173 {
174 if (domain != AttrDomain::Curve) {
175 return {};
176 }
177 const OffsetIndices points_by_curve = curves.points_by_curve();
178 return VArray<int>::ForFunc(curves.curves_num(), [points_by_curve](const int64_t curve_i) {
179 return points_by_curve[curve_i].size();
180 });
181 }
182
184 {
185 return 903847569873762;
186 }
187
188 bool is_equal_to(const fn::FieldNode &other) const final
189 {
190 return dynamic_cast<const CurvePointCountInput *>(&other) != nullptr;
191 }
192
193 std::optional<AttrDomain> preferred_domain(const bke::CurvesGeometry & /*curves*/) const final
194 {
195 return AttrDomain::Curve;
196 }
197};
198
200{
201 const Field<int> curve_index = params.extract_input<Field<int>>("Curve Index");
202 if (params.output_is_required("Total")) {
203 params.set_output("Total",
204 Field<int>(std::make_shared<bke::EvaluateAtIndexInput>(
205 curve_index,
206 Field<int>(std::make_shared<CurvePointCountInput>()),
207 AttrDomain::Curve)));
208 }
209 if (params.output_is_required("Point Index")) {
210 params.set_output("Point Index",
211 Field<int>(std::make_shared<PointsOfCurveInput>(
212 curve_index,
213 params.extract_input<Field<int>>("Sort Index"),
214 params.extract_input<Field<float>>("Weights"))));
215 }
216}
217
218static void node_register()
219{
220 static blender::bke::bNodeType ntype;
221 geo_node_type_base(&ntype, "GeometryNodePointsOfCurve", GEO_NODE_CURVE_TOPOLOGY_POINTS_OF_CURVE);
222 ntype.ui_name = "Points of Curve";
223 ntype.ui_description = "Retrieve a point index within a curve";
224 ntype.enum_name_legacy = "POINTS_OF_CURVE";
225 ntype.nclass = NODE_CLASS_INPUT;
227 ntype.declare = node_declare;
229}
231
232} // namespace blender::nodes::node_geo_curve_topology_points_of_curve_cc
Low-level operations for curves.
#define NODE_CLASS_INPUT
Definition BKE_node.hh:433
#define GEO_NODE_CURVE_TOPOLOGY_POINTS_OF_CURVE
#define final(a, b, c)
Definition BLI_hash.h:19
MINLINE int mod_i(int i, int n)
#define NOD_REGISTER_NODE(REGISTER_FUNC)
long long int int64_t
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
constexpr int64_t size() const
constexpr bool contains(int64_t value) const
void materialize_compressed(const IndexMask &mask, MutableSpan< T > r_span) const
static VArray ForContainer(ContainerT container)
static VArray ForSpan(Span< T > values)
static VArray ForFunc(const int64_t size, GetFunc get_func)
OffsetIndices< int > points_by_curve() const
IndexRange curves_range() const
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
bool depends_on_input() const
Definition FN_field.hh:554
const FieldNode & node() const
Definition FN_field.hh:135
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
void for_each_field_input_recursive(FunctionRef< void(const FieldInput &)> fn) const override
PointsOfCurveInput(Field< int > curve_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 fill_index_range(MutableSpan< T > span, const T start=0)
void node_register_type(bNodeType &ntype)
Definition node.cc:2748
void evaluate_constant_field(const GField &field, void *r_value)
Definition field.cc:493
static bool use_start_point_special_case(const Field< int > &curve_index, const Field< int > &sort_index, const Field< float > &sort_weights)
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