Blender V5.3
transform_convert_mask.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "DNA_mask_types.h"
10#include "DNA_space_types.h"
11
12#include "MEM_guardedalloc.h"
13
14#include "BLI_listbase.hh"
15#include "BLI_math_matrix_c.hh"
16#include "BLI_math_vector_c.hh"
17
18#include "BKE_compositor.hh"
19#include "BKE_context.hh"
20#include "BKE_mask.hh"
21
22#include "ED_clip.hh"
23#include "ED_image.hh"
24#include "ED_mask.hh"
25
26#include "ANIM_keyframing.hh"
27
28#include "WM_api.hh"
29#include "WM_types.hh"
30
31#include "transform.hh"
32#include "transform_convert.hh"
33
34namespace blender::ed::transform {
35
48
49/* -------------------------------------------------------------------- */
52
54 eMaskWhichHandle which_handle,
55 TransData *td,
56 TransData2D *td2d,
58 const float asp[2],
59 /*const*/ const float parent_matrix[3][3],
60 /*const*/ const float parent_inverse_matrix[3][3])
61{
62 BezTriple *bezt = &point->bezt;
63 const bool is_sel_any = BKE_mask_point_selected(point);
64
65 tdm->point = point;
66 copy_m3_m3(tdm->vec, bezt->vec);
67
68 tdm->is_handle = true;
69 copy_m3_m3(tdm->parent_matrix, parent_matrix);
70 copy_m3_m3(tdm->parent_inverse_matrix, parent_inverse_matrix);
71
72 BKE_mask_point_handle(point, which_handle, tdm->handle);
73 tdm->which_handle = which_handle;
74
75 copy_v2_v2(tdm->orig_handle, tdm->handle);
76
77 mul_v2_m3v2(td2d->loc, parent_matrix, tdm->handle);
78 td2d->loc[0] *= asp[0];
79 td2d->loc[1] *= asp[1];
80 td2d->loc[2] = 0.0f;
81
82 td2d->loc2d = tdm->handle;
83
84 td->flag = 0;
85 td->loc = td2d->loc;
86 mul_v2_m3v2(td->center, parent_matrix, bezt->vec[1]);
87 td->center[0] *= asp[0];
88 td->center[1] *= asp[1];
89 copy_v3_v3(td->iloc, td->loc);
90
91 memset(td->axismtx, 0, sizeof(td->axismtx));
92 td->axismtx[2][2] = 1.0f;
93
94 td->val = nullptr;
95
96 if (is_sel_any) {
97 td->flag |= TD_SELECTED;
98 }
99
100 td->dist = 0.0;
101
102 unit_m3(td->mtx);
103 unit_m3(td->smtx);
104
105 if (which_handle == MASK_WHICH_HANDLE_LEFT) {
106 tdm->orig_handle_type = bezt->h1;
107 }
108 else if (which_handle == MASK_WHICH_HANDLE_RIGHT) {
109 tdm->orig_handle_type = bezt->h2;
110 }
111}
112
113static void MaskPointToTransData(Scene *scene,
114 MaskSplinePoint *point,
115 TransData *td,
116 TransData2D *td2d,
117 TransDataMasking *tdm,
118 const bool is_prop_edit,
119 const float asp[2])
120{
121 BezTriple *bezt = &point->bezt;
122 const bool is_sel_point = BKE_mask_point_selected_knot(point);
123 const bool is_sel_any = BKE_mask_point_selected(point);
124 float parent_matrix[3][3], parent_inverse_matrix[3][3];
125
126 BKE_mask_point_parent_matrix_get(point, scene->r.cfra, parent_matrix);
127 invert_m3_m3(parent_inverse_matrix, parent_matrix);
128
129 if (is_prop_edit || is_sel_point) {
130
131 tdm->point = point;
132 copy_m3_m3(tdm->vec, bezt->vec);
133
134 for (int i = 0; i < 3; i++) {
135 copy_m3_m3(tdm->parent_matrix, parent_matrix);
136 copy_m3_m3(tdm->parent_inverse_matrix, parent_inverse_matrix);
137
138 /* CV coords are scaled by aspects. this is needed for rotations and
139 * proportional editing to be consistent with the stretched CV coords
140 * that are displayed. this also means that for display and number-input,
141 * and when the CV coords are flushed, these are converted each time. */
142 mul_v2_m3v2(td2d->loc, parent_matrix, bezt->vec[i]);
143 td2d->loc[0] *= asp[0];
144 td2d->loc[1] *= asp[1];
145 td2d->loc[2] = 0.0f;
146
147 td2d->loc2d = bezt->vec[i];
148
149 td->flag = 0;
150 td->loc = td2d->loc;
151 mul_v2_m3v2(td->center, parent_matrix, bezt->vec[1]);
152 td->center[0] *= asp[0];
153 td->center[1] *= asp[1];
154 copy_v3_v3(td->iloc, td->loc);
155
156 memset(td->axismtx, 0, sizeof(td->axismtx));
157 td->axismtx[2][2] = 1.0f;
158
159 if (i == 1) {
160 /* Scaling weights. */
161 td->val = &bezt->weight;
162 td->ival = *td->val;
163 }
164 else {
165 td->val = nullptr;
166 }
167
168 if (is_sel_any) {
169 td->flag |= TD_SELECTED;
170 }
171 td->dist = 0.0;
172
173 unit_m3(td->mtx);
174 unit_m3(td->smtx);
175
176 if (i == 0) {
177 tdm->orig_handle_type = bezt->h1;
178 }
179 else if (i == 2) {
180 tdm->orig_handle_type = bezt->h2;
181 }
182
183 td++;
184 td2d++;
185 tdm++;
186 }
187 }
188 else {
192 td,
193 td2d,
194 tdm,
195 asp,
196 parent_matrix,
197 parent_inverse_matrix);
198
199 td++;
200 td2d++;
201 tdm++;
202 }
203 else {
204 if (bezt->f1 & SELECT) {
207 td,
208 td2d,
209 tdm,
210 asp,
211 parent_matrix,
212 parent_inverse_matrix);
213
214 if (bezt->h1 == HD_VECT) {
215 bezt->h1 = HD_FREE;
216 }
217 else if (bezt->h1 == HD_AUTO) {
218 bezt->h1 = HD_ALIGN_DOUBLESIDE;
219 bezt->h2 = HD_ALIGN_DOUBLESIDE;
220 }
221
222 td++;
223 td2d++;
224 tdm++;
225 }
226 if (bezt->f3 & SELECT) {
229 td,
230 td2d,
231 tdm,
232 asp,
233 parent_matrix,
234 parent_inverse_matrix);
235
236 if (bezt->h2 == HD_VECT) {
237 bezt->h2 = HD_FREE;
238 }
239 else if (bezt->h2 == HD_AUTO) {
240 bezt->h1 = HD_ALIGN_DOUBLESIDE;
241 bezt->h2 = HD_ALIGN_DOUBLESIDE;
242 }
243
244 td++;
245 td2d++;
246 tdm++;
247 }
248 }
249 }
250}
251
253{
254 Scene *scene = CTX_data_scene(C);
256 TransData *td = nullptr;
257 TransData2D *td2d = nullptr;
258 TransDataMasking *tdm = nullptr;
259 int count = 0, countsel = 0;
260 const bool is_prop_edit = (t->flag & T_PROP_EDIT);
261 const bool is_prop_connected = (t->flag & T_PROP_CONNECTED) != 0;
262 float asp[2];
263
265
266 tc->data_len = 0;
267
269 return;
270 }
271
272 /* Count. */
273 for (MaskLayer &masklay : mask->masklayers) {
275 continue;
276 }
277
278 for (MaskSpline &spline : masklay.splines) {
279 int i;
280
281 for (i = 0; i < spline.tot_point; i++) {
282 MaskSplinePoint *point = &spline.points[i];
283
284 if (BKE_mask_point_selected(point)) {
285 if (BKE_mask_point_selected_knot(point)) {
286 countsel += 3;
287 }
288 else {
290 countsel += 1;
291 }
292 else {
293 BezTriple *bezt = &point->bezt;
294 if (bezt->f1 & SELECT) {
295 countsel++;
296 }
297 if (bezt->f3 & SELECT) {
298 countsel++;
299 }
300 }
301 }
302 }
303
304 if (is_prop_edit) {
305 count += 3;
306 }
307 }
308 }
309 }
310
311 /* NOTE: in prop mode we need at least 1 selected. */
312 if (countsel == 0) {
313 return;
314 }
315
316 ED_mask_get_aspect(t->area, t->region, &asp[0], &asp[1]);
317
318 tc->data_len = (is_prop_edit) ? count : countsel;
319 td = tc->data = MEM_new_array_zeroed<TransData>(tc->data_len, "TransObData(Mask Editing)");
320 /* For each 2d uv coord a 3d vector is allocated, so that they can be
321 * treated just as if they were 3d verts. */
323 "TransObData2D(Mask Editing)");
325 tc->data_len, "TransDataMasking(Mask Editing)");
326 tc->custom.type.use_free = true;
327
328 /* Create data. */
329 for (MaskLayer &masklay : mask->masklayers) {
331 continue;
332 }
333
334 for (MaskSpline &spline : masklay.splines) {
335 int i;
336 TransData *head, *tail;
337 head = tail = td;
338 for (i = 0; i < spline.tot_point; i++) {
339 MaskSplinePoint *point = &spline.points[i];
340
341 if (is_prop_edit || BKE_mask_point_selected(point)) {
342 MaskPointToTransData(scene, point, td, td2d, tdm, is_prop_edit, asp);
343
344 if (is_prop_edit || BKE_mask_point_selected_knot(point)) {
345 td += 3;
346 tail += 3;
347 td2d += 3;
348 tdm += 3;
349 }
350 else {
352 td++;
353 tail++;
354 td2d++;
355 tdm++;
356 }
357 else {
358 BezTriple *bezt = &point->bezt;
359 if (bezt->f1 & SELECT) {
360 td++;
361 tail++;
362 td2d++;
363 tdm++;
364 }
365 if (bezt->f3 & SELECT) {
366 td++;
367 tail++;
368 td2d++;
369 tdm++;
370 }
371 }
372 }
373 }
374 }
375
376 if (is_prop_edit && head != tail) {
377 tail -= 1;
378 if (is_prop_connected) {
379 const bool cyclic = (spline.flag & MASK_SPLINE_CYCLIC) != 0;
380 calc_distanceCurveVerts(head, tail, cyclic);
381 }
382 else {
383 for (td = head; td <= tail; td++) {
384 td->dist = FLT_MAX;
385 }
386 }
387 }
388 }
389 }
390}
391
393
394/* -------------------------------------------------------------------- */
397
399{
400 TransData2D *td;
401 TransDataMasking *tdm;
402 int a;
403 float asp[2], inv[2];
404
406
407 ED_mask_get_aspect(t->area, t->region, &asp[0], &asp[1]);
408 inv[0] = 1.0f / asp[0];
409 inv[1] = 1.0f / asp[1];
410
411 /* Flush to 2d vector from internally used 3d vector. */
412 for (a = 0, td = tc->data_2d, tdm = static_cast<TransDataMasking *>(tc->custom.type.data);
413 a < tc->data_len;
414 a++, td++, tdm++)
415 {
416 td->loc2d[0] = td->loc[0] * inv[0];
417 td->loc2d[1] = td->loc[1] * inv[1];
418 mul_m3_v2(tdm->parent_inverse_matrix, td->loc2d);
419
420 if (tdm->is_handle) {
422 tdm->which_handle,
423 td->loc2d,
424 (t->flag & T_ALT_TRANSFORM) != 0,
425 tdm->orig_handle,
426 tdm->vec);
427 }
428
429 if (t->state == TRANS_CANCEL) {
431 tdm->point->bezt.h1 = tdm->orig_handle_type;
432 }
433 else if (tdm->which_handle == MASK_WHICH_HANDLE_RIGHT) {
434 tdm->point->bezt.h2 = tdm->orig_handle_type;
435 }
436 }
437 }
438}
439
441{
443
445
446 DEG_id_tag_update(&mask->id, 0);
447}
448
450
451/* -------------------------------------------------------------------- */
454
456{
457 Mask *mask = nullptr;
458
459 if (t->spacetype == SPACE_CLIP) {
460 SpaceClip *sc = t->area->spacedata.first_as<SpaceClip>();
462 }
463 else if (t->spacetype == SPACE_IMAGE) {
464 SpaceImage *sima = t->area->spacedata.first_as<SpaceImage>();
466 }
467 else {
468 BLI_assert(0);
469 }
470
473 }
474
475 /* TODO: don't key all masks. */
476 const bool canceled = (t->state == TRANS_CANCEL);
477 if (animrig::is_autokey_on(t->scene) && !canceled) {
478 Scene *scene = t->scene;
479
482 DEG_id_tag_update(&mask->id, 0);
483 }
484 }
485}
486
488
490 /*flags*/ (T_POINTS | T_2D_EDIT),
491 /*create_trans_data*/ createTransMaskingData,
492 /*recalc_data*/ recalcData_mask_common,
493 /*special_aftertrans_update*/ special_aftertrans_update__mask,
494};
495
496} // namespace blender::ed::transform
Functions to insert, delete or modify keyframes.
#define SELECT
Definition BKE_mask.hh:19
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
#define ND_DATA
Definition WM_types.hh:552
#define NC_MASK
Definition WM_types.hh:411
#define FLT_MAX
bool is_autokey_on(const Scene *scene)
bool is_enabled(const Scene &scene, ExecutionMode mode)
TransConvertTypeInfo TransConvertType_Mask
static void MaskPointToTransData(Scene *scene, MaskSplinePoint *point, TransData *td, TransData2D *td2d, TransDataMasking *tdm, const bool is_prop_edit, const float asp[2])
static void MaskHandleToTransData(MaskSplinePoint *point, eMaskWhichHandle which_handle, TransData *td, TransData2D *td2d, TransDataMasking *tdm, const float asp[2], const float parent_matrix[3][3], const float parent_inverse_matrix[3][3])
static void flushTransMasking(TransInfo *t)
static void createTransMaskingData(bContext *C, TransInfo *t)
static void special_aftertrans_update__mask(bContext *C, TransInfo *t)
static void recalcData_mask_common(TransInfo *t)
void calc_distanceCurveVerts(TransData *head, TransData *tail, bool cyclic)
eMaskWhichHandle
Definition BKE_mask.hh:44
@ MASK_WHICH_HANDLE_LEFT
Definition BKE_mask.hh:47
@ MASK_WHICH_HANDLE_RIGHT
Definition BKE_mask.hh:48
@ MASK_WHICH_HANDLE_STICK
Definition BKE_mask.hh:46
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
bool ED_mask_layer_shape_auto_key_select(const bContext *C, Mask *mask, int frame)
MINLINE void copy_v3_v3(float r[3], const float a[3])
void copy_m3_m3(float m1[3][3], const float m2[3][3])
void BKE_mask_point_handle(const MaskSplinePoint *point, eMaskWhichHandle which_handle, float r_handle[2])
bool invert_m3_m3(float inverse[3][3], const float mat[3][3])
MINLINE void copy_v2_v2(float r[2], const float a[2])
void BKE_mask_point_parent_matrix_get(MaskSplinePoint *point, float ctime, float parent_matrix[3][3])
void mul_m3_v2(const float m[3][3], float r[2])
bool BKE_mask_point_selected(const MaskSplinePoint *p)
Definition BKE_mask.hh:155
void DEG_id_tag_update(ID *id, unsigned int flags)
void 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])
@ MASK_SPLINE_CYCLIC
bool BKE_mask_point_selected_knot(const MaskSplinePoint *p)
Definition BKE_mask.hh:160
Mask * CTX_data_edit_mask(const bContext *C)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
bool ED_maskedit_mask_visible_splines_poll(bContext *C)
Definition mask_edit.cc:77
void mul_v2_m3v2(float r[2], const float m[3][3], const float v[2])
void unit_m3(float m[3][3])
void ED_mask_get_aspect(ScrArea *area, ARegion *region, float *r_aspx, float *r_aspy)
Scene * CTX_data_scene(const bContext *C)
Mask * ED_space_clip_get_mask(const SpaceClip *sc)
eMaskhandleMode BKE_mask_point_handles_mode_get(const MaskSplinePoint *point)
@ MASK_HANDLE_MODE_STICK
Definition BKE_mask.hh:53
Mask * ED_space_image_get_mask(const SpaceImage *sima)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
eBezTriple_Handle h2
eBezTriple_Flag f3
eBezTriple_Flag f1
eBezTriple_Handle h1
MaskLayerVisibility visibility_flag
ListBaseT< MaskSpline > splines
MaskSplinePoint * points
MaskSplineFlag flag
struct RenderData r
ListBaseT< SpaceLink > spacedata
#define TRANS_DATA_CONTAINER_FIRST_SINGLE(t)
Definition transform.hh:45
conversion and adaptation of different datablocks to a common struct.