[geos-commits] r3311 - in trunk: include/geos/algorithm
src/algorithm tests/unit tests/unit/algorithm/CGAlgorithms
svn_geos at osgeo.org
svn_geos at osgeo.org
Wed Apr 27 12:42:15 EDT 2011
Author: strk
Date: 2011-04-27 09:42:15 -0700 (Wed, 27 Apr 2011)
New Revision: 3311
Added:
trunk/tests/unit/algorithm/CGAlgorithms/signedAreaTest.cpp
Modified:
trunk/include/geos/algorithm/CGAlgorithms.h
trunk/src/algorithm/CGAlgorithms.cpp
trunk/tests/unit/Makefile.am
Log:
Improve speed of Geometry.getArea, unit-test it.
Modified: trunk/include/geos/algorithm/CGAlgorithms.h
===================================================================
--- trunk/include/geos/algorithm/CGAlgorithms.h 2011-04-27 15:52:49 UTC (rev 3310)
+++ trunk/include/geos/algorithm/CGAlgorithms.h 2011-04-27 16:42:15 UTC (rev 3311)
@@ -1,9 +1,9 @@
/**********************************************************************
- * $Id$
*
* GEOS - Geometry Engine Open Source
* http://geos.refractions.net
*
+ * Copyright (C) 2011 Sandro Santilli <strk at keybit.net>
* Copyright (C) 2005-2006 Refractions Research Inc.
* Copyright (C) 2001-2002 Vivid Solutions Inc.
*
@@ -14,7 +14,7 @@
*
**********************************************************************
*
- * Last port: algorithm/CGAlgorithms.java rev. 1.46 (JTS-1.9)
+ * Last port: algorithm/CGAlgorithms.java r378 (JTS-1.12)
*
**********************************************************************/
@@ -228,14 +228,3 @@
} // namespace geos
#endif // GEOS_ALGORITHM_CGALGORITHM_H
-
-/**********************************************************************
- * $Log$
- * Revision 1.2 2006/05/02 14:51:53 strk
- * Added port info and fixed doxygen comments for CGAlgorithms class
- *
- * Revision 1.1 2006/03/09 16:46:48 strk
- * geos::geom namespace definition, first pass at headers split
- *
- **********************************************************************/
-
Modified: trunk/src/algorithm/CGAlgorithms.cpp
===================================================================
--- trunk/src/algorithm/CGAlgorithms.cpp 2011-04-27 15:52:49 UTC (rev 3310)
+++ trunk/src/algorithm/CGAlgorithms.cpp 2011-04-27 16:42:15 UTC (rev 3311)
@@ -1,11 +1,11 @@
/**********************************************************************
- * $Id$
*
* GEOS - Geometry Engine Open Source
* http://geos.refractions.net
*
- * Copyright (C) 2001-2002 Vivid Solutions Inc.
+ * Copyright (C) 2011 Sandro Santilli <strk at keybit.net>
* Copyright (C) 2005 2006 Refractions Research Inc.
+ * Copyright (C) 2001-2002 Vivid Solutions Inc.
*
* This is free software; you can redistribute and/or modify it under
* the terms of the GNU Lesser General Public Licence as published
@@ -14,7 +14,7 @@
*
**********************************************************************
*
- * Last port: algorithm/CGAlgorithms.java rev. 1.46 (JTS-1.9)
+ * Last port: algorithm/CGAlgorithms.java r378 (JTS-1.12)
*
**********************************************************************/
@@ -308,13 +308,17 @@
if (npts<3) return 0.0;
double sum=0.0;
- for (size_t i=0; i<npts-1; ++i)
+ Coordinate p = ring->getAt(0);
+ double bx = p.x;
+ double by = p.y;
+ for (size_t i=1; i<npts; ++i)
{
- double bx=ring->getAt(i).x;
- double by=ring->getAt(i).y;
- double cx=ring->getAt(i+1).x;
- double cy=ring->getAt(i+1).y;
- sum+=(bx+cx)*(cy-by);
+ ring->getAt(i, p);
+ double cx = p.x;
+ double cy = p.y;
+ sum += (bx+cx) * (cy-by);
+ bx = cx;
+ by = cy;
}
return -sum/2.0;
}
@@ -354,19 +358,3 @@
} // namespace geos.algorithm
} // namespace geos
-
-/**********************************************************************
- * $Log$
- * Revision 1.33 2006/06/12 10:10:39 strk
- * Fixed getGeometryN() to take size_t rather then int, changed unsigned int parameters to size_t.
- *
- * Revision 1.32 2006/05/02 14:51:53 strk
- * Added port info and fixed doxygen comments for CGAlgorithms class
- *
- * Revision 1.31 2006/03/21 11:12:23 strk
- * Cleanups: headers inclusion and Log section
- *
- * Revision 1.30 2006/03/09 16:46:45 strk
- * geos::geom namespace definition, first pass at headers split
- **********************************************************************/
-
Modified: trunk/tests/unit/Makefile.am
===================================================================
--- trunk/tests/unit/Makefile.am 2011-04-27 15:52:49 UTC (rev 3310)
+++ trunk/tests/unit/Makefile.am 2011-04-27 16:42:15 UTC (rev 3311)
@@ -34,6 +34,7 @@
algorithm/CGAlgorithms/isCCWTest.cpp \
algorithm/CGAlgorithms/isPointInRingTest.cpp \
algorithm/CGAlgorithms/computeOrientationTest.cpp \
+ algorithm/CGAlgorithms/signedAreaTest.cpp \
algorithm/ConvexHullTest.cpp \
algorithm/distance/DiscreteHausdorffDistanceTest.cpp \
algorithm/PointLocatorTest.cpp \
Added: trunk/tests/unit/algorithm/CGAlgorithms/signedAreaTest.cpp
===================================================================
--- trunk/tests/unit/algorithm/CGAlgorithms/signedAreaTest.cpp (rev 0)
+++ trunk/tests/unit/algorithm/CGAlgorithms/signedAreaTest.cpp 2011-04-27 16:42:15 UTC (rev 3311)
@@ -0,0 +1,103 @@
+// $Id$
+//
+// Test Suite for CGAlgorithms::signedArea() function
+
+// tut
+#include <tut.hpp>
+// geos
+#include <geos/algorithm/CGAlgorithms.h>
+#include <geos/geom/Polygon.h>
+#include <geos/geom/Geometry.h>
+#include <geos/geom/CoordinateSequence.h>
+#include <geos/geom/Coordinate.h>
+#include <geos/io/WKTReader.h>
+#include <geos/io/WKBReader.h>
+// std
+#include <string>
+#include <memory>
+#include <cassert>
+#include <sstream>
+
+using namespace geos::algorithm;
+
+namespace tut
+{
+ //
+ // Test Group
+ //
+
+ struct test_signedarea_data
+ {
+ typedef std::auto_ptr<geos::geom::Geometry> GeometryPtr;
+
+ geos::geom::CoordinateSequence* cs_;
+ geos::io::WKTReader reader_;
+ geos::io::WKBReader breader_;
+
+ test_signedarea_data()
+ : cs_(0)
+ {
+ assert(0 == cs_);
+ }
+
+ ~test_signedarea_data()
+ {
+ delete cs_;
+ cs_ = 0;
+ }
+ };
+
+ typedef test_group<test_signedarea_data> group;
+ typedef group::object object;
+
+ group test_signedarea_group("geos::algorithm::CGAlgorithms::signedArea");
+
+ //
+ // Test Cases
+ //
+
+ // 1 - clockwise oriented
+ template<>
+ template<>
+ void object::test<1>()
+ {
+ const std::string wkt("POLYGON ((60 180, 140 240, 140 240, 140 240, 200 180, 120 120, 60 180))");
+ GeometryPtr geom(reader_.read(wkt));
+
+ cs_ = geom->getCoordinates();
+ double area = CGAlgorithms::signedArea(cs_);
+
+ ensure_equals( area, 8400 );
+ }
+
+ // 2 - counter-clockwise oriented
+ template<>
+ template<>
+ void object::test<2>()
+ {
+ const std::string wkt("POLYGON ((60 180, 140 120, 100 180, 140 240, 60 180))");
+ GeometryPtr geom(reader_.read(wkt));
+
+ cs_ = geom->getCoordinates();
+ double area = CGAlgorithms::signedArea(cs_);
+
+ ensure_equals( area, -2400 );
+ }
+
+ // 3 - Test the same polygon as in test No 2 but with duplicated top point
+ template<>
+ template<>
+ void object::test<3>()
+ {
+ const std::string wkt("POLYGON ((60 180, 140 120, 100 180, 140 240, 140 240, 60 180))");
+ GeometryPtr geom(reader_.read(wkt));
+
+ cs_ = geom->getCoordinates();
+ double area = CGAlgorithms::signedArea(cs_);
+
+ ensure_equals( area, -2400 );
+ }
+
+
+} // namespace tut
+
More information about the geos-commits
mailing list