/* -*-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 #include #include #include #include #include #include #include using namespace osgSim; namespace ElevationSliceUtils { struct DistanceHeightCalculator { DistanceHeightCalculator(osg::EllipsoidModel* em, const osg::Vec3d& startPoint, osg::Vec3d& endPoint): _em(em), _startPoint(startPoint), _startNormal(startPoint), _endPoint(endPoint), _endNormal(endPoint) { double latitude, longitude, height; // set up start point variables _em->convertXYZToLatLongHeight(_startPoint.x(), _startPoint.y(), _startPoint.z(), latitude, longitude, height); _startRadius = _startPoint.length() - height; _startNormal.normalize(); // set up end point variables _em->convertXYZToLatLongHeight(_endPoint.x(), _endPoint.y(), _endPoint.z(), latitude, longitude, height); _endRadius = _endPoint.length() - height; _endNormal.normalize(); osg::Vec3d normal = _startNormal ^ _endNormal; normal.normalize(); _angleIncrement = 0.005; _radiusList.push_back(_startRadius); _distanceList.push_back(0.0); osg::Matrixd rotationMatrix; double angleBetweenStartEnd = acos( _startNormal * _endNormal ); double prevRadius = _startRadius; double distance = 0.0; for(double angle = _angleIncrement; angle < angleBetweenStartEnd; angle += _angleIncrement) { rotationMatrix.makeRotate(angle, normal); osg::Vec3d newVector = osg::Matrixd::transform3x3(_startPoint, rotationMatrix); _em->convertXYZToLatLongHeight(newVector.x(), newVector.y(), newVector.z(), latitude, longitude, height); double newRadius = newVector.length() - height; double distanceIncrement = _angleIncrement * (newRadius + prevRadius) *0.5; distance += distanceIncrement; _radiusList.push_back(newRadius); _distanceList.push_back(distance); // osg::notify(osg::INFO)<<" newVector = "<position; _em->convertXYZToLatLongHeight(p.x(), p.y(), p.z(), latitude, longitude, height); double delta1 = height - s._p1->height; p = s._p1->position; _em->convertXYZToLatLongHeight(p.x(), p.y(), p.z(), latitude, longitude, height); double delta2 = height - s._p2->height; if (delta1>0.0 || delta2>0.0) { osg::notify(osg::INFO)<<" "<<&s<<" computed height delta ="<=Segment::OVERLAPPING) { switch(classification) { case(Segment::OVERLAPPING): { // cases.... // compute new end points for both segments // need to work out which points are overlapping - lhs_p2 && rhs_p1 or lhs_p1 and rhs_p2 // also need to check for cross cases. const Segment& lhs = *itr; const Segment& rhs = *nextItr; bool rhs_p1_inside = (lhs._p1->distance <= rhs._p1->distance) && (rhs._p1->distance <= lhs._p2->distance); bool lhs_p2_inside = (rhs._p1->distance <= lhs._p2->distance) && (lhs._p2->distance <= rhs._p2->distance); if (rhs_p1_inside && lhs_p2_inside) { double distance_between = osg::Vec2d(lhs._p2->distance - rhs._p1->distance, lhs._p2->height - rhs._p1->height).length2(); if (distance_between < epsilon) { // osg::notify(osg::INFO)<<"OVERLAPPING : distance_between acceptable "<distance<<" "<height<distance<<" "<height<distance<<" "<height<distance<<" "<height<distance<<" "<height<distance<<" "<height<distance<<" "<height<distance<<" "<height<= 0.0 && dh2 >= 0.0) { osg::notify(osg::INFO)<<"OVERLAPPING : lhs above rhs "<distance<<" "<height<distance<<" "<height<distance<<" "<height<distance<<" "<height<distance<<" "<height<distance<<" "<height<distance<<" "<height<distance<<" "<height<=0.0 && dh2>=0.0) { osg::notify(osg::INFO)<<"ENCLOSING: ENCLOSING is below enclosed "<distance<<" "<height<distance<<" "<height<distance<<" "<height<distance<<" "<height<distance<<" "<height<distance<<" "<height<distance<<" "<height<distance<<" "<height<distance<<" "<height<distance<<" "<height<distance<<" "<height<distance<<" "<height<=0.0 && dh2>=0.0) { osg::notify(osg::INFO)<<"ENCLOSED: ENCLOSING is below enclosed "<distance<<" "<height<distance<<" "<height<distance<<" "<height<distance<<" "<height<distance<<" "<height<distance<<" "<height<distance<<" "<height<distance<<" "<height<distance<<" "<height<distance<<" "<height<distance<<" "<height<distance<<" "<height<compare(*nextItr) : Segment::UNCLASSIFIED; } } } unsigned int numOverlapping(SegmentSet::iterator startItr) { if (startItr==_segments.end()) return 0; SegmentSet::iterator nextItr = startItr; ++nextItr; unsigned int numOverlapping = 0; while (nextItr!=_segments.end() && startItr->compare(*nextItr)>=Segment::OVERLAPPING) { ++numOverlapping; ++nextItr; } return numOverlapping; } void copyPoints(ElevationSlice::Vec3dList& intersections, ElevationSlice::DistanceHeightList& distanceHeightIntersections) { SegmentSet::iterator prevItr = _segments.begin(); SegmentSet::iterator nextItr = prevItr; ++nextItr; intersections.push_back( prevItr->_p1->position ); distanceHeightIntersections.push_back( ElevationSlice::DistanceHeight(prevItr->_p1->distance, prevItr->_p1->height) ); intersections.push_back( prevItr->_p2->position ); distanceHeightIntersections.push_back( ElevationSlice::DistanceHeight(prevItr->_p2->distance, prevItr->_p2->height) ); for(; nextItr != _segments.end(); ++nextItr,++prevItr) { Segment::Classification classification = prevItr->compare(*nextItr); switch(classification) { case(Segment::SEPERATE): { intersections.push_back( nextItr->_p1->position ); distanceHeightIntersections.push_back( ElevationSlice::DistanceHeight(nextItr->_p1->distance, nextItr->_p1->height) ); intersections.push_back( nextItr->_p2->position ); distanceHeightIntersections.push_back( ElevationSlice::DistanceHeight(nextItr->_p2->distance, nextItr->_p2->height) ); break; } case(Segment::JOINED): { intersections.push_back( nextItr->_p2->position ); distanceHeightIntersections.push_back( ElevationSlice::DistanceHeight(nextItr->_p2->distance, nextItr->_p2->height) ); break; } default : break; } } } SegmentSet _segments; osg::ref_ptr _previousPoint; osg::Plane _plane; osg::ref_ptr _em; }; } ElevationSlice::ElevationSlice() { setDatabaseCacheReadCallback(new DatabaseCacheReadCallback); } void ElevationSlice::computeIntersections(osg::Node* scene) { osg::CoordinateSystemNode* csn = dynamic_cast(scene); osg::EllipsoidModel* em = csn ? csn->getEllipsoidModel() : 0; osg::Plane plane; osg::Polytope boundingPolytope; if (em) { osg::Vec3d start_upVector = em->computeLocalUpVector(_startPoint.x(), _startPoint.y(), _startPoint.z()); osg::Vec3d end_upVector = em->computeLocalUpVector(_endPoint.x(), _endPoint.y(), _endPoint.z()); double start_latitude, start_longitude, start_height; em->convertXYZToLatLongHeight(_startPoint.x(), _startPoint.y(), _startPoint.z(), start_latitude, start_longitude, start_height); osg::notify(osg::INFO)<<"start_lat = "< geode = new osg::Geode; for(itr = intersections.begin(); itr != intersections.end(); ++itr) { osgUtil::PlaneIntersector::Intersection& intersection = *itr; osg::Geometry* geometry = new osg::Geometry; osg::Vec3Array* vertices = new osg::Vec3Array; vertices->reserve(intersection.polyline.size()); for(Polyline::iterator pitr = intersection.polyline.begin(); pitr != intersection.polyline.end(); ++pitr) { vertices->push_back(*pitr); } geometry->setVertexArray( vertices ); geometry->addPrimitiveSet( new osg::DrawArrays(GL_LINE_STRIP, 0, vertices->size()) ); osg::Vec4Array* colours = new osg::Vec4Array; colours->push_back( osg::Vec4(1.0f,1.0f,1.0f,1.0f) ); geometry->setColorArray( colours ); geode->addDrawable( geometry ); geode->getOrCreateStateSet()->setMode( GL_LIGHTING, osg::StateAttribute::OFF ); } osgDB::writeNodeFile(*geode, "raw.osg"); #endif ElevationSliceUtils::LineConstructor constructor; constructor._plane = plane; constructor._em = em; if (em) { ElevationSliceUtils::DistanceHeightCalculator dhc(em, _startPoint, _endPoint); // convert into distance/height for(itr = intersections.begin(); itr != intersections.end(); ++itr) { osgUtil::PlaneIntersector::Intersection& intersection = *itr; if (intersection.attributes.size()!=intersection.polyline.size()) continue; Attributes::iterator aitr = intersection.attributes.begin(); for(Polyline::iterator pitr = intersection.polyline.begin(); pitr != intersection.polyline.end(); ++pitr, ++aitr) { const osg::Vec3d& v = *pitr; double distance, height; dhc.computeDistanceHeight(v, distance, height); double pi_height = *aitr; // osg::notify(osg::INFO)<<"computed height = "<