OpenWalnut  1.5.0dev
WGEZoomTrackballNodeVisitor.cpp
1 //---------------------------------------------------------------------------
2 //
3 // Project: OpenWalnut ( http://www.openwalnut.org )
4 //
5 // Copyright 2009 OpenWalnut Community, BSV@Uni-Leipzig and CNCF@MPI-CBS
6 // For more information see http://www.openwalnut.org/copying
7 //
8 // This file is part of OpenWalnut.
9 //
10 // OpenWalnut is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // OpenWalnut is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
22 //
23 //---------------------------------------------------------------------------
24 
25 #include <iostream>
26 
27 #include <osg/Camera>
28 #include <osg/Drawable>
29 #include <osg/Geometry>
30 #include <osg/MatrixTransform>
31 #include <osg/Projection>
32 
33 #include "geodes/WGEGridNode.h"
34 #include "WGEZoomTrackballNodeVisitor.h"
35 
36 WGEZoomTrackballNodeVisitor::WGEZoomTrackballNodeVisitor( TraversalMode traversalMode ): osg::NodeVisitor( traversalMode )
37 {
38 }
39 
41 {
42  m_matrixStack.clear();
43  m_bb.init();
44 }
45 
46 void WGEZoomTrackballNodeVisitor::apply( osg::Camera& node ) // NOLINT
47 {
48  // Ignore all cameras that are a 2D orthogonal projection.
49  double left, right, bottom, top, zNear, zFar;
50  if( node.getProjectionMatrixAsOrtho( left, right, bottom, top, zNear, zFar ) && zNear == -1 && zFar == 1 )
51  {
52  return;
53  }
54  traverse( node );
55 }
56 
57 void WGEZoomTrackballNodeVisitor::apply( osg::Projection& node ) // NOLINT
58 {
59  // Ignore all 2D orthogonal projections.
60  double left, right, bottom, top, zNear, zFar;
61  if( node.getMatrix().getOrtho( left, right, bottom, top, zNear, zFar ) && zNear == -1 && zFar == 1 )
62  {
63  return;
64  }
65  traverse( node );
66 }
67 
68 void WGEZoomTrackballNodeVisitor::apply( osg::Drawable& node ) // NOLINT
69 {
70  osg::BoundingBox bb = node.getBoundingBox();
71  if( bb.valid() )
72  {
73  if( m_matrixStack.empty() )
74  {
75  m_bb.expandBy( bb );
76  }
77  else
78  {
79  osg::Matrix matrix = m_matrixStack.back();
80  m_bb.expandBy( bb.corner( 0 ) * matrix );
81  m_bb.expandBy( bb.corner( 1 ) * matrix );
82  m_bb.expandBy( bb.corner( 2 ) * matrix );
83  m_bb.expandBy( bb.corner( 3 ) * matrix );
84  m_bb.expandBy( bb.corner( 4 ) * matrix );
85  m_bb.expandBy( bb.corner( 5 ) * matrix );
86  m_bb.expandBy( bb.corner( 6 ) * matrix );
87  m_bb.expandBy( bb.corner( 7 ) * matrix );
88  }
89  }
90 }
91 
92 void WGEZoomTrackballNodeVisitor::apply( osg::MatrixTransform& node ) // NOLINT
93 {
94  osg::Matrix matrix;
95  if( !m_matrixStack.empty() )
96  {
97  matrix = m_matrixStack.back();
98  }
99  node.computeLocalToWorldMatrix( matrix, this );
100 
101  m_matrixStack.push_back( matrix );
102  traverse( node );
103  m_matrixStack.pop_back();
104 }
105 
107 {
108  return m_bb;
109 }
std::vector< osg::Matrix > m_matrixStack
The stack for the matrices.
virtual void reset()
Resets this visitor.
osg::BoundingBox & getBoundingBox()
Gets the bounding box.
void apply(osg::Camera &node)
Handles camera nodes.
osg::BoundingBox m_bb
The bounding box that is generated.
WGEZoomTrackballNodeVisitor(TraversalMode traversalMode=TRAVERSE_ACTIVE_CHILDREN)
Creates a new node visitor.