PageRenderTime 92ms CodeModel.GetById 9ms app.highlight 71ms RepoModel.GetById 3ms app.codeStats 0ms

/Src/Dependencies/Boost/libs/geometry/test/algorithms/overlay/get_turns.cpp

http://hadesmem.googlecode.com/
C++ | 379 lines | 247 code | 81 blank | 51 comment | 3 complexity | 1b9f3944761de26ec2de259fa043858f 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 <iostream>
 16#include <iomanip>
 17
 18#include <geometry_test_common.hpp>
 19
 20#define BOOST_GEOMETRY_DEBUG_SEGMENT_IDENTIFIER
 21
 22
 23#include <boost/geometry/strategies/strategies.hpp>
 24
 25#include <boost/geometry/algorithms/correct.hpp>
 26#include <boost/geometry/algorithms/detail/overlay/get_turns.hpp>
 27
 28#include <boost/geometry/algorithms/detail/overlay/debug_turn_info.hpp>
 29
 30#include <boost/geometry/geometries/geometries.hpp>
 31
 32#include <boost/geometry/domains/gis/io/wkt/read_wkt.hpp>
 33#include <boost/geometry/domains/gis/io/wkt/write_wkt.hpp>
 34
 35#if defined(TEST_WITH_SVG)
 36#  include <boost/geometry/extensions/io/svg/svg_mapper.hpp>
 37#endif
 38
 39#include <algorithms/overlay/overlay_cases.hpp>
 40
 41
 42
 43
 44// To test that "get_turns" can be called using additional information
 45template <typename P>
 46struct my_turn_op : public bg::detail::overlay::turn_operation
 47{
 48};
 49
 50namespace detail
 51{
 52
 53struct test_get_turns
 54{
 55    template<typename G1, typename G2>
 56    static void apply(std::string const& id,
 57            int expected_count,
 58            G1 const& g1, G2 const& g2, double precision)
 59    {
 60            typedef bg::detail::overlay::turn_info
 61            <
 62                typename bg::point_type<G2>::type
 63            > turn_info;
 64        std::vector<turn_info> turns;
 65
 66        bg::detail::get_turns::no_interrupt_policy policy;
 67        bg::get_turns<false, false, bg::detail::overlay::assign_null_policy>(g1, g2, turns, policy);
 68
 69        BOOST_CHECK_MESSAGE(
 70            expected_count == boost::size(turns),
 71                "get_turns: " << id
 72                << " #turns expected: " << expected_count
 73                << " detected: " << boost::size(turns)
 74                << " type: " << string_from_type
 75                    <typename bg::coordinate_type<G1>::type>::name()
 76                );
 77
 78#if defined(TEST_WITH_SVG)
 79        {
 80            typedef typename bg::coordinate_type<G1>::type coordinate_type;
 81            std::map<std::pair<coordinate_type, coordinate_type>, int> offsets;
 82            std::ostringstream filename;
 83            filename << "get_turns_" << id
 84                << "_" << string_from_type<typename bg::coordinate_type<G1>::type>::name()
 85                << ".svg";
 86
 87            std::ofstream svg(filename.str().c_str());
 88
 89            bg::svg_mapper<typename bg::point_type<G2>::type> mapper(svg, 500, 500);
 90            mapper.add(g1);
 91            mapper.add(g2);
 92
 93            // Input shapes in green/blue
 94            mapper.map(g1, "fill-opacity:0.5;fill:rgb(153,204,0);"
 95                    "stroke:rgb(153,204,0);stroke-width:3");
 96            mapper.map(g2, "fill-opacity:0.3;fill:rgb(51,51,153);"
 97                    "stroke:rgb(51,51,153);stroke-width:3");
 98
 99            int index = 0;
100            BOOST_FOREACH(turn_info const& turn, turns)
101            {
102                mapper.map(turn.point, "fill:rgb(255,128,0);stroke:rgb(0,0,100);stroke-width:1");
103
104                // Map characteristics
105                std::pair<coordinate_type, coordinate_type> p
106                    = std::make_pair(bg::get<0>(turn.point), bg::get<1>(turn.point));
107
108                {
109                    std::ostringstream out;
110                    out << index
111                        << ": " << bg::operation_char(turn.operations[0].operation)
112                        << " " << bg::operation_char(turn.operations[1].operation)
113                        << " (" << bg::method_char(turn.method) << ")"
114                        << (turn.is_discarded() ? " (discarded) " : turn.blocked() ? " (blocked)" : "")
115                        ;
116
117                    offsets[p] += 10;
118                    int offset = offsets[p];
119                    mapper.text(turn.point, out.str(),
120                        "fill:rgb(0,0,0);font-family:Arial;font-size:8px",
121                        5, offset);
122                }
123
124                ++index;
125            }
126        }
127#endif
128    }
129};
130
131}
132
133template<typename G1, typename G2>
134struct test_get_turns
135{
136    inline static void apply(std::string const& id, int expected_count, 
137                std::string const& wkt1, std::string const& wkt2,
138                double precision = 0.001)
139    {
140        if (wkt1.empty() || wkt2.empty())
141        {
142            return;
143        }
144
145        G1 g1;
146        bg::read_wkt(wkt1, g1);
147
148        G2 g2;
149        bg::read_wkt(wkt2, g2);
150
151        bg::correct(g1);
152        bg::correct(g2);
153
154        // Try the overlay-function in both ways
155        std::string caseid = id;
156        //goto case_reversed;
157
158#ifdef BOOST_GEOMETRY_DEBUG_INTERSECTION
159        std::cout << std::endl << std::endl << "# " << caseid << std::endl;
160#endif
161        detail::test_get_turns::apply(caseid, expected_count, g1, g2, precision);
162
163#ifdef BOOST_GEOMETRY_DEBUG_INTERSECTION
164        return;
165#endif
166
167    //case_reversed:
168#if ! defined(BOOST_GEOMETRY_TEST_OVERLAY_NOT_EXCHANGED)
169        caseid = id + "_rev";
170#ifdef BOOST_GEOMETRY_DEBUG_INTERSECTION
171        std::cout << std::endl << std::endl << "# " << caseid << std::endl;
172#endif
173
174        detail::test_get_turns::apply(caseid, expected_count, g2, g1, precision);
175#endif
176    }
177};
178
179#if ! defined(GEOMETRY_TEST_MULTI)
180template <typename T>
181void test_all()
182{
183    typedef bg::model::point<T, 2, bg::cs::cartesian> P;
184    typedef bg::model::polygon<P> polygon;
185    typedef bg::model::linestring<P> linestring;
186    typedef bg::model::box<P> box;
187
188    // Expected count, average x, average y
189    typedef boost::tuple<int> Tuple;
190
191#ifdef BOOST_GEOMETRY_DEBUG_INTERSECTION
192    std::cout << string_from_type<T>::name() << std::endl;
193#endif
194
195
196    // snl
197    /*
198    test_get_turns<polygon, polygon>::apply("snl_2",
199        5,
200        //snl-1
201        //"POLYGON((182467 605842,182480 605954,182557 605958,182571 605958,182585 605958,182579 605843,182559 605838,182467 605842))",
202        //"POLYGON((182499 605955,182511 605960,182536 605974,182536 605981,182536 606006,182563 606006,182610 605985,182613 605976,182620 605948,182628 605937,182631 605924,182639 605889,182634 605885,182603 605848,182579 605843,182585 605958,182571 605958,182557 605958,182499 605955))");
203        //snl-2
204        //"POLYGON((120812 525783,120845 525792,120821 525842,120789 525826,120818 525849,120831 525854,120875 525875,120887 525881,120887 525881,120920 525834,120920 525834,120811 525772,120789 525826,120812 525783))",
205        //"POLYGON((120789 525826,120812 525783,120845 525792,120821 525842,120789 525826,120818 525849,120831 525854,120875 525875,120923 525836,120811 525772,120789 525826))"
206        //snl-4
207        "POLYGON((184913.4512400339881423860788345336914 606985.779408219968900084495544433594,184912.8999999999941792339086532592773 606987.145999999949708580970764160156,184904.4135310589917935431003570556641 606987.651360383024439215660095214844,184901.847619076987029984593391418457 607014.593436188995838165283203125,184916.3977574919990729540586471557617 607021.060164373018778860569000244141,184927.7147701499925460666418075561523 607008.126435620011761784553527832031,184926.0980706939881201833486557006836 606998.426238880958408117294311523438,184913.4512400339881423860788345336914 606985.779408219968900084495544433594),(184907.5560000000114087015390396118164 607013.300999999977648258209228515625,184905.7820000000065192580223083496094 607009.971999999950639903545379638672,184906.0039999999862629920244216918945 607005.978000000002793967723846435547,184908.4439999999885912984609603881836 606998.876999999978579580783843994141,184912.2149999999965075403451919555664 606994.217999999993480741977691650391,184919.3140000000130385160446166992188 606993.996000000042840838432312011719,184922.4200000000128056854009628295898 606995.770999999949708580970764160156,184925.7470000000030267983675003051758 606998.876999999978579580783843994141,184926.4130000000004656612873077392578 607002.871999999973922967910766601563,184925.7470000000030267983675003051758 607007.753000000026077032089233398438,184922.4200000000128056854009628295898 607012.190999999991618096828460693359,184917.0959999999904539436101913452148 607015.297999999951571226119995117188,184911.7710000000079162418842315673828 607015.297999999951571226119995117188,184907.5560000000114087015390396118164 607013.300999999977648258209228515625))",
208        "POLYGON((184861.1180000010062940418720245361328 606901.158000000054016709327697753906,184893.7870000000111758708953857421875 606898.482999998959712684154510498047,184925.0430000009946525096893310546875 606913.399999998975545167922973632813,184927.1739999990095384418964385986328 606951.758999999961815774440765380859,184912.8999999990046489983797073364258 606987.146000002045184373855590820313,184877.8700000010139774531126022338867 606989.232000001007691025733947753906,184885.1030000000027939677238464355469 607023.773999999975785613059997558594,184899.0579999980109278112649917602539 607022.743000000948086380958557128906,184906.0080000009911600500345230102539 607044.947999999043531715869903564453,184966.4649999999965075403451919555664 607025.020000000018626451492309570313,184968.4420000019890721887350082397461 606961.300000000977888703346252441406,185024.7679999989923089742660522460938 606947.401999998954124748706817626953,185024.5439999999944120645523071289063 606941.354999999981373548507690429688,185027.0069999989937059581279754638672 606937.322999999043531715869903564453,185030.3660000000090803951025009155273 606934.186999998986721038818359375,185035.5159999990137293934822082519531 606933.962999999988824129104614257813,185040.4420000019890721887350082397461 606935.530999999027699232101440429688,185042.905000000988366082310676574707 606939.114999998011626303195953369141,185088.3640000000013969838619232177734 606931.385000001988373696804046630859,185089.1389999990060459822416305541992 607015.508999999961815774440765380859,185095.1999999989930074661970138549805 607011.300000000977888703346252441406,185118.8269999999902211129665374755859 606995.545000002020969986915588378906,185126.813000001013278961181640625 606991.9950000010430812835693359375,185177.7270000019925646483898162841797 606973.798999998951330780982971191406,185181.4820000010076910257339477539063 606966.67599999904632568359375,185193.5709999990067444741725921630859 606977.795000002020969986915588378906,185193.710999998991610482335090637207 606960.300000000977888703346252441406,185189.3520000019925646483898162841797 606779.020000000018626451492309570313,185167.5150000010035000741481781005859 606783.844000000972300767898559570313,185086.9600000010104849934577941894531 606801.241000000038184225559234619141,185011.7069999990053474903106689453125 606817.809000000008381903171539306641,185000 606819.304000001051463186740875244141,184994.0340000019932631403207778930664 606819.793999999994412064552307128906,184976.3979999980074353516101837158203 606819.572000000975094735622406005859,184956.6539999989909119904041290283203 606817.1310000009834766387939453125,184934.9129999990109354257583618164063 606813.136999998008832335472106933594,184893.0969999989902134984731674194336 606804.927000000956468284130096435547,184884.4450000000069849193096160888672 606831.555000000051222741603851318359,184866.9189999999944120645523071289063 606883.480999998981133103370666503906,184861.1180000010062940418720245361328 606901.158000000054016709327697753906),(184907.5560000019904691725969314575195 607013.30099999904632568359375,184905.7820000019855797290802001953125 607009.971999999019317328929901123047,184906.0040000010048970580101013183594 607005.978000000002793967723846435547,184908.4439999980095308274030685424805 606998.876999999978579580783843994141,184912.2149999999965075403451919555664 606994.217999998014420270919799804688,184919.3139999989944044500589370727539 606993.995999998995102941989898681641,184922.420000001991866156458854675293 606995.771000002045184373855590820313,184925.7470000009925570338964462280273 606998.876999999978579580783843994141,184926.4129999990109354257583618164063 607002.872000001021660864353179931641,184925.7470000009925570338964462280273 607007.752999998978339135646820068359,184922.420000001991866156458854675293 607012.190999999991618096828460693359,184917.0960000010090880095958709716797 607015.297999999951571226119995117188,184911.7710000019869767129421234130859 607015.297999999951571226119995117188,184907.5560000019904691725969314575195 607013.30099999904632568359375))"
209        );
210
211
212    return;
213    */
214
215    // 1-6
216    test_get_turns<polygon, polygon>::apply("1", 6, case_1[0], case_1[1]);
217    test_get_turns<polygon, polygon>::apply("2", 8, case_2[0], case_2[1]);
218    test_get_turns<polygon, polygon>::apply("3", 4, case_3[0], case_3[1]);
219    test_get_turns<polygon, polygon>::apply("4", 12, case_4[0], case_4[1]);
220    test_get_turns<polygon, polygon>::apply("5", 17, case_5[0], case_5[1]);
221    test_get_turns<polygon, polygon>::apply("6", 3, case_6[0], case_6[1]);
222
223    // 7-12
224    test_get_turns<polygon, polygon>::apply("7", 2, case_7[0], case_7[1]);
225    test_get_turns<polygon, polygon>::apply("8", 2, case_8[0], case_8[1]);
226    test_get_turns<polygon, polygon>::apply("9", 1, case_9[0], case_9[1]);
227    test_get_turns<polygon, polygon>::apply("10", 3, case_10[0], case_10[1]);
228    test_get_turns<polygon, polygon>::apply("11", 1, case_11[0], case_11[1]);
229    test_get_turns<polygon, polygon>::apply("12", 8, case_12[0], case_12[1]);
230
231    // 13-18
232    test_get_turns<polygon, polygon>::apply("13", 2, case_13[0], case_13[1]);
233    test_get_turns<polygon, polygon>::apply("14", 2, case_14[0], case_14[1]);
234    test_get_turns<polygon, polygon>::apply("15", 2, case_15[0], case_15[1]);
235    test_get_turns<polygon, polygon>::apply("16", 4, case_16[0], case_16[1]);
236    test_get_turns<polygon, polygon>::apply("17", 2, case_17[0], case_17[1]);
237    test_get_turns<polygon, polygon>::apply("18", 4, case_18[0], case_18[1]);
238
239    // 19-24
240    test_get_turns<polygon, polygon>::apply("19", 2, case_19[0], case_19[1]);
241    test_get_turns<polygon, polygon>::apply("20", 3, case_20[0], case_20[1]);
242    test_get_turns<polygon, polygon>::apply("21", 3, case_21[0], case_21[1]);
243    test_get_turns<polygon, polygon>::apply("22", 1, case_22[0], case_22[1]);
244    test_get_turns<polygon, polygon>::apply("23", 2, case_23[0], case_23[1]);
245    test_get_turns<polygon, polygon>::apply("24", 1, case_24[0], case_24[1]);
246
247    // 25-30
248    test_get_turns<polygon, polygon>::apply("25", 1, case_25[0], case_25[1]);
249    test_get_turns<polygon, polygon>::apply("26", 1, case_26[0], case_26[1]);
250    test_get_turns<polygon, polygon>::apply("27", 2, case_27[0], case_27[1]);
251    test_get_turns<polygon, polygon>::apply("28", 2, case_28[0], case_28[1]);
252    test_get_turns<polygon, polygon>::apply("29", 2, case_29[0], case_29[1]);
253    test_get_turns<polygon, polygon>::apply("30", 2, case_30[0], case_30[1]);
254
255    // 31-36
256    test_get_turns<polygon, polygon>::apply("31", 1, case_31[0], case_31[1]);
257    test_get_turns<polygon, polygon>::apply("32", 1, case_32[0], case_32[1]);
258    test_get_turns<polygon, polygon>::apply("33", 1, case_33[0], case_33[1]);
259    test_get_turns<polygon, polygon>::apply("34", 2, case_34[0], case_34[1]);
260    test_get_turns<polygon, polygon>::apply("35", 1, case_35[0], case_35[1]);
261    test_get_turns<polygon, polygon>::apply("36", 3, case_36[0], case_36[1]);
262
263    // 37-42
264    test_get_turns<polygon, polygon>::apply("37", 3, case_37[0], case_37[1]);
265    test_get_turns<polygon, polygon>::apply("38", 3, case_38[0], case_38[1]);
266    test_get_turns<polygon, polygon>::apply("39", 4, case_39[0], case_39[1]);
267    test_get_turns<polygon, polygon>::apply("40", 3, case_40[0], case_40[1]);
268    test_get_turns<polygon, polygon>::apply("41", 5, case_41[0], case_41[1]);
269    test_get_turns<polygon, polygon>::apply("42", 5, case_42[0], case_42[1]);
270
271    // 43-48
272    test_get_turns<polygon, polygon>::apply("43", 4, case_43[0], case_43[1]);
273    test_get_turns<polygon, polygon>::apply("44", 4, case_44[0], case_44[1]);
274    test_get_turns<polygon, polygon>::apply("45", 4, case_45[0], case_45[1]);
275    test_get_turns<polygon, polygon>::apply("46", 4, case_46[0], case_46[1]);
276    test_get_turns<polygon, polygon>::apply("47", 5, case_47[0], case_47[1]);
277
278    // 49-54
279    test_get_turns<polygon, polygon>::apply("50", 4, case_50[0], case_50[1]);
280    test_get_turns<polygon, polygon>::apply("51", 3, case_51[0], case_51[1]);
281    test_get_turns<polygon, polygon>::apply("52", 8, case_52[0], case_52[1]);
282    // A touching point interior/ring exterior/ring can be represented in two ways:
283    test_get_turns<polygon, polygon>::apply("53a", 4, case_53[0], case_53[1]);
284    test_get_turns<polygon, polygon>::apply("53b", 4, case_53[0], case_53[2]);
285    test_get_turns<polygon, polygon>::apply("54aa", 13, case_54[0], case_54[2]);
286    test_get_turns<polygon, polygon>::apply("54ab", 13, case_54[0], case_54[3]);
287    test_get_turns<polygon, polygon>::apply("54ba", 13, case_54[1], case_54[2]);
288    test_get_turns<polygon, polygon>::apply("54bb", 13, case_54[1], case_54[3]);
289
290    test_get_turns<polygon, polygon>::apply("55", 12, case_55[0], case_55[1]);
291    test_get_turns<polygon, polygon>::apply("56", 9, case_56[0], case_56[1]);
292
293
294    // other
295    test_get_turns<polygon, polygon>::apply("many_situations", 31, case_many_situations[0], case_many_situations[1]);
296
297
298    // ticket#17
299    test_get_turns<polygon, box>::apply("ticket_17", 6, ticket_17[0], ticket_17[1]);
300
301    // GGL-mailing list
302    test_get_turns<polygon, polygon>::apply("ggl_list_20110306_javier",
303            4,
304            ggl_list_20110306_javier[0], ggl_list_20110306_javier[1]);
305
306
307    // pies
308    test_get_turns<polygon, polygon>::apply("pie_23_16_16", 3, pie_23_16_16[0], pie_23_16_16[1]);
309    test_get_turns<polygon, polygon>::apply("pie_16_4_12", 2, pie_16_4_12[0], pie_16_4_12[1]);
310    test_get_turns<polygon, polygon>::apply("pie_4_13_15", 3, pie_4_13_15[0], pie_4_13_15[1]);
311    test_get_turns<polygon, polygon>::apply("pie_16_2_15_0", 2, pie_16_2_15_0[0], pie_16_2_15_0[1]);
312    test_get_turns<polygon, polygon>::apply("pie_20_20_7_100", 3, pie_20_20_7_100[0], pie_20_20_7_100[1]);
313    test_get_turns<polygon, polygon>::apply("pie_23_23_3_2000", 5, pie_23_23_3_2000[0], pie_23_23_3_2000[1]);
314
315
316    // line-line
317    test_get_turns<linestring, linestring>::apply("lineline1", 3, line_line1[0], line_line1[1]);
318
319    // line-polygon
320    test_get_turns<linestring, polygon>::apply("line_poly1", 4, line_line1[0], case_1[1]);
321    test_get_turns<linestring, polygon>::apply("line_poly2", 4, line_line1[1], case_1[0]);
322    test_get_turns<polygon, linestring>::apply("poly_line", 4, case_1[1], line_line1[0]);
323}
324
325
326template <typename T>
327void test_ccw()
328{
329    typedef bg::model::point<T, 2, bg::cs::cartesian> P;
330    typedef bg::model::polygon<P, false, true> polygon;
331    typedef boost::tuple<int> Tuple;
332
333
334    test_get_turns<polygon, polygon>::apply("ccw_1",
335                6,
336                ccw_case_1[0], ccw_case_1[1]);
337
338    test_get_turns<polygon, polygon>::apply("ccw_9",
339                1,
340                case_9[0], case_9[1]);
341
342}
343
344template <typename T>
345void test_open()
346{
347    typedef bg::model::point<T, 2, bg::cs::cartesian> P;
348    typedef bg::model::polygon<P, true, false> polygon;
349    typedef boost::tuple<int> Tuple;
350
351    test_get_turns<polygon, polygon>::apply("open_1",
352                6,
353                open_case_1[0], open_case_1[1]);
354
355    test_get_turns<polygon, polygon>::apply("open_9",
356                1,
357                open_case_9[0], open_case_9[1]);
358}
359
360
361
362int test_main(int, char* [])
363{
364    test_all<float>();
365    test_all<double>();
366    test_ccw<double>();
367    test_open<double>();
368
369#if ! defined(_MSC_VER)
370    test_all<long double>();
371#endif
372
373#if defined(HAVE_TTMATH)
374    test_all<ttmath_big>();
375#endif
376    return 0;
377}
378
379#endif