PageRenderTime 36ms CodeModel.GetById 25ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/libs/geometry/test/geometries/boost_polygon_overlay.cpp

http://hadesmem.googlecode.com/
C++ | 122 lines | 90 code | 24 blank | 8 comment | 0 complexity | e019d0abd747cb2d57edfc4223f0bb34 MD5 | raw file
  1// Boost.Geometry (aka GGL, Generic Geometry Library)
  2// Unit Test
  3
  4// Copyright (c) 2011 Barend Gehrels, Amsterdam, the Netherlands.
  5
  6// Use, modification and distribution is subject to the Boost Software License,
  7// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8// http://www.boost.org/LICENSE_1_0.txt)
  9
 10#include <geometry_test_common.hpp>
 11
 12
 13#include <boost/geometry/geometry.hpp>
 14#include <boost/geometry/geometries/point_xy.hpp>
 15#include <boost/geometry/geometries/adapted/boost_polygon/point.hpp>
 16#include <boost/geometry/geometries/adapted/boost_polygon/box.hpp>
 17#include <boost/geometry/geometries/adapted/boost_polygon/ring.hpp>
 18#include <boost/geometry/geometries/adapted/boost_polygon/polygon.hpp>
 19#include <boost/geometry/domains/gis/io/wkt/wkt.hpp>
 20
 21#include<iostream>
 22
 23template <typename T>
 24void test_overlay_using_boost_polygon(std::string const& case_id, std::string const& wkt_p, std::string const& wkt_q)
 25{
 26    typedef boost::polygon::polygon_set_data<T> polygon_set;
 27
 28    polygon_set p, q;
 29    polygon_set out_i, out_u;
 30
 31    {
 32        // Read polygons, conveniently using Boost.Geometry
 33        typedef boost::geometry::model::multi_polygon
 34            <
 35                boost::polygon::polygon_with_holes_data<T>
 36            > mp_type;
 37        mp_type mp, mq;
 38
 39        bg::read_wkt(wkt_p, mp);
 40        bg::read_wkt(wkt_q, mq);
 41
 42        p.insert(mp.begin(), mp.end());
 43        q.insert(mq.begin(), mq.end());
 44    }
 45
 46    {
 47        using namespace boost::polygon::operators;
 48        out_i = p & q;
 49        out_u = p | q;
 50    }
 51
 52    double area_p = boost::polygon::area(p);
 53    double area_q = boost::polygon::area(q);
 54    double area_i = boost::polygon::area(out_i);
 55    double area_u = boost::polygon::area(out_u);
 56
 57    double sum = area_p + area_q - area_u - area_i;
 58    BOOST_CHECK_MESSAGE(abs(sum) < 0.001,
 59        "Overlay error\n"
 60            << "Boost.Polygon " << case_id
 61            << " area p: " << area_p
 62            << " area q: " << area_q
 63            << " area i: " << area_i
 64            << " area u: " << area_u
 65            << " sum: " << sum
 66         );
 67}
 68
 69
 70template <typename T>
 71void test_overlay_using_boost_geometry(std::string const& case_id, std::string const& wkt_p, std::string const& wkt_q)
 72{
 73    typedef boost::geometry::model::multi_polygon
 74        <
 75            boost::geometry::model::polygon
 76                <
 77                    boost::geometry::model::d2::point_xy<T>
 78                >
 79        > mp_type;
 80
 81    // Read it using Boost.Geometry
 82    mp_type p, q, out_i, out_u;
 83
 84    boost::geometry::read_wkt(wkt_p, p);
 85    boost::geometry::read_wkt(wkt_q, q);
 86
 87    boost::geometry::intersection(p, q, out_i);
 88    boost::geometry::union_(p, q, out_u);
 89
 90    double area_p = boost::geometry::area(p);
 91    double area_q = boost::geometry::area(q);
 92    double area_i = boost::geometry::area(out_i);
 93    double area_u = boost::geometry::area(out_u);
 94
 95    double sum = area_p + area_q - area_u - area_i;
 96    BOOST_CHECK_MESSAGE(abs(sum) < 0.001,
 97        "Overlay error\n"
 98            << "Boost.Geometry " << case_id
 99            << " area p: " << area_p
100            << " area q: " << area_q
101            << " area i: " << area_i
102            << " area u: " << area_u
103            << " sum: " << sum
104        );
105}
106
107template <typename T>
108void test_overlay(std::string const& case_id, std::string const& wkt_p, std::string const& wkt_q)
109{
110    test_overlay_using_boost_polygon<T>(case_id, wkt_p, wkt_q);
111    test_overlay_using_boost_geometry<T>(case_id, wkt_p, wkt_q);
112}
113
114
115int test_main(int, char* [])
116{
117    test_overlay<int>("case 1", "MULTIPOLYGON(((100 900,0 800,100 800,100 900)),((200 700,100 800,100 700,200 700)),((500 400,400 400,400 300,500 400)),((600 300,500 200,600 200,600 300)),((600 700,500 800,500 700,600 700)),((700 500,600 500,600 400,700 500)),((900 300,800 400,800 300,900 300)))",
118        "MULTIPOLYGON(((200 900,100 1000,100 800,200 800,200 900)),((400 500,300 600,300 500,400 500)),((500 900,400 800,500 800,500 900)),((600 800,500 700,600 700,600 800)),((700 500,600 400,700 400,700 500)),((1000 1000,900 900,1000 900,1000 1000)))");
119    test_overlay<int>("case 2", "MULTIPOLYGON(((200 400,100 400,100 300,200 400)),((300 100,200 100,200 0,300 0,300 100)),((600 700,500 700,500 600,600 700)),((700 300,600 300,600 200,700 300)),((800 500,700 600,700 500,800 500)),((900 800,800 700,900 700,900 800)),((1000 200,900 100,1000 100,1000 200)),((1000 800,900 900,900 800,1000 800)))",
120        "MULTIPOLYGON(((200 800,100 800,100 700,200 700,200 800)),((400 200,300 100,400 100,400 200)),((400 800,300 700,400 700,400 800)),((700 100,600 0,700 0,700 100)),((700 200,600 200,600 100,700 200)),((900 200,800 200,800 0,900 0,900 200)),((1000 300,900 200,1000 200,1000 300)))");
121    return 0;
122}