Welcome to mirror list, hosted at ThFree Co, Russian Federation.

geometry_component_curve.cc « intern « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a1f77c36cc0d9bf3668f8500861ead78311916d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "BKE_derived_curve.hh"

#include "BKE_attribute_access.hh"
#include "BKE_attribute_math.hh"
#include "BKE_geometry_set.hh"

#include "attribute_access_intern.hh"

/* -------------------------------------------------------------------- */
/** \name Geometry Component Implementation
 * \{ */

CurveComponent::CurveComponent() : GeometryComponent(GEO_COMPONENT_TYPE_CURVE)
{
}

CurveComponent::~CurveComponent()
{
  this->clear();
}

GeometryComponent *CurveComponent::copy() const
{
  CurveComponent *new_component = new CurveComponent();
  if (curve_ != nullptr) {
    new_component->curve_ = curve_->copy();
    new_component->ownership_ = GeometryOwnershipType::Owned;
  }
  return new_component;
}

void CurveComponent::clear()
{
  BLI_assert(this->is_mutable());
  if (curve_ != nullptr) {
    if (ownership_ == GeometryOwnershipType::Owned) {
      delete curve_;
    }
    curve_ = nullptr;
  }
}

bool CurveComponent::has_curve() const
{
  return curve_ != nullptr;
}

/* Clear the component and replace it with the new curve. */
void CurveComponent::replace(DCurve *curve, GeometryOwnershipType ownership)
{
  BLI_assert(this->is_mutable());
  this->clear();
  curve_ = curve;
  ownership_ = ownership;
}

DCurve *CurveComponent::release()
{
  BLI_assert(this->is_mutable());
  DCurve *curve = curve_;
  curve_ = nullptr;
  return curve;
}

const DCurve *CurveComponent::get_for_read() const
{
  return curve_;
}

DCurve *CurveComponent::get_for_write()
{
  BLI_assert(this->is_mutable());
  if (ownership_ == GeometryOwnershipType::ReadOnly) {
    curve_ = curve_->copy();
    ownership_ = GeometryOwnershipType::Owned;
  }
  return curve_;
}

bool CurveComponent::is_empty() const
{
  return curve_ == nullptr;
}

bool CurveComponent::owns_direct_data() const
{
  return ownership_ == GeometryOwnershipType::Owned;
}

void CurveComponent::ensure_owns_direct_data()
{
  BLI_assert(this->is_mutable());
  if (ownership_ != GeometryOwnershipType::Owned) {
    // curve_ = BKE_curve_copy_for_eval(curve_, false);
    ownership_ = GeometryOwnershipType::Owned;
  }
}

/** \} */

/* -------------------------------------------------------------------- */
/** \name Attribute Access
 * \{ */

int CurveComponent::attribute_domain_size(const AttributeDomain domain) const
{
  if (curve_ == nullptr) {
    return 0;
  }
  if (domain == ATTR_DOMAIN_POINT) {
    int total = 0;
    for (const SplinePtr &spline : curve_->splines) {
      total += spline->size();
    }
    return total;
  }
  if (domain == ATTR_DOMAIN_CURVE) {
    return curve_->splines.size();
  }
  return 0;
}

namespace blender::bke {

class BuiltinSplineAttributeProvider final : public BuiltinAttributeProvider {
  using AsReadAttribute = GVArrayPtr (*)(const DCurve &data);
  using AsWriteAttribute = GVMutableArrayPtr (*)(DCurve &data);
  using UpdateOnWrite = void (*)(Spline &spline);
  const AsReadAttribute as_read_attribute_;
  const AsWriteAttribute as_write_attribute_;

 public:
  BuiltinSplineAttributeProvider(std::string attribute_name,
                                 const CustomDataType attribute_type,
                                 const WritableEnum writable,
                                 const AsReadAttribute as_read_attribute,
                                 const AsWriteAttribute as_write_attribute)
      : BuiltinAttributeProvider(std::move(attribute_name),
                                 ATTR_DOMAIN_CURVE,
                                 attribute_type,
                                 BuiltinAttributeProvider::NonCreatable,
                                 writable,
                                 BuiltinAttributeProvider::NonDeletable),
        as_read_attribute_(as_read_attribute),
        as_write_attribute_(as_write_attribute)
  {
  }

