| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <osg/ComputeBoundsVisitor> |
|---|
| 15 | #include <osg/Transform> |
|---|
| 16 | #include <osg/Drawable> |
|---|
| 17 | #include <osg/Geode> |
|---|
| 18 | |
|---|
| 19 | using namespace osg; |
|---|
| 20 | |
|---|
| 21 | ComputeBoundsVisitor::ComputeBoundsVisitor(TraversalMode traversalMode): |
|---|
| 22 | osg::NodeVisitor(traversalMode) |
|---|
| 23 | { |
|---|
| 24 | } |
|---|
| 25 | |
|---|
| 26 | void ComputeBoundsVisitor::reset() |
|---|
| 27 | { |
|---|
| 28 | _matrixStack.clear(); |
|---|
| 29 | _bb.init(); |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | void ComputeBoundsVisitor::getPolytope(osg::Polytope& polytope, float margin) const |
|---|
| 33 | { |
|---|
| 34 | float delta = _bb.radius()*margin; |
|---|
| 35 | polytope.add( osg::Plane(0.0, 0.0, 1.0, -(_bb.zMin()-delta)) ); |
|---|
| 36 | polytope.add( osg::Plane(0.0, 0.0, -1.0, (_bb.zMax()+delta)) ); |
|---|
| 37 | |
|---|
| 38 | polytope.add( osg::Plane(1.0, 0.0, 0.0, -(_bb.xMin()-delta)) ); |
|---|
| 39 | polytope.add( osg::Plane(-1.0, 0.0, 0.0, (_bb.xMax()+delta)) ); |
|---|
| 40 | |
|---|
| 41 | polytope.add( osg::Plane(0.0, 1.0, 0.0, -(_bb.yMin()-delta)) ); |
|---|
| 42 | polytope.add( osg::Plane(0.0, -1.0, 0.0, (_bb.yMax()+delta)) ); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | void ComputeBoundsVisitor::getBase(osg::Polytope& polytope, float margin) const |
|---|
| 46 | { |
|---|
| 47 | float delta = _bb.radius()*margin; |
|---|
| 48 | polytope.add( osg::Plane(0.0, 0.0, 1.0, -(_bb.zMin()-delta)) ); |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | void ComputeBoundsVisitor::apply(osg::Node& node) |
|---|
| 52 | { |
|---|
| 53 | traverse(node); |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | void ComputeBoundsVisitor::apply(osg::Transform& transform) |
|---|
| 57 | { |
|---|
| 58 | osg::Matrix matrix; |
|---|
| 59 | if (!_matrixStack.empty()) matrix = _matrixStack.back(); |
|---|
| 60 | |
|---|
| 61 | transform.computeLocalToWorldMatrix(matrix,this); |
|---|
| 62 | |
|---|
| 63 | pushMatrix(matrix); |
|---|
| 64 | |
|---|
| 65 | traverse(transform); |
|---|
| 66 | |
|---|
| 67 | popMatrix(); |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | void ComputeBoundsVisitor::apply(osg::Geode& geode) |
|---|
| 71 | { |
|---|
| 72 | for(unsigned int i=0; i<geode.getNumDrawables(); ++i) |
|---|
| 73 | { |
|---|
| 74 | applyDrawable(geode.getDrawable(i)); |
|---|
| 75 | } |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | void ComputeBoundsVisitor::applyDrawable(osg::Drawable* drawable) |
|---|
| 79 | { |
|---|
| 80 | if (_matrixStack.empty()) _bb.expandBy(drawable->getBound()); |
|---|
| 81 | else |
|---|
| 82 | { |
|---|
| 83 | osg::Matrix& matrix = _matrixStack.back(); |
|---|
| 84 | const osg::BoundingBox& dbb = drawable->getBound(); |
|---|
| 85 | if (dbb.valid()) |
|---|
| 86 | { |
|---|
| 87 | _bb.expandBy(dbb.corner(0) * matrix); |
|---|
| 88 | _bb.expandBy(dbb.corner(1) * matrix); |
|---|
| 89 | _bb.expandBy(dbb.corner(2) * matrix); |
|---|
| 90 | _bb.expandBy(dbb.corner(3) * matrix); |
|---|
| 91 | _bb.expandBy(dbb.corner(4) * matrix); |
|---|
| 92 | _bb.expandBy(dbb.corner(5) * matrix); |
|---|
| 93 | _bb.expandBy(dbb.corner(6) * matrix); |
|---|
| 94 | _bb.expandBy(dbb.corner(7) * matrix); |
|---|
| 95 | } |
|---|
| 96 | } |
|---|
| 97 | } |
|---|