Blender V5.3
transform_mode_shear.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
10#include "BLI_math_vector_c.hh"
11#include "BLI_string_utf8.hh"
12#include "BLI_task.hh"
13
14#include "BKE_unit.hh"
15
16#include "ED_screen.hh"
17
18#include "UI_interface.hh"
19
20#include "BLT_translation.hh"
21#include "RNA_access.hh"
22
23#include "transform.hh"
24#include "transform_convert.hh"
25#include "transform_snap.hh"
26
27#include "transform_mode.hh"
28
29namespace blender::ed::transform {
30
31/* -------------------------------------------------------------------- */
34
35static void transdata_elem_shear(const TransInfo *t,
36 const TransDataContainer *tc,
37 TransData *td,
38 const float mat_final[3][3],
39 const bool is_local_center)
40{
41 float tmat[3][3];
42 const float *center;
43 if (t->flag & T_EDIT) {
44 mul_m3_series(tmat, td->smtx, mat_final, td->mtx);
45 }
46 else {
47 copy_m3_m3(tmat, mat_final);
48 }
49
50 if (is_local_center) {
51 center = td->center;
52 }
53 else {
54 center = tc->center_local;
55 }
56
57 float vec[3];
58 sub_v3_v3v3(vec, td->iloc, center);
59 mul_m3_v3(tmat, vec);
60 add_v3_v3(vec, center);
61 sub_v3_v3(vec, td->iloc);
62
64 /* Grease pencil multi-frame falloff. */
65 float *gp_falloff = static_cast<float *>(td->extra);
66 if (gp_falloff != nullptr) {
67 mul_v3_fl(vec, td->factor * *gp_falloff);
68 }
69 else {
70 mul_v3_fl(vec, td->factor);
71 }
72 }
73 else {
74 mul_v3_fl(vec, td->factor);
75 }
76
77 add_v3_v3v3(td->loc, td->iloc, vec);
78}
79
81
82/* -------------------------------------------------------------------- */
85
89
91{
92 float dir[3];
93 bool dir_flip = false;
95
96 /* Needed for axis aligned view gizmo. */
97 if (t->orient[t->orient_curr].type == V3D_ORIENT_VIEW) {
98 if (t->orient_axis_ortho == 0) {
99 if (t->center2d[1] > t->mouse.imval[1]) {
100 dir_flip = !dir_flip;
101 }
102 }
103 else if (t->orient_axis_ortho == 1) {
104 if (t->center2d[0] > t->mouse.imval[0]) {
105 dir_flip = !dir_flip;
106 }
107 }
108
109 /* A flipped view applies to this orientation,
110 * so the input direction must be flipped to match. */
111 if (t->flag & T_VIEW_NEGATIVE) {
112 dir_flip = !dir_flip;
113 }
114 }
115
116 /* Without this, half the gizmo handles move in the opposite direction. */
117 if ((t->orient_axis_ortho + 1) % 3 != t->orient_axis) {
118 dir_flip = !dir_flip;
119 }
120
121 if (dir_flip) {
122 negate_v3(dir);
123 }
124
125 mul_mat3_m4_v3(t->viewmat, dir);
126 if (normalize_v2(dir) == 0.0f) {
127 dir[0] = 1.0f;
128 }
130
132}
133
135{
137
138 if (event->type == MIDDLEMOUSE && event->val == KM_PRESS) {
139 /* Use custom.mode.data pointer to signal Shear direction. */
140 do {
141 t->orient_axis_ortho = (t->orient_axis_ortho + 1) % 3;
142 } while (t->orient_axis_ortho == t->orient_axis);
143
145
146 status = TREDRAW_HARD;
147 }
148 else if (event->type == EVT_XKEY && event->val == KM_PRESS) {
149 t->orient_axis_ortho = (t->orient_axis + 1) % 3;
151
152 status = TREDRAW_HARD;
153 }
154 else if (event->type == EVT_YKEY && event->val == KM_PRESS) {
155 t->orient_axis_ortho = (t->orient_axis + 2) % 3;
157
158 status = TREDRAW_HARD;
159 }
160
161 ShearCustomData *custom_data = static_cast<ShearCustomData *>(t->custom.mode.data);
162 bool is_event_handled = (event->type != MOUSEMOVE) && (status || t->redraw);
163 custom_data->update_status_bar = custom_data->update_status_bar || is_event_handled;
164
165 return status;
166}
167
168static void apply_shear_value(TransInfo *t, const float value)
169{
170 float smat[3][3];
171 unit_m3(smat);
172 smat[1][0] = value;
173
174 float axismat_inv[3][3];
175 copy_v3_v3(axismat_inv[0], t->spacemtx[t->orient_axis_ortho]);
176 copy_v3_v3(axismat_inv[2], t->spacemtx[t->orient_axis]);
177 cross_v3_v3v3(axismat_inv[1], axismat_inv[0], axismat_inv[2]);
178 float axismat[3][3];
179 invert_m3_m3(axismat, axismat_inv);
180
181 float mat_final[3][3];
182 mul_m3_series(mat_final, axismat_inv, smat, axismat);
183
184 const bool is_local_center = transdata_check_local_center(t, t->around);
185
187 threading::parallel_for(IndexRange(tc->data_len), 1024, [&](const IndexRange range) {
188 for (const int i : range) {
189 TransData *td = &tc->data[i];
190 if (td->flag & TD_SKIP) {
191 continue;
192 }
193 transdata_elem_shear(t, tc, td, mat_final, is_local_center);
194 }
195 });
196 }
197}
198
199static bool uv_shear_in_clip_bounds_test(const TransInfo *t, const float value)
200{
201 const int axis = t->orient_axis_ortho;
202 if (axis < 0 || 1 < axis) {
203 return true; /* Non standard axis, nothing to do. */
204 }
205 const float *center = t->center_global;
207 TransData *td = tc->data;
208 for (int i = 0; i < tc->data_len; i++, td++) {
209 if (td->flag & TD_SKIP) {
210 continue;
211 }
212 if (td->factor < 1.0f) {
213 continue; /* Proportional edit, will get picked up in next phase. */
214 }
215
216 float uv[2];
217 sub_v2_v2v2(uv, td->iloc, center);
218 uv[axis] = uv[axis] + value * uv[1 - axis] * (2 * axis - 1);
219 add_v2_v2(uv, center);
220 /* TODO: UDIM support. */
221 if (uv[axis] < 0.0f || 1.0f < uv[axis]) {
222 return false;
223 }
224 }
225 }
226 return true;
227}
228
229static bool clip_uv_transform_shear(const TransInfo *t, float *vec, float *vec_inside_bounds)
230{
231 float value = vec[0];
232 if (uv_shear_in_clip_bounds_test(t, value)) {
233 vec_inside_bounds[0] = value; /* Store for next iteration. */
234 return false; /* Nothing to do. */
235 }
236 float value_inside_bounds = vec_inside_bounds[0];
237 if (!uv_shear_in_clip_bounds_test(t, value_inside_bounds)) {
238 return false; /* No known way to fix, may as well shear anyway. */
239 }
240 const int max_i = 32; /* Limit iteration, mainly for debugging. */
241 for (int i = 0; i < max_i; i++) {
242 /* Binary search. */
243 const float value_mid = (value_inside_bounds + value) / 2.0f;
244 if (ELEM(value_mid, value_inside_bounds, value)) {
245 break; /* Float precision reached. */
246 }
247 if (uv_shear_in_clip_bounds_test(t, value_mid)) {
248 value_inside_bounds = value_mid;
249 }
250 else {
251 value = value_mid;
252 }
253 }
254
255 vec_inside_bounds[0] = value_inside_bounds; /* Store for next iteration. */
256 vec[0] = value_inside_bounds; /* Update shear value. */
257 return true;
258}
259
260static void apply_shear(TransInfo *t)
261{
262 float value = t->values[0] + t->values_modal_offset[0];
263 float angle = atanf(value);
265 applyNumInput(&t->num, &angle);
266 value = tanf(angle);
267 t->values_final[0] = value;
268
269 apply_shear_value(t, value);
270
271 if (t->flag & T_CLIP_UV) {
274 }
275
276 /* Not ideal, see #clipUVData code-comment. */
277 if (t->flag & T_PROP_EDIT) {
278 clipUVData(t);
279 }
280 }
281
282 recalc_data(t);
283
284 char str[UI_MAX_DRAW_STR];
285 /* Header print for NumInput. */
286 if (hasNumInput(&t->num)) {
287 char c[NUM_STR_REP_LEN];
288 outputNumInput(&(t->num), c, t->scene->unit);
289 SNPRINTF_UTF8(str, IFACE_("Shear: %s %s"), c, t->proptext);
290 }
291 else {
292 const float angle_deg = RAD2DEGF(angle);
293 /* Default header print. */
294 SNPRINTF_UTF8(str, IFACE_("Shear: %.3f° %s"), angle_deg, t->proptext);
295 }
296
298}
299
300static void shear_status(TransInfo *t)
301{
302 if (t->keymap == nullptr) {
303 return;
304 }
305 const wmKeyMap &keymap = *t->keymap;
306
307 ShearCustomData *custom_data = static_cast<ShearCustomData *>(t->custom.mode.data);
308 if (!custom_data->update_status_bar) {
309 return;
310 }
311 custom_data->update_status_bar = false;
312
313 WorkspaceStatus status(t->context);
314
315 status.modal_keymap(IFACE_("Confirm"), keymap, TFM_MODAL_CONFIRM);
316 status.modal_keymap(IFACE_("Cancel"), keymap, TFM_MODAL_CANCEL);
317
318 status.item_bool({}, t->orient_axis_ortho == (t->orient_axis + 1) % 3, ICON_EVENT_X);
319 status.item_bool({}, t->orient_axis_ortho == (t->orient_axis + 2) % 3, ICON_EVENT_Y);
320 status.item(IFACE_("Shear Axis"), ICON_NONE);
321 status.item(IFACE_("Swap Axes"), ICON_MOUSE_MMB);
322
323 status.modal_keymap(IFACE_("Snap"), keymap, TFM_MODAL_SNAP_TOGGLE, t->modifiers & MOD_SNAP);
324 status.modal_keymap(
325 IFACE_("Snap Invert"), keymap, TFM_MODAL_SNAP_INV_ON, t->modifiers & MOD_SNAP_INVERT);
326 status.modal_keymap(
327 IFACE_("Precision"), keymap, TFM_MODAL_PRECISION, t->modifiers & MOD_PRECISION);
328
329 if (t->proptext[0]) {
330 status.modal_keymap({}, keymap, TFM_MODAL_PROPSIZE_UP);
331 status.modal_keymap(IFACE_("Proportional Size"), keymap, TFM_MODAL_PROPSIZE_DOWN);
332 }
333}
334
335static void initShear(TransInfo *t, wmOperator *op)
336{
337 t->mode = TFM_SHEAR;
338
339 if (t->orient_axis == t->orient_axis_ortho) {
340 t->orient_axis = 2;
341 t->orient_axis_ortho = 1;
342 }
343
345
346 t->idx_max = 0;
347 t->num.idx_max = 0;
349
351 t->num.unit_sys = t->scene->unit.system;
354
355 ShearCustomData *custom_data = MEM_new_zeroed<ShearCustomData>(__func__);
356 t->custom.mode.data = custom_data;
357 t->custom.mode.free_cb = [](TransInfo *, TransDataContainer *, TransCustomData *custom_data) {
358 MEM_delete(static_cast<ShearCustomData *>(custom_data->data));
359 custom_data->data = nullptr;
360 };
361
362 custom_data->update_status_bar = true;
363
364 const float angle = RNA_float_get(op->ptr, "angle");
365 t->values[0] = tanf(angle);
367}
368
370
372 /*flags*/ T_NO_CONSTRAINT,
373 /*init_fn*/ initShear,
374 /*transform_fn*/ apply_shear,
375 /*transform_matrix_fn*/ nullptr,
376 /*handle_event_fn*/ handleEventShear,
377 /*snap_distance_fn*/ nullptr,
378 /*snap_apply_fn*/ nullptr,
379 /*draw_fn*/ nullptr,
380 /*status_fn*/ shear_status,
381};
382
383} // namespace blender::ed::transform
#define RAD2DEGF(_rad)
#define mul_m3_series(...)
#define SNPRINTF_UTF8(dst, format,...)
#define IFACE_(msgid)
#define NUM_STR_REP_LEN
#define UI_MAX_DRAW_STR
#define ELEM(...)
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
constexpr IndexRange()=default
void item(std::string text, int icon1, int icon2=0)
Definition area.cc:1076
void modal_keymap(std::string text, const wmKeyMap &keymap, int propvalue, bool inverted=false)
Definition area.cc:1119
void item_bool(std::string text, bool inverted, int icon1, int icon2=0)
Definition area.cc:1097
#define str(s)
static void shear_status(TransInfo *t)
void initMouseInputMode(TransInfo *t, MouseInput *mi, MouseInputMode mode)
void recalc_data(TransInfo *t)
void clipUVData(TransInfo *t)
static eRedrawFlag handleEventShear(TransInfo *t, const wmEvent *event)
bool transform_snap_increment(const TransInfo *t, float *r_val)
void setCustomPointsFromDirection(TransInfo *t, MouseInput *mi, const float2 &dir)
static void initShear_mouseInputMode(TransInfo *t)
static void apply_shear_value(TransInfo *t, const float value)
void transform_mode_default_modal_orientation_set(TransInfo *t, int type)
static void transdata_elem_shear(const TransInfo *t, const TransDataContainer *tc, TransData *td, const float mat_final[3][3], const bool is_local_center)
static bool clip_uv_transform_shear(const TransInfo *t, float *vec, float *vec_inside_bounds)
static bool uv_shear_in_clip_bounds_test(const TransInfo *t, const float value)
void initSnapAngleIncrements(TransInfo *t)
static void apply_shear(TransInfo *t)
static void initShear(TransInfo *t, wmOperator *op)
bool transdata_check_local_center(const TransInfo *t, short around)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
Definition BLI_task.hh:69
void mul_m3_v3(const float M[3][3], float r[3])
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void add_v3_v3v3(float r[3], const float a[3], const float b[3])
void copy_m3_m3(float m1[3][3], const float m2[3][3])
bool invert_m3_m3(float inverse[3][3], const float mat[3][3])
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
bool hasNumInput(const NumInput *n)
Definition numinput.cc:188
MINLINE float normalize_v2(float n[2])
MINLINE void mul_v3_fl(float r[3], float f)
MINLINE void sub_v3_v3(float r[3], const float a[3])
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
MINLINE void add_v2_v2(float r[2], const float a[2])
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void negate_v3(float r[3])
void ED_area_status_text(ScrArea *area, const char *str)
Definition area.cc:975
bool applyNumInput(NumInput *n, float *vec)
Definition numinput.cc:207
void mul_mat3_m4_v3(const float mat[4][4], float r[3])
MINLINE void add_v3_v3(float r[3], const float a[3])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
void unit_m3(float m[3][3])
MINLINE void copy_v3_fl(float r[3], float f)
void outputNumInput(NumInput *n, char *str, const UnitSettings &unit_settings)
Definition numinput.cc:104
@ B_UNIT_ROTATION
Definition BKE_unit.hh:170
float RNA_float_get(PointerRNA *ptr, const char *name)
#define atanf
#define tanf
int unit_type[NUM_MAX_ELEMENTS]
float val_inc[NUM_MAX_ELEMENTS]
struct UnitSettings unit
struct blender::ed::transform::TransInfo::@074260112357037217233016050204226071352346123342 orient[3]
TransCustomDataContainer custom
wmEventType type
Definition WM_types.hh:817
#define FOREACH_TRANS_DATA_CONTAINER(t, th)
Definition transform.hh:48
conversion and adaptation of different datablocks to a common struct.
transform modes used by different operators.