GeographicLib  1.44
PolygonArea.hpp
Go to the documentation of this file.
1 /**
2  * \file PolygonArea.hpp
3  * \brief Header for GeographicLib::PolygonAreaT class
4  *
5  * Copyright (c) Charles Karney (2010-2015) <charles@karney.com> and licensed
6  * under the MIT/X11 License. For more information, see
7  * http://geographiclib.sourceforge.net/
8  **********************************************************************/
9 
10 #if !defined(GEOGRAPHICLIB_POLYGONAREA_HPP)
11 #define GEOGRAPHICLIB_POLYGONAREA_HPP 1
12 
15 #include <GeographicLib/Rhumb.hpp>
17 
18 namespace GeographicLib {
19 
20  /**
21  * \brief Polygon areas
22  *
23  * This computes the area of a polygon whose edges are geodesics using the
24  * method given in Section 6 of
25  * - C. F. F. Karney,
26  * <a href="https://dx.doi.org/10.1007/s00190-012-0578-z">
27  * Algorithms for geodesics</a>,
28  * J. Geodesy <b>87</b>, 43--55 (2013);
29  * DOI: <a href="https://dx.doi.org/10.1007/s00190-012-0578-z">
30  * 10.1007/s00190-012-0578-z</a>;
31  * addenda: <a href="http://geographiclib.sf.net/geod-addenda.html">
32  * geod-addenda.html</a>.
33  *
34  * This class lets you add vertices and edges one at a time to the polygon.
35  * The sequence must start with a vertex and thereafter vertices and edges
36  * can be added in any order. Any vertex after the first creates a new edge
37  * which is the ''shortest'' geodesic from the previous vertex. In some
38  * cases there may be two or many such shortest geodesics and the area is
39  * then not uniquely defined. In this case, either add an intermediate
40  * vertex or add the edge ''as'' an edge (by defining its direction and
41  * length).
42  *
43  * The area and perimeter are accumulated at two times the standard floating
44  * point precision to guard against the loss of accuracy with many-sided
45  * polygons. At any point you can ask for the perimeter and area so far.
46  * There's an option to treat the points as defining a polyline instead of a
47  * polygon; in that case, only the perimeter is computed.
48  *
49  * This is a templated class to allow it to be used with Geodesic,
50  * GeodesicExact, and Rhumb. GeographicLib::PolygonArea,
51  * GeographicLib::PolygonAreaExact, and GeographicLib::PolygonAreaRhumb are
52  * typedefs for these cases.
53  *
54  * @tparam GeodType the geodesic class to use.
55  *
56  * Example of use:
57  * \include example-PolygonArea.cpp
58  *
59  * <a href="Planimeter.1.html">Planimeter</a> is a command-line utility
60  * providing access to the functionality of PolygonAreaT.
61  **********************************************************************/
62 
63  template <class GeodType = Geodesic>
64  class PolygonAreaT {
65  private:
66  typedef Math::real real;
67  GeodType _earth;
68  real _area0; // Full ellipsoid area
69  bool _polyline; // Assume polyline (don't close and skip area)
70  unsigned _mask;
71  unsigned _num;
72  int _crossings;
73  Accumulator<> _areasum, _perimetersum;
74  real _lat0, _lon0, _lat1, _lon1;
75  static inline int transit(real lon1, real lon2) {
76  // Return 1 or -1 if crossing prime meridian in east or west direction.
77  // Otherwise return zero.
78  // Compute lon12 the same way as Geodesic::Inverse.
79  lon1 = Math::AngNormalize(lon1);
80  lon2 = Math::AngNormalize(lon2);
81  real lon12 = Math::AngDiff(lon1, lon2);
82  int cross =
83  lon1 < 0 && lon2 >= 0 && lon12 > 0 ? 1 :
84  (lon2 < 0 && lon1 >= 0 && lon12 < 0 ? -1 : 0);
85  return cross;
86  }
87  // an alternate version of transit to deal with longitudes in the direct
88  // problem.
89  static inline int transitdirect(real lon1, real lon2) {
90  // We want to compute exactly
91  // int(floor(lon2 / 360)) - int(floor(lon1 / 360))
92  // Since we only need the parity of the result we can use std::remquo;
93  // but this is buggy with g++ 4.8.3 (glibc version < 2.22), see
94  // https://sourceware.org/bugzilla/show_bug.cgi?id=17569
95  // and requires C++11. So instead we do
96 #if GEOGRAPHICLIB_CXX11_MATH && GEOGRAPHICLIB_PRECISION != 4
97  using std::remainder;
98  lon1 = remainder(lon1, real(720)); lon2 = remainder(lon2, real(720));
99  return ( (lon2 >= 0 && lon2 < 360 ? 0 : 1) -
100  (lon1 >= 0 && lon1 < 360 ? 0 : 1) );
101 #else
102  using std::fmod;
103  lon1 = fmod(lon1, real(720)); lon2 = fmod(lon2, real(720));
104  return ( ((lon2 >= 0 && lon2 < 360) || lon2 < -360 ? 0 : 1) -
105  ((lon1 >= 0 && lon1 < 360) || lon1 < -360 ? 0 : 1) );
106 #endif
107  }
108  public:
109 
110  /**
111  * Constructor for PolygonAreaT.
112  *
113  * @param[in] earth the Geodesic object to use for geodesic calculations.
114  * @param[in] polyline if true that treat the points as defining a polyline
115  * instead of a polygon (default = false).
116  **********************************************************************/
117  PolygonAreaT(const GeodType& earth, bool polyline = false)
118  : _earth(earth)
119  , _area0(_earth.EllipsoidArea())
120  , _polyline(polyline)
121  , _mask(GeodType::LATITUDE | GeodType::LONGITUDE | GeodType::DISTANCE |
122  (_polyline ? GeodType::NONE :
123  GeodType::AREA | GeodType::LONG_UNROLL))
124  { Clear(); }
125 
126  /**
127  * Clear PolygonAreaT, allowing a new polygon to be started.
128  **********************************************************************/
129  void Clear() {
130  _num = 0;
131  _crossings = 0;
132  _areasum = 0;
133  _perimetersum = 0;
134  _lat0 = _lon0 = _lat1 = _lon1 = Math::NaN();
135  }
136 
137  /**
138  * Add a point to the polygon or polyline.
139  *
140  * @param[in] lat the latitude of the point (degrees).
141  * @param[in] lon the longitude of the point (degrees).
142  *
143  * \e lat should be in the range [&minus;90&deg;, 90&deg;].
144  **********************************************************************/
145  void AddPoint(real lat, real lon);
146 
147  /**
148  * Add an edge to the polygon or polyline.
149  *
150  * @param[in] azi azimuth at current point (degrees).
151  * @param[in] s distance from current point to next point (meters).
152  *
153  * This does nothing if no points have been added yet. Use
154  * PolygonAreaT::CurrentPoint to determine the position of the new vertex.
155  **********************************************************************/
156  void AddEdge(real azi, real s);
157 
158  /**
159  * Return the results so far.
160  *
161  * @param[in] reverse if true then clockwise (instead of counter-clockwise)
162  * traversal counts as a positive area.
163  * @param[in] sign if true then return a signed result for the area if
164  * the polygon is traversed in the "wrong" direction instead of returning
165  * the area for the rest of the earth.
166  * @param[out] perimeter the perimeter of the polygon or length of the
167  * polyline (meters).
168  * @param[out] area the area of the polygon (meters<sup>2</sup>); only set
169  * if \e polyline is false in the constructor.
170  * @return the number of points.
171  **********************************************************************/
172  unsigned Compute(bool reverse, bool sign,
173  real& perimeter, real& area) const;
174 
175  /**
176  * Return the results assuming a tentative final test point is added;
177  * however, the data for the test point is not saved. This lets you report
178  * a running result for the perimeter and area as the user moves the mouse
179  * cursor. Ordinary floating point arithmetic is used to accumulate the
180  * data for the test point; thus the area and perimeter returned are less
181  * accurate than if PolygonAreaT::AddPoint and PolygonAreaT::Compute are
182  * used.
183  *
184  * @param[in] lat the latitude of the test point (degrees).
185  * @param[in] lon the longitude of the test point (degrees).
186  * @param[in] reverse if true then clockwise (instead of counter-clockwise)
187  * traversal counts as a positive area.
188  * @param[in] sign if true then return a signed result for the area if
189  * the polygon is traversed in the "wrong" direction instead of returning
190  * the area for the rest of the earth.
191  * @param[out] perimeter the approximate perimeter of the polygon or length
192  * of the polyline (meters).
193  * @param[out] area the approximate area of the polygon
194  * (meters<sup>2</sup>); only set if polyline is false in the
195  * constructor.
196  * @return the number of points.
197  *
198  * \e lat should be in the range [&minus;90&deg;, 90&deg;].
199  **********************************************************************/
200  unsigned TestPoint(real lat, real lon, bool reverse, bool sign,
201  real& perimeter, real& area) const;
202 
203  /**
204  * Return the results assuming a tentative final test point is added via an
205  * azimuth and distance; however, the data for the test point is not saved.
206  * This lets you report a running result for the perimeter and area as the
207  * user moves the mouse cursor. Ordinary floating point arithmetic is used
208  * to accumulate the data for the test point; thus the area and perimeter
209  * returned are less accurate than if PolygonAreaT::AddEdge and
210  * PolygonAreaT::Compute are used.
211  *
212  * @param[in] azi azimuth at current point (degrees).
213  * @param[in] s distance from current point to final test point (meters).
214  * @param[in] reverse if true then clockwise (instead of counter-clockwise)
215  * traversal counts as a positive area.
216  * @param[in] sign if true then return a signed result for the area if
217  * the polygon is traversed in the "wrong" direction instead of returning
218  * the area for the rest of the earth.
219  * @param[out] perimeter the approximate perimeter of the polygon or length
220  * of the polyline (meters).
221  * @param[out] area the approximate area of the polygon
222  * (meters<sup>2</sup>); only set if polyline is false in the
223  * constructor.
224  * @return the number of points.
225  **********************************************************************/
226  unsigned TestEdge(real azi, real s, bool reverse, bool sign,
227  real& perimeter, real& area) const;
228 
229  /// \cond SKIP
230  /**
231  * <b>DEPRECATED</b>
232  * The old name for PolygonAreaT::TestPoint.
233  **********************************************************************/
234  unsigned TestCompute(real lat, real lon, bool reverse, bool sign,
235  real& perimeter, real& area) const {
236  return TestPoint(lat, lon, reverse, sign, perimeter, area);
237  }
238  /// \endcond
239 
240  /** \name Inspector functions
241  **********************************************************************/
242  ///@{
243  /**
244  * @return \e a the equatorial radius of the ellipsoid (meters). This is
245  * the value inherited from the Geodesic object used in the constructor.
246  **********************************************************************/
247 
248  Math::real MajorRadius() const { return _earth.MajorRadius(); }
249 
250  /**
251  * @return \e f the flattening of the ellipsoid. This is the value
252  * inherited from the Geodesic object used in the constructor.
253  **********************************************************************/
254  Math::real Flattening() const { return _earth.Flattening(); }
255 
256  /**
257  * Report the previous vertex added to the polygon or polyline.
258  *
259  * @param[out] lat the latitude of the point (degrees).
260  * @param[out] lon the longitude of the point (degrees).
261  *
262  * If no points have been added, then NaNs are returned. Otherwise, \e lon
263  * will be in the range [&minus;180&deg;, 180&deg;).
264  **********************************************************************/
265  void CurrentPoint(real& lat, real& lon) const
266  { lat = _lat1; lon = _lon1; }
267  ///@}
268  };
269 
270  /**
271  * @relates PolygonAreaT
272  *
273  * Polygon areas using Geodesic. This should be used if the flattening is
274  * small.
275  **********************************************************************/
277 
278  /**
279  * @relates PolygonAreaT
280  *
281  * Polygon areas using GeodesicExact. (But note that the implementation of
282  * areas in GeodesicExact uses a high order series and this is only accurate
283  * for modest flattenings.)
284  **********************************************************************/
286 
287  /**
288  * @relates PolygonAreaT
289  *
290  * Polygon areas using Rhumb.
291  **********************************************************************/
293 
294 } // namespace GeographicLib
295 
296 #endif // GEOGRAPHICLIB_POLYGONAREA_HPP
static T AngNormalize(T x)
Definition: Math.hpp:451
void CurrentPoint(real &lat, real &lon) const
unsigned TestEdge(real azi, real s, bool reverse, bool sign, real &perimeter, real &area) const
static T NaN()
Definition: Math.hpp:783
unsigned TestPoint(real lat, real lon, bool reverse, bool sign, real &perimeter, real &area) const
Definition: PolygonArea.cpp:96
PolygonAreaT< Rhumb > PolygonAreaRhumb
Header for GeographicLib::Rhumb and GeographicLib::RhumbLine classes.
void AddEdge(real azi, real s)
Definition: PolygonArea.cpp:37
Math::real Flattening() const
PolygonAreaT(const GeodType &earth, bool polyline=false)
An accumulator for sums.
Definition: Accumulator.hpp:40
Header for GeographicLib::Geodesic class.
PolygonAreaT< GeodesicExact > PolygonAreaExact
Header for GeographicLib::Accumulator class.
unsigned Compute(bool reverse, bool sign, real &perimeter, real &area) const
Definition: PolygonArea.cpp:54
Namespace for GeographicLib.
Definition: Accumulator.cpp:12
static T AngDiff(T x, T y)
Definition: Math.hpp:499
void AddPoint(real lat, real lon)
Definition: PolygonArea.cpp:17
Header for GeographicLib::GeodesicExact class.
Math::real MajorRadius() const
PolygonAreaT< Geodesic > PolygonArea