Skip to content

Commit 058bb45

Browse files
author
mhugent
committed
Move distanceSquaredPointToSegment(...) from QgsGeometry to QgsPoint
git-svn-id: http://svn.osgeo.org/qgis/trunk@13004 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent ad7b800 commit 058bb45

File tree

4 files changed

+35
-108
lines changed

4 files changed

+35
-108
lines changed

src/core/qgsgeometry.cpp

+4-100
Original file line numberDiff line numberDiff line change
@@ -2453,7 +2453,7 @@ double QgsGeometry::closestSegmentWithContext(
24532453

24542454
if ( index > 0 )
24552455
{
2456-
if (( testdist = distanceSquaredPointToSegment( point, prevx, prevy, thisx, thisy, distPoint ) ) < sqrDist )
2456+
if (( testdist = point.sqrDistToSegment( *prevx, *prevy, *thisx, *thisy, distPoint ) ) < sqrDist )
24572457
{
24582458
closestSegmentIndex = index;
24592459
sqrDist = testdist;
@@ -2497,7 +2497,7 @@ double QgsGeometry::closestSegmentWithContext(
24972497
}
24982498
if ( prevx && prevy )
24992499
{
2500-
if (( testdist = distanceSquaredPointToSegment( point, prevx, prevy, thisx, thisy, distPoint ) ) < sqrDist )
2500+
if (( testdist = point.sqrDistToSegment( *prevx, *prevy, *thisx, *thisy, distPoint ) ) < sqrDist )
25012501
{
25022502
closestSegmentIndex = pointindex;
25032503
sqrDist = testdist;
@@ -2539,7 +2539,7 @@ double QgsGeometry::closestSegmentWithContext(
25392539
}
25402540
if ( prevx && prevy )
25412541
{
2542-
if (( testdist = distanceSquaredPointToSegment( point, prevx, prevy, thisx, thisy, distPoint ) ) < sqrDist )
2542+
if (( testdist = point.sqrDistToSegment( *prevx, *prevy, *thisx, *thisy, distPoint ) ) < sqrDist )
25432543
{
25442544
closestSegmentIndex = index;
25452545
sqrDist = testdist;
@@ -2587,7 +2587,7 @@ double QgsGeometry::closestSegmentWithContext(
25872587
}
25882588
if ( prevx && prevy )
25892589
{
2590-
if (( testdist = distanceSquaredPointToSegment( point, prevx, prevy, thisx, thisy, distPoint ) ) < sqrDist )
2590+
if (( testdist = point.sqrDistToSegment( *prevx, *prevy, *thisx, *thisy, distPoint ) ) < sqrDist )
25912591
{
25922592
closestSegmentIndex = pointindex;
25932593
sqrDist = testdist;
@@ -4679,102 +4679,6 @@ bool QgsGeometry::exportGeosToWkb()
46794679
return FALSE;
46804680
}
46814681

4682-
4683-
4684-
4685-
double QgsGeometry::distanceSquaredPointToSegment(
4686-
const QgsPoint& point,
4687-
double *x1, double *y1,
4688-
double *x2, double *y2,
4689-
QgsPoint& minDistPoint )
4690-
{
4691-
4692-
double nx, ny; //normal vector
4693-
4694-
nx = *y2 - *y1;
4695-
ny = -( *x2 - *x1 );
4696-
4697-
double t;
4698-
t = ( point.x() * ny - point.y() * nx - *x1 * ny + *y1 * nx ) / (( *x2 - *x1 ) * ny - ( *y2 - *y1 ) * nx );
4699-
4700-
if ( t < 0.0 )
4701-
{
4702-
minDistPoint.setX( *x1 );
4703-
minDistPoint.setY( *y1 );
4704-
}
4705-
else if ( t > 1.0 )
4706-
{
4707-
minDistPoint.setX( *x2 );
4708-
minDistPoint.setY( *y2 );
4709-
}
4710-
else
4711-
{
4712-
minDistPoint.setX( *x1 + t *( *x2 - *x1 ) );
4713-
minDistPoint.setY( *y1 + t *( *y2 - *y1 ) );
4714-
}
4715-
4716-
return ( minDistPoint.sqrDist( point ) );
4717-
#if 0
4718-
double d;
4719-
4720-
// Proportion along segment (0 to 1) the perpendicular of the point falls upon
4721-
double t;
4722-
4723-
4724-
// Projection of point on the segment
4725-
double xn;
4726-
double yn;
4727-
4728-
double segmentsqrdist = ( *x2 - *x1 ) * ( *x2 - *x1 ) +
4729-
( *y2 - *y1 ) * ( *y2 - *y1 );
4730-
4731-
// QgsDebugMsg(QString("Entered with (%1, %2) (%3, %4) %5.").arg(*x1).arg(*y1).arg(*x2).arg(*y2).arg(segmentsqrdist));
4732-
4733-
4734-
if ( segmentsqrdist == 0.0 )
4735-
{
4736-
// segment is a point
4737-
xn = *x1;
4738-
yn = *y1;
4739-
}
4740-
else
4741-
{
4742-
4743-
d =
4744-
( point.x() - *x1 ) * ( *x2 - *x1 )
4745-
+ ( point.y() - *y1 ) * ( *y2 - *y1 );
4746-
4747-
t = d / segmentsqrdist;
4748-
4749-
// Do not go beyond end of line
4750-
// (otherwise this would be a comparison to an infinite line)
4751-
if ( t < 0.0 )
4752-
{
4753-
xn = *x1;
4754-
yn = *y1;
4755-
}
4756-
else if ( t > 1.0 )
4757-
{
4758-
xn = *x2;
4759-
yn = *y2;
4760-
}
4761-
else
4762-
{
4763-
xn = *x1 + t * ( *x2 - *x1 );
4764-
yn = *y1 + t * ( *y2 - *y1 );
4765-
}
4766-
4767-
}
4768-
4769-
minDistPoint.set( xn, yn );
4770-
4771-
return (
4772-
( xn - point.x() ) *( xn - point.x() ) +
4773-
( yn - point.y() ) *( yn - point.y() )
4774-
);
4775-
#endif //0
4776-
}
4777-
47784682
bool QgsGeometry::convertToMultiType()
47794683
{
47804684
if ( !mGeometry )

src/core/qgsgeometry.h

-8
Original file line numberDiff line numberDiff line change
@@ -419,14 +419,6 @@ class CORE_EXPORT QgsGeometry
419419

420420
// Private functions
421421

422-
/** Squared distance from point to the given line segment
423-
* TODO: Perhaps move this to QgsPoint
424-
*/
425-
double distanceSquaredPointToSegment( const QgsPoint& point,
426-
double *x1, double *y1,
427-
double *x2, double *y2,
428-
QgsPoint& minDistPoint );
429-
430422
/** Converts from the WKB geometry to the GEOS geometry.
431423
@return true in case of success and false else
432424
*/

src/core/qgspoint.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,32 @@ int QgsPoint::onSegment( const QgsPoint& a, const QgsPoint& b ) const
238238

239239
return 2;
240240
}
241+
242+
double QgsPoint::sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPoint& minDistPoint ) const
243+
{
244+
double nx, ny; //normal vector
245+
246+
nx = y2 - y1;
247+
ny = -( x2 - x1 );
248+
249+
double t;
250+
t = ( m_x * ny - m_y * nx - x1 * ny + y1 * nx ) / (( x2 - x1 ) * ny - ( y2 - y1 ) * nx );
251+
252+
if ( t < 0.0 )
253+
{
254+
minDistPoint.setX( x1 );
255+
minDistPoint.setY( y1 );
256+
}
257+
else if ( t > 1.0 )
258+
{
259+
minDistPoint.setX( x2 );
260+
minDistPoint.setY( y2 );
261+
}
262+
else
263+
{
264+
minDistPoint.setX( x1 + t *( x2 - x1 ) );
265+
minDistPoint.setY( y1 + t *( y2 - y1 ) );
266+
}
267+
268+
return ( sqrDist( minDistPoint ) );
269+
}

src/core/qgspoint.h

+2
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ class CORE_EXPORT QgsPoint
144144
/**Returns the squared distance between this and other point*/
145145
double sqrDist( const QgsPoint& other ) const;
146146

147+
double sqrDistToSegment( double x1, double y1, double x2, double y2, QgsPoint& minDistPoint ) const;
148+
147149
//! equality operator
148150
bool operator==( const QgsPoint &other );
149151

0 commit comments

Comments
 (0)