-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy patharcball.cpp
More file actions
251 lines (218 loc) · 6.14 KB
/
Copy patharcball.cpp
File metadata and controls
251 lines (218 loc) · 6.14 KB
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
#include "core.h"
#include "arcball.h"
/**
* \ingroup GLVisualization
* Default constructor, it sets the _ballRadius to 600
**/
Arcball::Arcball()
{
_ballRadius = 600;
_isRotating = false;
_isDragging = false;
_zoomRate = 0.3;
_winX = _winY = 0;
Reset();
}
/**
* \ingroup GLVisualization
* Set _winX and _winY of the current windows, it's needed every time you resize the window
* \param w _winX of the rendering window
* \param h _winY of the rendering window
**/
void Arcball::SetWidthHeight(int w, int h)
{
_winX = w;
_winY = h;
_ballRadius = std::min((int)(w/2), (int)(h/2));
}
/**
* \ingroup GLVisualization
* Set the radius of the ball (a typical radius for a 1024x768 window is 600
* \param newRadius The radius of the spherical dragging area
**/
void Arcball::SetRadius(float newRadius)
{
_ballRadius = newRadius;
}
void Arcball::StartDragging(int x, int y)
{
_startDragX = x;
_startDragY = y;
_isDragging = true;
_upDir = Eigen::Vector3f(0.0, 1.0, 0.0);
GLdouble mvMatrix[16];
glGetDoublev(GL_MODELVIEW_MATRIX, mvMatrix);
_viewDir[0] = -mvMatrix[2];
_viewDir[1] = -mvMatrix[6];
_viewDir[2] = -mvMatrix[10];
#if 0
_upDir[0] = mvMatrix[1];
_upDir[1] = mvMatrix[5];
_upDir[2] = mvMatrix[9];
_rightDir[0] = mvMatrix[0];
_rightDir[1] = mvMatrix[4];
_rightDir[2] = mvMatrix[8];
#else
_upDir = Eigen::Vector3f(0.0, 1.0, 0.0);
_rightDir = _viewDir.cross(_upDir);
#endif
}
Eigen::Vector3f Arcball::UpdateDragging(int nx, int ny)
{
_dragRate = 0.003 * _winX/1024;
Eigen::Vector3f offset;
#if 1
offset[0] = _dragRate * _rightDir[0] * (nx - _startDragX);
offset[1] = _dragRate * _upDir[1] * (_startDragY - ny);
offset[2] = _dragRate * _rightDir[2]*(nx-_startDragX) + _upDir[2]* (_startDragY-ny);
#else
_viewDir.normalize();
offset[0] = _dragRate * _viewDir[0] * (nx - _startDragX);
offset[1] = _dragRate * _rightDir[0] * (_startDragY - ny);
offset[2] = _dragRate * _upDir[0]*(nx-_startDragX) + _upDir[0]* (_startDragY-ny);
#endif
_startDragX = nx;
_startDragY = ny;
return offset;
}
void Arcball::StopDragging()
{
_isDragging = false;
}
void Arcball::StartZooming(int x, int y)
{
//store original transform matrix
glGetFloatv(GL_MODELVIEW_MATRIX, _startMatrix);
_startZoomX = x;
_startZoomY = y;
_isZooming = true;
}
void Arcball::UpdateZooming(int x, int y)
{
if (_isZooming) {
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, _zoomRate * (x - _startZoomX) );
}
ApplyRotationMatrix();
}
void Arcball::StopZooming()
{
_isZooming = false;
}
/**
* \ingroup GLVisualization
* Start the rotation. Use this method in association with the left click.
* Here you must give directly the coordinates of the mouse as the glut functions extract. This method supposes that the 0,0 is in the upper-left part of the screen
* \param _x Horizontal position of the mouse (0,0) = upperleft corner (w,h) = lower right
* \param _y Vertical position of the mouse (0,0) = upperleft corner (w,h) = lower right
*
**/
void Arcball::StartRotation(int _x, int _y)
{
int x = ( (_x)-(_winX/2) );
int y = ((_winY/2)-_y);
//store original transform matrix
glGetFloatv(GL_MODELVIEW_MATRIX, _startMatrix);
_startRotationVector = ConvertXY(x,y);
_startRotationVector.normalize();
_currentRotationVector= _startRotationVector;
_isRotating = true;
}
/**
* \ingroup GLVisualization
* Update the rotation. Use this method in association with the drag event.
* Here you must give directly the coordinates of the mouse as the glut functions extract. This method supposes that the 0,0 is in the upper-left part of the screen
* \param _x Horizontal position of the mouse (0,0) = upperleft corner (w,h) = lower right
* \param _y Vertical position of the mouse (0,0) = upperleft corner (w,h) = lower right
**/
void Arcball::UpdateRotation(int _x, int _y)
{
int x = ( (_x)-(_winX/2) );
int y = ((_winY/2)-_y);
_currentRotationVector = ConvertXY(x,y);
_currentRotationVector.normalize();
//Fixed by MY
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
ApplyRotationMatrix();
}
/**
* \ingroup GLVisualization
* Apply the computed rotation matrix
* This method must be invoked inside the \code glutDisplayFunc() \endcode
*
**/
void Arcball::ApplyRotationMatrix()
{
//recover original matrix
glMultMatrixf(_startMatrix);
if (_isRotating) {
// Do some rotation according to start and current rotation vectors
//cerr << _currentRotationVector.transpose() << " " << _startRotationVector.transpose() << endl;
if ( ( _currentRotationVector - _startRotationVector).norm() > 1E-6 ) {
Eigen::Vector3d rotationAxis = _currentRotationVector.cross(_startRotationVector);
rotationAxis.normalize();
//FIXED by MY
Eigen::Matrix3d sm;
for(int i = 0 ; i < 3 ; i++)
for(int j = 0 ; j < 3 ; j++)
sm(i,j) = (double)_startMatrix[4*i+j];
rotationAxis = sm * rotationAxis;
double val = _currentRotationVector.dot(_startRotationVector);
val > (1-1E-10) ? val=1.0 : val=val ;
double rotationAngle = acos(val) * 180.0f/(float)M_PI;
// rotate around the current position
glRotatef(rotationAngle * 2, -rotationAxis.x(), -rotationAxis.y(),-rotationAxis.z());
}
}
}
/**
* \ingroup GLVisualization
* Stop the current rotation and prepare for a new click-then-drag event
*
**/
void Arcball::StopRotation()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
ApplyRotationMatrix();
_isRotating = false;
}
Eigen::Vector3d Arcball::ConvertXY(int x, int y)
{
int d = x*x+y*y;
float radiusSquared = _ballRadius * _ballRadius;
if (d > radiusSquared) {
return Eigen::Vector3d((float)x,(float)y, 0 );
}
else {
return Eigen::Vector3d((float)x,(float)y, sqrt(radiusSquared - d));
}
}
/**
* \ingroup GLVisualization
* Reset the current transformation to the identity
**/
void Arcball::Reset()
{
// reset matrix
memset(_startMatrix, 0, sizeof(_startMatrix));
_startMatrix[0] = 1;
_startMatrix[1] =0;
_startMatrix[2] = 0;
_startMatrix[3] = 0;
_startMatrix[4] = 0;
_startMatrix[5] =1;
_startMatrix[6] = 0;
_startMatrix[7] = 0;
_startMatrix[8] = 0;
_startMatrix[9] =0;
_startMatrix[10] = 1;
_startMatrix[11] = 0;
_startMatrix[12] = 0;
_startMatrix[13] =0;
_startMatrix[14] = 0;
_startMatrix[15] = 1;
_startZoomX = _startZoomY = 0;
}