implementation.hpp 12 KB

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