distance_pythagoras.hpp 7.2 KB

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