GeographicLib  1.45
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  * More points can be added to the polygon after this call.
173  **********************************************************************/
174  unsigned Compute(bool reverse, bool sign,
175  real& perimeter, real& area) const;
176 
177  /**
178  * Return the results assuming a tentative final test point is added;
179  * however, the data for the test point is not saved. This lets you report
180  * a running result for the perimeter and area as the user moves the mouse
181  * cursor. Ordinary floating point arithmetic is used to accumulate the
182  * data for the test point; thus the area and perimeter returned are less
183  * accurate than if PolygonAreaT::AddPoint and PolygonAreaT::Compute are
184  * used.
185  *
186  * @param[in] lat the latitude of the test point (degrees).
187  * @param[in] lon the longitude of the test point (degrees).
188  * @param[in] reverse if true then clockwise (instead of counter-clockwise)
189  * traversal counts as a positive area.
190  * @param[in] sign if true then return a signed result for the area if
191  * the polygon is traversed in the "wrong" direction instead of returning
192  * the area for the rest of the earth.
193  * @param[out] perimeter the approximate perimeter of the polygon or length
194  * of the polyline (meters).
195  * @param[out] area the approximate area of the polygon
196  * (meters<sup>2</sup>); only set if polyline is false in the
197  * constructor.
198  * @return the number of points.
199  *
200  * \e lat should be in the range [&minus;90&deg;, 90&deg;].
201  **********************************************************************/
202  unsigned TestPoint(real lat, real lon, bool reverse, bool sign,
203  real& perimeter, real& area) const;
204 
205  /**
206  * Return the results assuming a tentative final test point is added via an
207  * azimuth and distance; however, the data for the test point is not saved.
208  * This lets you report a running result for the perimeter and area as the
209  * user moves the mouse cursor. Ordinary floating point arithmetic is used
210  * to accumulate the data for the test point; thus the area and perimeter
211  * returned are less accurate than if PolygonAreaT::AddEdge and
212  * PolygonAreaT::Compute are used.
213  *
214  * @param[in] azi azimuth at current point (degrees).
215  * @param[in] s distance from current point to final test point (meters).
216  * @param[in] reverse if true then clockwise (instead of counter-clockwise)
217  * traversal counts as a positive area.
218  * @param[in] sign if true then return a signed result for the area if
219  * the polygon is traversed in the "wrong" direction instead of returning
220  * the area for the rest of the earth.
221  * @param[out] perimeter the approximate perimeter of the polygon or length
222  * of the polyline (meters).
223  * @param[out] area the approximate area of the polygon
224  * (meters<sup>2</sup>); only set if polyline is false in the
225  * constructor.
226  * @return the number of points.
227  **********************************************************************/
228  unsigned TestEdge(real azi, real s, bool reverse, bool sign,
229  real& perimeter, real& area) const;
230 
231  /// \cond SKIP
232  /**
233  * <b>DEPRECATED</b>
234  * The old name for PolygonAreaT::TestPoint.
235  **********************************************************************/
236  unsigned TestCompute(real lat, real lon, bool reverse, bool sign,
237  real& perimeter, real& area) const {
238  return TestPoint(lat, lon, reverse, sign, perimeter, area);
239  }
240  /// \endcond
241 
242  /** \name Inspector functions
243  **********************************************************************/
244  ///@{
245  /**
246  * @return \e a the equatorial radius of the ellipsoid (meters). This is
247  * the value inherited from the Geodesic object used in the constructor.
248  **********************************************************************/
249 
250  Math::real MajorRadius() const { return _earth.MajorRadius(); }
251 
252  /**
253  * @return \e f the flattening of the ellipsoid. This is the value
254  * inherited from the Geodesic object used in the constructor.
255  **********************************************************************/
256  Math::real Flattening() const { return _earth.Flattening(); }
257 
258  /**
259  * Report the previous vertex added to the polygon or polyline.
260  *
261  * @param[out] lat the latitude of the point (degrees).
262  * @param[out] lon the longitude of the point (degrees).
263  *
264  * If no points have been added, then NaNs are returned. Otherwise, \e lon
265  * will be in the range [&minus;180&deg;, 180&deg;).
266  **********************************************************************/
267  void CurrentPoint(real& lat, real& lon) const
268  { lat = _lat1; lon = _lon1; }
269  ///@}
270  };
271 
272  /**
273  * @relates PolygonAreaT
274  *
275  * Polygon areas using Geodesic. This should be used if the flattening is
276  * small.
277  **********************************************************************/
279 
280  /**
281  * @relates PolygonAreaT
282  *
283  * Polygon areas using GeodesicExact. (But note that the implementation of
284  * areas in GeodesicExact uses a high order series and this is only accurate
285  * for modest flattenings.)
286  **********************************************************************/
288 
289  /**
290  * @relates PolygonAreaT
291  *
292  * Polygon areas using Rhumb.
293  **********************************************************************/
295 
296 } // namespace GeographicLib
297 
298 #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