Added beginnings of osgUtil::PlaneIntersector and osgSim::ElevationSlice

This commit is contained in:
Robert Osfield
2006-11-27 20:25:36 +00:00
parent ae79bebd82
commit 05bffbe9c4
6 changed files with 352 additions and 1 deletions

View File

@@ -0,0 +1,84 @@
/* -*-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/CoordinateSystemNode>
#include <osgSim/ElevationSlice>
#include <osg/Notify>
using namespace osgSim;
ElevationSlice::ElevationSlice()
{
setDatabaseCacheReadCallback(new DatabaseCacheReadCallback);
}
void ElevationSlice::computeIntersections(osg::Node* scene)
{
osg::CoordinateSystemNode* csn = dynamic_cast<osg::CoordinateSystemNode*>(scene);
osg::EllipsoidModel* em = csn ? csn->getEllipsoidModel() : 0;
osg::Plane plane;
osg::Polytope boundingPolytope;
if (em)
{
osg::Vec3d upVector = em->computeLocalUpVector(_startPoint.x(), _startPoint.y(), _startPoint.z());
double latitude, longitude, height;
em->convertXYZToLatLongHeight(_startPoint.x(), _startPoint.y(), _startPoint.z(), latitude, longitude, height);
osg::notify(osg::NOTICE)<<"lat = "<<latitude<<" longitude = "<<longitude<<" height = "<<height<<std::endl;
}
else
{
osg::Vec3d upVector (0.0, 0.0, 1.0);
}
osg::ref_ptr<osgUtil::PlaneIntersector> intersector = new osgUtil::PlaneIntersector(plane, boundingPolytope);
_intersectionVisitor.reset();
_intersectionVisitor.setIntersector( intersector.get() );
scene->accept(_intersectionVisitor);
osgUtil::PlaneIntersector::Intersections& intersections = intersector->getIntersections();
if (!intersections.empty())
{
osg::notify(osg::NOTICE)<<"Got intersections."<<std::endl;
}
else
{
osg::notify(osg::NOTICE)<<"No intersections found."<<std::endl;
}
}
ElevationSlice::Vec3dList ElevationSlice::computeElevationSlice(osg::Node* scene, const osg::Vec3d& startPoint, const osg::Vec3d& endPoint)
{
ElevationSlice es;
es.setStartPoint(startPoint);
es.setEndPoint(endPoint);
es.computeIntersections(scene);
return es.getIntersections();
}
void ElevationSlice::setDatabaseCacheReadCallback(DatabaseCacheReadCallback* dcrc)
{
_dcrc = dcrc;
_intersectionVisitor.setReadCallback(dcrc);
}

View File

@@ -15,6 +15,7 @@ CXXFILES = \
LightPointNode.cpp\
LineOfSight.cpp\
HeightAboveTerrain.cpp\
ElevationSlice.cpp\
MultiSwitch.cpp\
OverlayNode.cpp\
OpenFlightOptimizer.cpp\

View File

@@ -512,7 +512,7 @@ bool LineSegmentIntersector::intersectAndClip(osg::Vec3d& s, osg::Vec3d& e,const
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// IntersectorGroup
// PolytopeIntersector
//
PolytopeIntersector::PolytopeIntersector(const osg::Polytope& polytope):
_parent(0),
@@ -629,6 +629,105 @@ void PolytopeIntersector::reset()
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// PlaneIntersector
//
PlaneIntersector::PlaneIntersector(const osg::Plane& plane, const osg::Polytope& boundingPolytope):
_parent(0),
_plane(plane),
_polytope(boundingPolytope)
{
}
PlaneIntersector::PlaneIntersector(CoordinateFrame cf, const osg::Plane& plane, const osg::Polytope& boundingPolytope):
Intersector(cf),
_parent(0),
_plane(plane),
_polytope(boundingPolytope)
{
}
Intersector* PlaneIntersector::clone(osgUtil::IntersectionVisitor& iv)
{
if (_coordinateFrame==MODEL && iv.getModelMatrix()==0)
{
osg::ref_ptr<PlaneIntersector> pi = new PlaneIntersector(_plane, _polytope);
pi->_parent = this;
return pi.release();
}
// compute the matrix that takes this Intersector from its CoordinateFrame into the local MODEL coordinate frame
// that geometry in the scene graph will always be in.
osg::Matrix matrix;
switch (_coordinateFrame)
{
case(WINDOW):
if (iv.getWindowMatrix()) matrix.preMult( *iv.getWindowMatrix() );
if (iv.getProjectionMatrix()) matrix.preMult( *iv.getProjectionMatrix() );
if (iv.getViewMatrix()) matrix.preMult( *iv.getViewMatrix() );
if (iv.getModelMatrix()) matrix.preMult( *iv.getModelMatrix() );
break;
case(PROJECTION):
if (iv.getProjectionMatrix()) matrix.preMult( *iv.getProjectionMatrix() );
if (iv.getViewMatrix()) matrix.preMult( *iv.getViewMatrix() );
if (iv.getModelMatrix()) matrix.preMult( *iv.getModelMatrix() );
break;
case(VIEW):
if (iv.getViewMatrix()) matrix.preMult( *iv.getViewMatrix() );
if (iv.getModelMatrix()) matrix.preMult( *iv.getModelMatrix() );
break;
case(MODEL):
if (iv.getModelMatrix()) matrix = *iv.getModelMatrix();
break;
}
osg::Plane plane = _plane;
plane.transformProvidingInverse(matrix);
osg::Polytope transformedPolytope;
transformedPolytope.setAndTransformProvidingInverse(_polytope, matrix);
osg::ref_ptr<PlaneIntersector> pi = new PlaneIntersector(plane, transformedPolytope);
pi->_parent = this;
return pi.release();
}
bool PlaneIntersector::enter(const osg::Node& node)
{
return !node.isCullingActive() ||
( _plane.intersect(node.getBound()) && _polytope.contains(node.getBound()) );
}
void PlaneIntersector::leave()
{
// do nothing.
}
void PlaneIntersector::intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable* drawable)
{
if ( !_plane.intersect( drawable->getBound() ) ) return;
if ( !_polytope.contains( drawable->getBound() ) ) return;
Intersection hit;
hit.nodePath = iv.getNodePath();
hit.drawable = drawable;
insertIntersection(hit);
}
void PlaneIntersector::reset()
{
Intersector::reset();
_intersections.clear();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// IntersectorGroup