123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353 |
- #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISTANCE_PYTHAGORAS_POINT_BOX_HPP
- #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISTANCE_PYTHAGORAS_POINT_BOX_HPP
- #include <boost/geometry/core/access.hpp>
- #include <boost/geometry/core/point_type.hpp>
- #include <boost/geometry/geometries/concepts/point_concept.hpp>
- #include <boost/geometry/strategies/distance.hpp>
- #include <boost/geometry/util/calculation_type.hpp>
- #include <boost/geometry/util/math.hpp>
- #include <boost/geometry/util/numeric_cast.hpp>
- namespace boost { namespace geometry
- {
- namespace strategy { namespace distance
- {
- #ifndef DOXYGEN_NO_DETAIL
- namespace detail
- {
- template <size_t I>
- struct compute_pythagoras_point_box
- {
- template <typename Point, typename Box, typename T>
- static inline void apply(Point const& point, Box const& box, T& result)
- {
- T const p_coord = util::numeric_cast<T>(geometry::get<I-1>(point));
- T const b_min_coord =
- util::numeric_cast<T>(geometry::get<min_corner, I-1>(box));
- T const b_max_coord =
- util::numeric_cast<T>(geometry::get<max_corner, I-1>(box));
- if ( p_coord < b_min_coord )
- {
- T diff = b_min_coord - p_coord;
- result += diff * diff;
- }
- if ( p_coord > b_max_coord )
- {
- T diff = p_coord - b_max_coord;
- result += diff * diff;
- }
- compute_pythagoras_point_box<I-1>::apply(point, box, result);
- }
- };
- template <>
- struct compute_pythagoras_point_box<0>
- {
- template <typename Point, typename Box, typename T>
- static inline void apply(Point const&, Box const&, T&)
- {
- }
- };
- }
- #endif
- namespace comparable
- {
- template <typename CalculationType = void>
- class pythagoras_point_box
- {
- public :
- template <typename Point, typename Box>
- struct calculation_type
- {
- typedef typename util::calculation_type::geometric::binary
- <
- Point, Box, CalculationType
- >::type type;
- };
- template <typename Point, typename Box>
- static inline typename calculation_type<Point, Box>::type
- apply(Point const& point, Box const& box)
- {
- BOOST_CONCEPT_ASSERT( (concepts::ConstPoint<Point>) );
- BOOST_CONCEPT_ASSERT
- ( (concepts::ConstPoint<typename point_type<Box>::type>) );
-
-
- assert_dimension_equal<Point, Box>();
- typename calculation_type<Point, Box>::type result(0);
- detail::compute_pythagoras_point_box
- <
- dimension<Point>::value
- >::apply(point, box, result);
- return result;
- }
- };
- }
- template
- <
- typename CalculationType = void
- >
- class pythagoras_point_box
- {
- public :
- template <typename Point, typename Box>
- struct calculation_type
- : util::calculation_type::geometric::binary
- <
- Point,
- Box,
- CalculationType,
- double,
- double
- >
- {};
-
- template <typename Point, typename Box>
- static inline typename calculation_type<Point, Box>::type
- apply(Point const& point, Box const& box)
- {
-
- return math::sqrt
- (
- util::numeric_cast<typename calculation_type
- <
- Point, Box
- >::type>
- (
- comparable::pythagoras_point_box
- <
- CalculationType
- >::apply(point, box)
- )
- );
- }
- };
- #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
- namespace services
- {
- template <typename CalculationType>
- struct tag<pythagoras_point_box<CalculationType> >
- {
- typedef strategy_tag_distance_point_box type;
- };
- template <typename CalculationType, typename Point, typename Box>
- struct return_type<distance::pythagoras_point_box<CalculationType>, Point, Box>
- : pythagoras_point_box
- <
- CalculationType
- >::template calculation_type<Point, Box>
- {};
- template <typename CalculationType>
- struct comparable_type<pythagoras_point_box<CalculationType> >
- {
- typedef comparable::pythagoras_point_box<CalculationType> type;
- };
- template <typename CalculationType>
- struct get_comparable<pythagoras_point_box<CalculationType> >
- {
- typedef comparable::pythagoras_point_box<CalculationType> comparable_type;
- public :
- static inline comparable_type
- apply(pythagoras_point_box<CalculationType> const& )
- {
- return comparable_type();
- }
- };
- template <typename CalculationType, typename Point, typename Box>
- struct result_from_distance<pythagoras_point_box<CalculationType>, Point, Box>
- {
- private :
- typedef typename return_type
- <
- pythagoras_point_box<CalculationType>, Point, Box
- >::type return_type;
- public :
- template <typename T>
- static inline return_type
- apply(pythagoras_point_box<CalculationType> const& , T const& value)
- {
- return return_type(value);
- }
- };
- template <typename CalculationType>
- struct tag<comparable::pythagoras_point_box<CalculationType> >
- {
- typedef strategy_tag_distance_point_box type;
- };
- template <typename CalculationType, typename Point, typename Box>
- struct return_type
- <
- comparable::pythagoras_point_box<CalculationType>, Point, Box
- > : comparable::pythagoras_point_box
- <
- CalculationType
- >::template calculation_type<Point, Box>
- {};
- template <typename CalculationType>
- struct comparable_type<comparable::pythagoras_point_box<CalculationType> >
- {
- typedef comparable::pythagoras_point_box<CalculationType> type;
- };
- template <typename CalculationType>
- struct get_comparable<comparable::pythagoras_point_box<CalculationType> >
- {
- typedef comparable::pythagoras_point_box<CalculationType> comparable_type;
- public :
- static inline comparable_type apply(comparable_type const& )
- {
- return comparable_type();
- }
- };
- template <typename CalculationType, typename Point, typename Box>
- struct result_from_distance
- <
- comparable::pythagoras_point_box<CalculationType>, Point, Box
- >
- {
- private :
- typedef typename return_type
- <
- comparable::pythagoras_point_box<CalculationType>, Point, Box
- >::type return_type;
- public :
- template <typename T>
- static inline return_type
- apply(comparable::pythagoras_point_box<CalculationType> const& ,
- T const& value)
- {
- return_type const v = value;
- return v * v;
- }
- };
- template <typename Point, typename BoxPoint>
- struct default_strategy
- <
- point_tag, box_tag, Point, BoxPoint, cartesian_tag, cartesian_tag
- >
- {
- typedef pythagoras_point_box<> type;
- };
- template <typename BoxPoint, typename Point>
- struct default_strategy
- <
- box_tag, point_tag, BoxPoint, Point, cartesian_tag, cartesian_tag
- >
- {
- typedef typename default_strategy
- <
- point_tag, box_tag, Point, BoxPoint, cartesian_tag, cartesian_tag
- >::type type;
- };
- }
- #endif
- }}
- }}
- #endif
|