SUMO - Simulation of Urban MObility
GUIOSGBoundingBoxCalculator.h
Go to the documentation of this file.
1 /****************************************************************************/
8 // Calculates the bounding box of an osg node
9 // original source: http://www.vis-sim.com/osg/code/osgcode_bbox1.htm
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
12 // Copyright (C) 2001-2016 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 #ifndef GUIOSGBoundingBoxCalculator_h
23 #define GUIOSGBoundingBoxCalculator_h
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #ifdef HAVE_OSG
35 
36 #include <osg/NodeVisitor>
37 #include <osg/BoundingBox>
38 #include <osg/BoundingSphere>
39 #include <osg/MatrixTransform>
40 #include <osg/Billboard>
41 
42 
43 // ===========================================================================
44 // class definitions
45 // ===========================================================================
54 class GUIOSGBoundingBoxCalculator : public osg::NodeVisitor {
55 public:
56  GUIOSGBoundingBoxCalculator() : NodeVisitor(NodeVisitor::TRAVERSE_ALL_CHILDREN) {
57  myTransformMatrix.makeIdentity();
58  }
59 
60  virtual ~GUIOSGBoundingBoxCalculator() {}
61 
62  void apply(osg::Geode& geode) {
63  osg::BoundingBox bbox;
64  for (int i = 0; i < (int)geode.getNumDrawables(); ++i) {
65 #if OSG_MIN_VERSION_REQUIRED(3,4,0)
66  bbox.expandBy(geode.getDrawable(i)->getBoundingBox());
67 #else
68  bbox.expandBy(geode.getDrawable(i)->getBound());
69 #endif
70  }
71  osg::BoundingBox bboxTrans;
72  for (int i = 0; i < 8; ++i) {
73  osg::Vec3 xvec = bbox.corner(i) * myTransformMatrix;
74  bboxTrans.expandBy(xvec);
75  }
76  myBoundingBox.expandBy(bboxTrans);
77  traverse(geode);
78  }
79 
80  void apply(osg::MatrixTransform& node) {
81  myTransformMatrix *= node.getMatrix();
82  traverse(node);
83  }
84 
85  void apply(osg::Billboard& node) {
86  traverse(node);
87  }
88 
89  osg::BoundingBox& getBoundingBox() {
90  return myBoundingBox;
91  }
92 
93 
94 protected:
95  osg::BoundingBox myBoundingBox; // the overall resultant bounding box
96  osg::Matrix myTransformMatrix; // the current transform matrix
97 
98 
99 };
100 
101 #endif
102 
103 #endif