Blender V5.3
curve_legacy_convert.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
8
9#include "BLI_listbase.hh"
10#include "BLI_task.hh"
11#include "BLI_vector.hh"
12
13#include "DNA_curve_types.h"
14#include "DNA_curves_types.h"
15
16#include "BKE_curve.hh"
18#include "BKE_curves.hh"
19#include "BKE_curves_utils.hh"
20#include "BKE_geometry_set.hh"
21
22namespace blender::bke {
23
24static CurveType curve_type_from_legacy(const short type)
25{
26 switch (type) {
27 case CU_POLY:
28 return CURVE_TYPE_POLY;
29 case CU_BEZIER:
30 return CURVE_TYPE_BEZIER;
31 case CU_NURBS:
32 return CURVE_TYPE_NURBS;
33 }
35 return CURVE_TYPE_POLY;
36}
37
38static HandleType handle_type_from_legacy(const uint8_t handle_type_legacy)
39{
40 switch (handle_type_legacy) {
41 case HD_FREE:
42 return BEZIER_HANDLE_FREE;
43 case HD_AUTO:
44 return BEZIER_HANDLE_AUTO;
45 case HD_VECT:
47 case HD_ALIGN:
49 case HD_AUTO_ANIM:
50 return BEZIER_HANDLE_AUTO;
53 }
55 return BEZIER_HANDLE_AUTO;
56}
57
58static NormalMode normal_mode_from_legacy(const short twist_mode)
59{
60 switch (twist_mode) {
61 case CU_TWIST_Z_UP:
63 return NORMAL_MODE_Z_UP;
66 }
69}
70
72{
73 if (flag & CU_NURB_CUSTOM) {
75 }
76 switch (flag & (CU_NURB_ENDPOINT | CU_NURB_BEZIER)) {
79 case CU_NURB_BEZIER:
83 case 0:
85 }
88}
89
90Curves *curve_legacy_to_curves(const Curve &curve_legacy, const ListBaseT<Nurb> &nurbs_list)
91{
92 Vector<const Nurb *> src_curves;
93 for (const Nurb &item : nurbs_list) {
94 src_curves.append(&item);
95 }
96 if (src_curves.is_empty()) {
97 return nullptr;
98 }
99
100 Curves *curves_id = curves_new_nomain(0, src_curves.size());
101 CurvesGeometry &curves = curves_id->geometry.wrap();
102 MutableAttributeAccessor curves_attributes = curves.attributes_for_write();
103
104 MutableSpan<int8_t> types = curves.curve_types_for_write();
105 MutableSpan<bool> cyclic = curves.cyclic_for_write();
106
107 int offset = 0;
108 MutableSpan<int> offsets = curves.offsets_for_write();
109 for (const int i : src_curves.index_range()) {
110 offsets[i] = offset;
111
112 const Nurb &src_curve = *src_curves[i];
113 types[i] = curve_type_from_legacy(src_curve.type);
114 cyclic[i] = src_curve.flagu & CU_NURB_CYCLIC;
115
116 offset += src_curve.pntsu;
117 }
118 offsets.last() = offset;
119 curves.resize(curves.offsets().last(), curves.curves_num());
120
121 curves.update_curve_types();
122
123 const OffsetIndices points_by_curve = curves.points_by_curve();
124 MutableSpan<float3> positions = curves.positions_for_write();
125 SpanAttributeWriter<float> radius_attribute =
126 curves_attributes.lookup_or_add_for_write_only_span<float>("radius", AttrDomain::Point);
127 MutableSpan<float> radii = radius_attribute.span;
128 MutableSpan<float> tilts = curves.tilt_for_write();
129
130 auto create_poly = [&](const IndexMask &selection) {
131 selection.foreach_index(
132 [&](const int curve_i) {
133 const Nurb &src_curve = *src_curves[curve_i];
134 const Span<BPoint> src_points(src_curve.bp, src_curve.pntsu);
135 const IndexRange points = points_by_curve[curve_i];
136
137 for (const int i : src_points.index_range()) {
138 const BPoint &bp = src_points[i];
139 positions[points[i]] = bp.vec;
140 radii[points[i]] = bp.radius;
141 tilts[points[i]] = bp.tilt;
142 }
143 },
145 };
146
147 /* NOTE: For curve handles, legacy curves can end up in invalid situations where the handle
148 * positions don't agree with the types because of evaluation, or because one-sided aligned
149 * handles weren't considered. While recalculating automatic handles to fix those situations
150 * is an option, currently this opts not to for the sake of flexibility. */
151 auto create_bezier = [&](const IndexMask &selection) {
152 MutableSpan<int> resolutions = curves.resolution_for_write();
153 MutableSpan<float3> handle_positions_l = curves.handle_positions_left_for_write();
154 MutableSpan<float3> handle_positions_r = curves.handle_positions_right_for_write();
155 MutableSpan<int8_t> handle_types_l = curves.handle_types_left_for_write();
156 MutableSpan<int8_t> handle_types_r = curves.handle_types_right_for_write();
157
158 selection.foreach_index(
159 [&](const int curve_i) {
160 const Nurb &src_curve = *src_curves[curve_i];
161 const Span<BezTriple> src_points(src_curve.bezt, src_curve.pntsu);
162 const IndexRange points = points_by_curve[curve_i];
163
164 resolutions[curve_i] = src_curve.resolu;
165
166 for (const int i : src_points.index_range()) {
167 const BezTriple &point = src_points[i];
168 positions[points[i]] = point.vec[1];
169 handle_positions_l[points[i]] = point.vec[0];
170 handle_types_l[points[i]] = handle_type_from_legacy(point.h1);
171 handle_positions_r[points[i]] = point.vec[2];
172 handle_types_r[points[i]] = handle_type_from_legacy(point.h2);
173 radii[points[i]] = point.radius;
174 tilts[points[i]] = point.tilt;
175 }
176 },
178 };
179
180 auto create_nurbs = [&](const IndexMask &selection) {
181 MutableSpan<int> resolutions = curves.resolution_for_write();
182 MutableSpan<float> nurbs_weights = curves.nurbs_weights_for_write();
183 MutableSpan<int8_t> nurbs_orders = curves.nurbs_orders_for_write();
184 MutableSpan<int8_t> nurbs_knots_modes = curves.nurbs_knots_modes_for_write();
185
186 selection.foreach_index(
187 [&](const int curve_i) {
188 const Nurb &src_curve = *src_curves[curve_i];
189 const Span src_points(src_curve.bp, src_curve.pntsu);
190 const IndexRange points = points_by_curve[curve_i];
191
192 resolutions[curve_i] = src_curve.resolu;
193 nurbs_orders[curve_i] = src_curve.orderu;
194 nurbs_knots_modes[curve_i] = knots_mode_from_legacy(src_curve.flagu);
195
196 for (const int i : src_points.index_range()) {
197 const BPoint &bp = src_points[i];
198 positions[points[i]] = bp.vec;
199 radii[points[i]] = bp.radius;
200 tilts[points[i]] = bp.tilt;
201 nurbs_weights[points[i]] = bp.vec[3];
202 }
203 },
205
206 curves.nurbs_custom_knots_update_size();
207 if (!curves.nurbs_has_custom_knots()) {
208 return;
209 }
210
211 const OffsetIndices<int> knots_by_curve = curves.nurbs_custom_knots_by_curve();
212 MutableSpan<float> custom_knots = curves.nurbs_custom_knots_for_write();
213 selection.foreach_index([&](const int curve_i) {
214 if (nurbs_knots_modes[curve_i] == NURBS_KNOT_MODE_CUSTOM) {
215 const Nurb &src_curve = *src_curves[curve_i];
216 const IndexRange knots = knots_by_curve[curve_i];
217 custom_knots.slice(knots).copy_from(Span<float>(src_curve.knotsu, knots.size()));
218 }
219 });
220 };
221
223 curves.curve_types(),
224 curves.curve_type_counts(),
225 curves.curves_range(),
226 [&](const IndexMask & /*selection*/) { BLI_assert_unreachable(); },
227 create_poly,
228 create_bezier,
229 create_nurbs);
230
231 curves_attributes.add<int8_t>(
232 "normal_mode",
235
236 radius_attribute.finish();
237
238 curves_id->mat = MEM_dupalloc(curve_legacy.mat);
239 curves_id->totcol = curve_legacy.totcol;
240
241 return curves_id;
242}
243
245{
246 return curve_legacy_to_curves(curve_legacy, *BKE_curve_nurbs_get_for_read(&curve_legacy));
247}
248
249} // namespace blender::bke
Low-level operations for curves.
Low-level operations for curves.
#define BLI_assert_unreachable()
constexpr int64_t size() const
constexpr MutableSpan slice(const int64_t start, const int64_t size) const
Definition BLI_span.hh:579
constexpr void copy_from(Span< T > values) const
Definition BLI_span.hh:745
constexpr T & last(const int64_t n=0) const
Definition BLI_span.hh:695
constexpr IndexRange index_range() const
Definition BLI_span.hh:405
int64_t size() const
void append(const T &value)
bool is_empty() const
IndexRange index_range() const
bool add(const StringRef name, const AttrDomain domain, const AttrType data_type, const AttributeInit &initializer)
GSpanAttributeWriter lookup_or_add_for_write_only_span(StringRef name, AttrDomain domain, AttrType data_type)
void foreach_curve_by_type(const VArray< int8_t > &types, const std::array< int, CURVE_TYPES_NUM > &type_counts, const IndexMask &selection, FunctionRef< void(IndexMask)> catmull_rom_fn, FunctionRef< void(IndexMask)> poly_fn, FunctionRef< void(IndexMask)> bezier_fn, FunctionRef< void(IndexMask)> nurbs_fn)
Curves * curve_legacy_to_curves(const Curve &curve_legacy)
static CurveType curve_type_from_legacy(const short type)
static NormalMode normal_mode_from_legacy(const short twist_mode)
static HandleType handle_type_from_legacy(const uint8_t handle_type_legacy)
KnotsMode knots_mode_from_legacy(short flag)
Curves * curves_new_nomain(int points_num, int curves_num)
constexpr ParallelGrainSize grain_size(int grain_size)
const ListBaseT< Nurb > * BKE_curve_nurbs_get_for_read(const Curve *cu)
Definition curve.cc:5001
@ NURBS_KNOT_MODE_ENDPOINT_BEZIER
@ NURBS_KNOT_MODE_BEZIER
@ NURBS_KNOT_MODE_ENDPOINT
@ NURBS_KNOT_MODE_NORMAL
@ NURBS_KNOT_MODE_CUSTOM
unsigned char uint8_t
Definition BKE_text.h:79
@ NORMAL_MODE_MINIMUM_TWIST
signed char int8_t
Definition BKE_text.h:76
eBezTriple_Handle h2
eBezTriple_Handle h1
eCurveTwistMode twist_mode
struct Material ** mat
BezTriple * bezt
eNurbKnotFlag flagu