[geos-commits] r2357 - in trunk/source: algorithm algorithm/locate headers/geos/algorithm

svn_geos at osgeo.org svn_geos at osgeo.org
Tue Apr 14 08:55:39 EDT 2009


Author: strk
Date: 2009-04-14 08:55:39 -0400 (Tue, 14 Apr 2009)
New Revision: 2357

Modified:
   trunk/source/algorithm/RayCrossingCounter.cpp
   trunk/source/algorithm/locate/IndexedPointInAreaLocator.cpp
   trunk/source/headers/geos/algorithm/RayCrossingCounter.h
Log:
Fix memory leak in RayCrossingCounter; update signatures to avoid pointers when unneeded; add a locatePointInRing taking a vector of coordinate pointers, for use by CGAlgorithms


Modified: trunk/source/algorithm/RayCrossingCounter.cpp
===================================================================
--- trunk/source/algorithm/RayCrossingCounter.cpp	2009-04-14 12:42:42 UTC (rev 2356)
+++ trunk/source/algorithm/RayCrossingCounter.cpp	2009-04-14 12:55:39 UTC (rev 2357)
@@ -39,38 +39,58 @@
 // public:
 //
 /*static*/ int 
-RayCrossingCounter::locatePointInRing( const geom::Coordinate * point, const geom::CoordinateSequence * ring) 
+RayCrossingCounter::locatePointInRing(const geom::Coordinate& point,
+                         const geom::CoordinateSequence& ring) 
 {
-	RayCrossingCounter * rcc = new RayCrossingCounter( point);
+	RayCrossingCounter rcc(point);
 
-	for (int i = 1, ni = ring->size(); i < ni; i++) 
+	for (int i = 1, ni = ring.size(); i < ni; i++) 
 	{
-		const geom::Coordinate & p1 = (*ring)[ i ];
-		const geom::Coordinate & p2 = (*ring)[ i - 1 ];
+		const geom::Coordinate & p1 = ring[ i ];
+		const geom::Coordinate & p2 = ring[ i - 1 ];
 
-		rcc->countSegment( &p1, &p2);
+		rcc.countSegment(p1, p2);
 
-		if ( rcc->isOnSegment() )
-			return rcc->getLocation();
+		if ( rcc.isOnSegment() )
+			return rcc.getLocation();
 	}
-	return rcc->getLocation();
+	return rcc.getLocation();
 }
 
+/*static*/ int 
+RayCrossingCounter::locatePointInRing(const geom::Coordinate& point,
+	         const std::vector<const geom::Coordinate*>& ring)
+{
+	RayCrossingCounter rcc(point);
 
+	for (int i = 1, ni = ring.size(); i < ni; i++) 
+	{
+		const geom::Coordinate & p1 = *ring[ i ];
+		const geom::Coordinate & p2 = *ring[ i - 1 ];
+
+		rcc.countSegment(p1, p2);
+
+		if ( rcc.isOnSegment() )
+			return rcc.getLocation();
+	}
+	return rcc.getLocation();
+}
+
+
 void 
