#### /Src/Dependencies/Boost/libs/geometry/test/algorithms/equals.cpp

C++ | 162 lines | 92 code | 40 blank | 30 comment | 8 complexity | f7e9b15ef2206cca748d1cbce14410ce MD5 | raw file
```  1// Boost.Geometry (aka GGL, Generic Geometry Library)
2//
3// Copyright (c) 2007-2011 Barend Gehrels, Amsterdam, the Netherlands.
4// Use, modification and distribution is subject to the Boost Software License,
5// Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7
8#include <algorithms/test_equals.hpp>
9
10#include <boost/geometry/geometries/geometries.hpp>
11#include <boost/geometry/geometries/point_xy.hpp>
12
13
14
15
16template <typename P>
17void test_all()
18{
19    typedef bg::model::box<P> box;
20    typedef bg::model::ring<P> ring;
21    typedef bg::model::polygon<P> polygon;
22    typedef bg::model::linestring<P> linestring;
23
24    std::string case_p1 = "POLYGON((0 0,0 2,2 2,0 0))";
25
26    test_geometry<P, P>("p1", "POINT(1 1)", "POINT(1 1)", true);
27    test_geometry<P, P>("p2", "POINT(1 1)", "POINT(1 2)", false);
28    test_geometry<box, box>("b1", "BOX(1 1,2 2)", "BOX(1 2,2 2)", false);
29    test_geometry<box, box>("b1", "BOX(1 2,3 4)", "BOX(1 2,3 4)", true);
30
31    // Completely equal
32    test_geometry<ring, ring>("poly_eq", case_p1, case_p1, true);
33
34    // Shifted
35    test_geometry<ring, ring>("poly_sh", "POLYGON((2 2,0 0,0 2,2 2))", case_p1, true);
36    test_geometry<polygon, polygon>("poly_sh2", case_p1, "POLYGON((0 2,2 2,0 0,0 2))", true);
37
38    // Extra coordinate
39    test_geometry<ring, ring>("poly_extra", case_p1, "POLYGON((0 0,0 2,2 2,1 1,0 0))", true);
40
41    // Shifted + extra (redundant) coordinate
42    test_geometry<ring, ring>("poly_shifted_extra1", "POLYGON((2 2,1 1,0 0,0 2,2 2))", case_p1, true);
43
44    // Shifted + extra (redundant) coordinate being first/last point
45    test_geometry<ring, ring>("poly_shifted_extra2", "POLYGON((1 1,0 0,0 2,2 2,1 1))", case_p1, true);
46
47    // Degenerate (duplicate) points
48    test_geometry<ring, ring>("poly_degenerate", "POLYGON((0 0,0 2,2 2,2 2,0 0))", "POLYGON((0 0,0 2,0 2,2 2,0 0))", true);
49
50    // Two different bends, same area, unequal
51    test_geometry<ring, ring>("poly_bends",
52        "POLYGON((4 0,5 3,8 4,7 7,4 8,0 4,4 0))",
53        "POLYGON((4 0,7 1,8 4,5 5,4 8,0 4,4 0))", false);
54
55    // Unequal (but same area)
56    test_geometry<ring, ring>("poly_uneq", case_p1, "POLYGON((1 1,1 3,3 3,1 1))", false);
57
58    // One having hole
59    test_geometry<polygon, polygon>("poly_hole", "POLYGON((0 0,0 4,4 4,0 0))", "POLYGON((0 0,0 4,4 4,0 0),(1 1,2 1,2 2,1 2,1 1))", false);
60
61    // Both having holes
62    test_geometry<polygon, polygon>("poly_holes",
63            "POLYGON((0 0,0 4,4 4,0 0),(1 1,2 1,2 2,1 2,1 1))",
64            "POLYGON((0 0,0 4,4 4,0 0),(1 1,2 1,2 2,1 2,1 1))", true);
65
66    // Both having holes, outer equal, inner not equal
67    test_geometry<polygon, polygon>("poly_uneq_holes",
68            "POLYGON((0 0,0 4,4 4,0 0),(1 1,2 1,2 2,1 2,1 1))",
69            "POLYGON((0 0,0 4,4 4,0 0),(2 2,3 2,3 3,2 3,2 2))", false);
70
71    // Both having 2 holes, equal but in different order
72    test_geometry<polygon, polygon>("poly_holes_diff_order",
73            "POLYGON((0 0,0 4,4 4,0 0),(1 1,2 1,2 2,1 2,1 1),(2 2,3 2,3 3,2 3,2 2))",
74            "POLYGON((0 0,0 4,4 4,0 0),(2 2,3 2,3 3,2 3,2 2),(1 1,2 1,2 2,1 2,1 1))", true);
75
76    // Both having 3 holes, equal but in different order
77    test_geometry<polygon, polygon>("poly_holes_diff_order_3",
78            "POLYGON((0 0,0 10,10 10,0 0),(1 1,2 1,2 2,1 2,1 1),(4 1,5 1,5 2,4 2,4 1),(2 2,3 2,3 3,2 3,2 2))",
79            "POLYGON((0 0,0 10,10 10,0 0),(4 1,5 1,5 2,4 2,4 1),(2 2,3 2,3 3,2 3,2 2),(1 1,2 1,2 2,1 2,1 1))", true);
80
81    // polygon/ring vv
82    test_geometry<polygon, ring>("poly_sh2_pr", case_p1, case_p1, true);
83    test_geometry<ring, polygon>("poly_sh2_rp", case_p1, case_p1, true);
84
85    // box/ring/poly
86    test_geometry<box, ring>("boxring1", "BOX(1 1,2 2)", "POLYGON((1 1,1 2,2 2,2 1,1 1))", true);
87    test_geometry<ring, box>("boxring2", "POLYGON((1 1,1 2,2 2,2 1,1 1))", "BOX(1 1,2 2)", true);
88    test_geometry<box, polygon>("boxpoly1", "BOX(1 1,2 2)", "POLYGON((1 1,1 2,2 2,2 1,1 1))", true);
89    test_geometry<polygon, box>("boxpoly2", "POLYGON((1 1,1 2,2 2,2 1,1 1))", "BOX(1 1,2 2)", true);
90
91    test_geometry<polygon, box>("boxpoly2", "POLYGON((1 1,1 2,2 2,2 1,1 1))", "BOX(1 1,2 3)", false);
92
93    // linestring/linestring
94    // simplex
95    test_geometry<linestring, linestring>("ls1", "LINESTRING(1 1,2 2)", "LINESTRING(1 1,2 2)", true);
96
97    // REVERSE linestring
98    // Is this equal? To be decided.
99    //test_geometry<linestring, linestring>("ls1", "LINESTRING(1 1,2 2)", "LINESTRING(2 2,1 1)", true);
100
101}
102
103
104template <typename T>
105void verify()
106{
107    T dxn1, dyn1, dxn2, dyn2;
108
109    {
110        T x1 = "0", y1 = "0", x2 = "3", y2 = "3";
111        T dx = x2 - x1, dy = y2 - y1;
112        T mag = sqrt(dx * dx + dy * dy);
113        dxn1 = dx / mag;
114        dyn1 = dy / mag;
115    }
116
117    {
118        T x1 = "0", y1 = "0", x2 = "1", y2 = "1";
119        T dx = x2 - x1, dy = y2 - y1;
120        T mag = sqrt(dx * dx + dy * dy);
121        dxn2 = dx / mag;
122        dyn2 = dy / mag;
123    }
124
125    if (dxn1 == dxn2 && dyn1 == dyn2)
126    {
127        //std::cout << "vectors are equal, using ==" << std::endl;
128    }
129    if (boost::geometry::math::equals(dxn1, dxn2)
130        && boost::geometry::math::equals(dyn1, dyn2))
131    {
132        //std::cout << "vectors are equal, using bg::math::equals" << std::endl;
133    }
134
135    bool equals = boost::geometry::math::equals_with_epsilon(dxn1, dxn2)
136        && boost::geometry::math::equals_with_epsilon(dyn1, dyn2);
137
138    if (equals)
139    {
140        //std::cout << "vectors are equal, using bg::math::equals_with_epsilon" << std::endl;
141    }
142
143    BOOST_CHECK_EQUAL(equals, true);
144}
145
146
147int test_main( int , char* [] )
148{
149    //verify<double>();
150#if defined(HAVE_TTMATH)
151    verify<ttmath_big>();
152#endif
153
154    test_all<bg::model::d2::point_xy<int> >();
155    test_all<bg::model::d2::point_xy<double> >();
156
157#if defined(HAVE_TTMATH)
158    test_all<bg::model::d2::point_xy<ttmath_big> >();
159#endif
160
161    return 0;
162}
```