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