Blender V5.3
node_geo_construct_curves.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_array.hh"
7
8#include "BKE_curves.hh"
9#include "BKE_lib_id.hh"
10
13#include "NOD_rna_define.hh"
14#include "NOD_socket.hh"
16
17#include "list_function_eval.hh"
18#include "node_geometry_util.hh"
19
21
23{
24 b.add_output<decl::Geometry>("Curves"_ustr);
25
26 b.add_input<decl::Vector>("Positions"_ustr)
27 .structure_type(StructureType::List)
28 .hide_value()
29 .description("A list of the point positions in all curves");
30 b.add_input<decl::Int>("Curve Offsets"_ustr)
31 .structure_type(StructureType::List)
32 .hide_value()
33 .description(
34 "A list with a size one larger than the number of curves storing the first point index "
35 "of each curve and a final value for the total number of points. This is a prefix sum "
36 "of the curve sizes");
37}
38
40 const List<float3> &positions_list,
41 const List<int> &curve_offsets_list)
42{
43 const int points_num = positions_list.size();
44 const int curves_num = curve_offsets_list.size() - 1;
45 if (points_num < 1) {
46 params.error_message_add(NodeWarningType::Error, "Number of points must be greater than zero");
47 return false;
48 }
49 if (curves_num < 1) {
50 params.error_message_add(NodeWarningType::Error, "Number of curve offsets must be at least 2");
51 return false;
52 }
53 if (points_num < curves_num) {
54 params.error_message_add(
56 "Number of curves must be less than or equal to the number of points");
57 return false;
58 }
59 const std::variant<Span<int>, const int *> offset_values = curve_offsets_list.values();
60 if (const Span<int> *curve_offsets = std::get_if<Span<int>>(&offset_values)) {
61 if (curve_offsets->first() != 0) {
62 params.error_message_add(NodeWarningType::Error, "First curve offset must be 0");
63 return false;
64 }
65 if (curve_offsets->last() != points_num) {
67 "Curve sizes must sum to the number of points");
68 return false;
69 }
70 const bool sizes_greater_zero = threading::parallel_reduce(
71 curve_offsets->drop_back(1).index_range(),
72 4096,
73 true,
74 [&](const IndexRange range, const bool sizes_greater_zero) {
75 if (!sizes_greater_zero) {
76 return false;
77 }
78 for (const int i : range) {
79 const int curr = (*curve_offsets)[i];
80 const int next = (*curve_offsets)[i + 1];
81 if (next <= curr) {
82 return false;
83 }
84 }
85 return true;
86 },
87 std::logical_and<>());
88 if (!sizes_greater_zero) {
89 params.error_message_add(NodeWarningType::Error, "Curve sizes must be greater than zero");
90 return false;
91 }
92 return true;
93 }
94 /* In this case the offsets are a single value. */
95 params.error_message_add(NodeWarningType::Error, "Number of curve offsets must be at least 2");
96 return false;
97}
98
100 const List<int> &curve_offsets_list)
101{
102 const int points_num = positions_list.size();
103 BLI_assert(points_num > 0);
104 /* Curve offsets are shared with the list data. So don't create the offsets array here. */
106 bke::CurvesGeometry &curves = curves_id->geometry.wrap();
107
108 if (const auto *positions_array_data = std::get_if<nodes::GList::ArrayData>(
109 &positions_list.data()))
110 {
111 /* Share the position data with the list to avoid copying it. */
112 curves.attributes_for_write().add<float3>(
113 "position",
115 bke::AttributeInitShared(positions_array_data->data, *positions_array_data->sharing_info));
116 }
117 else if (const auto *positions_single_value = std::get_if<nodes::GList::SingleData>(
118 &positions_list.data()))
119 {
120 curves.attributes_for_write().add<float3>(
121 "position",
123 bke::AttributeInitValue(*static_cast<const float3 *>(positions_single_value->value)));
124 }
125 else {
126 /* Use VArray for the general case. This might be slow, because it has to copy data. */
127 curves.attributes_for_write().add<float3>(
128 "position", bke::AttrDomain::Point, bke::AttributeInitVArray(positions_list.varray()));
129 }
130
131 const auto *curve_offset_data = std::get_if<nodes::GList::ArrayData>(&curve_offsets_list.data());
132 BLI_assert(curve_offset_data != nullptr);
133
134 const int curves_num = curve_offsets_list.size() - 1;
135 BLI_assert(curves_num > 0);
136 curves.attribute_storage.wrap().resize(AttrDomain::Curve, curves_num);
137 /* Share the offset data with the list to avoid copying it. */
139 static_cast<int *>(const_cast<void *>(curve_offset_data->data)),
140 &(*curve_offset_data->sharing_info),
141 &curves.curve_offsets,
142 &curves.runtime->curve_offsets_sharing_info);
143 curves.curve_num = curves_num;
144
145 /* Create poly curves by default. */
146 curves.fill_curve_types(CURVE_TYPE_POLY);
147
148 return curves_id;
149}
150
152{
153 const GListPtr positions_glist = params.extract_input<GListPtr>("Positions"_ustr);
154 const GListPtr curve_offsets_glist = params.extract_input<GListPtr>("Curve Offsets"_ustr);
155 if (!positions_glist || !curve_offsets_glist) {
156 /* Empty Curves. */
158 return;
159 }
160 BLI_assert(positions_glist->cpp_type().is<float3>());
161 BLI_assert(curve_offsets_glist->cpp_type().is<int>());
162 const List<float3> &positions_list = positions_glist->typed<float3>();
163 const List<int> &curve_offsets_list = curve_offsets_glist->typed<int>();
164 if (!validate_inputs(params, positions_list, curve_offsets_list)) {
166 return;
167 }
168 if (params.output_is_required("Curves"_ustr)) {
169 Curves *curves = create_curves_from_topology_info(positions_list, curve_offsets_list);
170 params.set_output("Curves"_ustr, GeometrySet::from_curves(curves));
171 }
172}
173
174static void node_register()
175{
176 static bke::bNodeType ntype;
177 geo_node_type_base(&ntype, "GeometryNodeConstructCurves"_ustr);
178 ntype.ui_name = "Construct Curves";
179 ntype.ui_description = "Construct curves from raw topology data";
182 ntype.declare = node_declare;
184}
185NOD_REGISTER_NODE(node_register)
186
187} // namespace blender::nodes::node_geo_construct_curves_cc
Low-level operations for curves.
#define NODE_CLASS_GEOMETRY
Definition BKE_node.hh:492
#define NOD_REGISTER_NODE(REGISTER_FUNC)
bool is() const
const CPPType & cpp_type() const
const List< T > & typed() const
void error_message_add(const NodeWarningType type, StringRef message) const
T extract_input(const UString identifier)
bool output_is_required(const UString identifier) const
void set_output(const UString identifier, T &&value)
std::variant< Span< T >, const T * > values() const
void node_register_type(bNodeType &ntype)
Definition node.cc:2920
CurvesGeometry curves_new_no_attributes(int point_num, int curve_num)
Curves * curves_new_nomain(int points_num, int curves_num)
void copy_shared_pointer(T *src_ptr, const ImplicitSharingInfo *src_sharing_info, T **r_dst_ptr, const ImplicitSharingInfo **r_dst_sharing_info)
static bool validate_inputs(GeoNodeExecParams &params, const List< float3 > &positions_list, const List< int > &curve_offsets_list)
static Curves * create_curves_from_topology_info(const List< float3 > &positions_list, const List< int > &curve_offsets_list)
static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
Value parallel_reduce(IndexRange range, int64_t grain_size, const Value &identity, const Function &function, const Reduction &reduction)
Definition BLI_task.hh:127
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
VecBase< float, 3 > float3
void geo_node_type_base(bke::bNodeType *ntype, UString idname, const std::optional< int16_t > legacy_type)
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
NodeDeclareFunction declare
Definition BKE_node.hh:393
static GeometrySet from_curves(Curves *curves, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)