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