File: KdTree

package info (click to toggle)
openscenegraph 3.6.5%2Bdfsg1-8
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 40,028 kB
  • sloc: cpp: 392,065; ansic: 21,495; java: 1,020; yacc: 548; makefile: 430; objc: 406; xml: 155; lex: 151; javascript: 34
file content (220 lines) | stat: -rw-r--r-- 7,594 bytes parent folder | download | duplicates (4)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* -*-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);


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


        typedef std::vector< unsigned int > Indices;

        // index in the VertexIndices vector
        void setPrimitiveIndices(const Indices& indices) { _primitiveIndices = indices; }
        Indices& getPrimitiveIndices() { return _primitiveIndices; }
        const Indices& getPrimitiveIndices() const { return _primitiveIndices; }

        // vector containing the primitive vertex index data packed as no_vertice_indices then vertex indices ie. for points it's (1, p0), for lines (2, p0, p1) etc.
        void setVertexIndices(const Indices& indices) { _vertexIndices = indices; }
        Indices& getVertexIndices() { return _vertexIndices; }
        const Indices& getVertexIndices() const { return _vertexIndices; }


        inline unsigned int addPoint(unsigned int p0)
        {
            unsigned int i = _vertexIndices.size();
            _vertexIndices.push_back(_primitiveIndices.size() + _degenerateCount);
            _vertexIndices.push_back(1);
            _vertexIndices.push_back(p0);
            _primitiveIndices.push_back(i);
            return i;
        }
        inline unsigned int addLine(unsigned int p0, unsigned int p1)
        {
            unsigned int i = _vertexIndices.size();
            _vertexIndices.push_back(_primitiveIndices.size() + _degenerateCount);
            _vertexIndices.push_back(2);
            _vertexIndices.push_back(p0);
            _vertexIndices.push_back(p1);
            _primitiveIndices.push_back(i);
            return i;
        }

        inline unsigned int addTriangle(unsigned int p0, unsigned int p1, unsigned int p2)
        {
            unsigned int i = _vertexIndices.size();
            _vertexIndices.push_back(_primitiveIndices.size() + _degenerateCount);
            _vertexIndices.push_back(3);
            _vertexIndices.push_back(p0);
            _vertexIndices.push_back(p1);
            _vertexIndices.push_back(p2);
            _primitiveIndices.push_back(i);
            return i;
        }

        inline unsigned int addQuad(unsigned int p0, unsigned int p1, unsigned int p2, unsigned int p3)
        {
            unsigned int i = _vertexIndices.size();
            _vertexIndices.push_back(_primitiveIndices.size() + _degenerateCount);
            _vertexIndices.push_back(4);
            _vertexIndices.push_back(p0);
            _vertexIndices.push_back(p1);
            _vertexIndices.push_back(p2);
            _vertexIndices.push_back(p3);
            _primitiveIndices.push_back(i);
            return i;
        }



        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;
        };
        typedef std::vector< KdNode >       KdNodeList;

        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; }


        template<class IntersectFunctor>
        void intersect(IntersectFunctor& functor, const KdNode& node) const
        {
            if (node.first<0)
            {
                // treat as a leaf
                int istart = -node.first-1;
                int iend = istart + node.second;

                for(int i=istart; i<iend; ++i)
                {
                    unsigned int primitiveIndex = _primitiveIndices[i];
                    unsigned int originalPIndex = _vertexIndices[primitiveIndex++];
                    unsigned int numVertices = _vertexIndices[primitiveIndex++];
                    switch(numVertices)
                    {
                        case(1): functor.intersect(_vertices.get(), originalPIndex, _vertexIndices[primitiveIndex]); break;
                        case(2): functor.intersect(_vertices.get(), originalPIndex, _vertexIndices[primitiveIndex], _vertexIndices[primitiveIndex+1]); break;
                        case(3): functor.intersect(_vertices.get(), originalPIndex, _vertexIndices[primitiveIndex], _vertexIndices[primitiveIndex+1], _vertexIndices[primitiveIndex+2]); break;
                        case(4): functor.intersect(_vertices.get(), originalPIndex, _vertexIndices[primitiveIndex], _vertexIndices[primitiveIndex+1], _vertexIndices[primitiveIndex+2], _vertexIndices[primitiveIndex+3]); break;
                        default : OSG_NOTICE<<"Warning: KdTree::intersect() encounted unsupported primitive size of "<<numVertices<<std::endl; break;
                    }
                }
            }
            else if (functor.enter(node.bb))
            {
                if (node.first>0) intersect(functor, _kdNodes[node.first]);
                if (node.second>0) intersect(functor, _kdNodes[node.second]);

                functor.leave();
            }
        }

        unsigned int _degenerateCount;

    protected:

        osg::ref_ptr<osg::Vec3Array>    _vertices;
        Indices                         _primitiveIndices;
        Indices                         _vertexIndices;
        KdNodeList                      _kdNodes;
};

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(Geometry& geometry);

        KdTree::BuildOptions _buildOptions;

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



    protected:

        virtual ~KdTreeBuilder() {}

};

}

#endif