PageRenderTime 22ms CodeModel.GetById 1ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://hadesmem.googlecode.com/
C++ | 365 lines | 192 code | 66 blank | 107 comment | 0 complexity | 9c48b272c05c1027525574272729f993 MD5 | raw file
  1// Boost.Geometry (aka GGL, Generic Geometry Library)
  2// Unit Test
  3
  4// Copyright (c) 2010-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//#define BOOST_GEOMETRY_CHECK_WITH_POSTGIS
 11
 12//#define BOOST_GEOMETRY_DEBUG_ASSEMBLE
 13
 14
 15#include <iostream>
 16#include <string>
 17#include <iomanip>
 18
 19#include <boost/geometry/algorithms/correct.hpp>
 20#include <boost/geometry/algorithms/perimeter.hpp>
 21
 22#include <boost/geometry/multi/algorithms/correct.hpp>
 23#include <boost/geometry/multi/algorithms/intersection.hpp>
 24#include <boost/geometry/multi/algorithms/within.hpp>
 25
 26#include <boost/geometry/geometries/point_xy.hpp>
 27
 28//#include <boost/geometry/extensions/gis/io/wkb/read_wkb.hpp>
 29//#include <boost/geometry/extensions/gis/io/wkb/utility.hpp>
 30
 31#include <algorithms/test_difference.hpp>
 32#include <algorithms/test_overlay.hpp>
 33#include <algorithms/overlay/overlay_cases.hpp>
 34#include <multi/algorithms/overlay/multi_overlay_cases.hpp>
 35
 36
 37#ifdef HAVE_TTMATH
 38#  include <boost/geometry/extensions/contrib/ttmath_stub.hpp>
 39#endif
 40
 41
 42template <typename P>
 43void test_all()
 44{
 45    typedef bg::model::box<P> box;
 46    typedef bg::model::polygon<P> polygon;
 47    typedef bg::model::ring<P> ring;
 48
 49    bool const is_float =
 50        boost::is_same<typename bg::coordinate_type<P>::type, float>::value;
 51
 52    test_one<polygon, polygon, polygon>("simplex_normal",
 53        simplex_normal[0], simplex_normal[1],
 54        3, 3, 2.52636706856656,
 55        3, 3, 3.52636706856656);
 56
 57    test_one<polygon, polygon, polygon>("simplex_with_empty",
 58        simplex_normal[0], polygon_empty,
 59        1, 4, 8.0,
 60        0, 0, 0.0);
 61
 62    test_one<polygon, polygon, polygon>(
 63            "star_ring", example_star, example_ring,
 64            5, 22, 1.1901714,
 65            5, 27, 1.6701714);
 66
 67    test_one<polygon, polygon, polygon>("two_bends",
 68        two_bends[0], two_bends[1],
 69        1, 7, 8.0,
 70        1, 7, 8.0);
 71
 72    test_one<polygon, polygon, polygon>("star_comb_15",
 73        star_comb_15[0], star_comb_15[1],
 74        30, 150, 227.658275102812,
 75        30, 150, 480.485775259312);
 76
 77    test_one<polygon, polygon, polygon>("new_hole",
 78        new_hole[0], new_hole[1],
 79        1, 10, 7.0,
 80        1, 10, 14.0);
 81
 82
 83    test_one<polygon, polygon, polygon>("crossed",
 84        crossed[0], crossed[1],
 85        1, 0, 19.5,
 86        1, 0, 2.5);
 87
 88    test_one<polygon, polygon, polygon>("disjoint",
 89        disjoint[0], disjoint[1],
 90        1, 5, 1.0,
 91        1, 5, 1.0);
 92
 93    test_one<polygon, polygon, polygon>("distance_zero",
 94        distance_zero[0], distance_zero[1],
 95        2, 0, 8.7048386,
 96        is_float ? 1 : 2, // The too small one is discarded for floating point
 97        0, 0.0098387);
 98
 99
100    test_one<polygon, polygon, polygon>("equal_holes_disjoint",
101        equal_holes_disjoint[0], equal_holes_disjoint[1],
102        1, 0, 9.0,
103        1, 0, 9.0);
104
105    test_one<polygon, polygon, polygon>("only_hole_intersections1",
106        only_hole_intersections[0], only_hole_intersections[1],
107        2, 0,  1.9090909,
108        4, 0, 10.9090909);
109
110    test_one<polygon, polygon, polygon>("only_hole_intersection2",
111        only_hole_intersections[0], only_hole_intersections[2],
112        3, 0, 30.9090909,
113        4, 0, 10.9090909);
114
115    test_one<polygon, polygon, polygon>("first_within_second",
116        first_within_second[1], first_within_second[0],
117        1, 1, 24,
118        0, 0, 0);
119
120    test_one<polygon, polygon, polygon>("fitting",
121        fitting[0], fitting[1],
122        1, 0, 21.0,
123        1, 0, 4.0);
124
125    test_one<polygon, polygon, polygon>("identical",
126        identical[0], identical[1],
127        0, 0, 0.0,
128        0, 0, 0.0);
129
130    test_one<polygon, polygon, polygon>("intersect_exterior_and_interiors_winded",
131        intersect_exterior_and_interiors_winded[0], intersect_exterior_and_interiors_winded[1],
132        4, 0, 11.533333,
133        5, 0, 29.783333);
134
135    test_one<polygon, polygon, polygon>("intersect_holes_intersect_and_disjoint",
136        intersect_holes_intersect_and_disjoint[0], intersect_holes_intersect_and_disjoint[1],
137        2, 0, 15.75,
138        3, 0, 6.75);
139
140    test_one<polygon, polygon, polygon>("intersect_holes_intersect_and_touch",
141        intersect_holes_intersect_and_touch[0], intersect_holes_intersect_and_touch[1],
142        3, 0, 16.25,
143        3, 0, 6.25);
144
145    test_one<polygon, polygon, polygon>("intersect_holes_new_ring",
146        intersect_holes_new_ring[0], intersect_holes_new_ring[1],
147        3, 0, 9.8961,
148        4, 0, 121.8961, 0.01);
149
150    test_one<polygon, polygon, polygon>("first_within_hole_of_second",
151        first_within_hole_of_second[0], first_within_hole_of_second[1],
152        1, -1, 1,
153        1, -1, 16);
154
155    test_one<polygon, polygon, polygon>("intersect_holes_disjoint",
156        intersect_holes_disjoint[0], intersect_holes_disjoint[1],
157        2, 15, 16.0,
158        2, 15, 6.0);
159
160    test_one<polygon, polygon, polygon>("intersect_holes_intersect",
161        intersect_holes_intersect[0], intersect_holes_intersect[1],
162        2, 14, 15.75,
163        2, 14, 5.75);
164
165    test_one<polygon, polygon, polygon>(
166            "case4", case_4[0], case_4[1],
167            6, 22, 2.77878787878788,
168            4, 27, 4.77878787878788);
169
170    test_one<polygon, polygon, polygon>(
171            "case5", case_5[0], case_5[1],
172            8, 22, 2.43452380952381,
173            7, 27, 3.18452380952381);
174
175    test_one<polygon, polygon, polygon>("winded",
176        winded[0], winded[1],
177        3, 1, 61,
178        1, 0, 13);
179
180    test_one<polygon, polygon, polygon>("within_holes_disjoint",
181        within_holes_disjoint[0], within_holes_disjoint[1],
182        2, 1, 25,
183        1, 0, 1);
184
185    test_one<polygon, polygon, polygon>("side_side",
186        side_side[0], side_side[1],
187        1, 0, 1,
188        1, 0, 1);
189
190    /*** TODO: self-tangencies for difference
191    test_one<polygon, polygon, polygon>("wrapped_a",
192        wrapped[0], wrapped[1],
193        3, 1, 61,
194        1, 0, 13);
195
196    test_one<polygon, polygon, polygon>("wrapped_b",
197        wrapped[0], wrapped[2],
198        3, 1, 61,
199        1, 0, 13);
200    ***/
201
202#ifdef _MSC_VER
203    {
204        // Isovist (submitted by Brandon during Formal Review)
205        std::string tn = string_from_type<typename bg::coordinate_type<polygon>::type>::name();
206        test_one<polygon, polygon, polygon>("isovist",
207            isovist1[0], isovist1[1],
208            4, 0, 0.279121891701124,
209            4, 0, 224.889211358929,
210            0.01);
211    }
212#endif
213
214
215    // Other combi's
216    {
217        test_one<polygon, polygon, ring>(
218                "star_ring_ring", example_star, example_ring,
219                5, 22, 1.1901714, 5, 27, 1.6701714);
220
221        test_one<polygon, ring, polygon>(
222                "ring_star_ring", example_ring, example_star,
223                5, 22, 1.6701714, 5, 27, 1.1901714);
224
225        static std::string const clip = "POLYGON((2.5 0.5,5.5 2.5))";
226
227        test_one<polygon, box, ring>("star_box",
228            clip, example_star,
229            4, 11, 2.833333, 4, 11, 0.833333);
230
231        test_one<polygon, ring, box>("box_star",
232            example_star, clip,
233            4, 11, 0.833333, 4, 11, 2.833333);
234    }
235
236    // Counter clockwise
237    {
238        typedef bg::model::polygon<P, false> polygon_ccw;
239        test_one<polygon, polygon_ccw, polygon_ccw>(
240                "star_ring_ccw", example_star, example_ring,
241                5, 22, 1.1901714, 5, 27, 1.6701714);
242        test_one<polygon, polygon, polygon_ccw>(
243                "star_ring_ccw1", example_star, example_ring,
244                5, 22, 1.1901714, 5, 27, 1.6701714);
245        test_one<polygon, polygon_ccw, polygon>(
246                "star_ring_ccw2", example_star, example_ring,
247                5, 22, 1.1901714, 5, 27, 1.6701714);
248    }
249
250
251
252    // Multi
253    {
254        typedef bg::model::multi_polygon<polygon> mp;
255
256        static std::string const clip = "POLYGON((2 2,4 4))";
257
258        test_one<polygon, box, mp>("simplex_multi_box_mp",
259            clip, case_multi_simplex[0],
260            3, 11, 4.53333, 3, 11, 8.53333);
261        test_one<polygon, mp, box>("simplex_multi_mp_box",
262            case_multi_simplex[0], clip,
263            3, 11, 8.53333, 3, 11, 4.53333);
264    }
265
266    /***
267    Experimental (cut), does not work:
268    test_one<polygon, polygon, polygon>(
269            "polygon_pseudo_line",
270            "POLYGON((0 0,0 4,4 4,4 0,0 0))",
271            "POLYGON((2 -2,2 -1,2 6,2 -2))",
272            5, 22, 1.1901714,
273            5, 27, 1.6701714);
274    ***/
275}
276
277/*******
278// To be moved to another file
279template <typename T>
280void test_difference_parcel_precision()
281{
282    typedef bg::model::d2::point_xy<T> point_type;
283    typedef bg::model::polygon<point_type> polygon_type;
284    typedef bg::model::linestring<point_type> linestring_type;
285    typedef std::vector<boost::uint8_t> byte_vector;
286
287    polygon_type parcel, buffer;
288
289    {
290        byte_vector wkb;
291        bg::hex2wkb("0103000000010000001A00000023DBF97EE316064146B6F3FDD32513415A643BDFD216064175931804E225134196438B6C52150641C976BE9F34261341F6285C8F06150641022B871641261341F853E3A5D3140641E17A14AE50261341B81E85EBFB120641A4703D8A142713414E6210584D120641AC1C5A64602713414E621058431106414E621058C9271341A01A2FDD1B11064121B07268D8271341F4FDD478571006411904560ECF271341448B6CE7DD0F06418195438BC1271341F6285C8F6A0F06413BDF4F0DA72713410E2DB29D360F06416F1283C091271341E5D022DB070F0641F6285C0F7227134160E5D022DA0E06416F1283404F271341448B6CE7D30E0641F853E3A52427134154E3A59BE60E06411904568E07271341643BDF4F0C0F0641D7A3703DFC2613414A0C022B100F064125068115FB26134191ED7C3F310F0641B6F3FDD4F42613414C378941F11006414A0C022BA0261341EC51B81ECC1206413BDF4F0D40261341022B87167514064125068115F1251341B4C876BE8C160641C74B37897F25134121B07268C6160641508D976E7525134123DBF97EE316064146B6F3FDD3251341", std::back_inserter(wkb));
292        bg::read_wkb(wkb.begin(), wkb.end(), parcel);
293    }
294    {
295        byte_vector wkb;
296        bg::hex2wkb("01030000000100000083000000000032FCD716064100009E998F25134100000706D81606410040A4998F2513410000DA0FD816064100C0E6998F2513410000A819D81606410080659A8F25134100806A23D816064100C0209B8F25134100801E2DD81606410080189C8F2513410000BE36D816064100404D9D8F25134100004540D816064100C0BE9E8F2513410000AF49D816064100806DA08F2513410000F752D8160641008059A28F2513410000195CD816064100C082A48F25134100800F65D81606410080E9A68F2513410000D66DD816064100408EA98F25134100006876D816064100C070AC8F2513410000C17ED8160641000091AF8F2513410080DC86D816064100C0EFB28F25134100009E8ED816064100C081B68F2513410080EC95D816064100803ABA8F2513410080C79CD8160641000018BE8F25134100002FA3D8160641008017C28F251341008022A9D8160641000037C68F2513410080A1AED8160641000074CA8F2513410000ACB3D81606410040CCCE8F251341000042B8D816064100403DD38F251341000062BCD81606410000C5D78F25134100000DC0D8160641000061DC8F251341000042C3D816064100000FE18F251341000001C6D81606410080CCE58F251341008049C8D8160641004097EA8F25134100001BCAD816064100006DEF8F251341008075CBD816064100804BF48F251341008058CCD8160641004030F98F2513410000C4CCD8160641000019FE8F2513410080B7CCD81606410080030390251341008032CCD81606410000ED0790251341000035CBD81606410000D40C902513410080BEC9D81606410040B511902513410000CFC7D816064100408F1690251341008065C5D816064100005F1B90251341008082C2D81606410080222090251341000025BFD81606410080D7249025134100004DBBD816064100807B29902513410080FAB6D816064100800C2E9025134100002DB2D816064100C08732902513410080E3ACD81606410000EB369025134100801EA7D81606410000343B902513410000DEA0D81606410080603F902513410080209AD816064100406E43902513410080209AC015064100406E43302613410080FC92C015064100004F473026134100008B8BC01506410040F64A302613410000D083C015064100C0634E302613410000D17BC0150641008097513026134100009273C015064100409154302613410000186BC015064100C050573026134100806762C01506410000D6593026134100808559C01506410000215C3026134100007650C01506410000315E3026134100003E47C015064100800660302613410000E23DC01506410000A1613026134100006734C015064100800063302613410080D12AC015064100C024643026134100002621C015064100800D653026134100006917C015064100C0BA653026134100809F0DC015064100402C66302613410000CE03C015064100006266302613410000F9F9BF15064100C05B6630261341000026F0BF1506410040196630261341000058E6BF15064100809A6530261341008095DCBF1506410040DF64302613410080E1D2BF1506410080E76330261341000042C9BF15064100C0B262302613410000BBBFBF1506410040416130261341000051B6BF1506410080925F30261341000009ADBF1506410080A65D302613410000E7A3BF15064100407D5B302613410080F09ABF150641008016593026134100002A92BF15064100C071563026134100009889BF15064100408F533026134100003F81BF15064100006F503026134100802379BF1506410040104D3026134100006271BF15064100407E49302613410080136ABF1506410080C5453026134100803863BF1506410000E841302613410000D15CBF1506410080E83D302613410080DD56BF1506410000C9393026134100805E51BF15064100008C35302613410000544CBF15064100C03331302613410000BE47BF15064100C0C22C3026134100009E43BF15064100003B28302613410000F33FBF15064100009F23302613410000BE3CBF1506410000F11E302613410000FF39BF1506410080331A302613410080B637BF15064100C06815302613410000E535BF150641000093103026134100808A34BF1506410080B40B302613410080A733BF15064100C0CF063026134100003C33BF1506410000E7013026134100804833BF1506410080FCFC2F2613410080CD33BF150641000013F82F2613410000CB34BF15064100002CF32F26134100804136BF15064100C04AEE2F26134100003138BF15064100C070E92F26134100809A3ABF1506410000A1E42F26134100807D3DBF1506410080DDDF2F2613410000DB40BF150641008028DB2F2613410000B344BF150641008084D62F26134100800549BF1506410080F3D12F2613410000D34DBF150641004078CD2F26134100801C53BF150641000015C92F2613410080E158BF1506410000CCC42F2613410000225FBF15064100809FC02F2613410080DF65BF15064100C091BC2F2613410080DF65D716064100C091BC8F2513410080036DD71606410000B1B88F25134100007574D716064100C009B58F2513410000307CD716064100409CB18F25134100002F84D7160641008068AE8F25134100006E8CD716064100C06EAB8F2513410000E894D71606410040AFA88F2513410080989DD716064100002AA68F25134100807AA6D71606410000DFA38F25134100008AAFD71606410000CFA18F2513410000C2B8D71606410080F99F8F25134100001EC2D716064100005F9E8F251341000099CBD71606410080FF9C8F25134100802ED5D71606410040DB9B8F2513410000DADED71606410080F29A8F251341000097E8D71606410040459A8F251341008060F2D716064100C0D3998F251341000032FCD716064100009E998F251341", std::back_inserter(wkb));
297        bg::read_wkb(wkb.begin(), wkb.end(), buffer);
298    }
299    bg::correct(parcel);
300    bg::correct(buffer);
301
302    std::vector<polygon_type> pieces;
303    bg::difference(parcel, buffer, pieces);
304
305    std::vector<polygon_type> filled_out;
306    bg::difference(parcel, pieces.back(), filled_out);
307
308#if defined(TEST_OUTPUT)
309    std::cout << bg::area(parcel) << std::endl;
310    std::cout << bg::area(buffer) << std::endl;
311    std::cout << pieces.size() << std::endl;
312    std::cout << bg::area(pieces.front()) << std::endl;
313    std::cout << filled_out.size() << std::endl;
314    std::cout << std::setprecision(16) << bg::wkt(filled_out.front()) << std::endl;
315    std::cout << bg::wkt(filled_out.front()) << std::endl;
316    std::cout << bg::area(filled_out.front()) << std::endl;
317    std::cout << bg::perimeter(filled_out.front()) << std::endl;
318#endif
319
320#if defined(TEST_WITH_SVG)
321    {
322        linestring_type cut_line;
323        bg::read_wkt("linestring(180955 313700,180920 313740)", cut_line);
324
325        std::ostringstream filename;
326        filename << "difference_precision_"
327            << string_from_type<T>::name()
328            << ".svg";
329
330        std::ofstream svg(filename.str().c_str());
331
332        bg::svg_mapper<point_type> mapper(svg, 500, 500);
333
334        mapper.add(cut_line);
335
336        //mapper.map(parcel, "fill-opacity:0.5;fill:rgb(153,204,0);stroke:rgb(153,204,0);stroke-width:3");
337        mapper.map(pieces.front(), "fill-opacity:0.5;fill:rgb(153,204,0);stroke:rgb(153,204,0);stroke-width:1");
338        mapper.map(pieces.back(), "fill-opacity:0.5;fill:rgb(153,204,0);stroke:rgb(153,204,0);stroke-width:1");
339        mapper.map(filled_out.front(), "fill-opacity:0.3;fill:rgb(51,51,153);stroke:rgb(51,51,153);stroke-width:3");
340
341        mapper.map(cut_line, "opacity:0.8;fill:none;stroke:rgb(255,128,0);stroke-width:5;stroke-dasharray:1,7;stroke-linecap:round");
342        //mapper.map(cut_line, "opacity:0.8;fill:none;stroke:rgb(255,128,0);stroke-width:2");
343    }
344#endif
345}
346*****/
347
348int test_main(int, char* [])
349{
350    //test_difference_parcel_precision<float>();
351    //test_difference_parcel_precision<double>();
352
353    test_all<bg::model::d2::point_xy<double> >();
354
355#if ! defined(BOOST_GEOMETRY_TEST_ONLY_ONE_TYPE)
356    test_all<bg::model::d2::point_xy<float> >();
357
358#ifdef HAVE_TTMATH
359    test_all<bg::model::d2::point_xy<ttmath_big> >();
360    //test_difference_parcel_precision<ttmath_big>();
361#endif
362#endif
363
364    return 0;
365}