distance_pythagoras.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2008-2012 Bruno Lalande, Paris, France.
  3. // Copyright (c) 2008-2012 Barend Gehrels, Amsterdam, the Netherlands.
  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_STRATEGIES_CARTESIAN_DISTANCE_PYTHAGORAS_HPP
  11. #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISTANCE_PYTHAGORAS_HPP
  12. #include <boost/mpl/if.hpp>
  13. #include <boost/type_traits.hpp>
  14. #include <boost/geometry/core/access.hpp>
  15. #include <boost/geometry/strategies/distance.hpp>
  16. #include <boost/geometry/util/calculation_type.hpp>
  17. namespace boost { namespace geometry
  18. {
  19. namespace strategy { namespace distance
  20. {
  21. #ifndef DOXYGEN_NO_DETAIL
  22. namespace detail
  23. {
  24. template <size_t I, typename T>
  25. struct compute_pythagoras
  26. {
  27. template <typename Point1, typename Point2>
  28. static inline T apply(Point1 const& p1, Point2 const& p2)
  29. {
  30. T const c1 = boost::numeric_cast<T>(get<I-1>(p1));
  31. T const c2 = boost::numeric_cast<T>(get<I-1>(p2));
  32. T const d = c1 - c2;
  33. return d * d + compute_pythagoras<I-1, T>::apply(p1, p2);
  34. }
  35. };
  36. template <typename T>
  37. struct compute_pythagoras<0, T>
  38. {
  39. template <typename Point1, typename Point2>
  40. static inline T apply(Point1 const&, Point2 const&)
  41. {
  42. return boost::numeric_cast<T>(0);
  43. }
  44. };
  45. }
  46. #endif // DOXYGEN_NO_DETAIL
  47. namespace comparable
  48. {
  49. /*!
  50. \brief Strategy to calculate comparable distance between two points
  51. \ingroup strategies
  52. \tparam Point1 \tparam_first_point
  53. \tparam Point2 \tparam_second_point
  54. \tparam CalculationType \tparam_calculation
  55. */
  56. template <typename CalculationType = void>
  57. class pythagoras
  58. {
  59. public :
  60. template <typename Point1, typename Point2>
  61. struct calculation_type
  62. : util::calculation_type::geometric::binary
  63. <
  64. Point1,
  65. Point2,
  66. CalculationType
  67. >
  68. {};
  69. template <typename Point1, typename Point2>
  70. static inline typename calculation_type<Point1, Point2>::type
  71. apply(Point1 const& p1, Point2 const& p2)
  72. {
  73. BOOST_CONCEPT_ASSERT( (concept::ConstPoint<Point1>) );
  74. BOOST_CONCEPT_ASSERT( (concept::ConstPoint<Point2>) );
  75. // Calculate distance using Pythagoras
  76. // (Leave comment above for Doxygen)
  77. assert_dimension_equal<Point1, Point2>();
  78. return detail::compute_pythagoras
  79. <
  80. dimension<Point1>::value,
  81. typename calculation_type<Point1, Point2>::type
  82. >::apply(p1, p2);
  83. }
  84. };
  85. } // namespace comparable
  86. /*!
  87. \brief Strategy to calculate the distance between two points
  88. \ingroup strategies
  89. \tparam CalculationType \tparam_calculation
  90. \qbk{
  91. [heading Notes]
  92. [note Can be used for points with two\, three or more dimensions]
  93. [heading See also]
  94. [link geometry.reference.algorithms.distance.distance_3_with_strategy distance (with strategy)]
  95. }
  96. */
  97. template
  98. <
  99. typename CalculationType = void
  100. >
  101. class pythagoras
  102. {
  103. public :
  104. template <typename P1, typename P2>
  105. struct calculation_type
  106. : util::calculation_type::geometric::binary
  107. <
  108. P1,
  109. P2,
  110. CalculationType,
  111. double,
  112. double // promote integer to double
  113. >
  114. {};
  115. /*!
  116. \brief applies the distance calculation using pythagoras
  117. \return the calculated distance (including taking the square root)
  118. \param p1 first point
  119. \param p2 second point
  120. */
  121. template <typename P1, typename P2>
  122. static inline typename calculation_type<P1, P2>::type
  123. apply(P1 const& p1, P2 const& p2)
  124. {
  125. // The cast is necessary for MSVC which considers sqrt __int64 as an ambiguous call
  126. return std::sqrt
  127. (
  128. boost::numeric_cast<typename calculation_type<P1, P2>::type>
  129. (
  130. comparable::pythagoras<CalculationType>::apply(p1, p2)
  131. )
  132. );
  133. }
  134. };
  135. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  136. namespace services
  137. {
  138. template <typename CalculationType>
  139. struct tag<pythagoras<CalculationType> >
  140. {
  141. typedef strategy_tag_distance_point_point type;
  142. };
  143. template <typename CalculationType, typename P1, typename P2>
  144. struct return_type<distance::pythagoras<CalculationType>, P1, P2>
  145. : pythagoras<CalculationType>::template calculation_type<P1, P2>
  146. {};
  147. template <typename CalculationType>
  148. struct comparable_type<pythagoras<CalculationType> >
  149. {
  150. typedef comparable::pythagoras<CalculationType> type;
  151. };
  152. template <typename CalculationType>
  153. struct get_comparable<pythagoras<CalculationType> >
  154. {
  155. typedef comparable::pythagoras<CalculationType> comparable_type;
  156. public :
  157. static inline comparable_type apply(pythagoras<CalculationType> const& )
  158. {
  159. return comparable_type();
  160. }
  161. };
  162. template <typename CalculationType, typename Point1, typename Point2>
  163. struct result_from_distance<pythagoras<CalculationType>, Point1, Point2>
  164. {
  165. private :
  166. typedef typename return_type<pythagoras<CalculationType>, Point1, Point2>::type return_type;
  167. public :
  168. template <typename T>
  169. static inline return_type apply(pythagoras<CalculationType> const& , T const& value)
  170. {
  171. return return_type(value);
  172. }
  173. };
  174. // Specializations for comparable::pythagoras
  175. template <typename CalculationType>
  176. struct tag<comparable::pythagoras<CalculationType> >
  177. {
  178. typedef strategy_tag_distance_point_point type;
  179. };
  180. template <typename CalculationType, typename P1, typename P2>
  181. struct return_type<comparable::pythagoras<CalculationType>, P1, P2>
  182. : comparable::pythagoras<CalculationType>::template calculation_type<P1, P2>
  183. {};
  184. template <typename CalculationType>
  185. struct comparable_type<comparable::pythagoras<CalculationType> >
  186. {
  187. typedef comparable::pythagoras<CalculationType> type;
  188. };
  189. template <typename CalculationType>
  190. struct get_comparable<comparable::pythagoras<CalculationType> >
  191. {
  192. typedef comparable::pythagoras<CalculationType> comparable_type;
  193. public :
  194. static inline comparable_type apply(comparable::pythagoras<CalculationType> const& )
  195. {
  196. return comparable_type();
  197. }
  198. };
  199. template <typename CalculationType, typename Point1, typename Point2>
  200. struct result_from_distance<comparable::pythagoras<CalculationType>, Point1, Point2>
  201. {
  202. private :
  203. typedef typename return_type<comparable::pythagoras<CalculationType>, Point1, Point2>::type return_type;
  204. public :
  205. template <typename T>
  206. static inline return_type apply(comparable::pythagoras<CalculationType> const& , T const& value)
  207. {
  208. return_type const v = value;
  209. return v * v;
  210. }
  211. };
  212. template <typename Point1, typename Point2>
  213. struct default_strategy<point_tag, Point1, Point2, cartesian_tag, cartesian_tag, void>
  214. {
  215. typedef pythagoras<> type;
  216. };
  217. } // namespace services
  218. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  219. }} // namespace strategy::distance
  220. }} // namespace boost::geometry
  221. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISTANCE_PYTHAGORAS_HPP