[osg-users] KdTree support now checked in

Robert Osfield robert.osfield at gmail.com
Fri Jul 11 12:47:05 PDT 2008

Hi All,

I have now checked in my work on adding KdTree support into the core
OSG.  In SVN trunk you'll find a new include/osg/KdTree header that
contains osg::KdTree and osg::KdTreeBuilder.

The osg::KdTree class is subclassed osg::Shape, and is designed to be
attached to osg::Geometry leaves.  There are two key methods in

        /** Build the kdtree from the specified source geometry object.
          * retun true on success. */
        virtual bool build(BuildOptions& buildOptions, osg::Geometry* geometry);


        /** 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) const;

Note, both these are virtual so if you want to create your own custom
way of building KdTree's or intersecting KdTree's then you go ahead
and implement your own methods.

A companion class KdTreeBuilder is a NodeVisitor that has a prototype
osg::KdTree that it clones each time it encounters an osg::Geometry,
it then calls kdTree->build(..) on this clone, if a valid KdTree is
built then it'll assign to KdTree to the geometry via
geometry->setShape(kdTree).  The use of the prototype allows you to
provide your own subclasses from KdTree so that we KdTree's are built
then automatically use your version.

To enable automatic generation of KdTree's I have extended osgDB so
that the Registry and DatabasePager are both KdTree aware, and if
KdTree build option is enabled it'll use the Registry's new
KdTreeBuilder object to build KdTree's on loaded models.  To enable
this building you simple set the env var OSG_BUILD_KDTREES to on, i.e.


More information about the osg-users mailing list