SUMO - Simulation of Urban MObility
GUIOSGBoundingBoxCalculator.h
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
18 // Calculates the bounding box of an osg node
19 // original source: http://www.vis-sim.com/osg/code/osgcode_bbox1.htm
20 /****************************************************************************/
21 #ifndef GUIOSGBoundingBoxCalculator_h
22 #define GUIOSGBoundingBoxCalculator_h
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #ifdef HAVE_OSG
34 
35 #include <osg/NodeVisitor>
36 #include <osg/BoundingBox>
37 #include <osg/BoundingSphere>
38 #include <osg/MatrixTransform>
39 #include <osg/Billboard>
40 
41 
42 // ===========================================================================
43 // class definitions
44 // ===========================================================================
53 class GUIOSGBoundingBoxCalculator : public osg::NodeVisitor {
54 public:
55  GUIOSGBoundingBoxCalculator() : NodeVisitor(NodeVisitor::TRAVERSE_ALL_CHILDREN) {
56  myTransformMatrix.makeIdentity();
57  }
58 
59  virtual ~GUIOSGBoundingBoxCalculator() {}
60 
61  void apply(osg::Geode& geode) {
62  osg::BoundingBox bbox;
63  for (int i = 0; i < (int)geode.getNumDrawables(); ++i) {
64 #if OSG_MIN_VERSION_REQUIRED(3,4,0)
65  bbox.expandBy(geode.getDrawable(i)->getBoundingBox());
66 #else
67  bbox.expandBy(geode.getDrawable(i)->getBound());
68 #endif
69  }
70  osg::BoundingBox bboxTrans;
71  for (int i = 0; i < 8; ++i) {
72  osg::Vec3 xvec = bbox.corner(i) * myTransformMatrix;
73  bboxTrans.expandBy(xvec);
74  }
75  myBoundingBox.expandBy(bboxTrans);
76  traverse(geode);
77  }
78 
79  void apply(osg::MatrixTransform& node) {
80  myTransformMatrix *= node.getMatrix();
81  traverse(node);
82  }
83 
84  void apply(osg::Billboard& node) {
85  traverse(node);
86  }
87 
88  osg::BoundingBox& getBoundingBox() {
89  return myBoundingBox;
90  }
91 
92 
93 protected:
94  osg::BoundingBox myBoundingBox; // the overall resultant bounding box
95  osg::Matrix myTransformMatrix; // the current transform matrix
96 
97 
98 };
99 
100 #endif
101 
102 #endif