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:
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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user