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