implementation.hpp 12 KB

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