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