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-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
16 // Calculates the bounding box of an osg node
17 // original source: http://www.vis-sim.com/osg/code/osgcode_bbox1.htm
18 /****************************************************************************/
19 #ifndef GUIOSGBoundingBoxCalculator_h
20 #define GUIOSGBoundingBoxCalculator_h
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #ifdef HAVE_OSG
28 
29 #include <osg/NodeVisitor>
30 #include <osg/BoundingBox>
31 #include <osg/BoundingSphere>
32 #include <osg/MatrixTransform>
33 #include <osg/Billboard>
34 
35 
36 // ===========================================================================
37 // class definitions
38 // ===========================================================================
47 class GUIOSGBoundingBoxCalculator : public osg::NodeVisitor {
48 public:
49  GUIOSGBoundingBoxCalculator() : NodeVisitor(NodeVisitor::TRAVERSE_ALL_CHILDREN) {
50  myTransformMatrix.makeIdentity();
51  }
52 
53  virtual ~GUIOSGBoundingBoxCalculator() {}
54 
55  void apply(osg::Geode& geode) {
56  osg::BoundingBox bbox;
57  for (int i = 0; i < (int)geode.getNumDrawables(); ++i) {
58 #if OSG_MIN_VERSION_REQUIRED(3,4,0)
59  bbox.expandBy(geode.getDrawable(i)->getBoundingBox());
60 #else
61  bbox.expandBy(geode.getDrawable(i)->getBound());
62 #endif
63  }
64  osg::BoundingBox bboxTrans;
65  for (int i = 0; i < 8; ++i) {
66  osg::Vec3 xvec = bbox.corner(i) * myTransformMatrix;
67  bboxTrans.expandBy(xvec);
68  }
69  myBoundingBox.expandBy(bboxTrans);
70  traverse(geode);
71  }
72 
73  void apply(osg::MatrixTransform& node) {
74  myTransformMatrix *= node.getMatrix();
75  traverse(node);
76  }
77 
78  void apply(osg::Billboard& node) {
79  traverse(node);
80  }
81 
82  osg::BoundingBox& getBoundingBox() {
83  return myBoundingBox;
84  }
85 
86 
87 protected:
88  osg::BoundingBox myBoundingBox; // the overall resultant bounding box
89  osg::Matrix myTransformMatrix; // the current transform matrix
90 
91 
92 };
93 
94 #endif
95 
96 #endif