-RayCrossingCounter::countSegment(const geom::Coordinate * p1,
-                                 const geom::Coordinate * p2) 
+RayCrossingCounter::countSegment(const geom::Coordinate& p1,
+                                 const geom::Coordinate& p2) 
 {
 	// For each segment, check if it crosses 
 	// a horizontal ray running from the test point in
 	// the positive x direction.
 	
 	// check if the segment is strictly to the left of the test point
-	if (p1->x < point->x && p2->x < point->x)
+	if (p1.x < point.x && p2.x < point.x)
 		return;
 	
 	// check if the point is equal to the current ring vertex
-	if (point->x == p2->x && point->y == p2->y) 
+	if (point.x == p2.x && point.y == p2.y) 
 	{
 		isPointOnSegment = true;
 		return;
@@ -78,18 +98,18 @@
 
 	// For horizontal segments, check if the point is on the segment.
 	// Otherwise, horizontal segments are not counted.
-	if (p1->y == point->y && p2->y == point->y) 
+	if (p1.y == point.y && p2.y == point.y) 
 	{
-		double minx = p1->x;
-		double maxx = p2->x;
+		double minx = p1.x;
+		double maxx = p2.x;
 
 		if (minx > maxx) 
 		{
-			minx = p2->x;
-			maxx = p1->x;
+			minx = p2.x;
+			maxx = p1.x;
 		}
 		
-		if (point->x >= minx && point->x <= maxx) 
+		if (point.x >= minx && point.x <= maxx) 
 			isPointOnSegment = true;
 
 		return;
@@ -102,15 +122,15 @@
 	//   final endpoint
 	// - a downward edge excludes its starting endpoint, and includes its
 	//   final endpoint
-	if (((p1->y > point->y) && (p2->y <= point->y)) ||
-		((p2->y > point->y) && (p1->y <= point->y)) ) 
+	if (((p1.y > point.y) && (p2.y <= point.y)) ||
+		((p2.y > point.y) && (p1.y <= point.y)) ) 
 	{
 		// translate the segment so that the test point lies
 		// on the origin
-		double x1 = p1->x - point->x;
-		double y1 = p1->y - point->y;
-		double x2 = p2->x - point->x;
-		double y2 = p2->y - point->y;
+		double x1 = p1.x - point.x;
+		double y1 = p1.y - point.y;
+		double x2 = p2.x - point.x;
+		double y2 = p2.y - point.y;
 
 		// The translated segment straddles the x-axis.
 		// Compute the sign of the ordinate of intersection
@@ -140,8 +160,8 @@
 	if (isPointOnSegment)
 		return geom::Location::BOUNDARY;
 
-	// The point is in the interior of the ring if the number of X-crossings is
-	// odd.
+	// The point is in the interior of the ring if the number
+	// of X-crossings is odd.
 	if ((crossingCount % 2) == 1)
 		return geom::Location::INTERIOR;
 	

Modified: trunk/source/algorithm/locate/IndexedPointInAreaLocator.cpp
===================================================================
--- trunk/source/algorithm/locate/IndexedPointInAreaLocator.cpp	2009-04-14 12:42:42 UTC (rev 2356)
+++ trunk/source/algorithm/locate/IndexedPointInAreaLocator.cpp	2009-04-14 12:55:39 UTC (rev 2357)
@@ -113,7 +113,7 @@
 int 
 IndexedPointInAreaLocator::locate( const geom::Coordinate * /*const*/ p)
 {
-	algorithm::RayCrossingCounter rcc( p);
+	algorithm::RayCrossingCounter rcc(*p);
 
 	IndexedPointInAreaLocator::SegmentVisitor visitor( &rcc);
 
@@ -127,7 +127,7 @@
 {
 	geom::LineSegment * seg = (geom::LineSegment *)item;
 
-	counter->countSegment( &(*seg)[ 0 ], &(*seg)[ 1 ]);
+	counter->countSegment( (*seg)[ 0 ], (*seg)[ 1 ]);
 }
 
 void 

Modified: trunk/source/headers/geos/algorithm/RayCrossingCounter.h
===================================================================
--- trunk/source/headers/geos/algorithm/RayCrossingCounter.h	2009-04-14 12:42:42 UTC (rev 2356)
+++ trunk/source/headers/geos/algorithm/RayCrossingCounter.h	2009-04-14 12:55:39 UTC (rev 2357)
@@ -21,6 +21,7 @@
 #ifndef GEOS_ALGORITHM_RAYCROSSINGCOUNTER_H
 #define GEOS_ALGORITHM_RAYCROSSINGCOUNTER_H
 
+#include <vector>
 
 // forward declarations
 namespace geos {
@@ -64,7 +65,7 @@
 class RayCrossingCounter 
 {
 private:
-	const geom::Coordinate * point;
+	const geom::Coordinate& point;
 	
 	int crossingCount;
 	
@@ -81,10 +82,14 @@
 	 * @param ring an array of Coordinates forming a ring 
 	 * @return the location of the point in the ring
 	 */
-	static int locatePointInRing(const geom::Coordinate * p,
-	                             const geom::CoordinateSequence * ring);
+	static int locatePointInRing(const geom::Coordinate& p,
+	                             const geom::CoordinateSequence& ring);
 
-	RayCrossingCounter( const geom::Coordinate * point)
+	/// Semantically equal to the above, just different args encoding
+	static int locatePointInRing(const geom::Coordinate& p,
+	         const std::vector<const geom::Coordinate*>& ring);
+
+	RayCrossingCounter(const geom::Coordinate& point)
 	:	point( point),
 		crossingCount( 0),
 		isPointOnSegment( false)
@@ -96,7 +101,8 @@
 	 * @param p1 an endpoint of the segment
 	 * @param p2 another endpoint of the segment
 	 */
-	void countSegment( const geom::Coordinate * p1, const geom::Coordinate * p2);
+	void countSegment(const geom::Coordinate& p1,
+	                  const geom::Coordinate& p2);
 	
 	/**
 	 * Reports whether the point lies exactly on one of the supplied segments.



More information about the geos-commits mailing list