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