123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- #ifndef BOOST_GEOMETRY_STRATEGY_CARTESIAN_SIDE_BY_TRIANGLE_HPP
- #define BOOST_GEOMETRY_STRATEGY_CARTESIAN_SIDE_BY_TRIANGLE_HPP
- #include <type_traits>
- #include <boost/geometry/core/config.hpp>
- #include <boost/geometry/arithmetic/determinant.hpp>
- #include <boost/geometry/core/access.hpp>
- #include <boost/geometry/strategies/cartesian/point_in_point.hpp>
- #include <boost/geometry/strategies/compare.hpp>
- #include <boost/geometry/strategies/side.hpp>
- #include <boost/geometry/util/select_calculation_type.hpp>
- #include <boost/geometry/util/select_most_precise.hpp>
- namespace boost { namespace geometry
- {
- namespace strategy { namespace side
- {
- template <typename CalculationType = void>
- class side_by_triangle
- {
- template <typename Policy>
- struct eps_policy
- {
- eps_policy() {}
- template <typename Type>
- eps_policy(Type const& a, Type const& b, Type const& c, Type const& d)
- : policy(a, b, c, d)
- {}
- Policy policy;
- };
- struct eps_empty
- {
- eps_empty() {}
- template <typename Type>
- eps_empty(Type const&, Type const&, Type const&, Type const&) {}
- };
- public :
- using cs_tag = cartesian_tag;
-
-
-
-
-
- template
- <
- typename CoordinateType,
- typename PromotedType,
- typename P1,
- typename P2,
- typename P,
- typename EpsPolicy
- >
- static inline
- PromotedType side_value(P1 const& p1, P2 const& p2, P const& p, EpsPolicy & eps_policy)
- {
- CoordinateType const x = get<0>(p);
- CoordinateType const y = get<1>(p);
- CoordinateType const sx1 = get<0>(p1);
- CoordinateType const sy1 = get<1>(p1);
- CoordinateType const sx2 = get<0>(p2);
- CoordinateType const sy2 = get<1>(p2);
- PromotedType const dx = sx2 - sx1;
- PromotedType const dy = sy2 - sy1;
- PromotedType const dpx = x - sx1;
- PromotedType const dpy = y - sy1;
- eps_policy = EpsPolicy(dx, dy, dpx, dpy);
- return geometry::detail::determinant<PromotedType>
- (
- dx, dy,
- dpx, dpy
- );
- }
- template
- <
- typename CoordinateType,
- typename PromotedType,
- typename P1,
- typename P2,
- typename P
- >
- static inline
- PromotedType side_value(P1 const& p1, P2 const& p2, P const& p)
- {
- eps_empty dummy;
- return side_value<CoordinateType, PromotedType>(p1, p2, p, dummy);
- }
- template
- <
- typename CoordinateType,
- typename PromotedType,
- bool AreAllIntegralCoordinates
- >
- struct compute_side_value
- {
- template <typename P1, typename P2, typename P, typename EpsPolicy>
- static inline PromotedType apply(P1 const& p1, P2 const& p2, P const& p, EpsPolicy & epsp)
- {
- return side_value<CoordinateType, PromotedType>(p1, p2, p, epsp);
- }
- };
- template <typename CoordinateType, typename PromotedType>
- struct compute_side_value<CoordinateType, PromotedType, false>
- {
- template <typename P1, typename P2, typename P, typename EpsPolicy>
- static inline PromotedType apply(P1 const& p1, P2 const& p2, P const& p, EpsPolicy & epsp)
- {
-
-
-
- if (equals_point_point(p1, p2)
- || equals_point_point(p1, p)
- || equals_point_point(p2, p))
- {
- return PromotedType(0);
- }
-
-
-
-
-
-
-
-
-
- using less = compare::cartesian<compare::less, compare::equals_epsilon>;
- if (less::apply(p, p1))
- {
- if (less::apply(p, p2))
- {
-
- return side_value<CoordinateType, PromotedType>(p, p1, p2, epsp);
- }
- else
- {
-
- return side_value<CoordinateType, PromotedType>(p2, p, p1, epsp);
- }
- }
- if (less::apply(p1, p2))
- {
-
- return side_value<CoordinateType, PromotedType>(p1, p2, p, epsp);
- }
- else
- {
-
- return side_value<CoordinateType, PromotedType>(p2, p, p1, epsp);
- }
- }
- };
- template <typename P1, typename P2, typename P>
- static inline int apply(P1 const& p1, P2 const& p2, P const& p)
- {
- using coor_t = typename select_calculation_type_alt<CalculationType, P1, P2, P>::type;
-
- using promoted_t = typename select_most_precise<coor_t, double>::type;
- bool const are_all_integral_coordinates =
- std::is_integral<typename coordinate_type<P1>::type>::value
- && std::is_integral<typename coordinate_type<P2>::type>::value
- && std::is_integral<typename coordinate_type<P>::type>::value;
- eps_policy< math::detail::equals_factor_policy<promoted_t> > epsp;
- promoted_t s = compute_side_value
- <
- coor_t, promoted_t, are_all_integral_coordinates
- >::apply(p1, p2, p, epsp);
- promoted_t const zero = promoted_t();
- return math::detail::equals_by_policy(s, zero, epsp.policy) ? 0
- : s > zero ? 1
- : -1;
- }
- private:
- template <typename P1, typename P2>
- static inline bool equals_point_point(P1 const& p1, P2 const& p2)
- {
- return strategy::within::cartesian_point_point::apply(p1, p2);
- }
- };
- #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
- namespace services
- {
- template <typename CalculationType>
- struct default_strategy<cartesian_tag, CalculationType>
- {
- using type = side_by_triangle<CalculationType>;
- };
- }
- #endif
- }}
- }}
- #endif
|