]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Boost.Geometry (aka GGL, Generic Geometry Library) |
2 | ||
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. | |
6 | ||
7 | // This file was modified by Oracle on 2014. | |
8 | // Modifications copyright (c) 2014, Oracle and/or its affiliates. | |
9 | ||
10 | // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle | |
11 | ||
12 | // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library | |
13 | // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands. | |
14 | ||
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) | |
18 | ||
19 | #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISTANCE_PYTHAGORAS_POINT_BOX_HPP | |
20 | #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISTANCE_PYTHAGORAS_POINT_BOX_HPP | |
21 | ||
22 | ||
23 | #include <boost/geometry/core/access.hpp> | |
24 | ||
25 | #include <boost/geometry/strategies/distance.hpp> | |
26 | ||
27 | #include <boost/geometry/util/math.hpp> | |
28 | #include <boost/geometry/util/calculation_type.hpp> | |
29 | ||
30 | ||
31 | ||
32 | namespace boost { namespace geometry | |
33 | { | |
34 | ||
35 | namespace strategy { namespace distance | |
36 | { | |
37 | ||
38 | #ifndef DOXYGEN_NO_DETAIL | |
39 | namespace detail | |
40 | { | |
41 | ||
42 | template <size_t I> | |
43 | struct compute_pythagoras_point_box | |
44 | { | |
45 | template <typename Point, typename Box, typename T> | |
46 | static inline void apply(Point const& point, Box const& box, T& result) | |
47 | { | |
48 | T const p_coord = boost::numeric_cast<T>(geometry::get<I-1>(point)); | |
49 | T const b_min_coord = | |
50 | boost::numeric_cast<T>(geometry::get<min_corner, I-1>(box)); | |
51 | T const b_max_coord = | |
52 | boost::numeric_cast<T>(geometry::get<max_corner, I-1>(box)); | |
53 | ||
54 | if ( p_coord < b_min_coord ) | |
55 | { | |
56 | T diff = b_min_coord - p_coord; | |
57 | result += diff * diff; | |
58 | } | |
59 | if ( p_coord > b_max_coord ) | |
60 | { | |
61 | T diff = p_coord - b_max_coord; | |
62 | result += diff * diff; | |
63 | } | |
64 | ||
65 | compute_pythagoras_point_box<I-1>::apply(point, box, result); | |
66 | } | |
67 | }; | |
68 | ||
69 | template <> | |
70 | struct compute_pythagoras_point_box<0> | |
71 | { | |
72 | template <typename Point, typename Box, typename T> | |
73 | static inline void apply(Point const&, Box const&, T&) | |
74 | { | |
75 | } | |
76 | }; | |
77 | ||
78 | ||
79 | } // namespace detail | |
80 | #endif // DOXYGEN_NO_DETAIL | |
81 | ||
82 | ||
83 | namespace comparable | |
84 | { | |
85 | ||
86 | /*! | |
87 | \brief Strategy to calculate comparable distance between a point | |
88 | and a box | |
89 | \ingroup strategies | |
90 | \tparam Point \tparam_first_point | |
91 | \tparam Box \tparam_second_box | |
92 | \tparam CalculationType \tparam_calculation | |
93 | */ | |
94 | template <typename CalculationType = void> | |
95 | class pythagoras_point_box | |
96 | { | |
97 | public : | |
98 | ||
99 | template <typename Point, typename Box> | |
100 | struct calculation_type | |
101 | { | |
102 | typedef typename util::calculation_type::geometric::binary | |
103 | < | |
104 | Point, Box, CalculationType | |
105 | >::type type; | |
106 | }; | |
107 | ||
108 | template <typename Point, typename Box> | |
109 | static inline typename calculation_type<Point, Box>::type | |
110 | apply(Point const& point, Box const& box) | |
111 | { | |
112 | BOOST_CONCEPT_ASSERT( (concepts::ConstPoint<Point>) ); | |
113 | BOOST_CONCEPT_ASSERT | |
114 | ( (concepts::ConstPoint<typename point_type<Box>::type>) ); | |
115 | ||
116 | // Calculate distance using Pythagoras | |
117 | // (Leave comment above for Doxygen) | |
118 | ||
119 | assert_dimension_equal<Point, Box>(); | |
120 | ||
121 | typename calculation_type<Point, Box>::type result(0); | |
122 | ||
123 | detail::compute_pythagoras_point_box | |
124 | < | |
125 | dimension<Point>::value | |
126 | >::apply(point, box, result); | |
127 | ||
128 | return result; | |
129 | } | |
130 | }; | |
131 | ||
132 | } // namespace comparable | |
133 | ||
134 | ||
135 | /*! | |
136 | \brief Strategy to calculate the distance between a point and a box | |
137 | \ingroup strategies | |
138 | \tparam CalculationType \tparam_calculation | |
139 | ||
140 | \qbk{ | |
141 | [heading Notes] | |
142 | [note Can be used for points and boxes with two\, three or more dimensions] | |
143 | [heading See also] | |
144 | [link geometry.reference.algorithms.distance.distance_3_with_strategy distance (with strategy)] | |
145 | } | |
146 | ||
147 | */ | |
148 | template | |
149 | < | |
150 | typename CalculationType = void | |
151 | > | |
152 | class pythagoras_point_box | |
153 | { | |
154 | public : | |
155 | ||
156 | template <typename Point, typename Box> | |
157 | struct calculation_type | |
158 | : util::calculation_type::geometric::binary | |
159 | < | |
160 | Point, | |
161 | Box, | |
162 | CalculationType, | |
163 | double, | |
164 | double // promote integer to double | |
165 | > | |
166 | {}; | |
167 | ||
168 | /*! | |
169 | \brief applies the distance calculation using pythagoras | |
170 | \return the calculated distance (including taking the square root) | |
171 | \param point point | |
172 | \param box box | |
173 | */ | |
174 | template <typename Point, typename Box> | |
175 | static inline typename calculation_type<Point, Box>::type | |
176 | apply(Point const& point, Box const& box) | |
177 | { | |
178 | // The cast is necessary for MSVC which considers sqrt __int64 as an ambiguous call | |
179 | return math::sqrt | |
180 | ( | |
181 | boost::numeric_cast<typename calculation_type | |
182 | < | |
183 | Point, Box | |
184 | >::type> | |
185 | ( | |
186 | comparable::pythagoras_point_box | |
187 | < | |
188 | CalculationType | |
189 | >::apply(point, box) | |
190 | ) | |
191 | ); | |
192 | } | |
193 | }; | |
194 | ||
195 | ||
196 | #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS | |
197 | namespace services | |
198 | { | |
199 | ||
200 | template <typename CalculationType> | |
201 | struct tag<pythagoras_point_box<CalculationType> > | |
202 | { | |
203 | typedef strategy_tag_distance_point_box type; | |
204 | }; | |
205 | ||
206 | ||
207 | template <typename CalculationType, typename Point, typename Box> | |
208 | struct return_type<distance::pythagoras_point_box<CalculationType>, Point, Box> | |
209 | : pythagoras_point_box | |
210 | < | |
211 | CalculationType | |
212 | >::template calculation_type<Point, Box> | |
213 | {}; | |
214 | ||
215 | ||
216 | template <typename CalculationType> | |
217 | struct comparable_type<pythagoras_point_box<CalculationType> > | |
218 | { | |
219 | typedef comparable::pythagoras_point_box<CalculationType> type; | |
220 | }; | |
221 | ||
222 | ||
223 | template <typename CalculationType> | |
224 | struct get_comparable<pythagoras_point_box<CalculationType> > | |
225 | { | |
226 | typedef comparable::pythagoras_point_box<CalculationType> comparable_type; | |
227 | public : | |
228 | static inline comparable_type | |
229 | apply(pythagoras_point_box<CalculationType> const& ) | |
230 | { | |
231 | return comparable_type(); | |
232 | } | |
233 | }; | |
234 | ||
235 | ||
236 | template <typename CalculationType, typename Point, typename Box> | |
237 | struct result_from_distance<pythagoras_point_box<CalculationType>, Point, Box> | |
238 | { | |
239 | private : | |
240 | typedef typename return_type | |
241 | < | |
242 | pythagoras_point_box<CalculationType>, Point, Box | |
243 | >::type return_type; | |
244 | public : | |
245 | template <typename T> | |
246 | static inline return_type | |
247 | apply(pythagoras_point_box<CalculationType> const& , T const& value) | |
248 | { | |
249 | return return_type(value); | |
250 | } | |
251 | }; | |
252 | ||
253 | ||
254 | // Specializations for comparable::pythagoras_point_box | |
255 | template <typename CalculationType> | |
256 | struct tag<comparable::pythagoras_point_box<CalculationType> > | |
257 | { | |
258 | typedef strategy_tag_distance_point_box type; | |
259 | }; | |
260 | ||
261 | ||
262 | template <typename CalculationType, typename Point, typename Box> | |
263 | struct return_type | |
264 | < | |
265 | comparable::pythagoras_point_box<CalculationType>, Point, Box | |
266 | > : comparable::pythagoras_point_box | |
267 | < | |
268 | CalculationType | |
269 | >::template calculation_type<Point, Box> | |
270 | {}; | |
271 | ||
272 | ||
273 | ||
274 | ||
275 | template <typename CalculationType> | |
276 | struct comparable_type<comparable::pythagoras_point_box<CalculationType> > | |
277 | { | |
278 | typedef comparable::pythagoras_point_box<CalculationType> type; | |
279 | }; | |
280 | ||
281 | ||
282 | template <typename CalculationType> | |
283 | struct get_comparable<comparable::pythagoras_point_box<CalculationType> > | |
284 | { | |
285 | typedef comparable::pythagoras_point_box<CalculationType> comparable_type; | |
286 | public : | |
287 | static inline comparable_type apply(comparable_type const& ) | |
288 | { | |
289 | return comparable_type(); | |
290 | } | |
291 | }; | |
292 | ||
293 | ||
294 | template <typename CalculationType, typename Point, typename Box> | |
295 | struct result_from_distance | |
296 | < | |
297 | comparable::pythagoras_point_box<CalculationType>, Point, Box | |
298 | > | |
299 | { | |
300 | private : | |
301 | typedef typename return_type | |
302 | < | |
303 | comparable::pythagoras_point_box<CalculationType>, Point, Box | |
304 | >::type return_type; | |
305 | public : | |
306 | template <typename T> | |
307 | static inline return_type | |
308 | apply(comparable::pythagoras_point_box<CalculationType> const& , | |
309 | T const& value) | |
310 | { | |
311 | return_type const v = value; | |
312 | return v * v; | |
313 | } | |
314 | }; | |
315 | ||
316 | ||
317 | template <typename Point, typename BoxPoint> | |
318 | struct default_strategy | |
319 | < | |
320 | point_tag, box_tag, Point, BoxPoint, cartesian_tag, cartesian_tag | |
321 | > | |
322 | { | |
323 | typedef pythagoras_point_box<> type; | |
324 | }; | |
325 | ||
326 | template <typename BoxPoint, typename Point> | |
327 | struct default_strategy | |
328 | < | |
329 | box_tag, point_tag, BoxPoint, Point, cartesian_tag, cartesian_tag | |
330 | > | |
331 | { | |
332 | typedef typename default_strategy | |
333 | < | |
334 | point_tag, box_tag, Point, BoxPoint, cartesian_tag, cartesian_tag | |
335 | >::type type; | |
336 | }; | |
337 | ||
338 | ||
339 | } // namespace services | |
340 | #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS | |
341 | ||
342 | ||
343 | }} // namespace strategy::distance | |
344 | ||
345 | ||
346 | }} // namespace boost::geometry | |
347 | ||
348 | ||
349 | #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISTANCE_PYTHAGORAS_POINT_BOX_HPP |