Developer Documentation

TrackballNode.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4 * Copyright (c) 2001-2015, RWTH-Aachen University *
5 * Department of Computer Graphics and Multimedia *
6 * All rights reserved. *
7 * www.openflipper.org *
8 * *
9 *---------------------------------------------------------------------------*
10 * This file is part of OpenFlipper. *
11 *---------------------------------------------------------------------------*
12 * *
13 * Redistribution and use in source and binary forms, with or without *
14 * modification, are permitted provided that the following conditions *
15 * are met: *
16 * *
17 * 1. Redistributions of source code must retain the above copyright notice, *
18 * this list of conditions and the following disclaimer. *
19 * *
20 * 2. Redistributions in binary form must reproduce the above copyright *
21 * notice, this list of conditions and the following disclaimer in the *
22 * documentation and/or other materials provided with the distribution. *
23 * *
24 * 3. Neither the name of the copyright holder nor the names of its *
25 * contributors may be used to endorse or promote products derived from *
26 * this software without specific prior written permission. *
27 * *
28 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41
42
43
44
45
46
47 //=============================================================================
48 //
49 // CLASS TrackballNode - IMPLEMENTATION
50 //
51 //=============================================================================
52
53
54 //== INCLUDES =================================================================
55
56
57 #include "TrackballNode.hh"
58 #include <ACG/GL/GLPrimitives.hh>
59
60
61 //== NAMESPACES ===============================================================
62
63
64 namespace ACG {
65 namespace SceneGraph {
66
67
68 //== IMPLEMENTATION ==========================================================
69
70
71 void
72 TrackballNode::draw(GLState& /* _state */ , const DrawModes::DrawMode& /* _drawMode */ )
73 {
74 // draw the track ball
75 if (drawTrackball_)
76 {
77 ACG::GLState::disable(GL_LIGHTING);
78 ACG::GLState::shadeModel( GL_FLAT );
79 glPushMatrix();
80 glTranslatef(center()[0], center()[1], center()[2]);
81 glScalef(radius_, radius_, radius_);
82
83 GLint previous[2];
84 glGetIntegerv(GL_POLYGON_MODE,previous);
85 glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
86 ACG::GLSphere sphere(20, 20);
87 sphere.draw_primitive();
88 glPolygonMode(GL_FRONT,previous[0]);
89 glPolygonMode(GL_BACK,previous[1]);
90 glPopMatrix();
91 }
92
93
94 // draw the coord axes
95 if (drawAxes_)
96 {
97 // store original settings
98 GLfloat backupColor[4], backupLineWidth;
99 glGetFloatv(GL_CURRENT_COLOR, backupColor);
100 glGetFloatv(GL_LINE_WIDTH, &backupLineWidth);
101
102 ACG::GLState::disable(GL_LIGHTING);
103 ACG::GLState::shadeModel( GL_FLAT );
104 glLineWidth(3.0);
105
106 glColor3f(1.0, 0.0, 0.0);
107 glBegin(GL_LINES);
108 glVertex(center());
109 glVertex(center() + radius_*xAxis_);
110 glEnd();
111
112 glColor3f(0.0, 1.0, 0.0);
113 glBegin(GL_LINES);
114 glVertex(center());
115 glVertex(center() + radius_*yAxis_);
116 glEnd();
117
118 glColor3f(0.0, 0.0, 1.0);
119 glBegin(GL_LINES);
120 glVertex(center());
121 glVertex(center() + radius_*zAxis_);
122 glEnd();
123
124 // restore original settings
125 glColor4fv(backupColor);
126 glLineWidth(backupLineWidth);
127 }
128 }
129
130
131 //----------------------------------------------------------------------------
132
133
134 void
135 TrackballNode::mouseEvent(GLState& _state, QMouseEvent* _event)
136 {
137 Vec3d oldPoint3D;
138 Vec2i newPoint2D(_event->pos().x(), _event->pos().y());
139 Vec3d newPoint3D;
140
141
142 switch (_event->type())
143 {
144 case QEvent::MouseButtonPress:
145 {
146 break;
147 }
148
149
150 case QEvent::MouseButtonDblClick:
151 {
152 if (mapToSphere(_state, oldPoint2D_, oldPoint3D))
153 {
154 // toggle drawTrackball_
155 if (_event->button() == Qt::LeftButton)
156 drawTrackball_ = !drawTrackball_;
157
158 // toggle drawAxes_
159 if (_event->button() == Qt::MidButton)
160 drawAxes_ = !drawAxes_;
161 }
162 break;
163 }
164
165
166 case QEvent::MouseMove:
167 {
168 bool hit0 = mapToSphere(_state, newPoint2D, newPoint3D);
169 bool hit1 = mapToSphere(_state, oldPoint2D_, oldPoint3D);
170
171 if (hit0 && hit1)
172 {
173
174 // scaling
175 if ((_event->button() & Qt::LeftButton) &&
176 (_event->button() & Qt::MidButton))
177 {
178 double s = 1.0 + ((double) (newPoint2D[1] - oldPoint2D_[1]) /
179 (double) _state.viewport_height());
180 scale(s);
181 }
182
183
184 // translation
185 else if (_event->button() & Qt::MidButton)
186 {
187 double value_x = (radius_ * ((newPoint2D[0] - oldPoint2D_[0]))
188 * 2.0 / (double) _state.viewport_width());
189
190 double value_y = (radius_ * ((newPoint2D[1] - oldPoint2D_[1]))
191 * 2.0 / (double) _state.viewport_height());
192
193
194 /* need inverse transposed matrix in order to
195 transform direction vectors */
196 GLMatrixd m = inverse_matrix();
197 m.transpose();
198
199
200 Vec3d dx, dy;
201
202 // axis aligned
203 if (drawAxes_)
204 {
205 dx = m.transform_point(xAxis_);
206 dy = m.transform_point(yAxis_);
207 }
208
209 // screen space translation
210 else
211 {
212 dx = m.transform_vector(_state.right());
213 dy = m.transform_vector(_state.up());
214 }
215
216 dx.normalize();
217 dy.normalize();
218 translate(value_x*dx - value_y*dy);
219 }
220
221
222
223 // rotation
224 else if (_event->button() & Qt::LeftButton)
225 {
226 Vec3d axis = oldPoint3D % newPoint3D;
227 double cos_angle = ( oldPoint3D | newPoint3D );
228
229
230 if (fabs(cos_angle) < 1.0)
231 {
232 // rotate coord axes
233 if (_event->modifiers() & Qt::AltModifier)
234 {
235 GLMatrixd mat;
236
237 mat.identity();
238 mat.rotate(acos(cos_angle)*180.0/M_PI, axis);
239
240 /* transform_point works since only
241 rotations and no translations are
242 involved */
243 xAxis_ = mat.transform_point(xAxis_);
244 yAxis_ = mat.transform_point(yAxis_);
245 zAxis_ = mat.transform_point(zAxis_);
246 }
247
248 // normal rotation
249 else rotate(acos(cos_angle)*180.0/M_PI, axis);
250 }
251 }
252 }
253
254
255 break;
256 }
257
258 default: // avoid warning
259 break;
260 }
261
262
263 // store 2D point
264 oldPoint2D_ = newPoint2D;
265 }
266
267
268 //----------------------------------------------------------------------------
269
270
271 bool
272 TrackballNode::mapToSphere( GLState& _state,
273 const Vec2i& _v2,
274 Vec3d& _v3 )
275 {
276 // Qt -> GL coordinate systems
277 unsigned int x = _v2[0];
278 unsigned int y = _state.context_height() - _v2[1];
279
280
281 // get ray from eye through pixel (trackball coords)
282 Vec3d origin, direction;
283 _state.viewing_ray(x, y, origin, direction);
284
285
286 // translate and scale trackball to unit sphere
287 origin -= center();
288 origin /= radius_;
289
290
291 // calc sphere-ray intersection
292 // (sphere is centered at origin, has radius 1)
293 double a = direction.sqrnorm(),
294 b = 2.0 * (origin | direction),
295 c = origin.sqrnorm() - 1.0,
296 d = b*b - 4.0*a*c,
297 t;
298
299 if (d < 0.0) return false;
300 else if (d == 0.0) t = -b / (2.0*a);
301 else
302 {
303 // t1 = (-b - sqrt(d)) / (2.0*a), t2 = (-b + sqrt(d)) / (2.0*a)
304 a = 1.0 / (2.0*a);
305 d = sqrt(d);
306 double t1 = (-b - d) * a;
307 double t2 = (-b + d) * a;
308 t = (t1 < t2) ? t1 : t2;
309 }
310
311 _v3 = origin + direction*t;
312
313 return true;
314 }
315
316
317 //=============================================================================
318 } // namespace SceneGraph
319 } // namespace ACG
320 //=============================================================================
ACG::GLMatrixT::rotate
void rotate(Scalar angle, Scalar x, Scalar y, Scalar z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
Definition: GLMatrixT_impl.hh:155
ACG::SceneGraph::TransformNode::translate
void translate(const Vec3d &_v)
Add a translation to the current Transformation.
Definition: TransformNode.cc:125
ACG::GLState
Definition: GLState.hh:213
ACG::SceneGraph::TransformNode::center
const Vec3d & center() const
get center
Definition: TransformNode.hh:115
ACG::GLState::up
Vec3d up() const
get up-vector w.r.t. camera coordinates
Definition: GLState.cc:906
ACG::GLState::viewport_width
int viewport_width() const
get viewport width
Definition: GLState.hh:822
ACG
Namespace providing different geometric functions concerning angles.
Definition: BaseObjectData.hh:68
ACG::Matrix4x4T::transpose
void transpose()
transpose matrix
Definition: Matrix4x4T_impl.hh:266
ACG::SceneGraph::ACG::GLState::disable
static void disable(GLenum _cap, bool _warnRemoved=true)
replaces glDisable, but supports locking
ACG::SceneGraph::TrackballNode::mapToSphere
bool mapToSphere(GLState &_state, const Vec2i &_v2, Vec3d &_v3)
Map 2D-screen-coords to trackball.
Definition: TrackballNode.cc:272
OpenMesh::VectorT
Definition: Vector11T.hh:83
ACG::Matrix4x4T::identity
void identity()
setup an identity matrix
Definition: Matrix4x4T_impl.hh:250
ACG::SceneGraph::TrackballNode::mouseEvent
virtual void mouseEvent(GLState &_state, QMouseEvent *_event) override
get mouse events
Definition: TrackballNode.cc:135
ACG::Matrix4x4T::transform_point
VectorT< T, 3 > transform_point(const VectorT< T, 3 > &_v) const
transform point (x&#39;,y&#39;,z&#39;,1) = M * (x,y,z,1)
Definition: Matrix4x4T_impl.hh:196
SceneGraph
Definition: QtPrincipalAxisDialog.hh:67
ACG::SceneGraph::TransformNode::inverse_matrix
const GLMatrixd & inverse_matrix() const
return inverse matrix
Definition: TransformNode.hh:160
ACG::GLState::right
Vec3d right() const
get right-vector w.r.t. camera coordinates
Definition: GLState.cc:918
ACG::GLMatrixT< double >
ACG::GLState::viewport_height
int viewport_height() const
get viewport height
Definition: GLState.hh:824
ACG::Matrix4x4T::transform_vector
VectorT< T, 3 > transform_vector(const VectorT< T, 3 > &_v) const
transform vector (x&#39;,y&#39;,z&#39;,0) = A * (x,y,z,0)
Definition: Matrix4x4T_impl.hh:219
ACG::GLSphere
Definition: GLPrimitives.hh:139
ACG::glVertex
void glVertex(const Vec2i &_v)
Wrapper: glVertex for Vec2i.
Definition: gl.hh:95
ACG::SceneGraph::DrawModes::DrawMode
Specifies a DrawMode.
Definition: DrawModes.hh:288
ACG::GLState::viewing_ray
void viewing_ray(int _x, int _y, Vec3d &_origin, Vec3d &_direction) const
Definition: GLState.cc:930
OpenMesh::VectorT::sqrnorm
decltype(std::declval< S >() *std::declval< S >()) sqrnorm() const
compute squared euclidean norm
Definition: Vector11T.hh:402
ACG::GLState::context_height
int context_height() const
get gl context height
Definition: GLState.hh:830
ACG::SceneGraph::TransformNode::rotate
void rotate(double _angle, const Vec3d &_axis)
Definition: TransformNode.cc:148
ACG::SceneGraph::TrackballNode::draw
void draw(GLState &_state, const DrawModes::DrawMode &_drawMode) override
Draw the trackball + axes (if enabled)
Definition: TrackballNode.cc:72
OpenMesh::VectorT::normalize
auto normalize() -> decltype(*this/=std::declval< VectorT< S, DIM >>().norm())
Definition: Vector11T.hh:434
ACG::SceneGraph::ACG::GLState::shadeModel
static void shadeModel(GLenum _mode)
replaces glShadeModel, supports locking
ACG::SceneGraph::TransformNode::scale
const GLMatrixd & scale() const
return scale matrix
Definition: TransformNode.hh:181