Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:36:43

0001 // Boost.Geometry (aka GGL, Generic Geometry Library)
0002 
0003 // Copyright (c) 2008-2014 Bruno Lalande, Paris, France.
0004 // Copyright (c) 2008-2014 Barend Gehrels, Amsterdam, the Netherlands.
0005 // Copyright (c) 2009-2014 Mateusz Loskot, London, UK.
0006 
0007 // This file was modified by Oracle on 2014.
0008 // Modifications copyright (c) 2014, Oracle and/or its affiliates.
0009 
0010 // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
0011 // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
0012 
0013 // Parts of Boost.Geometry are redesigned from Geodan's Geographic Library
0014 // (geolib/GGL), copyright (c) 1995-2010 Geodan, Amsterdam, the Netherlands.
0015 
0016 // Use, modification and distribution is subject to the Boost Software License,
0017 // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
0018 // http://www.boost.org/LICENSE_1_0.txt)
0019 
0020 #ifndef BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISTANCE_PROJECTED_POINT_AX_HPP
0021 #define BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISTANCE_PROJECTED_POINT_AX_HPP
0022 
0023 
0024 #include <algorithm>
0025 
0026 #include <boost/concept_check.hpp>
0027 #include <boost/core/ignore_unused.hpp>
0028 
0029 #include <boost/geometry/core/access.hpp>
0030 #include <boost/geometry/core/point_type.hpp>
0031 
0032 #include <boost/geometry/algorithms/convert.hpp>
0033 #include <boost/geometry/arithmetic/arithmetic.hpp>
0034 #include <boost/geometry/arithmetic/dot_product.hpp>
0035 
0036 #include <boost/geometry/strategies/tags.hpp>
0037 #include <boost/geometry/strategies/distance.hpp>
0038 #include <boost/geometry/strategies/default_distance_result.hpp>
0039 #include <boost/geometry/strategies/cartesian/distance_pythagoras.hpp>
0040 #include <boost/geometry/strategies/cartesian/distance_projected_point.hpp>
0041 
0042 #include <boost/geometry/util/select_coordinate_type.hpp>
0043 
0044 // Helper geometry (projected point on line)
0045 #include <boost/geometry/geometries/point.hpp>
0046 
0047 
0048 namespace boost { namespace geometry
0049 {
0050 
0051 
0052 namespace strategy { namespace distance
0053 {
0054 
0055 
0056 #ifndef DOXYGEN_NO_DETAIL
0057 namespace detail
0058 {
0059 
0060 template <typename T>
0061 struct projected_point_ax_result
0062 {
0063     typedef T value_type;
0064 
0065     projected_point_ax_result(T const& c = T(0))
0066         : atd(c), xtd(c)
0067     {}
0068 
0069     projected_point_ax_result(T const& a, T const& x)
0070         : atd(a), xtd(x)
0071     {}
0072 
0073     friend inline bool operator<(projected_point_ax_result const& left,
0074                                  projected_point_ax_result const& right)
0075     {
0076         return left.xtd < right.xtd || left.atd < right.atd;
0077     }
0078 
0079     T atd, xtd;
0080 };
0081 
0082 // This less-comparator may be used as a parameter of detail::douglas_peucker.
0083 // In this simplify strategy distances are compared in 2 places
0084 // 1. to choose the furthest candidate (md < dist)
0085 // 2. to check if the candidate is further than max_distance (max_distance < md)
0086 template <typename Distance>
0087 class projected_point_ax_less
0088 {
0089 public:
0090     projected_point_ax_less(Distance const& max_distance)
0091         : m_max_distance(max_distance)
0092     {}
0093 
0094     inline bool operator()(Distance const& left, Distance const& right) const
0095     {
0096         //return left.xtd < right.xtd && right.atd < m_max_distance.atd;
0097 
0098         typedef typename Distance::value_type value_type;
0099 
0100         value_type const lx = left.xtd > m_max_distance.xtd ? left.xtd - m_max_distance.xtd : 0;
0101         value_type const rx = right.xtd > m_max_distance.xtd ? right.xtd - m_max_distance.xtd : 0;
0102         value_type const la = left.atd > m_max_distance.atd ? left.atd - m_max_distance.atd : 0;
0103         value_type const ra = right.atd > m_max_distance.atd ? right.atd - m_max_distance.atd : 0;
0104 
0105         value_type const l = (std::max)(lx, la);
0106         value_type const r = (std::max)(rx, ra);
0107 
0108         return l < r;
0109     }
0110 private:
0111     Distance const& m_max_distance;
0112 };
0113 
0114 // This strategy returns 2-component Point/Segment distance.
0115 // The ATD (along track distance) is parallel to the Segment
0116 // and is a distance between Point projected into a line defined by a Segment and the nearest Segment's endpoint.
0117 // If the projected Point intersects the Segment the ATD is equal to 0.
0118 // The XTD (cross track distance) is perpendicular to the Segment
0119 // and is a distance between input Point and its projection.
0120 // If the Segment has length equal to 0, ATD and XTD has value equal
0121 // to the distance between the input Point and one of the Segment's endpoints.
0122 //
0123 //          p3         p4
0124 //          ^         7
0125 //          |        /
0126 // p1<-----e========e----->p2
0127 //
0128 // p1: atd=D,   xtd=0
0129 // p2: atd=D,   xtd=0
0130 // p3: atd=0,   xtd=D
0131 // p4: atd=D/2, xtd=D
0132 template
0133 <
0134     typename CalculationType = void,
0135     typename Strategy = pythagoras<CalculationType>
0136 >
0137 class projected_point_ax
0138 {
0139 public :
0140     template <typename Point, typename PointOfSegment>
0141     struct calculation_type
0142         : public projected_point<CalculationType, Strategy>
0143             ::template calculation_type<Point, PointOfSegment>
0144     {};
0145 
0146     template <typename Point, typename PointOfSegment>
0147     struct result_type
0148     {
0149         typedef projected_point_ax_result
0150                     <
0151                         typename calculation_type<Point, PointOfSegment>::type
0152                     > type;
0153     };
0154 
0155 public :
0156 
0157     template <typename Point, typename PointOfSegment>
0158     inline typename result_type<Point, PointOfSegment>::type
0159     apply(Point const& p, PointOfSegment const& p1, PointOfSegment const& p2) const
0160     {
0161         assert_dimension_equal<Point, PointOfSegment>();
0162 
0163         typedef typename calculation_type<Point, PointOfSegment>::type calculation_type;
0164 
0165         // A projected point of points in Integer coordinates must be able to be
0166         // represented in FP.
0167         typedef model::point
0168             <
0169                 calculation_type,
0170                 dimension<PointOfSegment>::value,
0171                 typename coordinate_system<PointOfSegment>::type
0172             > fp_point_type;
0173 
0174         // For convenience
0175         typedef fp_point_type fp_vector_type;
0176 
0177         /*
0178             Algorithm [p: (px,py), p1: (x1,y1), p2: (x2,y2)]
0179             VECTOR v(x2 - x1, y2 - y1)
0180             VECTOR w(px - x1, py - y1)
0181             c1 = w . v
0182             c2 = v . v
0183             b = c1 / c2
0184             RETURN POINT(x1 + b * vx, y1 + b * vy)
0185         */
0186 
0187         // v is multiplied below with a (possibly) FP-value, so should be in FP
0188         // For consistency we define w also in FP
0189         fp_vector_type v, w, projected;
0190 
0191         geometry::convert(p2, v);
0192         geometry::convert(p, w);
0193         geometry::convert(p1, projected);
0194         subtract_point(v, projected);
0195         subtract_point(w, projected);
0196 
0197         Strategy strategy;
0198         boost::ignore_unused(strategy);
0199 
0200         typename result_type<Point, PointOfSegment>::type result;
0201 
0202         calculation_type const zero = calculation_type();
0203         calculation_type const c2 = dot_product(v, v);
0204         if ( math::equals(c2, zero) )
0205         {
0206             result.xtd = strategy.apply(p, projected);
0207             // assume that the 0-length segment is perpendicular to the Pt->ProjPt vector
0208             result.atd = 0;
0209             return result;
0210         }
0211 
0212         calculation_type const c1 = dot_product(w, v);
0213         calculation_type const b = c1 / c2;
0214         multiply_value(v, b);
0215         add_point(projected, v);
0216 
0217         result.xtd = strategy.apply(p, projected);
0218 
0219         if (c1 <= zero)
0220         {
0221             result.atd = strategy.apply(p1, projected);
0222         }
0223         else if (c2 <= c1)
0224         {
0225             result.atd = strategy.apply(p2, projected);
0226         }
0227         else
0228         {
0229             result.atd = 0;
0230         }
0231 
0232         return result;
0233     }
0234 };
0235 
0236 } // namespace detail
0237 #endif // DOXYGEN_NO_DETAIL
0238 
0239 #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
0240 namespace services
0241 {
0242 
0243 
0244 template <typename CalculationType, typename Strategy>
0245 struct tag<detail::projected_point_ax<CalculationType, Strategy> >
0246 {
0247     typedef strategy_tag_distance_point_segment type;
0248 };
0249 
0250 
0251 template <typename CalculationType, typename Strategy, typename P, typename PS>
0252 struct return_type<detail::projected_point_ax<CalculationType, Strategy>, P, PS>
0253 {
0254     typedef typename detail::projected_point_ax<CalculationType, Strategy>
0255                         ::template result_type<P, PS>::type type;
0256 };
0257 
0258 
0259 template <typename CalculationType, typename Strategy>
0260 struct comparable_type<detail::projected_point_ax<CalculationType, Strategy> >
0261 {
0262     // Define a projected_point strategy with its underlying point-point-strategy
0263     // being comparable
0264     typedef detail::projected_point_ax
0265         <
0266             CalculationType,
0267             typename comparable_type<Strategy>::type
0268         > type;
0269 };
0270 
0271 
0272 template <typename CalculationType, typename Strategy>
0273 struct get_comparable<detail::projected_point_ax<CalculationType, Strategy> >
0274 {
0275     typedef typename comparable_type
0276         <
0277             detail::projected_point_ax<CalculationType, Strategy>
0278         >::type comparable_type;
0279 public :
0280     static inline comparable_type apply(detail::projected_point_ax<CalculationType, Strategy> const& )
0281     {
0282         return comparable_type();
0283     }
0284 };
0285 
0286 
0287 template <typename CalculationType, typename Strategy, typename P, typename PS>
0288 struct result_from_distance<detail::projected_point_ax<CalculationType, Strategy>, P, PS>
0289 {
0290 private :
0291     typedef typename return_type<detail::projected_point_ax<CalculationType, Strategy>, P, PS>::type return_type;
0292 public :
0293     template <typename T>
0294     static inline return_type apply(detail::projected_point_ax<CalculationType, Strategy> const& , T const& value)
0295     {
0296         Strategy s;
0297         return_type ret;
0298         ret.atd = result_from_distance<Strategy, P, PS>::apply(s, value.atd);
0299         ret.xtd = result_from_distance<Strategy, P, PS>::apply(s, value.xtd);
0300         return ret;
0301     }
0302 };
0303 
0304 
0305 } // namespace services
0306 #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
0307 
0308 
0309 }} // namespace strategy::distance
0310 
0311 
0312 }} // namespace boost::geometry
0313 
0314 
0315 #endif // BOOST_GEOMETRY_STRATEGIES_CARTESIAN_DISTANCE_PROJECTED_POINT_AX_HPP