Developer Documentation
TransformNode.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 TransformNode - IMPLEMENTATION
50//
51//=============================================================================
52
53
54//== INCLUDES =================================================================
55
56#include "TransformNode.hh"
57
58
59//== IMPLEMENTATION ==========================================================
60
61
62namespace ACG {
63namespace SceneGraph {
64
65
66//----------------------------------------------------------------------------
67
68
70TransformNode(BaseNode* _parent, const std::string& _name)
71 : BaseNode(_parent,_name),
72 center_(0.0, 0.0, 0.0),
73 applyTransformation_(true),
74 is2DObject_(false), // ortho 2d stuff
75 isPerSkeletonObject_(false),
76 scaleFactor2D_(1.0),
77 imageDimensions_(ACG::Vec2i(-1,-1) ), // no image per default
78 offset_( ACG::Vec2d(0.0,0.0) )
79{
81}
82
83
84
85//----------------------------------------------------------------------------
86
87
88void
91{
92 matrix_.identity();
93 inverse_matrix_.identity();
94
95 rotation_matrix_.identity();
96 inverse_rotation_matrix_.identity();
97 quaternion_.identity();
98
99 scale_matrix_.identity();
100 inverse_scale_matrix_.identity();
101
102 translation_ = Vec3d(0.0, 0.0, 0.0);
103}
104
105
106//----------------------------------------------------------------------------
107
108
109void
112{
113 Vec3d v3 = matrix_.transform_point(center());
114 set_center(v3);
115 loadIdentity();
116}
117
118
119//----------------------------------------------------------------------------
120
121
122void
124translate(const Vec3d& _v)
125{
126 translation_ += _v;
127 updateMatrix();
128}
129
130//----------------------------------------------------------------------------
131
132
133void
135setTranslation(const Vec3d& _v)
136{
137 translation_ = _v;
138 updateMatrix();
139}
140
141
142//----------------------------------------------------------------------------
143
144
145void
147rotate(double _angle, const Vec3d& _axis)
148{
149 quaternion_ *= Quaterniond(_axis, _angle/180.0*M_PI);
150 quaternion_.normalize();
151 rotation_matrix_ = quaternion_.rotation_matrix();
152 inverse_rotation_matrix_ = rotation_matrix_;
153 inverse_rotation_matrix_.transpose();
154 updateMatrix();
155}
156
157
158//----------------------------------------------------------------------------
159
160
161void
163setRotation(const Quaterniond& rotation)
164{
165 quaternion_ = rotation;
166 quaternion_.normalize();
167 rotation_matrix_ = quaternion_.rotation_matrix();
168 inverse_rotation_matrix_ = rotation_matrix_;
169 inverse_rotation_matrix_.transpose();
170 updateMatrix();
171}
172
173
174//----------------------------------------------------------------------------
175
176
177void
179scale(const Vec3d& _s)
180{
181 scale_matrix_.scale(_s);
182 inverse_scale_matrix_ = scale_matrix_;
183 inverse_scale_matrix_.invert();
184 updateMatrix();
185}
186
187//----------------------------------------------------------------------------
188
189
190void
192scale(const GLMatrixd& _m)
193{
194 scale_matrix_ *= _m;
195 inverse_scale_matrix_ = scale_matrix_;
196 inverse_scale_matrix_.invert();
197 updateMatrix();
198}
199
200
201//----------------------------------------------------------------------------
202
203
204void
207{
208 // build matrix
209 matrix_.identity();
210 matrix_.translate(translation_);
211 matrix_.translate(center_);
212 matrix_ *= rotation_matrix_;
213 matrix_ *= scale_matrix_;
214 matrix_.translate(-center_);
215
216
217 // build inverse matrix
218 inverse_matrix_ = matrix_;
219 inverse_matrix_.invert();
220}
221
222
223//----------------------------------------------------------------------------
224
225
226void
228enter(GLState& _state, const DrawModes::DrawMode& /* _drawmode */ )
229{
230 _state.push_modelview_matrix();
231 _state.push_projection_matrix();
232
233 /*
234 double modelview[16];
235 glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
236 double projection[16];
237 glGetDoublev( GL_PROJECTION_MATRIX, projection );
238 GLint viewport[4];
239 glGetIntegerv(GL_VIEWPORT, viewport);
240
241
242 std::cout << "****************************** enter " << name() << std::endl;
243
244 std::cout << "\nenter: mv:" << std::endl;
245 for (uint i = 0; i < 16; ++i)
246 {
247 if (i%4==0)
248 std::cerr << std::endl;
249 std::cerr << modelview[i] << ", " << std::flush;
250 }
251
252
253 std::cout << "\nenter: pr:" << std::endl;
254 for (uint i = 0; i < 16; ++i)
255 {
256 if (i%4==0)
257 std::cerr << std::endl;
258 std::cerr << projection[i] << ", " << std::flush;
259 }
260
261 std::cout << "\nenter: vp:" << std::endl;
262 for (uint i = 0; i < 4; ++i)
263 {
264 std::cerr << viewport[i] << ", " << std::flush;
265 }
266
267
268 std::cout << "enter: mv = " << _state.modelview() << std::endl;
269 std::cout << "enter: pr = " << _state.projection() << std::endl;
270 std::cout << "enter: vp = " << _state.viewport() << std::endl;
271 */
272
273
274 if ( applyTransformation_ )
275 _state.mult_matrix(matrix_, inverse_matrix_);
276
277 if (is2DObject_)
278 ortho2DMode(_state);
279
280 if(isPerSkeletonObject_)
281 perSkeletonMode(_state);
282}
283
284
285//----------------------------------------------------------------------------
286
287
288void
290leave(GLState& _state, const DrawModes::DrawMode& /* _drawmode */ )
291{
292// _state.pop_projection_matrix();
293 _state.pop_modelview_matrix();
294 _state.pop_projection_matrix();
295}
296
297//----------------------------------------------------------------------------
298/*
299void
300TransformNode::
301ortho2DMode(GLState& _state)
302{
303// return;
304
305 double modelview[16];
306 glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
307 double projection[16];
308 glGetDoublev( GL_PROJECTION_MATRIX, projection );
309 GLint viewport[4];
310 glGetIntegerv(GL_VIEWPORT, viewport);
311
312 std::cout << "\n**************************************" << std::endl;
313 std::cout << "modelview: " << std::flush;
314 for (uint i = 0; i < 16; ++i)
315 std::cout << modelview[i] << ", " << std::flush;
316
317 std::cout << "\nprojection: " << std::flush;
318 for (uint i = 0; i < 16; ++i)
319 std::cout << projection[i] << ", " << std::flush;
320
321
322
323// std::cout << "****************************** ortho2DMode *******************************" << std::endl;
324 // set the whole GL matrices such that subsequent rendering of 2d
325 // image coordinates produces correct results
326 int width = _state.viewport_width();
327 int height = _state.viewport_height();
328
329 glViewport(0, 0, width, height);
330 glMatrixMode(GL_PROJECTION);
331 glLoadIdentity();
332
333 gluOrtho2D(0.0, (GLdouble)width, (GLdouble)height, 0.0);
334 glMatrixMode(GL_MODELVIEW);
335 glLoadIdentity();
336 // move image center to window center
337 glTranslatef( 0.5*(width-1), 0.5*(height-1), 0);
338 glScalef(scaleFactor2D_, scaleFactor2D_, 1.0);
339
340 if (imageDimensions_[0] != -1)
341 glTranslatef( -0.5*(imageDimensions_[0]-1), -0.5*(imageDimensions_[1]-1), 0);
342 glTranslatef(offset_[0], offset_[1], 0);
343
344
345 glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
346 glGetDoublev( GL_PROJECTION_MATRIX, projection );
347 glGetIntegerv(GL_VIEWPORT, viewport);
348
349 std::cout << "\nmodelview: " << std::flush;
350 for (uint i = 0; i < 16; ++i)
351 std::cout << modelview[i] << ", " << std::flush;
352
353 std::cout << "\nprojection: " << std::flush;
354 for (uint i = 0; i < 16; ++i)
355 std::cout << projection[i] << ", " << std::flush;
356
357}
358*/
359
360//----------------------------------------------------------------------------
361
362
363
364void
365TransformNode::
366perSkeletonMode(GLState& _state)
367{
368 _state.set_modelview(perSkeletonModelView_);
369}
370
371
372
373void
374TransformNode::
375ortho2DMode(GLState& _state)
376{
377 // set ortho 2D mode in glstate
378 int width = _state.viewport_width();
379 int height = _state.viewport_height();
380
381 if (width == 0 || height == 0)
382 return;
383
384// _state.viewport(0,0,width, height);
385 _state.reset_projection();
386
387 _state.ortho(-(GLdouble)width/2.0, (GLdouble)width/2.0, -(GLdouble)height/2.0, (GLdouble)height/2.0, 0.01, 20.0);
388// _state.ortho(0.0, (GLdouble)width, (GLdouble)height, 0.0, 0.01,20.0);
389
390 _state.reset_modelview();
391
392 _state.lookAt( Vec3d(0.0,0.0,0.0),
393 Vec3d(0.0,0.0,1.0),
394 Vec3d(0.0,-1.0,0.0)); // flip up direction (y-axis) s.t. opengl coordsys matches image coordsys
395
396 _state.scale(scaleFactor2D_, scaleFactor2D_, 1.0);
397
398 // move image center to window center
399 if (imageDimensions_[0] != -1)
400 _state.translate(-0.5*(imageDimensions_[0]-1), -0.5*(imageDimensions_[1]-1), 0);
401
402 _state.translate(offset_[0], offset_[1], 0);
403}
404
405//----------------------------------------------------------------------------
406
407//=============================================================================
408} // namespace SceneGraph
409} // namespace ACG
410//=============================================================================
void scale(Scalar _x, Scalar _y, Scalar _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with scaling matrix (x,y,z)
void translate(Scalar _x, Scalar _y, Scalar _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply self with translation matrix (x,y,z)
void ortho(double _left, double _right, double _bottom, double _top, double _near_plane, double _far_plane)
orthographic projection
Definition: GLState.cc:402
void pop_modelview_matrix()
pop modelview matrix
Definition: GLState.cc:1026
void lookAt(const Vec3d &_eye, const Vec3d &_center, const Vec3d &_up)
set camera by lookAt
Definition: GLState.cc:515
void pop_projection_matrix()
pop projection matrix
Definition: GLState.cc:989
int viewport_width() const
get viewport width
Definition: GLState.hh:847
void push_projection_matrix()
push projection matrix
Definition: GLState.cc:971
void reset_modelview()
reset modelview matrix (load identity)
Definition: GLState.cc:370
void scale(double _s)
scale by (_s, _s, _s)
Definition: GLState.hh:775
void translate(double _x, double _y, double _z, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
translate by (_x, _y, _z)
Definition: GLState.cc:533
void reset_projection()
reset projection matrix (load identity)
Definition: GLState.cc:334
void set_modelview(const GLMatrixd &_m)
set modelview
Definition: GLState.hh:753
void mult_matrix(const GLMatrixd &_m, const GLMatrixd &_inv_m, MultiplyFrom _mult_from=MULT_FROM_RIGHT)
multiply by a given transformation matrix
Definition: GLState.cc:614
int viewport_height() const
get viewport height
Definition: GLState.hh:849
void push_modelview_matrix()
push modelview matrix
Definition: GLState.cc:1010
void identity()
setup an identity matrix
bool invert()
matrix inversion (returns true on success)
void transpose()
transpose matrix
VectorT< T, 3 > transform_point(const VectorT< T, 3 > &_v) const
transform point (x',y',z',1) = M * (x,y,z,1)
void identity()
identity rotation
Definition: QuaternionT.hh:123
Matrix rotation_matrix() const
cast to rotation matrix
Definition: QuaternionT.hh:176
const Vec3d & center() const
get center
const GLMatrixd & scale() const
return scale matrix
void enter(GLState &_state, const DrawModes::DrawMode &_drawmode) override
set current GL-color and GL-material
void set_center(const Vec3d &_c)
set center
void leave(GLState &_state, const DrawModes::DrawMode &_drawmode) override
restores original GL-color and GL-material
const GLMatrixd & rotation() const
return rotation matrix
void setRotation(const Quaterniond &rotation)
rotation setter
void setTranslation(const Vec3d &_v)
translation setter
void updateMatrix()
update matrix
void rotate(double _angle, const Vec3d &_axis)
TransformNode(BaseNode *_parent=0, const std::string &_name="<TransformNode>")
Constructor.
void translate(const Vec3d &_v)
Add a translation to the current Transformation.
auto normalize() -> decltype(*this/=std::declval< VectorT< S, DIM > >().norm())
Definition: Vector11T.hh:454
Namespace providing different geometric functions concerning angles.
VectorT< double, 3 > Vec3d
Definition: VectorT.hh:121