1 // Boost.Geometry (aka GGL, Generic Geometry Library)
3 // Copyright (c) 2008-2014 Bruno Lalande, Paris, France.
4 // Copyright (c) 2008-2014 Barend Gehrels, Amsterdam, the Netherlands.
5 // Copyright (c) 2009-2014 Mateusz Loskot, London, UK.
7 // This file was modified by Oracle on 2014.
8 // Modifications copyright (c) 2014, Oracle and/or its affiliates.
10 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
12 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
13 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
15 // Use, modification and distribution is subject to the Boost Software License,
16 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
17 // http://www.boost.org/LICENSE_1_0.txt)
19 #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISTANCE_PYTHAGORAS_POINT_BOX_HPP
20 #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISTANCE_PYTHAGORAS_POINT_BOX_HPP
23 #include <boost/geometry/core/access.hpp>
25 #include <boost/geometry/strategies/distance.hpp>
27 #include <boost/geometry/util/math.hpp>
28 #include <boost/geometry/util/calculation_type.hpp>
32 namespace boost { namespace geometry
35 namespace strategy { namespace distance
38 #ifndef DOXYGEN_NO_DETAIL
43 struct compute_pythagoras_point_box
45 template <typename Point, typename Box, typename T>
46 static inline void apply(Point const& point, Box const& box, T& result)
48 T const p_coord = boost::numeric_cast<T>(geometry::get<I-1>(point));
50 boost::numeric_cast<T>(geometry::get<min_corner, I-1>(box));
52 boost::numeric_cast<T>(geometry::get<max_corner, I-1>(box));
54 if ( p_coord < b_min_coord )
56 T diff = b_min_coord - p_coord;
57 result += diff * diff;
59 if ( p_coord > b_max_coord )
61 T diff = p_coord - b_max_coord;
62 result += diff * diff;
65 compute_pythagoras_point_box<I-1>::apply(point, box, result);
70 struct compute_pythagoras_point_box<0>
72 template <typename Point, typename Box, typename T>
73 static inline void apply(Point const&, Box const&, T&)
80 #endif // DOXYGEN_NO_DETAIL
87 \brief Strategy to calculate comparable distance between a point
90 \tparam Point \tparam_first_point
91 \tparam Box \tparam_second_box
92 \tparam CalculationType \tparam_calculation
94 template <typename CalculationType = void>
95 class pythagoras_point_box
99 template <typename Point, typename Box>
100 struct calculation_type
102 typedef typename util::calculation_type::geometric::binary
104 Point, Box, CalculationType
108 template <typename Point, typename Box>
109 static inline typename calculation_type<Point, Box>::type
110 apply(Point const& point, Box const& box)
112 BOOST_CONCEPT_ASSERT( (concepts::ConstPoint<Point>) );
114 ( (concepts::ConstPoint<typename point_type<Box>::type>) );
116 // Calculate distance using Pythagoras
117 // (Leave comment above for Doxygen)
119 assert_dimension_equal<Point, Box>();
121 typename calculation_type<Point, Box>::type result(0);
123 detail::compute_pythagoras_point_box
125 dimension<Point>::value
126 >::apply(point, box, result);
132 } // namespace comparable
136 \brief Strategy to calculate the distance between a point and a box
138 \tparam CalculationType \tparam_calculation
142 [note Can be used for points and boxes with two\, three or more dimensions]
144 [link geometry.reference.algorithms.distance.distance_3_with_strategy distance (with strategy)]
150 typename CalculationType = void
152 class pythagoras_point_box
156 template <typename Point, typename Box>
157 struct calculation_type
158 : util::calculation_type::geometric::binary
164 double // promote integer to double
169 \brief applies the distance calculation using pythagoras
170 \return the calculated distance (including taking the square root)
174 template <typename Point, typename Box>
175 static inline typename calculation_type<Point, Box>::type
176 apply(Point const& point, Box const& box)
178 // The cast is necessary for MSVC which considers sqrt __int64 as an ambiguous call
181 boost::numeric_cast<typename calculation_type
186 comparable::pythagoras_point_box
196 #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
200 template <typename CalculationType>
201 struct tag<pythagoras_point_box<CalculationType> >
203 typedef strategy_tag_distance_point_box type;
207 template <typename CalculationType, typename Point, typename Box>
208 struct return_type<distance::pythagoras_point_box<CalculationType>, Point, Box>
209 : pythagoras_point_box
212 >::template calculation_type<Point, Box>
216 template <typename CalculationType>
217 struct comparable_type<pythagoras_point_box<CalculationType> >
219 typedef comparable::pythagoras_point_box<CalculationType> type;
223 template <typename CalculationType>
224 struct get_comparable<pythagoras_point_box<CalculationType> >
226 typedef comparable::pythagoras_point_box<CalculationType> comparable_type;
228 static inline comparable_type
229 apply(pythagoras_point_box<CalculationType> const& )
231 return comparable_type();
236 template <typename CalculationType, typename Point, typename Box>
237 struct result_from_distance<pythagoras_point_box<CalculationType>, Point, Box>
240 typedef typename return_type
242 pythagoras_point_box<CalculationType>, Point, Box
245 template <typename T>
246 static inline return_type
247 apply(pythagoras_point_box<CalculationType> const& , T const& value)
249 return return_type(value);
254 // Specializations for comparable::pythagoras_point_box
255 template <typename CalculationType>
256 struct tag<comparable::pythagoras_point_box<CalculationType> >
258 typedef strategy_tag_distance_point_box type;
262 template <typename CalculationType, typename Point, typename Box>
265 comparable::pythagoras_point_box<CalculationType>, Point, Box
266 > : comparable::pythagoras_point_box
269 >::template calculation_type<Point, Box>
275 template <typename CalculationType>
276 struct comparable_type<comparable::pythagoras_point_box<CalculationType> >
278 typedef comparable::pythagoras_point_box<CalculationType> type;
282 template <typename CalculationType>
283 struct get_comparable<comparable::pythagoras_point_box<CalculationType> >
285 typedef comparable::pythagoras_point_box<CalculationType> comparable_type;
287 static inline comparable_type apply(comparable_type const& )
289 return comparable_type();
294 template <typename CalculationType, typename Point, typename Box>
295 struct result_from_distance
297 comparable::pythagoras_point_box<CalculationType>, Point, Box
301 typedef typename return_type
303 comparable::pythagoras_point_box<CalculationType>, Point, Box
306 template <typename T>
307 static inline return_type
308 apply(comparable::pythagoras_point_box<CalculationType> const& ,
311 return_type const v = value;
317 template <typename Point, typename BoxPoint>
318 struct default_strategy
320 point_tag, box_tag, Point, BoxPoint, cartesian_tag, cartesian_tag
323 typedef pythagoras_point_box<> type;
326 template <typename BoxPoint, typename Point>
327 struct default_strategy
329 box_tag, point_tag, BoxPoint, Point, cartesian_tag, cartesian_tag
332 typedef typename default_strategy
334 point_tag, box_tag, Point, BoxPoint, cartesian_tag, cartesian_tag
339 } // namespace services
340 #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
343 }} // namespace strategy::distance
346 }} // namespace boost::geometry
349 #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISTANCE_PYTHAGORAS_POINT_BOX_HPP