Blender V5.3
node_geo_points_to_curves.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
6
7#include "BKE_attribute.hh"
8#include "BKE_curves.hh"
9
10#include "BLI_array_utils.hh"
11
13
14#include "BLI_sort.hh"
15#include "BLI_task.hh"
16
18#include "GEO_randomize.hh"
19
20#include "BKE_geometry_set.hh"
21
23
25{
26 b.add_input<decl::Geometry>("Points"_ustr)
28 .description("Points to generate curves from");
29 b.add_input<decl::Int>("Curve Group ID"_ustr)
30 .evaluated_geometry_field()
31 .hide_value()
32 .description(
33 "A curve is created for every distinct group ID. All points with the same ID are put "
34 "into the same curve");
35 b.add_input<decl::Float>("Weight"_ustr)
36 .evaluated_geometry_field()
37 .hide_value()
38 .description("Determines the order of points in each curve");
39
40 b.add_output<decl::Geometry>("Curves"_ustr).propagate_all_geometry();
41}
42
43static void grouped_sort(const OffsetIndices<int> offsets,
44 const Span<float> weights,
45 MutableSpan<int> indices)
46{
47 const auto comparator = [&](const int index_a, const int index_b) {
48 const float weight_a = weights[index_a];
49 const float weight_b = weights[index_b];
50 if (weight_a == weight_b) [[unlikely]] {
51 /* Approach to make it stable. */
52 return index_a < index_b;
53 }
54 return weight_a < weight_b;
55 };
56
57 threading::parallel_for(offsets.index_range(), 250, [&](const IndexRange range) {
58 for (const int group_index : range) {
59 MutableSpan<int> group = indices.slice(offsets[group_index]);
60 parallel_sort(group.begin(), group.end(), comparator);
61 }
62 });
63}
64
65static void find_points_by_group_index(const Span<int> indices_of_curves,
66 const bool sort,
67 MutableSpan<int> r_offsets,
68 MutableSpan<int> r_indices)
69{
70 const OffsetIndices offsets = offset_indices::build_reverse_offsets(indices_of_curves,
71 r_offsets);
72 offset_indices::reverse_indices_in_groups(indices_of_curves, offsets, r_indices, sort);
73}
74
75static int identifiers_to_indices(MutableSpan<int> r_identifiers_to_indices)
76{
77 Array<int> indices(r_identifiers_to_indices.size());
78 const int groups_num = array_utils::group_ids_to_indices(
79 r_identifiers_to_indices, indices.index_range(), indices);
80 r_identifiers_to_indices.copy_from(indices);
81 return groups_num;
82}
83
85 const VArray<float> &weights_varray,
86 const AttributeFilter &attribute_filter)
87{
88 const int domain_size = weights_varray.size();
89 Curves *curves_id = bke::curves_new_nomain_single(domain_size, CURVE_TYPE_POLY);
90 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
91 if (weights_varray.is_single()) {
92 bke::copy_attributes(attributes,
93 AttrDomain::Point,
94 AttrDomain::Point,
95 attribute_filter,
96 curves.attributes_for_write());
97 return curves_id;
98 }
99 Array<int> indices(domain_size);
101 const VArraySpan<float> weights(weights_varray);
102 grouped_sort(OffsetIndices<int>({0, domain_size}), weights, indices);
103 bke::gather_attributes(attributes,
104 AttrDomain::Point,
105 AttrDomain::Point,
106 attribute_filter,
107 indices,
108 curves.attributes_for_write());
109 return curves_id;
110}
111
113 const Field<int> &group_id_field,
114 const Field<float> &weight_field,
115 const AttributeFilter &attribute_filter)
116{
117 const int domain_size = points.totpoint;
118 if (domain_size == 0) {
119 return nullptr;
120 }
121
122 const bke::PointCloudFieldContext context(points);
123 fn::FieldEvaluator evaluator(context, domain_size);
124 evaluator.add(group_id_field);
125 evaluator.add(weight_field);
126 evaluator.evaluate();
127
128 const VArray<int> group_ids_varray = evaluator.get_evaluated<int>(0);
129 const VArray<float> weights_varray = evaluator.get_evaluated<float>(1);
130
131 if (group_ids_varray.is_single()) {
132 return curve_from_points(points.attributes(), weights_varray, attribute_filter);
133 }
134
135 Array<int> group_ids(domain_size);
136 group_ids_varray.materialize(group_ids.as_mutable_span());
137 const int total_curves = identifiers_to_indices(group_ids);
138 if (total_curves == 1) {
139 return curve_from_points(points.attributes(), weights_varray, attribute_filter);
140 }
141
142 Curves *curves_id = bke::curves_new_nomain(domain_size, total_curves);
143 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
144 curves.fill_curve_types(CURVE_TYPE_POLY);
145 MutableSpan<int> offset = curves.offsets_for_write();
146 offset.fill(0);
147
148 Array<int> indices(domain_size);
149
150 /* If the weights are specified, use them to sort the points, otherwise use the default sorting
151 * implemented by #offset_indices::reverse_indices_in_groups. */
152 const bool sort_by_weight = !weights_varray.is_single();
153 find_points_by_group_index(group_ids, !sort_by_weight, offset, indices.as_mutable_span());
154 if (sort_by_weight) {
155 const VArraySpan<float> weights(weights_varray);
156 grouped_sort(OffsetIndices<int>(offset), weights, indices);
157 }
158
159 bke::gather_attributes(points.attributes(),
160 AttrDomain::Point,
161 AttrDomain::Point,
162 attribute_filter,
163 indices,
164 curves.attributes_for_write());
165
167 return curves_id;
168}
169
171{
172 GeometrySet geometry_set = params.extract_input<GeometrySet>("Points"_ustr);
173 const Field<int> group_id_field = params.extract_input<Field<int>>("Curve Group ID"_ustr);
174 const Field<float> weight_field = params.extract_input<Field<float>>("Weight"_ustr);
175
176 const NodeAttributeFilter attribute_filter = params.get_attribute_filter("Curves"_ustr);
177 geometry::foreach_real_geometry(geometry_set, [&](GeometrySet &geometry_set) {
178 geometry_set.replace_curves(nullptr);
179 if (const PointCloud *points = geometry_set.get_pointcloud()) {
180 Curves *curves_id = curves_from_points(
181 *points, group_id_field, weight_field, attribute_filter);
182 geometry_set.replace_curves(curves_id);
183 }
185 });
186
187 params.set_output("Curves"_ustr, std::move(geometry_set));
188}
189
190static void node_register()
191{
192 static bke::bNodeType ntype;
193
194 geo_node_type_base(&ntype, "GeometryNodePointsToCurves"_ustr, GEO_NODE_POINTS_TO_CURVES);
195 ntype.ui_name = "Points to Curves";
196 ntype.ui_description = "Split all points to curve by its group ID and reorder by weight";
197 ntype.enum_name_legacy = "POINTS_TO_CURVES";
200 ntype.declare = node_declare;
202}
203NOD_REGISTER_NODE(node_register)
204
205} // namespace blender::nodes::node_geo_points_to_curves_cc
Low-level operations for curves.
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define GEO_NODE_POINTS_TO_CURVES
#define NOD_REGISTER_NODE(REGISTER_FUNC)
void sort(btMatrix3x3 &U, btVector3 &sigma, btMatrix3x3 &V, int t)
Helper function of 3X3 SVD for sorting singular values.
MutableSpan< T > as_mutable_span()
Definition BLI_array.hh:259
IndexRange index_range() const
Definition BLI_array.hh:371
constexpr int64_t size() const
Definition BLI_span.hh:499
constexpr void fill(const T &value) const
Definition BLI_span.hh:523
constexpr void copy_from(Span< T > values) const
Definition BLI_span.hh:745
void materialize(MutableSpan< T > r_span) const
int add(GField field, GVArray *varray_ptr)
const GVArray & get_evaluated(const int field_index) const
NodeAttributeFilter get_attribute_filter(const UString output_identifier) const
T extract_input(const UString identifier)
void set_output(const UString identifier, T &&value)
void fill_index_range(MutableSpan< T > span, const T start=0)
int group_ids_to_indices(Span< int > ids, const IndexMask &mask, MutableSpan< int > r_group_indices, Vector< int > *r_first_indices=nullptr)
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
void copy_attributes(const AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, MutableAttributeAccessor dst_attributes)
void gather_attributes(AttributeAccessor src_attributes, AttrDomain src_domain, AttrDomain dst_domain, const AttributeFilter &attribute_filter, const IndexMask &selection, MutableAttributeAccessor dst_attributes)
Curves * curves_new_nomain_single(int points_num, CurveType type)
Curves * curves_new_nomain(int points_num, int curves_num)
void foreach_real_geometry(bke::GeometrySet &geometry, FunctionRef< void(bke::GeometrySet &geometry_set)> fn)
void debug_randomize_curve_order(bke::CurvesGeometry *curves)
Definition randomize.cc:274
static void find_points_by_group_index(const Span< int > indices_of_curves, const bool sort, MutableSpan< int > r_offsets, MutableSpan< int > r_indices)
static void node_geo_exec(GeoNodeExecParams params)
static Curves * curve_from_points(const AttributeAccessor attributes, const VArray< float > &weights_varray, const AttributeFilter &attribute_filter)
static void grouped_sort(const OffsetIndices< int > offsets, const Span< float > weights, MutableSpan< int > indices)
static void node_declare(NodeDeclarationBuilder &b)
static Curves * curves_from_points(const PointCloud &points, const Field< int > &group_id_field, const Field< float > &weight_field, const AttributeFilter &attribute_filter)
static int identifiers_to_indices(MutableSpan< int > r_identifiers_to_indices)
OffsetIndices< int > build_reverse_offsets(Span< int > indices, MutableSpan< int > offsets)
void reverse_indices_in_groups(Span< int > group_indices, OffsetIndices< int > offsets, MutableSpan< int > results, bool sort=true)
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)
void keep_only(Span< GeometryComponent::Type > component_types)
void replace_curves(Curves *curves, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
const PointCloud * get_pointcloud() const
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