distance_pythagoras_point_box.hpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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_POINT_BOX_HPP
  15. #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISTANCE_PYTHAGORAS_POINT_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/calculation_type.hpp>
  21. #include <boost/geometry/util/math.hpp>
  22. #include <boost/geometry/util/numeric_cast.hpp>
  23. namespace boost { namespace geometry
  24. {
  25. namespace strategy { namespace distance
  26. {
  27. #ifndef DOXYGEN_NO_DETAIL
  28. namespace detail
  29. {
  30. template <size_t I>
  31. struct compute_pythagoras_point_box
  32. {
  33. template <typename Point, typename Box, typename T>
  34. static inline void apply(Point const& point, Box const& box, T& result)
  35. {
  36. T const p_coord = util::numeric_cast<T>(geometry::get<I-1>(point));
  37. T const b_min_coord =
  38. util::numeric_cast<T>(geometry::get<min_corner, I-1>(box));
  39. T const b_max_coord =
  40. util::numeric_cast<T>(geometry::get<max_corner, I-1>(box));
  41. if ( p_coord < b_min_coord )
  42. {
  43. T diff = b_min_coord - p_coord;
  44. result += diff * diff;
  45. }
  46. if ( p_coord > b_max_coord )
  47. {
  48. T diff = p_coord - b_max_coord;
  49. result += diff * diff;
  50. }
  51. compute_pythagoras_point_box<I-1>::apply(point, box, result);
  52. }
  53. };
  54. template <>
  55. struct compute_pythagoras_point_box<0>
  56. {
  57. template <typename Point, typename Box, typename T>
  58. static inline void apply(Point const&, Box const&, T&)
  59. {
  60. }
  61. };
  62. } // namespace detail
  63. #endif // DOXYGEN_NO_DETAIL
  64. namespace comparable
  65. {
  66. /*!
  67. \brief Strategy to calculate comparable distance between a point
  68. and a box
  69. \ingroup strategies
  70. \tparam Point \tparam_first_point
  71. \tparam Box \tparam_second_box
  72. \tparam CalculationType \tparam_calculation
  73. */
  74. template <typename CalculationType = void>
  75. class pythagoras_point_box
  76. {
  77. public :
  78. template <typename Point, typename Box>
  79. struct calculation_type
  80. {
  81. typedef typename util::calculation_type::geometric::binary
  82. <
  83. Point, Box, CalculationType
  84. >::type type;
  85. };
  86. template <typename Point, typename Box>
  87. static inline typename calculation_type<Point, Box>::type
  88. apply(Point const& point, Box const& box)
  89. {
  90. BOOST_CONCEPT_ASSERT( (concepts::ConstPoint<Point>) );
  91. BOOST_CONCEPT_ASSERT
  92. ( (concepts::ConstPoint<typename point_type<Box>::type>) );
  93. // Calculate distance using Pythagoras
  94. // (Leave comment above for Doxygen)
  95. assert_dimension_equal<Point, Box>();
  96. typename calculation_type<Point, Box>::type result(0);
  97. detail::compute_pythagoras_point_box
  98. <
  99. dimension<Point>::value
  100. >::apply(point, box, result);
  101. return result;
  102. }
  103. };
  104. } // namespace comparable
  105. /*!
  106. \brief Strategy to calculate the distance between a point and a box
  107. \ingroup strategies
  108. \tparam CalculationType \tparam_calculation
  109. \qbk{
  110. [heading Notes]
  111. [note Can be used for points and boxes with two\, three or more dimensions]
  112. [heading See also]
  113. [link geometry.reference.algorithms.distance.distance_3_with_strategy distance (with strategy)]
  114. }
  115. */
  116. template
  117. <
  118. typename CalculationType = void
  119. >
  120. class pythagoras_point_box
  121. {
  122. public :
  123. template <typename Point, typename Box>
  124. struct calculation_type
  125. : util::calculation_type::geometric::binary
  126. <
  127. Point,
  128. Box,
  129. CalculationType,
  130. double,
  131. double // promote integer to double
  132. >
  133. {};
  134. /*!
  135. \brief applies the distance calculation using pythagoras
  136. \return the calculated distance (including taking the square root)
  137. \param point point
  138. \param box box
  139. */
  140. template <typename Point, typename Box>
  141. static inline typename calculation_type<Point, Box>::type
  142. apply(Point const& point, Box const& box)
  143. {
  144. // The cast is necessary for MSVC which considers sqrt __int64 as an ambiguous call
  145. return math::sqrt
  146. (
  147. util::numeric_cast<typename calculation_type
  148. <
  149. Point, Box
  150. >::type>
  151. (
  152. comparable::pythagoras_point_box
  153. <
  154. CalculationType
  155. >::apply(point, box)
  156. )
  157. );
  158. }
  159. };
  160. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  161. namespace services
  162. {
  163. template <typename CalculationType>
  164. struct tag<pythagoras_point_box<CalculationType> >
  165. {
  166. typedef strategy_tag_distance_point_box type;
  167. };
  168. template <typename CalculationType, typename Point, typename Box>
  169. struct return_type<distance::pythagoras_point_box<CalculationType>, Point, Box>
  170. : pythagoras_point_box
  171. <
  172. CalculationType
  173. >::template calculation_type<Point, Box>
  174. {};
  175. template <typename CalculationType>
  176. struct comparable_type<pythagoras_point_box<CalculationType> >
  177. {
  178. typedef comparable::pythagoras_point_box<CalculationType> type;
  179. };
  180. template <typename CalculationType>
  181. struct get_comparable<pythagoras_point_box<CalculationType> >
  182. {
  183. typedef comparable::pythagoras_point_box<CalculationType> comparable_type;
  184. public :
  185. static inline comparable_type
  186. apply(pythagoras_point_box<CalculationType> const& )
  187. {
  188. return comparable_type();
  189. }
  190. };
  191. template <typename CalculationType, typename Point, typename Box>
  192. struct result_from_distance<pythagoras_point_box<CalculationType>, Point, Box>
  193. {
  194. private :
  195. typedef typename return_type
  196. <
  197. pythagoras_point_box<CalculationType>, Point, Box
  198. >::type return_type;
  199. public :
  200. template <typename T>
  201. static inline return_type
  202. apply(pythagoras_point_box<CalculationType> const& , T const& value)
  203. {
  204. return return_type(value);
  205. }
  206. };
  207. // Specializations for comparable::pythagoras_point_box
  208. template <typename CalculationType>
  209. struct tag<comparable::pythagoras_point_box<CalculationType> >
  210. {
  211. typedef strategy_tag_distance_point_box type;
  212. };
  213. template <typename CalculationType, typename Point, typename Box>
  214. struct return_type
  215. <
  216. comparable::pythagoras_point_box<CalculationType>, Point, Box
  217. > : comparable::pythagoras_point_box
  218. <
  219. CalculationType
  220. >::template calculation_type<Point, Box>
  221. {};
  222. template <typename CalculationType>
  223. struct comparable_type<comparable::pythagoras_point_box<CalculationType> >
  224. {
  225. typedef comparable::pythagoras_point_box<CalculationType> type;
  226. };
  227. template <typename CalculationType>
  228. struct get_comparable<comparable::pythagoras_point_box<CalculationType> >
  229. {
  230. typedef comparable::pythagoras_point_box<CalculationType> comparable_type;
  231. public :
  232. static inline comparable_type apply(comparable_type const& )
  233. {
  234. return comparable_type();
  235. }
  236. };
  237. template <typename CalculationType, typename Point, typename Box>
  238. struct result_from_distance
  239. <
  240. comparable::pythagoras_point_box<CalculationType>, Point, Box
  241. >
  242. {
  243. private :
  244. typedef typename return_type
  245. <
  246. comparable::pythagoras_point_box<CalculationType>, Point, Box
  247. >::type return_type;
  248. public :
  249. template <typename T>
  250. static inline return_type
  251. apply(comparable::pythagoras_point_box<CalculationType> const& ,
  252. T const& value)
  253. {
  254. return_type const v = value;
  255. return v * v;
  256. }
  257. };
  258. template <typename Point, typename BoxPoint>
  259. struct default_strategy
  260. <
  261. point_tag, box_tag, Point, BoxPoint, cartesian_tag, cartesian_tag
  262. >
  263. {
  264. typedef pythagoras_point_box<> type;
  265. };
  266. template <typename BoxPoint, typename Point>
  267. struct default_strategy
  268. <
  269. box_tag, point_tag, BoxPoint, Point, cartesian_tag, cartesian_tag
  270. >
  271. {
  272. typedef typename default_strategy
  273. <
  274. point_tag, box_tag, Point, BoxPoint, cartesian_tag, cartesian_tag
  275. >::type type;
  276. };
  277. } // namespace services
  278. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  279. }} // namespace strategy::distance
  280. }} // namespace boost::geometry
  281. #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISTANCE_PYTHAGORAS_POINT_BOX_HPP