add new mask blend mode: 'Merge Subtract'. gives better results when using feather...
[blender.git] / source / blender / blenkernel / intern / mask.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2012 Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Blender Foundation,
22  *                 Sergey Sharybin
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/blenkernel/intern/mask.c
28  *  \ingroup bke
29  */
30
31 #include <stddef.h>
32 #include <string.h>
33
34 #include "MEM_guardedalloc.h"
35
36 #include "BLI_utildefines.h"
37 #include "BLI_path_util.h"
38 #include "BLI_string.h"
39 #include "BLI_listbase.h"
40 #include "BLI_math.h"
41
42 #include "DNA_mask_types.h"
43 #include "DNA_node_types.h"
44 #include "DNA_scene_types.h"
45 #include "DNA_object_types.h"
46 #include "DNA_screen_types.h"
47 #include "DNA_space_types.h"
48 #include "DNA_movieclip_types.h"
49 #include "DNA_tracking_types.h"
50 #include "DNA_sequence_types.h"
51
52 #include "BKE_curve.h"
53 #include "BKE_global.h"
54 #include "BKE_library.h"
55 #include "BKE_main.h"
56 #include "BKE_mask.h"
57 #include "BKE_node.h"
58 #include "BKE_sequencer.h"
59 #include "BKE_tracking.h"
60 #include "BKE_movieclip.h"
61 #include "BKE_utildefines.h"
62
63 static MaskSplinePoint *mask_spline_point_next(MaskSpline *spline, MaskSplinePoint *points_array, MaskSplinePoint *point)
64 {
65         if (point == &points_array[spline->tot_point - 1]) {
66                 if (spline->flag & MASK_SPLINE_CYCLIC) {
67                         return &points_array[0];
68                 }
69                 else {
70                         return NULL;
71                 }
72         }
73         else {
74                 return point + 1;
75         }
76 }
77
78 static MaskSplinePoint *mask_spline_point_prev(MaskSpline *spline, MaskSplinePoint *points_array, MaskSplinePoint *point)
79 {
80         if (point == points_array) {
81                 if (spline->flag & MASK_SPLINE_CYCLIC) {
82                         return &points_array[spline->tot_point - 1];
83                 }
84                 else {
85                         return NULL;
86                 }
87         }
88         else {
89                 return point - 1;
90         }
91 }
92
93 static BezTriple *mask_spline_point_next_bezt(MaskSpline *spline, MaskSplinePoint *points_array, MaskSplinePoint *point)
94 {
95         if (point == &points_array[spline->tot_point - 1]) {
96                 if (spline->flag & MASK_SPLINE_CYCLIC) {
97                         return &(points_array[0].bezt);
98                 }
99                 else {
100                         return NULL;
101                 }
102         }
103         else {
104                 return &((point + 1))->bezt;
105         }
106 }
107
108 #if 0
109 static BezTriple *mask_spline_point_prev_bezt(MaskSpline *spline, MaskSplinePoint *points_array, MaskSplinePoint *point)
110 {
111         if (point == points_array) {
112                 if (spline->flag & MASK_SPLINE_CYCLIC) {
113                         return &(points_array[0].bezt);
114                 }
115                 else {
116                         return NULL;
117                 }
118         }
119         else {
120                 return &((point - 1))->bezt;
121         }
122 }
123 #endif
124
125 MaskSplinePoint *BKE_mask_spline_point_array(MaskSpline *spline)
126 {
127         return spline->points_deform ? spline->points_deform : spline->points;
128 }
129
130 MaskSplinePoint *BKE_mask_spline_point_array_from_point(MaskSpline *spline, MaskSplinePoint *point_ref)
131 {
132         if ((point_ref >= spline->points) && (point_ref < &spline->points[spline->tot_point])) {
133                 return spline->points;
134         }
135
136         if ((point_ref >= spline->points_deform) && (point_ref < &spline->points_deform[spline->tot_point])) {
137                 return spline->points_deform;
138         }
139
140         BLI_assert(!"wrong array");
141         return NULL;
142 }
143
144 /* mask layers */
145
146 MaskLayer *BKE_mask_layer_new(Mask *mask, const char *name)
147 {
148         MaskLayer *masklay = MEM_callocN(sizeof(MaskLayer), __func__);
149
150         if (name && name[0])
151                 BLI_strncpy(masklay->name, name, sizeof(masklay->name));
152         else
153                 strcpy(masklay->name, "MaskLayer");
154
155         BLI_addtail(&mask->masklayers, masklay);
156
157         BKE_mask_layer_unique_name(mask, masklay);
158
159         mask->masklay_tot++;
160
161         masklay->blend = MASK_BLEND_MERGE_ADD;
162         masklay->alpha = 1.0f;
163
164         return masklay;
165 }
166
167 /* note: may still be hidden, caller needs to check */
168 MaskLayer *BKE_mask_layer_active(Mask *mask)
169 {
170         return BLI_findlink(&mask->masklayers, mask->masklay_act);
171 }
172
173 void BKE_mask_layer_active_set(Mask *mask, MaskLayer *masklay)
174 {
175         mask->masklay_act = BLI_findindex(&mask->masklayers, masklay);
176 }
177
178 void BKE_mask_layer_remove(Mask *mask, MaskLayer *masklay)
179 {
180         BLI_remlink(&mask->masklayers, masklay);
181         BKE_mask_layer_free(masklay);
182
183         mask->masklay_tot--;
184
185         if (mask->masklay_act >= mask->masklay_tot)
186                 mask->masklay_act = mask->masklay_tot - 1;
187 }
188
189 void BKE_mask_layer_unique_name(Mask *mask, MaskLayer *masklay)
190 {
191         BLI_uniquename(&mask->masklayers, masklay, "MaskLayer", '.', offsetof(MaskLayer, name), sizeof(masklay->name));
192 }
193
194 MaskLayer *BKE_mask_layer_copy(MaskLayer *masklay)
195 {
196         MaskLayer *masklay_new;
197         MaskSpline *spline;
198
199         masklay_new = MEM_callocN(sizeof(MaskLayer), "new mask layer");
200
201         BLI_strncpy(masklay_new->name, masklay->name, sizeof(masklay_new->name));
202
203         masklay_new->alpha = masklay->alpha;
204         masklay_new->blend = masklay->blend;
205         masklay_new->blend_flag = masklay->blend_flag;
206         masklay_new->flag = masklay->flag;
207         masklay_new->restrictflag = masklay->restrictflag;
208
209         for (spline = masklay->splines.first; spline; spline = spline->next) {
210                 MaskSpline *spline_new = BKE_mask_spline_copy(spline);
211
212                 BLI_addtail(&masklay_new->splines, spline_new);
213         }
214
215         /* correct animation */
216         if (masklay->splines_shapes.first) {
217                 MaskLayerShape *masklay_shape;
218                 MaskLayerShape *masklay_shape_new;
219
220                 for (masklay_shape = masklay->splines_shapes.first;
221                      masklay_shape;
222                      masklay_shape = masklay_shape->next)
223                 {
224                         masklay_shape_new = MEM_callocN(sizeof(MaskLayerShape), "new mask layer shape");
225
226                         masklay_shape_new->data = MEM_dupallocN(masklay_shape->data);
227                         masklay_shape_new->tot_vert = masklay_shape->tot_vert;
228                         masklay_shape_new->flag = masklay_shape->flag;
229                         masklay_shape_new->frame = masklay_shape->frame;
230
231                         BLI_addtail(&masklay_new->splines_shapes, masklay_shape_new);
232                 }
233         }
234
235         return masklay_new;
236 }
237
238 void BKE_mask_layer_copy_list(ListBase *masklayers_new, ListBase *masklayers)
239 {
240         MaskLayer *layer;
241
242         for (layer = masklayers->first; layer; layer = layer->next) {
243                 MaskLayer *layer_new = BKE_mask_layer_copy(layer);
244
245                 BLI_addtail(masklayers_new, layer_new);
246         }
247 }
248
249 /* splines */
250
251 MaskSpline *BKE_mask_spline_add(MaskLayer *masklay)
252 {
253         MaskSpline *spline;
254
255         spline = MEM_callocN(sizeof(MaskSpline), "new mask spline");
256         BLI_addtail(&masklay->splines, spline);
257
258         /* spline shall have one point at least */
259         spline->points = MEM_callocN(sizeof(MaskSplinePoint), "new mask spline point");
260         spline->tot_point = 1;
261
262         /* cyclic shapes are more usually used */
263         // spline->flag |= MASK_SPLINE_CYCLIC; // disable because its not so nice for drawing. could be done differently
264
265         spline->weight_interp = MASK_SPLINE_INTERP_EASE;
266
267         BKE_mask_parent_init(&spline->parent);
268
269         return spline;
270 }
271
272 unsigned int BKE_mask_spline_resolution(MaskSpline *spline, int width, int height)
273 {
274         float max_segment = 0.01f;
275         unsigned int i, resol = 1;
276
277         if (width != 0 && height != 0) {
278                 if (width >= height)
279                         max_segment = 1.0f / (float) width;
280                 else
281                         max_segment = 1.0f / (float) height;
282         }
283
284         for (i = 0; i < spline->tot_point; i++) {
285                 MaskSplinePoint *point = &spline->points[i];
286                 BezTriple *bezt, *bezt_next;
287                 float a, b, c, len;
288                 unsigned int cur_resol;
289
290                 bezt = &point->bezt;
291                 bezt_next = mask_spline_point_next_bezt(spline, spline->points, point);
292
293                 if (bezt_next == NULL) {
294                         break;
295                 }
296
297                 a = len_v3v3(bezt->vec[1], bezt->vec[2]);
298                 b = len_v3v3(bezt->vec[2], bezt_next->vec[0]);
299                 c = len_v3v3(bezt_next->vec[0], bezt_next->vec[1]);
300
301                 len = a + b + c;
302                 cur_resol = len / max_segment;
303
304                 resol = MAX2(resol, cur_resol);
305
306                 if (resol >= MASK_RESOL_MAX) {
307                         break;
308                 }
309         }
310
311         return CLAMPIS(resol, 1, MASK_RESOL_MAX);
312 }
313
314 unsigned int BKE_mask_spline_feather_resolution(MaskSpline *spline, int width, int height)
315 {
316         const float max_segment = 0.005;
317         unsigned int resol = BKE_mask_spline_resolution(spline, width, height);
318         float max_jump = 0.0f;
319         int i;
320
321         /* avoid checking the featrher if we already hit the maximum value */
322         if (resol >= MASK_RESOL_MAX) {
323                 return MASK_RESOL_MAX;
324         }
325
326         for (i = 0; i < spline->tot_point; i++) {
327                 MaskSplinePoint *point = &spline->points[i];
328                 float prev_u, prev_w;
329                 int j;
330
331                 prev_u = 0.0f;
332                 prev_w = point->bezt.weight;
333
334                 for (j = 0; j < point->tot_uw; j++) {
335                         const float w_diff = (point->uw[j].w - prev_w);
336                         const float u_diff = (point->uw[j].u - prev_u);
337
338                         /* avoid divide by zero and very high values,
339                          * though these get clamped eventually */
340                         if (u_diff > FLT_EPSILON) {
341                                 float jump = fabsf(w_diff / u_diff);
342
343                                 max_jump = MAX2(max_jump, jump);
344                         }
345
346                         prev_u = point->uw[j].u;
347                         prev_w = point->uw[j].w;
348                 }
349         }
350
351         resol += max_jump / max_segment;
352
353         return CLAMPIS(resol, 1, MASK_RESOL_MAX);
354 }
355
356 int BKE_mask_spline_differentiate_calc_total(const MaskSpline *spline, const unsigned int resol)
357 {
358         if (spline->flag & MASK_SPLINE_CYCLIC) {
359                 return spline->tot_point * resol;
360         }
361         else {
362                 return ((spline->tot_point - 1) * resol) + 1;
363         }
364 }
365
366 float (*BKE_mask_spline_differentiate_with_resolution_ex(MaskSpline *spline,
367                                                          int *tot_diff_point,
368                                                          const unsigned int resol
369                                                          ))[2]
370 {
371         MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline);
372
373         MaskSplinePoint *point, *prev;
374         float (*diff_points)[2], (*fp)[2];
375         const int tot = BKE_mask_spline_differentiate_calc_total(spline, resol);
376         int a;
377
378         if (spline->tot_point <= 1) {
379                 /* nothing to differentiate */
380                 *tot_diff_point = 0;
381                 return NULL;
382         }
383
384         /* len+1 because of 'forward_diff_bezier' function */
385         *tot_diff_point = tot;
386         diff_points = fp = MEM_mallocN((tot + 1) * sizeof(*diff_points), "mask spline vets");
387
388         a = spline->tot_point - 1;
389         if (spline->flag & MASK_SPLINE_CYCLIC)
390                 a++;
391
392         prev = points_array;
393         point = prev + 1;
394
395         while (a--) {
396                 BezTriple *prevbezt;
397                 BezTriple *bezt;
398                 int j;
399
400                 if (a == 0 && (spline->flag & MASK_SPLINE_CYCLIC))
401                         point = points_array;
402
403                 prevbezt = &prev->bezt;
404                 bezt = &point->bezt;
405
406                 for (j = 0; j < 2; j++) {
407                         BKE_curve_forward_diff_bezier(prevbezt->vec[1][j], prevbezt->vec[2][j],
408                                                       bezt->vec[0][j], bezt->vec[1][j],
409                                                       &(*fp)[j], resol, 2 * sizeof(float));
410                 }
411
412                 fp += resol;
413
414                 if (a == 0 && (spline->flag & MASK_SPLINE_CYCLIC) == 0) {
415                         copy_v2_v2(*fp, bezt->vec[1]);
416                 }
417
418                 prev = point;
419                 point++;
420         }
421
422         return diff_points;
423 }
424
425 float (*BKE_mask_spline_differentiate_with_resolution(MaskSpline *spline, int width, int height,
426                                                       int *tot_diff_point
427                                                       ))[2]
428 {
429         int unsigned resol = BKE_mask_spline_resolution(spline, width, height);
430
431         return BKE_mask_spline_differentiate_with_resolution_ex(spline, tot_diff_point, resol);
432 }
433
434 float (*BKE_mask_spline_differentiate(MaskSpline *spline, int *tot_diff_point))[2]
435 {
436         return BKE_mask_spline_differentiate_with_resolution(spline, 0, 0, tot_diff_point);
437 }
438
439 /* ** feather points self-intersection collapse routine ** */
440
441 typedef struct FeatherEdgesBucket {
442         int tot_segment;
443         int (*segments)[2];
444         int alloc_segment;
445 } FeatherEdgesBucket;
446
447 static void feather_bucket_add_edge(FeatherEdgesBucket *bucket, int start, int end)
448 {
449         const int alloc_delta = 256;
450
451         if (bucket->tot_segment >= bucket->alloc_segment) {
452                 if (!bucket->segments) {
453                         bucket->segments = MEM_callocN(alloc_delta * sizeof(*bucket->segments), "feather bucket segments");
454                 }
455                 else {
456                         bucket->segments = MEM_reallocN(bucket->segments,
457                                         (alloc_delta + bucket->tot_segment) * sizeof(*bucket->segments));
458                 }
459
460                 bucket->alloc_segment += alloc_delta;
461         }
462
463         bucket->segments[bucket->tot_segment][0] = start;
464         bucket->segments[bucket->tot_segment][1] = end;
465
466         bucket->tot_segment++;
467 }
468
469 static void feather_bucket_check_intersect(float (*feather_points)[2], int tot_feather_point, FeatherEdgesBucket *bucket,
470                                            int cur_a, int cur_b)
471 {
472         int i;
473
474         float *v1 = (float *) feather_points[cur_a];
475         float *v2 = (float *) feather_points[cur_b];
476
477         for (i = 0; i < bucket->tot_segment; i++) {
478                 int check_a = bucket->segments[i][0];
479                 int check_b = bucket->segments[i][1];
480
481                 float *v3 = (float *) feather_points[check_a];
482                 float *v4 = (float *) feather_points[check_b];
483
484                 if (check_a >= cur_a - 1 || cur_b == check_a)
485                         continue;
486
487                 if (isect_seg_seg_v2(v1, v2, v3, v4)) {
488                         int k;
489                         float p[2];
490                         float min_a[2], max_a[2];
491                         float min_b[2], max_b[2];
492
493                         isect_seg_seg_v2_point(v1, v2, v3, v4, p);
494
495                         INIT_MINMAX2(min_a, max_a);
496                         INIT_MINMAX2(min_b, max_b);
497
498                         /* collapse loop with smaller AABB */
499                         for (k = 0; k < tot_feather_point; k++) {
500                                 if (k >= check_b && k <= cur_a) {
501                                         DO_MINMAX2(feather_points[k], min_a, max_a);
502                                 }
503                                 else {
504                                         DO_MINMAX2(feather_points[k], min_b, max_b);
505                                 }
506                         }
507
508                         if (max_a[0] - min_a[0] < max_b[0] - min_b[0] ||
509                             max_a[1] - min_a[1] < max_b[1] - min_b[1])
510                         {
511                                 for (k = check_b; k <= cur_a; k++) {
512                                         copy_v2_v2(feather_points[k], p);
513                                 }
514                         }
515                         else {
516                                 for (k = 0; k <= check_a; k++) {
517                                         copy_v2_v2(feather_points[k], p);
518                                 }
519
520                                 if (cur_b != 0) {
521                                         for (k = cur_b; k < tot_feather_point; k++) {
522                                                 copy_v2_v2(feather_points[k], p);
523                                         }
524                                 }
525                         }
526                 }
527         }
528 }
529
530 static int feather_bucket_index_from_coord(float co[2], const float min[2], const float bucket_scale[2],
531                                            const int buckets_per_side)
532 {
533         int x = (int) ((co[0] - min[0]) * bucket_scale[0]);
534         int y = (int) ((co[1] - min[1]) * bucket_scale[1]);
535
536         if (x == buckets_per_side)
537                 x--;
538
539         if (y == buckets_per_side)
540                 y--;
541
542         return y * buckets_per_side + x;
543 }
544
545 static void feather_bucket_get_diagonal(FeatherEdgesBucket *buckets, int start_bucket_index, int end_bucket_index,
546                                         int buckets_per_side, FeatherEdgesBucket **diagonal_bucket_a_r,
547                                         FeatherEdgesBucket **diagonal_bucket_b_r)
548 {
549         int start_bucket_x = start_bucket_index % buckets_per_side;
550         int start_bucket_y = start_bucket_index / buckets_per_side;
551
552         int end_bucket_x = end_bucket_index % buckets_per_side;
553         int end_bucket_y = end_bucket_index / buckets_per_side;
554
555         int diagonal_bucket_a_index = start_bucket_y * buckets_per_side + end_bucket_x;
556         int diagonal_bucket_b_index = end_bucket_y * buckets_per_side + start_bucket_x;
557
558         *diagonal_bucket_a_r = &buckets[diagonal_bucket_a_index];
559         *diagonal_bucket_b_r = &buckets[diagonal_bucket_b_index];
560 }
561
562 void BKE_mask_spline_feather_collapse_inner_loops(MaskSpline *spline, float (*feather_points)[2], const int tot_feather_point)
563 {
564 #define BUCKET_INDEX(co) \
565         feather_bucket_index_from_coord(co, min, bucket_scale, buckets_per_side)
566
567         int buckets_per_side, tot_bucket;
568         float bucket_size, bucket_scale[2];
569
570         FeatherEdgesBucket *buckets;
571
572         int i;
573         float min[2], max[2];
574         float max_delta_x = -1.0f, max_delta_y = -1.0f, max_delta;
575
576         if (tot_feather_point < 4) {
577                 /* self-intersection works only for quads at least,
578                  * in other cases polygon can't be self-intersecting anyway
579                  */
580
581                 return;
582         }
583
584         /* find min/max corners of mask to build buckets in that space */
585         INIT_MINMAX2(min, max);
586
587         for (i = 0; i < tot_feather_point; i++) {
588                 int next = i + 1;
589                 float delta;
590
591                 DO_MINMAX2(feather_points[i], min, max);
592
593                 if (next == tot_feather_point) {
594                         if (spline->flag & MASK_SPLINE_CYCLIC)
595                                 next = 0;
596                         else
597                                 break;
598                 }
599
600                 delta = fabsf(feather_points[i][0] - feather_points[next][0]);
601                 if (delta > max_delta_x)
602                         max_delta_x = delta;
603
604                 delta = fabsf(feather_points[i][1] - feather_points[next][1]);
605                 if (delta > max_delta_y)
606                         max_delta_y = delta;
607         }
608
609         /* prevent divisionsby zero by ensuring bounding box is not collapsed */
610         if (max[0] - min[0] < FLT_EPSILON) {
611                 max[0] += 0.01f;
612                 min[0] -= 0.01f;
613         }
614
615         if (max[1] - min[1] < FLT_EPSILON) {
616                 max[1] += 0.01f;
617                 min[1] -= 0.01f;
618         }
619
620         /* use dynamically calculated buckets per side, so we likely wouldn't
621          * run into a situation when segment doesn't fit two buckets which is
622          * pain collecting candidates for intersection
623          */
624
625         max_delta_x /= max[0] - min[0];
626         max_delta_y /= max[1] - min[1];
627
628         max_delta = MAX2(max_delta_x, max_delta_y);
629
630         buckets_per_side = MIN2(512, 0.9f / max_delta);
631
632         if (buckets_per_side == 0) {
633                 /* happens when some segment fills the whole bounding box across some of dimension */
634
635                 buckets_per_side = 1;
636         }
637
638         tot_bucket = buckets_per_side * buckets_per_side;
639         bucket_size = 1.0f / buckets_per_side;
640
641         /* pre-compute multipliers, to save mathematical operations in loops */
642         bucket_scale[0] = 1.0f / ((max[0] - min[0]) * bucket_size);
643         bucket_scale[1] = 1.0f / ((max[1] - min[1]) * bucket_size);
644
645         /* fill in buckets' edges */
646         buckets = MEM_callocN(sizeof(FeatherEdgesBucket) * tot_bucket, "feather buckets");
647
648         for (i = 0; i < tot_feather_point; i++) {
649                 int start = i, end = i + 1;
650                 int start_bucket_index, end_bucket_index;
651
652                 if (end == tot_feather_point) {
653                         if (spline->flag & MASK_SPLINE_CYCLIC)
654                                 end = 0;
655                         else
656                                 break;
657                 }
658
659                 start_bucket_index = BUCKET_INDEX(feather_points[start]);
660                 end_bucket_index = BUCKET_INDEX(feather_points[end]);
661
662                 feather_bucket_add_edge(&buckets[start_bucket_index], start, end);
663
664                 if (start_bucket_index != end_bucket_index) {
665                         FeatherEdgesBucket *end_bucket = &buckets[end_bucket_index];
666                         FeatherEdgesBucket *diagonal_bucket_a, *diagonal_bucket_b;
667
668                         feather_bucket_get_diagonal(buckets, start_bucket_index, end_bucket_index, buckets_per_side,
669                                                     &diagonal_bucket_a, &diagonal_bucket_b);
670
671                         feather_bucket_add_edge(end_bucket, start, end);
672                         feather_bucket_add_edge(diagonal_bucket_a, start, end);
673                         feather_bucket_add_edge(diagonal_bucket_a, start, end);
674                 }
675         }
676
677         /* check all edges for intersection with edges from their buckets */
678         for (i = 0; i < tot_feather_point; i++) {
679                 int cur_a = i, cur_b = i + 1;
680                 int start_bucket_index, end_bucket_index;
681
682                 FeatherEdgesBucket *start_bucket;
683
684                 if (cur_b == tot_feather_point)
685                         cur_b = 0;
686
687                 start_bucket_index = BUCKET_INDEX(feather_points[cur_a]);
688                 end_bucket_index = BUCKET_INDEX(feather_points[cur_b]);
689
690                 start_bucket = &buckets[start_bucket_index];
691
692                 feather_bucket_check_intersect(feather_points, tot_feather_point, start_bucket, cur_a, cur_b);
693
694                 if (start_bucket_index != end_bucket_index) {
695                         FeatherEdgesBucket *end_bucket = &buckets[end_bucket_index];
696                         FeatherEdgesBucket *diagonal_bucket_a, *diagonal_bucket_b;
697
698                         feather_bucket_get_diagonal(buckets, start_bucket_index, end_bucket_index, buckets_per_side,
699                                                     &diagonal_bucket_a, &diagonal_bucket_b);
700
701                         feather_bucket_check_intersect(feather_points, tot_feather_point, end_bucket, cur_a, cur_b);
702                         feather_bucket_check_intersect(feather_points, tot_feather_point, diagonal_bucket_a, cur_a, cur_b);
703                         feather_bucket_check_intersect(feather_points, tot_feather_point, diagonal_bucket_b, cur_a, cur_b);
704                 }
705         }
706
707         /* free buckets */
708         for (i = 0; i < tot_bucket; i++) {
709                 if (buckets[i].segments)
710                         MEM_freeN(buckets[i].segments);
711         }
712
713         MEM_freeN(buckets);
714
715 #undef BUCKET_INDEX
716 }
717
718 /**
719  * values align with #BKE_mask_spline_differentiate_with_resolution_ex
720  * when \a resol arguments match.
721  */
722 float (*BKE_mask_spline_feather_differentiated_points_with_resolution_ex(MaskSpline *spline,
723                                                                          int *tot_feather_point,
724                                                                          const unsigned int resol,
725                                                                          const int do_feather_isect
726                                                                          ))[2]
727 {
728         MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline);
729         MaskSplinePoint *point, *prev;
730         float (*feather)[2], (*fp)[2];
731
732         const int tot = BKE_mask_spline_differentiate_calc_total(spline, resol);
733         int a;
734
735         /* tot+1 because of 'forward_diff_bezier' function */
736         feather = fp = MEM_mallocN((tot + 1) * sizeof(*feather), "mask spline feather diff points");
737
738         a = spline->tot_point - 1;
739         if (spline->flag & MASK_SPLINE_CYCLIC)
740                 a++;
741
742         prev = points_array;
743         point = prev + 1;
744
745         while (a--) {
746                 /* BezTriple *prevbezt; */  /* UNUSED */
747                 /* BezTriple *bezt; */      /* UNUSED */
748                 int j;
749
750                 if (a == 0 && (spline->flag & MASK_SPLINE_CYCLIC))
751                         point = points_array;
752
753
754                 /* prevbezt = &prev->bezt; */
755                 /* bezt = &point->bezt; */
756
757                 for (j = 0; j < resol; j++, fp++) {
758                         float u = (float) j / resol, weight;
759                         float co[2], n[2];
760
761                         /* TODO - these calls all calculate similar things
762                          * could be unified for some speed */
763                         BKE_mask_point_segment_co(spline, prev, u, co);
764                         BKE_mask_point_normal(spline, prev, u, n);
765                         weight = BKE_mask_point_weight(spline, prev, u);
766
767                         madd_v2_v2v2fl(*fp, co, n, weight);
768                 }
769
770                 if (a == 0 && (spline->flag & MASK_SPLINE_CYCLIC) == 0) {
771                         float u = 1.0f, weight;
772                         float co[2], n[2];
773
774                         BKE_mask_point_segment_co(spline, prev, u, co);
775                         BKE_mask_point_normal(spline, prev, u, n);
776                         weight = BKE_mask_point_weight(spline, prev, u);
777
778                         madd_v2_v2v2fl(*fp, co, n, weight);
779                 }
780
781                 prev = point;
782                 point++;
783         }
784
785         *tot_feather_point = tot;
786
787         if ((spline->flag & MASK_SPLINE_NOINTERSECT) && do_feather_isect) {
788                 BKE_mask_spline_feather_collapse_inner_loops(spline, feather, tot);
789         }
790
791         return feather;
792 }
793
794 float (*BKE_mask_spline_feather_differentiated_points_with_resolution(MaskSpline *spline, int width, int height,
795                                                                       int *tot_feather_point, const int do_feather_isect))[2]
796 {
797         unsigned int resol = BKE_mask_spline_feather_resolution(spline, width, height);
798
799         return BKE_mask_spline_feather_differentiated_points_with_resolution_ex(spline, tot_feather_point, resol, do_feather_isect);
800 }
801
802 float (*BKE_mask_spline_feather_differentiated_points(MaskSpline *spline, int *tot_feather_point))[2]
803 {
804         return BKE_mask_spline_feather_differentiated_points_with_resolution(spline, 0, 0, tot_feather_point, TRUE);
805 }
806
807 float (*BKE_mask_spline_feather_points(MaskSpline *spline, int *tot_feather_point))[2]
808 {
809         MaskSplinePoint *points_array = BKE_mask_spline_point_array(spline);
810
811         int i, tot = 0;
812         float (*feather)[2], (*fp)[2];
813
814         /* count */
815         for (i = 0; i < spline->tot_point; i++) {
816                 MaskSplinePoint *point = &points_array[i];
817
818                 tot += point->tot_uw + 1;
819         }
820
821         /* create data */
822         feather = fp = MEM_mallocN(tot * sizeof(*feather), "mask spline feather points");
823
824         for (i = 0; i < spline->tot_point; i++) {
825                 MaskSplinePoint *point = &points_array[i];
826                 BezTriple *bezt = &point->bezt;
827                 float weight, n[2];
828                 int j;
829
830                 BKE_mask_point_normal(spline, point, 0.0f, n);
831                 weight = BKE_mask_point_weight(spline, point, 0.0f);
832
833                 madd_v2_v2v2fl(*fp, bezt->vec[1], n, weight);
834                 fp++;
835
836                 for (j = 0; j < point->tot_uw; j++) {
837                         float u = point->uw[j].u;
838                         float co[2];
839
840                         BKE_mask_point_segment_co(spline, point, u, co);
841                         BKE_mask_point_normal(spline, point, u, n);
842                         weight = BKE_mask_point_weight(spline, point, u);
843
844                         madd_v2_v2v2fl(*fp, co, n, weight);
845                         fp++;
846                 }
847         }
848
849         *tot_feather_point = tot;
850
851         return feather;
852 }
853
854 void BKE_mask_point_direction_switch(MaskSplinePoint *point)
855 {
856         const int tot_uw = point->tot_uw;
857         const int tot_uw_half = tot_uw / 2;
858         int i;
859
860         float co_tmp[2];
861
862         /* swap handles */
863         copy_v2_v2(co_tmp, point->bezt.vec[0]);
864         copy_v2_v2(point->bezt.vec[0], point->bezt.vec[2]);
865         copy_v2_v2(point->bezt.vec[2], co_tmp);
866         /* in this case the flags are unlikely to be different but swap anyway */
867         SWAP(char, point->bezt.f1, point->bezt.f3);
868         SWAP(char, point->bezt.h1, point->bezt.h2);
869
870
871         /* swap UW's */
872         if (tot_uw > 1) {
873                 /* count */
874                 for (i = 0; i < tot_uw_half; i++) {
875                         MaskSplinePointUW *uw_a = &point->uw[i];
876                         MaskSplinePointUW *uw_b = &point->uw[tot_uw - (i + 1)];
877                         SWAP(MaskSplinePointUW, *uw_a, *uw_b);
878                 }
879         }
880
881         for (i = 0; i < tot_uw; i++) {
882                 MaskSplinePointUW *uw = &point->uw[i];
883                 uw->u = 1.0f - uw->u;
884         }
885 }
886
887 void BKE_mask_spline_direction_switch(MaskLayer *masklay, MaskSpline *spline)
888 {
889         const int tot_point = spline->tot_point;
890         const int tot_point_half = tot_point / 2;
891         int i, i_prev;
892
893         if (tot_point < 2) {
894                 return;
895         }
896
897         /* count */
898         for (i = 0; i < tot_point_half; i++) {
899                 MaskSplinePoint *point_a = &spline->points[i];
900                 MaskSplinePoint *point_b = &spline->points[tot_point - (i + 1)];
901                 SWAP(MaskSplinePoint, *point_a, *point_b);
902         }
903
904         /* correct UW's */
905         i_prev = tot_point - 1;
906         for (i = 0; i < tot_point; i++) {
907
908                 BKE_mask_point_direction_switch(&spline->points[i]);
909
910                 SWAP(MaskSplinePointUW *, spline->points[i].uw,     spline->points[i_prev].uw);
911                 SWAP(int,                 spline->points[i].tot_uw, spline->points[i_prev].tot_uw);
912
913                 i_prev = i;
914         }
915
916         /* correct animation */
917         if (masklay->splines_shapes.first) {
918                 MaskLayerShape *masklay_shape;
919
920                 const int spline_index = BKE_mask_layer_shape_spline_to_index(masklay, spline);
921
922                 for (masklay_shape = masklay->splines_shapes.first;
923                      masklay_shape;
924                      masklay_shape = masklay_shape->next)
925                 {
926                         MaskLayerShapeElem *fp_arr = (MaskLayerShapeElem *)masklay_shape->data;
927
928                         for (i = 0; i < tot_point_half; i++) {
929                                 MaskLayerShapeElem *fp_a = &fp_arr[spline_index +              (i)     ];
930                                 MaskLayerShapeElem *fp_b = &fp_arr[spline_index + (tot_point - (i + 1))];
931                                 SWAP(MaskLayerShapeElem, *fp_a, *fp_b);
932                         }
933                 }
934         }
935 }
936
937
938 float BKE_mask_spline_project_co(MaskSpline *spline, MaskSplinePoint *point,
939                                  float start_u, const float co[2], const eMaskSign sign)
940 {
941         const float proj_eps         = 1e-3;
942         const float proj_eps_squared = proj_eps * proj_eps;
943         const int N = 1000;
944         float u = -1.0f, du = 1.0f / N, u1 = start_u, u2 = start_u;
945         float ang = -1.0f;
946
947         BLI_assert(ABS(sign) <= 1); /* (-1, 0, 1) */
948
949         while (u1 > 0.0f || u2 < 1.0f) {
950                 float n1[2], n2[2], co1[2], co2[2];
951                 float v1[2], v2[2];
952                 float ang1, ang2;
953
954                 if (u1 >= 0.0f) {
955                         BKE_mask_point_segment_co(spline, point, u1, co1);
956                         BKE_mask_point_normal(spline, point, u1, n1);
957                         sub_v2_v2v2(v1, co, co1);
958
959                         if ((sign == MASK_PROJ_ANY) ||
960                             ((sign == MASK_PROJ_NEG) && (dot_v2v2(v1, n1) <= 0.0f)) ||
961                             ((sign == MASK_PROJ_POS) && (dot_v2v2(v1, n1) >= 0.0f)))
962                         {
963
964                                 if (len_squared_v2(v1) > proj_eps_squared) {
965                                         ang1 = angle_v2v2(v1, n1);
966                                         if (ang1 > M_PI / 2.0f)
967                                                 ang1 = M_PI  - ang1;
968
969                                         if (ang < 0.0f || ang1 < ang) {
970                                                 ang = ang1;
971                                                 u = u1;
972                                         }
973                                 }
974                                 else {
975                                         u = u1;
976                                         break;
977                                 }
978                         }
979                 }
980
981                 if (u2 <= 1.0f) {
982                         BKE_mask_point_segment_co(spline, point, u2, co2);
983                         BKE_mask_point_normal(spline, point, u2, n2);
984                         sub_v2_v2v2(v2, co, co2);
985
986                         if ((sign == MASK_PROJ_ANY) ||
987                             ((sign == MASK_PROJ_NEG) && (dot_v2v2(v2, n2) <= 0.0f)) ||
988                             ((sign == MASK_PROJ_POS) && (dot_v2v2(v2, n2) >= 0.0f)))
989                         {
990
991                                 if (len_squared_v2(v2) > proj_eps_squared) {
992                                         ang2 = angle_v2v2(v2, n2);
993                                         if (ang2 > M_PI / 2.0f)
994                                                 ang2 = M_PI  - ang2;
995
996                                         if (ang2 < ang) {
997                                                 ang = ang2;
998                                                 u = u2;
999                                         }
1000                                 }
1001                                 else {
1002                                         u = u2;
1003                                         break;
1004                                 }
1005                         }
1006                 }
1007
1008                 u1 -= du;
1009                 u2 += du;
1010         }
1011
1012         return u;
1013 }
1014
1015 /* point */
1016
1017 int BKE_mask_point_has_handle(MaskSplinePoint *point)
1018 {
1019         BezTriple *bezt = &point->bezt;
1020
1021         return bezt->h1 == HD_ALIGN;
1022 }
1023
1024 void BKE_mask_point_handle(MaskSplinePoint *point, float handle[2])
1025 {
1026         float vec[2];
1027
1028         sub_v2_v2v2(vec, point->bezt.vec[0], point->bezt.vec[1]);
1029
1030         handle[0] = (point->bezt.vec[1][0] + vec[1]);
1031         handle[1] = (point->bezt.vec[1][1] - vec[0]);
1032 }
1033
1034 void BKE_mask_point_set_handle(MaskSplinePoint *point, float loc[2], int keep_direction,
1035                                float orig_handle[2], float orig_vec[3][3])
1036 {
1037         BezTriple *bezt = &point->bezt;
1038         float v1[2], v2[2], vec[2];
1039
1040         if (keep_direction) {
1041                 sub_v2_v2v2(v1, loc, orig_vec[1]);
1042                 sub_v2_v2v2(v2, orig_handle, orig_vec[1]);
1043
1044                 project_v2_v2v2(vec, v1, v2);
1045
1046                 if (dot_v2v2(v2, vec) > 0) {
1047                         float len = len_v2(vec);
1048
1049                         sub_v2_v2v2(v1, orig_vec[0], orig_vec[1]);
1050
1051                         mul_v2_fl(v1, len / len_v2(v1));
1052
1053                         add_v2_v2v2(bezt->vec[0], bezt->vec[1], v1);
1054                         sub_v2_v2v2(bezt->vec[2], bezt->vec[1], v1);
1055                 }
1056                 else {
1057                         copy_v3_v3(bezt->vec[0], bezt->vec[1]);
1058                         copy_v3_v3(bezt->vec[2], bezt->vec[1]);
1059                 }
1060         }
1061         else {
1062                 sub_v2_v2v2(v1, loc, bezt->vec[1]);
1063
1064                 v2[0] = -v1[1];
1065                 v2[1] =  v1[0];
1066
1067                 add_v2_v2v2(bezt->vec[0], bezt->vec[1], v2);
1068                 sub_v2_v2v2(bezt->vec[2], bezt->vec[1], v2);
1069         }
1070 }
1071
1072 float *BKE_mask_point_segment_feather_diff_with_resolution(MaskSpline *spline, MaskSplinePoint *point,
1073                                                            int width, int height,
1074                                                            unsigned int *tot_feather_point)
1075 {
1076         float *feather, *fp;
1077         unsigned int resol = BKE_mask_spline_feather_resolution(spline, width, height);
1078         unsigned int i;
1079
1080         feather = fp = MEM_callocN(2 * resol * sizeof(float), "mask point spline feather diff points");
1081
1082         for (i = 0; i < resol; i++, fp += 2) {
1083                 float u = (float)(i % resol) / resol, weight;
1084                 float co[2], n[2];
1085
1086                 BKE_mask_point_segment_co(spline, point, u, co);
1087                 BKE_mask_point_normal(spline, point, u, n);
1088                 weight = BKE_mask_point_weight(spline, point, u);
1089
1090                 fp[0] = co[0] + n[0] * weight;
1091                 fp[1] = co[1] + n[1] * weight;
1092         }
1093
1094         *tot_feather_point = resol;
1095
1096         return feather;
1097 }
1098
1099 float *BKE_mask_point_segment_feather_diff(MaskSpline *spline, MaskSplinePoint *point, unsigned int *tot_feather_point)
1100 {
1101         return BKE_mask_point_segment_feather_diff_with_resolution(spline, point, 0, 0, tot_feather_point);
1102 }
1103
1104 float *BKE_mask_point_segment_diff_with_resolution(MaskSpline *spline, MaskSplinePoint *point,
1105                                                    int width, int height, unsigned int *tot_diff_point)
1106 {
1107         MaskSplinePoint *points_array = BKE_mask_spline_point_array_from_point(spline, point);
1108
1109         BezTriple *bezt, *bezt_next;
1110         float *diff_points, *fp;
1111         int j, resol = BKE_mask_spline_resolution(spline, width, height);
1112
1113         bezt = &point->bezt;
1114         bezt_next = mask_spline_point_next_bezt(spline, points_array, point);
1115
1116         if (!bezt_next)
1117                 return NULL;
1118
1119         /* resol+1 because of 'forward_diff_bezier' function */
1120         *tot_diff_point = resol + 1;
1121         diff_points = fp = MEM_callocN((resol + 1) * 2 * sizeof(float), "mask segment vets");
1122
1123         for (j = 0; j < 2; j++) {
1124                 BKE_curve_forward_diff_bezier(bezt->vec[1][j], bezt->vec[2][j],
1125                                               bezt_next->vec[0][j], bezt_next->vec[1][j],
1126                                               fp + j, resol, 2 * sizeof(float));
1127         }
1128
1129         copy_v2_v2(fp + 2 * resol, bezt_next->vec[1]);
1130
1131         return diff_points;
1132 }
1133
1134 float *BKE_mask_point_segment_diff(MaskSpline *spline, MaskSplinePoint *point, unsigned int *tot_diff_point)
1135 {
1136         return BKE_mask_point_segment_diff_with_resolution(spline, point, 0, 0, tot_diff_point);
1137 }
1138
1139 void BKE_mask_point_segment_co(MaskSpline *spline, MaskSplinePoint *point, float u, float co[2])
1140 {
1141         MaskSplinePoint *points_array = BKE_mask_spline_point_array_from_point(spline, point);
1142
1143         BezTriple *bezt = &point->bezt, *bezt_next;
1144         float q0[2], q1[2], q2[2], r0[2], r1[2];
1145
1146         bezt_next = mask_spline_point_next_bezt(spline, points_array, point);
1147
1148         if (!bezt_next) {
1149                 copy_v2_v2(co, bezt->vec[1]);
1150                 return;
1151         }
1152
1153         interp_v2_v2v2(q0, bezt->vec[1], bezt->vec[2], u);
1154         interp_v2_v2v2(q1, bezt->vec[2], bezt_next->vec[0], u);
1155         interp_v2_v2v2(q2, bezt_next->vec[0], bezt_next->vec[1], u);
1156
1157         interp_v2_v2v2(r0, q0, q1, u);
1158         interp_v2_v2v2(r1, q1, q2, u);
1159
1160         interp_v2_v2v2(co, r0, r1, u);
1161 }
1162
1163 void BKE_mask_point_normal(MaskSpline *spline, MaskSplinePoint *point, float u, float n[2])
1164 {
1165         MaskSplinePoint *points_array = BKE_mask_spline_point_array_from_point(spline, point);
1166
1167         BezTriple *bezt = &point->bezt, *bezt_next;
1168         float q0[2], q1[2], q2[2], r0[2], r1[2], vec[2];
1169
1170         bezt_next = mask_spline_point_next_bezt(spline, points_array, point);
1171
1172         if (!bezt_next) {
1173                 BKE_mask_point_handle(point, vec);
1174
1175                 sub_v2_v2v2(n, vec, bezt->vec[1]);
1176                 normalize_v2(n);
1177                 return;
1178         }
1179
1180         interp_v2_v2v2(q0, bezt->vec[1], bezt->vec[2], u);
1181         interp_v2_v2v2(q1, bezt->vec[2], bezt_next->vec[0], u);
1182         interp_v2_v2v2(q2, bezt_next->vec[0], bezt_next->vec[1], u);
1183
1184         interp_v2_v2v2(r0, q0, q1, u);
1185         interp_v2_v2v2(r1, q1, q2, u);
1186
1187         sub_v2_v2v2(vec, r1, r0);
1188
1189         n[0] = -vec[1];
1190         n[1] =  vec[0];
1191
1192         normalize_v2(n);
1193 }
1194
1195 static float mask_point_interp_weight(BezTriple *bezt, BezTriple *bezt_next, const float u)
1196 {
1197         return (bezt->weight * (1.0f - u)) + (bezt_next->weight * u);
1198 }
1199
1200 float BKE_mask_point_weight_scalar(MaskSpline *spline, MaskSplinePoint *point, const float u)
1201 {
1202         MaskSplinePoint *points_array = BKE_mask_spline_point_array_from_point(spline, point);
1203         BezTriple *bezt = &point->bezt, *bezt_next;
1204
1205         bezt_next = mask_spline_point_next_bezt(spline, points_array, point);
1206
1207         if (!bezt_next) {
1208                 return bezt->weight;
1209         }
1210         else if (u <= 0.0) {
1211                 return bezt->weight;
1212         }
1213         else if (u >= 1.0f) {
1214                 return bezt_next->weight;
1215         }
1216         else {
1217                 return mask_point_interp_weight(bezt, bezt_next, u);
1218         }
1219 }
1220
1221 float BKE_mask_point_weight(MaskSpline *spline, MaskSplinePoint *point, const float u)
1222 {
1223         MaskSplinePoint *points_array = BKE_mask_spline_point_array_from_point(spline, point);
1224         BezTriple *bezt = &point->bezt, *bezt_next;
1225
1226         bezt_next = mask_spline_point_next_bezt(spline, points_array, point);
1227
1228         if (!bezt_next) {
1229                 return bezt->weight;
1230         }
1231         else if (u <= 0.0) {
1232                 return bezt->weight;
1233         }
1234         else if (u >= 1.0f) {
1235                 return bezt_next->weight;
1236         }
1237         else {
1238                 float cur_u = 0.0f, cur_w = 0.0f, next_u = 0.0f, next_w = 0.0f, fac; /* Quite warnings */
1239                 int i;
1240
1241                 for (i = 0; i < point->tot_uw + 1; i++) {
1242
1243                         if (i == 0) {
1244                                 cur_u = 0.0f;
1245                                 cur_w = 1.0f; /* mask_point_interp_weight will scale it */
1246                         }
1247                         else {
1248                                 cur_u = point->uw[i - 1].u;
1249                                 cur_w = point->uw[i - 1].w;
1250                         }
1251
1252                         if (i == point->tot_uw) {
1253                                 next_u = 1.0f;
1254                                 next_w = 1.0f; /* mask_point_interp_weight will scale it */
1255                         }
1256                         else {
1257                                 next_u = point->uw[i].u;
1258                                 next_w = point->uw[i].w;
1259                         }
1260
1261                         if (u >= cur_u && u <= next_u) {
1262                                 break;
1263                         }
1264                 }
1265
1266                 fac = (u - cur_u) / (next_u - cur_u);
1267
1268                 cur_w  *= mask_point_interp_weight(bezt, bezt_next, cur_u);
1269                 next_w *= mask_point_interp_weight(bezt, bezt_next, next_u);
1270
1271                 if (spline->weight_interp == MASK_SPLINE_INTERP_EASE) {
1272                         return cur_w + (next_w - cur_w) * (3.0f * fac * fac - 2.0f * fac * fac * fac);
1273                 }
1274                 else {
1275                         return (1.0f - fac) * cur_w + fac * next_w;
1276                 }
1277         }
1278 }
1279
1280 MaskSplinePointUW *BKE_mask_point_sort_uw(MaskSplinePoint *point, MaskSplinePointUW *uw)
1281 {
1282         if (point->tot_uw > 1) {
1283                 int idx = uw - point->uw;
1284
1285                 if (idx > 0 && point->uw[idx - 1].u > uw->u) {
1286                         while (idx > 0 && point->uw[idx - 1].u > point->uw[idx].u) {
1287                                 SWAP(MaskSplinePointUW, point->uw[idx - 1], point->uw[idx]);
1288                                 idx--;
1289                         }
1290                 }
1291
1292                 if (idx < point->tot_uw - 1 && point->uw[idx + 1].u < uw->u) {
1293                         while (idx < point->tot_uw - 1 && point->uw[idx + 1].u < point->uw[idx].u) {
1294                                 SWAP(MaskSplinePointUW, point->uw[idx + 1], point->uw[idx]);
1295                                 idx++;
1296                         }
1297                 }
1298
1299                 return &point->uw[idx];
1300         }
1301
1302         return uw;
1303 }
1304
1305 void BKE_mask_point_add_uw(MaskSplinePoint *point, float u, float w)
1306 {
1307         if (!point->uw)
1308                 point->uw = MEM_mallocN(sizeof(*point->uw), "mask point uw");
1309         else
1310                 point->uw = MEM_reallocN(point->uw, (point->tot_uw + 1) * sizeof(*point->uw));
1311
1312         point->uw[point->tot_uw].u = u;
1313         point->uw[point->tot_uw].w = w;
1314         point->uw[point->tot_uw].flag = 0;
1315
1316         point->tot_uw++;
1317
1318         BKE_mask_point_sort_uw(point, &point->uw[point->tot_uw - 1]);
1319 }
1320
1321 void BKE_mask_point_select_set(MaskSplinePoint *point, const short do_select)
1322 {
1323         int i;
1324
1325         if (do_select) {
1326                 MASKPOINT_SEL_ALL(point);
1327         }
1328         else {
1329                 MASKPOINT_DESEL_ALL(point);
1330         }
1331
1332         for (i = 0; i < point->tot_uw; i++) {
1333                 if (do_select) {
1334                         point->uw[i].flag |= SELECT;
1335                 }
1336                 else {
1337                         point->uw[i].flag &= ~SELECT;
1338                 }
1339         }
1340 }
1341
1342 void BKE_mask_point_select_set_handle(MaskSplinePoint *point, const short do_select)
1343 {
1344         if (do_select) {
1345                 MASKPOINT_SEL_HANDLE(point);
1346         }
1347         else {
1348                 MASKPOINT_DESEL_HANDLE(point);
1349         }
1350 }
1351
1352 /* only mask block itself */
1353 static Mask *mask_alloc(const char *name)
1354 {
1355         Mask *mask;
1356
1357         mask = BKE_libblock_alloc(&G.main->mask, ID_MSK, name);
1358
1359         mask->id.flag |= LIB_FAKEUSER;
1360
1361         return mask;
1362 }
1363
1364 Mask *BKE_mask_new(const char *name)
1365 {
1366         Mask *mask;
1367         char mask_name[MAX_ID_NAME - 2];
1368
1369         if (name && name[0])
1370                 BLI_strncpy(mask_name, name, sizeof(mask_name));
1371         else
1372                 strcpy(mask_name, "Mask");
1373
1374         mask = mask_alloc(mask_name);
1375
1376         /* arbitrary defaults */
1377         mask->sfra = 1;
1378         mask->efra = 100;
1379
1380         return mask;
1381 }
1382
1383 Mask *BKE_mask_copy_nolib(Mask *mask)
1384 {
1385         Mask *mask_new;
1386
1387         mask_new = MEM_dupallocN(mask);
1388
1389         /*take care here! - we may want to copy anim data  */
1390         mask_new->adt = NULL;
1391
1392         mask_new->masklayers.first = NULL;
1393         mask_new->masklayers.last = NULL;
1394
1395         BKE_mask_layer_copy_list(&mask_new->masklayers, &mask->masklayers);
1396
1397         /* enable fake user by default */
1398         if (!(mask_new->id.flag & LIB_FAKEUSER)) {
1399                 mask_new->id.flag |= LIB_FAKEUSER;
1400                 mask_new->id.us++;
1401         }
1402
1403         return mask_new;
1404 }
1405
1406 Mask *BKE_mask_copy(Mask *mask)
1407 {
1408         Mask *mask_new;
1409
1410         mask_new = BKE_libblock_copy(&mask->id);
1411
1412         mask_new->masklayers.first = NULL;
1413         mask_new->masklayers.last = NULL;
1414
1415         BKE_mask_layer_copy_list(&mask_new->masklayers, &mask->masklayers);
1416
1417         /* enable fake user by default */
1418         if (!(mask_new->id.flag & LIB_FAKEUSER)) {
1419                 mask_new->id.flag |= LIB_FAKEUSER;
1420                 mask_new->id.us++;
1421         }
1422
1423         return mask_new;
1424 }
1425
1426 void BKE_mask_point_free(MaskSplinePoint *point)
1427 {
1428         if (point->uw)
1429                 MEM_freeN(point->uw);
1430 }
1431
1432 void BKE_mask_spline_free(MaskSpline *spline)
1433 {
1434         int i = 0;
1435
1436         for (i = 0; i < spline->tot_point; i++) {
1437                 MaskSplinePoint *point;
1438                 point = &spline->points[i];
1439                 BKE_mask_point_free(point);
1440
1441                 if (spline->points_deform) {
1442                         point = &spline->points_deform[i];
1443                         BKE_mask_point_free(point);
1444                 }
1445         }
1446
1447         MEM_freeN(spline->points);
1448
1449         if (spline->points_deform) {
1450                 MEM_freeN(spline->points_deform);
1451         }
1452
1453         MEM_freeN(spline);
1454 }
1455
1456 static MaskSplinePoint *mask_spline_points_copy(MaskSplinePoint *points, int tot_point)
1457 {
1458         MaskSplinePoint *npoints;
1459         int i;
1460
1461         npoints = MEM_dupallocN(points);
1462
1463         for (i = 0; i < tot_point; i++) {
1464                 MaskSplinePoint *point = &npoints[i];
1465
1466                 if (point->uw)
1467                         point->uw = MEM_dupallocN(point->uw);
1468         }
1469
1470         return npoints;
1471 }
1472
1473 MaskSpline *BKE_mask_spline_copy(MaskSpline *spline)
1474 {
1475         MaskSpline *nspline = MEM_callocN(sizeof(MaskSpline), "new spline");
1476
1477         *nspline = *spline;
1478
1479         nspline->points_deform = NULL;
1480         nspline->points = mask_spline_points_copy(spline->points, spline->tot_point);
1481
1482         if (spline->points_deform) {
1483                 nspline->points_deform = mask_spline_points_copy(spline->points_deform, spline->tot_point);
1484         }
1485
1486         return nspline;
1487 }
1488
1489 /* note: does NOT add to the list */
1490 MaskLayerShape *BKE_mask_layer_shape_alloc(MaskLayer *masklay, const int frame)
1491 {
1492         MaskLayerShape *masklay_shape;
1493         int tot_vert = BKE_mask_layer_shape_totvert(masklay);
1494
1495         masklay_shape = MEM_mallocN(sizeof(MaskLayerShape), __func__);
1496         masklay_shape->frame = frame;
1497         masklay_shape->tot_vert = tot_vert;
1498         masklay_shape->data = MEM_mallocN(tot_vert * sizeof(float) * MASK_OBJECT_SHAPE_ELEM_SIZE, __func__);
1499
1500         return masklay_shape;
1501 }
1502
1503 void BKE_mask_layer_shape_free(MaskLayerShape *masklay_shape)
1504 {
1505         if (masklay_shape->data) {
1506                 MEM_freeN(masklay_shape->data);
1507         }
1508
1509         MEM_freeN(masklay_shape);
1510 }
1511
1512 /** \brief Free all animation keys for a mask layer
1513  */
1514 void BKE_mask_layer_free_shapes(MaskLayer *masklay)
1515 {
1516         MaskLayerShape *masklay_shape;
1517
1518         /* free animation data */
1519         masklay_shape = masklay->splines_shapes.first;
1520         while (masklay_shape) {
1521                 MaskLayerShape *next_masklay_shape = masklay_shape->next;
1522
1523                 BLI_remlink(&masklay->splines_shapes, masklay_shape);
1524                 BKE_mask_layer_shape_free(masklay_shape);
1525
1526                 masklay_shape = next_masklay_shape;
1527         }
1528 }
1529
1530 void BKE_mask_layer_free(MaskLayer *masklay)
1531 {
1532         MaskSpline *spline;
1533
1534         /* free splines */
1535         spline = masklay->splines.first;
1536         while (spline) {
1537                 MaskSpline *next_spline = spline->next;
1538
1539                 BLI_remlink(&masklay->splines, spline);
1540                 BKE_mask_spline_free(spline);
1541
1542                 spline = next_spline;
1543         }
1544
1545         /* free animation data */
1546         BKE_mask_layer_free_shapes(masklay);
1547
1548         MEM_freeN(masklay);
1549 }
1550
1551 void BKE_mask_layer_free_list(ListBase *masklayers)
1552 {
1553         MaskLayer *masklay = masklayers->first;
1554
1555         while (masklay) {
1556                 MaskLayer *masklay_next = masklay->next;
1557
1558                 BLI_remlink(masklayers, masklay);
1559                 BKE_mask_layer_free(masklay);
1560
1561                 masklay = masklay_next;
1562         }
1563 }
1564
1565 void BKE_mask_free(Mask *mask)
1566 {
1567         BKE_mask_layer_free_list(&mask->masklayers);
1568 }
1569
1570
1571 static void ntree_unlink_mask_cb(void *calldata, struct ID *UNUSED(owner_id), struct bNodeTree *ntree)
1572 {
1573         ID *id = (ID *)calldata;
1574         bNode *node;
1575
1576         for (node = ntree->nodes.first; node; node = node->next) {
1577                 if (node->id == id) {
1578                         node->id = NULL;
1579                 }
1580         }
1581 }
1582
1583 void BKE_mask_unlink(Main *bmain, Mask *mask)
1584 {
1585         bScreen *scr;
1586         ScrArea *area;
1587         SpaceLink *sl;
1588         Scene *scene;
1589
1590         for (scr = bmain->screen.first; scr; scr = scr->id.next) {
1591                 for (area = scr->areabase.first; area; area = area->next) {
1592                         for (sl = area->spacedata.first; sl; sl = sl->next) {
1593                                 switch (sl->spacetype) {
1594                                         case SPACE_CLIP:
1595                                         {
1596                                                 SpaceClip *sc = (SpaceClip *)sl;
1597
1598                                                 if (sc->mask_info.mask == mask) {
1599                                                         sc->mask_info.mask = NULL;
1600                                                 }
1601                                                 break;
1602                                         }
1603                                         case SPACE_IMAGE:
1604                                         {
1605                                                 SpaceImage *sima = (SpaceImage *)sl;
1606
1607                                                 if (sima->mask_info.mask == mask) {
1608                                                         sima->mask_info.mask = NULL;
1609                                                 }
1610                                                 break;
1611                                         }
1612                                 }
1613                         }
1614                 }
1615         }
1616
1617         for (scene = bmain->scene.first; scene; scene = scene->id.next) {
1618                 if (scene->ed) {
1619                         Sequence *seq;
1620
1621                         SEQ_BEGIN (scene->ed, seq)
1622                         {
1623                                 if (seq->mask == mask) {
1624                                         seq->mask = NULL;
1625                                 }
1626                         }
1627                         SEQ_END
1628                 }
1629
1630
1631                 if (scene->nodetree) {
1632                         bNode *node;
1633                         for (node = scene->nodetree->nodes.first; node; node = node->next) {
1634                                 if (node->id == &mask->id) {
1635                                         node->id = NULL;
1636                                 }
1637                         }
1638                 }
1639         }
1640
1641         {
1642                 bNodeTreeType *treetype = ntreeGetType(NTREE_COMPOSIT);
1643                 treetype->foreach_nodetree(bmain, (void *)mask, &ntree_unlink_mask_cb);
1644         }
1645
1646         BKE_libblock_free(&bmain->mask, mask);
1647 }
1648
1649 void BKE_mask_coord_from_frame(float r_co[2], const float co[2], const float frame_size[2])
1650 {
1651         if (frame_size[0] == frame_size[1]) {
1652                 r_co[0] = co[0];
1653                 r_co[1] = co[1];
1654         }
1655         else if (frame_size[0] < frame_size[1]) {
1656                 r_co[0] = ((co[0] - 0.5f) * (frame_size[0] / frame_size[1])) + 0.5f;
1657                 r_co[1] = co[1];
1658         }
1659         else { /* (frame_size[0] > frame_size[1]) */
1660                 r_co[0] = co[0];
1661                 r_co[1] = ((co[1] - 0.5f) * (frame_size[1] / frame_size[0])) + 0.5f;
1662         }
1663 }
1664 void BKE_mask_coord_from_movieclip(MovieClip *clip, MovieClipUser *user, float r_co[2], const float co[2])
1665 {
1666         int width, height;
1667         float frame_size[2];
1668
1669         /* scaling for the clip */
1670         BKE_movieclip_get_size(clip, user, &width, &height);
1671
1672         frame_size[0] = (float)width;
1673         frame_size[1] = (float)height;
1674
1675         BKE_mask_coord_from_frame(r_co, co, frame_size);
1676 }
1677
1678 /* as above but divide */
1679 void BKE_mask_coord_to_frame(float r_co[2], const float co[2], const float frame_size[2])
1680 {
1681         if (frame_size[0] == frame_size[1]) {
1682                 r_co[0] = co[0];
1683                 r_co[1] = co[1];
1684         }
1685         else if (frame_size[0] < frame_size[1]) {
1686                 r_co[0] = ((co[0] - 0.5f) / (frame_size[0] / frame_size[1])) + 0.5f;
1687                 r_co[1] = co[1];
1688         }
1689         else { /* (frame_size[0] > frame_size[1]) */
1690                 r_co[0] = co[0];
1691                 r_co[1] = ((co[1] - 0.5f) / (frame_size[1] / frame_size[0])) + 0.5f;
1692         }
1693 }
1694 void BKE_mask_coord_to_movieclip(MovieClip *clip, MovieClipUser *user, float r_co[2], const float co[2])
1695 {
1696         int width, height;
1697         float frame_size[2];
1698
1699         /* scaling for the clip */
1700         BKE_movieclip_get_size(clip, user, &width, &height);
1701
1702         frame_size[0] = (float)width;
1703         frame_size[1] = (float)height;
1704
1705         BKE_mask_coord_to_frame(r_co, co, frame_size);
1706 }
1707
1708 static int BKE_mask_evaluate_parent(MaskParent *parent, float ctime, float r_co[2])
1709 {
1710         if (!parent)
1711                 return FALSE;
1712
1713         if (parent->id_type == ID_MC) {
1714                 if (parent->id) {
1715                         MovieClip *clip = (MovieClip *) parent->id;
1716                         MovieTracking *tracking = (MovieTracking *) &clip->tracking;
1717                         MovieTrackingObject *ob = BKE_tracking_object_get_named(tracking, parent->parent);
1718
1719                         if (ob) {
1720                                 MovieTrackingTrack *track = BKE_tracking_track_get_named(tracking, ob, parent->sub_parent);
1721                                 float clip_framenr = BKE_movieclip_remap_scene_to_clip_frame(clip, ctime);
1722
1723                                 MovieClipUser user = {0};
1724                                 user.framenr = ctime;
1725
1726                                 if (track) {
1727                                         float marker_pos_ofs[2];
1728                                         BKE_tracking_marker_get_subframe_position(track, clip_framenr, marker_pos_ofs);
1729                                         BKE_mask_coord_from_movieclip(clip, &user, r_co, marker_pos_ofs);
1730
1731                                         return TRUE;
1732                                 }
1733                         }
1734                 }
1735         }
1736
1737         return FALSE;
1738 }
1739
1740 int BKE_mask_evaluate_parent_delta(MaskParent *parent, float ctime, float r_delta[2])
1741 {
1742         float parent_co[2];
1743
1744         if (BKE_mask_evaluate_parent(parent, ctime, parent_co)) {
1745                 sub_v2_v2v2(r_delta, parent_co, parent->parent_orig);
1746                 return TRUE;
1747         }
1748         else {
1749                 return FALSE;
1750         }
1751 }
1752
1753 static void mask_calc_point_handle(MaskSplinePoint *point, MaskSplinePoint *point_prev, MaskSplinePoint *point_next)
1754 {
1755         BezTriple *bezt = &point->bezt;
1756         BezTriple *bezt_prev = NULL, *bezt_next = NULL;
1757         //int handle_type = bezt->h1;
1758
1759         if (point_prev)
1760                 bezt_prev = &point_prev->bezt;
1761
1762         if (point_next)
1763                 bezt_next = &point_next->bezt;
1764
1765 #if 1
1766         if (bezt_prev || bezt_next) {
1767                 BKE_nurb_handle_calc(bezt, bezt_prev, bezt_next, 0);
1768         }
1769 #else
1770         if (handle_type == HD_VECT) {
1771                 BKE_nurb_handle_calc(bezt, bezt_prev, bezt_next, 0);
1772         }
1773         else if (handle_type == HD_AUTO) {
1774                 BKE_nurb_handle_calc(bezt, bezt_prev, bezt_next, 0);
1775         }
1776         else if (handle_type == HD_ALIGN) {
1777                 float v1[3], v2[3];
1778                 float vec[3], h[3];
1779
1780                 sub_v3_v3v3(v1, bezt->vec[0], bezt->vec[1]);
1781                 sub_v3_v3v3(v2, bezt->vec[2], bezt->vec[1]);
1782                 add_v3_v3v3(vec, v1, v2);
1783
1784                 if (len_v3(vec) > 1e-3) {
1785                         h[0] = vec[1];
1786                         h[1] = -vec[0];
1787                         h[2] = 0.0f;
1788                 }
1789                 else {
1790                         copy_v3_v3(h, v1);
1791                 }
1792
1793                 add_v3_v3v3(bezt->vec[0], bezt->vec[1], h);
1794                 sub_v3_v3v3(bezt->vec[2], bezt->vec[1], h);
1795         }
1796 #endif
1797 }
1798
1799 void BKE_mask_get_handle_point_adjacent(MaskSpline *spline, MaskSplinePoint *point,
1800                                         MaskSplinePoint **r_point_prev, MaskSplinePoint **r_point_next)
1801 {
1802         /* TODO, could avoid calling this at such low level */
1803         MaskSplinePoint *points_array = BKE_mask_spline_point_array_from_point(spline, point);
1804
1805         *r_point_prev = mask_spline_point_prev(spline, points_array, point);
1806         *r_point_next = mask_spline_point_next(spline, points_array, point);
1807 }
1808
1809 /* calculates the tanget of a point by its previous and next
1810  * (ignoring handles - as if its a poly line) */
1811 void BKE_mask_calc_tangent_polyline(MaskSpline *spline, MaskSplinePoint *point, float t[2])
1812 {
1813         float tvec_a[2], tvec_b[2];
1814
1815         MaskSplinePoint *point_prev, *point_next;
1816
1817         BKE_mask_get_handle_point_adjacent(spline, point,
1818                                            &point_prev, &point_next);
1819
1820         if (point_prev) {
1821                 sub_v2_v2v2(tvec_a, point->bezt.vec[1], point_prev->bezt.vec[1]);
1822                 normalize_v2(tvec_a);
1823         }
1824         else {
1825                 zero_v2(tvec_a);
1826         }
1827
1828         if (point_next) {
1829                 sub_v2_v2v2(tvec_b, point_next->bezt.vec[1], point->bezt.vec[1]);
1830                 normalize_v2(tvec_b);
1831         }
1832         else {
1833                 zero_v2(tvec_b);
1834         }
1835
1836         add_v2_v2v2(t, tvec_a, tvec_b);
1837         normalize_v2(t);
1838 }
1839
1840 void BKE_mask_calc_handle_point(MaskSpline *spline, MaskSplinePoint *point)
1841 {
1842         MaskSplinePoint *point_prev, *point_next;
1843
1844         BKE_mask_get_handle_point_adjacent(spline, point,
1845                                            &point_prev, &point_next);
1846
1847         mask_calc_point_handle(point, point_prev, point_next);
1848 }
1849
1850 static void enforce_dist_v2_v2fl(float v1[2], const float v2[2], const float dist)
1851 {
1852         if (!equals_v2v2(v2, v1)) {
1853                 float nor[2];
1854
1855                 sub_v2_v2v2(nor, v1, v2);
1856                 normalize_v2(nor);
1857                 madd_v2_v2v2fl(v1, v2, nor, dist);
1858         }
1859 }
1860
1861 void BKE_mask_calc_handle_adjacent_interp(MaskSpline *spline, MaskSplinePoint *point, const float u)
1862 {
1863         /* TODO! - make this interpolate between siblings - not always midpoint! */
1864         int length_tot = 0;
1865         float length_average = 0.0f;
1866         float weight_average = 0.0f;
1867
1868
1869         MaskSplinePoint *point_prev, *point_next;
1870
1871         BLI_assert(u >= 0.0f && u <= 1.0f);
1872
1873         BKE_mask_get_handle_point_adjacent(spline, point,
1874                                            &point_prev, &point_next);
1875
1876         if (point_prev && point_next) {
1877                 length_average = ((len_v2v2(point_prev->bezt.vec[0], point_prev->bezt.vec[1]) * (1.0f - u)) +
1878                                   (len_v2v2(point_next->bezt.vec[2], point_next->bezt.vec[1]) * u));
1879
1880                 weight_average = (point_prev->bezt.weight * (1.0f - u) +
1881                                   point_next->bezt.weight * u);
1882                 length_tot = 1;
1883         }
1884         else {
1885                 if (point_prev) {
1886                         length_average += len_v2v2(point_prev->bezt.vec[0], point_prev->bezt.vec[1]);
1887                         weight_average += point_prev->bezt.weight;
1888                         length_tot++;
1889                 }
1890
1891                 if (point_next) {
1892                         length_average += len_v2v2(point_next->bezt.vec[2], point_next->bezt.vec[1]);
1893                         weight_average += point_next->bezt.weight;
1894                         length_tot++;
1895                 }
1896         }
1897
1898         if (length_tot) {
1899                 length_average /= (float)length_tot;
1900                 weight_average /= (float)length_tot;
1901
1902                 enforce_dist_v2_v2fl(point->bezt.vec[0], point->bezt.vec[1], length_average);
1903                 enforce_dist_v2_v2fl(point->bezt.vec[2], point->bezt.vec[1], length_average);
1904                 point->bezt.weight = weight_average;
1905         }
1906 }
1907
1908
1909 /**
1910  * \brief Resets auto handles even for non-auto bezier points
1911  *
1912  * Useful for giving sane defaults.
1913  */
1914 void BKE_mask_calc_handle_point_auto(MaskSpline *spline, MaskSplinePoint *point,
1915                                      const short do_recalc_length)
1916 {
1917         MaskSplinePoint *point_prev, *point_next;
1918         const char h_back[2] = {point->bezt.h1, point->bezt.h2};
1919         const float length_average = (do_recalc_length) ? 0.0f /* dummy value */ :
1920                                      (len_v3v3(point->bezt.vec[0], point->bezt.vec[1]) +
1921                                       len_v3v3(point->bezt.vec[1], point->bezt.vec[2])) / 2.0f;
1922
1923         BKE_mask_get_handle_point_adjacent(spline, point,
1924                                            &point_prev, &point_next);
1925
1926         point->bezt.h1 = HD_AUTO;
1927         point->bezt.h2 = HD_AUTO;
1928         mask_calc_point_handle(point, point_prev, point_next);
1929
1930         point->bezt.h1 = h_back[0];
1931         point->bezt.h2 = h_back[1];
1932
1933         /* preserve length by applying it back */
1934         if (do_recalc_length == FALSE) {
1935                 enforce_dist_v2_v2fl(point->bezt.vec[0], point->bezt.vec[1], length_average);
1936                 enforce_dist_v2_v2fl(point->bezt.vec[2], point->bezt.vec[1], length_average);
1937         }
1938 }
1939
1940 void BKE_mask_layer_calc_handles(MaskLayer *masklay)
1941 {
1942         MaskSpline *spline;
1943         for (spline = masklay->splines.first; spline; spline = spline->next) {
1944                 int i;
1945                 for (i = 0; i < spline->tot_point; i++) {
1946                         BKE_mask_calc_handle_point(spline, &spline->points[i]);
1947                 }
1948         }
1949 }
1950
1951 void BKE_mask_layer_calc_handles_deform(MaskLayer *masklay)
1952 {
1953         MaskSpline *spline;
1954         for (spline = masklay->splines.first; spline; spline = spline->next) {
1955                 int i;
1956                 for (i = 0; i < spline->tot_point; i++) {
1957                         BKE_mask_calc_handle_point(spline, &spline->points_deform[i]);
1958                 }
1959         }
1960 }
1961
1962 void BKE_mask_calc_handles(Mask *mask)
1963 {
1964         MaskLayer *masklay;
1965         for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) {
1966                 BKE_mask_layer_calc_handles(masklay);
1967         }
1968 }
1969
1970 void BKE_mask_update_deform(Mask *mask)
1971 {
1972         MaskLayer *masklay;
1973
1974         for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) {
1975                 MaskSpline *spline;
1976
1977                 for (spline = masklay->splines.first; spline; spline = spline->next) {
1978                         int i;
1979
1980                         for (i = 0; i < spline->tot_point; i++) {
1981                                 const int i_prev = (i - 1) % spline->tot_point;
1982                                 const int i_next = (i + 1) % spline->tot_point;
1983
1984                                 BezTriple *bezt_prev = &spline->points[i_prev].bezt;
1985                                 BezTriple *bezt      = &spline->points[i].bezt;
1986                                 BezTriple *bezt_next = &spline->points[i_next].bezt;
1987
1988                                 BezTriple *bezt_def_prev = &spline->points_deform[i_prev].bezt;
1989                                 BezTriple *bezt_def      = &spline->points_deform[i].bezt;
1990                                 BezTriple *bezt_def_next = &spline->points_deform[i_next].bezt;
1991
1992                                 float w_src[4];
1993                                 int j;
1994
1995                                 for (j = 0; j <= 2; j += 2) { /* (0, 2) */
1996                                         printf("--- %d %d, %d, %d\n", i, j, i_prev, i_next);
1997                                         barycentric_weights_v2(bezt_prev->vec[1], bezt->vec[1], bezt_next->vec[1],
1998                                                                bezt->vec[j], w_src);
1999                                         interp_v3_v3v3v3(bezt_def->vec[j],
2000                                                          bezt_def_prev->vec[1], bezt_def->vec[1], bezt_def_next->vec[1], w_src);
2001                                 }
2002                         }
2003                 }
2004         }
2005 }
2006
2007 void BKE_mask_spline_ensure_deform(MaskSpline *spline)
2008 {
2009         int allocated_points = (MEM_allocN_len(spline->points_deform) / sizeof(*spline->points_deform));
2010         // printf("SPLINE ALLOC %p %d\n", spline->points_deform, allocated_points);
2011
2012         if (spline->points_deform == NULL || allocated_points != spline->tot_point) {
2013                 // printf("alloc new deform spline\n");
2014
2015                 if (spline->points_deform) {
2016                         int i;
2017
2018                         for (i = 0; i < allocated_points; i++) {
2019                                 MaskSplinePoint *point = &spline->points_deform[i];
2020                                 BKE_mask_point_free(point);
2021                         }
2022
2023                         MEM_freeN(spline->points_deform);
2024                 }
2025
2026                 spline->points_deform = MEM_callocN(sizeof(*spline->points_deform) * spline->tot_point, __func__);
2027         }
2028         else {
2029                 // printf("alloc spline done\n");
2030         }
2031 }
2032
2033 void BKE_mask_layer_evaluate(MaskLayer *masklay, const float ctime, const int do_newframe)
2034 {
2035         /* animation if available */
2036         if (do_newframe) {
2037                 MaskLayerShape *masklay_shape_a;
2038                 MaskLayerShape *masklay_shape_b;
2039                 int found;
2040
2041                 if ((found = BKE_mask_layer_shape_find_frame_range(masklay, ctime,
2042                                                                    &masklay_shape_a, &masklay_shape_b)))
2043                 {
2044                         if (found == 1) {
2045 #if 0
2046                                 printf("%s: exact %d %d (%d)\n", __func__, (int)ctime, BLI_countlist(&masklay->splines_shapes),
2047                                        masklay_shape_a->frame);
2048 #endif
2049
2050                                 BKE_mask_layer_shape_to_mask(masklay, masklay_shape_a);
2051                         }
2052                         else if (found == 2) {
2053                                 float w = masklay_shape_b->frame - masklay_shape_a->frame;
2054 #if 0
2055                                 printf("%s: tween %d %d (%d %d)\n", __func__, (int)ctime, BLI_countlist(&masklay->splines_shapes),
2056                                        masklay_shape_a->frame, masklay_shape_b->frame);
2057 #endif
2058                                 BKE_mask_layer_shape_to_mask_interp(masklay, masklay_shape_a, masklay_shape_b,
2059                                                                     (ctime - masklay_shape_a->frame) / w);
2060                         }
2061                         else {
2062                                 /* always fail, should never happen */
2063                                 BLI_assert(found == 2);
2064                         }
2065                 }
2066         }
2067         /* animation done... */
2068
2069         BKE_mask_layer_calc_handles(masklay);
2070
2071         /* update deform */
2072         {
2073                 MaskSpline *spline;
2074
2075                 for (spline = masklay->splines.first; spline; spline = spline->next) {
2076                         int i;
2077                         int has_auto = FALSE;
2078
2079                         BKE_mask_spline_ensure_deform(spline);
2080
2081                         for (i = 0; i < spline->tot_point; i++) {
2082                                 MaskSplinePoint *point = &spline->points[i];
2083                                 MaskSplinePoint *point_deform = &spline->points_deform[i];
2084                                 float delta[2];
2085
2086                                 BKE_mask_point_free(point_deform);
2087
2088                                 *point_deform = *point;
2089                                 point_deform->uw = point->uw ? MEM_dupallocN(point->uw) : NULL;
2090
2091                                 if (BKE_mask_evaluate_parent_delta(&point->parent, ctime, delta)) {
2092                                         add_v2_v2(point_deform->bezt.vec[0], delta);
2093                                         add_v2_v2(point_deform->bezt.vec[1], delta);
2094                                         add_v2_v2(point_deform->bezt.vec[2], delta);
2095                                 }
2096
2097                                 if (point->bezt.h1 == HD_AUTO) {
2098                                         has_auto = TRUE;
2099                                 }
2100                         }
2101
2102                         /* if the spline has auto handles, these need to be recalculated after deformation */
2103                         if (has_auto) {
2104                                 for (i = 0; i < spline->tot_point; i++) {
2105                                         MaskSplinePoint *point_deform = &spline->points_deform[i];
2106                                         if (point_deform->bezt.h1 == HD_AUTO) {
2107                                                 BKE_mask_calc_handle_point(spline, point_deform);
2108                                         }
2109                                 }
2110                         }
2111                         /* end extra calc handles loop */
2112                 }
2113         }
2114 }
2115
2116 void BKE_mask_evaluate(Mask *mask, const float ctime, const int do_newframe)
2117 {
2118         MaskLayer *masklay;
2119
2120         for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) {
2121                 BKE_mask_layer_evaluate(masklay, ctime, do_newframe);
2122         }
2123 }
2124
2125 /* the purpose of this function is to ensure spline->points_deform is never out of date.
2126  * for now re-evaluate all. eventually this might work differently */
2127 void BKE_mask_update_display(Mask *mask, float ctime)
2128 {
2129 #if 0
2130         MaskLayer *masklay;
2131
2132         for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) {
2133                 MaskSpline *spline;
2134
2135                 for (spline = masklay->splines.first; spline; spline = spline->next) {
2136                         if (spline->points_deform) {
2137                                 int i = 0;
2138
2139                                 for (i = 0; i < spline->tot_point; i++) {
2140                                         MaskSplinePoint *point;
2141
2142                                         if (spline->points_deform) {
2143                                                 point = &spline->points_deform[i];
2144                                                 BKE_mask_point_free(point);
2145                                         }
2146                                 }
2147                                 if (spline->points_deform) {
2148                                         MEM_freeN(spline->points_deform);
2149                                 }
2150
2151                                 spline->points_deform = NULL;
2152                         }
2153                 }
2154         }
2155 #endif
2156
2157         BKE_mask_evaluate(mask, ctime, FALSE);
2158 }
2159
2160 void BKE_mask_evaluate_all_masks(Main *bmain, float ctime, const int do_newframe)
2161 {
2162         Mask *mask;
2163
2164         for (mask = bmain->mask.first; mask; mask = mask->id.next) {
2165                 BKE_mask_evaluate(mask, ctime, do_newframe);
2166         }
2167 }
2168
2169 void BKE_mask_update_scene(Main *bmain, Scene *scene, const int do_newframe)
2170 {
2171         Mask *mask;
2172
2173         for (mask = bmain->mask.first; mask; mask = mask->id.next) {
2174                 if (mask->id.flag & LIB_ID_RECALC) {
2175                         BKE_mask_evaluate_all_masks(bmain, CFRA, do_newframe);
2176                 }
2177         }
2178 }
2179
2180 void BKE_mask_parent_init(MaskParent *parent)
2181 {
2182         parent->id_type = ID_MC;
2183 }
2184
2185
2186 /* *** own animation/shapekey implimentation ***
2187  * BKE_mask_layer_shape_XXX */
2188
2189 int BKE_mask_layer_shape_totvert(MaskLayer *masklay)
2190 {
2191         int tot = 0;
2192         MaskSpline *spline;
2193
2194         for (spline = masklay->splines.first; spline; spline = spline->next) {
2195                 tot += spline->tot_point;
2196         }
2197
2198         return tot;
2199 }
2200
2201 static void mask_layer_shape_from_mask_point(BezTriple *bezt, float fp[MASK_OBJECT_SHAPE_ELEM_SIZE])
2202 {
2203         copy_v2_v2(&fp[0], bezt->vec[0]);
2204         copy_v2_v2(&fp[2], bezt->vec[1]);
2205         copy_v2_v2(&fp[4], bezt->vec[2]);
2206         fp[6] = bezt->weight;
2207         fp[7] = bezt->radius;
2208 }
2209
2210 static void mask_layer_shape_to_mask_point(BezTriple *bezt, float fp[MASK_OBJECT_SHAPE_ELEM_SIZE])
2211 {
2212         copy_v2_v2(bezt->vec[0], &fp[0]);
2213         copy_v2_v2(bezt->vec[1], &fp[2]);
2214         copy_v2_v2(bezt->vec[2], &fp[4]);
2215         bezt->weight = fp[6];
2216         bezt->radius = fp[7];
2217 }
2218
2219 /* these functions match. copy is swapped */
2220 void BKE_mask_layer_shape_from_mask(MaskLayer *masklay, MaskLayerShape *masklay_shape)
2221 {
2222         int tot = BKE_mask_layer_shape_totvert(masklay);
2223
2224         if (masklay_shape->tot_vert == tot) {
2225                 float *fp = masklay_shape->data;
2226
2227                 MaskSpline *spline;
2228                 for (spline = masklay->splines.first; spline; spline = spline->next) {
2229                         int i;
2230                         for (i = 0; i < spline->tot_point; i++) {
2231                                 mask_layer_shape_from_mask_point(&spline->points[i].bezt, fp);
2232                                 fp += MASK_OBJECT_SHAPE_ELEM_SIZE;
2233                         }
2234                 }
2235         }
2236         else {
2237                 printf("%s: vert mismatch %d != %d (frame %d)\n",
2238                        __func__, masklay_shape->tot_vert, tot, masklay_shape->frame);
2239         }
2240 }
2241
2242 void BKE_mask_layer_shape_to_mask(MaskLayer *masklay, MaskLayerShape *masklay_shape)
2243 {
2244         int tot = BKE_mask_layer_shape_totvert(masklay);
2245
2246         if (masklay_shape->tot_vert == tot) {
2247                 float *fp = masklay_shape->data;
2248
2249                 MaskSpline *spline;
2250                 for (spline = masklay->splines.first; spline; spline = spline->next) {
2251                         int i;
2252                         for (i = 0; i < spline->tot_point; i++) {
2253                                 mask_layer_shape_to_mask_point(&spline->points[i].bezt, fp);
2254                                 fp += MASK_OBJECT_SHAPE_ELEM_SIZE;
2255                         }
2256                 }
2257         }
2258         else {
2259                 printf("%s: vert mismatch %d != %d (frame %d)\n",
2260                        __func__, masklay_shape->tot_vert, tot, masklay_shape->frame);
2261         }
2262 }
2263
2264 BLI_INLINE void interp_v2_v2v2_flfl(float target[2], const float a[2], const float b[2],
2265                                     const float t, const float s)
2266 {
2267         target[0] = s * a[0] + t * b[0];
2268         target[1] = s * a[1] + t * b[1];
2269 }
2270
2271 /* linear interpolation only */
2272 void BKE_mask_layer_shape_to_mask_interp(MaskLayer *masklay,
2273                                          MaskLayerShape *masklay_shape_a,
2274                                          MaskLayerShape *masklay_shape_b,
2275                                          const float fac)
2276 {
2277         int tot = BKE_mask_layer_shape_totvert(masklay);
2278         if (masklay_shape_a->tot_vert == tot && masklay_shape_b->tot_vert == tot) {
2279                 float *fp_a = masklay_shape_a->data;
2280                 float *fp_b = masklay_shape_b->data;
2281                 const float ifac = 1.0f - fac;
2282
2283                 MaskSpline *spline;
2284                 for (spline = masklay->splines.first; spline; spline = spline->next) {
2285                         int i;
2286                         for (i = 0; i < spline->tot_point; i++) {
2287                                 BezTriple *bezt = &spline->points[i].bezt;
2288                                 /* *** BKE_mask_layer_shape_from_mask - swapped *** */
2289                                 interp_v2_v2v2_flfl(bezt->vec[0], fp_a, fp_b, fac, ifac); fp_a += 2; fp_b += 2;
2290                                 interp_v2_v2v2_flfl(bezt->vec[1], fp_a, fp_b, fac, ifac); fp_a += 2; fp_b += 2;
2291                                 interp_v2_v2v2_flfl(bezt->vec[2], fp_a, fp_b, fac, ifac); fp_a += 2; fp_b += 2;
2292                                 bezt->weight = (fp_a[0] * ifac) + (fp_b[0] * fac);
2293                                 bezt->radius = (fp_a[1] * ifac) + (fp_b[1] * fac); fp_a += 2; fp_b += 2;
2294                         }
2295                 }
2296         }
2297         else {
2298                 printf("%s: vert mismatch %d != %d != %d (frame %d - %d)\n",
2299                        __func__, masklay_shape_a->tot_vert, masklay_shape_b->tot_vert, tot,
2300                        masklay_shape_a->frame, masklay_shape_b->frame);
2301         }
2302 }
2303
2304 MaskLayerShape *BKE_mask_layer_shape_find_frame(MaskLayer *masklay, const int frame)
2305 {
2306         MaskLayerShape *masklay_shape;
2307
2308         for (masklay_shape = masklay->splines_shapes.first;
2309              masklay_shape;
2310              masklay_shape = masklay_shape->next)
2311         {
2312                 if (frame == masklay_shape->frame) {
2313                         return masklay_shape;
2314                 }
2315                 else if (frame < masklay_shape->frame) {
2316                         break;
2317                 }
2318         }
2319
2320         return NULL;
2321 }
2322
2323 /* when returning 2 - the frame isnt found but before/after frames are */
2324 int BKE_mask_layer_shape_find_frame_range(MaskLayer *masklay, const float frame,
2325                                           MaskLayerShape **r_masklay_shape_a,
2326                                           MaskLayerShape **r_masklay_shape_b)
2327 {
2328         MaskLayerShape *masklay_shape;
2329
2330         for (masklay_shape = masklay->splines_shapes.first;
2331              masklay_shape;
2332              masklay_shape = masklay_shape->next)
2333         {
2334                 if (frame == masklay_shape->frame) {
2335                         *r_masklay_shape_a = masklay_shape;
2336                         *r_masklay_shape_b = NULL;
2337                         return 1;
2338                 }
2339                 else if (frame < masklay_shape->frame) {
2340                         if (masklay_shape->prev) {
2341                                 *r_masklay_shape_a = masklay_shape->prev;
2342                                 *r_masklay_shape_b = masklay_shape;
2343                                 return 2;
2344                         }
2345                         else {
2346                                 *r_masklay_shape_a = masklay_shape;
2347                                 *r_masklay_shape_b = NULL;
2348                                 return 1;
2349                         }
2350                 }
2351         }
2352
2353         if ((masklay_shape = masklay->splines_shapes.last)) {
2354                 *r_masklay_shape_a = masklay_shape;
2355                 *r_masklay_shape_b = NULL;
2356                 return 1;
2357         }
2358         else {
2359                 *r_masklay_shape_a = NULL;
2360                 *r_masklay_shape_b = NULL;
2361
2362                 return 0;
2363         }
2364 }
2365
2366 MaskLayerShape *BKE_mask_layer_shape_varify_frame(MaskLayer *masklay, const int frame)
2367 {
2368         MaskLayerShape *masklay_shape;
2369
2370         masklay_shape = BKE_mask_layer_shape_find_frame(masklay, frame);
2371
2372         if (masklay_shape == NULL) {
2373                 masklay_shape = BKE_mask_layer_shape_alloc(masklay, frame);
2374                 BLI_addtail(&masklay->splines_shapes, masklay_shape);
2375                 BKE_mask_layer_shape_sort(masklay);
2376         }
2377
2378 #if 0
2379         {
2380                 MaskLayerShape *masklay_shape;
2381                 int i = 0;
2382                 for (masklay_shape = masklay->splines_shapes.first;
2383                      masklay_shape;
2384                      masklay_shape = masklay_shape->next)
2385                 {
2386                         printf("mask %d, %d\n", i++, masklay_shape->frame);
2387                 }
2388         }
2389 #endif
2390
2391         return masklay_shape;
2392 }
2393
2394 MaskLayerShape *BKE_mask_layer_shape_duplicate(MaskLayerShape *masklay_shape)
2395 {
2396         MaskLayerShape *masklay_shape_copy;
2397
2398         masklay_shape_copy = MEM_dupallocN(masklay_shape);
2399
2400         if (LIKELY(masklay_shape_copy->data)) {
2401                 masklay_shape_copy->data = MEM_dupallocN(masklay_shape_copy->data);
2402         }
2403
2404         return masklay_shape_copy;
2405 }
2406
2407 void BKE_mask_layer_shape_unlink(MaskLayer *masklay, MaskLayerShape *masklay_shape)
2408 {
2409         BLI_remlink(&masklay->splines_shapes, masklay_shape);
2410
2411         BKE_mask_layer_shape_free(masklay_shape);
2412 }
2413
2414 static int mask_layer_shape_sort_cb(void *masklay_shape_a_ptr, void *masklay_shape_b_ptr)
2415 {
2416         MaskLayerShape *masklay_shape_a = (MaskLayerShape *)masklay_shape_a_ptr;
2417         MaskLayerShape *masklay_shape_b = (MaskLayerShape *)masklay_shape_b_ptr;
2418
2419         if      (masklay_shape_a->frame < masklay_shape_b->frame)  return -1;
2420         else if (masklay_shape_a->frame > masklay_shape_b->frame)  return  1;
2421         else                                                       return  0;
2422 }
2423
2424 void BKE_mask_layer_shape_sort(MaskLayer *masklay)
2425 {
2426         BLI_sortlist(&masklay->splines_shapes, mask_layer_shape_sort_cb);
2427 }
2428
2429 int BKE_mask_layer_shape_spline_from_index(MaskLayer *masklay, int index,
2430                                            MaskSpline **r_masklay_shape, int *r_index)
2431 {
2432         MaskSpline *spline;
2433
2434         for (spline = masklay->splines.first; spline; spline = spline->next) {
2435                 if (index < spline->tot_point) {
2436                         *r_masklay_shape = spline;
2437                         *r_index = index;
2438                         return TRUE;
2439                 }
2440                 index -= spline->tot_point;
2441         }
2442
2443         return FALSE;
2444 }
2445
2446 int BKE_mask_layer_shape_spline_to_index(MaskLayer *masklay, MaskSpline *spline)
2447 {
2448         MaskSpline *spline_iter;
2449         int i_abs = 0;
2450         for (spline_iter = masklay->splines.first;
2451              spline_iter && spline_iter != spline;
2452              i_abs += spline_iter->tot_point, spline_iter = spline_iter->next)
2453         {
2454                 /* pass */
2455         }
2456
2457         return i_abs;
2458 }
2459
2460 /* basic 2D interpolation functions, could make more comprehensive later */
2461 static void interp_weights_uv_v2_calc(float r_uv[2], const float pt[2], const float pt_a[2], const float pt_b[2])
2462 {
2463         float pt_on_line[2];
2464         r_uv[0] = closest_to_line_v2(pt_on_line, pt, pt_a, pt_b);
2465         r_uv[1] = (len_v2v2(pt_on_line, pt) / len_v2v2(pt_a, pt_b)) *
2466                   ((line_point_side_v2(pt_a, pt_b, pt) < 0.0f) ? -1.0 : 1.0);  /* this line only sets the sign */
2467 }
2468
2469
2470 static void interp_weights_uv_v2_apply(const float uv[2], float r_pt[2], const float pt_a[2], const float pt_b[2])
2471 {
2472         const float dvec[2] = {pt_b[0] - pt_a[0],
2473                                pt_b[1] - pt_a[1]};
2474
2475         /* u */
2476         madd_v2_v2v2fl(r_pt, pt_a, dvec, uv[0]);
2477
2478         /* v */
2479         r_pt[0] += -dvec[1] * uv[1];
2480         r_pt[1] +=  dvec[0] * uv[1];
2481 }
2482
2483 /* when a now points added - resize all shapekey array  */
2484 void BKE_mask_layer_shape_changed_add(MaskLayer *masklay, int index,
2485                                       int do_init, int do_init_interpolate)
2486 {
2487         MaskLayerShape *masklay_shape;
2488
2489         /* spline index from masklay */
2490         MaskSpline *spline;
2491         int spline_point_index;
2492
2493         if (BKE_mask_layer_shape_spline_from_index(masklay, index,
2494                                                    &spline, &spline_point_index))
2495         {
2496                 /* sanity check */
2497                 /* the point has already been removed in this array so subtract one when comparing with the shapes */
2498                 int tot = BKE_mask_layer_shape_totvert(masklay) - 1;
2499
2500                 /* for interpolation */
2501                 /* TODO - assumes closed curve for now */
2502                 float uv[3][2]; /* 3x 2D handles */
2503                 const int pi_curr =   spline_point_index;
2504                 const int pi_prev = ((spline_point_index - 1) + spline->tot_point) % spline->tot_point;
2505                 const int pi_next =  (spline_point_index + 1)                      % spline->tot_point;
2506
2507                 const int index_offset = index - spline_point_index;
2508                 /* const int pi_curr_abs = index; */
2509                 const int pi_prev_abs = pi_prev + index_offset;
2510                 const int pi_next_abs = pi_next + index_offset;
2511
2512                 int i;
2513                 if (do_init_interpolate) {
2514                         for (i = 0; i < 3; i++) {
2515                                 interp_weights_uv_v2_calc(uv[i],
2516                                                           spline->points[pi_curr].bezt.vec[i],
2517                                                           spline->points[pi_prev].bezt.vec[i],
2518                                                           spline->points[pi_next].bezt.vec[i]);
2519                         }
2520                 }
2521
2522                 for (masklay_shape = masklay->splines_shapes.first;
2523                      masklay_shape;
2524                      masklay_shape = masklay_shape->next)
2525                 {
2526                         if (tot == masklay_shape->tot_vert) {
2527                                 float *data_resized;
2528
2529                                 masklay_shape->tot_vert++;
2530                                 data_resized = MEM_mallocN(masklay_shape->tot_vert * sizeof(float) * MASK_OBJECT_SHAPE_ELEM_SIZE, __func__);
2531                                 if (index > 0) {
2532                                         memcpy(data_resized,
2533                                                masklay_shape->data,
2534                                                index * sizeof(float) * MASK_OBJECT_SHAPE_ELEM_SIZE);
2535                                 }
2536
2537                                 if (index != masklay_shape->tot_vert - 1) {
2538                                         memcpy(&data_resized[(index + 1) * MASK_OBJECT_SHAPE_ELEM_SIZE],
2539                                                masklay_shape->data + (index * MASK_OBJECT_SHAPE_ELEM_SIZE),
2540                                                (masklay_shape->tot_vert - (index + 1)) * sizeof(float) * MASK_OBJECT_SHAPE_ELEM_SIZE);
2541                                 }
2542
2543                                 if (do_init) {
2544                                         float *fp = &data_resized[index * MASK_OBJECT_SHAPE_ELEM_SIZE];
2545
2546                                         mask_layer_shape_from_mask_point(&spline->points[spline_point_index].bezt, fp);
2547
2548                                         if (do_init_interpolate && spline->tot_point > 2) {
2549                                                 for (i = 0; i < 3; i++) {
2550                                                         interp_weights_uv_v2_apply(uv[i],
2551                                                                                    &fp[i * 2],
2552                                                                                    &data_resized[(pi_prev_abs * MASK_OBJECT_SHAPE_ELEM_SIZE) + (i * 2)],
2553                                                                                    &data_resized[(pi_next_abs * MASK_OBJECT_SHAPE_ELEM_SIZE) + (i * 2)]);
2554                                                 }
2555                                         }
2556                                 }
2557                                 else {
2558                                         memset(&data_resized[index * MASK_OBJECT_SHAPE_ELEM_SIZE],
2559                                                0,
2560                                                sizeof(float) * MASK_OBJECT_SHAPE_ELEM_SIZE);
2561                                 }
2562
2563                                 MEM_freeN(masklay_shape->data);
2564                                 masklay_shape->data = data_resized;
2565                         }
2566                         else {
2567                                 printf("%s: vert mismatch %d != %d (frame %d)\n",
2568                                        __func__, masklay_shape->tot_vert, tot, masklay_shape->frame);
2569                         }
2570                 }
2571         }
2572 }
2573
2574
2575 /* move array to account for removed point */
2576 void BKE_mask_layer_shape_changed_remove(MaskLayer *masklay, int index, int count)
2577 {
2578         MaskLayerShape *masklay_shape;
2579
2580         /* the point has already been removed in this array so add one when comparing with the shapes */
2581         int tot = BKE_mask_layer_shape_totvert(masklay);
2582
2583         for (masklay_shape = masklay->splines_shapes.first;
2584              masklay_shape;
2585              masklay_shape = masklay_shape->next)
2586         {
2587                 if (tot == masklay_shape->tot_vert - count) {
2588                         float *data_resized;
2589
2590                         masklay_shape->tot_vert -= count;
2591                         data_resized = MEM_mallocN(masklay_shape->tot_vert * sizeof(float) * MASK_OBJECT_SHAPE_ELEM_SIZE, __func__);
2592                         if (index > 0) {
2593                                 memcpy(data_resized,
2594                                        masklay_shape->data,
2595                                        index * sizeof(float) * MASK_OBJECT_SHAPE_ELEM_SIZE);
2596                         }
2597
2598                         if (index != masklay_shape->tot_vert) {
2599                                 memcpy(&data_resized[index * MASK_OBJECT_SHAPE_ELEM_SIZE],
2600                                        masklay_shape->data + ((index + count) * MASK_OBJECT_SHAPE_ELEM_SIZE),
2601                                        (masklay_shape->tot_vert - index) * sizeof(float) * MASK_OBJECT_SHAPE_ELEM_SIZE);
2602                         }
2603
2604                         MEM_freeN(masklay_shape->data);
2605                         masklay_shape->data = data_resized;
2606                 }
2607                 else {
2608                         printf("%s: vert mismatch %d != %d (frame %d)\n",
2609                                __func__, masklay_shape->tot_vert - count, tot, masklay_shape->frame);
2610                 }
2611         }
2612 }
2613
2614 int BKE_mask_get_duration(Mask *mask)
2615 {
2616         return maxi(1, mask->efra - mask->sfra);
2617 }