convert.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Unit Test
  3. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  5. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  6. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  7. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  8. // Use, modification and distribution is subject to the Boost Software License,
  9. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. #include <algorithms/test_convert.hpp>
  12. template <typename Point1, typename Point2>
  13. void test_mixed_point_types()
  14. {
  15. // Point
  16. test_mixed_identical_result<Point1, Point2>("POINT(1 2)");
  17. // Box
  18. test_mixed_identical_result
  19. <
  20. bg::model::box<Point1>,
  21. bg::model::box<Point2>
  22. >
  23. ("POLYGON((1 2,1 4,3 4,3 2,1 2))");
  24. test_mixed_identical_result
  25. <
  26. bg::model::segment<Point1>,
  27. bg::model::segment<Point2>
  28. >
  29. ("LINESTRING(1 1,2 2)");
  30. // Linestring
  31. test_mixed_identical_result
  32. <
  33. bg::model::linestring<Point1>,
  34. bg::model::linestring<Point2>
  35. >
  36. ("LINESTRING(1 1,2 2)");
  37. // Ring
  38. test_mixed_identical_result
  39. <
  40. bg::model::ring<Point1>,
  41. bg::model::ring<Point2>
  42. >
  43. ("POLYGON((1 1,2 2,3 0,1 1))");
  44. test_mixed_reversible_result
  45. <
  46. bg::model::ring<Point1, true>,
  47. bg::model::ring<Point2, false>
  48. >
  49. (
  50. "POLYGON((1 1,2 2,3 0,1 1))",
  51. "POLYGON((1 1,3 0,2 2,1 1))"
  52. );
  53. test_mixed
  54. <
  55. bg::model::ring<Point1, true, true>,
  56. bg::model::ring<Point2, true, false>
  57. >
  58. (
  59. "POLYGON((1 1,2 2,3 0,1 1))",
  60. "POLYGON((1 1,2 2,3 0))",
  61. 3
  62. );
  63. test_mixed
  64. <
  65. bg::model::ring<Point1, true, false>,
  66. bg::model::ring<Point2, true, true>
  67. >
  68. (
  69. "POLYGON((1 1,2 2,3 0))",
  70. "POLYGON((1 1,2 2,3 0,1 1))",
  71. 4
  72. );
  73. // Polygon
  74. test_mixed_reversible_result
  75. <
  76. bg::model::polygon<Point1, true>,
  77. bg::model::polygon<Point2, false>
  78. >
  79. (
  80. "POLYGON((0 0,0 5,5 5,5 0,0 0),(1 1,3 2,2 4,1 1))",
  81. "POLYGON((0 0,5 0,5 5,0 5,0 0),(1 1,2 4,3 2,1 1))"
  82. );
  83. test_mixed
  84. <
  85. bg::model::polygon<Point1>,
  86. bg::model::polygon<Point2, false, false>
  87. >
  88. (
  89. "POLYGON((0 0,0 5,5 5,5 0,0 0),(1 1,3 2,2 4,1 1))",
  90. "POLYGON((0 0,5 0,5 5,0 5,0 0),(1 1,2 4,3 2,1 1))",
  91. 7 // WKT is closed, polygon is open
  92. );
  93. // (polygon uses ring, so other tests omitted here)
  94. // Combinations:
  95. // ring <-> polygon
  96. test_mixed_identical_result
  97. <
  98. bg::model::polygon<Point1>,
  99. bg::model::ring<Point2>
  100. >
  101. ("POLYGON((1 1,2 2,3 0,1 1))");
  102. test_mixed_reversible_result
  103. <
  104. bg::model::polygon<Point1, true>,
  105. bg::model::ring<Point2, false>
  106. >
  107. (
  108. "POLYGON((1 1,2 2,3 0,1 1))",
  109. "POLYGON((1 1,3 0,2 2,1 1))"
  110. );
  111. // Any hole will be omitted going from polygon to ring
  112. test_mixed
  113. <
  114. bg::model::polygon<Point1>,
  115. bg::model::ring<Point2>
  116. >
  117. (
  118. "POLYGON((0 0,0 5,5 5,5 0,0 0),(1 1,3 2,2 4,1 1))",
  119. "POLYGON((0 0,0 5,5 5,5 0,0 0))",
  120. 5
  121. );
  122. // point -> box
  123. test_mixed
  124. <
  125. Point1,
  126. bg::model::box<Point2>
  127. >
  128. (
  129. "POINT(0 0)",
  130. "POLYGON((0 0,0 0,0 0,0 0,0 0))",
  131. 4
  132. );
  133. // segment -> line
  134. test_mixed
  135. <
  136. bg::model::segment<Point1>,
  137. bg::model::linestring<Point2>
  138. >
  139. (
  140. "LINESTRING(0 0,1 1)",
  141. "LINESTRING(0 0,1 1)",
  142. 2
  143. );
  144. // box -> ring ( <- is NYI)
  145. test_mixed
  146. <
  147. bg::model::box<Point1>,
  148. bg::model::ring<Point2>
  149. >
  150. (
  151. "BOX(0 0,2 2)",
  152. "POLYGON((0 0,0 2,2 2,2 0,0 0))",
  153. 5
  154. );
  155. test_mixed
  156. <
  157. bg::model::box<Point1>,
  158. bg::model::ring<Point2, false>
  159. >
  160. (
  161. "BOX(0 0,2 2)",
  162. "POLYGON((0 0,2 0,2 2,0 2,0 0))",
  163. 5
  164. );
  165. test_mixed
  166. <
  167. bg::model::box<Point1>,
  168. bg::model::ring<Point2, true, false>
  169. >
  170. (
  171. "BOX(0 0,2 2)",
  172. "POLYGON((0 0,0 2,2 2,2 0))",
  173. 4
  174. );
  175. test_mixed
  176. <
  177. bg::model::box<Point1>,
  178. bg::model::ring<Point2, false, false>
  179. >
  180. (
  181. "BOX(0 0,2 2)",
  182. "POLYGON((0 0,2 0,2 2,0 2))",
  183. 4
  184. );
  185. // box -> polygon ( <- is NYI)
  186. test_mixed
  187. <
  188. bg::model::box<Point1>,
  189. bg::model::polygon<Point2>
  190. >
  191. (
  192. "BOX(0 0,2 2)",
  193. "POLYGON((0 0,0 2,2 2,2 0,0 0))",
  194. 5
  195. );
  196. test_mixed
  197. <
  198. bg::model::box<Point1>,
  199. bg::model::polygon<Point2, false>
  200. >
  201. (
  202. "BOX(0 0,2 2)",
  203. "POLYGON((0 0,2 0,2 2,0 2,0 0))",
  204. 5
  205. );
  206. test_mixed
  207. <
  208. bg::model::box<Point1>,
  209. bg::model::polygon<Point2, true, false>
  210. >
  211. (
  212. "BOX(0 0,2 2)",
  213. "POLYGON((0 0,0 2,2 2,2 0,0 0))",
  214. 4 // WKT is closed, polygon is open
  215. );
  216. test_mixed
  217. <
  218. bg::model::box<Point1>,
  219. bg::model::polygon<Point2, false, false>
  220. >
  221. (
  222. "BOX(0 0,2 2)",
  223. "POLYGON((0 0,2 0,2 2,0 2,0 0))",
  224. 4 // WKT is closed, polygon is open
  225. );
  226. }
  227. template <typename Point1, typename Point2>
  228. void test_mixed_point_types_3d()
  229. {
  230. // Point
  231. test_mixed_identical_result<Point1, Point2>("POINT(1 2 3)");
  232. test_mixed_identical_result
  233. <
  234. bg::model::segment<Point1>,
  235. bg::model::segment<Point2>
  236. >
  237. ("LINESTRING(1 2 3,4 5 6)");
  238. // Linestring
  239. test_mixed_identical_result
  240. <
  241. bg::model::linestring<Point1>,
  242. bg::model::linestring<Point2>
  243. >
  244. ("LINESTRING(1 2 3,4 5 6,7 8 9)");
  245. // segment -> line
  246. test_mixed
  247. <
  248. bg::model::segment<Point1>,
  249. bg::model::linestring<Point2>
  250. >
  251. (
  252. "LINESTRING(1 2 3,4 5 6)",
  253. "LINESTRING(1 2 3,4 5 6)",
  254. 2
  255. );
  256. }
  257. template <typename Point1, typename Point2>
  258. void test_mixed_types()
  259. {
  260. test_mixed_point_types<Point1, Point2>();
  261. test_mixed_point_types<Point2, Point1>();
  262. }
  263. template <typename Point1, typename Point2>
  264. void test_mixed_types_3d()
  265. {
  266. test_mixed_point_types_3d<Point1, Point2>();
  267. test_mixed_point_types_3d<Point2, Point1>();
  268. }
  269. void test_array()
  270. {
  271. int a[2] = {1, 2};
  272. int b[2];
  273. bg::convert(a, b);
  274. BOOST_CHECK_EQUAL(b[0], 1);
  275. BOOST_CHECK_EQUAL(b[1], 2);
  276. }
  277. int test_main(int, char* [])
  278. {
  279. test_mixed_types
  280. <
  281. bg::model::point<int, 2, bg::cs::cartesian>,
  282. bg::model::point<double, 2, bg::cs::cartesian>
  283. >();
  284. test_mixed_types
  285. <
  286. boost::tuple<float, float>,
  287. bg::model::point<float, 2, bg::cs::cartesian>
  288. >();
  289. test_mixed_types_3d
  290. <
  291. boost::tuple<double, double, double>,
  292. bg::model::point<double, 3, bg::cs::cartesian>
  293. >();
  294. test_array();
  295. return 0;
  296. }