bool getIsectPoint( const GeoPoint& p1, const GeoPoint& p2, const GeoPoint& p3, const GeoPoint& p4, GeoPoint& out ) { double denom = (p4.y()-p3.y())*(p2.x()-p1.x()) - (p4.x()-p3.x())*(p2.y()-p1.y()); if ( denom == 0.0 ) { out = GeoPoint::invalid(); // parallel lines return false; } double ua_num = (p4.x()-p3.x())*(p1.y()-p3.y()) - (p4.y()-p3.y())*(p1.x()-p3.x()); double ub_num = (p2.x()-p1.x())*(p1.y()-p3.y()) - (p2.y()-p1.y())*(p1.x()-p3.x()); double ua = ua_num/denom; double ub = ub_num/denom; if ( ua < 0.0 || ua > 1.0 ) // || ub < 0.0 || ub > 1.0 ) { out = GeoPoint::invalid(); // isect point is on line, but not on line segment return false; } double x = p1.x() + ua*(p2.x()-p1.x()); double y = p1.y() + ua*(p2.y()-p1.y()); double z = p1.getDim() > 2? p1.z() + ua*(p2.z()-p1.z()) : 0.0; //right? out = GeoPoint( x, y, z, p1.getSRS() ); return true; }
bool GeoPoint::operator == ( const GeoPoint& rhs ) const { return isValid() && rhs.isValid() && SpatialReference::equivalent( getSRS(), rhs.getSRS() ) && getDim() == rhs.getDim() && x() == rhs.x() && (getDim() < 2 || y() == rhs.y()) && (getDim() < 3 || z() == rhs.z()); }
bool visitPoint( GeoPoint& p ) { bool ok = true; // pull it out of the source reference frame: p.set( p * src_rf ); // reproject it: if ( handle ) ok = OCTTransform( handle, 1, &p.x(), &p.y(), &p.z() ) != 0; // push it into the new reference frame: p.set( p * to_srs->getReferenceFrame() ); p = p.getDim() == 2? GeoPoint( p.x(), p.y(), to_srs ) : GeoPoint( p.x(), p.y(), p.z(), to_srs ); return ok; }
bool findIsectSegmentAndPoint(const GeoPoint& p1, const GeoPoint& p2, const GeoPointList& input, const UINT start_i, int& out_seg_i, GeoPoint& out_isect_p ) { for( UINT i=0; i<input.size()-1; i++ ) { // ignore the segments preceding and following the start index if ( (input.size()+start_i-1)%input.size() <= i && i <= (input.size()+start_i+1)%input.size() ) continue; const GeoPoint& p3 = input[i]; const GeoPoint& p4 = input[i+1]; double denom = (p4.y()-p3.y())*(p2.x()-p1.x()) - (p4.x()-p3.x())*(p2.y()-p1.y()); if ( denom != 0.0 ) { double ua_num = (p4.x()-p3.x())*(p1.y()-p3.y()) - (p4.y()-p3.y())*(p1.x()-p3.x()); double ub_num = (p2.x()-p1.x())*(p1.y()-p3.y()) - (p2.y()-p1.y())*(p1.x()-p3.x()); double ua = ua_num/denom; double ub = ub_num/denom; if ( ua <= 1.0 ) { //osgGIS::notify( osg::WARN ) // << " [" // << i << "] " // << "isect point was found at on source segment (ua=" << ua << ")" // << std::endl // << " source segment = (" << p1.toString() << " => " << p2.toString() << "), " // << " target segment = (" << p3.toString() << " => " << p4.toString() << ")" // << std::endl; } else if ( ub < 0.0 || ub > 1.0 ) { //osgGIS::notify( osg::WARN ) // << " [" // << i << "] " // << "isect point was not found on target segment (ub=" << ub << ")" // << std::endl // << " source segment = (" << p1.toString() << " => " << p2.toString() << "), " // << " target segment = (" << p3.toString() << " => " << p4.toString() << ")" // << std::endl; } else { double isect_x = p1.x() + ua*(p2.x()-p1.x()); double isect_y = p1.y() + ua*(p2.y()-p1.y()); out_seg_i = i; out_isect_p = p1.getDim() == 2? GeoPoint( isect_x, isect_y, p4.getSRS() ) : GeoPoint( isect_x, isect_y, p4.z(), p4.getSRS() ); return true; } } } return false; }