distance_cross_track.hpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2014 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2014-2017.
  4. // Modifications copyright (c) 2014-2017, Oracle and/or its affiliates.
  5. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  6. // Contributed and/or modified by Menelaos Karavelas, on behalf of Oracle
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_GEOMETRY_STRATEGIES_SPHERICAL_DISTANCE_CROSS_TRACK_HPP
  11. #define BOOST_GEOMETRY_STRATEGIES_SPHERICAL_DISTANCE_CROSS_TRACK_HPP
  12. #include <algorithm>
  13. #include <boost/config.hpp>
  14. #include <boost/concept_check.hpp>
  15. #include <boost/mpl/if.hpp>
  16. #include <boost/type_traits/is_void.hpp>
  17. #include <boost/geometry/core/cs.hpp>
  18. #include <boost/geometry/core/access.hpp>
  19. #include <boost/geometry/core/radian_access.hpp>
  20. #include <boost/geometry/core/tags.hpp>
  21. #include <boost/geometry/strategies/distance.hpp>
  22. #include <boost/geometry/strategies/concepts/distance_concept.hpp>
  23. #include <boost/geometry/strategies/spherical/distance_haversine.hpp>
  24. #include <boost/geometry/util/math.hpp>
  25. #include <boost/geometry/util/promote_floating_point.hpp>
  26. #include <boost/geometry/util/select_calculation_type.hpp>
  27. #ifdef BOOST_GEOMETRY_DEBUG_CROSS_TRACK
  28. # include <boost/geometry/io/dsv/write.hpp>
  29. #endif
  30. namespace boost { namespace geometry
  31. {
  32. namespace strategy { namespace distance
  33. {
  34. namespace comparable
  35. {
  36. /*
  37. Given a spherical segment AB and a point D, we are interested in
  38. computing the distance of D from AB. This is usually known as the
  39. cross track distance.
  40. If the projection (along great circles) of the point D lies inside
  41. the segment AB, then the distance (cross track error) XTD is given
  42. by the formula (see http://williams.best.vwh.net/avform.htm#XTE):
  43. XTD = asin( sin(dist_AD) * sin(crs_AD-crs_AB) )
  44. where dist_AD is the great circle distance between the points A and
  45. B, and crs_AD, crs_AB is the course (bearing) between the points A,
  46. D and A, B, respectively.
  47. If the point D does not project inside the arc AB, then the distance
  48. of D from AB is the minimum of the two distances dist_AD and dist_BD.
  49. Our reference implementation for this procedure is listed below
  50. (this was the old Boost.Geometry implementation of the cross track distance),
  51. where:
  52. * The member variable m_strategy is the underlying haversine strategy.
  53. * p stands for the point D.
  54. * sp1 stands for the segment endpoint A.
  55. * sp2 stands for the segment endpoint B.
  56. ================= reference implementation -- start =================
  57. return_type d1 = m_strategy.apply(sp1, p);
  58. return_type d3 = m_strategy.apply(sp1, sp2);
  59. if (geometry::math::equals(d3, 0.0))
  60. {
  61. // "Degenerate" segment, return either d1 or d2
  62. return d1;
  63. }
  64. return_type d2 = m_strategy.apply(sp2, p);
  65. return_type crs_AD = geometry::detail::course<return_type>(sp1, p);
  66. return_type crs_AB = geometry::detail::course<return_type>(sp1, sp2);
  67. return_type crs_BA = crs_AB - geometry::math::pi<return_type>();
  68. return_type crs_BD = geometry::detail::course<return_type>(sp2, p);
  69. return_type d_crs1 = crs_AD - crs_AB;
  70. return_type d_crs2 = crs_BD - crs_BA;
  71. // d1, d2, d3 are in principle not needed, only the sign matters
  72. return_type projection1 = cos( d_crs1 ) * d1 / d3;
  73. return_type projection2 = cos( d_crs2 ) * d2 / d3;
  74. if (projection1 > 0.0 && projection2 > 0.0)
  75. {
  76. return_type XTD
  77. = radius() * math::abs( asin( sin( d1 / radius() ) * sin( d_crs1 ) ));
  78. // Return shortest distance, projected point on segment sp1-sp2
  79. return return_type(XTD);
  80. }
  81. else
  82. {
  83. // Return shortest distance, project either on point sp1 or sp2
  84. return return_type( (std::min)( d1 , d2 ) );
  85. }
  86. ================= reference implementation -- end =================
  87. Motivation
  88. ----------
  89. In what follows we develop a comparable version of the cross track
  90. distance strategy, that meets the following goals:
  91. * It is more efficient than the original cross track strategy (less
  92. operations and less calls to mathematical functions).
  93. * Distances using the comparable cross track strategy can not only
  94. be compared with other distances using the same strategy, but also with
  95. distances computed with the comparable version of the haversine strategy.
  96. * It can serve as the basis for the computation of the cross track distance,
  97. as it is more efficient to compute its comparable version and
  98. transform that to the actual cross track distance, rather than
  99. follow/use the reference implementation listed above.
  100. Major idea
  101. ----------
  102. The idea here is to use the comparable haversine strategy to compute
  103. the distances d1, d2 and d3 in the above listing. Once we have done
  104. that we need also to make sure that instead of returning XTD (as
  105. computed above) that we return a distance CXTD that is compatible
  106. with the comparable haversine distance. To achieve this CXTD must satisfy
  107. the relation:
  108. XTD = 2 * R * asin( sqrt(XTD) )
  109. where R is the sphere's radius.
  110. Below we perform the mathematical analysis that show how to compute CXTD.
  111. Mathematical analysis
  112. ---------------------
  113. Below we use the following trigonometric identities:
  114. sin(2 * x) = 2 * sin(x) * cos(x)
  115. cos(asin(x)) = sqrt(1 - x^2)
  116. Observation:
  117. The distance d1 needed when the projection of the point D is within the
  118. segment must be the true distance. However, comparable::haversine<>
  119. returns a comparable distance instead of the one needed.
  120. To remedy this, we implicitly compute what is needed.
  121. More precisely, we need to compute sin(true_d1):
  122. sin(true_d1) = sin(2 * asin(sqrt(d1)))
  123. = 2 * sin(asin(sqrt(d1)) * cos(asin(sqrt(d1)))
  124. = 2 * sqrt(d1) * sqrt(1-(sqrt(d1))^2)
  125. = 2 * sqrt(d1 - d1 * d1)
  126. This relation is used below.
  127. As we mentioned above the goal is to find CXTD (named "a" below for
  128. brevity) such that ("b" below stands for "d1", and "c" for "d_crs1"):
  129. 2 * R * asin(sqrt(a)) == R * asin(2 * sqrt(b-b^2) * sin(c))
  130. Analysis:
  131. 2 * R * asin(sqrt(a)) == R * asin(2 * sqrt(b-b^2) * sin(c))
  132. <=> 2 * asin(sqrt(a)) == asin(sqrt(b-b^2) * sin(c))
  133. <=> sin(2 * asin(sqrt(a))) == 2 * sqrt(b-b^2) * sin(c)
  134. <=> 2 * sin(asin(sqrt(a))) * cos(asin(sqrt(a))) == 2 * sqrt(b-b^2) * sin(c)
  135. <=> 2 * sqrt(a) * sqrt(1-a) == 2 * sqrt(b-b^2) * sin(c)
  136. <=> sqrt(a) * sqrt(1-a) == sqrt(b-b^2) * sin(c)
  137. <=> sqrt(a-a^2) == sqrt(b-b^2) * sin(c)
  138. <=> a-a^2 == (b-b^2) * (sin(c))^2
  139. Consider the quadratic equation: x^2-x+p^2 == 0,
  140. where p = sqrt(b-b^2) * sin(c); its discriminant is:
  141. d = 1 - 4 * p^2 = 1 - 4 * (b-b^2) * (sin(c))^2
  142. The two solutions are:
  143. a_1 = (1 - sqrt(d)) / 2
  144. a_2 = (1 + sqrt(d)) / 2
  145. Which one to choose?
  146. "a" refers to the distance (on the unit sphere) of D from the
  147. supporting great circle Circ(A,B) of the segment AB.
  148. The two different values for "a" correspond to the lengths of the two
  149. arcs delimited D and the points of intersection of Circ(A,B) and the
  150. great circle perperdicular to Circ(A,B) passing through D.
  151. Clearly, the value we want is the smallest among these two distances,
  152. hence the root we must choose is the smallest root among the two.
  153. So the answer is:
  154. CXTD = ( 1 - sqrt(1 - 4 * (b-b^2) * (sin(c))^2) ) / 2
  155. Therefore, in order to implement the comparable version of the cross
  156. track strategy we need to:
  157. (1) Use the comparable version of the haversine strategy instead of
  158. the non-comparable one.
  159. (2) Instead of return XTD when D projects inside the segment AB, we
  160. need to return CXTD, given by the following formula:
  161. CXTD = ( 1 - sqrt(1 - 4 * (d1-d1^2) * (sin(d_crs1))^2) ) / 2;
  162. Complexity Analysis
  163. -------------------
  164. In the analysis that follows we refer to the actual implementation below.
  165. In particular, instead of computing CXTD as above, we use the more
  166. efficient (operation-wise) computation of CXTD shown here:
  167. return_type sin_d_crs1 = sin(d_crs1);
  168. return_type d1_x_sin = d1 * sin_d_crs1;
  169. return_type d = d1_x_sin * (sin_d_crs1 - d1_x_sin);
  170. return d / (0.5 + math::sqrt(0.25 - d));
  171. Notice that instead of computing:
  172. 0.5 - 0.5 * sqrt(1 - 4 * d) = 0.5 - sqrt(0.25 - d)
  173. we use the following formula instead:
  174. d / (0.5 + sqrt(0.25 - d)).
  175. This is done for numerical robustness. The expression 0.5 - sqrt(0.25 - x)
  176. has large numerical errors for values of x close to 0 (if using doubles
  177. the error start to become large even when d is as large as 0.001).
  178. To remedy that, we re-write 0.5 - sqrt(0.25 - x) as:
  179. 0.5 - sqrt(0.25 - d)
  180. = (0.5 - sqrt(0.25 - d) * (0.5 - sqrt(0.25 - d)) / (0.5 + sqrt(0.25 - d)).
  181. The numerator is the difference of two squares:
  182. (0.5 - sqrt(0.25 - d) * (0.5 - sqrt(0.25 - d))
  183. = 0.5^2 - (sqrt(0.25 - d))^ = 0.25 - (0.25 - d) = d,
  184. which gives the expression we use.
  185. For the complexity analysis, we distinguish between two cases:
  186. (A) The distance is realized between the point D and an
  187. endpoint of the segment AB
  188. Gains:
  189. Since we are using comparable::haversine<> which is called
  190. 3 times, we gain:
  191. -> 3 calls to sqrt
  192. -> 3 calls to asin
  193. -> 6 multiplications
  194. Loses: None
  195. So the net gain is:
  196. -> 6 function calls (sqrt/asin)
  197. -> 6 arithmetic operations
  198. If we use comparable::cross_track<> to compute
  199. cross_track<> we need to account for a call to sqrt, a call
  200. to asin and 2 multiplications. In this case the net gain is:
  201. -> 4 function calls (sqrt/asin)
  202. -> 4 arithmetic operations
  203. (B) The distance is realized between the point D and an
  204. interior point of the segment AB
  205. Gains:
  206. Since we are using comparable::haversine<> which is called
  207. 3 times, we gain:
  208. -> 3 calls to sqrt
  209. -> 3 calls to asin
  210. -> 6 multiplications
  211. Also we gain the operations used to compute XTD:
  212. -> 2 calls to sin
  213. -> 1 call to asin
  214. -> 1 call to abs
  215. -> 2 multiplications
  216. -> 1 division
  217. So the total gains are:
  218. -> 9 calls to sqrt/sin/asin
  219. -> 1 call to abs
  220. -> 8 multiplications
  221. -> 1 division
  222. Loses:
  223. To compute a distance compatible with comparable::haversine<>
  224. we need to perform a few more operations, namely:
  225. -> 1 call to sin
  226. -> 1 call to sqrt
  227. -> 2 multiplications
  228. -> 1 division
  229. -> 1 addition
  230. -> 2 subtractions
  231. So roughly speaking the net gain is:
  232. -> 8 fewer function calls and 3 fewer arithmetic operations
  233. If we were to implement cross_track directly from the
  234. comparable version (much like what haversine<> does using
  235. comparable::haversine<>) we need additionally
  236. -> 2 function calls (asin/sqrt)
  237. -> 2 multiplications
  238. So it pays off to re-implement cross_track<> to use
  239. comparable::cross_track<>; in this case the net gain would be:
  240. -> 6 function calls
  241. -> 1 arithmetic operation
  242. Summary/Conclusion
  243. ------------------
  244. Following the mathematical and complexity analysis above, the
  245. comparable cross track strategy (as implemented below) satisfies
  246. all the goal mentioned in the beginning:
  247. * It is more efficient than its non-comparable counter-part.
  248. * Comparable distances using this new strategy can also be compared
  249. with comparable distances computed with the comparable haversine
  250. strategy.
  251. * It turns out to be more efficient to compute the actual cross
  252. track distance XTD by first computing CXTD, and then computing
  253. XTD by means of the formula:
  254. XTD = 2 * R * asin( sqrt(CXTD) )
  255. */
  256. template
  257. <
  258. typename CalculationType = void,
  259. typename Strategy = comparable::haversine<double, CalculationType>
  260. >
  261. class cross_track
  262. {
  263. public :
  264. template <typename Point, typename PointOfSegment>
  265. struct return_type
  266. : promote_floating_point
  267. <
  268. typename select_calculation_type
  269. <
  270. Point,
  271. PointOfSegment,
  272. CalculationType
  273. >::type
  274. >
  275. {};
  276. typedef typename Strategy::radius_type radius_type;
  277. inline cross_track()
  278. {}
  279. explicit inline cross_track(typename Strategy::radius_type const& r)
  280. : m_strategy(r)
  281. {}
  282. inline cross_track(Strategy const& s)
  283. : m_strategy(s)
  284. {}
  285. // It might be useful in the future
  286. // to overload constructor with strategy info.
  287. // crosstrack(...) {}
  288. template <typename Point, typename PointOfSegment>
  289. inline typename return_type<Point, PointOfSegment>::type
  290. apply(Point const& p, PointOfSegment const& sp1, PointOfSegment const& sp2) const
  291. {
  292. #if !defined(BOOST_MSVC)
  293. BOOST_CONCEPT_ASSERT
  294. (
  295. (concepts::PointDistanceStrategy<Strategy, Point, PointOfSegment>)
  296. );
  297. #endif
  298. typedef typename return_type<Point, PointOfSegment>::type return_type;
  299. // http://williams.best.vwh.net/avform.htm#XTE
  300. return_type d1 = m_strategy.apply(sp1, p);
  301. return_type d3 = m_strategy.apply(sp1, sp2);
  302. if (geometry::math::equals(d3, 0.0))
  303. {
  304. // "Degenerate" segment, return either d1 or d2
  305. return d1;
  306. }
  307. return_type d2 = m_strategy.apply(sp2, p);
  308. return_type lon1 = geometry::get_as_radian<0>(sp1);
  309. return_type lat1 = geometry::get_as_radian<1>(sp1);
  310. return_type lon2 = geometry::get_as_radian<0>(sp2);
  311. return_type lat2 = geometry::get_as_radian<1>(sp2);
  312. return_type lon = geometry::get_as_radian<0>(p);
  313. return_type lat = geometry::get_as_radian<1>(p);
  314. return_type crs_AD = geometry::formula::spherical_azimuth<return_type, false>
  315. (lon1, lat1, lon, lat).azimuth;
  316. geometry::formula::result_spherical<return_type> result =
  317. geometry::formula::spherical_azimuth<return_type, true>
  318. (lon1, lat1, lon2, lat2);
  319. return_type crs_AB = result.azimuth;
  320. return_type crs_BA = result.reverse_azimuth - geometry::math::pi<return_type>();
  321. return_type crs_BD = geometry::formula::spherical_azimuth<return_type, false>
  322. (lon2, lat2, lon, lat).azimuth;
  323. return_type d_crs1 = crs_AD - crs_AB;
  324. return_type d_crs2 = crs_BD - crs_BA;
  325. // d1, d2, d3 are in principle not needed, only the sign matters
  326. return_type projection1 = cos( d_crs1 ) * d1 / d3;
  327. return_type projection2 = cos( d_crs2 ) * d2 / d3;
  328. #ifdef BOOST_GEOMETRY_DEBUG_CROSS_TRACK
  329. std::cout << "Course " << dsv(sp1) << " to " << dsv(p) << " "
  330. << crs_AD * geometry::math::r2d<return_type>() << std::endl;
  331. std::cout << "Course " << dsv(sp1) << " to " << dsv(sp2) << " "
  332. << crs_AB * geometry::math::r2d<return_type>() << std::endl;
  333. std::cout << "Course " << dsv(sp2) << " to " << dsv(sp1) << " "
  334. << crs_BA * geometry::math::r2d<return_type>() << std::endl;
  335. std::cout << "Course " << dsv(sp2) << " to " << dsv(p) << " "
  336. << crs_BD * geometry::math::r2d<return_type>() << std::endl;
  337. std::cout << "Projection AD-AB " << projection1 << " : "
  338. << d_crs1 * geometry::math::r2d<return_type>() << std::endl;
  339. std::cout << "Projection BD-BA " << projection2 << " : "
  340. << d_crs2 * geometry::math::r2d<return_type>() << std::endl;
  341. std::cout << " d1: " << (d1 )
  342. << " d2: " << (d2 )
  343. << std::endl;
  344. #endif
  345. if (projection1 > 0.0 && projection2 > 0.0)
  346. {
  347. #ifdef BOOST_GEOMETRY_DEBUG_CROSS_TRACK
  348. return_type XTD = radius() * geometry::math::abs( asin( sin( d1 ) * sin( d_crs1 ) ));
  349. std::cout << "Projection ON the segment" << std::endl;
  350. std::cout << "XTD: " << XTD
  351. << " d1: " << (d1 * radius())
  352. << " d2: " << (d2 * radius())
  353. << std::endl;
  354. #endif
  355. return_type const half(0.5);
  356. return_type const quarter(0.25);
  357. return_type sin_d_crs1 = sin(d_crs1);
  358. /*
  359. This is the straightforward obvious way to continue:
  360. return_type discriminant
  361. = 1.0 - 4.0 * (d1 - d1 * d1) * sin_d_crs1 * sin_d_crs1;
  362. return 0.5 - 0.5 * math::sqrt(discriminant);
  363. Below we optimize the number of arithmetic operations
  364. and account for numerical robustness:
  365. */
  366. return_type d1_x_sin = d1 * sin_d_crs1;
  367. return_type d = d1_x_sin * (sin_d_crs1 - d1_x_sin);
  368. return d / (half + math::sqrt(quarter - d));
  369. }
  370. else
  371. {
  372. #ifdef BOOST_GEOMETRY_DEBUG_CROSS_TRACK
  373. std::cout << "Projection OUTSIDE the segment" << std::endl;
  374. #endif
  375. // Return shortest distance, project either on point sp1 or sp2
  376. return return_type( (std::min)( d1 , d2 ) );
  377. }
  378. }
  379. template <typename T1, typename T2>
  380. inline radius_type vertical_or_meridian(T1 lat1, T2 lat2) const
  381. {
  382. return m_strategy.radius() * (lat1 - lat2);
  383. }
  384. inline typename Strategy::radius_type radius() const
  385. { return m_strategy.radius(); }
  386. private :
  387. Strategy m_strategy;
  388. };
  389. } // namespace comparable
  390. /*!
  391. \brief Strategy functor for distance point to segment calculation
  392. \ingroup strategies
  393. \details Class which calculates the distance of a point to a segment, for points on a sphere or globe
  394. \see http://williams.best.vwh.net/avform.htm
  395. \tparam CalculationType \tparam_calculation
  396. \tparam Strategy underlying point-point distance strategy, defaults to haversine
  397. \qbk{
  398. [heading See also]
  399. [link geometry.reference.algorithms.distance.distance_3_with_strategy distance (with strategy)]
  400. }
  401. */
  402. template
  403. <
  404. typename CalculationType = void,
  405. typename Strategy = haversine<double, CalculationType>
  406. >
  407. class cross_track
  408. {
  409. public :
  410. template <typename Point, typename PointOfSegment>
  411. struct return_type
  412. : promote_floating_point
  413. <
  414. typename select_calculation_type
  415. <
  416. Point,
  417. PointOfSegment,
  418. CalculationType
  419. >::type
  420. >
  421. {};
  422. typedef typename Strategy::radius_type radius_type;
  423. inline cross_track()
  424. {}
  425. explicit inline cross_track(typename Strategy::radius_type const& r)
  426. : m_strategy(r)
  427. {}
  428. inline cross_track(Strategy const& s)
  429. : m_strategy(s)
  430. {}
  431. // It might be useful in the future
  432. // to overload constructor with strategy info.
  433. // crosstrack(...) {}
  434. template <typename Point, typename PointOfSegment>
  435. inline typename return_type<Point, PointOfSegment>::type
  436. apply(Point const& p, PointOfSegment const& sp1, PointOfSegment const& sp2) const
  437. {
  438. #if !defined(BOOST_MSVC)
  439. BOOST_CONCEPT_ASSERT
  440. (
  441. (concepts::PointDistanceStrategy<Strategy, Point, PointOfSegment>)
  442. );
  443. #endif
  444. typedef typename return_type<Point, PointOfSegment>::type return_type;
  445. typedef cross_track<CalculationType, Strategy> this_type;
  446. typedef typename services::comparable_type
  447. <
  448. this_type
  449. >::type comparable_type;
  450. comparable_type cstrategy
  451. = services::get_comparable<this_type>::apply(m_strategy);
  452. return_type const a = cstrategy.apply(p, sp1, sp2);
  453. return_type const c = return_type(2.0) * asin(math::sqrt(a));
  454. return c * radius();
  455. }
  456. template <typename T1, typename T2>
  457. inline radius_type vertical_or_meridian(T1 lat1, T2 lat2) const
  458. {
  459. return m_strategy.radius() * (lat1 - lat2);
  460. }
  461. inline typename Strategy::radius_type radius() const
  462. { return m_strategy.radius(); }
  463. private :
  464. Strategy m_strategy;
  465. };
  466. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  467. namespace services
  468. {
  469. template <typename CalculationType, typename Strategy>
  470. struct tag<cross_track<CalculationType, Strategy> >
  471. {
  472. typedef strategy_tag_distance_point_segment type;
  473. };
  474. template <typename CalculationType, typename Strategy, typename P, typename PS>
  475. struct return_type<cross_track<CalculationType, Strategy>, P, PS>
  476. : cross_track<CalculationType, Strategy>::template return_type<P, PS>
  477. {};
  478. template <typename CalculationType, typename Strategy>
  479. struct comparable_type<cross_track<CalculationType, Strategy> >
  480. {
  481. typedef comparable::cross_track
  482. <
  483. CalculationType, typename comparable_type<Strategy>::type
  484. > type;
  485. };
  486. template
  487. <
  488. typename CalculationType,
  489. typename Strategy
  490. >
  491. struct get_comparable<cross_track<CalculationType, Strategy> >
  492. {
  493. typedef typename comparable_type
  494. <
  495. cross_track<CalculationType, Strategy>
  496. >::type comparable_type;
  497. public :
  498. static inline comparable_type
  499. apply(cross_track<CalculationType, Strategy> const& strategy)
  500. {
  501. return comparable_type(strategy.radius());
  502. }
  503. };
  504. template
  505. <
  506. typename CalculationType,
  507. typename Strategy,
  508. typename P,
  509. typename PS
  510. >
  511. struct result_from_distance<cross_track<CalculationType, Strategy>, P, PS>
  512. {
  513. private :
  514. typedef typename cross_track
  515. <
  516. CalculationType, Strategy
  517. >::template return_type<P, PS>::type return_type;
  518. public :
  519. template <typename T>
  520. static inline return_type
  521. apply(cross_track<CalculationType, Strategy> const& , T const& distance)
  522. {
  523. return distance;
  524. }
  525. };
  526. // Specializations for comparable::cross_track
  527. template <typename RadiusType, typename CalculationType>
  528. struct tag<comparable::cross_track<RadiusType, CalculationType> >
  529. {
  530. typedef strategy_tag_distance_point_segment type;
  531. };
  532. template
  533. <
  534. typename RadiusType,
  535. typename CalculationType,
  536. typename P,
  537. typename PS
  538. >
  539. struct return_type<comparable::cross_track<RadiusType, CalculationType>, P, PS>
  540. : comparable::cross_track
  541. <
  542. RadiusType, CalculationType
  543. >::template return_type<P, PS>
  544. {};
  545. template <typename RadiusType, typename CalculationType>
  546. struct comparable_type<comparable::cross_track<RadiusType, CalculationType> >
  547. {
  548. typedef comparable::cross_track<RadiusType, CalculationType> type;
  549. };
  550. template <typename RadiusType, typename CalculationType>
  551. struct get_comparable<comparable::cross_track<RadiusType, CalculationType> >
  552. {
  553. private :
  554. typedef comparable::cross_track<RadiusType, CalculationType> this_type;
  555. public :
  556. static inline this_type apply(this_type const& input)
  557. {
  558. return input;
  559. }
  560. };
  561. template
  562. <
  563. typename RadiusType,
  564. typename CalculationType,
  565. typename P,
  566. typename PS
  567. >
  568. struct result_from_distance
  569. <
  570. comparable::cross_track<RadiusType, CalculationType>, P, PS
  571. >
  572. {
  573. private :
  574. typedef comparable::cross_track<RadiusType, CalculationType> strategy_type;
  575. typedef typename return_type<strategy_type, P, PS>::type return_type;
  576. public :
  577. template <typename T>
  578. static inline return_type apply(strategy_type const& strategy,
  579. T const& distance)
  580. {
  581. return_type const s
  582. = sin( (distance / strategy.radius()) / return_type(2.0) );
  583. return s * s;
  584. }
  585. };
  586. /*
  587. TODO: spherical polar coordinate system requires "get_as_radian_equatorial<>"
  588. template <typename Point, typename PointOfSegment, typename Strategy>
  589. struct default_strategy
  590. <
  591. segment_tag, Point, PointOfSegment,
  592. spherical_polar_tag, spherical_polar_tag,
  593. Strategy
  594. >
  595. {
  596. typedef cross_track
  597. <
  598. void,
  599. typename boost::mpl::if_
  600. <
  601. boost::is_void<Strategy>,
  602. typename default_strategy
  603. <
  604. point_tag, Point, PointOfSegment,
  605. spherical_polar_tag, spherical_polar_tag
  606. >::type,
  607. Strategy
  608. >::type
  609. > type;
  610. };
  611. */
  612. template <typename Point, typename PointOfSegment, typename Strategy>
  613. struct default_strategy
  614. <
  615. point_tag, segment_tag, Point, PointOfSegment,
  616. spherical_equatorial_tag, spherical_equatorial_tag,
  617. Strategy
  618. >
  619. {
  620. typedef cross_track
  621. <
  622. void,
  623. typename boost::mpl::if_
  624. <
  625. boost::is_void<Strategy>,
  626. typename default_strategy
  627. <
  628. point_tag, point_tag, Point, PointOfSegment,
  629. spherical_equatorial_tag, spherical_equatorial_tag
  630. >::type,
  631. Strategy
  632. >::type
  633. > type;
  634. };
  635. template <typename PointOfSegment, typename Point, typename Strategy>
  636. struct default_strategy
  637. <
  638. segment_tag, point_tag, PointOfSegment, Point,
  639. spherical_equatorial_tag, spherical_equatorial_tag,
  640. Strategy
  641. >
  642. {
  643. typedef typename default_strategy
  644. <
  645. point_tag, segment_tag, Point, PointOfSegment,
  646. spherical_equatorial_tag, spherical_equatorial_tag,
  647. Strategy
  648. >::type type;
  649. };
  650. } // namespace services
  651. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  652. }} // namespace strategy::distance
  653. }} // namespace boost::geometry
  654. #endif // BOOST_GEOMETRY_STRATEGIES_SPHERICAL_DISTANCE_CROSS_TRACK_HPP