[QGIS Commit] r13004 - trunk/qgis/src/core

svn_qgis at osgeo.org svn_qgis at osgeo.org
Fri Mar 5 07:14:43 EST 2010


Author: mhugent
Date: 2010-03-05 07:14:41 -0500 (Fri, 05 Mar 2010)
New Revision: 13004

Modified:
   trunk/qgis/src/core/qgsgeometry.cpp
   trunk/qgis/src/core/qgsgeometry.h
   trunk/qgis/src/core/qgspoint.cpp
   trunk/qgis/src/core/qgspoint.h
Log:
Move distanceSquaredPointToSegment(...) from QgsGeometry to QgsPoint

Modified: trunk/qgis/src/core/qgsgeometry.cpp
===================================================================
--- trunk/qgis/src/core/qgsgeometry.cpp	2010-03-05 12:08:32 UTC (rev 13003)
+++ trunk/qgis/src/core/qgsgeometry.cpp	2010-03-05 12:14:41 UTC (rev 13004)
@@ -2453,7 +2453,7 @@
 
         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 @@
           }
           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 @@
           }
           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 @@
             }
             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 @@
   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 )

Modified: trunk/qgis/src/core/qgsgeometry.h
===================================================================
--- trunk/qgis/src/core/qgsgeometry.h	2010-03-05 12:08:32 UTC (rev 13003)
+++ trunk/qgis/src/core/qgsgeometry.h	2010-03-05 12:14:41 UTC (rev 13004)
@@ -419,14 +419,6 @@
 
     // 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
      */

Modified: trunk/qgis/src/core/qgspoint.cpp
===================================================================
--- trunk/qgis/src/core/qgspoint.cpp	2010-03-05 12:08:32 UTC (rev 13003)
+++ trunk/qgis/src/core/qgspoint.cpp	2010-03-05 12:14:41 UTC (rev 13004)
@@ -238,3 +238,32 @@
 
   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 ) );
+}

Modified: trunk/qgis/src/core/qgspoint.h
===================================================================
--- trunk/qgis/src/core/qgspoint.h	2010-03-05 12:08:32 UTC (rev 13003)
+++ trunk/qgis/src/core/qgspoint.h	2010-03-05 12:14:41 UTC (rev 13004)
@@ -144,6 +144,8 @@
     /**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 );
 



More information about the QGIS-commit mailing list