points_view.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  6. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_GEOMETRY_VIEWS_DETAIL_POINTS_VIEW_HPP
  11. #define BOOST_GEOMETRY_VIEWS_DETAIL_POINTS_VIEW_HPP
  12. #include <boost/range.hpp>
  13. #include <boost/iterator/iterator_facade.hpp>
  14. #include <boost/iterator/iterator_categories.hpp>
  15. #include <boost/geometry/core/exception.hpp>
  16. namespace boost { namespace geometry
  17. {
  18. namespace detail
  19. {
  20. // Adapts pointer, on points, to a Boost.Range
  21. template <typename Point, int MaxSize>
  22. class points_view
  23. {
  24. // Iterates over a series of points (indicated by pointer
  25. // to have it lightweight). Probably there is already an
  26. // equivalent of this within Boost. If so, TODO: use that one.
  27. // This used to be "box_iterator" and "segment_iterator".
  28. // ALTERNATIVE: use boost:array and its iterators
  29. struct points_iterator
  30. : public boost::iterator_facade
  31. <
  32. points_iterator,
  33. Point const,
  34. boost::random_access_traversal_tag
  35. >
  36. {
  37. // Constructor: Begin iterator
  38. inline points_iterator(Point const* p)
  39. : m_points(p)
  40. , m_index(0)
  41. {}
  42. // Constructor: End iterator
  43. inline points_iterator(Point const* p, bool)
  44. : m_points(p)
  45. , m_index(MaxSize)
  46. {}
  47. // Constructor: default (for Range Concept checking).
  48. inline points_iterator()
  49. : m_points(NULL)
  50. , m_index(MaxSize)
  51. {}
  52. typedef std::ptrdiff_t difference_type;
  53. private:
  54. friend class boost::iterator_core_access;
  55. inline Point const& dereference() const
  56. {
  57. if (m_index >= 0 && m_index < MaxSize)
  58. {
  59. return m_points[m_index];
  60. }
  61. // If it index larger (or smaller) return first point
  62. // (assuming initialized)
  63. return m_points[0];
  64. }
  65. inline bool equal(points_iterator const& other) const
  66. {
  67. return other.m_index == this->m_index;
  68. }
  69. inline void increment()
  70. {
  71. m_index++;
  72. }
  73. inline void decrement()
  74. {
  75. m_index--;
  76. }
  77. inline difference_type distance_to(points_iterator const& other) const
  78. {
  79. return other.m_index - this->m_index;
  80. }
  81. inline void advance(difference_type n)
  82. {
  83. m_index += n;
  84. }
  85. Point const* m_points;
  86. difference_type m_index;
  87. };
  88. public :
  89. typedef points_iterator const_iterator;
  90. typedef points_iterator iterator; // must be defined
  91. const_iterator begin() const { return const_iterator(m_points); }
  92. const_iterator end() const { return const_iterator(m_points, true); }
  93. // It may NOT be used non-const, so commented:
  94. //iterator begin() { return m_begin; }
  95. //iterator end() { return m_end; }
  96. protected :
  97. template <typename CopyPolicy>
  98. explicit points_view(CopyPolicy const& copy)
  99. {
  100. copy.apply(m_points);
  101. }
  102. private :
  103. // Copy points here - box might define them otherwise
  104. Point m_points[MaxSize];
  105. };
  106. }
  107. }} // namespace boost::geometry
  108. #endif // BOOST_GEOMETRY_VIEWS_DETAIL_POINTS_VIEW_HPP