Blender V5.3
geometry_component_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
8
9#include "BLI_task.hh"
10
11#include "DNA_curve_types.h"
12
13#include "BKE_curves.hh"
15#include "BKE_geometry_set.hh"
16#include "BKE_lib_id.hh"
17
18namespace blender::bke {
19
20/* -------------------------------------------------------------------- */
23
25
27 : GeometryComponent(Type::Curve), curves_(curve), ownership_(ownership)
28{
29}
30
35
37{
38 CurveComponent *new_component = new CurveComponent();
39 if (curves_ != nullptr) {
40 new_component->curves_ = BKE_curves_copy_for_eval(curves_);
41 new_component->ownership_ = GeometryOwnershipType::Owned;
42 }
43 return GeometryComponentPtr(new_component);
44}
45
47{
48 BLI_assert(this->is_mutable() || this->is_expired());
49 if (curves_ != nullptr) {
50 if (ownership_ == GeometryOwnershipType::Owned) {
51 BKE_id_free(nullptr, curves_);
52 }
53 if (curve_for_render_ != nullptr) {
54 /* The curve created by this component should not have any edit mode data. */
55 BLI_assert(curve_for_render_->editfont == nullptr && curve_for_render_->editnurb == nullptr);
56 BKE_id_free(nullptr, curve_for_render_);
57 curve_for_render_ = nullptr;
58 }
59
60 curves_ = nullptr;
61 }
62}
63
65{
66 return curves_ != nullptr;
67}
68
70{
71 BLI_assert(this->is_mutable());
72 this->clear();
73 curves_ = curves;
74 ownership_ = ownership;
75}
76
78{
79 BLI_assert(this->is_mutable());
80 Curves *curves = curves_;
81 curves_ = nullptr;
82 return curves;
83}
84
86{
87 return curves_;
88}
89
91{
92 BLI_assert(this->is_mutable());
93 if (ownership_ == GeometryOwnershipType::ReadOnly) {
94 curves_ = BKE_curves_copy_for_eval(curves_);
96 }
97 return curves_;
98}
99
101{
102 return curves_ == nullptr;
103}
104
106{
107 return ownership_ == GeometryOwnershipType::Owned;
108}
109
111{
112 BLI_assert(this->is_mutable());
113 if (ownership_ != GeometryOwnershipType::Owned) {
114 if (curves_) {
115 curves_ = BKE_curves_copy_for_eval(curves_);
116 }
117 ownership_ = GeometryOwnershipType::Owned;
118 }
119}
120
122{
123 if (curves_) {
124 curves_->geometry.wrap().count_memory(memory);
125 }
126}
127
129{
130 if (curves_ == nullptr) {
131 return nullptr;
132 }
133 if (curve_for_render_ != nullptr) {
134 return curve_for_render_;
135 }
136 std::lock_guard lock{curve_for_render_mutex_};
137 if (curve_for_render_ != nullptr) {
138 return curve_for_render_;
139 }
140
141 curve_for_render_ = BKE_id_new_nomain<Curve>(nullptr);
142 curve_for_render_->curve_eval = curves_;
143
144 return curve_for_render_;
145}
146
148
149/* -------------------------------------------------------------------- */
152
154{
155 const OffsetIndices points_by_curve = curves.points_by_curve();
156 const OffsetIndices evaluated_points_by_curve = curves.evaluated_points_by_curve();
157 const VArray<int8_t> types = curves.curve_types();
158 const VArray<int> resolutions = curves.resolution();
159 const VArray<bool> curves_cyclic = curves.cyclic();
160 const AttributeAccessor attributes = curves.attributes();
161 const VArray<float3> custom_normals = *attributes.lookup_or_default<float3>(
162 "custom_normal", AttrDomain::Point, float3(0, 0, 1));
163
164 const Span<float3> positions = curves.positions();
165 const VArray<int8_t> normal_modes = curves.normal_mode();
166
167 const Span<float3> evaluated_normals = curves.evaluated_normals();
168
169 Array<float3> results(curves.points_num());
170
171 threading::parallel_for(curves.curves_range(), 128, [&](IndexRange range) {
172 Vector<float3> nurbs_tangents;
173
174 for (const int i_curve : range) {
175 const IndexRange points = points_by_curve[i_curve];
176 const IndexRange evaluated_points = evaluated_points_by_curve[i_curve];
177
178 MutableSpan<float3> curve_normals = results.as_mutable_span().slice(points);
179
180 switch (types[i_curve]) {
181 case CURVE_TYPE_CATMULL_ROM: {
182 const Span<float3> normals = evaluated_normals.slice(evaluated_points);
183 const int resolution = resolutions[i_curve];
184 for (const int i : IndexRange(points.size())) {
185 curve_normals[i] = normals[resolution * i];
186 }
187 break;
188 }
189 case CURVE_TYPE_POLY:
190 curve_normals.copy_from(evaluated_normals.slice(evaluated_points));
191 break;
192 case CURVE_TYPE_BEZIER: {
193 const Span<float3> normals = evaluated_normals.slice(evaluated_points);
194 curve_normals.first() = normals.first();
195 const Span<int> offsets = curves.bezier_evaluated_offsets_for_curve(i_curve);
196 for (const int i : IndexRange(points.size()).drop_front(1)) {
197 curve_normals[i] = normals[offsets[i]];
198 }
199 break;
200 }
201 case CURVE_TYPE_NURBS: {
202 /* For NURBS curves there is no obvious correspondence between specific evaluated points
203 * and control points, so normals are determined by treating them as poly curves. */
204 nurbs_tangents.clear();
205 nurbs_tangents.resize(points.size());
206 const bool cyclic = curves_cyclic[i_curve];
207 const Span<float3> curve_positions = positions.slice(points);
208 curves::poly::calculate_tangents(curve_positions, cyclic, nurbs_tangents);
209 switch (NormalMode(normal_modes[i_curve])) {
210 case NORMAL_MODE_Z_UP:
211 curves::poly::calculate_normals_z_up(nurbs_tangents, curve_normals);
212 break;
213 case NORMAL_MODE_MINIMUM_TWIST:
214 curves::poly::calculate_normals_minimum(nurbs_tangents, cyclic, curve_normals);
215 break;
216 case NORMAL_MODE_FREE:
217 custom_normals.materialize(points, results);
218 break;
219 }
220 break;
221 }
222 }
223 }
224 });
225 return results;
226}
227
229{
230 const VArray<int8_t> types = curves.curve_types();
231 if (curves.is_single_type(CURVE_TYPE_POLY)) {
232 return curves.adapt_domain<float3>(
233 VArray<float3>::from_span(curves.evaluated_normals()), AttrDomain::Point, domain);
234 }
235
237
238 if (domain == AttrDomain::Point) {
239 return VArray<float3>::from_container(std::move(normals));
240 }
241
242 if (domain == AttrDomain::Curve) {
243 return curves.adapt_domain<float3>(
245 }
246
247 return nullptr;
248}
249
251
252/* -------------------------------------------------------------------- */
255
257 const AttrDomain domain)
258{
259 curves.ensure_evaluated_lengths();
260
261 VArray<bool> cyclic = curves.cyclic();
263 curves.curves_num(), [&curves, cyclic = std::move(cyclic)](int64_t index) {
264 return curves.evaluated_length_total_for_curve(index, cyclic[index]);
265 });
266
267 if (domain == AttrDomain::Curve) {
268 return lengths;
269 }
270
271 if (domain == AttrDomain::Point) {
272 return curves.adapt_domain<float>(std::move(lengths), AttrDomain::Curve, AttrDomain::Point);
273 }
274
275 return {};
276}
277
279 : CurvesFieldInput(CPPType::get<float>(), "Spline Length node")
280{
281}
282
284 const AttrDomain domain,
285 const IndexMask & /*mask*/) const
286{
288}
289
291 fn::FieldHashDeep & /*deep_hash_cache*/) const
292{
293 static constexpr int8_t id = 0;
294 hash.add(&id);
295}
296
298 const CurvesGeometry & /*curves*/) const
299{
300 return AttrDomain::Curve;
301}
302
304
305/* -------------------------------------------------------------------- */
308
309std::optional<AttributeAccessor> CurveComponent::attributes() const
310{
311 return AttributeAccessor(curves_ ? &curves_->geometry : nullptr,
313}
314
315std::optional<MutableAttributeAccessor> CurveComponent::attributes_for_write()
316{
317 Curves *curves = this->get_for_write();
318 return MutableAttributeAccessor(curves ? &curves->geometry : nullptr,
320}
321
323
324} // namespace blender::bke
Low-level operations for curves.
volatile int lock
static VArray from_func(const int64_t size, GetFunc get_func)
static VArray from_span(Span< T > values)
static VArray from_container(ContainerT container)
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
AttributeAccessor(const void *owner, const AttributeAccessorFunctions &fn)
void count_memory(MemoryCounter &memory) const override
void replace(Curves *curve, GeometryOwnershipType ownership=GeometryOwnershipType::Owned)
GeometryComponentPtr copy() const override
std::optional< AttributeAccessor > attributes() const final
std::optional< MutableAttributeAccessor > attributes_for_write() final
GVArray get_varray_for_context(const CurvesGeometry &curves, AttrDomain domain, const IndexMask &mask) const final
void hash_unique(UniqueHashBytes &hash, fn::FieldHashDeep &deep_hash_cache) const override
std::optional< AttrDomain > preferred_domain(const bke::CurvesGeometry &curves) const final
MutableAttributeAccessor(void *owner, const AttributeAccessorFunctions &fn)
uint64_t hash() const
Definition field.cc:185
const AttributeAccessorFunctions & get_attribute_accessor_functions()
ImplicitSharingPtr< GeometryComponent > GeometryComponentPtr
static Array< float3 > curve_normal_point_domain(const CurvesGeometry &curves)
VArray< float3 > curve_normals_varray(const CurvesGeometry &curves, AttrDomain domain)
static VArray< float > construct_curve_length_gvarray(const CurvesGeometry &curves, const AttrDomain domain)
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
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
struct Curves * BKE_curves_copy_for_eval(const struct Curves *curves_src)
void BKE_id_free(Main *bmain, void *idv)
void * BKE_id_new_nomain(short type, const char *name)
Definition lib_id.cc:1617
VecBase< float, 3 > float3
signed char int8_t
Definition BKE_text.h:76
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float