  GVArrayPtr try_get_for_read(const GeometryComponent &component) const final
  {
    const CurveComponent &curve_component = static_cast<const CurveComponent &>(component);
    const DCurve *curve = curve_component.get_for_read();
    if (curve == nullptr) {
      return {};
    }

    return as_read_attribute_(*curve);
  }

  GVMutableArrayPtr try_get_for_write(GeometryComponent &component) const final
  {
    if (writable_ != Writable) {
      return {};
    }
    CurveComponent &curve_component = static_cast<CurveComponent &>(component);
    DCurve *curve = curve_component.get_for_write();
    if (curve == nullptr) {
      return {};
    }

    return as_write_attribute_(*curve);
  }

  bool try_delete(GeometryComponent &UNUSED(component)) const final
  {
    return false;
  }

  bool try_create(GeometryComponent &UNUSED(component)) const final
  {
    return false;
  }

  bool exists(const GeometryComponent &component) const final
  {
    return component.attribute_domain_size(ATTR_DOMAIN_CURVE) != 0;
  }
};

static int get_spline_resolution(const SplinePtr &spline)
{
  return spline->resolution();
}

static void set_spline_resolution(SplinePtr &spline, const int resolution)
{
  spline->set_resolution(std::max(resolution, 1));
  spline->mark_cache_invalid();
}

static GVArrayPtr make_resolution_read_attribute(const DCurve &curve)
{
  return std::make_unique<fn::GVArray_For_DerivedSpan<SplinePtr, int, get_spline_resolution>>(
      curve.splines.as_span());
}

static GVMutableArrayPtr make_resolution_write_attribute(DCurve &curve)
{
  return std::make_unique<fn::GVMutableArray_For_DerivedSpan<SplinePtr,
                                                             int,
                                                             get_spline_resolution,
                                                             set_spline_resolution>>(
      curve.splines.as_mutable_span());
}

static float get_spline_length(const SplinePtr &spline)
{
  return spline->length();
}

static GVArrayPtr make_length_attribute(const DCurve &curve)
{
  return std::make_unique<fn::GVArray_For_DerivedSpan<SplinePtr, float, get_spline_length>>(
      curve.splines.as_span());
}

static bool get_cyclic_value(const SplinePtr &spline)
{
  return spline->is_cyclic;
}

static void set_cyclic_value(SplinePtr &spline, const bool value)
{
  if (spline->is_cyclic != value) {
    spline->is_cyclic = value;
    spline->mark_cache_invalid();
  }
}

static GVArrayPtr make_cyclic_read_attribute(const DCurve &curve)
{
  return std::make_unique<fn::GVArray_For_DerivedSpan<SplinePtr, bool, get_cyclic_value>>(
      curve.splines.as_span());
}

static GVMutableArrayPtr make_cyclic_write_attribute(DCurve &curve)
{
  return std::make_unique<
      fn::GVMutableArray_For_DerivedSpan<SplinePtr, bool, get_cyclic_value, set_cyclic_value>>(
      curve.splines.as_mutable_span());
}

class BuiltinPointAttributeProvider final : public BuiltinAttributeProvider {
  using GetSplineData = void (*)(const Spline &spline, fn::GMutableSpan r_data);
  using SetSplineData = void (*)(Spline &spline, fn::GSpan data);
  const GetSplineData get_spline_data_;
  const SetSplineData set_spline_data_;

