root/OpenSceneGraph/trunk/examples/osgkdtree/osgkdtree.cpp @ 8408

Revision 8408, 4.9 kB (checked in by robert, 6 years ago)

Further work on fleshing out basic classes

Line 
1/* OpenSceneGraph example, osgintersection.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osg/ArgumentParser>
20#include <osg/ApplicationUsage>
21#include <osg/Timer>
22#include <osg/CoordinateSystemNode>
23#include <osg/Notify>
24#include <osg/io_utils>
25#include <osg/Geometry>
26
27#include <osgDB/ReadFile>
28
29#include <osgUtil/IntersectionVisitor>
30#include <osgUtil/LineSegmentIntersector>
31
32#include <osgSim/LineOfSight>
33#include <osgSim/HeightAboveTerrain>
34#include <osgSim/ElevationSlice>
35
36#include <iostream>
37
38namespace osg
39{
40
41class KDNode
42{
43    public:
44   
45        typedef short value_type;
46
47        value_type _leftChild;
48        value_type _rightChild;
49};
50
51class KDLeaf : public osg::Referenced
52{
53    public:
54   
55        KDLeaf();
56   
57        typedef unsigned int index_type;
58        typedef std::vector< index_type > Indices;
59
60        Indices _vertexIndices;       
61   
62    protected:
63   
64        virtual ~KDLeaf() {}
65};
66
67class KDTree : public osg::Shape
68{
69    public:
70   
71   
72        KDTree() {}
73       
74        KDTree(const KDTree& rhs, const CopyOp& copyop=CopyOp::SHALLOW_COPY):
75            Shape(rhs,copyop) {}
76
77        META_Shape(osg, KDTree)
78   
79        typedef std::vector< unsigned int > AxisStack;
80        typedef std::vector< KDNode > KDNodeList;
81        typedef std::vector< osg::ref_ptr<KDLeaf> > KDLeafList;
82
83        osg::observer_ptr<osg::Geometry> _geometry;
84
85        osg::BoundingBox                _bb;
86
87        AxisStack                       _axisStack;
88        KDNodeList                      _kdNodes;
89        KDLeafList                      _kdLeaves;
90};
91
92class KDTreeTraverser
93{
94    public:
95   
96        void traverse(KDTree& tree, KDNode::value_type nodeIndex, unsigned int level)
97        {
98            for(unsigned int i=0; i<level; ++i)
99            {
100                osg::notify(osg::NOTICE)<<"  ";
101            }
102            osg::notify(osg::NOTICE)<<"traverse("<<nodeIndex<<", "<< level<<") { "<<std::endl;
103           
104            if (nodeIndex>=0)
105            {       
106                KDNode& node = tree._kdNodes[nodeIndex];
107                traverse(tree,node._leftChild,level+1);
108                traverse(tree,node._rightChild,level+1);
109            }
110            else 
111            {
112                KDNode::value_type leafIndex = -nodeIndex-1;
113                KDLeaf& leaf = *(tree._kdLeaves[leafIndex]);
114            }
115
116            for(unsigned int i=0; i<level; ++i)
117            {
118                osg::notify(osg::NOTICE)<<"  ";
119            }
120            osg::notify(osg::NOTICE)<<"}"<<std::endl;;
121        }
122
123
124        void traverse(KDTree& tree)
125        {
126            osg::notify(osg::NOTICE)<<"traverse(tree)"<<std::endl;
127            if (!tree._kdNodes.empty()) traverse(tree,0,0);
128        }
129   
130};
131
132
133class KDTreeBuilder : public osg::NodeVisitor
134{
135    public:
136   
137        KDTreeBuilder():
138            osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
139   
140   
141        void apply(osg::Geode& geode)
142        {
143            for(unsigned int i=0; i<geode.getNumDrawables(); ++i)
144            {
145                osg::Geometry* geom = geode.getDrawable(i)->asGeometry();
146                if (geom)
147                {
148                    geom->setShape(createKDTree(geom));
149                }   
150            }
151        }
152   
153        KDTree* createKDTree(osg::Geometry* geometry);
154};
155
156
157KDTree* KDTreeBuilder::createKDTree(osg::Geometry* geometry)
158{
159    KDTree* kdTree = new KDTree;
160    kdTree->_geometry = geometry;
161    kdTree->_bb = kdTree->_geometry->getBound();
162   
163    osg::notify(osg::NOTICE)<<"osg::KDTreeBuilder::createKDTree()"<<std::endl;
164   
165    return kdTree;
166}   
167
168}
169
170int main(int argc, char **argv)
171{
172    // use an ArgumentParser object to manage the program arguments.
173    osg::ArgumentParser arguments(&argc,argv);
174   
175    osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments);
176   
177    if (!scene)
178    {
179        std::cout<<"No model loaded, please specify a valid model on the command line."<<std::endl;
180        return 0;
181    }
182   
183    osg::KDTreeBuilder builder;
184    scene->accept(builder);
185   
186   
187   
188    return 0;
189}
Note: See TracBrowser for help on using the browser.