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 (unsigned int i = 0; i < geode.getNumDrawables(); ++i) {
65  bbox.expandBy(geode.getDrawable(i)->getBound());
66  }
67  osg::BoundingBox bboxTrans;
68  for (unsigned int i = 0; i < 8; ++i) {
69  osg::Vec3 xvec = bbox.corner(i) * myTransformMatrix;
70  bboxTrans.expandBy(xvec);
71  }
72  myBoundingBox.expandBy(bboxTrans);
73  traverse(geode);
74  }
75 
76  void apply(osg::MatrixTransform& node) {
77  myTransformMatrix *= node.getMatrix();
78  traverse(node);
79  }
80 
81  void apply(osg::Billboard& node) {
82  traverse(node);
83  }
84 
85  osg::BoundingBox& getBoundingBox() {
86  return myBoundingBox;
87  }
88 
89 
90 protected:
91  osg::BoundingBox myBoundingBox; // the overall resultant bounding box
92  osg::Matrix myTransformMatrix; // the current transform matrix
93 
94 
95 };
96 
97 #endif
98 
99 #endif