implementation.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2015, Oracle and/or its affiliates.
  3. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  4. // Licensed under the Boost Software License version 1.0.
  5. // http://www.boost.org/users/license.html
  6. #ifndef BOOST_GEOMETRY_VIEWS_DETAIL_BOUNDARY_VIEW_IMPLEMENTATION_HPP
  7. #define BOOST_GEOMETRY_VIEWS_DETAIL_BOUNDARY_VIEW_IMPLEMENTATION_HPP
  8. #include <cstddef>
  9. #include <algorithm>
  10. #include <iterator>
  11. #include <memory>
  12. #include <new>
  13. #include <utility>
  14. #include <vector>
  15. #include <boost/core/addressof.hpp>
  16. #include <boost/iterator/iterator_facade.hpp>
  17. #include <boost/iterator/iterator_categories.hpp>
  18. #include <boost/mpl/assert.hpp>
  19. #include <boost/mpl/if.hpp>
  20. #include <boost/range.hpp>
  21. #include <boost/type_traits/is_const.hpp>
  22. #include <boost/type_traits/is_convertible.hpp>
  23. #include <boost/type_traits/remove_reference.hpp>
  24. #include <boost/geometry/core/assert.hpp>
  25. #include <boost/geometry/core/closure.hpp>
  26. #include <boost/geometry/core/exterior_ring.hpp>
  27. #include <boost/geometry/core/interior_rings.hpp>
  28. #include <boost/geometry/core/ring_type.hpp>
  29. #include <boost/geometry/core/tags.hpp>
  30. #include <boost/geometry/iterators/flatten_iterator.hpp>
  31. #include <boost/geometry/util/range.hpp>
  32. #include <boost/geometry/views/closeable_view.hpp>
  33. #include <boost/geometry/algorithms/num_interior_rings.hpp>
  34. namespace boost { namespace geometry
  35. {
  36. #ifndef DOXYGEN_NO_DETAIL
  37. namespace detail { namespace boundary_views
  38. {
  39. template
  40. <
  41. typename Polygon,
  42. typename Value = typename ring_type<Polygon>::type,
  43. typename Reference = typename ring_return_type<Polygon>::type,
  44. typename Difference = typename boost::range_difference
  45. <
  46. typename boost::remove_reference
  47. <
  48. typename interior_return_type<Polygon>::type
  49. >::type
  50. >::type
  51. >
  52. class polygon_rings_iterator
  53. : public boost::iterator_facade
  54. <
  55. polygon_rings_iterator<Polygon, Value, Reference, Difference>,
  56. Value,
  57. boost::random_access_traversal_tag,
  58. Reference,
  59. Difference
  60. >
  61. {
  62. typedef typename boost::range_size
  63. <
  64. typename boost::remove_reference
  65. <
  66. typename interior_return_type<Polygon>::type
  67. >::type
  68. >::type size_type;
  69. public:
  70. // default constructor
  71. polygon_rings_iterator()
  72. : m_polygon(NULL)
  73. , m_index(0)
  74. {}
  75. // for begin
  76. polygon_rings_iterator(Polygon& polygon)
  77. : m_polygon(boost::addressof(polygon))
  78. , m_index(0)
  79. {}
  80. // for end
  81. polygon_rings_iterator(Polygon& polygon, bool)
  82. : m_polygon(boost::addressof(polygon))
  83. , m_index(static_cast<size_type>(num_rings(polygon)))
  84. {}
  85. template
  86. <
  87. typename OtherPolygon,
  88. typename OtherValue,
  89. typename OtherReference,
  90. typename OtherDifference
  91. >
  92. polygon_rings_iterator(polygon_rings_iterator
  93. <
  94. OtherPolygon,
  95. OtherValue,
  96. OtherReference,
  97. OtherDifference
  98. > const& other)
  99. : m_polygon(other.m_polygon)
  100. , m_index(other.m_index)
  101. {
  102. static const bool is_convertible
  103. = boost::is_convertible<OtherPolygon, Polygon>::value;
  104. BOOST_MPL_ASSERT_MSG((is_convertible),
  105. NOT_CONVERTIBLE,
  106. (types<OtherPolygon>));
  107. }
  108. private:
  109. friend class boost::iterator_core_access;
  110. template
  111. <
  112. typename OtherPolygon,
  113. typename OtherValue,
  114. typename OtherReference,
  115. typename OtherDifference
  116. >
  117. friend class polygon_rings_iterator;
  118. static inline std::size_t num_rings(Polygon const& polygon)
  119. {
  120. return geometry::num_interior_rings(polygon) + 1;
  121. }
  122. inline Reference dereference() const
  123. {
  124. if (m_index == 0)
  125. {
  126. return exterior_ring(*m_polygon);
  127. }
  128. return range::at(interior_rings(*m_polygon), m_index - 1);
  129. }
  130. template
  131. <
  132. typename OtherPolygon,
  133. typename OtherValue,
  134. typename OtherReference,
  135. typename OtherDifference
  136. >
  137. inline bool equal(polygon_rings_iterator
  138. <
  139. OtherPolygon,
  140. OtherValue,
  141. OtherReference,
  142. OtherDifference
  143. > const& other) const
  144. {
  145. BOOST_GEOMETRY_ASSERT(m_polygon == other.m_polygon);
  146. return m_index == other.m_index;
  147. }
  148. inline void increment()
  149. {
  150. ++m_index;
  151. }
  152. inline void decrement()
  153. {
  154. --m_index;
  155. }
  156. template
  157. <
  158. typename OtherPolygon,
  159. typename OtherValue,
  160. typename OtherReference,
  161. typename OtherDifference
  162. >
  163. inline Difference distance_to(polygon_rings_iterator
  164. <
  165. OtherPolygon,
  166. OtherValue,
  167. OtherReference,
  168. OtherDifference
  169. > const& other) const
  170. {
  171. return static_cast<Difference>(other.m_index)
  172. - static_cast<Difference>(m_index);
  173. }
  174. inline void advance(Difference n)
  175. {
  176. m_index += n;
  177. }
  178. private:
  179. Polygon* m_polygon;
  180. size_type m_index;
  181. };
  182. template <typename Ring>
  183. class ring_boundary : closeable_view<Ring, closure<Ring>::value>::type
  184. {
  185. private:
  186. typedef typename closeable_view<Ring, closure<Ring>::value>::type base_type;
  187. public:
  188. typedef typename base_type::iterator iterator;
  189. typedef typename base_type::const_iterator const_iterator;
  190. typedef linestring_tag tag_type;
  191. explicit ring_boundary(Ring& ring)
  192. : base_type(ring) {}
  193. iterator begin() { return base_type::begin(); }
  194. iterator end() { return base_type::end(); }
  195. const_iterator begin() const { return base_type::begin(); }
  196. const_iterator end() const { return base_type::end(); }
  197. };
  198. template <typename Geometry, typename Tag = typename tag<Geometry>::type>
  199. struct num_rings
  200. {};
  201. template <typename Polygon>
  202. struct num_rings<Polygon, polygon_tag>
  203. {
  204. static inline std::size_t apply(Polygon const& polygon)
  205. {
  206. return geometry::num_interior_rings(polygon) + 1;
  207. }
  208. };
  209. template <typename MultiPolygon>
  210. struct num_rings<MultiPolygon, multi_polygon_tag>
  211. {
  212. static inline std::size_t apply(MultiPolygon const& multipolygon)
  213. {
  214. return geometry::num_interior_rings(multipolygon)
  215. + static_cast<std::size_t>(boost::size(multipolygon));
  216. }
  217. };
  218. template <typename Geometry, typename Tag = typename tag<Geometry>::type>
  219. struct views_container_initializer
  220. {};
  221. template <typename Polygon>
  222. struct views_container_initializer<Polygon, polygon_tag>
  223. {
  224. template <typename BoundaryView>
  225. static inline void apply(Polygon const& polygon, BoundaryView* views)
  226. {
  227. typedef polygon_rings_iterator<Polygon> rings_iterator_type;
  228. std::uninitialized_copy(rings_iterator_type(polygon),
  229. rings_iterator_type(polygon, true),
  230. views);
  231. }
  232. };
  233. template <typename MultiPolygon>
  234. class views_container_initializer<MultiPolygon, multi_polygon_tag>
  235. {
  236. typedef typename boost::mpl::if_
  237. <
  238. boost::is_const<MultiPolygon>,
  239. typename boost::range_value<MultiPolygon>::type const,
  240. typename boost::range_value<MultiPolygon>::type
  241. >::type polygon_type;
  242. typedef polygon_rings_iterator<polygon_type> inner_iterator_type;
  243. struct polygon_rings_begin
  244. {
  245. static inline inner_iterator_type apply(polygon_type& polygon)
  246. {
  247. return inner_iterator_type(polygon);
  248. }
  249. };
  250. struct polygon_rings_end
  251. {
  252. static inline inner_iterator_type apply(polygon_type& polygon)
  253. {
  254. return inner_iterator_type(polygon, true);
  255. }
  256. };
  257. typedef flatten_iterator
  258. <
  259. typename boost::range_iterator<MultiPolygon>::type,
  260. inner_iterator_type,
  261. typename std::iterator_traits<inner_iterator_type>::value_type,
  262. polygon_rings_begin,
  263. polygon_rings_end,
  264. typename std::iterator_traits<inner_iterator_type>::reference
  265. > rings_iterator_type;
  266. public:
  267. template <typename BoundaryView>
  268. static inline void apply(MultiPolygon const& multipolygon,
  269. BoundaryView* views)
  270. {
  271. rings_iterator_type first(boost::begin(multipolygon),
  272. boost::end(multipolygon));
  273. rings_iterator_type last(boost::end(multipolygon));
  274. std::uninitialized_copy(first, last, views);
  275. }
  276. };
  277. template <typename Areal>
  278. class areal_boundary
  279. {
  280. typedef boundary_view<typename ring_type<Areal>::type> boundary_view_type;
  281. typedef views_container_initializer<Areal> exception_safe_initializer;
  282. template <typename T>
  283. struct automatic_deallocator
  284. {
  285. automatic_deallocator(T* ptr) : m_ptr(ptr) {}
  286. ~automatic_deallocator()
  287. {
  288. operator delete(m_ptr);
  289. }
  290. inline void release() { m_ptr = NULL; }
  291. T* m_ptr;
  292. };
  293. inline void initialize_views(Areal const& areal)
  294. {
  295. // initialize number of rings
  296. std::size_t n_rings = num_rings<Areal>::apply(areal);
  297. if (n_rings == 0)
  298. {
  299. return;
  300. }
  301. // allocate dynamic memory
  302. boundary_view_type* views_ptr = static_cast
  303. <
  304. boundary_view_type*
  305. >(operator new(sizeof(boundary_view_type) * n_rings));
  306. // initialize; if exceptions are thrown by constructors
  307. // they are handled automatically by automatic_deallocator
  308. automatic_deallocator<boundary_view_type> deallocator(views_ptr);
  309. exception_safe_initializer::apply(areal, views_ptr);
  310. deallocator.release();
  311. // now initialize member variables safely
  312. m_views = views_ptr;
  313. m_num_rings = n_rings;
  314. }
  315. // disallow copies and/or assignments
  316. areal_boundary(areal_boundary const&);
  317. areal_boundary& operator=(areal_boundary const&);
  318. public:
  319. typedef boundary_view_type* iterator;
  320. typedef boundary_view_type const* const_iterator;
  321. typedef multi_linestring_tag tag_type;
  322. explicit areal_boundary(Areal& areal)
  323. : m_views(NULL)
  324. , m_num_rings(0)
  325. {
  326. initialize_views(areal);
  327. }
  328. ~areal_boundary()
  329. {
  330. boundary_view_type* last = m_views + m_num_rings;
  331. for (boundary_view_type* it = m_views; it != last; ++it)
  332. {
  333. it->~boundary_view_type();
  334. }
  335. operator delete(m_views);
  336. }
  337. inline iterator begin() { return m_views; }
  338. inline iterator end() { return m_views + m_num_rings; }
  339. inline const_iterator begin() const { return m_views; }
  340. inline const_iterator end() const { return m_views + m_num_rings; }
  341. private:
  342. boundary_view_type* m_views;
  343. std::size_t m_num_rings;
  344. };
  345. }} // namespace detail::boundary_view
  346. #endif // DOXYGEN_NO_DETAIL
  347. #ifndef DOXYGEN_NO_DISPATCH
  348. namespace detail_dispatch
  349. {
  350. template <typename Ring>
  351. struct boundary_view<Ring, ring_tag>
  352. : detail::boundary_views::ring_boundary<Ring>
  353. {
  354. explicit boundary_view(Ring& ring)
  355. : detail::boundary_views::ring_boundary<Ring>(ring)
  356. {}
  357. };
  358. template <typename Polygon>
  359. struct boundary_view<Polygon, polygon_tag>
  360. : detail::boundary_views::areal_boundary<Polygon>
  361. {
  362. explicit boundary_view(Polygon& polygon)
  363. : detail::boundary_views::areal_boundary<Polygon>(polygon)
  364. {}
  365. };
  366. template <typename MultiPolygon>
  367. struct boundary_view<MultiPolygon, multi_polygon_tag>
  368. : detail::boundary_views::areal_boundary<MultiPolygon>
  369. {
  370. explicit boundary_view(MultiPolygon& multipolygon)
  371. : detail::boundary_views::areal_boundary
  372. <
  373. MultiPolygon
  374. >(multipolygon)
  375. {}
  376. };
  377. } // namespace detail_dispatch
  378. #endif // DOXYGEN_NO_DISPATCH
  379. }} // namespace boost::geometry
  380. #endif // BOOST_GEOMETRY_VIEWS_DETAIL_BOUNDARY_VIEW_IMPLEMENTATION_HPP