1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
4 // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
5 // Copyright (c) 2009-2012 Mateusz Loskot, London, UK.
7 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
8 // (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)
14 #ifndef BOOST_GEOMETRY_GEOMETRIES_SEGMENT_HPP
15 #define BOOST_GEOMETRY_GEOMETRIES_SEGMENT_HPP
19 #include <boost/concept/assert.hpp>
20 #include <boost/mpl/if.hpp>
21 #include <boost/type_traits/is_const.hpp>
23 #include <boost/geometry/geometries/concepts/point_concept.hpp>
25 namespace boost { namespace geometry
32 \brief Class segment: small class containing two points
34 \details From Wikipedia: In geometry, a line segment is a part of a line that is bounded
35 by two distinct end points, and contains every point on the line between its end points.
36 \note There is also a point-referring-segment, class referring_segment,
37 containing point references, where points are NOT copied
39 \qbk{[include reference/geometries/segment.qbk]}
42 [link geometry.reference.concepts.concept_segment Segment Concept]
45 template<typename Point>
46 class segment : public std::pair<Point, Point>
48 BOOST_CONCEPT_ASSERT( (concepts::Point<Point>) );
52 #ifndef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS
53 /// \constructor_default_no_init
56 /// \constructor_default_no_init
62 \brief Constructor taking the first and the second point
64 inline segment(Point const& p1, Point const& p2)
73 \brief Class segment: small class containing two (templatized) point references
75 \details From Wikipedia: In geometry, a line segment is a part of a line that is bounded
76 by two distinct end points, and contains every point on the line between its end points.
77 \note The structure is like std::pair, and can often be used interchangeable.
78 Difference is that it refers to points, does not have points.
79 \note Like std::pair, points are public available.
80 \note type is const or non const, so geometry::segment<P> or geometry::segment<P const>
81 \note We cannot derive from std::pair<P&, P&> because of
82 reference assignments.
83 \tparam ConstOrNonConstPoint point type of the segment, maybe a point or a const point
85 template<typename ConstOrNonConstPoint>
86 class referring_segment
88 BOOST_CONCEPT_ASSERT( (
89 typename boost::mpl::if_
91 boost::is_const<ConstOrNonConstPoint>,
92 concepts::Point<ConstOrNonConstPoint>,
93 concepts::ConstPoint<ConstOrNonConstPoint>
97 typedef ConstOrNonConstPoint point_type;
105 \brief Constructor taking the first and the second point
107 inline referring_segment(point_type& p1, point_type& p2)
117 // Traits specializations for segment above
118 #ifndef DOXYGEN_NO_TRAITS_SPECIALIZATIONS
122 template <typename Point>
123 struct tag<model::segment<Point> >
125 typedef segment_tag type;
128 template <typename Point>
129 struct point_type<model::segment<Point> >
134 template <typename Point, std::size_t Dimension>
135 struct indexed_access<model::segment<Point>, 0, Dimension>
137 typedef model::segment<Point> segment_type;
138 typedef typename geometry::coordinate_type<segment_type>::type coordinate_type;
140 static inline coordinate_type get(segment_type const& s)
142 return geometry::get<Dimension>(s.first);
145 static inline void set(segment_type& s, coordinate_type const& value)
147 geometry::set<Dimension>(s.first, value);
152 template <typename Point, std::size_t Dimension>
153 struct indexed_access<model::segment<Point>, 1, Dimension>
155 typedef model::segment<Point> segment_type;
156 typedef typename geometry::coordinate_type<segment_type>::type coordinate_type;
158 static inline coordinate_type get(segment_type const& s)
160 return geometry::get<Dimension>(s.second);
163 static inline void set(segment_type& s, coordinate_type const& value)
165 geometry::set<Dimension>(s.second, value);
170 template <typename ConstOrNonConstPoint>
171 struct tag<model::referring_segment<ConstOrNonConstPoint> >
173 typedef segment_tag type;
176 template <typename ConstOrNonConstPoint>
177 struct point_type<model::referring_segment<ConstOrNonConstPoint> >
179 typedef ConstOrNonConstPoint type;
182 template <typename ConstOrNonConstPoint, std::size_t Dimension>
183 struct indexed_access<model::referring_segment<ConstOrNonConstPoint>, 0, Dimension>
185 typedef model::referring_segment<ConstOrNonConstPoint> segment_type;
186 typedef typename geometry::coordinate_type<segment_type>::type coordinate_type;
188 static inline coordinate_type get(segment_type const& s)
190 return geometry::get<Dimension>(s.first);
193 static inline void set(segment_type& s, coordinate_type const& value)
195 geometry::set<Dimension>(s.first, value);
200 template <typename ConstOrNonConstPoint, std::size_t Dimension>
201 struct indexed_access<model::referring_segment<ConstOrNonConstPoint>, 1, Dimension>
203 typedef model::referring_segment<ConstOrNonConstPoint> segment_type;
204 typedef typename geometry::coordinate_type<segment_type>::type coordinate_type;
206 static inline coordinate_type get(segment_type const& s)
208 return geometry::get<Dimension>(s.second);
211 static inline void set(segment_type& s, coordinate_type const& value)
213 geometry::set<Dimension>(s.second, value);
219 } // namespace traits
220 #endif // DOXYGEN_NO_TRAITS_SPECIALIZATIONS
222 }} // namespace boost::geometry
224 #endif // BOOST_GEOMETRY_GEOMETRIES_SEGMENT_HPP