implementation.hpp 12 KB

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