Blender V5.3
view3d_navigate_view_rotate.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BKE_context.hh"
10
11#include "BLI_math_matrix_c.hh"
13#include "BLI_math_vector_c.hh"
14
15#include "WM_api.hh"
16
17#include "ED_screen.hh"
18
19#include "view3d_intern.hh"
20#include "view3d_navigate.hh" /* own include */
21
22namespace blender {
23
24/* -------------------------------------------------------------------- */
27
29{
30 static const EnumPropertyItem modal_items[] = {
31 {VIEW_MODAL_CANCEL, "CANCEL", 0, "Cancel", ""},
32 {VIEW_MODAL_CONFIRM, "CONFIRM", 0, "Confirm", ""},
33
34 {VIEWROT_MODAL_AXIS_SNAP_ENABLE, "AXIS_SNAP_ENABLE", 0, "Axis Snap", ""},
35 {VIEWROT_MODAL_AXIS_SNAP_DISABLE, "AXIS_SNAP_DISABLE", 0, "Axis Snap (Off)", ""},
36
37 {VIEWROT_MODAL_SWITCH_ZOOM, "SWITCH_TO_ZOOM", 0, "Switch to Zoom"},
38 {VIEWROT_MODAL_SWITCH_MOVE, "SWITCH_TO_MOVE", 0, "Switch to Move"},
39
40 {0, nullptr, 0, nullptr, nullptr},
41 };
42
43 wmKeyMap *keymap = WM_modalkeymap_find(keyconf, "View3D Rotate Modal");
44
45 /* This function is called for each space-type, only needs to add map once. */
46 if (keymap && keymap->modal_items) {
47 return;
48 }
49
50 keymap = WM_modalkeymap_ensure(keyconf, "View3D Rotate Modal", modal_items);
51
52 /* assign map to operators */
53 WM_modalkeymap_assign(keymap, "VIEW3D_OT_rotate");
54}
55
57{
58 const float axis_limit = DEG2RADF(45 / 3);
59
60 RegionView3D *rv3d = vod->rv3d;
61
62 float viewquat_inv[4];
63 float zaxis[3] = {0, 0, 1};
64 float zaxis_best[3];
65 int x, y, z;
66 bool found = false;
67
68 invert_qt_qt_normalized(viewquat_inv, vod->curr.viewquat);
69
70 mul_qt_v3(viewquat_inv, zaxis);
71 normalize_v3(zaxis);
72
73 for (x = -1; x < 2; x++) {
74 for (y = -1; y < 2; y++) {
75 for (z = -1; z < 2; z++) {
76 if (x || y || z) {
77 float zaxis_test[3] = {float(x), float(y), float(z)};
78
79 normalize_v3(zaxis_test);
80
81 if (angle_normalized_v3v3(zaxis_test, zaxis) < axis_limit) {
82 copy_v3_v3(zaxis_best, zaxis_test);
83 found = true;
84 }
85 }
86 }
87 }
88 }
89
90 if (found) {
91
92 /* find the best roll */
93 float quat_roll[4], quat_final[4], quat_best[4], quat_snap[4];
94 float viewquat_align[4]; /* viewquat aligned to zaxis_best */
95 float viewquat_align_inv[4]; /* viewquat aligned to zaxis_best */
96 float best_angle = axis_limit;
97 int j;
98
99 /* viewquat_align is the original viewquat aligned to the snapped axis
100 * for testing roll */
101 rotation_between_vecs_to_quat(viewquat_align, zaxis_best, zaxis);
102 normalize_qt(viewquat_align);
103 mul_qt_qtqt(viewquat_align, vod->curr.viewquat, viewquat_align);
104 normalize_qt(viewquat_align);
105 invert_qt_qt_normalized(viewquat_align_inv, viewquat_align);
106
107 vec_to_quat(quat_snap, zaxis_best, OB_NEGZ, OB_POSY);
108 normalize_qt(quat_snap);
109 invert_qt_normalized(quat_snap);
110
111 /* check if we can find the roll */
112 found = false;
113
114 /* find best roll */
115 for (j = 0; j < 8; j++) {
116 float angle;
117 float xaxis1[3] = {1, 0, 0};
118 float xaxis2[3] = {1, 0, 0};
119 float quat_final_inv[4];
120
121 axis_angle_to_quat(quat_roll, zaxis_best, float(j * DEG2RADF(45.0f)));
122 normalize_qt(quat_roll);
123
124 mul_qt_qtqt(quat_final, quat_snap, quat_roll);
125 normalize_qt(quat_final);
126
127 /* compare 2 vector angles to find the least roll */
128 invert_qt_qt_normalized(quat_final_inv, quat_final);
129 mul_qt_v3(viewquat_align_inv, xaxis1);
130 mul_qt_v3(quat_final_inv, xaxis2);
131 angle = angle_v3v3(xaxis1, xaxis2);
132
133 if (angle <= best_angle) {
134 found = true;
135 best_angle = angle;
136 copy_qt_qt(quat_best, quat_final);
137 }
138 }
139
140 if (found) {
141 /* lock 'quat_best' to an axis view if we can */
143 quat_best, 0.01f, &rv3d->view, &rv3d->view_axis_roll);
144 }
145 else {
146 copy_qt_qt(quat_best, viewquat_align);
147 }
148
149 copy_qt_qt(rv3d->viewquat, quat_best);
150
152
153 if (U.uiflag & USER_AUTOPERSP) {
154 if (RV3D_VIEW_IS_AXIS(rv3d->view)) {
155 if (rv3d->persp == RV3D_PERSP) {
156 rv3d->persp = RV3D_ORTHO;
157 }
158 }
159 }
160 }
161 else if (U.uiflag & USER_AUTOPERSP) {
162 rv3d->persp = vod->init.persp_with_auto_persp_applied;
163 }
164}
165
166static void viewrotate_apply(ViewOpsData *vod, const int event_xy[2])
167{
168 RegionView3D *rv3d = vod->rv3d;
169
170 rv3d->view = RV3D_VIEW_USER; /* need to reset every time because of view snapping */
171
172 if (U.flag & USER_TRACKBALL) {
173 float axis[3], q1[4], dvec[3], newvec[3];
174 float angle;
175
176 {
177 const int event_xy_offset[2] = {
178 event_xy[0] + vod->init.event_xy_offset[0],
179 event_xy[1] + vod->init.event_xy_offset[1],
180 };
181 calctrackballvec(&vod->region->winrct, event_xy_offset, newvec);
182 }
183
184 sub_v3_v3v3(dvec, newvec, vod->init.trackvec);
185
186 angle = (len_v3(dvec) / (2.0f * V3D_OP_TRACKBALLSIZE)) * float(M_PI);
187
188 /* Before applying the sensitivity this is rotating 1:1,
189 * where the cursor would match the surface of a sphere in the view. */
190 angle *= U.view_rotate_sensitivity_trackball;
191
192 /* Allow for rotation beyond the interval [-pi, pi] */
194
195 /* This relation is used instead of the actual angle between vectors
196 * so that the angle of rotation is linearly proportional to
197 * the distance that the mouse is dragged. */
198
199 cross_v3_v3v3(axis, vod->init.trackvec, newvec);
200 axis_angle_to_quat(q1, axis, angle);
201
202 mul_qt_qtqt(vod->curr.viewquat, q1, vod->init.quat);
203
204 viewrotate_apply_dyn_ofs(vod, vod->curr.viewquat);
205 }
206 else {
207 float quat_local_x[4], quat_global_z[4];
208 float m[3][3];
209 float m_inv[3][3];
210 const float zvec_global[3] = {0.0f, 0.0f, 1.0f};
211 float xaxis[3];
212
213 /* Radians per-pixel. */
214 const float sensitivity = U.view_rotate_sensitivity_turntable / UI_SCALE_FAC;
215
216 /* Get the 3x3 matrix and its inverse from the quaternion */
217 quat_to_mat3(m, vod->curr.viewquat);
218 invert_m3_m3(m_inv, m);
219
220 /* Avoid Gimbal Lock
221 *
222 * Even though turn-table mode is in use, this can occur when the user exits the camera view
223 * or when aligning the view to a rotated object.
224 *
225 * We have gimbal lock when the user's view is rotated +/- 90 degrees along the view axis.
226 * In this case the vertical rotation is the same as the sideways turntable motion.
227 * Making it impossible to get out of the gimbal locked state without resetting the view.
228 *
229 * The logic below lets the user exit out of this state without any abrupt 'fix'
230 * which would be disorienting.
231 *
232 * This works by blending two horizons:
233 * - Rotated-horizon: `cross_v3_v3v3(xaxis, zvec_global, m_inv[2])`
234 * When only this is used, this turntable rotation works - but it's side-ways
235 * (as if the entire turn-table has been placed on its side)
236 * While there is no gimbal lock, it's also awkward to use.
237 * - Un-rotated-horizon: `m_inv[0]`
238 * When only this is used, the turntable rotation can have gimbal lock.
239 *
240 * The solution used here is to blend between these two values,
241 * so the severity of the gimbal lock is used to blend the rotated horizon.
242 * Blending isn't essential, it just makes the transition smoother.
243 *
244 * This allows sideways turn-table rotation on a Z axis that isn't world-space Z,
245 * While up-down turntable rotation eventually corrects gimbal lock. */
246#if 1
247 if (len_squared_v3v3(zvec_global, m_inv[2]) > 0.001f) {
248 float fac;
249 cross_v3_v3v3(xaxis, zvec_global, m_inv[2]);
250 if (dot_v3v3(xaxis, m_inv[0]) < 0) {
251 negate_v3(xaxis);
252 }
253 fac = angle_normalized_v3v3(zvec_global, m_inv[2]) / float(M_PI);
254 fac = fabsf(fac - 0.5f) * 2;
255 fac = fac * fac;
256 interp_v3_v3v3(xaxis, xaxis, m_inv[0], fac);
257 }
258 else {
259 copy_v3_v3(xaxis, m_inv[0]);
260 }
261#else
262 copy_v3_v3(xaxis, m_inv[0]);
263#endif
264
265 /* Determine the direction of the x vector (for rotating up and down) */
266 /* This can likely be computed directly from the quaternion. */
267
268 /* Perform the up/down rotation */
269 axis_angle_to_quat(quat_local_x, xaxis, sensitivity * -(event_xy[1] - vod->prev.event_xy[1]));
270 mul_qt_qtqt(quat_local_x, vod->curr.viewquat, quat_local_x);
271
272 /* Perform the orbital rotation */
274 quat_global_z, 'Z', sensitivity * vod->reverse * (event_xy[0] - vod->prev.event_xy[0]));
275 mul_qt_qtqt(vod->curr.viewquat, quat_local_x, quat_global_z);
276
277 viewrotate_apply_dyn_ofs(vod, vod->curr.viewquat);
278 }
279
280 /* avoid precision loss over time */
281 normalize_qt(vod->curr.viewquat);
282
283 /* use a working copy so view rotation locking doesn't overwrite the locked
284 * rotation back into the view we calculate with */
285 copy_qt_qt(rv3d->viewquat, vod->curr.viewquat);
286
287 /* Check for view snap,
288 * NOTE: don't apply snap to `vod->viewquat` so the view won't jam up. */
289 if (vod->axis_snap) {
291 }
292 vod->prev.event_xy[0] = event_xy[0];
293 vod->prev.event_xy[1] = event_xy[1];
294
295 ED_view3d_camera_lock_sync(vod->depsgraph, vod->v3d, rv3d);
296
298}
299
301 ViewOpsData *vod,
302 const eV3D_OpEvent event_code,
303 const int xy[2])
304{
305 bool use_autokey = false;
307
308 switch (event_code) {
309 case VIEW_APPLY: {
310 viewrotate_apply(vod, xy);
312 use_autokey = true;
313 }
314 break;
315 }
316 case VIEW_CONFIRM: {
317 use_autokey = true;
319 break;
320 }
321 case VIEW_CANCEL: {
322 vod->state_restore();
324 break;
325 }
326 case VIEW_PASS:
327 break;
328 }
329
330 if (use_autokey) {
331 ED_view3d_camera_lock_autokey(vod->v3d, vod->rv3d, C, true, true);
332 }
333
334 return ret;
335}
336
338 ViewOpsData *vod,
339 const wmEvent *event,
340 PointerRNA * /*ptr*/)
341{
342 if (vod->use_dyn_ofs && (vod->rv3d->is_persp == false)) {
344 }
345
347
348 if (event_code == VIEW_CONFIRM) {
349 /* MOUSEROTATE performs orbital rotation, so y axis delta is set to 0 */
350 const bool is_inverted = (event->flag & WM_EVENT_SCROLL_INVERT) &&
351 (event->type != MOUSEROTATE);
352
353 int m_xy[2];
354 if (is_inverted) {
355 m_xy[0] = 2 * event->xy[0] - event->prev_xy[0];
356 m_xy[1] = 2 * event->xy[1] - event->prev_xy[1];
357 }
358 else {
359 copy_v2_v2_int(m_xy, event->prev_xy);
360 }
361 viewrotate_apply(vod, m_xy);
362
363 ED_view3d_camera_lock_autokey(vod->v3d, vod->rv3d, C, true, true);
364 return OPERATOR_FINISHED;
365 }
366
368}
369
371{
373}
374
376{
377 /* identifiers */
378 ot->name = "Rotate View";
379 ot->description = "Rotate the view";
381
382 /* API callbacks. */
387
388 /* flags */
390
392}
393
395
398 /*idname*/ "VIEW3D_OT_rotate",
399 /*poll_fn*/ view3d_rotation_poll,
400 /*init_fn*/ viewrotate_invoke_impl,
401 /*apply_fn*/ viewrotate_modal_impl,
402};
403
404} // namespace blender
#define DEG2RADF(_deg)
#define M_PI
#define UI_SCALE_FAC
#define RV3D_VIEW_IS_AXIS(view)
#define C
Definition RandGen.cpp:29
#define ELEM(...)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
BMesh Iterators.
bool ED_view3d_camera_lock_autokey(View3D *v3d, RegionView3D *rv3d, bContext *C, bool do_rotate, bool do_translate)
UserDef U
Definition blender.cc:58
MINLINE float len_squared_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
float angle_normalized_v3v3(const float v1[3], const float v2[3]) ATTR_WARN_UNUSED_RESULT
bool ED_view3d_quat_to_axis_view_and_reset_quat(float quat[4], float epsilon, eRegionView3D_View *r_view, eRegionView3D_ViewAxisRoll *r_view_axis_roll)
MINLINE void copy_v2_v2_int(int r[2], const int a[2])
static void viewrotate_apply(ViewOpsData *vod, const int event_xy[2])
wmKeyMap * WM_modalkeymap_find(wmKeyConfig *keyconf, const char *idname)
Definition wm_keymap.cc:999
MINLINE void copy_v3_v3(float r[3], const float a[3])
void viewrotate_apply_dyn_ofs(ViewOpsData *vod, const float viewquat_new[4])
void viewrotate_modal_keymap(wmKeyConfig *keyconf)
void vec_to_quat(float q[4], const float vec[3], short axis, short upflag)
bool invert_m3_m3(float inverse[3][3], const float mat[3][3])
void view3d_operator_properties_common(wmOperatorType *ot, const enum eV3D_OpPropFlag flag)
float normalize_qt(float q[4])
bool ED_view3d_camera_lock_sync(const Depsgraph *depsgraph, View3D *v3d, RegionView3D *rv3d)
float angle_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
void axis_angle_to_quat(float r[4], const float axis[3], float angle)
static void viewrotate_apply_snap(ViewOpsData *vod)
void rotation_between_vecs_to_quat(float q[4], const float v1[3], const float v2[3])
static wmOperatorStatus viewrotate_modal_impl(bContext *C, ViewOpsData *vod, const eV3D_OpEvent event_code, const int xy[2])
void mul_qt_qtqt(float q[4], const float a[4], const float b[4])
void VIEW3D_OT_rotate(wmOperatorType *ot)
void ED_region_tag_redraw(ARegion *region)
Definition area.cc:681
void mul_qt_v3(const float q[4], float r[3])
void quat_to_mat3(float m[3][3], const float q[4])
@ WM_EVENT_SCROLL_INVERT
Definition WM_types.hh:737
wmOperatorStatus view3d_navigate_invoke_impl(bContext *C, wmOperator *op, const wmEvent *event, const ViewOpsType *nav_type)
MINLINE void cross_v3_v3v3(float r[3], const float a[3], const float b[3])
static wmOperatorStatus viewrotate_invoke_impl(bContext *C, ViewOpsData *vod, const wmEvent *event, PointerRNA *)
void copy_qt_qt(float q[4], const float a[4])
wmWindowManager * CTX_wm_manager(const bContext *C)
const ViewOpsType ViewOpsType_rotate
void axis_angle_to_quat_single(float q[4], char axis, float angle)
void invert_qt_normalized(float q[4])
MINLINE void negate_v3(float r[3])
@ VIEW_CONFIRM
Definition image_ops.cc:652
void interp_v3_v3v3(float r[3], const float a[3], const float b[3], float t)
MINLINE float len_v3(const float a[3]) ATTR_WARN_UNUSED_RESULT
void calctrackballvec(const rcti *rect, const int event_xy[2], float r_dir[3])
bool view3d_rotation_poll(bContext *C)
@ VIEWOPS_FLAG_DEPTH_NAVIGATE
@ VIEWOPS_FLAG_ORBIT_SELECT
@ VIEWOPS_FLAG_PERSP_ENSURE
@ VIEWROT_MODAL_AXIS_SNAP_ENABLE
@ VIEWROT_MODAL_AXIS_SNAP_DISABLE
@ VIEWROT_MODAL_SWITCH_MOVE
@ VIEWROT_MODAL_SWITCH_ZOOM
MINLINE float normalize_v3(float n[3])
MINLINE void sub_v3_v3v3(float r[3], const float a[3], const float b[3])
void view3d_navigate_cancel_fn(bContext *C, wmOperator *op)
static wmOperatorStatus viewrotate_invoke(bContext *C, wmOperator *op, const wmEvent *event)
void WM_modalkeymap_assign(wmKeyMap *km, const char *opname)
@ V3D_OP_PROP_USE_MOUSE_INIT
void invert_qt_qt_normalized(float q1[4], const float q2[4])
wmKeyMap * WM_modalkeymap_ensure(wmKeyConfig *keyconf, const char *idname, const EnumPropertyItem *items)
Definition wm_keymap.cc:972
@ OPTYPE_BLOCKING
Definition WM_types.hh:192
@ OPTYPE_GRAB_CURSOR_XY
Definition WM_types.hh:196
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
bScreen * ED_screen_animation_playing(const wmWindowManager *wm)
float angle_wrap_rad(float angle)
wmOperatorStatus view3d_navigate_modal_fn(bContext *C, wmOperator *op, const wmEvent *event)
#define fabsf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
eRegionView3D_ViewAxisRoll view_axis_roll
eRegionView3D_Persp persp
eRegionView3D_View view
struct blender::ViewOpsData::@013213354235237006300365026145043140112021271057 prev
struct blender::ViewOpsData::@337054214024237015354033064215073157327063021373 curr
struct blender::ViewOpsData::@130157306301264131203124134151333300254160116071 init
wmEventType type
Definition WM_types.hh:817
const char * description
Definition WM_types.hh:1115
void(*) cancel(bContext *C, wmOperator *op)
Definition WM_types.hh:1149
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
wmOperatorStatus(*) modal(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1157
#define V3D_OP_TRACKBALLSIZE