segment_view.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  4. // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
  5. // This file was modified by Oracle on 2020-2021.
  6. // Modifications copyright (c) 2020-2021 Oracle and/or its affiliates.
  7. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  8. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  9. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  10. // Use, modification and distribution is subject to the Boost Software License,
  11. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. #ifndef BOOST_GEOMETRY_VIEWS_SEGMENT_VIEW_HPP
  14. #define BOOST_GEOMETRY_VIEWS_SEGMENT_VIEW_HPP
  15. #include <array>
  16. #include <boost/geometry/algorithms/detail/assign_indexed_point.hpp>
  17. #include <boost/geometry/core/point_type.hpp>
  18. #include <boost/geometry/core/tag.hpp>
  19. namespace boost { namespace geometry
  20. {
  21. // NOTE: This is equivalent to the previous implementation with detail::points_view.
  22. // Technically this should not be called a view because it owns the elements.
  23. // It's also not a borrowed_range because of dangling iterators after the
  24. // destruction.
  25. // It's a container or more specifically a linestring of some sort, e.g. static_linestring.
  26. // NOTE: It would be possible to implement a borrowed_range or a view.
  27. // The iterators would have to store copies of points.
  28. // Another possibility is to store the original Segment or reference/pointer
  29. // to Segment and index. But then the reference would be the value type
  30. // so technically they would be InputIterators not RandomAccessIterators.
  31. /*!
  32. \brief Makes a segment behave like a linestring or a range
  33. \details Adapts a segment to the Boost.Range concept, enabling the user to
  34. iterate the two segment points. The segment_view is registered as a LineString Concept
  35. \tparam Segment \tparam_geometry{Segment}
  36. \ingroup views
  37. \qbk{before.synopsis,
  38. [heading Model of]
  39. [link geometry.reference.concepts.concept_linestring LineString Concept]
  40. }
  41. \qbk{[include reference/views/segment_view.qbk]}
  42. */
  43. template <typename Segment>
  44. struct segment_view
  45. {
  46. using array_t = std::array<typename geometry::point_type<Segment>::type, 2>;
  47. using iterator = typename array_t::const_iterator;
  48. using const_iterator = typename array_t::const_iterator;
  49. /// Constructor accepting the segment to adapt
  50. explicit segment_view(Segment const& segment)
  51. {
  52. geometry::detail::assign_point_from_index<0>(segment, m_array[0]);
  53. geometry::detail::assign_point_from_index<1>(segment, m_array[1]);
  54. }
  55. const_iterator begin() const noexcept { return m_array.begin(); }
  56. const_iterator end() const noexcept { return m_array.end(); }
  57. private:
  58. array_t m_array;
  59. };
  60. #ifndef DOXYGEN_NO_TRAITS_SPECIALIZATIONS
  61. // All segment ranges can be handled as linestrings
  62. namespace traits
  63. {
  64. template<typename Segment>
  65. struct tag<segment_view<Segment> >
  66. {
  67. typedef linestring_tag type;
  68. };
  69. }
  70. #endif // DOXYGEN_NO_TRAITS_SPECIALIZATIONS
  71. }} // namespace boost::geometry
  72. #endif // BOOST_GEOMETRY_VIEWS_SEGMENT_VIEW_HPP