 public:
  BuiltinPointAttributeProvider(std::string attribute_name,
                                const CustomDataType attribute_type,
                                const WritableEnum writable,
                                const GetSplineData get_spline_data,
                                const SetSplineData set_spline_data)
      : BuiltinAttributeProvider(std::move(attribute_name),
                                 ATTR_DOMAIN_POINT,
                                 attribute_type,
                                 BuiltinAttributeProvider::NonCreatable,
                                 writable,
                                 BuiltinAttributeProvider::NonDeletable),
        get_spline_data_(get_spline_data),
        set_spline_data_(set_spline_data)
  {
  }

  GVArrayPtr try_get_for_read(const GeometryComponent &component) const final
  {
    const CurveComponent &curve_component = static_cast<const CurveComponent &>(component);
    const DCurve *curve = curve_component.get_for_read();
    if (curve == nullptr) {
      return {};
    }

    GVArrayPtr varray;
    attribute_math::convert_to_static_type(data_type_, [&](auto dummy) {
      using T = decltype(dummy);
      Array<T> values(curve_component.attribute_domain_size(ATTR_DOMAIN_POINT));

      int offset = 0;
      for (const SplinePtr &spline : curve->splines) {
        const int points_len = spline->size();
        MutableSpan<T> spline_data = values.as_mutable_span().slice(offset, points_len);
        fn::GMutableSpan generic_spline_data(spline_data);
        get_spline_data_(*spline, generic_spline_data);
        offset += points_len;
      }

      varray = std::make_unique<fn::GVArray_For_ArrayContainer<Array<T>>>(std::move(values));
    });

    return varray;
  }

  GVMutableArrayPtr try_get_for_write(GeometryComponent &UNUSED(component)) const final
  {
    return {};
  }

  bool try_delete(GeometryComponent &UNUSED(component)) const final
  {
    return false;
  }

  bool try_create(GeometryComponent &UNUSED(component)) const final
  {
    return false;
  }

  bool exists(const GeometryComponent &component) const final
  {
    return component.attribute_domain_size(ATTR_DOMAIN_POINT) != 0;
  }
};

static void get_spline_radius_data(const Spline &spline, fn::GMutableSpan r_data)
{
  MutableSpan<float> r_span = r_data.typed<float>();
  r_span.copy_from(spline.radii());
}

static void get_spline_position_data(const Spline &spline, fn::GMutableSpan r_data)
{
  MutableSpan<float3> r_span = r_data.typed<float3>();
  r_span.copy_from(spline.positions());
}

/**
 * In this function all the attribute providers for a curve component are created. Most data
 * in this function is statically allocated, because it does not change over time.
 */
static ComponentAttributeProviders create_attribute_providers_for_curve()
{
  static BuiltinSplineAttributeProvider resolution("resolution",
                                                   CD_PROP_INT32,
                                                   BuiltinAttributeProvider::Writable,
                                                   make_resolution_read_attribute,
                                                   make_resolution_write_attribute);

  static BuiltinSplineAttributeProvider length(
      "length", CD_PROP_FLOAT, BuiltinAttributeProvider::Readonly, make_length_attribute, nullptr);

  static BuiltinSplineAttributeProvider cyclic("cyclic",
                                               CD_PROP_BOOL,
                                               BuiltinAttributeProvider::Writable,
                                               make_cyclic_read_attribute,
                                               make_cyclic_write_attribute);

  static BuiltinPointAttributeProvider position("position",
                                                CD_PROP_FLOAT3,
                                                BuiltinAttributeProvider::Readonly,
                                                get_spline_position_data,
                                                nullptr);

  static BuiltinPointAttributeProvider radius("radius",
                                              CD_PROP_FLOAT,
                                              BuiltinAttributeProvider::Readonly,
                                              get_spline_radius_data,
                                              nullptr);

  return ComponentAttributeProviders({&resolution, &length, &cyclic, &position, &radius}, {});
}

}  // namespace blender::bke

const blender::bke::ComponentAttributeProviders *CurveComponent::get_attribute_providers() const
{
  static blender::bke::ComponentAttributeProviders providers =
      blender::bke::create_attribute_providers_for_curve();
  return &providers;
}

/** \} */