distance_pythagoras_box_box.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2008-2014 Bruno Lalande, Paris, France.
  3. // Copyright (c) 2008-2014 Barend Gehrels, Amsterdam, the Netherlands.
  4. // Copyright (c) 2009-2014 Mateusz Loskot, London, UK.
  5. // This file was modified by Oracle on 2014, 2018.
  6. // Modifications copyright (c) 2014, 2018, Oracle and/or its affiliates.
  7. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  8. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  9. // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
  10. // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
  11. // Use, modification and distribution is subject to the Boost Software License,
  12. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  13. // http://www.boost.org/LICENSE_1_0.txt)
  14. #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISTANCE_PYTHAGORAS_BOX_BOX_HPP
  15. #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISTANCE_PYTHAGORAS_BOX_BOX_HPP
  16. #include <boost/geometry/core/access.hpp>
  17. #include <boost/geometry/core/point_type.hpp>
  18. #include <boost/geometry/geometries/concepts/point_concept.hpp>
  19. #include <boost/geometry/strategies/distance.hpp>
  20. #include <boost/geometry/util/math.hpp>
  21. #include <boost/geometry/util/calculation_type.hpp>
  22. namespace boost { namespace geometry
  23. {
  24. namespace strategy { namespace distance
  25. {
  26. #ifndef DOXYGEN_NO_DETAIL
  27. namespace detail
  28. {
  29. template <std::size_t I>
  30. struct compute_pythagoras_box_box
  31. {
  32. template <typename Box1, typename Box2, typename T>
  33. static inline void apply(Box1 const& box1, Box2 const& box2, T& result)
  34. {
  35. T const b1_min_coord =
  36. boost::numeric_cast<T>(geometry::get<min_corner, I-1>(box1));
  37. T const b1_max_coord =
  38. boost::numeric_cast<T>(geometry::get<max_corner, I-1>(box1));
  39. T const b2_min_coord =
  40. boost::numeric_cast<T>(geometry::get<min_corner, I-1>(box2));
  41. T const b2_max_coord =
  42. boost::numeric_cast<T>(geometry::get<max_corner, I-1>(box2));
  43. if ( b1_max_coord < b2_min_coord )
  44. {
  45. T diff = b2_min_coord - b1_max_coord;
  46. result += diff * diff;
  47. }
  48. if ( b1_min_coord > b2_max_coord )
  49. {
  50. T diff = b1_min_coord - b2_max_coord;
  51. result += diff * diff;
  52. }
  53. compute_pythagoras_box_box<I-1>::apply(box1, box2, result);
  54. }
  55. };
  56. template <>
  57. struct compute_pythagoras_box_box<0>
  58. {
  59. template <typename Box1, typename Box2, typename T>
  60. static inline void apply(Box1 const&, Box2 const&, T&)
  61. {
  62. }
  63. };
  64. }
  65. #endif // DOXYGEN_NO_DETAIL
  66. namespace comparable
  67. {
  68. /*!
  69. \brief Strategy to calculate comparable distance between two boxes
  70. \ingroup strategies
  71. \tparam Box1 \tparam_first_box
  72. \tparam Box2 \tparam_second_box
  73. \tparam CalculationType \tparam_calculation
  74. */
  75. template <typename CalculationType = void>
  76. class pythagoras_box_box
  77. {
  78. public :
  79. template <typename Box1, typename Box2>
  80. struct calculation_type
  81. {
  82. typedef typename util::calculation_type::geometric::binary
  83. <
  84. Box1,
  85. Box2,
  86. CalculationType
  87. >::type type;
  88. };
  89. template <typename Box1, typename Box2>
  90. static inline typename calculation_type<Box1, Box2>::type
  91. apply(Box1 const& box1, Box2 const& box2)
  92. {
  93. BOOST_CONCEPT_ASSERT
  94. ( (concepts::ConstPoint<typename point_type<Box1>::type>) );
  95. BOOST_CONCEPT_ASSERT
  96. ( (concepts::ConstPoint<typename point_type<Box2>::type>) );
  97. // Calculate distance using Pythagoras
  98. // (Leave comment above for Doxygen)
  99. assert_dimension_equal<Box1, Box2>();
  100. typename calculation_type<Box1, Box2>::type result(0);
  101. detail::compute_pythagoras_box_box
  102. <
  103. dimension<Box1>::value
  104. >::apply(box1, box2, result);
  105. return result;
  106. }
  107. };
  108. } // namespace comparable
  109. /*!
  110. \brief Strategy to calculate the distance between two boxes
  111. \ingroup strategies
  112. \tparam CalculationType \tparam_calculation
  113. \qbk{
  114. [heading Notes]
  115. [note Can be used for boxes with two\, three or more dimensions]
  116. [heading See also]
  117. [link geometry.reference.algorithms.distance.distance_3_with_strategy distance (with strategy)]
  118. }
  119. */
  120. template
  121. <
  122. typename CalculationType = void
  123. >
  124. class pythagoras_box_box
  125. {
  126. public :
  127. template <typename Box1, typename Box2>
  128. struct calculation_type
  129. : util::calculation_type::geometric::binary
  130. <
  131. Box1,
  132. Box2,
  133. CalculationType,
  134. double,
  135. double // promote integer to double
  136. >
  137. {};
  138. /*!
  139. \brief applies the distance calculation using pythagoras_box_box
  140. \return the calculated distance (including taking the square root)
  141. \param box1 first box
  142. \param box2 second box
  143. */
  144. template <typename Box1, typename Box2>
  145. static inline typename calculation_type<Box1, Box2>::type
  146. apply(Box1 const& box1, Box2 const& box2)
  147. {
  148. // The cast is necessary for MSVC which considers sqrt __int64 as an ambiguous call
  149. return math::sqrt
  150. (
  151. boost::numeric_cast<typename calculation_type
  152. <
  153. Box1, Box2
  154. >::type>
  155. (
  156. comparable::pythagoras_box_box
  157. <
  158. CalculationType
  159. >::apply(box1, box2)
  160. )
  161. );
  162. }
  163. };
  164. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  165. namespace services
  166. {
  167. template <typename CalculationType>
  168. struct tag<pythagoras_box_box<CalculationType> >
  169. {
  170. typedef strategy_tag_distance_box_box type;
  171. };
  172. template <typename CalculationType, typename Box1, typename Box2>
  173. struct return_type<distance::pythagoras_box_box<CalculationType>, Box1, Box2>
  174. : pythagoras_box_box<CalculationType>::template calculation_type<Box1, Box2>
  175. {};
  176. template <typename CalculationType>
  177. struct comparable_type<pythagoras_box_box<CalculationType> >
  178. {
  179. typedef comparable::pythagoras_box_box<CalculationType> type;
  180. };
  181. template <typename CalculationType>
  182. struct get_comparable<pythagoras_box_box<CalculationType> >
  183. {
  184. typedef comparable::pythagoras_box_box<CalculationType> comparable_type;
  185. public :
  186. static inline comparable_type
  187. apply(pythagoras_box_box<CalculationType> const& )
  188. {
  189. return comparable_type();
  190. }
  191. };
  192. template <typename CalculationType, typename Box1, typename Box2>
  193. struct result_from_distance<pythagoras_box_box<CalculationType>, Box1, Box2>
  194. {
  195. private:
  196. typedef typename return_type
  197. <
  198. pythagoras_box_box<CalculationType>, Box1, Box2
  199. >::type return_type;
  200. public:
  201. template <typename T>
  202. static inline return_type
  203. apply(pythagoras_box_box<CalculationType> const& , T const& value)
  204. {
  205. return return_type(value);
  206. }
  207. };
  208. // Specializations for comparable::pythagoras_box_box
  209. template <typename CalculationType>
  210. struct tag<comparable::pythagoras_box_box<CalculationType> >
  211. {
  212. typedef strategy_tag_distance_box_box type;
  213. };
  214. template <typename CalculationType, typename Box1, typename Box2>
  215. struct return_type<comparable::pythagoras_box_box<CalculationType>, Box1, Box2>
  216. : comparable::pythagoras_box_box
  217. <
  218. CalculationType
  219. >::template calculation_type<Box1, Box2>
  220. {};
  221. template <typename CalculationType>
  222. struct comparable_type<comparable::pythagoras_box_box<CalculationType> >
  223. {
  224. typedef comparable::pythagoras_box_box<CalculationType> type;
  225. };
  226. template <typename CalculationType>
  227. struct get_comparable<comparable::pythagoras_box_box<CalculationType> >
  228. {
  229. typedef comparable::pythagoras_box_box<CalculationType> comparable_type;
  230. public :
  231. static inline comparable_type apply(comparable_type const& )
  232. {
  233. return comparable_type();
  234. }
  235. };
  236. template <typename CalculationType, typename Box1, typename Box2>
  237. struct result_from_distance
  238. <
  239. comparable::pythagoras_box_box<CalculationType>, Box1, Box2
  240. >
  241. {
  242. private :
  243. typedef typename return_type
  244. <
  245. comparable::pythagoras_box_box<CalculationType>, Box1, Box2
  246. >::type return_type;
  247. public :
  248. template <typename T>
  249. static inline return_type
  250. apply(comparable::pythagoras_box_box<CalculationType> const&,
  251. T const& value)
  252. {
  253. return_type const v = value;
  254. return v * v;
  255. }
  256. };
  257. template <typename BoxPoint1, typename BoxPoint2>
  258. struct default_strategy
  259. <
  260. box_tag, box_tag, BoxPoint1, BoxPoint2, cartesian_tag, cartesian_tag
  261. >
  262. {
  263. typedef pythagoras_box_box<> type;
  264. };
  265. } // namespace services
  266. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  267. }} // namespace strategy::distance
  268. }} // namespace boost::geometry
  269. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISTANCE_PYTHAGORAS_BOX_BOX_HPP