Blender V5.3
obj_export_nurbs.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 <numeric>
10
11#include "BLI_listbase.hh"
12#include "BLI_utility_mixins.hh"
13
15#include "BKE_curves.hh"
16#include "DNA_curve_types.h"
17
18#include "DEG_depsgraph.hh"
20
21#include "obj_export_nurbs.hh"
22
23namespace blender::io::obj {
24
25/* -------------------------------------------------------------------- */
28
33static int find_leftmost_span(const int8_t order, const Span<int> multiplicity)
34{
35 int index = -1;
36 int acc = 0;
37 while (acc < order) {
38 acc += multiplicity[++index];
39 }
40 BLI_assert(index > -1);
41 return index;
42}
43
48static int find_rightmost_span(const int8_t order, const Span<int> multiplicity)
49{
50 int index = multiplicity.size();
51 int acc = 0;
52 while (acc < order) {
53 acc += multiplicity[--index];
54 }
55 BLI_assert(index < multiplicity.size());
56 return index;
57}
58
60 const Span<float> knots,
61 IndexRange &point_range)
62{
63 /* No consideration for cyclic, export must expand the knot vector! */
64 BLI_assert(knots.size() == bke::curves::nurbs::knots_num(point_range.size(), order, false));
65
66 /* This assumes multiplicity < order * 2 */
67 const int order2 = order * 2;
69 knots.slice(0, order2));
71 knots.slice(knots.size() - order2, order2));
72
73 const int leftmost = find_leftmost_span(order, left_mult);
74 const int rightmost = find_rightmost_span(order, right_mult);
75
76 /* For reasonable curve knots they should add up to 0 */
77 const int acc_start = std::accumulate(left_mult.begin(), left_mult.begin() + leftmost + 1, 0);
78 const int acc_end = std::accumulate(&right_mult[rightmost], right_mult.end(), 0);
79 int skip_start = acc_start - order;
80 int skip_end = acc_end - order;
81
82 /* Update ranges */
83 point_range = point_range.drop_front(skip_start).drop_back(skip_end);
84 return knots.drop_front(skip_start).drop_back(skip_end);
85}
86
88
89/* -------------------------------------------------------------------- */
92
94 const float4x4 &transform,
95 const std::string &name)
96 : curve_(curve), transform_(transform), name_(name)
97{
98}
99
101{
102 return transform_;
103}
104
105const char *OBJCurves::get_curve_name() const
106{
107 return name_.c_str();
108}
109
111{
112 return curve_.curve_num;
113}
114
115int OBJCurves::total_spline_vertices(int spline_index) const
116{
117 return curve_.points_by_curve()[spline_index].size();
118}
119
120int OBJCurves::num_control_points_u(int spline_index) const
121{
122 return bke::curves::nurbs::control_points_num(curve_.points_by_curve()[spline_index].size(),
123 get_nurbs_degree_u(spline_index) + 1,
124 get_cyclic_u(spline_index));
125}
126
127int OBJCurves::num_control_points_v(int /*spline_index*/) const
128{
129 return 1;
130}
131
132int OBJCurves::get_nurbs_degree_u(int spline_index) const
133{
134 return curve_.nurbs_orders()[spline_index] - 1;
135}
136
137int OBJCurves::get_nurbs_degree_v(int /*spline_index*/) const
138{
139 return -1;
140}
141
142bool OBJCurves::get_cyclic_u(int spline_index) const
143{
144 return curve_.cyclic()[spline_index];
145}
146
147Span<float> OBJCurves::get_knots_u(int spline_index, Vector<float> &knot_buffer) const
148{
149 const int point_count = curve_.points_by_curve()[spline_index].size();
150 const int8_t order = curve_.nurbs_orders()[spline_index];
151 const bool cyclic = curve_.cyclic()[spline_index];
152 const KnotsMode mode = KnotsMode(curve_.nurbs_knots_modes()[spline_index]);
153 const int knot_count = bke::curves::nurbs::knots_num(point_count, order, cyclic);
154
155 knot_buffer.resize(knot_count);
156 bke::curves::nurbs::calculate_knots(point_count, mode, order, cyclic, knot_buffer);
157 return knot_buffer;
158}
159
161 Vector<float3> & /*dynamic_point_buffer*/) const
162{
163 const IndexRange point_range = curve_.points_by_curve()[spline_index];
164 return curve_.positions().slice(point_range);
165}
166
168
169/* -------------------------------------------------------------------- */
172
173OBJLegacyCurve::OBJLegacyCurve(const Depsgraph *depsgraph, Object *curve_object)
174 : export_object_eval_(curve_object)
175{
176 export_object_eval_ = DEG_get_evaluated(depsgraph, curve_object);
177 export_curve_ = id_cast<Curve *>(export_object_eval_->data);
178}
179
180const Nurb *OBJLegacyCurve::get_spline(const int spline_index) const
181{
182 return static_cast<Nurb *>(BLI_findlink(&export_curve_->nurb, spline_index));
183}
184
186{
187 return export_object_eval_->id.name + 2;
188}
189
191{
192 return export_curve_->nurb.count();
193}
194
196{
197 return export_object_eval_->object_to_world();
198}
199
200int OBJLegacyCurve::total_spline_vertices(const int spline_index) const
201{
202 const Nurb *const nurb = get_spline(spline_index);
203 return nurb->pntsu * nurb->pntsv;
204}
205
207 Vector<float3> &dynamic_point_buffer) const
208{
209 const Nurb *const nurb = get_spline(spline_index);
210 dynamic_point_buffer.resize(nurb->pntsu);
211
212 for (int64_t i = nurb->pntsu - 1; i >= 0; --i) {
213 const BPoint &bpoint = nurb->bp[i];
214 copy_v3_v3(dynamic_point_buffer[i], bpoint.vec);
215 }
216
217 return dynamic_point_buffer.as_span();
218}
219
220int OBJLegacyCurve::num_control_points_u(int spline_index) const
221{
222 const Nurb *const nurb = get_spline(spline_index);
223
225 nurb->pntsu, get_nurbs_degree_u(spline_index) + 1, get_cyclic_u(spline_index));
226}
227
228int OBJLegacyCurve::num_control_points_v(int spline_index) const
229{
230 const Nurb *const nurb = get_spline(spline_index);
231 return nurb->pntsv;
232}
233
234int OBJLegacyCurve::get_nurbs_degree_u(const int spline_index) const
235{
236 const Nurb *const nurb = get_spline(spline_index);
237 return nurb->type == CU_POLY ? 1 : nurb->orderu - 1;
238}
239
240int OBJLegacyCurve::get_nurbs_degree_v(const int spline_index) const
241{
242 const Nurb *const nurb = get_spline(spline_index);
243 return nurb->type == CU_POLY ? 1 : nurb->orderv - 1;
244}
245
246bool OBJLegacyCurve::get_cyclic_u(int spline_index) const
247{
248 const Nurb *const nurb = get_spline(spline_index);
249 return bool(nurb->flagu & CU_NURB_CYCLIC);
250}
251
252Span<float> OBJLegacyCurve::get_knots_u(int spline_index, Vector<float> &knot_buffer) const
253{
254 const Nurb *const nurb = get_spline(spline_index);
255 const short flag = nurb->flagu;
256 const int8_t order = get_nurbs_degree_u(spline_index) + 1; /* Use utility in case of POLY */
257 const bool cyclic = flag & CU_NURB_CYCLIC;
258
259 const int knot_count = bke::curves::nurbs::knots_num(nurb->pntsu, order, cyclic);
260
261 if (flag & CU_NURB_CUSTOM) {
262 return Span<float>(nurb->knotsu, knot_count);
263 }
264
265 knot_buffer.resize(knot_count);
267 nurb->pntsu, bke::knots_mode_from_legacy(flag), order, cyclic, knot_buffer);
268 return knot_buffer;
269}
270
272
273} // namespace blender::io::obj
Low-level operations for curves.
constexpr IndexRange drop_back(int64_t n) const
constexpr int64_t size() const
constexpr IndexRange slice(int64_t start, int64_t size) const
constexpr IndexRange drop_front(int64_t n) const
constexpr Span drop_front(int64_t n) const
Definition BLI_span.hh:175
constexpr Span slice(int64_t start, int64_t size) const
Definition BLI_span.hh:141
constexpr Span drop_back(int64_t n) const
Definition BLI_span.hh:186
constexpr int64_t size() const
Definition BLI_span.hh:256
void resize(const int64_t new_size)
Span< T > as_span() const
int num_control_points_v(int spline_index) const override
const char * get_curve_name() const override
int get_nurbs_degree_u(int spline_index) const override
int total_splines() const override
bool get_cyclic_u(int spline_index) const override
Span< float3 > vertex_coordinates(int spline_index, Vector< float3 > &dynamic_point_buffer) const override
OBJCurves(const bke::CurvesGeometry &curve, const float4x4 &transform, const std::string &name)
const float4x4 & object_transform() const override
int total_spline_vertices(int spline_index) const override
Span< float > get_knots_u(int spline_index, Vector< float > &buffer) const override
int num_control_points_u(int spline_index) const override
int get_nurbs_degree_v(int spline_index) const override
bool get_cyclic_u(int spline_index) const override
const float4x4 & object_transform() const override
int get_nurbs_degree_u(int spline_index) const override
int get_nurbs_degree_v(int spline_index) const override
int num_control_points_u(int spline_index) const override
OBJLegacyCurve(const Depsgraph *depsgraph, Object *curve_object)
const char * get_curve_name() const override
Span< float > get_knots_u(int spline_index, Vector< float > &buffer) const override
int total_spline_vertices(int spline_index) const override
int num_control_points_v(int spline_index) const override
Span< float3 > vertex_coordinates(int spline_index, Vector< float3 > &dynamic_point_buffer) const override
Vector< int > calculate_multiplicity_sequence(Span< float > knots)
void calculate_knots(int points_num, KnotsMode mode, int8_t order, bool cyclic, MutableSpan< float > knots)
int knots_num(int points_num, int8_t order, bool cyclic)
int control_points_num(int points_num, int8_t order, bool cyclic)
KnotsMode knots_mode_from_legacy(short flag)
static int find_rightmost_span(const int8_t order, const Span< int > multiplicity)
Span< float > valid_nurb_control_point_range(const int8_t order, const Span< float > knots, IndexRange &point_range)
static int find_leftmost_span(const int8_t order, const Span< int > multiplicity)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
MINLINE void copy_v3_v3(float r[3], const float a[3])
MatBase< float, 4, 4 > float4x4
void * BLI_findlink(const ListBase *listbase, int number) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1)
Definition listbase.cc:541
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
signed char int8_t
Definition BKE_text.h:76
ListBaseT< Nurb > nurb
eNurbKnotFlag flagu