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

spline_bezier.cc « intern « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b8f3717c464950694c108bcdb90200c49065c122 (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
/*
 * 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 "BLI_array.hh"
#include "BLI_listbase.h"
#include "BLI_span.hh"

#include "BKE_spline.hh"

using blender::Array;
using blender::float3;
using blender::float4x4;
using blender::IndexRange;
using blender::MutableSpan;
using blender::Span;
using blender::Vector;

SplinePtr BezierSpline::copy() const
{
  SplinePtr new_spline = std::make_unique<BezierSpline>(*this);

  return new_spline;
}

int BezierSpline::size() const
{
  const int size = this->positions_.size();
  BLI_assert(this->handle_types_start_.size() == size);
  BLI_assert(this->handle_positions_start_.size() == size);
  BLI_assert(this->handle_types_end_.size() == size);
  BLI_assert(this->handle_positions_end_.size() == size);
  BLI_assert(this->radii_.size() == size);
  BLI_assert(this->tilts_.size() == size);
  return size;
}

int BezierSpline::resolution() const
{
  return this->resolution_u_;
}

void BezierSpline::set_resolution(const int value)
{
  this->resolution_u_ = value;
  this->mark_cache_invalid();
}

MutableSpan<float3> BezierSpline::positions()
{
  return this->positions_;
}
Span<float3> BezierSpline::positions() const
{
  return this->positions_;
}
MutableSpan<float> BezierSpline::radii()
{
  return this->radii_;
}
Span<float> BezierSpline::radii() const
{
  return this->radii_;
}
MutableSpan<float> BezierSpline::tilts()
{
  return this->tilts_;
}
Span<float> BezierSpline::tilts() const
{
  return this->tilts_;
}
Span<BezierSpline::HandleType> BezierSpline::handle_types_start() const
{
  return this->handle_types_start_;
}
MutableSpan<BezierSpline::HandleType> BezierSpline::handle_types_start()
{
  return this->handle_types_start_;
}
Span<float3> BezierSpline::handle_positions_start() const
{
  return this->handle_positions_start_;
}
MutableSpan<float3> BezierSpline::handle_positions_start()
{
  return this->handle_positions_start_;
}
Span<BezierSpline::HandleType> BezierSpline::handle_types_end() const
{
  return this->handle_types_end_;
}
MutableSpan<BezierSpline::HandleType> BezierSpline::handle_types_end()
{
  return this->handle_types_end_;
}
Span<float3> BezierSpline::handle_positions_end() const
{
  return this->handle_positions_end_;
}
MutableSpan<float3> BezierSpline::handle_positions_end()
{
  return this->handle_positions_end_;
}

void BezierSpline::add_point(const float3 position,
                             const HandleType handle_type_start,
                             const float3 handle_position_start,
                             const HandleType handle_type_end,
                             const float3 handle_position_end,
                             const float radius,
                             const float tilt)
{
  handle_types_start_.append(handle_type_start);
  handle_positions_start_.append(handle_position_start);
  positions_.append(position);
  handle_types_end_.append(handle_type_end);
  handle_positions_end_.append(handle_position_end);
  radii_.append(radius);
  tilts_.append(tilt);
}

void BezierSpline::drop_front(const int count)
{
  BLI_assert(this->size() - count > 0);
  this->handle_types_start_.remove(0, count);
  this->handle_positions_start_.remove(0, count);
  this->positions_.remove(0, count);
  this->handle_types_end_.remove(0, count);
  this->handle_positions_end_.remove(0, count);
  this->radii_.remove(0, count);
  this->tilts_.remove(0, count);
  this->mark_cache_invalid();
}

void BezierSpline::drop_back(const int count)
{
  const int new_size = this->size() - count;
  BLI_assert(new_size > 0);
  this->handle_types_start_.resize(new_size);
  this->handle_positions_start_.resize(new_size);
  this->positions_.resize(new_size);
  this->handle_types_end_.resize(new_size);
  this->handle_positions_end_.resize(new_size);
  this->radii_.resize(new_size);
  this->tilts_.resize(new_size);
  this->mark_cache_invalid();
}

bool BezierSpline::point_is_sharp(const int index) const
{
  return ELEM(handle_types_start_[index], HandleType::Vector, HandleType::Free) ||
         ELEM(handle_types_end_[index], HandleType::Vector, HandleType::Free);
}

bool BezierSpline::handle_start_is_automatic(const int index) const
{
  return ELEM(handle_types_start_[index], HandleType::Free, HandleType::Align);
}

bool BezierSpline::handle_end_is_automatic(const int index) const
{
  return ELEM(handle_types_end_[index], HandleType::Free, HandleType::Align);
}

void BezierSpline::move_control_point(const int index, const blender::float3 new_position)
{
  const float3 position_delta = new_position - positions_[index];
  if (!this->handle_start_is_automatic(index)) {
    handle_positions_start_[index] += position_delta;
  }
  if (!this->handle_end_is_automatic(index)) {
    handle_positions_end_[index] += position_delta;
  }
  positions_[index] = new_position;
}

bool BezierSpline::segment_is_vector(const int index) const
{
  if (index == this->size() - 1) {
    BLI_assert(this->is_cyclic);
    return this->handle_types_end_.last() == HandleType::Vector &&
           this->handle_types_start_.first() == HandleType::Vector;
  }
  return this->handle_types_end_[index] == HandleType::Vector &&
         this->handle_types_start_[index + 1] == HandleType::Vector;
}

int BezierSpline::evaluated_points_size() const
{
  BLI_assert(this->size() > 0);
#ifndef DEBUG
  if (!this->base_cache_dirty_) {
    /* In a non-debug build, assume that the cache's size has not changed, and that any operation
     * that would cause the cache to change its length would also mark the cache dirty. This is
     * checked at the end of this function in a debug build. */
    return this->evaluated_positions_cache_.size();
  }
