[geos-commits] r2494 - in trunk/source: headers/geos/operation/distance operation/distance

svn_geos at osgeo.org svn_geos at osgeo.org
Fri May 8 06:10:47 EDT 2009


Author: strk
Date: 2009-05-08 06:10:47 -0400 (Fri, 08 May 2009)
New Revision: 2494

Modified:
   trunk/source/headers/geos/operation/distance/DistanceOp.h
   trunk/source/operation/distance/DistanceOp.cpp
Log:
Add isWithinDistance() to DistanceOp, reaching rev 1.17 of JTS


Modified: trunk/source/headers/geos/operation/distance/DistanceOp.h
===================================================================
--- trunk/source/headers/geos/operation/distance/DistanceOp.h	2009-05-08 09:39:39 UTC (rev 2493)
+++ trunk/source/headers/geos/operation/distance/DistanceOp.h	2009-05-08 10:10:47 UTC (rev 2494)
@@ -13,7 +13,7 @@
  *
  **********************************************************************
  *
- * Last port: operation/distance/DistanceOp.java rev 1.16
+ * Last port: operation/distance/DistanceOp.java rev 1.17
  *
  **********************************************************************/
 
@@ -46,32 +46,56 @@
 namespace operation { // geos::operation
 namespace distance { // geos::operation::distance
 
-/** \brief
- * Computes the distance and
- * closest points between two {@link Geometry}s.
+/**
+ * \brief
+ * Find two points on two {@link Geometry}s which lie
+ * within a given distance, or else are the nearest points
+ * on the geometries (in which case this also
+ * provides the distance between the geometries).
  * 
- * The distance computation finds a pair of points in the input geometries
- * which have minimum distance between them.  These points may
- * not be vertices of the geometries, but may lie in the interior of
- * a line segment. In this case the coordinate computed is a close
+ * The distance computation also finds a pair of points in the
+ * input geometries which have the minimum distance between them.
+ * If a point lies in the interior of a line segment,
+ * the coordinate computed is a close
  * approximation to the exact point.
- *
+ * 
  * The algorithms used are straightforward O(n^2)
  * comparisons.  This worst-case performance could be improved on
- * by using Voronoi techniques.
+ * by using Voronoi techniques or spatial indexes.
  *
  */
 class DistanceOp {
 public:
 	/**
+	 * \brief
 	 * Compute the distance between the closest points of two geometries.
+	 *
 	 * @param g0 a {@link Geometry}
 	 * @param g1 another {@link Geometry}
 	 * @return the distance between the geometries
 	 */
-	static double distance(const geom::Geometry *g0, const geom::Geometry *g1);
+	static double distance(const geom::Geometry& g0,
+	                       const geom::Geometry& g1);
 
+	/// @deprecated, use the version taking references
+	static double distance(const geom::Geometry *g0,
+	                        const geom::Geometry *g1);
+
 	/**
+	 * \brief
+	 * Test whether two geometries lie within a given distance of
+	 * each other.
+	 *
+	 * @param g0 a {@link Geometry}
+	 * @param g1 another {@link Geometry}
+	 * @param distance the distance to test
+	 * @return true if g0.distance(g1) <= distance
+	 */
+	static bool isWithinDistance(const geom::Geometry& g0,
+	                             const geom::Geometry& g1,
+	                                            double distance);
+
+	/**
 	 * Compute the the closest points of two geometries.
 	 * The points are presented in the same order as the input Geometries.
 	 *
@@ -81,12 +105,32 @@
 	 */
 	static geom::CoordinateSequence* closestPoints(geom::Geometry *g0, geom::Geometry *g1);
 
+	/// @deprecated use the one taking references
+	DistanceOp(const geom::Geometry *g0, const geom::Geometry *g1);
+
 	/**
-	 * Constructs a DistanceOp that computes the distance and closest points between
-	 * the two specified geometries.
+	 * \brief
+	 * Constructs a DistanceOp that computes the distance and
+	 * closest points between the two specified geometries.
+	 *
+	 * @param g0 a Geometry
+	 * @param g1 a Geometry
 	 */
-	DistanceOp(const geom::Geometry *g0, const geom::Geometry *g1);
+	DistanceOp(const geom::Geometry& g0, const geom::Geometry& g1);
 
+	/**
+	 * \brief
+	 * Constructs a DistanceOp that computes the distance and nearest
+	 * points between the two specified geometries.
+	 *
+	 * @param g0 a Geometry
+	 * @param g1 a Geometry
+	 * @param terminateDistance the distance on which to terminate
+	 *                          the search
+	 */
+	DistanceOp(const geom::Geometry& g0, const geom::Geometry& g1,
+	                                      double terminateDistance);
+
 	~DistanceOp();
 
 	/**
@@ -119,11 +163,19 @@
 
 private:
 
+	// input (TODO: use two references instead..)
+	std::vector<geom::Geometry const*> geom;
+	double terminateDistance; 
+
+	// working (TODO: use two auto_ptr, or to concrete types)
 	algorithm::PointLocator ptLocator;
-	std::vector<geom::Geometry const*> geom;
-	std::vector<geom::Coordinate *> newCoords;
 	std::vector<GeometryLocation*> *minDistanceLocation;
 	double minDistance;
+
+	// memory management
+	std::vector<geom::Coordinate *> newCoords;
+
+
 	void updateMinDistance(double dist);
 	void updateMinDistance(std::vector<GeometryLocation*> *locGeom, bool flip);
 	void computeMinDistance();

Modified: trunk/source/operation/distance/DistanceOp.cpp
===================================================================
--- trunk/source/operation/distance/DistanceOp.cpp	2009-05-08 09:39:39 UTC (rev 2493)
+++ trunk/source/operation/distance/DistanceOp.cpp	2009-05-08 10:10:47 UTC (rev 2494)
@@ -14,7 +14,7 @@
  *
  **********************************************************************
  *
- * Last port: operation/distance/DistanceOp.java rev 1.16
+ * Last port: operation/distance/DistanceOp.java rev 1.17
  *
  **********************************************************************/
 
@@ -50,7 +50,7 @@
 using namespace geom;
 //using namespace geom::util;
 
-/*public static*/
+/*public static (deprecated)*/
 double
 DistanceOp::distance(const Geometry *g0, const Geometry *g1)
 {
@@ -59,6 +59,14 @@
 }
 
 /*public static*/
+double
+DistanceOp::distance(const Geometry& g0, const Geometry& g1)
+{
+	DistanceOp distOp(g0,g1);
+	return distOp.distance();
+}
+
+/*public static*/
 CoordinateSequence*
 DistanceOp::closestPoints(Geometry *g0,Geometry *g1)
 {
@@ -67,14 +75,36 @@
 }
 
 DistanceOp::DistanceOp(const Geometry *g0, const Geometry *g1):
-	geom(2)
+	geom(2),
+	terminateDistance(0.0),
+	minDistanceLocation(0),
+	minDistance(DoubleInfinity)
 {
-	geom[0]=g0;
-	geom[1]=g1;
-	minDistance=DoubleInfinity;
-	minDistanceLocation=NULL;
+	geom[0] = g0;
+	geom[1] = g1;
 }
 
+DistanceOp::DistanceOp(const Geometry& g0, const Geometry& g1):
+	geom(2),
+	terminateDistance(0.0),
+	minDistanceLocation(0),
+	minDistance(DoubleInfinity)
+{
+	geom[0] = &g0;
+	geom[1] = &g1;
+}
+
+DistanceOp::DistanceOp(const Geometry& g0, const Geometry& g1, double tdist)
+	:
+	geom(2),
+	terminateDistance(tdist),
+	minDistanceLocation(0),
+	minDistance(DoubleInfinity)
+{
+	geom[0] = &g0;
+	geom[1] = &g1;
+}
+
 DistanceOp::~DistanceOp()
 {
 	size_t i;
@@ -169,7 +199,7 @@
     if (minDistanceLocation!=NULL) return;
     minDistanceLocation = new vector<GeometryLocation*>(2);
     computeContainmentDistance();
-    if (minDistance<=0.0) return;
+    if (minDistance <= terminateDistance) return;
     computeLineDistance();
 }
 
@@ -190,7 +220,7 @@
 	if (polys1.size()>0) {
 		vector<GeometryLocation*> *insideLocs0 = ConnectedElementLocationFilter::getLocations(geom[0]);
 		computeInside(insideLocs0, polys1, locPtPoly);
-		if (minDistance <= 0.0) {
+		if (minDistance <= terminateDistance) {
 			(*minDistanceLocation)[0] = (*locPtPoly)[0];
 			(*minDistanceLocation)[1] = (*locPtPoly)[1];
 			delete locPtPoly;
@@ -213,7 +243,7 @@
 	if (polys0.size()>0) {
 		vector<GeometryLocation*> *insideLocs1 = ConnectedElementLocationFilter::getLocations(geom[1]);
 		computeInside(insideLocs1, polys0, locPtPoly);
-		if (minDistance <= 0.0) {
+		if (minDistance <= terminateDistance) {
 // flip locations, since we are testing geom 1 VS geom 0
 			(*minDistanceLocation)[0] = (*locPtPoly)[1];
 			(*minDistanceLocation)[1] = (*locPtPoly)[0];
@@ -250,7 +280,7 @@
 		for (size_t j=0, nj=polys.size(); j<nj; ++j)
 		{
 			computeInside(loc, polys[j], locPtPoly);
-			if (minDistance<=0.0) return;
+			if (minDistance<=terminateDistance) return;
 		}
 	}
 }
@@ -299,7 +329,7 @@
 	// bail whenever minDistance goes to zero, since it can't get any less
 	computeMinDistanceLines(lines0, lines1, locGeom);
 	updateMinDistance(&locGeom, false);
-	if (minDistance <= 0.0) {
+	if (minDistance <= terminateDistance) {
 		return;
 	};
 
@@ -307,7 +337,7 @@
 	locGeom[1]=NULL;
 	computeMinDistanceLinesPoints(lines0, pts1, locGeom);
 	updateMinDistance(&locGeom, false);
-	if (minDistance <= 0.0) {
+	if (minDistance <= terminateDistance) {
 		return;
 	};
 
@@ -315,7 +345,7 @@
 	locGeom[1]=NULL;
 	computeMinDistanceLinesPoints(lines1, pts0, locGeom);
 	updateMinDistance(&locGeom, true);
-	if (minDistance <= 0.0){
+	if (minDistance <= terminateDistance){
 		return;
 	};
 
@@ -338,7 +368,7 @@
 		for (size_t j=0, nj=lines1.size(); j<nj; ++j) {
 			const LineString *line1=lines1[j];
 			computeMinDistance(line0, line1, locGeom);
-			if (minDistance<=0.0) return;
+			if (minDistance<=terminateDistance) return;
 		}
 	}
 }
@@ -363,7 +393,7 @@
 				locGeom[0] = new GeometryLocation(pt0, 0, *(pt0->getCoordinate()));
 				locGeom[1] = new GeometryLocation(pt1, 0, *(pt1->getCoordinate()));
 			}
-			if (minDistance<=0.0) return;
+			if (minDistance<=terminateDistance) return;
 			if ( i<points0.size()-1 || j<points1.size()-1)
 			{
 				delete locGeom[0]; locGeom[0]=NULL;
@@ -385,7 +415,7 @@
 		for (size_t j=0;j<points.size();j++) {
 			const Point *pt=points[j];
 			computeMinDistance(line,pt,locGeom);
-			if (minDistance<=0.0) return;
+			if (minDistance<=terminateDistance) return;
 			if ( i<lines.size()-1 || j<points.size()-1)
 			{
 				delete locGeom[0]; locGeom[0]=NULL;
@@ -435,7 +465,7 @@
 				locGeom[0] = new GeometryLocation(line0, i, *c1);
 				locGeom[1] = new GeometryLocation(line1, j, *c2);
 			}
-			if (minDistance<=0.0) return;
+			if (minDistance<=terminateDistance) return;
 			if ( i<npts0-1 || j<npts1-1)
 			{
 				delete locGeom[0]; locGeom[0]=NULL;
@@ -478,10 +508,20 @@
 			delete locGeom[1];
 			locGeom[1] = new GeometryLocation(pt, 0, *coord);
         	}
-		if (minDistance<=0.0) return;
+		if (minDistance<=terminateDistance) return;
 	}
 }
 
+/* public static */
+bool
+DistanceOp::isWithinDistance(const geom::Geometry& g0,
+	                     const geom::Geometry& g1,
+	                     double distance)
+{
+	DistanceOp distOp(g0, g1, distance);
+	return distOp.distance() <= distance;
+}
+
 } // namespace geos.operation.distance
 } // namespace geos.operation
 } // namespace geos



More information about the geos-commits mailing list