-
Notifications
You must be signed in to change notification settings - Fork 1
/
arc_ball.cpp
154 lines (123 loc) · 4.84 KB
/
arc_ball.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// Created by janos on 2/16/20.
//
#include "arc_ball.hpp"
#include <Magnum/Math/Quaternion.h>
#include <Magnum/Magnum.h>
using namespace Magnum;
namespace {
/* Project a point in NDC onto the arcball sphere */
Quaternion ndcToArcBall(const Vector2& p) {
const Float dist = Math::dot(p, p);
/* Point is on sphere */
if(dist <= 1.0f)
return {{p.x(), p.y(), Math::sqrt(1.0f - dist)}, 0.0f};
/* Point is outside sphere */
else {
const Vector2 proj = p.normalized();
return {{proj.x(), proj.y(), 0.0f}, 0.0f};
}
}
}
ArcBall::ArcBall(const Vector3& eye, const Vector3& viewCenter,
const Vector3& upDir, Deg fov, const Vector2i& windowSize):
_fov{fov}, _windowSize{windowSize}
{
setViewParameters(eye, viewCenter, upDir);
}
void ArcBall::setViewParameters(const Vector3& eye, const Vector3& viewCenter,
const Vector3& upDir)
{
const Vector3 dir = viewCenter - eye;
Vector3 zAxis = dir.normalized();
Vector3 xAxis = (Math::cross(zAxis, upDir.normalized())).normalized();
Vector3 yAxis = (Math::cross(xAxis, zAxis)).normalized();
xAxis = (Math::cross(zAxis, yAxis)).normalized();
_targetPosition = -viewCenter;
_targetZooming = -dir.length();
_targetQRotation = Quaternion::fromMatrix(
Matrix3x3{xAxis, yAxis, -zAxis}.transposed()).normalized();
_positionT0 = _currentPosition = _targetPosition;
_zoomingT0 = _currentZooming = _targetZooming;
_qRotationT0 = _currentQRotation = _targetQRotation;
updateInternalTransformations();
}
void ArcBall::reset() {
_targetPosition = _positionT0;
_targetZooming = _zoomingT0;
_targetQRotation = _qRotationT0;
}
void ArcBall::setLagging(const Float lagging) {
CORRADE_INTERNAL_ASSERT(lagging >= 0.0f && lagging < 1.0f);
_lagging = lagging;
}
void ArcBall::initTransformation(const Vector2i& mousePos) {
_prevMousePosNDC = screenCoordToNDC(mousePos);
}
void ArcBall::rotate(const Vector2i& mousePos) {
const Vector2 mousePosNDC = screenCoordToNDC(mousePos);
const Quaternion currentQRotation = ndcToArcBall(mousePosNDC);
const Quaternion prevQRotation = ndcToArcBall(_prevMousePosNDC);
_prevMousePosNDC = mousePosNDC;
_targetQRotation =
(currentQRotation*prevQRotation*_targetQRotation).normalized();
}
void ArcBall::translate(const Vector2i& mousePos) {
const Vector2 mousePosNDC = screenCoordToNDC(mousePos);
const Vector2 translationNDC = mousePosNDC - _prevMousePosNDC;
_prevMousePosNDC = mousePosNDC;
translateDelta(translationNDC);
}
void ArcBall::translateDelta(const Vector2& translationNDC) {
/* Half size of the screen viewport at the view center and perpendicular
with the viewDir */
const Float hh = Math::abs(_targetZooming)*Math::tan(_fov*0.5f);
const Float hw = hh*Vector2{_windowSize}.aspectRatio();
_targetPosition += _inverseView.transformVector(
{translationNDC.x()*hw, translationNDC.y()*hh, 0.0f});
}
void ArcBall::zoom(const Float delta) {
_targetZooming += delta;
}
bool ArcBall::updateTransformation() {
const Vector3 diffViewCenter = _targetPosition - _currentPosition;
const Quaternion diffRotation = _targetQRotation - _currentQRotation;
const Float diffZooming = _targetZooming - _currentZooming;
const Float dViewCenter = Math::dot(diffViewCenter, diffViewCenter);
const Float dRotation = Math::dot(diffRotation, diffRotation);
const Float dZooming = diffZooming * diffZooming;
/* Nothing change */
if(dViewCenter < 1.0e-10f &&
dRotation < 1.0e-10f &&
dZooming < 1.0e-10f) {
return false;
}
/* Nearly done: just jump directly to the target */
if(dViewCenter < 1.0e-6f &&
dRotation < 1.0e-6f &&
dZooming < 1.0e-6f) {
_currentPosition = _targetPosition;
_currentQRotation = _targetQRotation;
_currentZooming = _targetZooming;
/* Interpolate between the current transformation and the target
transformation */
} else {
const Float t = 1 - _lagging;
_currentPosition = Math::lerp(_currentPosition, _targetPosition, t);
_currentZooming = Math::lerp(_currentZooming, _targetZooming, t);
_currentQRotation = Math::slerpShortestPath(
_currentQRotation, _targetQRotation, t);
}
updateInternalTransformations();
return true;
}
void ArcBall::updateInternalTransformations() {
_view = DualQuaternion::translation(Vector3::zAxis(_currentZooming))*
DualQuaternion{_currentQRotation}*
DualQuaternion::translation(_currentPosition);
_inverseView = _view.inverted();
}
Vector2 ArcBall::screenCoordToNDC(const Vector2i& mousePos) const {
return {mousePos.x()*2.0f/_windowSize.x() - 1.0f,
1.0f - 2.0f*mousePos.y()/ _windowSize.y()};
}