/* 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 "fixeddivision.h" namespace fixeddivision { struct TriangleIndicesCollector { TriangleIndicesCollector(): _indices(0) { } inline void operator () (unsigned int v1, unsigned int v2, unsigned int v3) { _indices->push_back(v1); _indices->push_back(v2); _indices->push_back(v3); } Indices* _indices; }; KDTree* KDTreeBuilder::createKDTree(osg::Geometry* geometry) { #ifdef VERBOSE_OUTPUT osg::notify(osg::NOTICE)<<"osg::KDTreeBuilder::createKDTree()"<(geometry->getVertexArray()); if (!vertices) return 0; if (vertices->size() <= _targetNumVerticesPerLeaf) return 0; osg::ref_ptr kdTree = new KDTree; kdTree->_geometry = geometry; kdTree->_bb = kdTree->_geometry->getBound(); kdTree->_vertices = vertices; unsigned int estimatedSize = (unsigned int)(2.0*float(vertices->size())/float(_targetNumVerticesPerLeaf)); #ifdef VERBOSE_OUTPUT osg::notify(osg::NOTICE)<<"kdTree->_kdNodes.reserve()="<_kdNodes.reserve(estimatedSize); kdTree->_kdLeaves.reserve(estimatedSize); computeDivisions(*kdTree); _numVerticesProcessed += vertices->size(); if (_processTriangles) { osg::TriangleIndexFunctor collectTriangleIndices; collectTriangleIndices._indices = &(kdTree->_vertexIndices); geometry->accept(collectTriangleIndices); KDPrimitiveLeaf* primitiveLeaf = createKDPrimitiveLeaf(); primitiveLeaf->_indices.insert(primitiveLeaf->_indices.end(), kdTree->_vertexIndices.begin(), kdTree->_vertexIndices.end()); // osg::notify(osg::NOTICE)<<"kdTree->_vertexIndices.size()="<_vertexIndices.size()<addPrimitiveLeaf(primitiveLeaf); osg::BoundingBox bb = kdTree->_bb; int nodeNum = divideTriangles(*kdTree, bb, leafNum, 0); std::cout<<"_vertexIndices.size() = "<_vertexIndices.size()<_kdNodes.size()="<_kdNodes.size()<_kdLeaves.size()="<_kdLeaves.size()<_kdNodes.size()="<_kdNodes.size()<<" estimated size = "<_kdLeaves.size()="<_kdLeaves.size()<<" estimated 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)<<" "<_indices.size()<=_targetNumIndicesPerLeaf) return nodeIndex; #ifdef VERBOSE_OUTPUT osg::notify(osg::NOTICE)<<" divide leaf"<_indices; #ifdef VERBOSE_OUTPUT osg::notify(osg::NOTICE)<<" divide leaf->_indices.size()="<_indices.size()< leftLeaf = createKDPrimitiveLeaf(); osg::ref_ptr rightLeaf = createKDPrimitiveLeaf(); for(int i = 0; i0) { leftLeaf->_indices.push_back(i1); leftLeaf->_indices.push_back(i2); leftLeaf->_indices.push_back(i3); } if (numRight>0) { rightLeaf->_indices.push_back(i1); rightLeaf->_indices.push_back(i2); rightLeaf->_indices.push_back(i3); } #if 0 if (numRight>0 && numLeft>0) { std::cout<<"In both"<_indices.empty()) { #ifdef VERBOSE_OUTPUT osg::notify(osg::NOTICE)<<"LeftLeaf empty"<_indices.empty()) { #ifdef VERBOSE_OUTPUT osg::notify(osg::NOTICE)<<"RightLeaf empty"<_vertexIndices.size()="<_vertexIndices.size()<mid) { --right; } if (left