#endif

  int total_len = 0;
  for (const int i : IndexRange(this->size() - 1)) {
    if (this->segment_is_vector(i)) {
      total_len += 1;
    }
    else {
      total_len += this->resolution_u_;
    }
  }

  if (this->is_cyclic) {
    if (segment_is_vector(this->size() - 1)) {
      total_len++;
    }
    else {
      total_len += this->resolution_u_;
    }
  }
  else {
    /* Since evaulating the bezier doesn't add the final point's position,
     * it must be added manually in the non-cyclic case. */
    total_len++;
  }

  /* Assert that the cache has the correct length in debug mode. */
  if (!this->base_cache_dirty_) {
    BLI_assert(this->evaluated_positions_cache_.size() == total_len);
  }

  return total_len;
}

/**
 * If the spline is not cyclic, the direction for the first and last points is just the
 * direction formed by the corresponding handles and control points. In the unlikely situation
 * that the handles define a zero direction, fallback to using the direction defined by the
 * first and last evaluated segments already calculated in #Spline::evaluated_tangents().
 */
void BezierSpline::correct_end_tangents() const
{
  MutableSpan<float3> tangents(this->evaluated_tangents_cache_);

  if (handle_positions_start_.first() != positions_.first()) {
    tangents.first() = (positions_.first() - handle_positions_start_.first()).normalized();
  }
  if (handle_positions_end_.last() != positions_.last()) {
    tangents.last() = (handle_positions_end_.last() - positions_.last()).normalized();
  }
}

static void bezier_forward_difference_3d(const float3 &point_0,
                                         const float3 &point_1,
                                         const float3 &point_2,
                                         const float3 &point_3,
                                         MutableSpan<float3> result)
{
  const float len = static_cast<float>(result.size());
  const float len_squared = len * len;
  const float len_cubed = len_squared * len;
  BLI_assert(len > 0.0f);

  const float3 rt1 = 3.0f * (point_1 - point_0) / len;
  const float3 rt2 = 3.0f * (point_0 - 2.0f * point_1 + point_2) / len_squared;
  const float3 rt3 = (point_3 - point_0 + 3.0f * (point_1 - point_2)) / len_cubed;

  float3 q0 = point_0;
  float3 q1 = rt1 + rt2 + rt3;
  float3 q2 = 2.0f * rt2 + 6.0f * rt3;
  float3 q3 = 6.0f * rt3;
  for (const int i : result.index_range()) {
    result[i] = q0;
    q0 += q1;
    q1 += q2;
    q2 += q3;
  }
}

static void evaluate_segment_mapping(Span<float3> evaluated_positions,
                                     MutableSpan<PointMapping> mappings,
                                     const int index)
{
  float length = 0.0f;
  mappings[0] = PointMapping{index, 0.0f};
  for (const int i : IndexRange(1, mappings.size() - 1)) {
    length += float3::distance(evaluated_positions[i - 1], evaluated_positions[i]);
    mappings[i] = PointMapping{index, length};
  }

  /* To get the factors instead of the accumulated lengths, divide the mapping factors by the
   * accumulated length. */
  if (length != 0.0f) {
    for (PointMapping &mapping : mappings) {
      mapping.factor /= length;
    }
  }
}

void BezierSpline::evaluate_bezier_segment(const int index,
                                           const int next_index,
                                           int &offset,
                                           MutableSpan<float3> positions,
                                           MutableSpan<PointMapping> mappings) const
{
  if (this->segment_is_vector(index)) {
    positions[offset] = positions_[index];
    mappings[offset] = PointMapping{index, 0.0f};
    offset++;
  }
  else {
    bezier_forward_difference_3d(this->positions_[index],
                                 this->handle_positions_end_[index],
                                 this->handle_positions_start_[next_index],
                                 this->positions_[next_index],
                                 positions.slice(offset, this->resolution_u_));
    evaluate_segment_mapping(positions.slice(offset, this->resolution_u_),
                             mappings.slice(offset, this->resolution_u_),
                             index);
    offset += this->resolution_u_;
  }
}

void BezierSpline::evaluate_bezier_position_and_mapping(MutableSpan<float3> positions,
                                                        MutableSpan<PointMapping> mappings) const
{
  /* TODO: It would also be possible to store an array of offsets to facilitate parallelism here,
   * maybe it is worth it? */
  int offset = 0;
  for (const int i : IndexRange(this->size() - 1)) {
    this->evaluate_bezier_segment(i, i + 1, offset, positions, mappings);
  }

  const int i_last = this->size() - 1;
  if (this->is_cyclic) {
    this->evaluate_bezier_segment(i_last, 0, offset, positions, mappings);
  }
  else {
    /* Since evaulating the bezier doesn't add the final point's position,
     * it must be added manually in the non-cyclic case. */
    positions[offset] = this->positions_.last();
    mappings[offset] = PointMapping{i_last - 1, 1.0f};
    offset++;
  }

  BLI_assert(offset == positions.size());
}

void BezierSpline::ensure_base_cache() const
{
  if (!this->base_cache_dirty_) {
    return;
  }

  std::lock_guard lock{this->base_cache_mutex_};
  if (!this->base_cache_dirty_) {
    return;
  }

  const int total = this->evaluated_points_size();
  this->evaluated_positions_cache_.resize(total);
  this->evaluated_mapping_cache_.resize(total);

  this->evaluate_bezier_position_and_mapping(this->evaluated_positions_cache_,
                                             this->evaluated_mapping_cache_);

  this->base_cache_dirty_ = false;
}