segment.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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_GEOMETRIES_SEGMENT_HPP
  11. #define BOOST_GEOMETRY_GEOMETRIES_SEGMENT_HPP
  12. #include <cstddef>
  13. #include <boost/concept/assert.hpp>
  14. #include <boost/mpl/if.hpp>
  15. #include <boost/type_traits/is_const.hpp>
  16. #include <boost/geometry/geometries/concepts/point_concept.hpp>
  17. namespace boost { namespace geometry
  18. {
  19. namespace model
  20. {
  21. /*!
  22. \brief Class segment: small class containing two points
  23. \ingroup geometries
  24. \details From Wikipedia: In geometry, a line segment is a part of a line that is bounded
  25. by two distinct end points, and contains every point on the line between its end points.
  26. \note There is also a point-referring-segment, class referring_segment,
  27. containing point references, where points are NOT copied
  28. \qbk{[include reference/geometries/segment.qbk]}
  29. \qbk{before.synopsis,
  30. [heading Model of]
  31. [link geometry.reference.concepts.concept_segment Segment Concept]
  32. }
  33. */
  34. template<typename Point>
  35. class segment : public std::pair<Point, Point>
  36. {
  37. BOOST_CONCEPT_ASSERT( (concepts::Point<Point>) );
  38. public :
  39. #ifndef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS
  40. /// \constructor_default_no_init
  41. segment() = default;
  42. #else
  43. /// \constructor_default_no_init
  44. inline segment()
  45. {}
  46. #endif
  47. /*!
  48. \brief Constructor taking the first and the second point
  49. */
  50. inline segment(Point const& p1, Point const& p2)
  51. {
  52. this->first = p1;
  53. this->second = p2;
  54. }
  55. };
  56. /*!
  57. \brief Class segment: small class containing two (templatized) point references
  58. \ingroup geometries
  59. \details From Wikipedia: In geometry, a line segment is a part of a line that is bounded
  60. by two distinct end points, and contains every point on the line between its end points.
  61. \note The structure is like std::pair, and can often be used interchangeable.
  62. Difference is that it refers to points, does not have points.
  63. \note Like std::pair, points are public available.
  64. \note type is const or non const, so geometry::segment<P> or geometry::segment<P const>
  65. \note We cannot derive from std::pair<P&, P&> because of
  66. reference assignments.
  67. \tparam ConstOrNonConstPoint point type of the segment, maybe a point or a const point
  68. */
  69. template<typename ConstOrNonConstPoint>
  70. class referring_segment
  71. {
  72. BOOST_CONCEPT_ASSERT( (
  73. typename boost::mpl::if_
  74. <
  75. boost::is_const<ConstOrNonConstPoint>,
  76. concepts::Point<ConstOrNonConstPoint>,
  77. concepts::ConstPoint<ConstOrNonConstPoint>
  78. >
  79. ) );
  80. typedef ConstOrNonConstPoint point_type;
  81. public:
  82. point_type& first;
  83. point_type& second;
  84. /*!
  85. \brief Constructor taking the first and the second point
  86. */
  87. inline referring_segment(point_type& p1, point_type& p2)
  88. : first(p1)
  89. , second(p2)
  90. {}
  91. };
  92. } // namespace model
  93. // Traits specializations for segment above
  94. #ifndef DOXYGEN_NO_TRAITS_SPECIALIZATIONS
  95. namespace traits
  96. {
  97. template <typename Point>
  98. struct tag<model::segment<Point> >
  99. {
  100. typedef segment_tag type;
  101. };
  102. template <typename Point>
  103. struct point_type<model::segment<Point> >
  104. {
  105. typedef Point type;
  106. };
  107. template <typename Point, std::size_t Dimension>
  108. struct indexed_access<model::segment<Point>, 0, Dimension>
  109. {
  110. typedef model::segment<Point> segment_type;
  111. typedef typename geometry::coordinate_type<segment_type>::type coordinate_type;
  112. static inline coordinate_type get(segment_type const& s)
  113. {
  114. return geometry::get<Dimension>(s.first);
  115. }
  116. static inline void set(segment_type& s, coordinate_type const& value)
  117. {
  118. geometry::set<Dimension>(s.first, value);
  119. }
  120. };
  121. template <typename Point, std::size_t Dimension>
  122. struct indexed_access<model::segment<Point>, 1, Dimension>
  123. {
  124. typedef model::segment<Point> segment_type;
  125. typedef typename geometry::coordinate_type<segment_type>::type coordinate_type;
  126. static inline coordinate_type get(segment_type const& s)
  127. {
  128. return geometry::get<Dimension>(s.second);
  129. }
  130. static inline void set(segment_type& s, coordinate_type const& value)
  131. {
  132. geometry::set<Dimension>(s.second, value);
  133. }
  134. };
  135. template <typename ConstOrNonConstPoint>
  136. struct tag<model::referring_segment<ConstOrNonConstPoint> >
  137. {
  138. typedef segment_tag type;
  139. };
  140. template <typename ConstOrNonConstPoint>
  141. struct point_type<model::referring_segment<ConstOrNonConstPoint> >
  142. {
  143. typedef ConstOrNonConstPoint type;
  144. };
  145. template <typename ConstOrNonConstPoint, std::size_t Dimension>
  146. struct indexed_access<model::referring_segment<ConstOrNonConstPoint>, 0, Dimension>
  147. {
  148. typedef model::referring_segment<ConstOrNonConstPoint> segment_type;
  149. typedef typename geometry::coordinate_type<segment_type>::type coordinate_type;
  150. static inline coordinate_type get(segment_type const& s)
  151. {
  152. return geometry::get<Dimension>(s.first);
  153. }
  154. static inline void set(segment_type& s, coordinate_type const& value)
  155. {
  156. geometry::set<Dimension>(s.first, value);
  157. }
  158. };
  159. template <typename ConstOrNonConstPoint, std::size_t Dimension>
  160. struct indexed_access<model::referring_segment<ConstOrNonConstPoint>, 1, Dimension>
  161. {
  162. typedef model::referring_segment<ConstOrNonConstPoint> segment_type;
  163. typedef typename geometry::coordinate_type<segment_type>::type coordinate_type;
  164. static inline coordinate_type get(segment_type const& s)
  165. {
  166. return geometry::get<Dimension>(s.second);
  167. }
  168. static inline void set(segment_type& s, coordinate_type const& value)
  169. {
  170. geometry::set<Dimension>(s.second, value);
  171. }
  172. };
  173. } // namespace traits
  174. #endif // DOXYGEN_NO_TRAITS_SPECIALIZATIONS
  175. }} // namespace boost::geometry
  176. #endif // BOOST_GEOMETRY_GEOMETRIES_SEGMENT_HPP