123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_CLOSEST_POINTS_PT_SEG_HPP
- #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_CLOSEST_POINTS_PT_SEG_HPP
- #include <boost/geometry/algorithms/convert.hpp>
- #include <boost/geometry/core/coordinate_promotion.hpp>
- #include <boost/geometry/geometries/point.hpp>
- #include <boost/geometry/strategies/cartesian/distance_pythagoras.hpp>
- #include <boost/geometry/strategies/closest_points/services.hpp>
- namespace boost { namespace geometry
- {
- namespace strategy { namespace closest_points
- {
- #ifndef DOXYGEN_NO_DETAIL
- namespace detail
- {
- template <typename CalculationType>
- struct compute_closest_point_to_segment
- {
- template <typename Point, typename PointOfSegment>
- static inline auto
- apply(Point const& p, PointOfSegment const& p1, PointOfSegment const& p2)
- {
-
-
- using fp_point_type = model::point
- <
- CalculationType,
- dimension<PointOfSegment>::value,
- typename coordinate_system<PointOfSegment>::type
- >;
-
- using fp_vector_type = fp_point_type;
-
-
-
- fp_vector_type v, w, projected;
- geometry::convert(p2, v);
- geometry::convert(p, w);
- geometry::convert(p1, projected);
- subtract_point(v, projected);
- subtract_point(w, projected);
- CalculationType const zero = CalculationType();
- CalculationType const c1 = dot_product(w, v);
- if (c1 <= zero)
- {
- fp_vector_type fp_p1;
- geometry::convert(p1, fp_p1);
- return fp_p1;
- }
- CalculationType const c2 = dot_product(v, v);
- if (c2 <= c1)
- {
- fp_vector_type fp_p2;
- geometry::convert(p2, fp_p2);
- return fp_p2;
- }
-
- CalculationType const b = c1 / c2;
- multiply_value(v, b);
- add_point(projected, v);
- return projected;
- }
- };
- }
- #endif
- template
- <
- typename CalculationType = void
- >
- class projected_point
- {
- public:
-
-
-
-
-
- template <typename Point, typename PointOfSegment>
- struct calculation_type
- : promote_floating_point
- <
- typename select_most_precise
- <
- typename coordinate_type<Point>::type,
- typename coordinate_type<PointOfSegment>::type,
- CalculationType
- >::type
- >
- {};
- template <typename Point, typename PointOfSegment>
- inline auto
- apply(Point const& p, PointOfSegment const& p1, PointOfSegment const& p2) const
- {
- assert_dimension_equal<Point, PointOfSegment>();
- using calculation_type = typename calculation_type<Point, PointOfSegment>::type;
- return detail::compute_closest_point_to_segment<calculation_type>::apply(p, p1, p2);
- }
- };
- }}
- }}
- #endif
|