Blender V5.3
transform_mode_rotate.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 <cstdlib>
10#include <optional>
11
12#include "BLI_math_matrix_c.hh"
14#include "BLI_math_vector_c.hh"
15#include "BLI_task.hh"
16
17#include "BKE_context.hh"
18#include "BKE_report.hh"
19#include "BKE_unit.hh"
20
21#include "BLT_translation.hh"
22
23#include "ED_screen.hh"
24
25#include "UI_interface.hh"
26
27#include "transform.hh"
28#include "transform_convert.hh"
29#include "transform_snap.hh"
30
31#include "transform_mode.hh"
33
34namespace blender::ed::transform {
35
36/* -------------------------------------------------------------------- */
39
47 float mat[3][3];
48};
49
51 const float angle,
52 const float axis[3],
53 const std::optional<int> quadrant)
54{
55 if (quadrant.has_value()) {
56 /* Verify the angle (radians) is close to a multiple of 90 degrees.
57 * Failure indicates the quadrant and angle have gone out of sync,
58 * e.g. the angle was modified without clearing the quadrant.
59 * Check both near-zero and near-M_PI_2 because `fmodf` can return
60 * either depending on float representation (e.g. `18 * DEG2RAD(5)` vs `M_PI_2`). */
61 const float epsilon = DEG2RADF(0.5f) * max_ff(1.0f, fabsf(angle) / 1000.0f);
62 const float remainder = fabsf(fmodf(angle, float(M_PI_2)));
63 UNUSED_VARS_NDEBUG(epsilon, remainder);
64 BLI_assert(remainder < epsilon || (float(M_PI_2) - remainder) < epsilon);
65 }
67 rmc->do_update_matrix = 0;
68}
69
71{
72 rmc->do_update_matrix = 2;
73}
74
76 const float axis[3],
77 const float angle,
78 const std::optional<int> quadrant)
79{
80 if (rmc->do_update_matrix > 0) {
82 rmc->do_update_matrix--;
83 }
84}
85
87
88/* -------------------------------------------------------------------- */
91
92static void transdata_elem_rotate(const TransInfo *t,
93 const TransDataContainer *tc,
94 TransData *td,
95 TransDataExtension *td_ext,
96 const float axis[3],
97 const float angle,
98 const float angle_step,
99 const bool is_large_rotation,
100 const std::optional<int> quadrant,
102{
103 float axis_buffer[3];
104 const float *axis_final = axis;
105
106 float angle_final = angle;
107 /* When the angle is scaled by a factor, it's no longer a known quadrant angle. */
108 std::optional<int> quadrant_final = quadrant;
109 if (t->con.applyRot) {
110 copy_v3_v3(axis_buffer, axis);
111 axis_final = axis_buffer;
112 t->con.applyRot(t, tc, td, axis_buffer);
113 angle_final = angle * td->factor;
114 if (td->factor != 1.0f) {
115 quadrant_final = std::nullopt;
116 }
117 /* Even though final angle might be identical to orig value,
118 * we have to update the rotation matrix in that case... */
119 rmat_cache_reset(rmc);
120 }
121 else if (t->flag & T_PROP_EDIT) {
122 angle_final = angle * td->factor;
123 if (td->factor != 1.0f) {
124 quadrant_final = std::nullopt;
125 }
126 }
127
128 /* Rotation is very likely to be above 180 degrees we need to do rotation by steps.
129 * Note that this is only needed when doing 'absolute' rotation
130 * (i.e. from initial rotation again, typically when using numinput).
131 * regular incremental rotation (from mouse/widget/...) will be called often enough,
132 * hence steps are small enough to be properly handled without that complicated trick.
133 * Note that we can only do that kind of stepped rotation if we have initial rotation values
134 * (and access to some actual rotation value storage).
135 * Otherwise, just assume it's useless (e.g. in case of mesh/UV/etc. editing).
136 * Also need to be in Euler rotation mode, the others never allow more than one turn anyway.
137 */
138 if (is_large_rotation && td_ext != nullptr && td_ext->rotOrder == ROT_MODE_EUL) {
139 copy_v3_v3(td_ext->rot, td_ext->irot);
140 for (float angle_progress = angle_step; fabsf(angle_progress) < fabsf(angle_final);
141 angle_progress += angle_step)
142 {
143 /* Intermediate step angles are not quadrant angles. */
145 rmc->mat, axis_final, angle_progress, std::nullopt);
146 ElementRotation(t, tc, td, td_ext, rmc->mat, t->around);
147 }
148 rmat_cache_reset(rmc);
149 }
150 else if (angle_final != angle) {
151 rmat_cache_reset(rmc);
152 }
153
154 rmat_cache_update(rmc, axis_final, angle_final, quadrant_final);
155
156 ElementRotation(t, tc, td, td_ext, rmc->mat, t->around);
157}
158
160
161/* -------------------------------------------------------------------- */
164
165static float RotationBetween(TransInfo *t, const float p1[3], const float p2[3])
166{
167 float angle, start[3], end[3];
168
169 sub_v3_v3v3(start, p1, t->center_global);
170 sub_v3_v3v3(end, p2, t->center_global);
171
172 /* Angle around a constraint axis (error prone, will need debug). */
173 if (t->con.applyRot != nullptr && (t->con.mode & CON_APPLY)) {
174 float axis[3];
175
176 t->con.applyRot(t, nullptr, nullptr, axis);
177
178 angle = -angle_signed_on_axis_v3v3_v3(start, end, axis);
179 }
180 else {
181 float mtx[3][3];
182
183 copy_m3_m4(mtx, t->viewmat);
184
185 mul_m3_v3(mtx, end);
186 mul_m3_v3(mtx, start);
187
188 angle = (atan2f(start[1], start[0]) - atan2f(end[1], end[0]));
189
190 if (t->flag & T_VIEW_NEGATIVE) {
191 angle = -angle;
192 }
193 }
194
195 if (angle > float(M_PI)) {
196 angle = angle - 2 * float(M_PI);
197 }
198 else if (angle < -float(M_PI)) {
199 angle = 2.0f * float(M_PI) + angle;
200 }
201
202 return angle;
203}
204
205static void ApplySnapRotation(TransInfo *t, float *value)
206{
207 float point[3];
208 getSnapPoint(t, point);
209
210 float dist = RotationBetween(t, t->tsnap.snap_source, point);
211 *value = dist;
212}
213
214static float large_rotation_limit(float angle)
215{
216 /* Limit rotation to 1001 turns max
217 * (otherwise iterative handling of 'large' rotations would become too slow). */
218 const float angle_max = float(M_PI * 2000.0);
219 if (fabsf(angle) > angle_max) {
220 const float angle_sign = angle < 0.0f ? -1.0f : 1.0f;
221 angle = angle_sign * (fmodf(fabsf(angle), float(M_PI * 2.0)) + angle_max);
222 }
223 return angle;
224}
225
227 float angle,
228 const float axis[3],
229 const bool is_large_rotation,
230 const std::optional<int> quadrant)
231{
232 const float angle_sign = angle < 0.0f ? -1.0f : 1.0f;
233 /* We cannot use something too close to 180 degrees, or 'continuous' rotation may fail
234 * due to computing error. */
235 const float angle_step = angle_sign * float(0.9 * M_PI);
236
237 if (is_large_rotation) {
238 /* Just in case, calling code should have already done that in practice
239 * (for UI feedback reasons). */
241 }
242
244 threading::parallel_for(IndexRange(tc->data_len), 1024, [&](const IndexRange range) {
245 RotateMatrixCache rmc = {0};
246 rmat_cache_init(&rmc, angle, axis, quadrant);
247 for (const int i : range) {
248 TransData *td = &tc->data[i];
249 TransDataExtension *td_ext = tc->data_ext ? &tc->data_ext[i] : nullptr;
250 if (td->flag & TD_SKIP) {
251 continue;
252 }
254 t, tc, td, td_ext, axis, angle, angle_step, is_large_rotation, quadrant, &rmc);
255 }
256 });
257 }
258}
259
260static bool uv_rotation_in_clip_bounds_test(const TransInfo *t, const float angle)
261{
262 const float cos_angle = cosf(angle);
263 const float sin_angle = sinf(angle);
264 const float *center = t->center_global;
266 TransData *td = tc->data;
267 for (int i = 0; i < tc->data_len; i++, td++) {
268 if (td->flag & TD_SKIP) {
269 continue;
270 }
271 if (td->factor < 1.0f) {
272 continue; /* Proportional edit, will get picked up in next phase. */
273 }
274
275 float uv[2];
276 sub_v2_v2v2(uv, td->iloc, center);
277 float pr[2];
278 pr[0] = cos_angle * uv[0] + sin_angle * uv[1];
279 pr[1] = -sin_angle * uv[0] + cos_angle * uv[1];
280 add_v2_v2(pr, center);
281 /* TODO: UDIM support. */
282 if (pr[0] < 0.0f || 1.0f < pr[0]) {
283 return false;
284 }
285 if (pr[1] < 0.0f || 1.0f < pr[1]) {
286 return false;
287 }
288 }
289 }
290 return true;
291}
292
293static bool clip_uv_transform_rotate(const TransInfo *t, float *vec, float *vec_inside_bounds)
294{
295 float angle = vec[0];
297 vec_inside_bounds[0] = angle; /* Store for next iteration. */
298 return false; /* Nothing to do. */
299 }
300 float angle_inside_bounds = vec_inside_bounds[0];
301 if (!uv_rotation_in_clip_bounds_test(t, angle_inside_bounds)) {
302 return false; /* No known way to fix, may as well rotate anyway. */
303 }
304 const int max_i = 32; /* Limit iteration, mainly for debugging. */
305 for (int i = 0; i < max_i; i++) {
306 /* Binary search. */
307 const float angle_mid = (angle_inside_bounds + angle) / 2.0f;
308 if (ELEM(angle_mid, angle_inside_bounds, angle)) {
309 break; /* Float precision reached. */
310 }
311 if (uv_rotation_in_clip_bounds_test(t, angle_mid)) {
312 angle_inside_bounds = angle_mid;
313 }
314 else {
315 angle = angle_mid;
316 }
317 }
318
319 vec_inside_bounds[0] = angle_inside_bounds; /* Store for next iteration. */
320 vec[0] = angle_inside_bounds; /* Update rotation angle. */
321 return true;
322}
323
325{
326 float3 axis_final;
328
329 float final;
330 bool is_large_rotation_limited = false;
331 if (applyNumInput(&t->num, &final)) {
332 /* We have to limit the amount of turns to a reasonable number here,
333 * to avoid things getting *very* slow, see how applyRotationValue() handles those... */
334 const float final_unlimited = final;
335 final = large_rotation_limit(final);
336 is_large_rotation_limited = (final != final_unlimited);
337 }
338 else {
339 final = t->values[0] + t->values_modal_offset[0];
340 if (!(t->flag & T_INPUT_IS_VALUES_FINAL) &&
342 {
343 /* Flip rotation direction if axis is pointing to screen. */
344 final = -final;
345 }
347 if (!(transform_snap_is_active(t) && validSnap(t))) {
348 transform_snap_increment(t, &final);
349 }
350 }
351
352 t->values_final[0] = final;
353
354 const std::optional<int> quadrant = transform_angle_to_quadrant_or_null(
355 t, is_large_rotation_limited);
356
357 const bool is_large_rotation = hasNumInput(&t->num);
358 applyRotationValue(t, final, axis_final, is_large_rotation, quadrant);
359
360 if (t->flag & T_CLIP_UV) {
362 applyRotationValue(t, t->values_final[0], axis_final, is_large_rotation, quadrant);
363 }
364
365 /* Not ideal, see #clipUVData code-comment. */
366 if (t->flag & T_PROP_EDIT) {
367 clipUVData(t);
368 }
369 }
370
371 recalc_data(t);
372
373 char str[UI_MAX_DRAW_STR];
374 headerRotation(t, str, sizeof(str), t->values_final[0]);
376}
377
378static void applyRotationMatrix(TransInfo *t, float mat_xform[4][4])
379{
380 float3 axis_final;
382
383 float mat3[3][3];
384 float mat4[4][4];
385 /* NOTE: is_large_rotation_limited state from applyRotation is not available here,
386 * but values_final was already set by applyRotation which skips quadrant in that case. */
388 mat3, axis_final, t->values_final[0], transform_angle_to_quadrant_or_null(t, false));
389 copy_m4_m3(mat4, mat3);
391 mul_m4_m4m4(mat_xform, mat4, mat_xform);
392}
393
394static void initRotation(TransInfo *t, wmOperator * /*op*/)
395{
396 if (t->spacetype == SPACE_ACTION) {
397 BKE_report(t->reports, RPT_ERROR, "Rotation is not supported in the Dope Sheet Editor");
398 t->state = TRANS_CANCEL;
399 }
400
401 t->mode = TFM_ROTATION;
402
404 WorkspaceStatus status(t->context);
405 status.item(TIP_("Transform is set to only affect location"), ICON_STATUS_WARNING_FILLED);
407 }
408 else {
410 }
411
412 t->idx_max = 0;
413 t->num.idx_max = 0;
415
417 t->num.unit_sys = t->scene->unit.system;
420
421 if (t->flag & T_2D_EDIT) {
422 t->flag |= T_NO_CONSTRAINT;
423 }
424
426}
427
429
431 /*flags*/ 0,
432 /*init_fn*/ initRotation,
433 /*transform_fn*/ applyRotation,
434 /*transform_matrix_fn*/ applyRotationMatrix,
435 /*handle_event_fn*/ nullptr,
436 /*snap_distance_fn*/ RotationBetween,
437 /*snap_apply_fn*/ ApplySnapRotation,
438 /*draw_fn*/ nullptr,
439 /*status_fn*/ nullptr,
440};
441
442} // namespace blender::ed::transform
#define DEG2RADF(_deg)
#define M_PI_2
#define M_PI
#define TIP_(msgid)
#define UI_MAX_DRAW_STR
#define UNUSED_VARS_NDEBUG(...)
#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
#define fmodf(x, y)
#define str(s)
static void rmat_cache_init(RotateMatrixCache *rmc, const float angle, const float axis[3], const std::optional< int > quadrant)
static void rmat_cache_update(RotateMatrixCache *rmc, const float axis[3], const float angle, const std::optional< int > quadrant)
void initMouseInputMode(TransInfo *t, MouseInput *mi, MouseInputMode mode)
void recalc_data(TransInfo *t)
void clipUVData(TransInfo *t)
void getSnapPoint(const TransInfo *t, float vec[3])
bool transform_snap_increment(const TransInfo *t, float *r_val)
bool transform_mode_is_axis_pointing_to_screen(const TransInfo *t, const float3 &axis)
static void applyRotationValue(TransInfo *t, float angle, const float axis[3], const bool is_large_rotation, const std::optional< int > quadrant)
void transform_snap_mixed_apply(TransInfo *t, float *vec)
static float RotationBetween(TransInfo *t, const float p1[3], const float p2[3])
bool validSnap(const TransInfo *t)
static void ApplySnapRotation(TransInfo *t, float *value)
void headerRotation(TransInfo *t, char *str, const int str_maxncpy, float final)
static void applyRotation(TransInfo *t)
static bool clip_uv_transform_rotate(const TransInfo *t, float *vec, float *vec_inside_bounds)
static void rmat_cache_reset(RotateMatrixCache *rmc)
std::optional< int > transform_angle_to_quadrant_or_null(const TransInfo *t, const bool is_large_rotation_limited)
void transform_mode_default_modal_orientation_set(TransInfo *t, int type)
void axis_angle_normalized_to_mat3_with_quadrant(float mat[3][3], const float axis[3], const float angle, const std::optional< int > quadrant)
static float large_rotation_limit(float angle)
static void initRotation(TransInfo *t, wmOperator *)
bool transform_mode_affect_only_locations(const TransInfo *t)
bool transform_snap_is_active(const TransInfo *t)
void initSnapAngleIncrements(TransInfo *t)
static void applyRotationMatrix(TransInfo *t, float mat_xform[4][4])
static void transdata_elem_rotate(const TransInfo *t, const TransDataContainer *tc, TransData *td, TransDataExtension *td_ext, const float axis[3], const float angle, const float angle_step, const bool is_large_rotation, const std::optional< int > quadrant, RotateMatrixCache *rmc)
void ElementRotation(const TransInfo *t, const TransDataContainer *tc, TransData *td, TransDataExtension *td_ext, const float mat[3][3], const short around)
static bool uv_rotation_in_clip_bounds_test(const TransInfo *t, const float angle)
void transform_mode_rotation_axis_get(const TransInfo *t, float3 &r_axis)
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
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void mul_m3_v3(const float M[3][3], float r[3])
MINLINE void copy_v3_v3(float r[3], const float a[3])
void copy_m3_m4(float m1[3][3], const float m2[4][4])
void copy_m4_m3(float m1[4][4], const float m2[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
void mul_m4_m4m4(float R[4][4], const float A[4][4], const float B[4][4])
float angle_signed_on_axis_v3v3_v3(const float v1[3], const float v2[3], const float axis[3]) ATTR_WARN_UNUSED_RESULT
MINLINE void add_v2_v2(float r[2], const float a[2])
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
void transform_pivot_set_m4(float mat[4][4], const float pivot[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
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
MINLINE void copy_v3_fl(float r[3], float f)
MINLINE float max_ff(float a, float b)
VecBase< float, 3 > float3
@ B_UNIT_ROTATION
Definition BKE_unit.hh:170
#define fabsf
#define sinf
#define cosf
#define atan2f
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
int unit_type[NUM_MAX_ELEMENTS]
float val_inc[NUM_MAX_ELEMENTS]
struct UnitSettings unit
void(*) applyRot(const TransInfo *t, const TransDataContainer *tc, const TransData *td, float r_axis[3])
Definition transform.hh:617
#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.