Added preliminary support for generating distance, height lists for ElevationSlice.

This commit is contained in:
Robert Osfield
2006-12-04 12:36:13 +00:00
parent fd8d6b7f3b
commit 0f5aeb5fa3
3 changed files with 209 additions and 7 deletions

View File

@@ -141,6 +141,18 @@ int main(int argc, char **argv)
osg::Timer_t endTick = osg::Timer::instance()->tick();
std::cout<<"Completed in "<<osg::Timer::instance()->delta_s(startTick,endTick)<<std::endl;
typedef osgSim::ElevationSlice::DistanceHeightList DistanceHeightList;
const DistanceHeightList& dhl = es.getDistanceHeightIntersections();
std::cout<<"Number of intersections ="<<dhl.size()<<std::endl;
for(DistanceHeightList::const_iterator dhitr = dhl.begin();
dhitr != dhl.end();
++dhitr)
{
std::cout<<" "<<dhitr->first<<" "<<dhitr->second<<std::endl;
}
}
}
else if (useIntersectorGroup)

View File

@@ -12,6 +12,8 @@
*/
#include <osg/CoordinateSystemNode>
#include <osg/io_utils>
#include <osgSim/ElevationSlice>
#include <osg/Notify>
@@ -19,6 +21,47 @@
using namespace osgSim;
namespace ElevationSliceUtils
{
struct DistanceHeightXYZ
{
DistanceHeightXYZ():
distance(0.0),
height(0.0) {}
DistanceHeightXYZ(double d, double h, const osg::Vec3d& pos):
distance(d),
height(h),
position(pos) {}
double distance;
double height;
osg::Vec3d position;
bool operator < ( const DistanceHeightXYZ& rhs) const
{
// small distance values first
if (distance < rhs.distance) return true;
if (distance > rhs.distance) return false;
// greatest heights first
if (height > rhs.height) return true;
if (height < rhs.height) return false;
return false;
}
bool equal_distance(const DistanceHeightXYZ& rhs, double epsilon=1e-6) const
{
return osg::absolute(rhs.distance - distance) <= epsilon;
}
};
}
ElevationSlice::ElevationSlice()
{
setDatabaseCacheReadCallback(new DatabaseCacheReadCallback);
@@ -94,20 +137,117 @@ void ElevationSlice::computeIntersections(osg::Node* scene)
scene->accept(_intersectionVisitor);
osgUtil::PlaneIntersector::Intersections& intersections = intersector->getIntersections();
osg::notify(osg::NOTICE)<<std::endl<<"&intersector->getIntersections()="<<&(intersector->getIntersections())<<std::endl<<std::endl;
typedef osgUtil::PlaneIntersector::Intersection::Polyline Polyline;
if (!intersections.empty())
{
osg::notify(osg::NOTICE)<<"Got intersections."<<std::endl;
// osg::notify(osg::NOTICE)<<"Got intersections."<<std::endl;
for(osgUtil::PlaneIntersector::Intersections::iterator itr = intersections.begin();
itr != intersections.end();
++itr)
{
osgUtil::PlaneIntersector::Intersection& intersection = *itr;
osg::notify(osg::NOTICE)<<" intersection - "<<intersection.polyline.size()<<std::endl;
// osg::notify(osg::NOTICE)<<" intersection - "<<intersection.polyline.size()<<std::endl;
if (intersection.matrix.valid())
{
// osg::notify(osg::NOTICE)<<" transforming "<<std::endl;
// transform points on polyline
for(Polyline::iterator pitr = intersection.polyline.begin();
pitr != intersection.polyline.end();
++pitr)
{
*pitr = (*pitr) * (*intersection.matrix);
}
// matrix no longer needed.
intersection.matrix = 0;
}
#if 0
for(Polyline::iterator pitr = intersection.polyline.begin();
pitr != intersection.polyline.end();
++pitr)
{
osg::notify(osg::NOTICE)<<" v = "<<*pitr<<std::endl;
}
#endif
}
typedef std::set<ElevationSliceUtils::DistanceHeightXYZ> DistanceHeightSet;
DistanceHeightSet distanceHeightSet;
if (em)
{
osg::Vec3d directionVector = _endPoint-_startPoint;
directionVector.normalize();
osg::Vec3d startNormal = _startPoint;
startNormal.normalize();
// convert into distance/height
for(osgUtil::PlaneIntersector::Intersections::iterator itr = intersections.begin();
itr != intersections.end();
++itr)
{
osgUtil::PlaneIntersector::Intersection& intersection = *itr;
for(Polyline::iterator pitr = intersection.polyline.begin();
pitr != intersection.polyline.end();
++pitr)
{
const osg::Vec3d& v = *pitr;
osg::Vec3d vNormal = v;
vNormal.normalize();
double latitude, longitude, height;
em->convertXYZToLatLongHeight(v.x(), v.y(), v.z(),
latitude, longitude, height);
double Rv = v.length() - height;
double alpha = acos( vNormal * startNormal);
double Raverage = Rv;
double distance = alpha * Raverage;
distanceHeightSet.insert(ElevationSliceUtils::DistanceHeightXYZ( distance, height, v));
}
}
}
else
{
// convert into distance/height
for(osgUtil::PlaneIntersector::Intersections::iterator itr = intersections.begin();
itr != intersections.end();
++itr)
{
osgUtil::PlaneIntersector::Intersection& intersection = *itr;
for(Polyline::iterator pitr = intersection.polyline.begin();
pitr != intersection.polyline.end();
++pitr)
{
const osg::Vec3d& v = *pitr;
osg::Vec2d delta_xy( v.x() - _startPoint.x(), v.y() - _startPoint.y());
double distance = delta_xy.length();
distanceHeightSet.insert(ElevationSliceUtils::DistanceHeightXYZ( distance, v.z(), v));
}
}
}
// copy final results
_intersections.clear();
_distanceHeightIntersections.clear();
_intersections.reserve(distanceHeightSet.size());
_distanceHeightIntersections.reserve(distanceHeightSet.size());
for(DistanceHeightSet::iterator dhitr = distanceHeightSet.begin();
dhitr != distanceHeightSet.end();
++dhitr)
{
const ElevationSliceUtils::DistanceHeightXYZ& dh = *dhitr;
_intersections.push_back( dh.position );
_distanceHeightIntersections.push_back( DistanceHeight(dh.distance, dh.height) );
}
}
else
{

View File

@@ -302,7 +302,7 @@ namespace PlaneIntersectorUtils
void report()
{
osg::notify(osg::NOTICE)<<"report()"<<std::endl;
#if 0
osg::notify(osg::NOTICE)<<"start:"<<std::endl;
for(PolylineMap::iterator sitr = _startPolylineMap.begin();
sitr != _startPolylineMap.end();
@@ -312,14 +312,12 @@ namespace PlaneIntersectorUtils
}
osg::notify(osg::NOTICE)<<"ends:"<<std::endl;
for(PolylineMap::iterator eitr = _endPolylineMap.begin();
eitr != _endPolylineMap.end();
++eitr)
{
osg::notify(osg::NOTICE)<<" line - end = "<<eitr->first<<" polyline size = "<<eitr->second->_polyline.size()<<std::endl;
}
#endif
for(PolylineList::iterator pitr = _polylines.begin();
pitr != _polylines.end();
@@ -368,6 +366,57 @@ namespace PlaneIntersectorUtils
_hit = false;
}
inline void add(osg::Vec3d& vs, osg::Vec3d& ve)
{
if (_polytope.getPlaneList().empty())
{
_polylineConnector.add(vs,ve);
}
else
{
for(osg::Polytope::PlaneList::iterator itr = _polytope.getPlaneList().begin();
itr != _polytope.getPlaneList().end();
++itr)
{
osg::Plane& plane = *itr;
double ds = plane.distance(vs);
double de = plane.distance(ve);
if (ds<0.0)
{
if (de<0.0)
{
// osg::notify(osg::NOTICE)<<"Disgard segment "<<std::endl;
return;
}
// osg::notify(osg::NOTICE)<<"Trim start vs="<<vs;
double div = 1.0/(de-ds);
vs = vs*(de*div) - ve*(ds*div);
// osg::notify(osg::NOTICE)<<" after vs="<<vs<<std::endl;
}
else if (de<0.0)
{
// osg::notify(osg::NOTICE)<<"Trim end ve="<<ve;
double div = 1.0/(ds-de);
ve = ve*(ds*div) - vs*(de*div);
// osg::notify(osg::NOTICE)<<" after ve="<<ve<<std::endl;
}
}
// osg::notify(osg::NOTICE)<<"Segment fine"<<std::endl;
_polylineConnector.add(vs,ve);
}
}
inline void operator () (const osg::Vec3& v1,const osg::Vec3& v2,const osg::Vec3& v3, bool)
{
@@ -448,7 +497,7 @@ namespace PlaneIntersectorUtils
}
_polylineConnector.add(v[0],v[1]);
add(v[0],v[1]);
}
@@ -564,6 +613,7 @@ void PlaneIntersector::intersect(osgUtil::IntersectionVisitor& iv, osg::Drawable
intersections.push_back(Intersection());
Intersection& new_intersection = intersections[pos];
new_intersection.matrix = iv.getModelMatrix();
new_intersection.polyline = (*pitr)->_polyline;
new_intersection.nodePath = iv.getNodePath();
new_intersection.drawable = drawable;