|
|
00001 // Boost.Geometry (aka GGL, Generic Geometry Library) 00002 // 00003 // Copyright Barend Gehrels 2007-2009, Geodan, Amsterdam, the Netherlands. 00004 // Copyright Bruno Lalande 2008, 2009 00005 // Copyright Mateusz Loskot 2009, mateusz@loskot.net 00006 // Use, modification and distribution is subject to the Boost Software License, 00007 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 00008 // http://www.boost.org/LICENSE_1_0.txt) 00009 00010 #ifndef BOOST_GEOMETRY_ITERATORS_SEGMENT_ITERATOR_HPP 00011 #define BOOST_GEOMETRY_ITERATORS_SEGMENT_ITERATOR_HPP 00012 00013 // TODO: This is very experimental version of input iterator 00014 // reading collection of points as segments - proof of concept. 00015 // --mloskot 00016 00017 // TODO: Move to boost::iterator_adaptor 00018 00019 #include <iterator> 00020 00021 #include <boost/assert.hpp> 00022 #include <boost/iterator.hpp> 00023 #include <boost/iterator/iterator_adaptor.hpp> 00024 #include <boost/iterator/iterator_categories.hpp> 00025 00026 #include <boost/geometry/algorithms/equals.hpp> 00027 #include <boost/geometry/geometries/segment.hpp> 00028 00029 namespace boost { namespace geometry 00030 { 00031 00032 template <typename Base, typename Point> 00033 struct segment_iterator 00034 { 00035 typedef Base base_type; 00036 typedef Point point_type; 00037 typedef typename geometry::segment<Point> segment_type; 00038 00039 typedef std::input_iterator_tag iterator_category; 00040 typedef typename std::iterator_traits<Base>::difference_type difference_type; 00041 typedef segment_type value_type; 00042 typedef segment_type* pointer; 00043 typedef segment_type& reference; 00044 00045 explicit segment_iterator(Base const& end) 00046 : m_segment(p1 , p2) 00047 , m_prev(end) 00048 , m_it(end) 00049 , m_end(end) 00050 { 00051 } 00052 00053 segment_iterator(Base const& it, Base const& end) 00054 : m_segment(p1 , p2) 00055 , m_prev(it) 00056 , m_it(it) 00057 , m_end(end) 00058 { 00059 if (m_it != m_end) 00060 { 00061 BOOST_ASSERT(m_prev != m_end); 00062 ++m_it; 00063 } 00064 } 00065 00066 reference operator*() 00067 { 00068 BOOST_ASSERT(m_it != m_end && m_prev != m_end); 00069 00070 p1 = *m_prev; 00071 p2 = *m_it; 00072 00073 return m_segment; 00074 } 00075 00076 pointer operator->() 00077 { 00078 return &(operator*()); 00079 } 00080 00081 segment_iterator& operator++() 00082 { 00083 ++m_prev; 00084 ++m_it; 00085 return *this; 00086 } 00087 00088 segment_iterator operator++(int) 00089 { 00090 segment_iterator it(*this); 00091 ++(*this); 00092 return it; 00093 } 00094 00095 Base const& base() const { return m_it; } 00096 00097 private: 00098 00099 point_type p1; 00100 point_type p2; 00101 segment_type m_segment; 00102 00103 Base m_prev; 00104 Base m_it; 00105 Base m_end; 00106 }; 00107 00108 template <typename Base, typename Point> 00109 bool operator==(segment_iterator<Base, Point> const& lhs, 00110 segment_iterator<Base, Point> const& rhs) 00111 { 00112 return (lhs.base() == rhs.base()); 00113 } 00114 00115 template <typename Base, typename Point> 00116 bool operator!=(segment_iterator<Base, Point> const& lhs, 00117 segment_iterator<Base, Point> const& rhs) 00118 { 00119 return (lhs.base() != rhs.base()); 00120 } 00121 00122 template <typename C> 00123 segment_iterator 00124 < 00125 typename C::iterator, 00126 typename C::value_type 00127 > 00128 make_segment_iterator(C& c) 00129 { 00130 typedef typename C::iterator base_iterator; 00131 typedef typename C::value_type point_type; 00132 return segment_iterator<base_iterator, point_type>(c.begin(), c.end()); 00133 } 00134 00135 }} // namespace boost::geometry 00136 00137 #endif // BOOST_GEOMETRY_ITERATORS_SEGMENT_ITERATOR_HPP
|
December 1, 2009 |
Copyright © 1995-2009 Barend Gehrels, Geodan, Amsterdam Copyright © 2008-2009 Bruno Lalande, Paris Copyright © 2009 Mateusz Loskot, Cadcorp, London |