forked from funkey/gui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotateView.cpp
282 lines (189 loc) · 6.27 KB
/
RotateView.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
#include <cmath>
#include <util/Logger.h>
#include "RotateView.h"
namespace gui {
static logger::LogChannel rotateviewlog("rotateviewlog", "[RotateView] ");
RotateView::RotateView() :
_prevX(0.0),
_prevY(1.0),
_prevZ(0.0),
_prevW(0.0) {
registerInput(_content, "painter");
registerOutput(_rotated, "painter");
_content.registerBackwardSlot(_update);
_content.registerBackwardSlot(_keyDown);
_content.registerBackwardSlot(_keyUp);
_content.registerBackwardSlot(_mouseDown);
_content.registerBackwardSlot(_mouseUp);
_content.registerBackwardSlot(_mouseMove);
_content.registerBackwardCallback(&RotateView::onInputSet, this);
_content.registerBackwardCallback(&RotateView::onModified, this);
_content.registerBackwardCallback(&RotateView::onContentChanged, this);
_content.registerBackwardCallback(&RotateView::onSizeChanged, this);
_rotated.registerForwardSlot(_modified);
_rotated.registerForwardSlot(_contentChanged);
_rotated.registerForwardSlot(_sizeChanged);
_rotated.registerForwardCallback(&RotateView::onUpdate, this);
_rotated.registerForwardCallback(&RotateView::onKeyUp, this);
_rotated.registerForwardCallback(&RotateView::onKeyDown, this);
_rotated.registerForwardCallback(&RotateView::onMouseUp, this);
_rotated.registerForwardCallback(&RotateView::onMouseDown, this);
_rotated.registerForwardCallback(&RotateView::onMouseMove, this);
}
void
RotateView::onInputSet(const pipeline::InputSet<Painter>& signal) {
LOG_ALL(rotateviewlog) << "got a new painter" << std::endl;
if (!_rotated)
_rotated.createData();
_rotated->setContent(_content);
_modified();
}
void
RotateView::onModified(const pipeline::Modified& signal) {
// just pass this signal on
_modified();
}
void
RotateView::onContentChanged(const ContentChanged& signal) {
_contentChanged();
}
void
RotateView::onSizeChanged(const SizeChanged& signal) {
_rotated->updateSize();
_sizeChanged(SizeChanged(_rotated->getSize()));
}
void
RotateView::onUpdate(const pipeline::Update& signal) {
_update(signal);
_rotated->setRotation(_x, _y, _z, _w/M_PI*180.0);
}
void
RotateView::onKeyUp(const KeyUp& signal) {
// pass on the signal
_keyUp(signal);
}
void
RotateView::onKeyDown(KeyDown& signal) {
LOG_ALL(rotateviewlog) << "a key was pressed" << std::endl;
if (signal.key == keys::R) {
LOG_ALL(rotateviewlog) << "resetting rotation" << std::endl;
_x = _z = _w = 0.0;
_y = 1.0;
_modified();
signal.processed = true;
} else {
// pass on the signal
_keyDown(signal);
}
}
void
RotateView::onMouseUp(const MouseUp& signal) {
LOG_ALL(rotateviewlog) << "a button was released" << std::endl;
MouseUp unrotatedSignal = signal;
unrotatedSignal.position = unrotatePosition(signal.position);
_mouseUp(unrotatedSignal);
}
void
RotateView::onMouseDown(MouseDown& signal) {
LOG_ALL(rotateviewlog) << "a button was pressed" << std::endl;
MouseDown unrotatedSignal = signal;
unrotatedSignal.position = unrotatePosition(signal.position);
_mouseDown(unrotatedSignal);
if (unrotatedSignal.processed)
return;
util::point<double> position = signal.position;
LOG_ALL(rotateviewlog) << "mouse button " << signal.button << " down, position is " << position << std::endl;
if (signal.button == buttons::Left && _rotated->getSize().contains(position)) {
LOG_ALL(rotateviewlog) << "it's the left mouse button -- start dragging mode" << std::endl;
_dragging = true;
_buttonDown = position;
_prevX = _x;
_prevY = _y;
_prevZ = _z;
_prevW = _w;
signal.processed = true;
return;
}
}
void
RotateView::onMouseMove(MouseMove& signal) {
LOG_ALL(rotateviewlog) << "the mouse is moved" << std::endl;
MouseMove unrotatedSignal = signal;
unrotatedSignal.position = unrotatePosition(signal.position);
_mouseMove(unrotatedSignal);
if (unrotatedSignal.processed)
return;
if (!_dragging) {
return;
}
LOG_ALL(rotateviewlog) << "I am in dragging mode" << std::endl;
double amp = 1.0;
if (signal.modifiers & keys::ControlDown)
amp = 10.0;
// mouse is dragged
if (signal.modifiers & buttons::LeftDown) {
LOG_ALL(rotateviewlog) << "left button is still pressed" << std::endl;
util::point<double> moved = signal.position - _buttonDown;
//_buttonDown = signal.position;
// change the current rotation according to mouse movement
rotate(moved);
_modified();
signal.processed = true;
} else {
LOG_ALL(rotateviewlog) << "left button released -- stop dragging" << std::endl;
_dragging = false;
}
}
util::point<double>
RotateView::unrotatePosition(const util::point<double>& position) {
// TODO:
// • rotate (position.x, position.y, 0) around (_x, _y, _z) by -_w
return position;
}
void
RotateView::rotate(const util::point<double>& moved) {
LOG_ALL(rotateviewlog) << "current rotation: " << _w << ", (" << _x << ", " << _y << ", " << _z << ")" << std::endl;
LOG_ALL(rotateviewlog) << "moved by: (" << moved.x << ", " << moved.y << ")" << std::endl;
// previous rotation to quaternion
double qx, qy, qz, qw;
qx = _prevX*sin(_prevW/2.0);
qy = _prevY*sin(_prevW/2.0);
qz = _prevZ*sin(_prevW/2.0);
qw = cos(_prevW/2.0);
// difference rotation as angle-axis
double dx, dy, dz, dw;
// ensure numerical stability
double dnorm = sqrt(moved.y*moved.y + moved.x*moved.x);
if (dnorm <= 0.0001)
return;
dx = -moved.y/dnorm;
dy = moved.x/dnorm;
dz = 0.0;
dw = dnorm/180.0*M_PI;
LOG_ALL(rotateviewlog) << "add rotation: " << dw << ", (" << dx << ", " << dy << ", " << dz << ")" << std::endl;
// difference rotation to quaternion
double qdx, qdy, qdz, qdw;
qdx = dx*sin(dw/2.0);
qdy = dy*sin(dw/2.0);
qdz = dz*sin(dw/2.0);
qdw = cos(dw/2.0);
// composition of both quaternions
double cx, cy, cz, cw;
cw = (qdw*qw - qdx*qx - qdy*qy - qdz*qz);
cx = (qdw*qx + qdx*qw + qdy*qz - qdz*qy);
cy = (qdw*qy - qdx*qz + qdy*qw + qdz*qx);
cz = (qdw*qz + qdx*qy - qdy*qx + qdz*qw);
LOG_ALL(rotateviewlog) << "result quaternion: " << cw << ", (" << cx << ", " << cy << ", " << cz << ")" << std::endl;
// back to axis-angle rotation
_w = 2*acos(cw);
_x = cx/sin(_w/2.0);
_y = cy/sin(_w/2.0);
_z = cz/sin(_w/2.0);
// normalize rotation vector
double norm = sqrt(_x*_x + _y*_y + _z*_z);
_x = _x/norm;
_y = _y/norm;
_z = _z/norm;
LOG_ALL(rotateviewlog) << "new rotation: " << _w << ", (" << _x << ", " << _y << ", " << _z << ")" << std::endl;
}
} // namespace gui