initialization.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // Boost.Geometry
  2. // Unit Test
  3. // Copyright (c) 2014-2015 Adam Wulkiewicz, Lodz, Poland.
  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
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #include <geometry_test_common.hpp>
  8. #include <boost/assign.hpp>
  9. #include <boost/range.hpp>
  10. #include <boost/geometry/geometries/geometries.hpp>
  11. #include <boost/geometry/geometries/point_xy.hpp>
  12. #include <boost/geometry/geometries/adapted/boost_tuple.hpp>
  13. #include <boost/geometry/geometries/register/point.hpp>
  14. #include <boost/geometry/algorithms/num_points.hpp>
  15. #include <boost/geometry/algorithms/num_geometries.hpp>
  16. typedef std::pair<float, float> pt_pair_t;
  17. BOOST_GEOMETRY_REGISTER_POINT_2D(pt_pair_t, float, bg::cs::cartesian, first, second)
  18. BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
  19. template <typename P>
  20. void test_default()
  21. {
  22. typedef bg::model::multi_point<P> mpt;
  23. typedef bg::model::linestring<P> ls;
  24. typedef bg::model::multi_linestring<ls> mls;
  25. typedef bg::model::ring<P> ring;
  26. typedef bg::model::polygon<P> poly;
  27. typedef bg::model::multi_polygon<poly> mpoly;
  28. // multi_point()
  29. mpt mptd;
  30. BOOST_CHECK(bg::num_points(mptd) == 0);
  31. // linestring()
  32. ls lsd;
  33. BOOST_CHECK(bg::num_points(lsd) == 0);
  34. // multi_linestring()
  35. mls mlsd;
  36. BOOST_CHECK(bg::num_points(mlsd) == 0);
  37. // ring()
  38. ring rd;
  39. BOOST_CHECK(bg::num_points(rd) == 0);
  40. // polygon()
  41. poly pd;
  42. BOOST_CHECK(bg::num_points(pd) == 0);
  43. // multi_polygon()
  44. mpoly mpd;
  45. BOOST_CHECK(bg::num_points(mpd) == 0);
  46. }
  47. template <typename P>
  48. void test_boost_assign_2d()
  49. {
  50. typedef bg::model::multi_point<P> mpt;
  51. typedef bg::model::linestring<P> ls;
  52. typedef bg::model::ring<P> ring;
  53. // using Boost.Assign
  54. mpt mpt2 = boost::assign::list_of(P(0, 0))(P(1, 0));
  55. BOOST_CHECK(bg::num_points(mpt2) == 2);
  56. mpt2 = boost::assign::list_of(P(0, 0))(P(1, 0));
  57. BOOST_CHECK(bg::num_points(mpt2) == 2);
  58. // using Boost.Assign
  59. ls ls2 = boost::assign::list_of(P(0, 0))(P(1, 0))(P(1, 1));
  60. BOOST_CHECK(bg::num_points(ls2) == 3);
  61. ls2 = boost::assign::list_of(P(0, 0))(P(1, 0))(P(1, 1));
  62. BOOST_CHECK(bg::num_points(ls2) == 3);
  63. // using Boost.Assign
  64. ring r2 = boost::assign::list_of(P(0, 0))(P(0, 1))(P(1, 1))(P(1, 0))(P(0, 0));
  65. BOOST_CHECK(bg::num_points(r2) == 5);
  66. r2 = boost::assign::list_of(P(0, 0))(P(0, 1))(P(1, 1))(P(1, 0))(P(0, 0));
  67. BOOST_CHECK(bg::num_points(r2) == 5);
  68. }
  69. void test_boost_assign_pair_2d()
  70. {
  71. typedef std::pair<float, float> pt;
  72. test_boost_assign_2d<pt>();
  73. typedef bg::model::multi_point<pt> mpt;
  74. // using Boost.Assign
  75. mpt mpt2 = boost::assign::pair_list_of(0, 0)(1, 0);
  76. BOOST_CHECK(bg::num_points(mpt2) == 2);
  77. mpt2 = boost::assign::pair_list_of(0, 0)(1, 0);
  78. BOOST_CHECK(bg::num_points(mpt2) == 2);
  79. }
  80. void test_boost_assign_tuple_2d()
  81. {
  82. typedef boost::tuple<float, float> pt;
  83. test_boost_assign_2d<pt>();
  84. typedef bg::model::multi_point<pt> mpt;
  85. // using Boost.Assign
  86. mpt mpt2 = boost::assign::tuple_list_of(0, 0)(1, 0);
  87. BOOST_CHECK(bg::num_points(mpt2) == 2);
  88. mpt2 = boost::assign::tuple_list_of(0, 0)(1, 0);
  89. BOOST_CHECK(bg::num_points(mpt2) == 2);
  90. }
  91. template <typename P>
  92. void test_initializer_list_2d()
  93. {
  94. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) && !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX)
  95. typedef bg::model::multi_point<P> mpt;
  96. typedef bg::model::linestring<P> ls;
  97. typedef bg::model::multi_linestring<ls> mls;
  98. typedef bg::model::ring<P> ring;
  99. typedef bg::model::polygon<P> poly;
  100. typedef bg::model::multi_polygon<poly> mpoly;
  101. // multi_point(initializer_list<Point>)
  102. mpt mpt1 = {{0, 0}, {1, 0}, {2, 0}};
  103. BOOST_CHECK(bg::num_geometries(mpt1) == 3);
  104. BOOST_CHECK(bg::num_points(mpt1) == 3);
  105. // multi_point::operator=(initializer_list<Point>)
  106. mpt1 = {{0, 0}, {1, 0}, {2, 0}, {3, 0}};
  107. BOOST_CHECK(bg::num_points(mpt1) == 4);
  108. // linestring(initializer_list<Point>)
  109. ls ls1 = {{0, 0}, {1, 0}, {2, 0}};
  110. BOOST_CHECK(bg::num_geometries(ls1) == 1);
  111. BOOST_CHECK(bg::num_points(ls1) == 3);
  112. // linestring::operator=(initializer_list<Point>)
  113. ls1 = {{0, 0}, {1, 0}, {2, 0}, {3, 0}};
  114. BOOST_CHECK(bg::num_points(ls1) == 4);
  115. // multi_linestring(initializer_list<Linestring>)
  116. mls mls1 = {{{0, 0}, {1, 0}, {2, 0}}, {{3, 0}, {4, 0}}};
  117. BOOST_CHECK(bg::num_geometries(mls1) == 2);
  118. BOOST_CHECK(bg::num_points(mls1) == 5);
  119. // multi_linestring::operator=(initializer_list<Linestring>)
  120. mls1 = {{{0, 0}, {1, 0}, {2, 0}}, {{3, 0}, {4, 0}, {5, 0}}};
  121. BOOST_CHECK(bg::num_points(mls1) == 6);
  122. // ring(initializer_list<Point>)
  123. ring r1 = {{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}};
  124. BOOST_CHECK(bg::num_geometries(r1) == 1);
  125. BOOST_CHECK(bg::num_points(r1) == 5);
  126. // ring::operator=(initializer_list<Point>)
  127. r1 = {{0, 0}, {0, 1}, {1, 2}, {2, 1}, {1, 0}, {0, 0}};
  128. BOOST_CHECK(bg::num_points(r1) == 6);
  129. // polygon(initializer_list<ring_type>)
  130. poly p1 = {{{0, 0}, {0, 9}, {9, 9}, {9, 0}, {0, 0}}, {{1, 1}, {2, 1}, {2, 2}, {1, 2}, {1, 1}}};
  131. BOOST_CHECK(bg::num_geometries(p1) == 1);
  132. BOOST_CHECK(bg::num_points(p1) == 10);
  133. BOOST_CHECK(boost::size(bg::interior_rings(p1)) == 1);
  134. // polygon::operator=(initializer_list<ring_type>)
  135. p1 = {{{0, 0}, {0, 8}, {8, 9}, {9, 8}, {8, 0}, {0, 0}}, {{1, 1}, {2, 1}, {2, 2}, {1, 2}, {1, 1}}};
  136. BOOST_CHECK(bg::num_points(p1) == 11);
  137. BOOST_CHECK(boost::size(bg::interior_rings(p1)) == 1);
  138. p1 = {{{0, 0}, {0, 9}, {9, 9}, {9, 0}, {0, 0}}};
  139. BOOST_CHECK(bg::num_points(p1) == 5);
  140. BOOST_CHECK(boost::size(bg::interior_rings(p1)) == 0);
  141. // polygon(initializer_list<ring_type>)
  142. poly p2 = {{{0, 0}, {0, 9}, {9, 9}, {9, 0}, {0, 0}}};
  143. BOOST_CHECK(bg::num_geometries(p2) == 1);
  144. BOOST_CHECK(bg::num_points(p2) == 5);
  145. BOOST_CHECK(boost::size(bg::interior_rings(p1)) == 0);
  146. // polygon::operator=(initializer_list<ring_type>)
  147. p2 = {{{0, 0}, {0, 8}, {8, 9}, {9, 8}, {8, 0}, {0, 0}}};
  148. BOOST_CHECK(bg::num_points(p2) == 6);
  149. // multi_polygon(initializer_list<Polygon>)
  150. mpoly mp1 = {{{{0, 0}, {0, 1}, {1, 1}, {1, 0}, {0, 0}}}, {{{2, 2}, {2, 3}, {3, 3}, {3, 2}, {2, 2}}}};
  151. BOOST_CHECK(bg::num_geometries(mp1) == 2);
  152. BOOST_CHECK(bg::num_points(mp1) == 10);
  153. // multi_polygon::operator=(initializer_list<Polygon>)
  154. mp1 = {{{{0, 0}, {0, 1}, {1, 2}, {2, 1}, {1, 0}, {0, 0}}}, {{{2, 2}, {2, 3}, {3, 3}, {3, 2}, {2, 2}}}};
  155. BOOST_CHECK(bg::num_points(mp1) == 11);
  156. #endif
  157. }
  158. template <typename P>
  159. void test_all_2d()
  160. {
  161. test_default<P>();
  162. test_boost_assign_2d<P>();
  163. test_initializer_list_2d<P>();
  164. }
  165. template <typename T>
  166. struct test_point
  167. {
  168. test_point(T = T(), T = T()) {}
  169. };
  170. template <typename T>
  171. struct test_range
  172. {
  173. test_range() {}
  174. template <typename It>
  175. test_range(It, It) {}
  176. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST)
  177. test_range(std::initializer_list<T>) {}
  178. //test_range & operator=(std::initializer_list<T>) { return *this; }
  179. #endif
  180. };
  181. void test_sanity_check()
  182. {
  183. typedef test_point<float> P;
  184. typedef test_range<P> R;
  185. typedef std::vector<P> V;
  186. #if !defined(BOOST_NO_CXX11_HDR_INITIALIZER_LIST) && !defined(BOOST_NO_CXX11_UNIFIED_INITIALIZATION_SYNTAX)
  187. {
  188. R r = {{1, 1},{2, 2},{3, 3}};
  189. r = {{1, 1},{2, 2},{3, 3}};
  190. V v = {{1, 1},{2, 2},{3, 3}};
  191. v = {{1, 1},{2, 2},{3, 3}};
  192. }
  193. #endif
  194. {
  195. R r = boost::assign::list_of(P(1, 1))(P(2, 2))(P(3, 3));
  196. r = boost::assign::list_of(P(1, 1))(P(2, 2))(P(3, 3));
  197. V v = boost::assign::list_of(P(1, 1))(P(2, 2))(P(3, 3));
  198. //v = boost::assign::list_of(P(1, 1))(P(2, 2))(P(3, 3));
  199. v.empty();
  200. }
  201. }
  202. int test_main(int, char* [])
  203. {
  204. test_all_2d< bg::model::point<float, 2, bg::cs::cartesian> >();
  205. test_all_2d< bg::model::d2::point_xy<float> >();
  206. test_boost_assign_pair_2d();
  207. test_boost_assign_tuple_2d();
  208. test_sanity_check();
  209. return 0;
  210. }