| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 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 | |
|---|
| 38 | namespace osg |
|---|
| 39 | { |
|---|
| 40 | |
|---|
| 41 | class KDNode |
|---|
| 42 | { |
|---|
| 43 | public: |
|---|
| 44 | |
|---|
| 45 | typedef short value_type; |
|---|
| 46 | |
|---|
| 47 | value_type _leftChild; |
|---|
| 48 | value_type _rightChild; |
|---|
| 49 | }; |
|---|
| 50 | |
|---|
| 51 | class 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 | |
|---|
| 67 | class 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 | |
|---|
| 92 | class 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 | |
|---|
| 133 | class 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 | |
|---|
| 157 | KDTree* 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 | |
|---|
| 170 | int main(int argc, char **argv) |
|---|
| 171 | { |
|---|
| 172 | |
|---|
| 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 | } |
|---|