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