Added prelimnary KdTree data structure and automatic kdtree build support
into osgDB::Registry/osgTerrain so that newly created subgraphs can have KdTree built on all osg::Geometry automatically on load/creation.
This commit is contained in:
@@ -37,9 +37,12 @@
|
||||
#include <osgSim/HeightAboveTerrain>
|
||||
#include <osgSim/ElevationSlice>
|
||||
|
||||
#include <osgViewer/Viewer>
|
||||
|
||||
#include "fixeddivision.h"
|
||||
#include "variabledivision.h"
|
||||
|
||||
#include <osg/KdTree>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
@@ -55,6 +58,8 @@ int main(int argc, char **argv)
|
||||
while (arguments.read("--points")) processTriangles = false;
|
||||
while (arguments.read("--triangles")) processTriangles = true;
|
||||
|
||||
osgDB::Registry::instance()->setBuildKdTreesHint(osgDB::ReaderWriter::Options::BUILD_KDTREES);
|
||||
|
||||
osg::ref_ptr<osg::Node> scene = osgDB::readNodeFiles(arguments);
|
||||
|
||||
if (!scene)
|
||||
@@ -88,7 +93,7 @@ int main(int argc, char **argv)
|
||||
osg::notify(osg::NOTICE)<<"Time to build "<<time*1000.0<<"ms "<<builder._numVerticesProcessed<<std::endl;
|
||||
osg::notify(osg::NOTICE)<<"build speed "<<(double(builder._numVerticesProcessed)/time)/1000000.0<<"M vertices per second"<<std::endl;
|
||||
}
|
||||
else
|
||||
else if (arguments.read("--vd"))
|
||||
{
|
||||
variabledivision::KDTreeBuilder builder;
|
||||
|
||||
@@ -107,6 +112,12 @@ int main(int argc, char **argv)
|
||||
osg::notify(osg::NOTICE)<<"Time to build "<<time*1000.0<<"ms "<<builder._numVerticesProcessed<<std::endl;
|
||||
osg::notify(osg::NOTICE)<<"build speed "<<(double(builder._numVerticesProcessed)/time)/1000000.0<<"M vertices per second"<<std::endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
osgViewer::Viewer viewer;
|
||||
viewer.setSceneData(scene.get());
|
||||
return viewer.run();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
149
include/osg/KdTree
Normal file
149
include/osg/KdTree
Normal file
@@ -0,0 +1,149 @@
|
||||
/* -*-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>
|
||||
|
||||
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):
|
||||
Shape(rhs,copyop) {}
|
||||
|
||||
META_Shape(osg, KdTree)
|
||||
|
||||
/** Build the kdtree from the specified source geometry object.
|
||||
* retun true on success. */
|
||||
virtual bool build(osg::Geometry* geometry);
|
||||
|
||||
|
||||
struct LineSegmentIntersection
|
||||
{
|
||||
LineSegmentIntersection():
|
||||
ratio(-1.0),
|
||||
primitiveIndex(0) {}
|
||||
|
||||
bool operator < (const LineSegmentIntersection& rhs) const { return ratio < rhs.ratio; }
|
||||
|
||||
typedef std::vector<unsigned int> IndexList;
|
||||
typedef std::vector<double> RatioList;
|
||||
|
||||
double ratio;
|
||||
osg::Vec3d intersectionPoint;
|
||||
osg::Vec3 intersectionNormal;
|
||||
IndexList indexList;
|
||||
RatioList ratioList;
|
||||
unsigned int primitiveIndex;
|
||||
};
|
||||
|
||||
|
||||
typedef std::multiset<LineSegmentIntersection> LineSegmentIntersections;
|
||||
|
||||
/** compute the intersection of a line segment and the kdtree, return true if an intersection has been found.*/
|
||||
virtual bool intersect(const osg::Vec3& start, const osg::Vec3& end, LineSegmentIntersections& intersections);
|
||||
|
||||
|
||||
|
||||
typedef int value_type;
|
||||
typedef std::vector< value_type > Indices;
|
||||
|
||||
struct KDNode
|
||||
{
|
||||
KDNode():
|
||||
first(0),
|
||||
second(0) {}
|
||||
|
||||
KDNode(value_type f, value_type s):
|
||||
first(f),
|
||||
second(s) {}
|
||||
|
||||
value_type first;
|
||||
value_type second;
|
||||
|
||||
osg::BoundingBox bb;
|
||||
};
|
||||
|
||||
|
||||
struct KDLeaf
|
||||
{
|
||||
KDLeaf():
|
||||
first(0),
|
||||
second(0) {}
|
||||
|
||||
KDLeaf(value_type f, value_type s):
|
||||
first(f),
|
||||
second(s) {}
|
||||
|
||||
value_type first;
|
||||
value_type second;
|
||||
|
||||
osg::BoundingBox bb;
|
||||
};
|
||||
|
||||
struct Triangle
|
||||
{
|
||||
Triangle(unsigned int p1, unsigned int p2, unsigned int p3):
|
||||
_p1(p1), _p2(p2), _p3(p3) {}
|
||||
|
||||
bool operator < (const Triangle& rhs) const
|
||||
{
|
||||
if (_p1<rhs._p1) return true;
|
||||
if (_p1>rhs._p1) return false;
|
||||
if (_p2<rhs._p2) return true;
|
||||
if (_p2>rhs._p2) return false;
|
||||
return _p3<rhs._p3;
|
||||
}
|
||||
|
||||
unsigned int _p1;
|
||||
unsigned int _p2;
|
||||
unsigned int _p3;
|
||||
};
|
||||
|
||||
|
||||
};
|
||||
|
||||
class OSG_EXPORT KdTreeBuilder : public osg::NodeVisitor
|
||||
{
|
||||
public:
|
||||
|
||||
KdTreeBuilder();
|
||||
|
||||
void apply(osg::Geode& geode);
|
||||
|
||||
osg::ref_ptr<osg::KdTree> _kdTreePrototype;
|
||||
|
||||
unsigned int _maxNumLevels;
|
||||
unsigned int _targetNumTrianglesPerLeaf;
|
||||
|
||||
unsigned int _numVerticesProcessed;
|
||||
|
||||
protected:
|
||||
|
||||
virtual ~KdTreeBuilder() {}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -86,22 +86,33 @@ class OSGDB_EXPORT ReaderWriter : public osg::Object
|
||||
CACHE_OBJECTS |
|
||||
CACHE_SHADERS
|
||||
};
|
||||
|
||||
/// range of options of whether to build kdtrees automatically on loading
|
||||
enum BuildKdTreesHint
|
||||
{
|
||||
NO_PREFERENCE,
|
||||
DO_NOT_BUILD_KDTREES,
|
||||
BUILD_KDTREES
|
||||
};
|
||||
|
||||
|
||||
Options():
|
||||
osg::Object(true),
|
||||
_objectCacheHint(CACHE_ARCHIVES) {}
|
||||
_objectCacheHint(CACHE_ARCHIVES),
|
||||
_buildKdTreesHint(NO_PREFERENCE) {}
|
||||
|
||||
Options(const std::string& str):
|
||||
osg::Object(true),
|
||||
_str(str),
|
||||
_objectCacheHint(CACHE_ARCHIVES) {}
|
||||
_objectCacheHint(CACHE_ARCHIVES),
|
||||
_buildKdTreesHint(NO_PREFERENCE) {}
|
||||
|
||||
Options(const Options& options,const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
|
||||
osg::Object(options,copyop),
|
||||
_str(options._str),
|
||||
_databasePaths(options._databasePaths),
|
||||
_objectCacheHint(options._objectCacheHint) {}
|
||||
_objectCacheHint(options._objectCacheHint),
|
||||
_buildKdTreesHint(options._buildKdTreesHint) {}
|
||||
|
||||
META_Object(osgDB,Options);
|
||||
|
||||
@@ -128,6 +139,14 @@ class OSGDB_EXPORT ReaderWriter : public osg::Object
|
||||
CacheHintOptions getObjectCacheHint() const { return _objectCacheHint; }
|
||||
|
||||
|
||||
/** Set whether the KdTrees should be built for geometry in the loader model. */
|
||||
void setBuildKdTreesHint(BuildKdTreesHint hint) { _buildKdTreesHint = hint; }
|
||||
|
||||
/** Get whether the KdTrees should be built for geometry in the loader model. */
|
||||
BuildKdTreesHint getBuildKdTreesHint() const { return _buildKdTreesHint; }
|
||||
|
||||
|
||||
|
||||
/** Sets a plugindata value PluginData with a string */
|
||||
void setPluginData(const std::string& s, void* v) const { _pluginData[s] = v; }
|
||||
|
||||
@@ -151,6 +170,7 @@ class OSGDB_EXPORT ReaderWriter : public osg::Object
|
||||
std::string _str;
|
||||
FilePathList _databasePaths;
|
||||
CacheHintOptions _objectCacheHint;
|
||||
BuildKdTreesHint _buildKdTreesHint;
|
||||
|
||||
typedef std::map<std::string,void*> PluginDataMap;
|
||||
mutable PluginDataMap _pluginData;
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
#include <osg/ref_ptr>
|
||||
#include <osg/ArgumentParser>
|
||||
#include <osg/KdTree>
|
||||
|
||||
#include <osgDB/DynamicLibrary>
|
||||
#include <osgDB/ReaderWriter>
|
||||
@@ -130,7 +131,6 @@ class OSGDB_EXPORT Registry : public osg::Referenced
|
||||
|
||||
bool writeObject(const osg::Object& obj,Output& fw);
|
||||
|
||||
|
||||
class ReadFileCallback : public virtual osg::Referenced
|
||||
{
|
||||
public:
|
||||
@@ -188,8 +188,13 @@ class OSGDB_EXPORT Registry : public osg::Referenced
|
||||
|
||||
ReaderWriter::ReadResult readObject(const std::string& fileName,const ReaderWriter::Options* options)
|
||||
{
|
||||
if (_readFileCallback.valid()) return _readFileCallback->readObject(fileName,options);
|
||||
else return readObjectImplementation(fileName,options);
|
||||
ReaderWriter::ReadResult result = _readFileCallback.valid() ?
|
||||
_readFileCallback->readObject(fileName,options) :
|
||||
readObjectImplementation(fileName,options);
|
||||
|
||||
buildKdTreeIfRequired(result, options);
|
||||
|
||||
return result;
|
||||
}
|
||||
ReaderWriter::ReadResult readObjectImplementation(const std::string& fileName,const ReaderWriter::Options* options);
|
||||
|
||||
@@ -209,8 +214,13 @@ class OSGDB_EXPORT Registry : public osg::Referenced
|
||||
|
||||
ReaderWriter::ReadResult readNode(const std::string& fileName,const ReaderWriter::Options* options)
|
||||
{
|
||||
if (_readFileCallback.valid()) return _readFileCallback->readNode(fileName,options);
|
||||
else return readNodeImplementation(fileName,options);
|
||||
ReaderWriter::ReadResult result = _readFileCallback.valid() ?
|
||||
_readFileCallback->readNode(fileName,options) :
|
||||
readNodeImplementation(fileName,options);
|
||||
|
||||
buildKdTreeIfRequired(result, options);
|
||||
|
||||
return result;
|
||||
}
|
||||
ReaderWriter::ReadResult readNodeImplementation(const std::string& fileName,const ReaderWriter::Options* options);
|
||||
|
||||
@@ -302,6 +312,26 @@ class OSGDB_EXPORT Registry : public osg::Referenced
|
||||
ReaderWriter::WriteResult writeShaderImplementation(const osg::Shader& obj, const std::string& fileName,const ReaderWriter::Options* options);
|
||||
|
||||
|
||||
inline void buildKdTreeIfRequired(ReaderWriter::ReadResult& result, const ReaderWriter::Options* options)
|
||||
{
|
||||
bool doKdTreeBuilder = (options && options->getBuildKdTreesHint()!=ReaderWriter::Options::NO_PREFERENCE) ?
|
||||
options->getBuildKdTreesHint() == ReaderWriter::Options::BUILD_KDTREES :
|
||||
_buildKdTreesHint == ReaderWriter::Options::BUILD_KDTREES;
|
||||
|
||||
if (doKdTreeBuilder && _kdTreeBuilder.valid() && result.validNode())
|
||||
{
|
||||
result.getNode()->accept(*_kdTreeBuilder);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void setBuildKdTreesHint(ReaderWriter::Options::BuildKdTreesHint hint) { _buildKdTreesHint = hint; }
|
||||
ReaderWriter::Options::BuildKdTreesHint getBuildKdTreesHint() const { return _buildKdTreesHint; }
|
||||
|
||||
void setKdTreeBuilder(osg::KdTreeBuilder* builder) { _kdTreeBuilder = builder; }
|
||||
osg::KdTreeBuilder* getKdTreeBuilder() { return _kdTreeBuilder.get(); }
|
||||
|
||||
|
||||
void setCreateNodeFromImage(bool flag) { _createNodeFromImage = flag; }
|
||||
bool getCreateNodeFromImage() const { return _createNodeFromImage; }
|
||||
|
||||
@@ -437,7 +467,10 @@ class OSGDB_EXPORT Registry : public osg::Referenced
|
||||
/** get the attached library with specified name.*/
|
||||
DynamicLibraryList::iterator getLibraryItr(const std::string& fileName);
|
||||
|
||||
bool _createNodeFromImage;
|
||||
ReaderWriter::Options::BuildKdTreesHint _buildKdTreesHint;
|
||||
osg::ref_ptr<osg::KdTreeBuilder> _kdTreeBuilder;
|
||||
|
||||
bool _createNodeFromImage;
|
||||
|
||||
osg::Object* readObject(DotOsgWrapperMap& dowMap,Input& fr);
|
||||
|
||||
|
||||
@@ -72,6 +72,7 @@ SET(LIB_PUBLIC_HEADERS
|
||||
${HEADER_PATH}/Image
|
||||
${HEADER_PATH}/ImageStream
|
||||
${HEADER_PATH}/io_utils
|
||||
${HEADER_PATH}/KdTree
|
||||
${HEADER_PATH}/Light
|
||||
${HEADER_PATH}/LightModel
|
||||
${HEADER_PATH}/LightSource
|
||||
@@ -228,6 +229,7 @@ ADD_LIBRARY(${LIB_NAME}
|
||||
Hint.cpp
|
||||
Image.cpp
|
||||
ImageStream.cpp
|
||||
KdTree.cpp
|
||||
LOD.cpp
|
||||
Light.cpp
|
||||
LightModel.cpp
|
||||
|
||||
67
src/osg/KdTree.cpp
Normal file
67
src/osg/KdTree.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
/* -*-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.
|
||||
*/
|
||||
|
||||
#include <osg/KdTree>
|
||||
#include <osg/Geode>
|
||||
|
||||
using namespace osg;
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// KdTree
|
||||
|
||||
bool KdTree::build(osg::Geometry* geometry)
|
||||
{
|
||||
osg::notify(osg::NOTICE)<<"KdTree::build("<<geometry<<")"<<std::endl;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool KdTree::intersect(const osg::Vec3& start, const osg::Vec3& end, LineSegmentIntersections& intersections)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// KdTreeBuilder
|
||||
KdTreeBuilder::KdTreeBuilder():
|
||||
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
|
||||
_maxNumLevels(16),
|
||||
_targetNumTrianglesPerLeaf(4),
|
||||
_numVerticesProcessed(0)
|
||||
{
|
||||
_kdTreePrototype = new osg::KdTree;
|
||||
}
|
||||
|
||||
|
||||
void KdTreeBuilder::apply(osg::Geode& geode)
|
||||
{
|
||||
for(unsigned int i=0; i<geode.getNumDrawables(); ++i)
|
||||
{
|
||||
|
||||
osg::Geometry* geom = geode.getDrawable(i)->asGeometry();
|
||||
if (geom)
|
||||
{
|
||||
osg::KdTree* previous = dynamic_cast<osg::KdTree*>(geom->getShape());
|
||||
if (previous) continue;
|
||||
|
||||
osg::ref_ptr<osg::KdTree> kdTree = dynamic_cast<osg::KdTree*>(_kdTreePrototype->cloneType());
|
||||
|
||||
if (kdTree->build(geom))
|
||||
{
|
||||
geom->setShape(kdTree.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -158,6 +158,17 @@ Registry::Registry()
|
||||
// comment out because it was causing problems under OSX - causing it to crash osgconv when constructing ostream in osg::notify().
|
||||
// notify(INFO) << "Constructing osg::Registry"<<std::endl;
|
||||
|
||||
_buildKdTreesHint = ReaderWriter::Options::NO_PREFERENCE;
|
||||
_kdTreeBuilder = new osg::KdTreeBuilder;
|
||||
|
||||
const char* kdtree_str = getenv("OSG_BUILD_KDTREES");
|
||||
if (kdtree_str)
|
||||
{
|
||||
bool switchOff = (strcmp(kdtree_str, "off")==0 || strcmp(kdtree_str, "OFF")==0 || strcmp(kdtree_str, "Off")==0 );
|
||||
if (switchOff) _buildKdTreesHint = ReaderWriter::Options::DO_NOT_BUILD_KDTREES;
|
||||
else _buildKdTreesHint = ReaderWriter::Options::BUILD_KDTREES;
|
||||
}
|
||||
|
||||
_createNodeFromImage = false;
|
||||
_openingLibrary = false;
|
||||
|
||||
@@ -166,6 +177,8 @@ Registry::Registry()
|
||||
|
||||
initFilePathLists();
|
||||
|
||||
|
||||
|
||||
// register file extension alias.
|
||||
const char* flt_str = getenv("OSG_OPEN_FLIGHT_PLUGIN");
|
||||
if (flt_str)
|
||||
@@ -388,7 +401,7 @@ void Registry::readCommandLine(osg::ArgumentParser& arguments)
|
||||
|
||||
while(arguments.read("-O",value))
|
||||
{
|
||||
setOptions(new osgDB::ReaderWriter::Options(value));
|
||||
setOptions(new ReaderWriter::Options(value));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -642,6 +642,15 @@ void GeometryTechnique::generateGeometry(Locator* masterLocator, const osg::Vec3
|
||||
|
||||
//geometry->setUseDisplayList(false);
|
||||
geometry->setUseVertexBufferObjects(true);
|
||||
|
||||
|
||||
if (osgDB::Registry::instance()->getBuildKdTreesHint()==osgDB::ReaderWriter::Options::BUILD_KDTREES &&
|
||||
osgDB::Registry::instance()->getKdTreeBuilder())
|
||||
{
|
||||
//osg::notify(osg::NOTICE)<<"osgTerrain::GeometryTechnique::build kd tree"<<std::endl;
|
||||
buffer._geode->accept(*(osgDB::Registry::instance()->getKdTreeBuilder()));
|
||||
//osg::notify(osg::NOTICE)<<"after"<<std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
void GeometryTechnique::applyColorLayers()
|
||||
|
||||
Reference in New Issue
Block a user