root/OpenSceneGraph/trunk/src/osg/ComputeBoundsVisitor.cpp @ 13041

Revision 13041, 2.9 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
Line 
1/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2 *
3 * This library is open source and may be redistributed and/or modified under
4 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5 * (at your option) any later version.  The full license is in LICENSE file
6 * included with this distribution, and on the openscenegraph.org website.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 * OpenSceneGraph Public License for more details.
12*/
13
14#include <osg/ComputeBoundsVisitor>
15#include <osg/Transform>
16#include <osg/Drawable>
17#include <osg/Geode>
18
19using namespace osg;
20
21ComputeBoundsVisitor::ComputeBoundsVisitor(TraversalMode traversalMode):
22    osg::NodeVisitor(traversalMode)
23{
24}
25
26void ComputeBoundsVisitor::reset()
27{
28    _matrixStack.clear();
29    _bb.init();
30}
31
32void 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
45void 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
51void ComputeBoundsVisitor::apply(osg::Node& node)
52{
53    traverse(node);
54}
55
56void 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
70void 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
78void 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}
Note: See TracBrowser for help on using the browser.