Blender V5.3
node_geo_curve_spline_parameter.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 "BLI_array_utils.hh"
6#include "BLI_task.hh"
7
8#include "BKE_curves.hh"
9
10#include "node_geometry_util.hh"
11
13
15{
16 b.add_output<decl::Float>("Factor"_ustr)
17 .structure_type(StructureType::Field)
19 "For points, the portion of the spline's total length at the control point. For "
20 "Splines, the factor of that spline within the entire curve");
21 b.add_output<decl::Float>("Length"_ustr)
22 .structure_type(StructureType::Field)
24 "For points, the distance along the control point's spline, For splines, the "
25 "distance along the entire curve");
26 b.add_output<decl::Int>("Index"_ustr)
27 .structure_type(StructureType::Field)
28 .description("Each control point's index on its spline");
29}
30
37{
38 curves.ensure_evaluated_lengths();
39
40 Array<float> lengths(curves.curves_num());
41 const VArray<bool> cyclic = curves.cyclic();
42 float length = 0.0f;
43 for (const int i : curves.curves_range()) {
44 lengths[i] = length;
45 length += curves.evaluated_length_total_for_curve(i, cyclic[i]);
46 }
47
48 return lengths;
49}
50
52{
53 const VArray<bool> cyclic = curves.cyclic();
55
56 const int last_index = curves.curves_num() - 1;
57 const float total_length = lengths.last() + curves.evaluated_length_total_for_curve(
58 last_index, cyclic[last_index]);
59 if (total_length > 0.0f) {
60 const float factor = 1.0f / total_length;
61 for (float &value : lengths) {
62 value *= factor;
63 }
64 }
65 else {
66 /* It is arbitrary what to do in those rare cases when all the points are
67 * in the same position. In this case we are just arbitrarily giving a valid
68 * value in the range based on the curve index. */
69 for (const int i : lengths.index_range()) {
70 lengths[i] = i / (lengths.size() - 1.0f);
71 }
72 }
73 return lengths;
74}
75
88 const FunctionRef<void(MutableSpan<float>, float)> postprocess_lengths_for_curve)
89{
90 curves.ensure_evaluated_lengths();
91 const OffsetIndices points_by_curve = curves.points_by_curve();
92 const VArray<int8_t> types = curves.curve_types();
93 const VArray<int> resolutions = curves.resolution();
94 const VArray<bool> cyclic = curves.cyclic();
95
96 Array<float> result(curves.points_num());
97
98 threading::parallel_for(curves.curves_range(), 128, [&](IndexRange range) {
99 for (const int i_curve : range) {
100 const IndexRange points = points_by_curve[i_curve];
101 const bool is_cyclic = cyclic[i_curve];
102 const Span<float> evaluated_lengths = curves.evaluated_lengths_for_curve(i_curve, is_cyclic);
103 MutableSpan<float> lengths = result.as_mutable_span().slice(points);
104 lengths.first() = 0.0f;
105 const float last_evaluated_length = evaluated_lengths.is_empty() ? 0.0f :
106 evaluated_lengths.last();
107
108 float total;
109 switch (types[i_curve]) {
110 case CURVE_TYPE_CATMULL_ROM: {
111 const int resolution = resolutions[i_curve];
112 for (const int i : IndexRange(points.size()).drop_back(1)) {
113 lengths[i + 1] = evaluated_lengths[resolution * (i + 1) - 1];
114 }
115 total = last_evaluated_length;
116 break;
117 }
118 case CURVE_TYPE_POLY:
119 lengths.drop_front(1).copy_from(evaluated_lengths.take_front(lengths.size() - 1));
120 total = last_evaluated_length;
121 break;
122 case CURVE_TYPE_BEZIER: {
123 const Span<int> offsets = curves.bezier_evaluated_offsets_for_curve(i_curve);
124 for (const int i : IndexRange(points.size()).drop_back(1)) {
125 lengths[i + 1] = evaluated_lengths[offsets[i + 1] - 1];
126 }
127 total = last_evaluated_length;
128 break;
129 }
130 case CURVE_TYPE_NURBS: {
131 const Span<float3> positions = curves.positions().slice(points);
132 float length = 0.0f;
133 for (const int i : positions.index_range().drop_back(1)) {
134 lengths[i] = length;
135 length += math::distance(positions[i], positions[i + 1]);
136 }
137 lengths.last() = length;
138 if (is_cyclic) {
139 length += math::distance(positions.first(), positions.last());
140 }
141 total = length;
142 break;
143 }
144 }
145 postprocess_lengths_for_curve(lengths, total);
146 }
147 });
148 return result;
149}
150
151static void convert_lengths_to_factors(MutableSpan<float> lengths, const float total_curve_length)
152{
153 if (total_curve_length > 0.0f) {
154 const float factor = 1.0f / total_curve_length;
155 for (float &value : lengths.drop_front(1)) {
156 value *= factor;
157 }
158 }
159 else if (lengths.size() == 1) {
160 /* The curve is a single point. */
161 lengths[0] = 0.0f;
162 }
163 else {
164 /* It is arbitrary what to do in those rare cases when all the
165 * points are in the same position. In this case we are just
166 * arbitrarily giving a valid
167 * value in the range based on the point index. */
168 for (const int i : lengths.index_range()) {
169 lengths[i] = i / (lengths.size() - 1.0f);
170 }
171 }
172}
177
179 public:
181 : bke::CurvesFieldInput(CPPType::get<float>(), "Spline Parameter node")
182 {
183 }
184
186 const AttrDomain domain,
187 const IndexMask & /*mask*/) const final
188 {
189 switch (domain) {
190 case AttrDomain::Point:
192 case AttrDomain::Curve:
194 default:
196 return {};
197 }
198 }
199
200 void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep & /*deep_hash_cache*/) const override
201 {
202 static constexpr int8_t id = 0;
203 hash.add(&id);
204 }
205};
206
208 public:
210 : bke::CurvesFieldInput(CPPType::get<float>(), "Curve Length node")
211 {
212 }
213
215 const AttrDomain domain,
216 const IndexMask & /*mask*/) const final
217 {
218 switch (domain) {
219 case AttrDomain::Point:
221 curves, [](MutableSpan<float> /*lengths*/, const float /*total*/) {}));
222 case AttrDomain::Curve:
224 default:
226 return {};
227 }
228 }
229
230 void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep & /*deep_hash_cache*/) const override
231 {
232 static constexpr int8_t id = 0;
233 hash.add(&id);
234 }
235};
236
238 public:
239 IndexOnSplineFieldInput() : bke::CurvesFieldInput(CPPType::get<int>(), "Spline Index") {}
240
242 const AttrDomain domain,
243 const IndexMask & /*mask*/) const final
244 {
245 if (domain != AttrDomain::Point) {
246 return {};
247 }
248 Array<int> result(curves.points_num());
249 const OffsetIndices points_by_curve = curves.points_by_curve();
250 threading::parallel_for(curves.curves_range(), 1024, [&](IndexRange range) {
251 for (const int i_curve : range) {
252 MutableSpan<int> indices = result.as_mutable_span().slice(points_by_curve[i_curve]);
253 array_utils::fill_index_range(indices);
254 }
255 });
256 return VArray<int>::from_container(std::move(result));
257 }
258
259 void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep & /*deep_hash_cache*/) const override
260 {
261 static constexpr int8_t id = 0;
262 hash.add(&id);
263 }
264
265 std::optional<AttrDomain> preferred_domain(const bke::CurvesGeometry & /*curves*/) const final
266 {
267 return AttrDomain::Point;
268 }
269};
270
277
278static void node_register()
279{
280 static bke::bNodeType ntype;
281 geo_node_type_base(&ntype, "GeometryNodeSplineParameter"_ustr, GEO_NODE_CURVE_SPLINE_PARAMETER);
282 ntype.ui_name = "Spline Parameter";
283 ntype.ui_description = "Retrieve how far along each spline a control point is";
284 ntype.enum_name_legacy = "SPLINE_PARAMETER";
285 ntype.nclass = NODE_CLASS_INPUT;
287 ntype.declare = node_declare;
289}
290NOD_REGISTER_NODE(node_register)
291
292} // namespace blender::nodes::node_geo_curve_spline_parameter_cc
Low-level operations for curves.
#define NODE_CLASS_INPUT
Definition BKE_node.hh:478
#define GEO_NODE_CURVE_SPLINE_PARAMETER
#define final(a, b, c)
Definition BLI_hash_c.hh:21
#define NOD_REGISTER_NODE(REGISTER_FUNC)
#define BLI_assert_unreachable()
int64_t size() const
Definition BLI_array.hh:267
const T & last(const int64_t n=0) const
Definition BLI_array.hh:307
IndexRange index_range() const
Definition BLI_array.hh:371
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr MutableSpan drop_front(const int64_t n) const
Definition BLI_span.hh:613
constexpr IndexRange index_range() const
Definition BLI_span.hh:676
static VArray from_container(ContainerT container)
uint64_t hash() const
Definition field.cc:185
static Field from_input(Args &&...args)
void set_output(const UString identifier, T &&value)
GVArray get_varray_for_context(const bke::CurvesGeometry &curves, const AttrDomain domain, const IndexMask &) const final
GVArray get_varray_for_context(const bke::CurvesGeometry &curves, const AttrDomain domain, const IndexMask &) const final
void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &) const override
void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &) const override
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
float length(VecOp< float, D >) RET
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
static Array< float > calculate_point_lengths(const bke::CurvesGeometry &curves, const FunctionRef< void(MutableSpan< float >, float)> postprocess_lengths_for_curve)
static Array< float > calculate_point_parameters(const bke::CurvesGeometry &curves)
static Array< float > accumulated_lengths_curve_domain(const bke::CurvesGeometry &curves)
static void convert_lengths_to_factors(MutableSpan< float > lengths, const float total_curve_length)
static Array< float > calculate_curve_parameters(const bke::CurvesGeometry &curves)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
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
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
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