d08b7316e612d5b9c621670b6c43ad3844ef5809
[blender-staging.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  *                 Campbell Barton
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/blenkernel/intern/mask.c
29  *  \ingroup bke
30  */
31
32 #include <stddef.h>
33 #include <string.h>
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_utildefines.h"
38 #include "BLI_ghash.h"
39 #include "BLI_path_util.h"
40 #include "BLI_string.h"
41 #include "BLI_listbase.h"
42 #include "BLI_math.h"
43
44 #include "BLF_translation.h"
45
46 #include "DNA_mask_types.h"
47 #include "DNA_node_types.h"
48 #include "DNA_screen_types.h"
49 #include "DNA_space_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_image.h"
62
63 #include "NOD_composite.h"
64
65 static struct {
66         ListBase splines;
67         struct GHash *id_hash;
68 } mask_clipboard = {{NULL}};
69
70 static MaskSplinePoint *mask_spline_point_next(MaskSpline *spline, MaskSplinePoint *points_array, MaskSplinePoint *point)
71 {
72         if (point == &points_array[spline->tot_point - 1]) {
73                 if (spline->flag & MASK_SPLINE_CYCLIC) {
74                         return &points_array[0];
75                 }
76                 else {
77                         return NULL;
78                 }
79         }
80         else {
81                 return point + 1;
82         }
83 }
84
85 static MaskSplinePoint *mask_spline_point_prev(MaskSpline *spline, MaskSplinePoint *points_array, MaskSplinePoint *point)
86 {
87         if (point == points_array) {
88                 if (spline->flag & MASK_SPLINE_CYCLIC) {
89                         return &points_array[spline->tot_point - 1];
90                 }
91                 else {
92                         return NULL;
93                 }
94         }
95         else {
96                 return point - 1;
97         }
98 }
99
100 BezTriple *BKE_mask_spline_point_next_bezt(MaskSpline *spline, MaskSplinePoint *points_array, MaskSplinePoint *point)
101 {
102         if (point == &points_array[spline->tot_point - 1]) {
103                 if (spline->flag & MASK_SPLINE_CYCLIC) {
104                         return &(points_array[0].bezt);
105                 }
106                 else {
107                         return NULL;
108                 }
109         }
110         else {
111                 return &((point + 1))->bezt;
112         }
113 }
114
115 MaskSplinePoint *BKE_mask_spline_point_array(MaskSpline *spline)
116 {
117         return spline->points_deform ? spline->points_deform : spline->points;
118 }
119
120 MaskSplinePoint *BKE_mask_spline_point_array_from_point(MaskSpline *spline, MaskSplinePoint *point_ref)
121 {
122         if ((point_ref >= spline->points) && (point_ref < &spline->points[spline->tot_point])) {
123                 return spline->points;
124         }
125
126         if ((point_ref >= spline->points_deform) && (point_ref < &spline->points_deform[spline->tot_point])) {
127                 return spline->points_deform;
128         }
129
130         BLI_assert(!"wrong array");
131         return NULL;
132 }
133
134 /* mask layers */
135
136 MaskLayer *BKE_mask_layer_new(Mask *mask, const char *name)
137 {
138         MaskLayer *masklay = MEM_callocN(sizeof(MaskLayer), __func__);
139
140         if (name && name[0])
141                 BLI_strncpy(masklay->name, name, sizeof(masklay->name));
142         else
143                 strcpy(masklay->name, "MaskLayer");
144
145         BLI_addtail(&mask->masklayers, masklay);
146
147         BKE_mask_layer_unique_name(mask, masklay);
148
149         mask->masklay_tot++;
150
151         masklay->blend = MASK_BLEND_MERGE_ADD;
152         masklay->alpha = 1.0f;
153         masklay->flag = MASK_LAYERFLAG_FILL_DISCRETE | MASK_LAYERFLAG_FILL_OVERLAP;
154
155         return masklay;
156 }
157
158 /* note: may still be hidden, caller needs to check */
159 MaskLayer *BKE_mask_layer_active(Mask *mask)
160 {
161         return BLI_findlink(&mask->masklayers, mask->masklay_act);
162 }
163
164 void BKE_mask_layer_active_set(Mask *mask, MaskLayer *masklay)
165 {
166         mask->masklay_act = BLI_findindex(&mask->masklayers, masklay);
167 }
168
169 void BKE_mask_layer_remove(Mask *mask, MaskLayer *masklay)
170 {
171         BLI_remlink(&mask->masklayers, masklay);
172         BKE_mask_layer_free(masklay);
173
174         mask->masklay_tot--;
175
176         if (mask->masklay_act >= mask->masklay_tot)
177                 mask->masklay_act = mask->masklay_tot - 1;
178 }
179
180 void BKE_mask_layer_unique_name(Mask *mask, MaskLayer *masklay)
181 {
182         BLI_uniquename(&mask->masklayers, masklay, DATA_("MaskLayer"), '.', offsetof(MaskLayer, name),
183                        sizeof(masklay->name));
184 }
185
186 MaskLayer *BKE_mask_layer_copy(MaskLayer *masklay)
187 {
188         MaskLayer *masklay_new;
189         MaskSpline *spline;
190
191         masklay_new = MEM_callocN(sizeof(MaskLayer), "new mask layer");
192
193         BLI_strncpy(masklay_new->name, masklay->name, sizeof(masklay_new->name));
194
195         masklay_new->alpha = masklay->alpha;
196         masklay_new->blend = masklay->blend;
197         masklay_new->blend_flag = masklay->blend_flag;
198         masklay_new->flag = masklay->flag;
199         masklay_new->restrictflag = masklay->restrictflag;
200
201         for (spline = masklay->splines.first; spline; spline = spline->next) {
202                 MaskSpline *spline_new = BKE_mask_spline_copy(spline);
203
204                 BLI_addtail(&masklay_new->splines, spline_new);
205         }
206
207         /* correct animation */
208         if (masklay->splines_shapes.first) {
209                 MaskLayerShape *masklay_shape;
210                 MaskLayerShape *masklay_shape_new;
211
212                 for (masklay_shape = masklay->splines_shapes.first;
213                      masklay_shape;
214                      masklay_shape = masklay_shape->next)
215                 {
216                         masklay_shape_new = MEM_callocN(sizeof(MaskLayerShape), "new mask layer shape");
217
218                         masklay_shape_new->data = MEM_dupallocN(masklay_shape->data);
219                         masklay_shape_new->tot_vert = masklay_shape->tot_vert;
220                         masklay_shape_new->flag = masklay_shape->flag;
221                         masklay_shape_new->frame = masklay_shape->frame;
222
223                         BLI_addtail(&masklay_new->splines_shapes, masklay_shape_new);
224                 }
225         }
226
227         return masklay_new;
228 }
229
230 void BKE_mask_layer_copy_list(ListBase *masklayers_new, ListBase *masklayers)
231 {
232         MaskLayer *layer;
233
234         for (layer = masklayers->first; layer; layer = layer->next) {
235                 MaskLayer *layer_new = BKE_mask_layer_copy(layer);
236
237                 BLI_addtail(masklayers_new, layer_new);
238         }
239 }
240
241 /* splines */
242
243 MaskSpline *BKE_mask_spline_add(MaskLayer *masklay)
244 {
245         MaskSpline *spline;
246
247         spline = MEM_callocN(sizeof(MaskSpline), "new mask spline");
248         BLI_addtail(&masklay->splines, spline);
249
250         /* spline shall have one point at least */
251         spline->points = MEM_callocN(sizeof(MaskSplinePoint), "new mask spline point");
252         spline->tot_point = 1;
253
254         /* cyclic shapes are more usually used */
255         // spline->flag |= MASK_SPLINE_CYCLIC; // disable because its not so nice for drawing. could be done differently
256
257         spline->weight_interp = MASK_SPLINE_INTERP_EASE;
258
259         BKE_mask_parent_init(&spline->parent);
260
261         return spline;
262 }
263
264 bool BKE_mask_spline_remove(MaskLayer *mask_layer, MaskSpline *spline)
265 {
266         if (BLI_remlink_safe(&mask_layer->splines, spline) == FALSE) {
267                 return false;
268         }
269
270         BKE_mask_spline_free(spline);
271
272         return true;
273 }
274
275 void BKE_mask_point_direction_switch(MaskSplinePoint *point)
276 {
277         const int tot_uw = point->tot_uw;
278         const int tot_uw_half = tot_uw / 2;
279         int i;
280
281         float co_tmp[2];
282
283         /* swap handles */
284         copy_v2_v2(co_tmp, point->bezt.vec[0]);
285         copy_v2_v2(point->bezt.vec[0], point->bezt.vec[2]);
286         copy_v2_v2(point->bezt.vec[2], co_tmp);
287         /* in this case the flags are unlikely to be different but swap anyway */
288         SWAP(char, point->bezt.f1, point->bezt.f3);
289         SWAP(char, point->bezt.h1, point->bezt.h2);
290
291
292         /* swap UW's */
293         if (tot_uw > 1) {
294                 /* count */
295                 for (i = 0; i < tot_uw_half; i++) {
296                         MaskSplinePointUW *uw_a = &point->uw[i];
297                         MaskSplinePointUW *uw_b = &point->uw[tot_uw - (i + 1)];
298                         SWAP(MaskSplinePointUW, *uw_a, *uw_b);
299                 }
300         }
301
302         for (i = 0; i < tot_uw; i++) {
303                 MaskSplinePointUW *uw = &point->uw[i];
304                 uw->u = 1.0f - uw->u;
305         }
306 }
307
308 void BKE_mask_spline_direction_switch(MaskLayer *masklay, MaskSpline *spline)
309 {
310         const int tot_point = spline->tot_point;
311         const int tot_point_half = tot_point / 2;
312         int i, i_prev;
313
314         if (tot_point < 2) {
315                 return;
316         }
317
318         /* count */
319         for (i = 0; i < tot_point_half; i++) {
320                 MaskSplinePoint *point_a = &spline->points[i];
321                 MaskSplinePoint *point_b = &spline->points[tot_point - (i + 1)];
322                 SWAP(MaskSplinePoint, *point_a, *point_b);
323         }
324
325         /* correct UW's */
326         i_prev = tot_point - 1;
327         for (i = 0; i < tot_point; i++) {
328
329                 BKE_mask_point_direction_switch(&spline->points[i]);
330
331                 SWAP(MaskSplinePointUW *, spline->points[i].uw,     spline->points[i_prev].uw);
332                 SWAP(int,                 spline->points[i].tot_uw, spline->points[i_prev].tot_uw);
333
334                 i_prev = i;
335         }
336
337         /* correct animation */
338         if (masklay->splines_shapes.first) {
339                 MaskLayerShape *masklay_shape;
340
341                 const int spline_index = BKE_mask_layer_shape_spline_to_index(masklay, spline);
342
343                 for (masklay_shape = masklay->splines_shapes.first;
344                      masklay_shape;
345                      masklay_shape = masklay_shape->next)
346                 {
347                         MaskLayerShapeElem *fp_arr = (MaskLayerShapeElem *)masklay_shape->data;
348
349                         for (i = 0; i < tot_point_half; i++) {
350                                 MaskLayerShapeElem *fp_a = &fp_arr[spline_index +              (i)     ];
351                                 MaskLayerShapeElem *fp_b = &fp_arr[spline_index + (tot_point - (i + 1))];
352                                 SWAP(MaskLayerShapeElem, *fp_a, *fp_b);
353                         }
354                 }
355         }
356 }
357
358
359 float BKE_mask_spline_project_co(MaskSpline *spline, MaskSplinePoint *point,
360                                  float start_u, const float co[2], const eMaskSign sign)
361 {
362         const float proj_eps    = 1e-3;
363         const float proj_eps_sq = proj_eps * proj_eps;
364         const int N = 1000;
365         float u = -1.0f, du = 1.0f / N, u1 = start_u, u2 = start_u;
366         float ang = -1.0f;
367
368         BLI_assert(ABS(sign) <= 1); /* (-1, 0, 1) */
369
370         while (u1 > 0.0f || u2 < 1.0f) {
371                 float n1[2], n2[2], co1[2], co2[2];
372                 float v1[2], v2[2];
373                 float ang1, ang2;
374
375                 if (u1 >= 0.0f) {
376                         BKE_mask_point_segment_co(spline, point, u1, co1);
377                         BKE_mask_point_normal(spline, point, u1, n1);
378                         sub_v2_v2v2(v1, co, co1);
379
380                         if ((sign == MASK_PROJ_ANY) ||
381                             ((sign == MASK_PROJ_NEG) && (dot_v2v2(v1, n1) <= 0.0f)) ||
382                             ((sign == MASK_PROJ_POS) && (dot_v2v2(v1, n1) >= 0.0f)))
383                         {
384
385                                 if (len_squared_v2(v1) > proj_eps_sq) {
386                                         ang1 = angle_v2v2(v1, n1);
387                                         if (ang1 > (float)M_PI / 2.0f)
388                                                 ang1 = (float)M_PI - ang1;
389
390                                         if (ang < 0.0f || ang1 < ang) {
391                                                 ang = ang1;
392                                                 u = u1;
393                                         }
394                                 }
395                                 else {
396                                         u = u1;
397                                         break;
398                                 }
399                         }
400                 }
401
402                 if (u2 <= 1.0f) {
403                         BKE_mask_point_segment_co(spline, point, u2, co2);
404                         BKE_mask_point_normal(spline, point, u2, n2);
405                         sub_v2_v2v2(v2, co, co2);
406
407                         if ((sign == MASK_PROJ_ANY) ||
408                             ((sign == MASK_PROJ_NEG) && (dot_v2v2(v2, n2) <= 0.0f)) ||
409                             ((sign == MASK_PROJ_POS) && (dot_v2v2(v2, n2) >= 0.0f)))
410                         {
411
412                                 if (len_squared_v2(v2) > proj_eps_sq) {
413                                         ang2 = angle_v2v2(v2, n2);
414                                         if (ang2 > (float)M_PI / 2.0f)
415                                                 ang2 = (float)M_PI - ang2;
416
417                                         if (ang2 < ang) {
418                                                 ang = ang2;
419                                                 u = u2;
420                                         }
421                                 }
422                                 else {
423                                         u = u2;
424                                         break;
425                                 }
426                         }
427                 }
428
429                 u1 -= du;
430                 u2 += du;
431         }
432
433         return u;
434 }
435
436 /* point */
437
438 bool BKE_mask_point_has_handle(MaskSplinePoint *point)
439 {
440         BezTriple *bezt = &point->bezt;
441
442         return bezt->h1 == HD_ALIGN;
443 }
444
445 void BKE_mask_point_handle(MaskSplinePoint *point, float handle[2])
446 {
447         float vec[2];
448
449         sub_v2_v2v2(vec, point->bezt.vec[0], point->bezt.vec[1]);
450
451         handle[0] = (point->bezt.vec[1][0] + vec[1]);
452         handle[1] = (point->bezt.vec[1][1] - vec[0]);
453 }
454
455 void BKE_mask_point_set_handle(MaskSplinePoint *point, float loc[2], bool keep_direction,
456                                float orig_handle[2], float orig_vec[3][3])
457 {
458         BezTriple *bezt = &point->bezt;
459         float v1[2], v2[2], vec[2];
460
461         if (keep_direction) {
462                 sub_v2_v2v2(v1, loc, orig_vec[1]);
463                 sub_v2_v2v2(v2, orig_handle, orig_vec[1]);
464
465                 project_v2_v2v2(vec, v1, v2);
466
467                 if (dot_v2v2(v2, vec) > 0) {
468                         float len = len_v2(vec);
469
470                         sub_v2_v2v2(v1, orig_vec[0], orig_vec[1]);
471
472                         mul_v2_fl(v1, len / len_v2(v1));
473
474                         add_v2_v2v2(bezt->vec[0], bezt->vec[1], v1);
475                         sub_v2_v2v2(bezt->vec[2], bezt->vec[1], v1);
476                 }
477                 else {
478                         copy_v3_v3(bezt->vec[0], bezt->vec[1]);
479                         copy_v3_v3(bezt->vec[2], bezt->vec[1]);
480                 }
481         }
482         else {
483                 sub_v2_v2v2(v1, loc, bezt->vec[1]);
484
485                 v2[0] = -v1[1];
486                 v2[1] =  v1[0];
487
488                 add_v2_v2v2(bezt->vec[0], bezt->vec[1], v2);
489                 sub_v2_v2v2(bezt->vec[2], bezt->vec[1], v2);
490         }
491 }
492
493 void BKE_mask_point_segment_co(MaskSpline *spline, MaskSplinePoint *point, float u, float co[2])
494 {
495         MaskSplinePoint *points_array = BKE_mask_spline_point_array_from_point(spline, point);
496
497         BezTriple *bezt = &point->bezt, *bezt_next;
498         float q0[2], q1[2], q2[2], r0[2], r1[2];
499
500         bezt_next = BKE_mask_spline_point_next_bezt(spline, points_array, point);
501
502         if (!bezt_next) {
503                 copy_v2_v2(co, bezt->vec[1]);
504                 return;
505         }
506
507         interp_v2_v2v2(q0, bezt->vec[1], bezt->vec[2], u);
508         interp_v2_v2v2(q1, bezt->vec[2], bezt_next->vec[0], u);
509         interp_v2_v2v2(q2, bezt_next->vec[0], bezt_next->vec[1], u);
510
511         interp_v2_v2v2(r0, q0, q1, u);
512         interp_v2_v2v2(r1, q1, q2, u);
513
514         interp_v2_v2v2(co, r0, r1, u);
515 }
516
517 void BKE_mask_point_normal(MaskSpline *spline, MaskSplinePoint *point, float u, float n[2])
518 {
519         MaskSplinePoint *points_array = BKE_mask_spline_point_array_from_point(spline, point);
520
521         BezTriple *bezt = &point->bezt, *bezt_next;
522         float q0[2], q1[2], q2[2], r0[2], r1[2], vec[2];
523
524         bezt_next = BKE_mask_spline_point_next_bezt(spline, points_array, point);
525
526         if (!bezt_next) {
527                 BKE_mask_point_handle(point, vec);
528
529                 sub_v2_v2v2(n, vec, bezt->vec[1]);
530                 normalize_v2(n);
531                 return;
532         }
533
534         interp_v2_v2v2(q0, bezt->vec[1], bezt->vec[2], u);
535         interp_v2_v2v2(q1, bezt->vec[2], bezt_next->vec[0], u);
536         interp_v2_v2v2(q2, bezt_next->vec[0], bezt_next->vec[1], u);
537
538         interp_v2_v2v2(r0, q0, q1, u);
539         interp_v2_v2v2(r1, q1, q2, u);
540
541         sub_v2_v2v2(vec, r1, r0);
542
543         n[0] = -vec[1];
544         n[1] =  vec[0];
545
546         normalize_v2(n);
547 }
548
549 static float mask_point_interp_weight(BezTriple *bezt, BezTriple *bezt_next, const float u)
550 {
551         return (bezt->weight * (1.0f - u)) + (bezt_next->weight * u);
552 }
553
554 float BKE_mask_point_weight_scalar(MaskSpline *spline, MaskSplinePoint *point, const float u)
555 {
556         MaskSplinePoint *points_array = BKE_mask_spline_point_array_from_point(spline, point);
557         BezTriple *bezt = &point->bezt, *bezt_next;
558
559         bezt_next = BKE_mask_spline_point_next_bezt(spline, points_array, point);
560
561         if (!bezt_next) {
562                 return bezt->weight;
563         }
564         else if (u <= 0.0f) {
565                 return bezt->weight;
566         }
567         else if (u >= 1.0f) {
568                 return bezt_next->weight;
569         }
570         else {
571                 return mask_point_interp_weight(bezt, bezt_next, u);
572         }
573 }
574
575 float BKE_mask_point_weight(MaskSpline *spline, MaskSplinePoint *point, const float u)
576 {
577         MaskSplinePoint *points_array = BKE_mask_spline_point_array_from_point(spline, point);
578         BezTriple *bezt = &point->bezt, *bezt_next;
579
580         bezt_next = BKE_mask_spline_point_next_bezt(spline, points_array, point);
581
582         if (!bezt_next) {
583                 return bezt->weight;
584         }
585         else if (u <= 0.0f) {
586                 return bezt->weight;
587         }
588         else if (u >= 1.0f) {
589                 return bezt_next->weight;
590         }
591         else {
592                 float cur_u = 0.0f, cur_w = 0.0f, next_u = 0.0f, next_w = 0.0f, fac; /* Quite warnings */
593                 int i;
594
595                 for (i = 0; i <= point->tot_uw; i++) {
596
597                         if (i == 0) {
598                                 cur_u = 0.0f;
599                                 cur_w = 1.0f; /* mask_point_interp_weight will scale it */
600                         }
601                         else {
602                                 cur_u = point->uw[i - 1].u;
603                                 cur_w = point->uw[i - 1].w;
604                         }
605
606                         if (i == point->tot_uw) {
607                                 next_u = 1.0f;
608                                 next_w = 1.0f; /* mask_point_interp_weight will scale it */
609                         }
610                         else {
611                                 next_u = point->uw[i].u;
612                                 next_w = point->uw[i].w;
613                         }
614
615                         if (u >= cur_u && u <= next_u) {
616                                 break;
617                         }
618                 }
619
620                 fac = (u - cur_u) / (next_u - cur_u);
621
622                 cur_w  *= mask_point_interp_weight(bezt, bezt_next, cur_u);
623                 next_w *= mask_point_interp_weight(bezt, bezt_next, next_u);
624
625                 if (spline->weight_interp == MASK_SPLINE_INTERP_EASE) {
626                         return cur_w + (next_w - cur_w) * (3.0f * fac * fac - 2.0f * fac * fac * fac);
627                 }
628                 else {
629                         return (1.0f - fac) * cur_w + fac * next_w;
630                 }
631         }
632 }
633
634 MaskSplinePointUW *BKE_mask_point_sort_uw(MaskSplinePoint *point, MaskSplinePointUW *uw)
635 {
636         if (point->tot_uw > 1) {
637                 int idx = uw - point->uw;
638
639                 if (idx > 0 && point->uw[idx - 1].u > uw->u) {
640                         while (idx > 0 && point->uw[idx - 1].u > point->uw[idx].u) {
641                                 SWAP(MaskSplinePointUW, point->uw[idx - 1], point->uw[idx]);
642                                 idx--;
643                         }
644                 }
645
646                 if (idx < point->tot_uw - 1 && point->uw[idx + 1].u < uw->u) {
647                         while (idx < point->tot_uw - 1 && point->uw[idx + 1].u < point->uw[idx].u) {
648                                 SWAP(MaskSplinePointUW, point->uw[idx + 1], point->uw[idx]);
649                                 idx++;
650                         }
651                 }
652
653                 return &point->uw[idx];
654         }
655
656         return uw;
657 }
658
659 void BKE_mask_point_add_uw(MaskSplinePoint *point, float u, float w)
660 {
661         if (!point->uw)
662                 point->uw = MEM_mallocN(sizeof(*point->uw), "mask point uw");
663         else
664                 point->uw = MEM_reallocN(point->uw, (point->tot_uw + 1) * sizeof(*point->uw));
665
666         point->uw[point->tot_uw].u = u;
667         point->uw[point->tot_uw].w = w;
668         point->uw[point->tot_uw].flag = 0;
669
670         point->tot_uw++;
671
672         BKE_mask_point_sort_uw(point, &point->uw[point->tot_uw - 1]);
673 }
674
675 void BKE_mask_point_select_set(MaskSplinePoint *point, const bool do_select)
676 {
677         int i;
678
679         if (do_select) {
680                 MASKPOINT_SEL_ALL(point);
681         }
682         else {
683                 MASKPOINT_DESEL_ALL(point);
684         }
685
686         for (i = 0; i < point->tot_uw; i++) {
687                 if (do_select) {
688                         point->uw[i].flag |= SELECT;
689                 }
690                 else {
691                         point->uw[i].flag &= ~SELECT;
692                 }
693         }
694 }
695
696 void BKE_mask_point_select_set_handle(MaskSplinePoint *point, const bool do_select)
697 {
698         if (do_select) {
699                 MASKPOINT_SEL_HANDLE(point);
700         }
701         else {
702                 MASKPOINT_DESEL_HANDLE(point);
703         }
704 }
705
706 /* only mask block itself */
707 static Mask *mask_alloc(Main *bmain, const char *name)
708 {
709         Mask *mask;
710
711         mask = BKE_libblock_alloc(bmain, ID_MSK, name);
712
713         mask->id.flag |= LIB_FAKEUSER;
714
715         return mask;
716 }
717
718 Mask *BKE_mask_new(Main *bmain, const char *name)
719 {
720         Mask *mask;
721         char mask_name[MAX_ID_NAME - 2];
722
723         if (name && name[0])
724                 BLI_strncpy(mask_name, name, sizeof(mask_name));
725         else
726                 strcpy(mask_name, "Mask");
727
728         mask = mask_alloc(bmain, mask_name);
729
730         /* arbitrary defaults */
731         mask->sfra = 1;
732         mask->efra = 100;
733
734         return mask;
735 }
736
737 /* TODO(sergey): Use generic BKE_libblock_copy_nolib() instead. */
738 Mask *BKE_mask_copy_nolib(Mask *mask)
739 {
740         Mask *mask_new;
741
742         mask_new = MEM_dupallocN(mask);
743
744         /*take care here! - we may want to copy anim data  */
745         mask_new->adt = NULL;
746
747         BLI_listbase_clear(&mask_new->masklayers);
748
749         BKE_mask_layer_copy_list(&mask_new->masklayers, &mask->masklayers);
750
751         /* enable fake user by default */
752         if (!(mask_new->id.flag & LIB_FAKEUSER)) {
753                 mask_new->id.flag |= LIB_FAKEUSER;
754                 mask_new->id.us++;
755         }
756
757         return mask_new;
758 }
759
760 Mask *BKE_mask_copy(Mask *mask)
761 {
762         Mask *mask_new;
763
764         mask_new = BKE_libblock_copy(&mask->id);
765
766         BLI_listbase_clear(&mask_new->masklayers);
767
768         BKE_mask_layer_copy_list(&mask_new->masklayers, &mask->masklayers);
769
770         /* enable fake user by default */
771         if (!(mask_new->id.flag & LIB_FAKEUSER)) {
772                 mask_new->id.flag |= LIB_FAKEUSER;
773                 mask_new->id.us++;
774         }
775
776         return mask_new;
777 }
778
779 void BKE_mask_point_free(MaskSplinePoint *point)
780 {
781         if (point->uw)
782                 MEM_freeN(point->uw);
783 }
784
785 void BKE_mask_spline_free(MaskSpline *spline)
786 {
787         int i = 0;
788
789         for (i = 0; i < spline->tot_point; i++) {
790                 MaskSplinePoint *point;
791                 point = &spline->points[i];
792                 BKE_mask_point_free(point);
793
794                 if (spline->points_deform) {
795                         point = &spline->points_deform[i];
796                         BKE_mask_point_free(point);
797                 }
798         }
799
800         MEM_freeN(spline->points);
801
802         if (spline->points_deform) {
803                 MEM_freeN(spline->points_deform);
804         }
805
806         MEM_freeN(spline);
807 }
808
809 void BKE_mask_spline_free_list(ListBase *splines)
810 {
811         MaskSpline *spline = splines->first;
812         while (spline) {
813                 MaskSpline *next_spline = spline->next;
814
815                 BLI_remlink(splines, spline);
816                 BKE_mask_spline_free(spline);
817
818                 spline = next_spline;
819         }
820 }
821
822 static MaskSplinePoint *mask_spline_points_copy(MaskSplinePoint *points, int tot_point)
823 {
824         MaskSplinePoint *npoints;
825         int i;
826
827         npoints = MEM_dupallocN(points);
828
829         for (i = 0; i < tot_point; i++) {
830                 MaskSplinePoint *point = &npoints[i];
831
832                 if (point->uw)
833                         point->uw = MEM_dupallocN(point->uw);
834         }
835
836         return npoints;
837 }
838
839 MaskSpline *BKE_mask_spline_copy(MaskSpline *spline)
840 {
841         MaskSpline *nspline = MEM_callocN(sizeof(MaskSpline), "new spline");
842
843         *nspline = *spline;
844
845         nspline->points_deform = NULL;
846         nspline->points = mask_spline_points_copy(spline->points, spline->tot_point);
847
848         if (spline->points_deform) {
849                 nspline->points_deform = mask_spline_points_copy(spline->points_deform, spline->tot_point);
850         }
851
852         return nspline;
853 }
854
855 /* note: does NOT add to the list */
856 MaskLayerShape *BKE_mask_layer_shape_alloc(MaskLayer *masklay, const int frame)
857 {
858         MaskLayerShape *masklay_shape;
859         int tot_vert = BKE_mask_layer_shape_totvert(masklay);
860
861         masklay_shape = MEM_mallocN(sizeof(MaskLayerShape), __func__);
862         masklay_shape->frame = frame;
863         masklay_shape->tot_vert = tot_vert;
864         masklay_shape->data = MEM_mallocN(tot_vert * sizeof(float) * MASK_OBJECT_SHAPE_ELEM_SIZE, __func__);
865
866         return masklay_shape;
867 }
868
869 void BKE_mask_layer_shape_free(MaskLayerShape *masklay_shape)
870 {
871         if (masklay_shape->data) {
872                 MEM_freeN(masklay_shape->data);
873         }
874
875         MEM_freeN(masklay_shape);
876 }
877
878 /** \brief Free all animation keys for a mask layer
879  */
880 void BKE_mask_layer_free_shapes(MaskLayer *masklay)
881 {
882         MaskLayerShape *masklay_shape;
883
884         /* free animation data */
885         masklay_shape = masklay->splines_shapes.first;
886         while (masklay_shape) {
887                 MaskLayerShape *next_masklay_shape = masklay_shape->next;
888
889                 BLI_remlink(&masklay->splines_shapes, masklay_shape);
890                 BKE_mask_layer_shape_free(masklay_shape);
891
892                 masklay_shape = next_masklay_shape;
893         }
894 }
895
896 void BKE_mask_layer_free(MaskLayer *masklay)
897 {
898         /* free splines */
899         BKE_mask_spline_free_list(&masklay->splines);
900
901         /* free animation data */
902         BKE_mask_layer_free_shapes(masklay);
903
904         MEM_freeN(masklay);
905 }
906
907 void BKE_mask_layer_free_list(ListBase *masklayers)
908 {
909         MaskLayer *masklay = masklayers->first;
910
911         while (masklay) {
912                 MaskLayer *masklay_next = masklay->next;
913
914                 BLI_remlink(masklayers, masklay);
915                 BKE_mask_layer_free(masklay);
916
917                 masklay = masklay_next;
918         }
919 }
920
921 /** free for temp copy, but don't manage unlinking from other pointers */
922 void BKE_mask_free_nolib(Mask *mask)
923 {
924         BKE_mask_layer_free_list(&mask->masklayers);
925 }
926
927 void BKE_mask_free(Main *bmain, Mask *mask)
928 {
929         bScreen *scr;
930         ScrArea *area;
931         SpaceLink *sl;
932         Scene *scene;
933
934         for (scr = bmain->screen.first; scr; scr = scr->id.next) {
935                 for (area = scr->areabase.first; area; area = area->next) {
936                         for (sl = area->spacedata.first; sl; sl = sl->next) {
937                                 switch (sl->spacetype) {
938                                         case SPACE_CLIP:
939                                         {
940                                                 SpaceClip *sc = (SpaceClip *)sl;
941
942                                                 if (sc->mask_info.mask == mask) {
943                                                         sc->mask_info.mask = NULL;
944                                                 }
945                                                 break;
946                                         }
947                                         case SPACE_IMAGE:
948                                         {
949                                                 SpaceImage *sima = (SpaceImage *)sl;
950
951                                                 if (sima->mask_info.mask == mask) {
952                                                         sima->mask_info.mask = NULL;
953                                                 }
954                                                 break;
955                                         }
956                                 }
957                         }
958                 }
959         }
960
961         for (scene = bmain->scene.first; scene; scene = scene->id.next) {
962                 if (scene->ed) {
963                         Sequence *seq;
964
965                         SEQ_BEGIN (scene->ed, seq)
966                         {
967                                 if (seq->mask == mask) {
968                                         seq->mask = NULL;
969                                 }
970                         }
971                         SEQ_END
972                 }
973         }
974
975         FOREACH_NODETREE(bmain, ntree, id) {
976                 BKE_node_tree_unlink_id((ID *)mask, ntree);
977         } FOREACH_NODETREE_END
978
979         /* free mask data */
980         BKE_mask_layer_free_list(&mask->masklayers);
981 }
982
983 void BKE_mask_coord_from_frame(float r_co[2], const float co[2], const float frame_size[2])
984 {
985         if (frame_size[0] == frame_size[1]) {
986                 r_co[0] = co[0];
987                 r_co[1] = co[1];
988         }
989         else if (frame_size[0] < frame_size[1]) {
990                 r_co[0] = ((co[0] - 0.5f) * (frame_size[0] / frame_size[1])) + 0.5f;
991                 r_co[1] = co[1];
992         }
993         else { /* (frame_size[0] > frame_size[1]) */
994                 r_co[0] = co[0];
995                 r_co[1] = ((co[1] - 0.5f) * (frame_size[1] / frame_size[0])) + 0.5f;
996         }
997 }
998
999 void BKE_mask_coord_from_movieclip(MovieClip *clip, MovieClipUser *user, float r_co[2], const float co[2])
1000 {
1001         float aspx, aspy;
1002         float frame_size[2];
1003
1004         /* scaling for the clip */
1005         BKE_movieclip_get_size_fl(clip, user, frame_size);
1006         BKE_movieclip_get_aspect(clip, &aspx, &aspy);
1007
1008         frame_size[1] *= (aspy / aspx);
1009
1010         BKE_mask_coord_from_frame(r_co, co, frame_size);
1011 }
1012
1013 void BKE_mask_coord_from_image(Image *image, ImageUser *iuser, float r_co[2], const float co[2])
1014 {
1015         float aspx, aspy;
1016         float frame_size[2];
1017
1018         BKE_image_get_size_fl(image, iuser, frame_size);
1019         BKE_image_get_aspect(image, &aspx, &aspy);
1020
1021         frame_size[1] *= (aspy / aspx);
1022
1023         BKE_mask_coord_from_frame(r_co, co, frame_size);
1024 }
1025
1026 /* as above but divide */
1027 void BKE_mask_coord_to_frame(float r_co[2], const float co[2], const float frame_size[2])
1028 {
1029         if (frame_size[0] == frame_size[1]) {
1030                 r_co[0] = co[0];
1031                 r_co[1] = co[1];
1032         }
1033         else if (frame_size[0] < frame_size[1]) {
1034                 r_co[0] = ((co[0] - 0.5f) / (frame_size[0] / frame_size[1])) + 0.5f;
1035                 r_co[1] = co[1];
1036         }
1037         else { /* (frame_size[0] > frame_size[1]) */
1038                 r_co[0] = co[0];
1039                 r_co[1] = ((co[1] - 0.5f) / (frame_size[1] / frame_size[0])) + 0.5f;
1040         }
1041 }
1042
1043 void BKE_mask_coord_to_movieclip(MovieClip *clip, MovieClipUser *user, float r_co[2], const float co[2])
1044 {
1045         float aspx, aspy;
1046         float frame_size[2];
1047
1048         /* scaling for the clip */
1049         BKE_movieclip_get_size_fl(clip, user, frame_size);
1050         BKE_movieclip_get_aspect(clip, &aspx, &aspy);
1051
1052         frame_size[1] /= (aspy / aspx);
1053
1054         BKE_mask_coord_to_frame(r_co, co, frame_size);
1055 }
1056
1057 void BKE_mask_coord_to_image(Image *image, ImageUser *iuser, float r_co[2], const float co[2])
1058 {
1059         float aspx, aspy;
1060         float frame_size[2];
1061
1062         /* scaling for the clip */
1063         BKE_image_get_size_fl(image, iuser, frame_size);
1064         BKE_image_get_aspect(image, &aspx, &aspy);
1065
1066         frame_size[1] /= (aspy / aspx);
1067
1068         BKE_mask_coord_to_frame(r_co, co, frame_size);
1069 }
1070
1071 void BKE_mask_point_parent_matrix_get(MaskSplinePoint *point, float ctime, float parent_matrix[3][3])
1072 {
1073         MaskParent *parent = &point->parent;
1074
1075         unit_m3(parent_matrix);
1076
1077         if (!parent) {
1078                 return;
1079         }
1080
1081         if (parent->id_type == ID_MC) {
1082                 if (parent->id) {
1083                         MovieClip *clip = (MovieClip *) parent->id;
1084                         MovieTracking *tracking = (MovieTracking *) &clip->tracking;
1085                         MovieTrackingObject *ob = BKE_tracking_object_get_named(tracking, parent->parent);
1086
1087                         if (ob) {
1088                                 MovieClipUser user = {0};
1089                                 float clip_framenr = BKE_movieclip_remap_scene_to_clip_frame(clip, ctime);
1090                                 BKE_movieclip_user_set_frame(&user, ctime);
1091
1092                                 if (parent->type == MASK_PARENT_POINT_TRACK) {
1093                                         MovieTrackingTrack *track = BKE_tracking_track_get_named(tracking, ob, parent->sub_parent);
1094
1095                                         if (track) {
1096                                                 float marker_position[2], parent_co[2];
1097                                                 BKE_tracking_marker_get_subframe_position(track, clip_framenr, marker_position);
1098                                                 BKE_mask_coord_from_movieclip(clip, &user, parent_co, marker_position);
1099                                                 sub_v2_v2v2(parent_matrix[2], parent_co, parent->parent_orig);
1100                                         }
1101                                 }
1102                                 else /* if (parent->type == MASK_PARENT_PLANE_TRACK) */ {
1103                                         MovieTrackingPlaneTrack *plane_track = BKE_tracking_plane_track_get_named(tracking, ob, parent->sub_parent);
1104
1105                                         if (plane_track) {
1106                                                 MovieTrackingPlaneMarker *plane_marker = BKE_tracking_plane_marker_get(plane_track, clip_framenr);
1107                                                 float aspx, aspy;
1108                                                 float frame_size[2], H[3][3], mask_from_clip_matrix[3][3], mask_to_clip_matrix[3][3];
1109
1110                                                 BKE_tracking_homography_between_two_quads(parent->parent_corners_orig, plane_marker->corners, H);
1111
1112                                                 unit_m3(mask_from_clip_matrix);
1113
1114                                                 BKE_movieclip_get_size_fl(clip, &user, frame_size);
1115                                                 BKE_movieclip_get_aspect(clip, &aspx, &aspy);
1116
1117                                                 frame_size[1] *= (aspy / aspx);
1118                                                 if (frame_size[0] == frame_size[1]) {
1119                                                         /* pass */
1120                                                 }
1121                                                 else if (frame_size[0] < frame_size[1]) {
1122                                                         mask_from_clip_matrix[0][0] = frame_size[1] / frame_size[0];
1123                                                         mask_from_clip_matrix[2][0] = -0.5f * (frame_size[1] / frame_size[0]) + 0.5f;
1124                                                 }
1125                                                 else { /* (frame_size[0] > frame_size[1]) */
1126                                                         mask_from_clip_matrix[1][1] = frame_size[1] / frame_size[0];
1127                                                         mask_from_clip_matrix[2][1] = -0.5f * (frame_size[1] / frame_size[0]) + 0.5f;
1128                                                 }
1129
1130                                                 invert_m3_m3(mask_to_clip_matrix, mask_from_clip_matrix);
1131                                                 mul_serie_m3(parent_matrix, mask_to_clip_matrix, H, mask_from_clip_matrix, NULL, NULL, NULL, NULL, NULL);
1132                                         }
1133                                 }
1134                         }
1135                 }
1136         }
1137 }
1138
1139 static void mask_evaluate_apply_point_parent(MaskSplinePoint *point, float ctime)
1140 {
1141         float parent_matrix[3][3];
1142
1143         BKE_mask_point_parent_matrix_get(point, ctime, parent_matrix);
1144
1145         mul_m3_v2(parent_matrix, point->bezt.vec[0]);
1146         mul_m3_v2(parent_matrix, point->bezt.vec[1]);
1147         mul_m3_v2(parent_matrix, point->bezt.vec[2]);
1148 }
1149
1150 static void mask_calc_point_handle(MaskSplinePoint *point, MaskSplinePoint *point_prev, MaskSplinePoint *point_next)
1151 {
1152         BezTriple *bezt = &point->bezt;
1153         BezTriple *bezt_prev = NULL, *bezt_next = NULL;
1154         //int handle_type = bezt->h1;
1155
1156         if (point_prev)
1157                 bezt_prev = &point_prev->bezt;
1158
1159         if (point_next)
1160                 bezt_next = &point_next->bezt;
1161
1162 #if 1
1163         if (bezt_prev || bezt_next) {
1164                 BKE_nurb_handle_calc(bezt, bezt_prev, bezt_next, 0);
1165         }
1166 #else
1167         if (handle_type == HD_VECT) {
1168                 BKE_nurb_handle_calc(bezt, bezt_prev, bezt_next, 0);
1169         }
1170         else if (handle_type == HD_AUTO) {
1171                 BKE_nurb_handle_calc(bezt, bezt_prev, bezt_next, 0);
1172         }
1173         else if (handle_type == HD_ALIGN) {
1174                 float v1[3], v2[3];
1175                 float vec[3], h[3];
1176
1177                 sub_v3_v3v3(v1, bezt->vec[0], bezt->vec[1]);
1178                 sub_v3_v3v3(v2, bezt->vec[2], bezt->vec[1]);
1179                 add_v3_v3v3(vec, v1, v2);
1180
1181                 if (len_squared_v3(vec) > (1e-3f * 1e-3f)) {
1182                         h[0] = vec[1];
1183                         h[1] = -vec[0];
1184                         h[2] = 0.0f;
1185                 }
1186                 else {
1187                         copy_v3_v3(h, v1);
1188                 }
1189
1190                 add_v3_v3v3(bezt->vec[0], bezt->vec[1], h);
1191                 sub_v3_v3v3(bezt->vec[2], bezt->vec[1], h);
1192         }
1193 #endif
1194 }
1195
1196 void BKE_mask_get_handle_point_adjacent(MaskSpline *spline, MaskSplinePoint *point,
1197                                         MaskSplinePoint **r_point_prev, MaskSplinePoint **r_point_next)
1198 {
1199         /* TODO, could avoid calling this at such low level */
1200         MaskSplinePoint *points_array = BKE_mask_spline_point_array_from_point(spline, point);
1201
1202         *r_point_prev = mask_spline_point_prev(spline, points_array, point);
1203         *r_point_next = mask_spline_point_next(spline, points_array, point);
1204 }
1205
1206 /* calculates the tangent of a point by its previous and next
1207  * (ignoring handles - as if its a poly line) */
1208 void BKE_mask_calc_tangent_polyline(MaskSpline *spline, MaskSplinePoint *point, float t[2])
1209 {
1210         float tvec_a[2], tvec_b[2];
1211
1212         MaskSplinePoint *point_prev, *point_next;
1213
1214         BKE_mask_get_handle_point_adjacent(spline, point,
1215                                            &point_prev, &point_next);
1216
1217         if (point_prev) {
1218                 sub_v2_v2v2(tvec_a, point->bezt.vec[1], point_prev->bezt.vec[1]);
1219                 normalize_v2(tvec_a);
1220         }
1221         else {
1222                 zero_v2(tvec_a);
1223         }
1224
1225         if (point_next) {
1226                 sub_v2_v2v2(tvec_b, point_next->bezt.vec[1], point->bezt.vec[1]);
1227                 normalize_v2(tvec_b);
1228         }
1229         else {
1230                 zero_v2(tvec_b);
1231         }
1232
1233         add_v2_v2v2(t, tvec_a, tvec_b);
1234         normalize_v2(t);
1235 }
1236
1237 void BKE_mask_calc_handle_point(MaskSpline *spline, MaskSplinePoint *point)
1238 {
1239         MaskSplinePoint *point_prev, *point_next;
1240
1241         BKE_mask_get_handle_point_adjacent(spline, point,
1242                                            &point_prev, &point_next);
1243
1244         mask_calc_point_handle(point, point_prev, point_next);
1245 }
1246
1247 void BKE_mask_calc_handle_adjacent_interp(MaskSpline *spline, MaskSplinePoint *point, const float u)
1248 {
1249         /* TODO! - make this interpolate between siblings - not always midpoint! */
1250         int length_tot = 0;
1251         float length_average = 0.0f;
1252         float weight_average = 0.0f;
1253
1254
1255         MaskSplinePoint *point_prev, *point_next;
1256
1257         BLI_assert(u >= 0.0f && u <= 1.0f);
1258
1259         BKE_mask_get_handle_point_adjacent(spline, point,
1260                                            &point_prev, &point_next);
1261
1262         if (point_prev && point_next) {
1263                 length_average = ((len_v2v2(point_prev->bezt.vec[0], point_prev->bezt.vec[1]) * (1.0f - u)) +
1264                                   (len_v2v2(point_next->bezt.vec[2], point_next->bezt.vec[1]) * u));
1265
1266                 weight_average = (point_prev->bezt.weight * (1.0f - u) +
1267                                   point_next->bezt.weight * u);
1268                 length_tot = 1;
1269         }
1270         else {
1271                 if (point_prev) {
1272                         length_average += len_v2v2(point_prev->bezt.vec[0], point_prev->bezt.vec[1]);
1273                         weight_average += point_prev->bezt.weight;
1274                         length_tot++;
1275                 }
1276
1277                 if (point_next) {
1278                         length_average += len_v2v2(point_next->bezt.vec[2], point_next->bezt.vec[1]);
1279                         weight_average += point_next->bezt.weight;
1280                         length_tot++;
1281                 }
1282         }
1283
1284         if (length_tot) {
1285                 length_average /= (float)length_tot;
1286                 weight_average /= (float)length_tot;
1287
1288                 dist_ensure_v2_v2fl(point->bezt.vec[0], point->bezt.vec[1], length_average);
1289                 dist_ensure_v2_v2fl(point->bezt.vec[2], point->bezt.vec[1], length_average);
1290                 point->bezt.weight = weight_average;
1291         }
1292 }
1293
1294
1295 /**
1296  * \brief Resets auto handles even for non-auto bezier points
1297  *
1298  * Useful for giving sane defaults.
1299  */
1300 void BKE_mask_calc_handle_point_auto(MaskSpline *spline, MaskSplinePoint *point,
1301                                      const bool do_recalc_length)
1302 {
1303         MaskSplinePoint *point_prev, *point_next;
1304         const char h_back[2] = {point->bezt.h1, point->bezt.h2};
1305         const float length_average = (do_recalc_length) ? 0.0f /* dummy value */ :
1306                                      (len_v3v3(point->bezt.vec[0], point->bezt.vec[1]) +
1307                                       len_v3v3(point->bezt.vec[1], point->bezt.vec[2])) / 2.0f;
1308
1309         BKE_mask_get_handle_point_adjacent(spline, point,
1310                                            &point_prev, &point_next);
1311
1312         point->bezt.h1 = HD_AUTO;
1313         point->bezt.h2 = HD_AUTO;
1314         mask_calc_point_handle(point, point_prev, point_next);
1315
1316         point->bezt.h1 = h_back[0];
1317         point->bezt.h2 = h_back[1];
1318
1319         /* preserve length by applying it back */
1320         if (do_recalc_length == FALSE) {
1321                 dist_ensure_v2_v2fl(point->bezt.vec[0], point->bezt.vec[1], length_average);
1322                 dist_ensure_v2_v2fl(point->bezt.vec[2], point->bezt.vec[1], length_average);
1323         }
1324 }
1325
1326 void BKE_mask_layer_calc_handles(MaskLayer *masklay)
1327 {
1328         MaskSpline *spline;
1329         for (spline = masklay->splines.first; spline; spline = spline->next) {
1330                 int i;
1331                 for (i = 0; i < spline->tot_point; i++) {
1332                         BKE_mask_calc_handle_point(spline, &spline->points[i]);
1333                 }
1334         }
1335 }
1336
1337 void BKE_mask_spline_ensure_deform(MaskSpline *spline)
1338 {
1339         int allocated_points = (MEM_allocN_len(spline->points_deform) / sizeof(*spline->points_deform));
1340         // printf("SPLINE ALLOC %p %d\n", spline->points_deform, allocated_points);
1341
1342         if (spline->points_deform == NULL || allocated_points != spline->tot_point) {
1343                 // printf("alloc new deform spline\n");
1344
1345                 if (spline->points_deform) {
1346                         int i;
1347
1348                         for (i = 0; i < allocated_points; i++) {
1349                                 MaskSplinePoint *point = &spline->points_deform[i];
1350                                 BKE_mask_point_free(point);
1351                         }
1352
1353                         MEM_freeN(spline->points_deform);
1354                 }
1355
1356                 spline->points_deform = MEM_callocN(sizeof(*spline->points_deform) * spline->tot_point, __func__);
1357         }
1358         else {
1359                 // printf("alloc spline done\n");
1360         }
1361 }
1362
1363 void BKE_mask_layer_evaluate(MaskLayer *masklay, const float ctime, const bool do_newframe)
1364 {
1365         /* animation if available */
1366         if (do_newframe) {
1367                 MaskLayerShape *masklay_shape_a;
1368                 MaskLayerShape *masklay_shape_b;
1369                 int found;
1370
1371                 if ((found = BKE_mask_layer_shape_find_frame_range(masklay, ctime,
1372                                                                    &masklay_shape_a, &masklay_shape_b)))
1373                 {
1374                         if (found == 1) {
1375 #if 0
1376                                 printf("%s: exact %d %d (%d)\n", __func__, (int)ctime, BLI_countlist(&masklay->splines_shapes),
1377                                        masklay_shape_a->frame);
1378 #endif
1379
1380                                 BKE_mask_layer_shape_to_mask(masklay, masklay_shape_a);
1381                         }
1382                         else if (found == 2) {
1383                                 float w = masklay_shape_b->frame - masklay_shape_a->frame;
1384 #if 0
1385                                 printf("%s: tween %d %d (%d %d)\n", __func__, (int)ctime, BLI_countlist(&masklay->splines_shapes),
1386                                        masklay_shape_a->frame, masklay_shape_b->frame);
1387 #endif
1388                                 BKE_mask_layer_shape_to_mask_interp(masklay, masklay_shape_a, masklay_shape_b,
1389                                                                     (ctime - masklay_shape_a->frame) / w);
1390                         }
1391                         else {
1392                                 /* always fail, should never happen */
1393                                 BLI_assert(found == 2);
1394                         }
1395                 }
1396         }
1397         /* animation done... */
1398
1399         BKE_mask_layer_calc_handles(masklay);
1400
1401         /* update deform */
1402         {
1403                 MaskSpline *spline;
1404
1405                 for (spline = masklay->splines.first; spline; spline = spline->next) {
1406                         int i;
1407                         int need_handle_recalc = FALSE;
1408
1409                         BKE_mask_spline_ensure_deform(spline);
1410
1411                         for (i = 0; i < spline->tot_point; i++) {
1412                                 MaskSplinePoint *point = &spline->points[i];
1413                                 MaskSplinePoint *point_deform = &spline->points_deform[i];
1414
1415                                 BKE_mask_point_free(point_deform);
1416
1417                                 *point_deform = *point;
1418                                 point_deform->uw = point->uw ? MEM_dupallocN(point->uw) : NULL;
1419
1420                                 mask_evaluate_apply_point_parent(point_deform, ctime);
1421
1422                                 if (ELEM(point->bezt.h1, HD_AUTO, HD_VECT)) {
1423                                         need_handle_recalc = TRUE;
1424                                 }
1425                         }
1426
1427                         /* if the spline has auto or vector handles, these need to be recalculated after deformation */
1428                         if (need_handle_recalc) {
1429                                 for (i = 0; i < spline->tot_point; i++) {
1430                                         MaskSplinePoint *point_deform = &spline->points_deform[i];
1431                                         if (ELEM(point_deform->bezt.h1, HD_AUTO, HD_VECT)) {
1432                                                 BKE_mask_calc_handle_point(spline, point_deform);
1433                                         }
1434                                 }
1435                         }
1436                         /* end extra calc handles loop */
1437                 }
1438         }
1439 }
1440
1441 void BKE_mask_evaluate(Mask *mask, const float ctime, const bool do_newframe)
1442 {
1443         MaskLayer *masklay;
1444
1445         for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) {
1446                 BKE_mask_layer_evaluate(masklay, ctime, do_newframe);
1447         }
1448 }
1449
1450 /* the purpose of this function is to ensure spline->points_deform is never out of date.
1451  * for now re-evaluate all. eventually this might work differently */
1452 void BKE_mask_update_display(Mask *mask, float ctime)
1453 {
1454 #if 0
1455         MaskLayer *masklay;
1456
1457         for (masklay = mask->masklayers.first; masklay; masklay = masklay->next) {
1458                 MaskSpline *spline;
1459
1460                 for (spline = masklay->splines.first; spline; spline = spline->next) {
1461                         if (spline->points_deform) {
1462                                 int i = 0;
1463
1464                                 for (i = 0; i < spline->tot_point; i++) {
1465                                         MaskSplinePoint *point;
1466
1467                                         if (spline->points_deform) {
1468                                                 point = &spline->points_deform[i];
1469                                                 BKE_mask_point_free(point);
1470                                         }
1471                                 }
1472                                 if (spline->points_deform) {
1473                                         MEM_freeN(spline->points_deform);
1474                                 }
1475
1476                                 spline->points_deform = NULL;
1477                         }
1478                 }
1479         }
1480 #endif
1481
1482         BKE_mask_evaluate(mask, ctime, false);
1483 }
1484
1485 void BKE_mask_evaluate_all_masks(Main *bmain, float ctime, const bool do_newframe)
1486 {
1487         Mask *mask;
1488
1489         for (mask = bmain->mask.first; mask; mask = mask->id.next) {
1490                 BKE_mask_evaluate(mask, ctime, do_newframe);
1491         }
1492 }
1493
1494 void BKE_mask_update_scene(Main *bmain, Scene *scene)
1495 {
1496         Mask *mask;
1497
1498         for (mask = bmain->mask.first; mask; mask = mask->id.next) {
1499                 if (mask->id.flag & (LIB_ID_RECALC | LIB_ID_RECALC_DATA)) {
1500                         bool do_new_frame = (mask->id.flag & LIB_ID_RECALC_DATA) != 0;
1501                         BKE_mask_evaluate_all_masks(bmain, CFRA, do_new_frame);
1502                 }
1503         }
1504 }
1505
1506 void BKE_mask_parent_init(MaskParent *parent)
1507 {
1508         parent->id_type = ID_MC;
1509 }
1510
1511
1512 /* *** own animation/shapekey implementation ***
1513  * BKE_mask_layer_shape_XXX */
1514
1515 int BKE_mask_layer_shape_totvert(MaskLayer *masklay)
1516 {
1517         int tot = 0;
1518         MaskSpline *spline;
1519
1520         for (spline = masklay->splines.first; spline; spline = spline->next) {
1521                 tot += spline->tot_point;
1522         }
1523
1524         return tot;
1525 }
1526
1527 static void mask_layer_shape_from_mask_point(BezTriple *bezt, float fp[MASK_OBJECT_SHAPE_ELEM_SIZE])
1528 {
1529         copy_v2_v2(&fp[0], bezt->vec[0]);
1530         copy_v2_v2(&fp[2], bezt->vec[1]);
1531         copy_v2_v2(&fp[4], bezt->vec[2]);
1532         fp[6] = bezt->weight;
1533         fp[7] = bezt->radius;
1534 }
1535
1536 static void mask_layer_shape_to_mask_point(BezTriple *bezt, float fp[MASK_OBJECT_SHAPE_ELEM_SIZE])
1537 {
1538         copy_v2_v2(bezt->vec[0], &fp[0]);
1539         copy_v2_v2(bezt->vec[1], &fp[2]);
1540         copy_v2_v2(bezt->vec[2], &fp[4]);
1541         bezt->weight = fp[6];
1542         bezt->radius = fp[7];
1543 }
1544
1545 /* these functions match. copy is swapped */
1546 void BKE_mask_layer_shape_from_mask(MaskLayer *masklay, MaskLayerShape *masklay_shape)
1547 {
1548         int tot = BKE_mask_layer_shape_totvert(masklay);
1549
1550         if (masklay_shape->tot_vert == tot) {
1551                 float *fp = masklay_shape->data;
1552
1553                 MaskSpline *spline;
1554                 for (spline = masklay->splines.first; spline; spline = spline->next) {
1555                         int i;
1556                         for (i = 0; i < spline->tot_point; i++) {
1557                                 mask_layer_shape_from_mask_point(&spline->points[i].bezt, fp);
1558                                 fp += MASK_OBJECT_SHAPE_ELEM_SIZE;
1559                         }
1560                 }
1561         }
1562         else {
1563                 printf("%s: vert mismatch %d != %d (frame %d)\n",
1564                        __func__, masklay_shape->tot_vert, tot, masklay_shape->frame);
1565         }
1566 }
1567
1568 void BKE_mask_layer_shape_to_mask(MaskLayer *masklay, MaskLayerShape *masklay_shape)
1569 {
1570         int tot = BKE_mask_layer_shape_totvert(masklay);
1571
1572         if (masklay_shape->tot_vert == tot) {
1573                 float *fp = masklay_shape->data;
1574
1575                 MaskSpline *spline;
1576                 for (spline = masklay->splines.first; spline; spline = spline->next) {
1577                         int i;
1578                         for (i = 0; i < spline->tot_point; i++) {
1579                                 mask_layer_shape_to_mask_point(&spline->points[i].bezt, fp);
1580                                 fp += MASK_OBJECT_SHAPE_ELEM_SIZE;
1581                         }
1582                 }
1583         }
1584         else {
1585                 printf("%s: vert mismatch %d != %d (frame %d)\n",
1586                        __func__, masklay_shape->tot_vert, tot, masklay_shape->frame);
1587         }
1588 }
1589
1590 BLI_INLINE void interp_v2_v2v2_flfl(float target[2], const float a[2], const float b[2],
1591                                     const float t, const float s)
1592 {
1593         target[0] = s * a[0] + t * b[0];
1594         target[1] = s * a[1] + t * b[1];
1595 }
1596
1597 /* linear interpolation only */
1598 void BKE_mask_layer_shape_to_mask_interp(MaskLayer *masklay,
1599                                          MaskLayerShape *masklay_shape_a,
1600                                          MaskLayerShape *masklay_shape_b,
1601                                          const float fac)
1602 {
1603         int tot = BKE_mask_layer_shape_totvert(masklay);
1604         if (masklay_shape_a->tot_vert == tot && masklay_shape_b->tot_vert == tot) {
1605                 float *fp_a = masklay_shape_a->data;
1606                 float *fp_b = masklay_shape_b->data;
1607                 const float ifac = 1.0f - fac;
1608
1609                 MaskSpline *spline;
1610                 for (spline = masklay->splines.first; spline; spline = spline->next) {
1611                         int i;
1612                         for (i = 0; i < spline->tot_point; i++) {
1613                                 BezTriple *bezt = &spline->points[i].bezt;
1614                                 /* *** BKE_mask_layer_shape_from_mask - swapped *** */
1615                                 interp_v2_v2v2_flfl(bezt->vec[0], fp_a, fp_b, fac, ifac); fp_a += 2; fp_b += 2;
1616                                 interp_v2_v2v2_flfl(bezt->vec[1], fp_a, fp_b, fac, ifac); fp_a += 2; fp_b += 2;
1617                                 interp_v2_v2v2_flfl(bezt->vec[2], fp_a, fp_b, fac, ifac); fp_a += 2; fp_b += 2;
1618                                 bezt->weight = (fp_a[0] * ifac) + (fp_b[0] * fac);
1619                                 bezt->radius = (fp_a[1] * ifac) + (fp_b[1] * fac); fp_a += 2; fp_b += 2;
1620                         }
1621                 }
1622         }
1623         else {
1624                 printf("%s: vert mismatch %d != %d != %d (frame %d - %d)\n",
1625                        __func__, masklay_shape_a->tot_vert, masklay_shape_b->tot_vert, tot,
1626                        masklay_shape_a->frame, masklay_shape_b->frame);
1627         }
1628 }
1629
1630 MaskLayerShape *BKE_mask_layer_shape_find_frame(MaskLayer *masklay, const int frame)
1631 {
1632         MaskLayerShape *masklay_shape;
1633
1634         for (masklay_shape = masklay->splines_shapes.first;
1635              masklay_shape;
1636              masklay_shape = masklay_shape->next)
1637         {
1638                 if (frame == masklay_shape->frame) {
1639                         return masklay_shape;
1640                 }
1641                 else if (frame < masklay_shape->frame) {
1642                         break;
1643                 }
1644         }
1645
1646         return NULL;
1647 }
1648
1649 /* when returning 2 - the frame isnt found but before/after frames are */
1650 int BKE_mask_layer_shape_find_frame_range(MaskLayer *masklay, const float frame,
1651                                           MaskLayerShape **r_masklay_shape_a,
1652                                           MaskLayerShape **r_masklay_shape_b)
1653 {
1654         MaskLayerShape *masklay_shape;
1655
1656         for (masklay_shape = masklay->splines_shapes.first;
1657              masklay_shape;
1658              masklay_shape = masklay_shape->next)
1659         {
1660                 if (frame == masklay_shape->frame) {
1661                         *r_masklay_shape_a = masklay_shape;
1662                         *r_masklay_shape_b = NULL;
1663                         return 1;
1664                 }
1665                 else if (frame < masklay_shape->frame) {
1666                         if (masklay_shape->prev) {
1667                                 *r_masklay_shape_a = masklay_shape->prev;
1668                                 *r_masklay_shape_b = masklay_shape;
1669                                 return 2;
1670                         }
1671                         else {
1672                                 *r_masklay_shape_a = masklay_shape;
1673                                 *r_masklay_shape_b = NULL;
1674                                 return 1;
1675                         }
1676                 }
1677         }
1678
1679         if ((masklay_shape = masklay->splines_shapes.last)) {
1680                 *r_masklay_shape_a = masklay_shape;
1681                 *r_masklay_shape_b = NULL;
1682                 return 1;
1683         }
1684         else {
1685                 *r_masklay_shape_a = NULL;
1686                 *r_masklay_shape_b = NULL;
1687
1688                 return 0;
1689         }
1690 }
1691
1692 MaskLayerShape *BKE_mask_layer_shape_verify_frame(MaskLayer *masklay, const int frame)
1693 {
1694         MaskLayerShape *masklay_shape;
1695
1696         masklay_shape = BKE_mask_layer_shape_find_frame(masklay, frame);
1697
1698         if (masklay_shape == NULL) {
1699                 masklay_shape = BKE_mask_layer_shape_alloc(masklay, frame);
1700                 BLI_addtail(&masklay->splines_shapes, masklay_shape);
1701                 BKE_mask_layer_shape_sort(masklay);
1702         }
1703
1704 #if 0
1705         {
1706                 MaskLayerShape *masklay_shape;
1707                 int i = 0;
1708                 for (masklay_shape = masklay->splines_shapes.first;
1709                      masklay_shape;
1710                      masklay_shape = masklay_shape->next)
1711                 {
1712                         printf("mask %d, %d\n", i++, masklay_shape->frame);
1713                 }
1714         }
1715 #endif
1716
1717         return masklay_shape;
1718 }
1719
1720 MaskLayerShape *BKE_mask_layer_shape_duplicate(MaskLayerShape *masklay_shape)
1721 {
1722         MaskLayerShape *masklay_shape_copy;
1723
1724         masklay_shape_copy = MEM_dupallocN(masklay_shape);
1725
1726         if (LIKELY(masklay_shape_copy->data)) {
1727                 masklay_shape_copy->data = MEM_dupallocN(masklay_shape_copy->data);
1728         }
1729
1730         return masklay_shape_copy;
1731 }
1732
1733 void BKE_mask_layer_shape_unlink(MaskLayer *masklay, MaskLayerShape *masklay_shape)
1734 {
1735         BLI_remlink(&masklay->splines_shapes, masklay_shape);
1736
1737         BKE_mask_layer_shape_free(masklay_shape);
1738 }
1739
1740 static int mask_layer_shape_sort_cb(void *masklay_shape_a_ptr, void *masklay_shape_b_ptr)
1741 {
1742         MaskLayerShape *masklay_shape_a = (MaskLayerShape *)masklay_shape_a_ptr;
1743         MaskLayerShape *masklay_shape_b = (MaskLayerShape *)masklay_shape_b_ptr;
1744
1745         if      (masklay_shape_a->frame < masklay_shape_b->frame)  return -1;
1746         else if (masklay_shape_a->frame > masklay_shape_b->frame)  return  1;
1747         else                                                       return  0;
1748 }
1749
1750 void BKE_mask_layer_shape_sort(MaskLayer *masklay)
1751 {
1752         BLI_sortlist(&masklay->splines_shapes, mask_layer_shape_sort_cb);
1753 }
1754
1755 bool BKE_mask_layer_shape_spline_from_index(MaskLayer *masklay, int index,
1756                                             MaskSpline **r_masklay_shape, int *r_index)
1757 {
1758         MaskSpline *spline;
1759
1760         for (spline = masklay->splines.first; spline; spline = spline->next) {
1761                 if (index < spline->tot_point) {
1762                         *r_masklay_shape = spline;
1763                         *r_index = index;
1764                         return true;
1765                 }
1766                 index -= spline->tot_point;
1767         }
1768
1769         return false;
1770 }
1771
1772 int BKE_mask_layer_shape_spline_to_index(MaskLayer *masklay, MaskSpline *spline)
1773 {
1774         MaskSpline *spline_iter;
1775         int i_abs = 0;
1776         for (spline_iter = masklay->splines.first;
1777              spline_iter && spline_iter != spline;
1778              i_abs += spline_iter->tot_point, spline_iter = spline_iter->next)
1779         {
1780                 /* pass */
1781         }
1782
1783         return i_abs;
1784 }
1785
1786 /* basic 2D interpolation functions, could make more comprehensive later */
1787 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])
1788 {
1789         float pt_on_line[2];
1790         r_uv[0] = closest_to_line_v2(pt_on_line, pt, pt_a, pt_b);
1791         r_uv[1] = (len_v2v2(pt_on_line, pt) / len_v2v2(pt_a, pt_b)) *
1792                   ((line_point_side_v2(pt_a, pt_b, pt) < 0.0f) ? -1.0f : 1.0f);  /* this line only sets the sign */
1793 }
1794
1795
1796 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])
1797 {
1798         const float dvec[2] = {pt_b[0] - pt_a[0],
1799                                pt_b[1] - pt_a[1]};
1800
1801         /* u */
1802         madd_v2_v2v2fl(r_pt, pt_a, dvec, uv[0]);
1803
1804         /* v */
1805         r_pt[0] += -dvec[1] * uv[1];
1806         r_pt[1] +=  dvec[0] * uv[1];
1807 }
1808
1809 /* when a new points added - resize all shapekey array  */
1810 void BKE_mask_layer_shape_changed_add(MaskLayer *masklay, int index,
1811                                       bool do_init, bool do_init_interpolate)
1812 {
1813         MaskLayerShape *masklay_shape;
1814
1815         /* spline index from masklay */
1816         MaskSpline *spline;
1817         int spline_point_index;
1818
1819         if (BKE_mask_layer_shape_spline_from_index(masklay, index,
1820                                                    &spline, &spline_point_index))
1821         {
1822                 /* sanity check */
1823                 /* the point has already been removed in this array so subtract one when comparing with the shapes */
1824                 int tot = BKE_mask_layer_shape_totvert(masklay) - 1;
1825
1826                 /* for interpolation */
1827                 /* TODO - assumes closed curve for now */
1828                 float uv[3][2]; /* 3x 2D handles */
1829                 const int pi_curr =   spline_point_index;
1830                 const int pi_prev = ((spline_point_index - 1) + spline->tot_point) % spline->tot_point;
1831                 const int pi_next =  (spline_point_index + 1)                      % spline->tot_point;
1832
1833                 const int index_offset = index - spline_point_index;
1834                 /* const int pi_curr_abs = index; */
1835                 const int pi_prev_abs = pi_prev + index_offset;
1836                 const int pi_next_abs = pi_next + index_offset;
1837
1838                 int i;
1839                 if (do_init_interpolate) {
1840                         for (i = 0; i < 3; i++) {
1841                                 interp_weights_uv_v2_calc(uv[i],
1842                                                           spline->points[pi_curr].bezt.vec[i],
1843                                                           spline->points[pi_prev].bezt.vec[i],
1844                                                           spline->points[pi_next].bezt.vec[i]);
1845                         }
1846                 }
1847
1848                 for (masklay_shape = masklay->splines_shapes.first;
1849                      masklay_shape;
1850                      masklay_shape = masklay_shape->next)
1851                 {
1852                         if (tot == masklay_shape->tot_vert) {
1853                                 float *data_resized;
1854
1855                                 masklay_shape->tot_vert++;
1856                                 data_resized = MEM_mallocN(masklay_shape->tot_vert * sizeof(float) * MASK_OBJECT_SHAPE_ELEM_SIZE, __func__);
1857                                 if (index > 0) {
1858                                         memcpy(data_resized,
1859                                                masklay_shape->data,
1860                                                index * sizeof(float) * MASK_OBJECT_SHAPE_ELEM_SIZE);
1861                                 }
1862
1863                                 if (index != masklay_shape->tot_vert - 1) {
1864                                         memcpy(&data_resized[(index + 1) * MASK_OBJECT_SHAPE_ELEM_SIZE],
1865                                                masklay_shape->data + (index * MASK_OBJECT_SHAPE_ELEM_SIZE),
1866                                                (masklay_shape->tot_vert - (index + 1)) * sizeof(float) * MASK_OBJECT_SHAPE_ELEM_SIZE);
1867                                 }
1868
1869                                 if (do_init) {
1870                                         float *fp = &data_resized[index * MASK_OBJECT_SHAPE_ELEM_SIZE];
1871
1872                                         mask_layer_shape_from_mask_point(&spline->points[spline_point_index].bezt, fp);
1873
1874                                         if (do_init_interpolate && spline->tot_point > 2) {
1875                                                 for (i = 0; i < 3; i++) {
1876                                                         interp_weights_uv_v2_apply(uv[i],
1877                                                                                    &fp[i * 2],
1878                                                                                    &data_resized[(pi_prev_abs * MASK_OBJECT_SHAPE_ELEM_SIZE) + (i * 2)],
1879                                                                                    &data_resized[(pi_next_abs * MASK_OBJECT_SHAPE_ELEM_SIZE) + (i * 2)]);
1880                                                 }
1881                                         }
1882                                 }
1883                                 else {
1884                                         memset(&data_resized[index * MASK_OBJECT_SHAPE_ELEM_SIZE],
1885                                                0,
1886                                                sizeof(float) * MASK_OBJECT_SHAPE_ELEM_SIZE);
1887                                 }
1888
1889                                 MEM_freeN(masklay_shape->data);
1890                                 masklay_shape->data = data_resized;
1891                         }
1892                         else {
1893                                 printf("%s: vert mismatch %d != %d (frame %d)\n",
1894                                        __func__, masklay_shape->tot_vert, tot, masklay_shape->frame);
1895                         }
1896                 }
1897         }
1898 }
1899
1900
1901 /* move array to account for removed point */
1902 void BKE_mask_layer_shape_changed_remove(MaskLayer *masklay, int index, int count)
1903 {
1904         MaskLayerShape *masklay_shape;
1905
1906         /* the point has already been removed in this array so add one when comparing with the shapes */
1907         int tot = BKE_mask_layer_shape_totvert(masklay);
1908
1909         for (masklay_shape = masklay->splines_shapes.first;
1910              masklay_shape;
1911              masklay_shape = masklay_shape->next)
1912         {
1913                 if (tot == masklay_shape->tot_vert - count) {
1914                         float *data_resized;
1915
1916                         masklay_shape->tot_vert -= count;
1917                         data_resized = MEM_mallocN(masklay_shape->tot_vert * sizeof(float) * MASK_OBJECT_SHAPE_ELEM_SIZE, __func__);
1918                         if (index > 0) {
1919                                 memcpy(data_resized,
1920                                        masklay_shape->data,
1921                                        index * sizeof(float) * MASK_OBJECT_SHAPE_ELEM_SIZE);
1922                         }
1923
1924                         if (index != masklay_shape->tot_vert) {
1925                                 memcpy(&data_resized[index * MASK_OBJECT_SHAPE_ELEM_SIZE],
1926                                        masklay_shape->data + ((index + count) * MASK_OBJECT_SHAPE_ELEM_SIZE),
1927                                        (masklay_shape->tot_vert - index) * sizeof(float) * MASK_OBJECT_SHAPE_ELEM_SIZE);
1928                         }
1929
1930                         MEM_freeN(masklay_shape->data);
1931                         masklay_shape->data = data_resized;
1932                 }
1933                 else {
1934                         printf("%s: vert mismatch %d != %d (frame %d)\n",
1935                                __func__, masklay_shape->tot_vert - count, tot, masklay_shape->frame);
1936                 }
1937         }
1938 }
1939
1940 int BKE_mask_get_duration(Mask *mask)
1941 {
1942         return max_ii(1, mask->efra - mask->sfra);
1943 }
1944
1945 /*********************** clipboard *************************/
1946
1947 static void mask_clipboard_free_ex(bool final_free)
1948 {
1949         BKE_mask_spline_free_list(&mask_clipboard.splines);
1950         BLI_listbase_clear(&mask_clipboard.splines);
1951         if (mask_clipboard.id_hash) {
1952                 if (final_free) {
1953                         BLI_ghash_free(mask_clipboard.id_hash, NULL, MEM_freeN);
1954                 }
1955                 else {
1956                         BLI_ghash_clear(mask_clipboard.id_hash, NULL, MEM_freeN);
1957                 }
1958         }
1959 }
1960
1961 /* Free the clipboard. */
1962 void BKE_mask_clipboard_free(void)
1963 {
1964         mask_clipboard_free_ex(true);
1965 }
1966
1967 /* Copy selected visible splines from the given layer to clipboard. */
1968 void BKE_mask_clipboard_copy_from_layer(MaskLayer *mask_layer)
1969 {
1970         MaskSpline *spline;
1971
1972         /* Nothing to do if selection if disabled for the given layer. */
1973         if (mask_layer->restrictflag & MASK_RESTRICT_SELECT) {
1974                 return;
1975         }
1976
1977         mask_clipboard_free_ex(false);
1978         if (mask_clipboard.id_hash == NULL) {
1979                 mask_clipboard.id_hash = BLI_ghash_ptr_new("mask clipboard ID hash");
1980         }
1981
1982         for (spline = mask_layer->splines.first; spline; spline = spline->next) {
1983                 if (spline->flag & SELECT) {
1984                         MaskSpline *spline_new = BKE_mask_spline_copy(spline);
1985                         int i;
1986                         for (i = 0; i < spline_new->tot_point; i++) {
1987                                 MaskSplinePoint *point = &spline_new->points[i];
1988                                 if (point->parent.id) {
1989                                         if (!BLI_ghash_lookup(mask_clipboard.id_hash, point->parent.id)) {
1990                                                 int len = strlen(point->parent.id->name);
1991                                                 char *name_copy = MEM_mallocN(len + 1, "mask clipboard ID name");
1992                                                 strcpy(name_copy, point->parent.id->name);
1993                                                 BLI_ghash_insert(mask_clipboard.id_hash,
1994                                                                  point->parent.id,
1995                                                                  name_copy);
1996                                         }
1997                                 }
1998                         }
1999
2000                         BLI_addtail(&mask_clipboard.splines, spline_new);
2001                 }
2002         }
2003 }
2004
2005 /* Check clipboard is empty. */
2006 bool BKE_mask_clipboard_is_empty(void)
2007 {
2008         return BLI_listbase_is_empty(&mask_clipboard.splines);
2009 }
2010
2011 /* Paste the contents of clipboard to given mask layer */
2012 void BKE_mask_clipboard_paste_to_layer(Main *bmain, MaskLayer *mask_layer)
2013 {
2014         MaskSpline *spline;
2015
2016         for (spline = mask_clipboard.splines.first; spline; spline = spline->next) {
2017                 MaskSpline *spline_new = BKE_mask_spline_copy(spline);
2018                 int i;
2019
2020                 for (i = 0; i < spline_new->tot_point; i++) {
2021                         MaskSplinePoint *point = &spline_new->points[i];
2022                         if (point->parent.id) {
2023                                 char *id_name = BLI_ghash_lookup(mask_clipboard.id_hash, point->parent.id);
2024                                 ListBase *listbase;
2025
2026                                 BLI_assert(id_name != NULL);
2027
2028                                 listbase = which_libbase(bmain, GS(id_name));
2029                                 point->parent.id = BLI_findstring(listbase, id_name + 2, offsetof(ID, name) + 2);
2030                         }
2031                 }
2032
2033                 BLI_addtail(&mask_layer->splines, spline_new);
2034         }
2035 }