Skip to content
Permalink
Browse files

Move distanceSquaredPointToSegment(...) from QgsGeometry to QgsPoint

git-svn-id: http://svn.osgeo.org/qgis/trunk@13004 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent
mhugent committed Mar 5, 2010
1 parent ad7b800 commit 058bb4571044b1b3a483b441b07095ac934ad1d1
Showing with 35 additions and 108 deletions.
  1. +4 −100 src/core/qgsgeometry.cpp
  2. +0 −8 src/core/qgsgeometry.h
  3. +29 −0 src/core/qgspoint.cpp
  4. +2 −0 src/core/qgspoint.h
@@ -2453,7 +2453,7 @@ double QgsGeometry::closestSegmentWithContext(

if ( index > 0 )
{
if (( testdist = distanceSquaredPointToSegment( point, prevx, prevy, thisx, thisy, distPoint ) ) < sqrDist )
if (( testdist = point.sqrDistToSegment( *prevx, *prevy, *thisx, *thisy, distPoint ) ) < sqrDist )
{
closestSegmentIndex = index;
sqrDist = testdist;
@@ -2497,7 +2497,7 @@ double QgsGeometry::closestSegmentWithContext(
}
if ( prevx && prevy )
{
if (( testdist = distanceSquaredPointToSegment( point, prevx, prevy, thisx, thisy, distPoint ) ) < sqrDist )
if (( testdist = point.sqrDistToSegment( *prevx, *prevy, *thisx, *thisy, distPoint ) ) < sqrDist )
{
closestSegmentIndex = pointindex;
sqrDist = testdist;
@@ -2539,7 +2539,7 @@ double QgsGeometry::closestSegmentWithContext(
}
if ( prevx && prevy )
{
if (( testdist = distanceSquaredPointToSegment( point, prevx, prevy, thisx, thisy, distPoint ) ) < sqrDist )
if (( testdist = point.sqrDistToSegment( *prevx, *prevy, *thisx, *thisy, distPoint ) ) < sqrDist )
{
closestSegmentIndex = index;
sqrDist = testdist;
@@ -2587,7 +2587,7 @@ double QgsGeometry::closestSegmentWithContext(
}
if ( prevx && prevy )
{
if (( testdist = distanceSquaredPointToSegment( point, prevx, prevy, thisx, thisy, distPoint ) ) < sqrDist )
if (( testdist = point.sqrDistToSegment( *prevx, *prevy, *thisx, *thisy, distPoint ) ) < sqrDist )
{
closestSegmentIndex = pointindex;
sqrDist = testdist;
@@ -4679,102 +4679,6 @@ bool QgsGeometry::exportGeosToWkb()
return FALSE;
}




double QgsGeometry::distanceSquaredPointToSegment(
const QgsPoint& point,
double *x1, double *y1,
double *x2, double *y2,
QgsPoint& minDistPoint )
{

double nx, ny; //normal vector

nx = *y2 - *y1;
ny = -( *x2 - *x1 );

double t;
t = ( point.x() * ny - point.y() * nx - *x1 * ny + *y1 * nx ) / (( *x2 - *x1 ) * ny - ( *y2 - *y1 ) * nx );

if ( t < 0.0 )
{
minDistPoint.setX( *x1 );
minDistPoint.setY( *y1 );
}
else if ( t > 1.0 )
{
minDistPoint.setX( *x2 );
minDistPoint.setY( *y2 );
}
else
{
minDistPoint.setX( *x1 + t *( *x2 - *x1 ) );
minDistPoint.setY( *y1 + t *( *y2 - *y1 ) );
}

return ( minDistPoint.sqrDist( point ) );
#if 0
double d;

// Proportion along segment (0 to 1) the perpendicular of the point falls upon
double t;


// Projection of point on the segment
double xn;
double yn;

double segmentsqrdist = ( *x2 - *x1 ) * ( *x2 - *x1 ) +
( *y2 - *y1 ) * ( *y2 - *y1 );

// QgsDebugMsg(QString("Entered with (%1, %2) (%3, %4) %5.").arg(*x1).arg(*y1).arg(*x2).arg(*y2).arg(segmentsqrdist));


if ( segmentsqrdist == 0.0 )
{
// segment is a point
xn = *x1;
yn = *y1;
}
else
{

d =
( point.x() - *x1 ) * ( *x2 - *x1 )
+ ( point.y() - *y1 ) * ( *y2 - *y1 );

t = d / segmentsqrdist;

// Do not go beyond end of line
// (otherwise this would be a comparison to an infinite line)
if ( t < 0.0 )
{
xn = *x1;
yn = *y1;
}
else if ( t > 1.0 )
{
xn = *x2;
yn = *y2;
}
else
{
xn = *x1 + t * ( *x2 - *x1 );
yn = *y1 + t * ( *y2 - *y1 );
}

}

minDistPoint.set( xn, yn );

return (
( xn - point.x() ) *( xn - point.x() ) +
( yn - point.y() ) *( yn - point.y() )
);
#endif //0
}

bool QgsGeometry::convertToMultiType()
{
if ( !mGeometry )
@@ -419,14 +419,6 @@ class CORE_EXPORT QgsGeometry

// Private functions

/** Squared distance from point to the given line segment
* TODO: Perhaps move this to QgsPoint
*/
double distanceSquaredPointToSegment( const QgsPoint& point,
double *x1, double *y1,
double *x2, double *y2,
QgsPoint& minDistPoint );

/** Converts from the WKB geometry to the GEOS geometry.
@return true in case of success and false else
*/
@@ -238,3 +238,32 @@ int QgsPoint::onSegment( const QgsPoint& a, const QgsPoint& b ) const

return 2;
}

double QgsPoint::sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPoint& minDistPoint ) const
{
double nx, ny; //normal vector

nx = y2 - y1;
ny = -( x2 - x1 );

double t;
t = ( m_x * ny - m_y * nx - x1 * ny + y1 * nx ) / (( x2 - x1 ) * ny - ( y2 - y1 ) * nx );

if ( t < 0.0 )
{
minDistPoint.setX( x1 );
minDistPoint.setY( y1 );
}
else if ( t > 1.0 )
{
minDistPoint.setX( x2 );
minDistPoint.setY( y2 );
}
else
{
minDistPoint.setX( x1 + t *( x2 - x1 ) );
minDistPoint.setY( y1 + t *( y2 - y1 ) );
}

return ( sqrDist( minDistPoint ) );
}
@@ -144,6 +144,8 @@ class CORE_EXPORT QgsPoint
/**Returns the squared distance between this and other point*/
double sqrDist( const QgsPoint& other ) const;

double sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPoint& minDistPoint ) const;

//! equality operator
bool operator==( const QgsPoint &other );

0 comments on commit 058bb45

Please sign in to comment.
You can’t perform that action at this time.