$include_dir="/home/hyper-archives/boost-commit/include"; include("$include_dir/msg-header.inc") ?>
Subject: [Boost-commit] svn:boost r75683 - trunk/libs/geometry/test/algorithms
From: barend.gehrels_at_[hidden]
Date: 2011-11-27 05:27:49
Author: barendgehrels
Date: 2011-11-27 05:27:47 EST (Sun, 27 Nov 2011)
New Revision: 75683
URL: http://svn.boost.org/trac/boost/changeset/75683
Log:
Linestring/polygon overlay, unit tests for phase 8
Text files modified: 
   trunk/libs/geometry/test/algorithms/difference.cpp      |    52 +++++++++++++++++                       
   trunk/libs/geometry/test/algorithms/test_difference.hpp |   117 ++++++++++++++++++++++++++++----------- 
   2 files changed, 136 insertions(+), 33 deletions(-)
Modified: trunk/libs/geometry/test/algorithms/difference.cpp
==============================================================================
--- trunk/libs/geometry/test/algorithms/difference.cpp	(original)
+++ trunk/libs/geometry/test/algorithms/difference.cpp	2011-11-27 05:27:47 EST (Sun, 27 Nov 2011)
@@ -9,7 +9,12 @@
 
 //#define BOOST_GEOMETRY_CHECK_WITH_POSTGIS
 
+//#define BOOST_GEOMETRY_DEBUG_SEGMENT_IDENTIFIER
+//#define BOOST_GEOMETRY_DEBUG_INTERSECTION
+//#define BOOST_GEOMETRY_DEBUG_TRAVERSE
+//#define BOOST_GEOMETRY_DEBUG_FOLLOW
 //#define BOOST_GEOMETRY_DEBUG_ASSEMBLE
+//#define BOOST_GEOMETRY_DEBUG_IDENTIFIER
 
 
 #include <iostream>
@@ -39,15 +44,62 @@
 #endif
 
 
