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

curve_decimate.c « intern « blenkernel « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 200b827703050d13851d930fb632648b51016538 (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
/* SPDX-License-Identifier: GPL-2.0-or-later */

/** \file
 * \ingroup bke
 */

#include "DNA_curve_types.h"

#include "BLI_heap.h"
#include "BLI_math_vector.h"
#include "MEM_guardedalloc.h"

#include "BKE_curve.h"

#include "curve_fit_nd.h"

#include "BLI_strict_flags.h"

struct Knot {
  struct Knot *next, *prev;
  uint point_index; /* Index in point array. */
  uint knot_index;  /* Index in knot array. */
  float tan[2][3];
  float handles[2];

  HeapNode *heap_node;
  uint can_remove : 1;
  uint is_removed : 1;

#ifndef NDEBUG
  const float *co;
#endif
};

struct Removal {
  uint knot_index;
  /* handles for prev/next knots */
  float handles[2];
};

static float knot_remove_error_value(const float tan_l[3],
                                     const float tan_r[3],
                                     const float (*points)[3],
                                     const uint points_len,
                                     /* avoid having to re-calculate again */
                                     float r_handle_factors[2])
{
  const uint dims = 3;
  float error_sq = FLT_MAX;
  uint error_sq_index;
  float handle_factors[2][3];

  curve_fit_cubic_to_points_single_fl(&points[0][0],
                                      points_len,
                                      NULL,
                                      dims,
                                      0.0f,
                                      tan_l,
                                      tan_r,
                                      handle_factors[0],
                                      handle_factors[1],
                                      &error_sq,
                                      &error_sq_index);

  sub_v3_v3(handle_factors[0], points[0]);
  r_handle_factors[0] = dot_v3v3(tan_l, handle_factors[0]);

  sub_v3_v3(handle_factors[1], points[points_len - 1]);
  r_handle_factors[1] = dot_v3v3(tan_r, handle_factors[1]);

  return error_sq;
}

static void knot_remove_error_recalculate(Heap *heap,
                                          const float (*points)[3],
                                          const uint points_len,
                                          struct Knot *k,
                                          const float error_sq_max)
{
  BLI_assert(k->can_remove);
  float handles[2];

#ifndef NDEBUG
  BLI_assert(equals_v3v3(points[k->prev->point_index], k->prev->co));
  BLI_assert(equals_v3v3(points[k->next->point_index], k->next->co));
#endif

  const float(*points_offset)[3];
  uint points_offset_len;

  if (k->prev->point_index < k->next->point_index) {
    points_offset = &points[k->prev->point_index];
    points_offset_len = (k->next->point_index - k->prev->point_index) + 1;
  }
  else {
    points_offset = &points[k->prev->point_index];
    points_offset_len = ((k->next->point_index + points_len) - k->prev->point_index) + 1;
  }

  const float cost_sq = knot_remove_error_value(
      k->prev->tan[1], k->next->tan[0], points_offset, points_offset_len, handles);

  if (cost_sq < error_sq_max) {
    struct Removal *r;
    if (k->heap_node) {
      r = BLI_heap_node_ptr(k->heap_node);
    }
    else {
      r = MEM_mallocN(sizeof(*r), __func__);
      r->knot_index = k->knot_index;
    }

    copy_v2_v2(r->handles, handles);

    BLI_heap_insert_or_update(heap, &k->heap_node, cost_sq, r);
  }
  else {
    if (k->heap_node) {
      struct Removal *r;
      r = BLI_heap_node_ptr(k->heap_node);
      BLI_heap_remove(heap, k->heap_node);

      MEM_freeN(r);

      k->heap_node = NULL;
    }
  }
}

static void curve_decimate(const float (*points)[3],
                           const uint points_len,
                           struct Knot *knots,
                           const uint knots_len,
                           float error_sq_max,
                           const uint error_target_len)
{
  Heap *heap = BLI_heap_new_ex(knots_len);
  for (uint i = 0; i < knots_len; i++) {
    struct Knot *k = &knots[i];
    if (k->can_remove) {
      knot_remove_error_recalculate(heap, points, points_len, k, error_sq_max);
    }
  }

  uint knots_len_remaining = knots_len;

  while ((knots_len_remaining > error_target_len) && (BLI_heap_is_empty(heap) == false)) {
    struct Knot *k;

    {
      struct Removal *r = BLI_heap_pop_min(heap);
      k = &knots[r->knot_index];
      k->heap_node = NULL;
      k->prev->handles[1] = r->handles[0];
      k->next->handles[0] = r->handles[1];
      MEM_freeN(r);
    }

    struct Knot *k_prev = k->prev;
    struct Knot *k_next = k->next;

    /* remove ourselves */
    k_next->prev = k_prev;
    k_prev->next = k_next;

    k->next = NULL;
    k->prev = NULL;
    k->is_removed = true;

    if (k_prev->can_remove) {
      knot_remove_error_recalculate(heap, points, points_len, k_prev, error_sq_max);
    }

    if (k_next->can_remove) {
      knot_remove_error_recalculate(heap, points, points_len, k_next, error_sq_max);
    }

    knots_len_remaining -= 1;
  }

  BLI_heap_free(heap, MEM_freeN);
}

uint BKE_curve_decimate_bezt_array(BezTriple *bezt_array,
                                   const uint bezt_array_len,
                                   const uint resolu,
                                   const bool is_cyclic,
                                   const char flag_test,
                                   const char flag_set,
                                   const float error_sq_max,
                                   const uint error_target_len)
{
  const uint bezt_array_last = bezt_array_len - 1;
  const uint points_len = BKE_curve_calc_coords_axis_len(bezt_array_len, resolu, is_cyclic, true);

  float(*points)[3] = MEM_mallocN((sizeof(float[3]) * points_len * (is_cyclic ? 2 : 1)), __func__);

  BKE_curve_calc_coords_axis(
      bezt_array, bezt_array_len, resolu, is_cyclic, false, 0, sizeof(float[3]), &points[0][0]);
  BKE_curve_calc_coords_axis(
      bezt_array, bezt_array_len, resolu, is_cyclic, false, 1, sizeof(float[3]), &points[0][1]);
  BKE_curve_calc_coords_axis(
      bezt_array, bezt_array_len, resolu, is_cyclic, false, 2, sizeof(float[3]), &points[0][2]);

  const uint knots_len = bezt_array_len;
  struct Knot *knots = MEM_mallocN((sizeof(*knots) * bezt_array_len), __func__);

  if (is_cyclic) {
    memcpy(points[points_len], points[0], sizeof(float[3]) * points_len);
  }

  for (uint i = 0; i < bezt_array_len; i++) {
    knots[i].heap_node = NULL;
    knots[i].can_remove = (bezt_array[i].f2 & flag_test) != 0;
    knots[i].is_removed = false;
    knots[i].next = &knots[i + 1];
    knots[i].prev = &knots[i - 1];
    knots[i].point_index = i * resolu;
    knots[i].knot_index = i;

    sub_v3_v3v3(knots[i].tan[0], bezt_array[i].vec[0], bezt_array[i].vec[1]);
    knots[i].handles[0] = normalize_v3(knots[i].tan[0]);

    sub_v3_v3v3(knots[i].tan[1], bezt_array[i].vec[1], bezt_array[i].vec[2]);
    knots[i].handles[1] = -normalize_v3(knots[i].tan[1]);

#ifndef NDEBUG
    knots[i].co = bezt_array[i].vec[1];
    BLI_assert(equals_v3v3(knots[i].co, points[knots[i].point_index]));
#endif
  }

  if (is_cyclic) {
    knots[0].prev = &knots[bezt_array_last];
    knots[bezt_array_last].next = &knots[0];
  }
  else {
    knots[0].prev = NULL;
    knots[bezt_array_last].next = NULL;

    /* always keep end-points */
    knots[0].can_remove = false;
    knots[bezt_array_last].can_remove = false;
  }

  curve_decimate(points, points_len, knots, knots_len, error_sq_max, error_target_len);

  MEM_freeN(points);

  uint knots_len_decimated = knots_len;

  /* Update handle type on modifications. */
#define HANDLE_UPDATE(a, b) \
  { \
    if (a == HD_VECT) { \
      a = HD_FREE; \
    } \
    else if (ELEM(a, HD_AUTO, HD_AUTO_ANIM)) { \
      a = HD_ALIGN; \
    } \
    /* opposite handle */ \
    if (ELEM(b, HD_AUTO, HD_AUTO_ANIM)) { \
      b = HD_ALIGN; \
    } \
  } \
  ((void)0)

  for (uint i = 0; i < bezt_array_len; i++) {
    if (knots[i].is_removed) {
      /* caller must remove */
      bezt_array[i].f2 |= flag_set;
      knots_len_decimated--;
    }
    else {
      bezt_array[i].f2 &= (char)~flag_set;
      if (is_cyclic || i != 0) {
        uint i_prev = (i != 0) ? i - 1 : bezt_array_last;
        if (knots[i_prev].is_removed) {
          madd_v3_v3v3fl(
              bezt_array[i].vec[0], bezt_array[i].vec[1], knots[i].tan[0], knots[i].handles[0]);
          HANDLE_UPDATE(bezt_array[i].h1, bezt_array[i].h2);
        }
      }
      if (is_cyclic || i != bezt_array_last) {
        uint i_next = (i != bezt_array_last) ? i + 1 : 0;
        if (knots[i_next].is_removed) {
          madd_v3_v3v3fl(
              bezt_array[i].vec[2], bezt_array[i].vec[1], knots[i].tan[1], knots[i].handles[1]);
          HANDLE_UPDATE(bezt_array[i].h2, bezt_array[i].h1);
        }
      }
    }
  }

#undef HANDLE_UPDATE

  MEM_freeN(knots);

  return knots_len_decimated;
}
#define SELECT 1

void BKE_curve_decimate_nurb(Nurb *nu,
                             const uint resolu,
                             const float error_sq_max,
                             const uint error_target_len)
{
  const char flag_test = BEZT_FLAG_TEMP_TAG;

  const uint pntsu_dst = BKE_curve_decimate_bezt_array(nu->bezt,
                                                       (uint)nu->pntsu,
                                                       resolu,
                                                       (nu->flagu & CU_NURB_CYCLIC) != 0,
                                                       SELECT,
                                                       flag_test,
                                                       error_sq_max,
                                                       error_target_len);

  if (pntsu_dst == (uint)nu->pntsu) {
    return;
  }

  BezTriple *bezt_src = nu->bezt;
  BezTriple *bezt_dst = MEM_mallocN(sizeof(BezTriple) * pntsu_dst, __func__);

  int i_src = 0, i_dst = 0;

  while (i_src < nu->pntsu) {
    if ((bezt_src[i_src].f2 & flag_test) == 0) {
      bezt_dst[i_dst] = bezt_src[i_src];
      i_dst++;
    }
    i_src++;
  }

  MEM_freeN(bezt_src);

  nu->bezt = bezt_dst;
  nu->pntsu = i_dst;
}