KdTree 5.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 
 *
 * This library is open source and may be redistributed and/or modified under  
 * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 
 * (at your option) any later version.  The full license is in LICENSE file
 * included with this distribution, and on the openscenegraph.org website.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * OpenSceneGraph Public License for more details.
*/

#ifndef OSG_KDTREE
#define OSG_KDTREE 1

#include <osg/Shape>
#include <osg/Geometry>

#include <map>

namespace osg
{

/** Implementation of a kdtree for Geometry leaves, to enable fast intersection tests.*/
class OSG_EXPORT KdTree : public osg::Shape 
{
    public:
    
        
        KdTree();
        
        KdTree(const KdTree& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY);

        META_Shape(osg, KdTree)
        
        struct OSG_EXPORT BuildOptions
        {
            BuildOptions();
                        
            unsigned int _numVerticesProcessed;
            unsigned int _targetNumTrianglesPerLeaf;
            unsigned int _maxNumLevels;
        };
        
        
        /** Build the kdtree from the specified source geometry object.
          * retun true on success. */ 
        virtual bool build(BuildOptions& buildOptions, osg::Geometry* geometry);
        
        struct LineSegmentIntersection
        {
            LineSegmentIntersection():
                ratio(-1.0),
                p0(0),
                p1(0),
                p2(0),
                r0(0.0f),
                r1(0.0f),
                r2(0.0f),
                primitiveIndex(0) {}

            bool operator < (const LineSegmentIntersection& rhs) const { return ratio < rhs.ratio; }

            typedef std::vector<unsigned int>   IndexList;
            typedef std::vector<double>         RatioList;

            double                          ratio;
            osg::Vec3d                      intersectionPoint;
            osg::Vec3                       intersectionNormal;

            unsigned int                    p0;
            unsigned int                    p1;
            unsigned int                    p2;
            float                           r0;
            float                           r1;
            float                           r2;

            unsigned int                    primitiveIndex;
        };        
        

        typedef std::vector<LineSegmentIntersection> LineSegmentIntersections;
        
        /** compute the intersection of a line segment and the kdtree, return true if an intersection has been found.*/
        virtual bool intersect(const osg::Vec3d& start, const osg::Vec3d& end, LineSegmentIntersections& intersections) const;


        typedef int value_type;

        struct KdNode
        {
            KdNode():
                first(0),
                second(0) {}

            KdNode(value_type f, value_type s):
                first(f),
                second(s) {}

            osg::BoundingBox bb;

            value_type first;    
            value_type second;
        };
    
        struct Triangle
        {
            Triangle():
                p0(0),p1(0),p2(0) {}
        
            Triangle(unsigned int ip0, unsigned int ip1, unsigned int ip2):
                p0(ip0), p1(ip1), p2(ip2) {}

            bool operator < (const Triangle& rhs) const
            {
                if (p0<rhs.p0) return true;
                if (p0>rhs.p0) return false;
                if (p1<rhs.p1) return true;
                if (p1>rhs.p1) return false;
                return p2<rhs.p2;
            }

            unsigned int p0;
            unsigned int p1;
            unsigned int p2;
        };

        typedef std::vector< KdNode >       KdNodeList;
        typedef std::vector< Triangle >     TriangleList;

        int addNode(const KdNode& node)
        {
            int num = static_cast<int>(_kdNodes.size()); 
            _kdNodes.push_back(node); 
            return num;
        }

        KdNode& getNode(int nodeNum) { return _kdNodes[nodeNum]; }
        const KdNode& getNode(int nodeNum) const { return _kdNodes[nodeNum]; }
        
        KdNodeList& getNodes() { return _kdNodes; }
        const KdNodeList& getNodes() const { return _kdNodes; }

        void setVertices(osg::Vec3Array* vertices) { _vertices = vertices; }
        const osg::Vec3Array* getVertices() const { return _vertices.get(); }

        unsigned int addTriangle(const Triangle& tri)
        {
            unsigned int num = static_cast<unsigned int>(_triangles.size());
            _triangles.push_back(tri);
            return num;
        }

        Triangle& getTriangle(unsigned int i) { return _triangles[i]; }
        const Triangle& getTriangle(unsigned int i) const { return _triangles[i]; }

        TriangleList& getTriangles() { return _triangles; }
        const TriangleList& getTriangles() const { return _triangles; }


    protected:

        osg::ref_ptr<osg::Vec3Array>        _vertices;
        KdNodeList                          _kdNodes;
        TriangleList                        _triangles;

};

class OSG_EXPORT KdTreeBuilder : public osg::NodeVisitor
{
    public:
    
        KdTreeBuilder();
    
        KdTreeBuilder(const KdTreeBuilder& rhs);

        META_NodeVisitor("osg","KdTreeBuilder")

        virtual KdTreeBuilder* clone() { return new KdTreeBuilder(*this); }

        void apply(osg::Geode& geode);
    
        KdTree::BuildOptions _buildOptions;

        osg::ref_ptr<osg::KdTree> _kdTreePrototype;


                
    protected:
    
        virtual ~KdTreeBuilder() {}
        
};

}

#endif