Blender V5.3
transform_convert_sequencer_image.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "MEM_guardedalloc.h"
10
11#include "DNA_screen_types.h"
12#include "DNA_sequence_types.h"
13#include "DNA_space_types.h"
14
15#include "BKE_context.hh"
16
17#include "BLI_array.hh"
18#include "BLI_math_matrix.hh"
19#include "BLI_math_matrix_c.hh"
21#include "BLI_math_vector.hh"
22#include "BLI_math_vector_c.hh"
23
24#include "SEQ_channels.hh"
25#include "SEQ_iterator.hh"
26#include "SEQ_relations.hh"
27#include "SEQ_sequencer.hh"
28#include "SEQ_transform.hh"
29
30#include "ANIM_keyframing.hh"
31
32#include "RNA_access.hh"
33#include "RNA_prototypes.hh"
34
35#include "transform.hh"
36#include "transform_convert.hh"
37
38namespace blender::ed::transform {
39
40namespace {
41
43struct TransDataSeq {
44 Strip *strip;
45 Array<float2> quad_orig;
46 float3x3 orig_matrix;
47
48 float2 orig_origin_relative; /* 0-1 range within image bounds. */
49 float2 orig_origin_pixelspace;
50 float2 orig_translation;
51 float2 orig_scale;
52 float orig_rotation;
53 eStripFlag orig_flag;
54 float active_seq_orig_rotation;
55 float2 orig_mirror;
56};
57
58} // namespace
59
60static void store_transform_properties(const Scene *scene,
61 Strip *strip,
62 float2 origin,
63 TransData *td)
64{
65 Editing *ed = seq::editing_get(scene);
66 const StripTransform *transform = strip->data->transform;
67 TransDataSeq *tdseq = MEM_new<TransDataSeq>("TransSeq TransDataSeq");
68 tdseq->strip = strip;
69 copy_v2_v2(tdseq->orig_origin_relative, transform->origin);
70 tdseq->orig_origin_pixelspace = origin;
71 tdseq->quad_orig = seq::image_transform_quad_get(scene, strip);
72 tdseq->orig_matrix = math::invert(seq::image_transform_matrix_get(scene, strip));
73
74 tdseq->orig_translation[0] = transform->xofs;
75 tdseq->orig_translation[1] = transform->yofs;
76 tdseq->orig_scale[0] = transform->scale_x;
77 tdseq->orig_scale[1] = transform->scale_y;
78 tdseq->orig_rotation = transform->rotation;
79 tdseq->orig_flag = strip->flag;
80 tdseq->orig_mirror = seq::image_transform_mirror_factor_get(strip);
81 tdseq->active_seq_orig_rotation = ed->act_strip && ed->act_strip->data->transform ?
82 ed->act_strip->data->transform->rotation :
83 transform->rotation;
84 tdseq->strip = strip;
85 td->extra = static_cast<void *>(tdseq);
86}
87
89 const Scene *scene, Strip *strip, TransData *td, TransData2D *td2d, int vert_index)
90{
91 const StripTransform *transform = strip->data->transform;
92 const float2 origin = seq::image_transform_origin_preview_offset_get(scene, strip);
94 float vertex[2] = {origin[0], origin[1]};
95
96 /* Add control vertex, so rotation and scale can be calculated.
97 * All three vertices will form a "L" shape that is aligned to the local strip axis. */
98 if (vert_index == 1) {
99 vertex[0] += cosf(transform->rotation);
100 vertex[1] += sinf(transform->rotation);
101 }
102 else if (vert_index == 2) {
103 vertex[0] -= sinf(transform->rotation);
104 vertex[1] += cosf(transform->rotation);
105 }
106
107 td2d->loc[0] = vertex[0];
108 td2d->loc[1] = vertex[1];
109 td2d->loc2d = nullptr;
110 td->loc = td2d->loc;
111 copy_v3_v3(td->iloc, td->loc);
112
113 td->center[0] = origin[0];
114 td->center[1] = origin[1];
115
116 unit_m3(td->mtx);
117 unit_m3(td->smtx);
118
119 axis_angle_to_mat3_single(td->axismtx, 'Z', transform->rotation * mirror[0] * mirror[1]);
121
122 /* Store properties only once per vertex "triad". */
123 if (vert_index == 0) {
124 store_transform_properties(scene, strip, origin, td);
125 }
126
127 td->flag |= TD_SELECTED;
128 td->dist = 0.0;
129
130 return td;
131}
132
133static void freeSeqData(TransInfo * /*t*/,
135 TransCustomData * /*custom_data*/)
136{
137 TransData *td = tc->data;
138 for (int i = 0; i < tc->data_len; i += 3) {
139 TransDataSeq *tdseq = static_cast<TransDataSeq *>((td + i)->extra);
140 MEM_delete(tdseq);
141 }
142}
143
145{
147 Editing *ed = seq::editing_get(scene);
148 const SpaceSeq *sseq = t->area->spacedata.first_as<SpaceSeq>();
149 const ARegion *region = t->region;
150
151 if (ed == nullptr) {
152 return;
153 }
154 if (sseq->mainb != SEQ_DRAW_IMG_IMBUF) {
155 return;
156 }
157 if (region->regiontype == RGN_TYPE_PREVIEW && sseq->view == SEQ_VIEW_SEQUENCE_PREVIEW) {
158 return;
159 }
160
161 ListBaseT<Strip> *seqbase = seq::active_seqbase_get(ed);
162 ListBaseT<SeqTimelineChannel> *channels = seq::channels_displayed_get(ed);
163 VectorSet strips = seq::query_rendered_strips(scene, channels, seqbase, scene->r.cfra, 0);
164 strips.remove_if([&](Strip *strip) { return (strip->flag & SEQ_SELECT) == 0; });
165
166 if (strips.is_empty()) {
167 return;
168 }
169
172
173 tc->data_len = strips.size() * 3; /* 3 vertices per sequence are needed. */
174 TransData *td = tc->data = MEM_new_array_zeroed<TransData>(tc->data_len, "TransSeq TransData");
176 "TransSeq TransData2D");
177
178 for (Strip *strip : strips) {
179 /* One `Strip` needs 3 `TransData` entries - center point placed at the image origin, then 2
180 * points offset by 1 in X and Y direction respectively, so rotation and scale can be
181 * calculated from these points. */
182 SeqToTransData(scene, strip, td++, td2d++, 0);
183 SeqToTransData(scene, strip, td++, td2d++, 1);
184 SeqToTransData(scene, strip, td++, td2d++, 2);
185 }
186}
187
189 Scene *scene,
191 const int tmode)
192{
193 PropertyRNA *prop;
194 PointerRNA ptr = RNA_pointer_create_discrete(&scene->id, RNA_StripTransform, transform);
195
196 const bool around_cursor = scene->toolsettings->sequencer_tool_settings->pivot_point ==
198 const bool do_loc = tmode == TFM_TRANSLATION || around_cursor;
199 const bool do_rot = tmode == TFM_ROTATION;
200 const bool do_scale = tmode == TFM_RESIZE;
201 const bool only_when_keyed = animrig::is_keying_flag(scene, AUTOKEY_FLAG_INSERTAVAILABLE);
202
203 bool changed = false;
204 if (do_rot) {
205 prop = RNA_struct_find_property(&ptr, "rotation");
207 C, scene, &ptr, prop, -1, scene->r.cfra, only_when_keyed);
208 }
209 if (do_loc) {
210 prop = RNA_struct_find_property(&ptr, "offset_x");
212 C, scene, &ptr, prop, -1, scene->r.cfra, only_when_keyed);
213 prop = RNA_struct_find_property(&ptr, "offset_y");
215 C, scene, &ptr, prop, -1, scene->r.cfra, only_when_keyed);
216 }
217 if (do_scale) {
218 prop = RNA_struct_find_property(&ptr, "scale_x");
220 C, scene, &ptr, prop, -1, scene->r.cfra, only_when_keyed);
221 prop = RNA_struct_find_property(&ptr, "scale_y");
223 C, scene, &ptr, prop, -1, scene->r.cfra, only_when_keyed);
224 }
225
226 return changed;
227}
228
234
236 TransDataSeq *tdseq,
237 TransData2D *td2d,
238 Strip *strip)
239{
241 float2 handle_origin = {td2d->loc[0], td2d->loc[1]};
242 /* X and Y control points used to read scale and rotation. */
243 float2 handle_x = float2((td2d + 1)->loc) - handle_origin;
244 float2 handle_y = float2((td2d + 2)->loc) - handle_origin;
245 float2 aspect = {scene->r.yasp / scene->r.xasp, 1.0f};
247 float2 orig_strip_origin_pixelspace = tdseq->orig_origin_pixelspace;
248
249 return TransformResult{(orig_strip_origin_pixelspace - handle_origin) * mirror * aspect,
250 {math::length(handle_x), math::length(handle_y)},
251 t->values_final[0] * mirror[0] * mirror[1]};
252}
253
255{
257 TransData *td = nullptr;
258 TransData2D *td2d = nullptr;
260 Editing *ed = seq::editing_get(scene);
261 int i;
262
263 for (i = 0, td = tc->data, td2d = tc->data_2d; i < tc->data_len; i += 3, td += 3, td2d += 3) {
264 TransDataSeq *tdseq = static_cast<TransDataSeq *>(td->extra);
265 Strip *strip = tdseq->strip;
266 StripTransform *transform = strip->data->transform;
267 TransformResult result = transform_result_get(t, tdseq, td2d, strip);
268
269 /* Round resulting position to integer pixels. Resulting strip
270 * will more often end up using faster interpolation (without bilinear),
271 * and avoids "text edges are too dark" artifacts with light text strips
272 * on light backgrounds. The latter happens because bilinear filtering
273 * does not do full alpha pre-multiplication. */
274 transform->xofs = roundf(tdseq->orig_translation.x - result.translation.x);
275 transform->yofs = roundf(tdseq->orig_translation.y - result.translation.y);
276
277 /* Scale. */
278 transform->scale_x = tdseq->orig_scale.x * result.scale.x;
279 transform->scale_y = tdseq->orig_scale.y * result.scale.y;
280
281 /* Rotation. Scaling can cause negative rotation. */
282 if (t->mode == TFM_ROTATION) {
283 transform->rotation = tdseq->orig_rotation + result.rotation;
284 }
285
286 if (t->mode == TFM_MIRROR) {
287
288 transform->xofs *= t->values_final[0];
289 transform->yofs *= t->values_final[1];
290
291 if (t->orient_curr == O_SET && t->orient_type_mask & (1 << V3D_ORIENT_LOCAL)) {
292 if (strip == ed->act_strip) {
293 transform->rotation = tdseq->orig_rotation;
294 }
295 else {
296 transform->rotation = tdseq->orig_rotation + (2 * tdseq->active_seq_orig_rotation);
297 }
298 }
299 else {
300 transform->rotation = tdseq->orig_rotation;
301 }
302
303 strip->flag = tdseq->orig_flag;
304 if (t->values_final[0] == -1) {
305 strip->flag ^= SEQ_FLIPX;
306 }
307 if (t->values_final[1] == -1) {
308 strip->flag ^= SEQ_FLIPY;
309 }
310 }
311
312 if ((t->animtimer) && animrig::is_autokey_on(scene)) {
313 animrecord_check_state(t, &scene->id);
315 }
316
318 }
319}
320
321static float2 calculate_translation_offset(TransInfo *t, TransDataSeq *tdseq)
322{
324 Strip *strip = tdseq->strip;
325 StripTransform *transform = strip->data->transform;
326
327 /* During modal operation, transform->*ofs is adjusted. Reset this value to original state, so
328 * that new offset can be calculated. */
329 transform->xofs = tdseq->orig_translation[0];
330 transform->yofs = tdseq->orig_translation[1];
331
332 const float2 viewport_pixel_aspect = {scene->r.xasp / scene->r.yasp, 1.0f};
334
335 Array<float2> quad_new = seq::image_transform_quad_get(scene, strip);
336 return (quad_new[0] - tdseq->quad_orig[0]) * mirror / viewport_pixel_aspect;
337}
338
339static float2 calculate_new_origin_position(TransInfo *t, TransDataSeq *tdseq, TransData2D *td2d)
340{
342 Strip *strip = tdseq->strip;
343
344 const float2 box_size = float2(seq::image_transform_box_size_get(scene, strip));
345
346 const float2 viewport_pixel_aspect = {scene->r.xasp / scene->r.yasp, 1.0f};
348
349 const float2 origin = tdseq->orig_origin_pixelspace;
350 const float2 translation = transform_result_get(t, tdseq, td2d, strip).translation;
351 const float2 origin_translated = origin - translation * viewport_pixel_aspect * mirror;
352 const float2 origin_raw_space = math::transform_point(tdseq->orig_matrix, origin_translated);
353 const float2 origin_abs = origin_raw_space + box_size / 2;
354 const float2 origin_rel = origin_abs / box_size;
355 return origin_rel;
356}
357
359{
361 TransData *td = nullptr;
362 TransData2D *td2d = nullptr;
364 int i;
365
366 for (i = 0, td = tc->data, td2d = tc->data_2d; i < tc->data_len; i += 3, td += 3, td2d += 3) {
367 TransDataSeq *tdseq = static_cast<TransDataSeq *>(td->extra);
368 Strip *strip = tdseq->strip;
369 StripTransform *transform = strip->data->transform;
370
371 const float2 origin_rel = calculate_new_origin_position(t, tdseq, td2d);
372 transform->origin[0] = origin_rel.x;
373 transform->origin[1] = origin_rel.y;
374
375 /* Calculate offset, so image does not change it's position in preview. */
376 float2 delta_translation = calculate_translation_offset(t, tdseq);
377 transform->xofs = tdseq->orig_translation.x - delta_translation.x;
378 transform->yofs = tdseq->orig_translation.y - delta_translation.y;
379
381 }
382}
383
385{
386 if ((t->flag & T_ORIGIN) == 0) {
388 }
389 else {
391 }
392}
393
395{
396
398 TransData *td = nullptr;
400 int i;
401
402 for (i = 0, td = tc->data; i < tc->data_len; i += 3, td += 3) {
403 TransDataSeq *tdseq = static_cast<TransDataSeq *>(td->extra);
404 Strip *strip = tdseq->strip;
405 StripTransform *transform = strip->data->transform;
406 if (t->state == TRANS_CANCEL) {
407 transform->xofs = tdseq->orig_translation.x;
408 transform->yofs = tdseq->orig_translation.y;
409 transform->rotation = tdseq->orig_rotation;
410 transform->scale_x = tdseq->orig_scale.x;
411 transform->scale_y = tdseq->orig_scale.y;
412 transform->origin[0] = tdseq->orig_origin_relative.x;
413 transform->origin[1] = tdseq->orig_origin_relative.y;
414 strip->flag = tdseq->orig_flag;
415 continue;
416 }
417
418 if (animrig::is_autokey_on(scene)) {
420 }
421 }
422}
423
425 /*flags*/ (T_POINTS | T_2D_EDIT),
426 /*create_trans_data*/ createTransSeqImageData,
427 /*recalc_data*/ recalcData_sequencer_image,
428 /*special_aftertrans_update*/ special_aftertrans_update__sequencer_image,
429};
430
431} // namespace blender::ed::transform
Functions to insert, delete or modify keyframes.
Read Blender memory allocation and freeing.
#define C
Definition RandGen.cpp:29
int64_t size() const
int64_t remove_if(Predicate &&predicate)
const T & data(const Action &owning_action) const
#define roundf(x)
#define vertex
bool is_autokey_on(const Scene *scene)
bool autokeyframe_property(bContext *C, Scene *scene, PointerRNA *ptr, PropertyRNA *prop, int rnaindex, float cfra, bool only_if_property_keyed)
bool is_keying_flag(const Scene *scene, eKeying_Flag flag)
void animrecord_check_state(TransInfo *t, ID *id)
static void createTransSeqImageData(bContext *C, TransInfo *t)
static TransformResult transform_result_get(TransInfo *t, TransDataSeq *tdseq, TransData2D *td2d, Strip *strip)
static float2 calculate_translation_offset(TransInfo *t, TransDataSeq *tdseq)
static TransData * SeqToTransData(Scene *scene, TransData *td, TransData2D *td2d, TransDataSeq *tdsq, Strip *strip, int flag, int sel_flag)
static void image_transform_set(TransInfo *t)
static bool autokeyframe_sequencer_image(bContext *C, Scene *scene, StripTransform *transform, const int tmode)
static void recalcData_sequencer_image(TransInfo *t)
static void freeSeqData(TransInfo *t, TransDataContainer *tc, TransCustomData *custom_data)
static void store_transform_properties(const Scene *scene, Strip *strip, float2 origin, TransData *td)
static float2 calculate_new_origin_position(TransInfo *t, TransDataSeq *tdseq, TransData2D *td2d)
static void special_aftertrans_update__sequencer_image(bContext *C, TransInfo *t)
T length(const VecBase< T, Size > &a)
CartesianBasis invert(const CartesianBasis &basis)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
int2 image_transform_box_size_get(const Scene *scene, const Strip *strip)
float2 image_transform_mirror_factor_get(const Strip *strip)
void relations_invalidate_cache(Scene *scene, Strip *strip)
ListBaseT< Strip > * active_seqbase_get(const Editing *ed)
Definition sequencer.cc:463
Array< float2 > image_transform_quad_get(const Scene *scene, const Strip *strip)
Editing * editing_get(const Scene *scene)
Definition sequencer.cc:304
float3x3 image_transform_matrix_get(const Scene *scene, const Strip *strip)
float2 image_transform_origin_preview_offset_get(const Scene *scene, const Strip *strip)
ListBaseT< SeqTimelineChannel > * channels_displayed_get(const Editing *ed)
Definition channels.cc:26
VectorSet< Strip * > query_rendered_strips(const Scene *scene, ListBaseT< SeqTimelineChannel > *channels, ListBaseT< Strip > *seqbase, const int timeline_frame, const int displayed_channel)
Definition iterator.cc:239
Scene * CTX_data_sequencer_scene(const bContext *C)
PropertyRNA * RNA_struct_find_property(PointerRNA *ptr, const char *identifier)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void copy_v2_v2(float r[2], const float a[2])
void normalize_m3(float R[3][3]) ATTR_NONNULL()
@ SEQ_VIEW_SEQUENCE_PREVIEW
VecBase< float, 2 > float2
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
MatBase< float, 3, 3 > float3x3
void unit_m3(float m[3][3])
@ AUTOKEY_FLAG_INSERTAVAILABLE
void axis_angle_to_mat3_single(float R[3][3], char axis, float angle)
void * MEM_new_array_zeroed(size_t len, size_t size, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1
#define sinf
#define cosf
struct RenderData r
struct ToolSettings * toolsettings
ListBaseT< SpaceLink > spacedata
eSpaceSeq_RegionType mainb
eSpaceSeq_Displays view
struct SequencerToolSettings * sequencer_tool_settings
void(*) free_cb(TransInfo *, TransDataContainer *tc, TransCustomData *custom_data)
Definition transform.hh:655
#define TRANS_DATA_CONTAINER_FIRST_SINGLE(t)
Definition transform.hh:45
conversion and adaptation of different datablocks to a common struct.