/* OpenSceneGraph example, osgintersection. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace osg { typedef int value_type; typedef std::vector< value_type > Indices; //#define VERBOSE_OUTPUT typedef std::pair< value_type, value_type> KDNode; typedef std::pair< value_type, value_type> KDLeaf; class KDTree : public osg::Shape { public: KDTree() {} KDTree(const KDTree& rhs, const CopyOp& copyop=CopyOp::SHALLOW_COPY): Shape(rhs,copyop) {} META_Shape(osg, KDTree) typedef std::vector< unsigned int > AxisStack; typedef std::vector< KDNode > KDNodeList; typedef std::vector< KDLeaf > KDLeafList; /// note, leafNum is negative to distinguish from nodeNum int addLeaf(const KDLeaf& leaf) { int num = _kdLeaves.size(); _kdLeaves.push_back(leaf); return -(num+1); } int replaceLeaf(int leafNum, const KDLeaf& leaf) { int num = -leafNum-1; if (num>_kdLeaves.size()-1) { osg::notify(osg::NOTICE)<<"Warning: replaceChild("<=0) { KDNode& node = tree._kdNodes[nodeIndex]; if (node.first) traverse(tree,node.first,level+1); else output(level+1)<<"empty left child()"<asGeometry(); if (geom) { geom->setShape(createKDTree(geom)); } } } KDTree* createKDTree(osg::Geometry* geometry); void computeDivisions(KDTree& kdTree); int divide(KDTree& kdTree, osg::BoundingBox& bb, int nodeIndex, unsigned int level); unsigned int _maxNumLevels; unsigned int _targetNumVerticesPerLeaf; unsigned int _numVerticesProcessed; }; KDTree* KDTreeBuilder::createKDTree(osg::Geometry* geometry) { #ifdef VERBOSE_OUTPUT osg::notify(osg::NOTICE)<<"osg::KDTreeBuilder::createKDTree()"<(geometry->getVertexArray()); if (!vertices) return 0; osg::ref_ptr kdTree = new KDTree; kdTree->_geometry = geometry; kdTree->_bb = kdTree->_geometry->getBound(); unsigned int estimatedSize = (unsigned int)(float(vertices->size())/float(_targetNumVerticesPerLeaf)*1.5); #ifdef VERBOSE_OUTPUT osg::notify(osg::NOTICE)<<"kdTree->_kdNodes.reserve()="<_kdNodes.reserve(estimatedSize); kdTree->_kdLeaves.reserve(estimatedSize); computeDivisions(*kdTree); _numVerticesProcessed += vertices->size(); kdTree->_vertexIndices.reserve(vertices->size()); for(unsigned int i=0; isize(); ++i) { kdTree->_vertexIndices.push_back(i); } KDLeaf leaf(0, kdTree->_vertexIndices.size()); int leafNum = kdTree->addLeaf(leaf); osg::BoundingBox bb = kdTree->_bb; int nodeNum = divide(*kdTree, bb, leafNum, 0); #ifdef VERBOSE_OUTPUT osg::notify(osg::NOTICE)<<"Root nodeNum="<_kdNodes.size()="<_kdNodes.size()<_kdLeaves.size()="<_kdLeaves.size()<=dimensions[1]) { if (dimensions[0]>=dimensions[2]) axis = 0; else axis = 2; } else if (dimensions[1]>=dimensions[2]) axis = 1; else axis = 2; kdTree._axisStack.push_back(axis); dimensions[axis] /= 2.0f; #ifdef VERBOSE_OUTPUT osg::notify(osg::NOTICE)<<" "<(kdTree._geometry->getVertexArray()); //osg::notify(osg::NOTICE)<<" divide leaf->_vertexIndices.size()="<_vertexIndices.size()<mid) { --right; } if (left scene = osgDB::readNodeFiles(arguments); if (!scene) { std::cout<<"No model loaded, please specify a valid model on the command line."<accept(updateVisitor); scene->getBound(); osg::Timer_t start = osg::Timer::instance()->tick(); osg::KDTreeBuilder builder; scene->accept(builder); osg::Timer_t end = osg::Timer::instance()->tick(); double time = osg::Timer::instance()->delta_s(start,end); osg::notify(osg::NOTICE)<<"Time to build "<