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