PageRenderTime 43ms CodeModel.GetById 8ms app.highlight 29ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/libs/geometry/test/algorithms/correct.cpp

http://hadesmem.googlecode.com/
C++ | 182 lines | 94 code | 50 blank | 38 comment | 0 complexity | 4ef8342bffed2451d6952c4c7dcc81f0 MD5 | raw file
  1// Boost.Geometry (aka GGL, Generic Geometry Library)
  2// Unit Test
  3
  4// Copyright (c) 2007-2011 Barend Gehrels, Amsterdam, the Netherlands.
  5// Copyright (c) 2008-2011 Bruno Lalande, Paris, France.
  6// Copyright (c) 2009-2011 Mateusz Loskot, London, UK.
  7
  8// Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  9// (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
 10
 11// Use, modification and distribution is subject to the Boost Software License,
 12// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
 13// http://www.boost.org/LICENSE_1_0.txt)
 14
 15#include <sstream>
 16
 17#include <geometry_test_common.hpp>
 18
 19#include <boost/geometry/algorithms/correct.hpp>
 20#include <boost/geometry/strategies/strategies.hpp>
 21
 22#include <boost/geometry/util/write_dsv.hpp>
 23
 24#include <boost/geometry/domains/gis/io/wkt/read_wkt.hpp>
 25#include <boost/geometry/domains/gis/io/wkt/write_wkt.hpp>
 26
 27#include <boost/geometry/geometries/point_xy.hpp>
 28#include <boost/geometry/geometries/box.hpp>
 29#include <boost/geometry/geometries/ring.hpp>
 30#include <boost/geometry/geometries/polygon.hpp>
 31
 32
 33template <typename Geometry>
 34void test_geometry(std::string const& wkt, std::string const& expected)
 35{
 36    Geometry geometry;
 37
 38    bg::read_wkt(wkt, geometry);
 39    bg::correct(geometry);
 40
 41    std::ostringstream out;
 42    out << bg::wkt(geometry);
 43
 44    BOOST_CHECK_EQUAL(out.str(), expected);
 45}
 46
 47
 48// Note: 3D/box test cannot be done using WKT because:
 49// -> wkt-box does not exist
 50// -> so it is converted to a ring
 51// -> ring representation of 3d-box is not supported, nor feasible
 52// -> so it uses DSV which can represent a box
 53template <typename Geometry>
 54void test_geometry_dsv(std::string const& wkt, std::string const& expected)
 55{
 56    Geometry geometry;
 57
 58    bg::read_wkt(wkt, geometry);
 59    bg::correct(geometry);
 60
 61    std::ostringstream out;
 62    out << bg::dsv(geometry);
 63
 64    BOOST_CHECK_EQUAL(out.str(), expected);
 65}
 66
 67
 68
 69
 70
 71
 72template <typename P>
 73void test_all()
 74{
 75    // Define clockwise and counter clockwise polygon
 76    std::string cw_ring       = "POLYGON((0 0,0 1,1 1,1 0,0 0))";
 77    std::string ccw_ring      = "POLYGON((0 0,1 0,1 1,0 1,0 0))";
 78    std::string cw_open_ring  = "POLYGON((0 0,0 1,1 1,1 0))";
 79    std::string ccw_open_ring = "POLYGON((0 0,1 0,1 1,0 1))";
 80
 81    // already cw_ring
 82    test_geometry<bg::model::ring<P> >(cw_ring, cw_ring);
 83
 84    // wrong order
 85    test_geometry<bg::model::ring<P> >(ccw_ring, cw_ring);
 86
 87    // ccw-ring, input ccw-ring, already correct
 88    test_geometry<bg::model::ring<P, false> >(ccw_ring, ccw_ring);
 89
 90    // ccw-ring, input cw-ring, corrected
 91    test_geometry<bg::model::ring<P, false> >(cw_ring, ccw_ring);
 92
 93    // open-ring, input ccw-ring, already correct
 94    test_geometry<bg::model::ring<P, true, false> >(cw_open_ring, cw_open_ring);
 95
 96    // ccw-ring, input cw-ring, corrected
 97    test_geometry<bg::model::ring<P, true, false> >(ccw_open_ring, "POLYGON((0 1,1 1,1 0,0 0))");
 98
 99
100
101    // not closed
102    test_geometry<bg::model::ring<P> >(
103            ccw_open_ring,
104            cw_ring);
105
106    // counter clockwise, cw_ring
107    test_geometry<bg::model::ring<P, false> >(ccw_ring, ccw_ring);
108
109    test_geometry<bg::model::ring<P, false> >(cw_ring, ccw_ring);
110
111
112    // polygon: cw_ring
113    test_geometry<bg::model::polygon<P> >(cw_ring, cw_ring);
114    // wrong order
115    test_geometry<bg::model::polygon<P> >(
116            "POLYGON((0 0,1 0,1 1,0 1,0 0))",
117            cw_ring);
118    // wrong order & not closed
119    test_geometry<bg::model::polygon<P> >(
120            ccw_open_ring,
121            cw_ring);
122
123
124    std::string cw_holey_polygon =
125            "POLYGON((0 0,0 4,4 4,4 0,0 0),(1 1,2 1,2 2,1 2,1 1))";
126    std::string ccw_holey_polygon =
127            "POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1,1 2,2 2,2 1,1 1))";
128
129    // with holes: cw_ring
130    test_geometry<bg::model::polygon<P> >(
131            cw_holey_polygon,
132            cw_holey_polygon);
133    // wrong order of main
134    test_geometry<bg::model::polygon<P> >(
135            "POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1))",
136            cw_holey_polygon);
137    // wrong order of hole
138    test_geometry<bg::model::polygon<P> >(
139            "POLYGON((0 0,0 4,4 4,4 0,0 0),(1 1,1 2,2 2,2 1,1 1))",
140            cw_holey_polygon);
141
142    // wrong order of main and hole
143    test_geometry<bg::model::polygon<P> >(ccw_holey_polygon, cw_holey_polygon);
144
145    // test the counter-clockwise
146    test_geometry<bg::model::polygon<P, false> >(
147            ccw_holey_polygon, ccw_holey_polygon);
148
149    // Boxes
150    std::string proper_box = "POLYGON((0 0,0 2,2 2,2 0,0 0))";
151    test_geometry<bg::model::box<P> >(proper_box, proper_box);
152    test_geometry<bg::model::box<P> >("BOX(0 0,2 2)", proper_box);
153    test_geometry<bg::model::box<P> >("BOX(2 2,0 0)", proper_box);
154    test_geometry<bg::model::box<P> >("BOX(0 2,2 0)", proper_box);
155
156    // Cubes
157    typedef bg::model::box<bg::model::point<double, 3, bg::cs::cartesian> > box3d;
158    std::string proper_3d_dsv_box = "((0, 0, 0), (2, 2, 2))";
159    test_geometry_dsv<box3d>("BOX(0 0 0,2 2 2)", proper_3d_dsv_box);
160    test_geometry_dsv<box3d>("BOX(2 2 2,0 0 0)", proper_3d_dsv_box);
161    test_geometry_dsv<box3d>("BOX(0 2 2,2 0 0)", proper_3d_dsv_box);
162    test_geometry_dsv<box3d>("BOX(2 0 2,0 2 0)", proper_3d_dsv_box);
163    test_geometry_dsv<box3d>("BOX(0 0 2,2 2 0)", proper_3d_dsv_box);
164}
165
166
167int test_main(int, char* [])
168{
169    //test_all<int[2]>();
170    //test_all<float[2]>(); not yet because cannot be copied, for polygon
171    //test_all<double[2]>();
172
173    //test_all<point_xy<int> >();
174    test_all<bg::model::d2::point_xy<float> >();
175    test_all<bg::model::d2::point_xy<double> >();
176
177#if defined(HAVE_TTMATH)
178    test_all<bg::model::d2::point_xy<ttmath_big> >();
179#endif
180
181    return 0;
182}