-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPolygonArea.cpp
208 lines (194 loc) · 6.06 KB
/
PolygonArea.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/**
* \file PolygonArea.cpp
* \brief Implementation for GeographicLib::PolygonAreaT class
*
* Copyright (c) Charles Karney (2010-2015) <[email protected]> and licensed
* under the MIT/X11 License. For more information, see
* https://geographiclib.sourceforge.io/
**********************************************************************/
#include <GeographicLib/PolygonArea.hpp>
namespace GeographicLib {
using namespace std;
template <class GeodType>
void PolygonAreaT<GeodType>::AddPoint(real lat, real lon) {
lat = Math::LatFix(lat);
lon = Math::AngNormalize(lon);
if (_num == 0) {
_lat0 = _lat1 = lat;
_lon0 = _lon1 = lon;
} else {
real s12, S12, t;
_earth.GenInverse(_lat1, _lon1, lat, lon, _mask,
s12, t, t, t, t, t, S12);
_perimetersum += s12;
if (!_polyline) {
_areasum += S12;
_crossings += transit(_lon1, lon);
}
_lat1 = lat; _lon1 = lon;
}
++_num;
}
template <class GeodType>
void PolygonAreaT<GeodType>::AddEdge(real azi, real s) {
if (_num) { // Do nothing if _num is zero
real lat, lon, S12, t;
_earth.GenDirect(_lat1, _lon1, azi, false, s, _mask,
lat, lon, t, t, t, t, t, S12);
_perimetersum += s;
if (!_polyline) {
_areasum += S12;
_crossings += transitdirect(_lon1, lon);
lon = Math::AngNormalize(lon);
}
_lat1 = lat; _lon1 = lon;
++_num;
}
}
template <class GeodType>
unsigned PolygonAreaT<GeodType>::Compute(bool reverse, bool sign,
real& perimeter, real& area) const
{
real s12, S12, t;
if (_num < 2) {
perimeter = 0;
if (!_polyline)
area = 0;
return _num;
}
if (_polyline) {
perimeter = _perimetersum();
return _num;
}
_earth.GenInverse(_lat1, _lon1, _lat0, _lon0, _mask,
s12, t, t, t, t, t, S12);
perimeter = _perimetersum(s12);
Accumulator<> tempsum(_areasum);
tempsum += S12;
int crossings = _crossings + transit(_lon1, _lon0);
if (crossings & 1)
tempsum += (tempsum < 0 ? 1 : -1) * _area0/2;
// area is with the clockwise sense. If !reverse convert to
// counter-clockwise convention.
if (!reverse)
tempsum *= -1;
// If sign put area in (-area0/2, area0/2], else put area in [0, area0)
if (sign) {
if (tempsum > _area0/2)
tempsum -= _area0;
else if (tempsum <= -_area0/2)
tempsum += _area0;
} else {
if (tempsum >= _area0)
tempsum -= _area0;
else if (tempsum < 0)
tempsum += _area0;
}
area = 0 + tempsum();
return _num;
}
template <class GeodType>
unsigned PolygonAreaT<GeodType>::TestPoint(real lat, real lon,
bool reverse, bool sign,
real& perimeter, real& area) const
{
if (_num == 0) {
perimeter = 0;
if (!_polyline)
area = 0;
return 1;
}
perimeter = _perimetersum();
real tempsum = _polyline ? 0 : _areasum();
int crossings = _crossings;
unsigned num = _num + 1;
for (int i = 0; i < (_polyline ? 1 : 2); ++i) {
real s12, S12, t;
_earth.GenInverse(i == 0 ? _lat1 : lat, i == 0 ? _lon1 : lon,
i != 0 ? _lat0 : lat, i != 0 ? _lon0 : lon,
_mask, s12, t, t, t, t, t, S12);
perimeter += s12;
if (!_polyline) {
tempsum += S12;
crossings += transit(i == 0 ? _lon1 : lon,
i != 0 ? _lon0 : lon);
}
}
if (_polyline)
return num;
if (crossings & 1)
tempsum += (tempsum < 0 ? 1 : -1) * _area0/2;
// area is with the clockwise sense. If !reverse convert to
// counter-clockwise convention.
if (!reverse)
tempsum *= -1;
// If sign put area in (-area0/2, area0/2], else put area in [0, area0)
if (sign) {
if (tempsum > _area0/2)
tempsum -= _area0;
else if (tempsum <= -_area0/2)
tempsum += _area0;
} else {
if (tempsum >= _area0)
tempsum -= _area0;
else if (tempsum < 0)
tempsum += _area0;
}
area = 0 + tempsum;
return num;
}
template <class GeodType>
unsigned PolygonAreaT<GeodType>::TestEdge(real azi, real s,
bool reverse, bool sign,
real& perimeter, real& area) const
{
if (_num == 0) { // we don't have a starting point!
perimeter = Math::NaN();
if (!_polyline)
area = Math::NaN();
return 0;
}
unsigned num = _num + 1;
perimeter = _perimetersum() + s;
if (_polyline)
return num;
real tempsum = _areasum();
int crossings = _crossings;
{
real lat, lon, s12, S12, t;
_earth.GenDirect(_lat1, _lon1, azi, false, s, _mask,
lat, lon, t, t, t, t, t, S12);
tempsum += S12;
crossings += transitdirect(_lon1, lon);
lon = Math::AngNormalize(lon);
_earth.GenInverse(lat, lon, _lat0, _lon0, _mask,
s12, t, t, t, t, t, S12);
perimeter += s12;
tempsum += S12;
crossings += transit(lon, _lon0);
}
if (crossings & 1)
tempsum += (tempsum < 0 ? 1 : -1) * _area0/2;
// area is with the clockwise sense. If !reverse convert to
// counter-clockwise convention.
if (!reverse)
tempsum *= -1;
// If sign put area in (-area0/2, area0/2], else put area in [0, area0)
if (sign) {
if (tempsum > _area0/2)
tempsum -= _area0;
else if (tempsum <= -_area0/2)
tempsum += _area0;
} else {
if (tempsum >= _area0)
tempsum -= _area0;
else if (tempsum < 0)
tempsum += _area0;
}
area = 0 + tempsum;
return num;
}
template class GEOGRAPHICLIB_EXPORT PolygonAreaT<Geodesic>;
template class GEOGRAPHICLIB_EXPORT PolygonAreaT<GeodesicExact>;
template class GEOGRAPHICLIB_EXPORT PolygonAreaT<Rhumb>;
} // namespace GeographicLib