Skip to content

Commit

Permalink
Move distanceSquaredPointToSegment(...) from QgsGeometry to QgsPoint
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13004 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Mar 5, 2010
1 parent 851fc10 commit 8f3083b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 108 deletions.
104 changes: 4 additions & 100 deletions src/core/qgsgeometry.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 )
Expand Down
8 changes: 0 additions & 8 deletions src/core/qgsgeometry.h
Expand Up @@ -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
*/
Expand Down
29 changes: 29 additions & 0 deletions src/core/qgspoint.cpp
Expand Up @@ -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 ) );
}
2 changes: 2 additions & 0 deletions src/core/qgspoint.h
Expand Up @@ -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 );

Expand Down

0 comments on commit 8f3083b

Please sign in to comment.