• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Boost.Geometry (aka GGL, Generic Geometry Library)
2 // Unit Test
3 
4 // Copyright (c) 2020 Digvijay Janartha, Hamirpur, India.
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 <iostream>
11 
12 #include <geometry_test_common.hpp>
13 
14 #include <boost/core/ignore_unused.hpp>
15 #include <boost/geometry/algorithms/make.hpp>
16 #include <boost/geometry/algorithms/append.hpp>
17 #include <boost/geometry/geometries/point.hpp>
18 #include <boost/geometry/geometries/point_xy.hpp>
19 #include <boost/geometry/geometries/polygon.hpp>
20 #include <boost/geometry/geometries/concepts/polygon_concept.hpp>
21 #include <boost/geometry/geometries/adapted/c_array.hpp>
22 #include <boost/geometry/geometries/adapted/boost_tuple.hpp>
23 #include <boost/geometry/io/dsv/write.hpp>
24 
25 #include <test_common/test_point.hpp>
26 
27 BOOST_GEOMETRY_REGISTER_C_ARRAY_CS(cs::cartesian)
BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)28 BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
29 
30 #ifdef BOOST_NO_CXX11_HDR_INITIALIZER_LIST
31 #include <initializer_list>
32 #endif//BOOST_NO_CXX11_HDR_INITIALIZER_LIST
33 
34 template <typename P>
35 bg::model::polygon<P> create_polygon()
36 {
37     bg::model::polygon<P> pl1;
38     P p1;
39     P p2;
40     P p3;
41     bg::assign_values(p1, 1, 2);
42     bg::assign_values(p2, 2, 0);
43     bg::assign_values(p3, 0, 0);
44 
45     bg::append(pl1, p1);
46     bg::append(pl1, p2);
47     bg::append(pl1, p3);
48     bg::append(pl1, p1);
49     return pl1;
50 }
51 
52 template <typename PL, typename P>
check_polygon(PL & to_check,P p1,P p2,P p3)53 void check_polygon(PL& to_check, P p1, P p2, P p3)
54 {
55     PL cur;
56     bg::append(cur, p1);
57     bg::append(cur, p2);
58     bg::append(cur, p3);
59     bg::append(cur, p1);
60 
61     std::ostringstream out1, out2;
62     out1 << bg::dsv(to_check);
63     out2 << bg::dsv(cur);
64     BOOST_CHECK_EQUAL(out1.str(), out2.str());
65 }
66 
67 template <typename P>
test_default_constructor()68 void test_default_constructor()
69 {
70     bg::model::polygon<P> pl1(create_polygon<P>());
71     check_polygon(pl1, P(1, 2), P(2, 0), P(0, 0));
72 }
73 
74 template <typename P>
test_copy_constructor()75 void test_copy_constructor()
76 {
77     bg::model::polygon<P> pl1 = create_polygon<P>();
78     check_polygon(pl1, P(1, 2), P(2, 0), P(0, 0));
79 }
80 
81 template <typename P>
test_copy_assignment()82 void test_copy_assignment()
83 {
84     bg::model::polygon<P> pl1(create_polygon<P>()), pl2;
85     pl2 = pl1;
86     check_polygon(pl2, P(1, 2), P(2, 0), P(0, 0));
87 }
88 
89 template <typename P>
test_concept()90 void test_concept()
91 {
92     typedef bg::model::polygon<P> PL;
93 
94     BOOST_CONCEPT_ASSERT( (bg::concepts::ConstPolygon<PL>) );
95     BOOST_CONCEPT_ASSERT( (bg::concepts::Polygon<PL>) );
96 
97     typedef typename bg::coordinate_type<PL>::type T;
98     typedef typename bg::point_type<PL>::type PPL;
99     boost::ignore_unused<T, PPL>();
100 }
101 
102 template <typename P>
test_all()103 void test_all()
104 {
105     test_default_constructor<P>();
106     test_copy_constructor<P>();
107     test_copy_assignment<P>();
108     test_concept<P>();
109 }
110 
111 template <typename P>
test_custom_polygon(bg::model::ring<P> IL)112 void test_custom_polygon(bg::model::ring<P> IL)
113 {
114 #ifdef BOOST_NO_CXX11_HDR_INITIALIZER_LIST
115     std::initializer_list<bg::model::ring<P> > RIL = {IL};
116     bg::model::polygon<P> pl1(RIL);
117     std::ostringstream out;
118     out << bg::dsv(pl1);
119     BOOST_CHECK_EQUAL(out.str(), "(((3, 3), (3, 0), (0, 0), (0, 3), (3, 3)))");
120 #endif//BOOST_NO_CXX11_HDR_INITIALIZER_LIST
121 }
122 
123 template <typename P>
test_custom()124 void test_custom()
125 {
126     std::initializer_list<P> IL = {P(3, 3), P(3, 0), P(0, 0), P(0, 3), P(3, 3)};
127     bg::model::ring<P> r1(IL);
128     test_custom_polygon<P>(r1);
129 }
130 
131 template <typename CS>
test_cs()132 void test_cs()
133 {
134     test_all<bg::model::point<int, 2, CS> >();
135     test_all<bg::model::point<float, 2, CS> >();
136     test_all<bg::model::point<double, 2, CS> >();
137 
138     test_custom<bg::model::point<double, 2, CS> >();
139 }
140 
141 
test_main(int,char * [])142 int test_main(int, char* [])
143 {
144     test_cs<bg::cs::cartesian>();
145     test_cs<bg::cs::spherical<bg::degree> >();
146     test_cs<bg::cs::spherical_equatorial<bg::degree> >();
147     test_cs<bg::cs::geographic<bg::degree> >();
148 
149     test_custom<bg::model::d2::point_xy<double> >();
150 
151     return 0;
152 }
153