+
+template <typename Polygon, typename LineString>
+void test_areal_linear()
+{
+    std::string const poly_simplex = "POLYGON((1 1,1 3,3 3,3 1,1 1))";
+    test_one_lp<LineString, LineString, Polygon>("simplex", "LINESTRING(0 2,4 2)", poly_simplex, 2, 4, 2.0);
+    test_one_lp<LineString, LineString, Polygon>("case2",  "LINESTRING(0 1,4 3)", poly_simplex, 2, 4, sqrt(5.0));
+    test_one_lp<LineString, LineString, Polygon>("case3", "LINESTRING(0 1,1 2,3 2,4 3,6 3,7 4)", "POLYGON((2 0,2 5,5 5,5 0,2 0))", 2, 6, 2.0 + 2.0 * sqrt(2.0));
+    test_one_lp<LineString, LineString, Polygon>("case4", "LINESTRING(1 1,3 2,1 3)", "POLYGON((0 0,0 4,2 4,2 0,0 0))", 1, 3, sqrt(5.0));
+
+    test_one_lp<LineString, LineString, Polygon>("case5", "LINESTRING(0 1,3 4)", poly_simplex, 2, 4, 2.0 * sqrt(2.0));
+    test_one_lp<LineString, LineString, Polygon>("case6", "LINESTRING(1 1,10 3)", "POLYGON((2 0,2 4,3 4,3 1,4 1,4 3,5 3,5 1,6 1,6 3,7 3,7 1,8 1,8 3,9 3,9 0,2 0))", 5, 10, 
+            // Pieces are 1 x 2/9:
+            5.0 * sqrt(1.0 + 4.0/81.0));
+
+
+    test_one_lp<LineString, LineString, Polygon>("case7", "LINESTRING(1.5 1.5,2.5 2.5)", poly_simplex, 0, 0, 0.0);
+    test_one_lp<LineString, LineString, Polygon>("case8", "LINESTRING(1 0,2 0)", poly_simplex, 1, 2, 1.0);
+
+    std::string const poly_9 = "POLYGON((1 1,1 4,4 4,4 1,1 1))";
+    test_one_lp<LineString, LineString, Polygon>("case9", "LINESTRING(0 1,1 2,2 2)", poly_9, 1, 2, sqrt(2.0));
+    test_one_lp<LineString, LineString, Polygon>("case10", "LINESTRING(0 1,1 2,0 2)", poly_9, 1, 3, 1.0 + sqrt(2.0));
+    test_one_lp<LineString, LineString, Polygon>("case11", "LINESTRING(2 2,4 2,3 3)", poly_9, 0, 0, 0.0);
+    test_one_lp<LineString, LineString, Polygon>("case12", "LINESTRING(2 3,4 4,5 6)", poly_9, 1, 2, sqrt(5.0));
+
+    test_one_lp<LineString, LineString, Polygon>("case13", "LINESTRING(3 2,4 4,2 3)", poly_9, 0, 0, 0.0);
+    test_one_lp<LineString, LineString, Polygon>("case14", "LINESTRING(5 6,4 4,6 5)", poly_9, 1, 3, 2.0 * sqrt(5.0));
+
+    //return;
+    // The rest compiles but is NOT yet correct
+
+    test_one_lp<LineString, LineString, Polygon>("case15", "LINESTRING(0 2,1 2,1 3,0 3)", poly_9, 2, 4, 2.0);
+    test_one_lp<LineString, LineString, Polygon>("case16", "LINESTRING(2 2,1 2,1 3,2 3)", poly_9, 0, 0, 0.0);
+
+    std::string const angly = "LINESTRING(2 2,2 1,4 1,4 2,5 2,5 3,4 3,4 4,5 4,3 6,3 5,2 5,2 6,0 4)";
+    test_one_lp<LineString, LineString, Polygon>("case17", angly, "POLYGON((1 1,1 5,4 5,4 1,1 1))", 3, 11, 6.0 + 4.0 * sqrt(2.0));
+    test_one_lp<LineString, LineString, Polygon>("case18", angly, "POLYGON((1 1,1 5,5 5,5 1,1 1))", 2, 6, 2.0 + 3.0 * sqrt(2.0));
+    test_one_lp<LineString, LineString, Polygon>("case19", "LINESTRING(1 2,1 3,0 3)", poly_9, 1, 2, 1.0);
+    test_one_lp<LineString, LineString, Polygon>("case20", "LINESTRING(1 2,1 3,2 3)", poly_9, 0, 0, 0.0);
+
+    test_one_lp<LineString, LineString, Polygon>("case21", "LINESTRING(1 2,1 4,4 4,4 1,2 1,2 2)", poly_9, 0, 0, 0.0);
+}
+
 template <typename P>
 void test_all()
 {
     typedef bg::model::box<P> box;
     typedef bg::model::polygon<P> polygon;
+    typedef bg::model::linestring<P> linestring;
     typedef bg::model::ring<P> ring;
 
     typedef typename bg::coordinate_type<P>::type ct;
 
+    test_areal_linear<polygon, linestring>();
+
+
     test_one<polygon, polygon, polygon>("simplex_normal",
         simplex_normal[0], simplex_normal[1],
         3, 3, 2.52636706856656,
Modified: trunk/libs/geometry/test/algorithms/test_difference.hpp
==============================================================================
--- trunk/libs/geometry/test/algorithms/test_difference.hpp	(original)
+++ trunk/libs/geometry/test/algorithms/test_difference.hpp	2011-11-27 05:27:47 EST (Sun, 27 Nov 2011)
@@ -41,9 +41,42 @@
 #endif
 
 
+template <typename Output, typename G1, typename G2>
+void difference_output(std::string const& caseid, G1 const& g1, G2 const& g2, Output const& output)
+{
+#if defined(TEST_WITH_SVG)
+    {
+        typedef typename bg::coordinate_type<G1>::type coordinate_type;
+        typedef typename bg::point_type<G1>::type point_type;
 
+        std::ostringstream filename;
+        filename << "difference_"
+            << caseid << "_"
+            << string_from_type<coordinate_type>::name()
+            << ".svg";
 
-template <typename OutputType, typename CalculationType, typename G1, typename G2>
+        std::ofstream svg(filename.str().c_str());
+
+        bg::svg_mapper<point_type> mapper(svg, 500, 500);
+
+        mapper.add(g1);
+        mapper.add(g2);
+
+        mapper.map(g1, "fill-opacity:0.3;fill:rgb(51,51,153);stroke:rgb(51,51,153);stroke-width:3");
+        mapper.map(g2, "fill-opacity:0.5;fill:rgb(153,204,0);stroke:rgb(153,204,0);stroke-width:3");
+
+
+        for (typename Output::const_iterator it = output.begin(); it != output.end(); ++it)
+        {
+            mapper.map(*it,
+                //sym ? "fill-opacity:0.2;stroke-opacity:0.4;fill:rgb(255,255,0);stroke:rgb(255,0,255);stroke-width:8" :
+                "fill-opacity:0.2;stroke-opacity:0.4;fill:rgb(255,0,0);stroke:rgb(255,0,255);stroke-width:8");
+        }
+    }
+#endif
+}
+
+template <typename OutputType, typename G1, typename G2>
 void test_difference(std::string const& caseid, G1 const& g1, G2 const& g2,
         std::size_t expected_count, std::size_t expected_point_count,
         double expected_area,
@@ -53,7 +86,6 @@
     std::vector<OutputType> clip;
 
     typedef typename bg::coordinate_type<G1>::type coordinate_type;
-    typedef typename bg::point_type<G1>::type point_type;
 
     if (sym)
     {
@@ -78,6 +110,8 @@
         area += bg::area(*it);
     }
 
+    difference_output(caseid, g1, g2, clip);
+
 #ifndef BOOST_GEOMETRY_DEBUG_ASSEMBLE
     {
         // Test inserter functionality
@@ -124,34 +158,6 @@
 #endif
 
 
-#if defined(TEST_WITH_SVG)
-    {
-        std::ostringstream filename;
-        filename << "difference_"
-            << caseid << "_"
-            << string_from_type<coordinate_type>::name()
-            << string_from_type<CalculationType>::name()
-            << ".svg";
-
-        std::ofstream svg(filename.str().c_str());
-
-        bg::svg_mapper<point_type> mapper(svg, 500, 500);
-
-        mapper.add(g1);
-        mapper.add(g2);
-
-        mapper.map(g1, "fill-opacity:0.3;fill:rgb(51,51,153);stroke:rgb(51,51,153);stroke-width:3");
-        mapper.map(g2, "fill-opacity:0.5;fill:rgb(153,204,0);stroke:rgb(153,204,0);stroke-width:3");
-
-        for (typename std::vector<OutputType>::const_iterator it = clip.begin();
-                it != clip.end(); ++it)
-        {
-            mapper.map(*it,
-                //sym ? "fill-opacity:0.2;stroke-opacity:0.4;fill:rgb(255,255,0);stroke:rgb(255,0,255);stroke-width:8" :
-                "fill-opacity:0.2;stroke-opacity:0.4;fill:rgb(255,0,0);stroke:rgb(255,0,255);stroke-width:8");
-        }
-    }
-#endif
 }
 
 
@@ -195,16 +201,16 @@
     bg::correct(g1);
     bg::correct(g2);
 
-    test_difference<OutputType, void>(caseid + "_a", g1, g2,
+    test_difference<OutputType>(caseid + "_a", g1, g2,
         expected_count1, expected_point_count1,
         expected_area1, percentage);
 #ifdef BOOST_GEOMETRY_DEBUG_ASSEMBLE
     return;
 #endif
-    test_difference<OutputType, void>(caseid + "_b", g2, g1,
+    test_difference<OutputType>(caseid + "_b", g2, g1,
         expected_count2, expected_point_count2,
         expected_area2, percentage);
-    test_difference<OutputType, void>(caseid + "_s", g1, g2,
+    test_difference<OutputType>(caseid + "_s", g1, g2,
         expected_count1 + expected_count2,
         expected_point_count1 + expected_point_count2,
         expected_area1 + expected_area2,
@@ -245,6 +251,51 @@
 
 }
 
+template <typename OutputType, typename G1, typename G2>
+void test_one_lp(std::string const& caseid,
+        std::string const& wkt1, std::string const& wkt2,
+        std::size_t expected_count,
+        std::size_t expected_point_count,
+        double expected_length)
+{
+    G1 g1;
+    bg::read_wkt(wkt1, g1);
+
+    G2 g2;
+    bg::read_wkt(wkt2, g2);
+
+    bg::correct(g1);
+
+    std::vector<OutputType> pieces;
+    bg::difference(g1, g2, pieces);
+
+    typename bg::default_length_result<G1>::type length = 0;
+    std::size_t n = 0;
+    for (typename std::vector<OutputType>::iterator it = pieces.begin();
+            it != pieces.end();
+            ++it)
+    {
+        if (expected_point_count > 0)
+        {
+            n += bg::num_points(*it);
+        }
+
+        length += bg::length(*it);
+    }
+
+    BOOST_CHECK_MESSAGE(pieces.size() == expected_count,
+            "difference: " << caseid
+            << " #outputs expected: " << expected_count
+            << " detected: " << pieces.size()
+            );
+
+    BOOST_CHECK_EQUAL(n, expected_point_count);
+
+    BOOST_CHECK_CLOSE(length, expected_length, 0.001);
+
+    difference_output(caseid, g1, g2, pieces);
+}
+
 
 
 #endif