area_huiller.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Boost.Geometry (aka GGL, Generic Geometry Library)
  2. // Copyright (c) 2007-2012 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Use, modification and distribution is subject to the Boost Software License,
  4. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_GEOMETRY_STRATEGIES_SPHERICAL_AREA_HUILLER_HPP
  7. #define BOOST_GEOMETRY_STRATEGIES_SPHERICAL_AREA_HUILLER_HPP
  8. #include <boost/geometry/strategies/spherical/distance_haversine.hpp>
  9. #include <boost/geometry/core/radian_access.hpp>
  10. #include <boost/geometry/util/math.hpp>
  11. namespace boost { namespace geometry
  12. {
  13. namespace strategy { namespace area
  14. {
  15. /*!
  16. \brief Area calculation by spherical excess / Huiller's formula
  17. \ingroup strategies
  18. \tparam PointOfSegment point type of segments of rings/polygons
  19. \tparam CalculationType \tparam_calculation
  20. \author Barend Gehrels. Adapted from:
  21. - http://www.soe.ucsc.edu/~pang/160/f98/Gems/GemsIV/sph_poly.c
  22. - http://williams.best.vwh.net/avform.htm
  23. \note The version in Gems didn't account for polygons crossing the 180 meridian.
  24. \note This version works for convex and non-convex polygons, for 180 meridian
  25. crossing polygons and for polygons with holes. However, some cases (especially
  26. 180 meridian cases) must still be checked.
  27. \note The version which sums angles, which is often seen, doesn't handle non-convex
  28. polygons correctly.
  29. \note The version which sums longitudes, see
  30. http://trs-new.jpl.nasa.gov/dspace/bitstream/2014/40409/1/07-03.pdf, is simple
  31. and works well in most cases but not in 180 meridian crossing cases. This probably
  32. could be solved.
  33. \note This version is made for spherical equatorial coordinate systems
  34. \qbk{
  35. [heading Example]
  36. [area_with_strategy]
  37. [area_with_strategy_output]
  38. [heading See also]
  39. [link geometry.reference.algorithms.area.area_2_with_strategy area (with strategy)]
  40. }
  41. */
  42. template
  43. <
  44. typename PointOfSegment,
  45. typename CalculationType = void
  46. >
  47. class huiller
  48. {
  49. typedef typename boost::mpl::if_c
  50. <
  51. boost::is_void<CalculationType>::type::value,
  52. typename select_most_precise
  53. <
  54. typename coordinate_type<PointOfSegment>::type,
  55. double
  56. >::type,
  57. CalculationType
  58. >::type calculation_type;
  59. protected :
  60. struct excess_sum
  61. {
  62. calculation_type sum;
  63. // Distances are calculated on unit sphere here
  64. strategy::distance::haversine<calculation_type> distance_over_unit_sphere;
  65. inline excess_sum()
  66. : sum(0)
  67. , distance_over_unit_sphere(1)
  68. {}
  69. inline calculation_type area(calculation_type radius) const
  70. {
  71. return - sum * radius * radius;
  72. }
  73. };
  74. public :
  75. typedef calculation_type return_type;
  76. typedef PointOfSegment segment_point_type;
  77. typedef excess_sum state_type;
  78. inline huiller(calculation_type radius = 1.0)
  79. : m_radius(radius)
  80. {}
  81. inline void apply(PointOfSegment const& p1,
  82. PointOfSegment const& p2,
  83. excess_sum& state) const
  84. {
  85. if (! geometry::math::equals(get<0>(p1), get<0>(p2)))
  86. {
  87. calculation_type const half = 0.5;
  88. calculation_type const two = 2.0;
  89. calculation_type const four = 4.0;
  90. calculation_type const two_pi = two * geometry::math::pi<calculation_type>();
  91. calculation_type const half_pi = half * geometry::math::pi<calculation_type>();
  92. // Distance p1 p2
  93. calculation_type a = state.distance_over_unit_sphere.apply(p1, p2);
  94. // Sides on unit sphere to south pole
  95. calculation_type b = half_pi - geometry::get_as_radian<1>(p2);
  96. calculation_type c = half_pi - geometry::get_as_radian<1>(p1);
  97. // Semi parameter
  98. calculation_type s = half * (a + b + c);
  99. // E: spherical excess, using l'Huiller's formula
  100. // [tg(e / 4)]2 = tg[s / 2] tg[(s-a) / 2] tg[(s-b) / 2] tg[(s-c) / 2]
  101. calculation_type E = four * atan(sqrt(geometry::math::abs(tan(s / two)
  102. * tan((s - a) / two)
  103. * tan((s - b) / two)
  104. * tan((s - c) / two))));
  105. E = geometry::math::abs(E);
  106. // In right direction: positive, add area. In left direction: negative, subtract area.
  107. // Longitude comparisons are not so obvious. If one is negative, other is positive,
  108. // we have to take the dateline into account.
  109. // TODO: check this / enhance this, should be more robust. See also the "grow" for ll
  110. // TODO: use minmax or "smaller"/"compare" strategy for this
  111. calculation_type lon1 = geometry::get_as_radian<0>(p1) < 0
  112. ? geometry::get_as_radian<0>(p1) + two_pi
  113. : geometry::get_as_radian<0>(p1);
  114. calculation_type lon2 = geometry::get_as_radian<0>(p2) < 0
  115. ? geometry::get_as_radian<0>(p2) + two_pi
  116. : geometry::get_as_radian<0>(p2);
  117. if (lon2 < lon1)
  118. {
  119. E = -E;
  120. }
  121. state.sum += E;
  122. }
  123. }
  124. inline return_type result(excess_sum const& state) const
  125. {
  126. return state.area(m_radius);
  127. }
  128. private :
  129. /// Radius of the sphere
  130. calculation_type m_radius;
  131. };
  132. #ifndef DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  133. namespace services
  134. {
  135. template <typename Point>
  136. struct default_strategy<spherical_equatorial_tag, Point>
  137. {
  138. typedef strategy::area::huiller<Point> type;
  139. };
  140. // Note: spherical polar coordinate system requires "get_as_radian_equatorial"
  141. /***template <typename Point>
  142. struct default_strategy<spherical_polar_tag, Point>
  143. {
  144. typedef strategy::area::huiller<Point> type;
  145. };***/
  146. } // namespace services
  147. #endif // DOXYGEN_NO_STRATEGY_SPECIALIZATIONS
  148. }} // namespace strategy::area
  149. }} // namespace boost::geometry
  150. #endif // BOOST_GEOMETRY_STRATEGIES_SPHERICAL_AREA_HUILLER_HPP