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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
use crate::geomath::ang_diff;
use crate::geomath::ang_normalize;
use crate::Geodesic;

use crate::geodesic_capability as caps;

const POLYGONAREA_MASK: u64 =
    caps::LATITUDE | caps::LONGITUDE | caps::DISTANCE | caps::AREA | caps::LONG_UNROLL;

#[cfg(feature = "accurate")]
use accurate::traits::*;

/// Clockwise or CounterClockwise winding
///
/// The standard winding of a Simple Feature polygon is counter-clockwise. However, if the polygon is a hole, then the winding is clockwise.
/// ESRI Shapefile polygons are opposite, with the outer-ring being clockwise and holes being counter-clockwise.
#[derive(Debug, Default, Copy, Clone)]
pub enum Winding {
    Clockwise,
    #[default]
    CounterClockwise,
}

/// Compute the perimeter and area of a polygon on a Geodesic.
#[derive(Debug, Clone)]
pub struct PolygonArea<'a> {
    geoid: &'a Geodesic,
    winding: Winding,
    num: usize,

    #[cfg(not(feature = "accurate"))]
    areasum: f64,

    #[cfg(feature = "accurate")]
    areasum: accurate::sum::Sum2<f64>,

    #[cfg(not(feature = "accurate"))]
    perimetersum: f64,

    #[cfg(feature = "accurate")]
    perimetersum: accurate::sum::Sum2<f64>,

    crossings: i64,
    initial_lat: f64,
    initial_lon: f64,
    latest_lat: f64,
    latest_lon: f64,
}

/// PolygonArea can be used to compute the perimeter and area of a polygon on a Geodesic.
///
/// # Example
/// ```rust
/// use geographiclib_rs::{Geodesic, PolygonArea, Winding};
///
/// let g = Geodesic::wgs84();
/// let mut pa = PolygonArea::new(&g, Winding::CounterClockwise);
///
/// pa.add_point(0.0, 0.0);
/// pa.add_point(0.0, 1.0);
/// pa.add_point(1.0, 1.0);
/// pa.add_point(1.0, 0.0);
///
/// let (perimeter, area, _num) = pa.compute(false);
///
/// use approx::assert_relative_eq;
/// assert_relative_eq!(perimeter, 443770.917248302);
/// assert_relative_eq!(area, 12308778361.469452);
/// ```
impl<'a> PolygonArea<'a> {
    /// Create a new PolygonArea using a Geodesic.
    pub fn new(geoid: &'a Geodesic, winding: Winding) -> PolygonArea {
        PolygonArea {
            geoid,
            winding,

            num: 0,

            #[cfg(not(feature = "accurate"))]
            areasum: 0.0,

            #[cfg(feature = "accurate")]
            areasum: accurate::sum::Sum2::zero(),

            #[cfg(not(feature = "accurate"))]
            perimetersum: 0.0,

            #[cfg(feature = "accurate")]
            perimetersum: accurate::sum::Sum2::zero(),

            crossings: 0,
            initial_lat: 0.0,
            initial_lon: 0.0,
            latest_lat: 0.0,
            latest_lon: 0.0,
        }
    }

    /// Add a point to the polygon
    pub fn add_point(&mut self, lat: f64, lon: f64) {
        if self.num == 0 {
            self.initial_lat = lat;
            self.initial_lon = lon;
        } else {
            #[allow(non_snake_case)]
            let (_a12, s12, _salp1, _calp1, _salp2, _calp2, _m12, _M12, _M21, S12) = self
                .geoid
                ._gen_inverse(self.latest_lat, self.latest_lon, lat, lon, POLYGONAREA_MASK);
            self.perimetersum += s12;
            self.areasum += S12;
            self.crossings += PolygonArea::transit(self.latest_lon, lon);
        }
        self.latest_lat = lat;
        self.latest_lon = lon;
        self.num += 1;
    }

    /// Add an edge to the polygon using an azimuth (in degrees) and a distance (in meters). This can only be called after at least one point has been added.
    ///
    /// # Panics
    /// Panics if no points have been added yet.
    pub fn add_edge(&mut self, azimuth: f64, distance: f64) {
        if self.num == 0 {
            panic!("PolygonArea::add_edge: No points added yet");
        }

        #[allow(non_snake_case)]
        let (_a12, lat, lon, _azi2, _s12, _m12, _M12, _M21, S12) = self.geoid._gen_direct(
            self.latest_lat,
            self.latest_lon,
            azimuth,
            false,
            distance,
            POLYGONAREA_MASK,
        );
        self.perimetersum += distance;
        self.areasum += S12;
        self.crossings += PolygonArea::transitdirect(self.latest_lon, lon);
        self.latest_lat = lat;
        self.latest_lon = lon;
        self.num += 1;
    }

    /// Consumes the PolygonArea and returns the following tuple:
    ///  - 0: Perimeter in (meters) of the polygon.
    ///  - 1: Area (meters²) of the polygon.
    ///  - 2: Number of points added to the polygon.
    ///
    /// # Parameters
    ///
    /// - `sign`: Whether to allow negative values for the area.
    ///   - `true`: Interpret an inversely wound polygon to be a "negative" area. This will produce incorrect results if the polygon covers over half the geodesic. See "Interpreting negative area values" below.
    ///   - `false`: Always return a positive area. Inversely wound polygons are treated as if they are always wound the same way as the winding specified during creation of the PolygonArea. This is useful if you are dealing with very large polygons that might cover over half the geodesic, or if you are certain of your winding.
    ///
    /// # Interpreting negative area values
    ///
    /// A negative value can mean one of two things:
    /// 1. The winding of the polygon is opposite the winding specified during creation of the PolygonArea.
    /// 2. The polygon is larger than half the planet. In this case, to get the final area of the polygon, add the area of the planet to the result. If you expect to be dealing with polygons of this size pass `signed = false` to `compute()` to get the correct result.
    ///
    /// # Large polygon example
    /// ```rust
    /// use geographiclib_rs::{Geodesic, PolygonArea, Winding};
    /// let g = Geodesic::wgs84();
    ///
    /// // Describe a polygon that covers all of the earth EXCEPT this small square.
    /// // The outside of the polygon is in this square, the inside of the polygon is the rest of the earth.
    /// let mut pa = PolygonArea::new(&g, Winding::CounterClockwise);
    /// pa.add_point(0.0, 0.0);
    /// pa.add_point(1.0, 0.0);
    /// pa.add_point(1.0, 1.0);
    /// pa.add_point(0.0, 1.0);
    ///
    /// let (_perimeter, area, _count) = pa.compute(false);
    ///
    /// // Over 5 trillion square meters!
    /// assert_eq!(area, 510053312945726.94);
    /// ```
    pub fn compute(mut self, sign: bool) -> (f64, f64, usize) {
        #[allow(non_snake_case)]
        let (_a12, s12, _salp1, _calp1, _salp2, _calp2, _m12, _M12, _M21, S12) =
            self.geoid._gen_inverse(
                self.latest_lat,
                self.latest_lon,
                self.initial_lat,
                self.initial_lon,
                POLYGONAREA_MASK,
            );
        self.perimetersum += s12;
        self.areasum += S12;

        let perimetersum;
        let areasum;

        #[cfg(not(feature = "accurate"))]
        {
            perimetersum = self.perimetersum;
            areasum = self.areasum;
        }
        #[cfg(feature = "accurate")]
        {
            perimetersum = self.perimetersum.sum();
            areasum = self.areasum.sum();
        }

        self.crossings += PolygonArea::transit(self.latest_lon, self.initial_lon);

        // Properly take into account crossings when calculating area.
        let areasum = self.reduce_area(areasum, sign);

        (perimetersum, areasum, self.num)
    }

    /// Check what the perimeter and area would be if this point was added to the polygon without actually adding it
    pub fn test_point(&self, lat: f64, lon: f64, sign: bool) -> (f64, f64, usize) {
        let mut pa = self.clone();
        pa.add_point(lat, lon);
        pa.compute(sign)
    }

    /// Check what the perimeter and area would be if this edge was added to the polygon without actually adding it
    pub fn test_edge(&self, azimuth: f64, distance: f64, sign: bool) -> (f64, f64, usize) {
        let mut pa = self.clone();
        pa.add_edge(azimuth, distance);
        pa.compute(sign)
    }

    // Return 1 or -1 if crossing prime meridian in east or west direction.
    // Otherwise return zero.  longitude = +/-0 considered to be positive.
    fn transit(lon1: f64, lon2: f64) -> i64 {
        let (lon12, _lon12s) = ang_diff(lon1, lon2);
        let lon1 = ang_normalize(lon1);
        let lon2 = ang_normalize(lon2);

        // Translation from the following cpp code:
        //  https://github.com/geographiclib/geographiclib/blob/8bc13eb53acdd8bc4fbe4de212d42dbb29779476/src/PolygonArea.cpp#L22
        //
        //  lon12 > 0 && ((lon1 < 0 && lon2 >= 0) ||
        //  (lon1 > 0 && lon2 == 0)) ? 1 :
        //  (lon12 < 0 && lon1 >= 0 && lon2 < 0 ? -1 : 0);

        if lon12 > 0.0 && ((lon1 < 0.0 && lon2 >= 0.0) || (lon1 > 0.0 && lon2 == 0.0)) {
            1
        } else if lon12 < 0.0 && lon1 >= 0.0 && lon2 < 0.0 {
            -1
        } else {
            0
        }
    }

    fn transitdirect(lon1: f64, lon2: f64) -> i64 {
        // We want to compute exactly: floor(lon2 / 360) - floor(lon1 / 360)
        let lon1 = lon1 % 720.0;
        let lon2 = lon2 % 720.0;

        let a = if 0.0 <= lon2 && lon2 < 360.0 { 0 } else { 1 };

        let b = if 0.0 <= lon1 && lon1 < 360.0 { 0 } else { 1 };

        a - b
    }

    fn reduce_area(&self, area: f64, signed: bool) -> f64 {
        let geoid_area = self.geoid.area(); // Area of the planet
        let mut area = area % geoid_area;

        // Translation of the following cpp code:
        // if (crossings & 1) area += (area < 0 ? 1 : -1) * _area0/2;
        if self.crossings % 2 != 0 {
            if area < 0.0 {
                area += geoid_area / 2.0;
            } else {
                area -= geoid_area / 2.0;
            }
        }

        // Area is with the clockwise sense. If needed convert to counter-clockwise convention.
        area = match self.winding {
            Winding::Clockwise => area,
            Winding::CounterClockwise => -area,
        };

        if signed {
            // Put area in (-geoid_area/2, geoid_area/2]
            if area > geoid_area / 2.0 {
                area -= geoid_area;
            } else if area <= -geoid_area / 2.0 {
                area += geoid_area;
            }
        } else {
            // Negative values mean the polygon is larger than half the planet. Correct for this.
            if area < 0.0 {
                area += geoid_area;
            }
        }

        area
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Geodesic;
    use approx::assert_relative_eq;

    #[test]
    fn test_simple_polygonarea() {
        let geoid = Geodesic::wgs84();
        let mut pa = PolygonArea::new(&geoid, Winding::CounterClockwise);

        pa.add_point(0.0, 0.0);
        pa.add_point(0.0, 1.0);
        pa.add_point(1.0, 1.0);
        pa.add_point(1.0, 0.0);

        let (perimeter, area, _count) = pa.compute(true);

        assert_relative_eq!(perimeter, 443770.917, epsilon = 1.0e-3);
        assert_relative_eq!(area, 12308778361.469, epsilon = 1.0e-3);

        let mut pa = PolygonArea::new(&geoid, Winding::Clockwise);

        pa.add_point(0.0, 0.0);
        pa.add_point(0.0, 1.0);
        pa.add_point(1.0, 1.0);
        pa.add_point(1.0, 0.0);

        let (perimeter, area, _count) = pa.compute(true);

        assert_relative_eq!(perimeter, 443770.917, epsilon = 1.0e-3);
        assert_relative_eq!(area, -12308778361.469, epsilon = 1.0e-3);
    }

    #[test]
    fn test_planimeter0() {
        // Copied from https://github.com/geographiclib/geographiclib-octave/blob/0662e05a432a040a60ab27c779fa09b554177ba9/inst/geographiclib_test.m#L644

        let geoid = Geodesic::wgs84();

        let mut pa = PolygonArea::new(&geoid, Winding::CounterClockwise);
        pa.add_point(89.0, 0.0);
        pa.add_point(89.0, 90.0);
        pa.add_point(89.0, 180.0);
        pa.add_point(89.0, 270.0);
        let (perimeter, area, _count) = pa.compute(true);
        assert_relative_eq!(perimeter, 631819.8745, epsilon = 1.0e-4);
        assert_relative_eq!(area, 24952305678.0, epsilon = 1.0);

        let mut pa = PolygonArea::new(&geoid, Winding::CounterClockwise);
        pa.add_point(-89.0, 0.0);
        pa.add_point(-89.0, 90.0);
        pa.add_point(-89.0, 180.0);
        pa.add_point(-89.0, 270.0);
        let (perimeter, area, _count) = pa.compute(true);
        assert_relative_eq!(perimeter, 631819.8745, epsilon = 1.0e-4);
        assert_relative_eq!(area, -24952305678.0, epsilon = 1.0);

        let mut pa = PolygonArea::new(&geoid, Winding::CounterClockwise);
        pa.add_point(0.0, -1.0);
        pa.add_point(-1.0, 0.0);
        pa.add_point(0.0, 1.0);
        pa.add_point(1.0, 0.0);
        let (perimeter, area, _count) = pa.compute(true);
        assert_relative_eq!(perimeter, 627598.2731, epsilon = 1.0e-4);
        assert_relative_eq!(area, 24619419146.0, epsilon = 1.0);

        let mut pa = PolygonArea::new(&geoid, Winding::CounterClockwise);
        pa.add_point(90.0, 0.0);
        pa.add_point(0.0, 0.0);
        pa.add_point(0.0, 90.0);
        let (perimeter, area, _count) = pa.compute(true);
        assert_relative_eq!(perimeter, 30022685.0, epsilon = 1.0);
        assert_relative_eq!(area, 63758202715511.0, epsilon = 1.0);
    }

    #[test]
    fn test_planimeter5() {
        // Copied from https://github.com/geographiclib/geographiclib-octave/blob/0662e05a432a040a60ab27c779fa09b554177ba9/inst/geographiclib_test.m#L670

        let geoid = Geodesic::wgs84();
        let mut pa = PolygonArea::new(&geoid, Winding::CounterClockwise);
        pa.add_point(89.0, 0.1);
        pa.add_point(89.0, 90.1);
        pa.add_point(89.0, -179.9);
        let (perimeter, area, _) = pa.compute(true);
        assert_relative_eq!(perimeter, 539297.0, epsilon = 1.0);
        assert_relative_eq!(area, 12476152838.5, epsilon = 1.0);
    }

    #[test]
    fn test_planimeter6() {
        // Copied from https://github.com/geographiclib/geographiclib-octave/blob/0662e05a432a040a60ab27c779fa09b554177ba9/inst/geographiclib_test.m#L679

        let geoid = Geodesic::wgs84();

        let mut pa = PolygonArea::new(&geoid, Winding::CounterClockwise);
        pa.add_point(9.0, -0.00000000000001);
        pa.add_point(9.0, 180.0);
        pa.add_point(9.0, 0.0);
        let (perimeter, area, _) = pa.compute(true);
        assert_relative_eq!(perimeter, 36026861.0, epsilon = 1.0);
        assert_relative_eq!(area, 0.0, epsilon = 1.0);

        let mut pa = PolygonArea::new(&geoid, Winding::CounterClockwise);
        pa.add_point(9.0, 0.00000000000001);
        pa.add_point(9.0, 0.0);
        pa.add_point(9.0, 180.0);
        let (perimeter, area, _) = pa.compute(true);
        assert_relative_eq!(perimeter, 36026861.0, epsilon = 1.0);
        assert_relative_eq!(area, 0.0, epsilon = 1.0);

        let mut pa = PolygonArea::new(&geoid, Winding::CounterClockwise);
        pa.add_point(9.0, 0.00000000000001);
        pa.add_point(9.0, 180.0);
        pa.add_point(9.0, 0.0);
        let (perimeter, area, _) = pa.compute(true);
        assert_relative_eq!(perimeter, 36026861.0, epsilon = 1.0);
        assert_relative_eq!(area, 0.0, epsilon = 1.0);

        let mut pa = PolygonArea::new(&geoid, Winding::CounterClockwise);
        pa.add_point(9.0, -0.00000000000001);
        pa.add_point(9.0, 0.0);
        pa.add_point(9.0, 180.0);
        let (perimeter, area, _) = pa.compute(true);
        assert_relative_eq!(perimeter, 36026861.0, epsilon = 1.0);
        assert_relative_eq!(area, 0.0, epsilon = 1.0);
    }

    #[test]
    fn test_planimeter12() {
        // Copied from https://github.com/geographiclib/geographiclib-octave/blob/0662e05a432a040a60ab27c779fa09b554177ba9/inst/geographiclib_test.m#L701
        let geoid = Geodesic::wgs84();

        let mut pa = PolygonArea::new(&geoid, Winding::CounterClockwise);
        pa.add_point(66.562222222, 0.0);
        pa.add_point(66.562222222, 180.0);
        pa.add_point(66.562222222, 360.0);
        let (perimeter, area, _) = pa.compute(true);
        assert_relative_eq!(perimeter, 10465729.0, epsilon = 1.0);
        assert_relative_eq!(area, 0.0);
    }

    #[test]
    fn test_planimeter12r() {
        // Copied from https://github.com/geographiclib/geographiclib-octave/blob/0662e05a432a040a60ab27c779fa09b554177ba9/inst/geographiclib_test.m#L710
        let geoid = Geodesic::wgs84();

        let mut pa = PolygonArea::new(&geoid, Winding::CounterClockwise);

        pa.add_point(66.562222222, -0.0);
        pa.add_point(66.562222222, -180.0);
        pa.add_point(66.562222222, -360.0);

        let (perimeter, area, _) = pa.compute(true);
        assert_relative_eq!(perimeter, 10465729.0, epsilon = 1.0);
        assert_relative_eq!(area, 0.0);
    }

    #[test]
    fn test_planimeter13() {
        // Copied from https://github.com/geographiclib/geographiclib-octave/blob/0662e05a432a040a60ab27c779fa09b554177ba9/inst/geographiclib_test.m#L719

        let geoid = Geodesic::wgs84();

        let mut pa = PolygonArea::new(&geoid, Winding::CounterClockwise);
        pa.add_point(89.0, -360.0);
        pa.add_point(89.0, -240.0);
        pa.add_point(89.0, -120.0);
        pa.add_point(89.0, 0.0);
        pa.add_point(89.0, 120.0);
        pa.add_point(89.0, 240.0);
        let (perimeter, area, _) = pa.compute(true);
        assert_relative_eq!(perimeter, 1160741.0, epsilon = 1.0);
        assert_relative_eq!(area, 32415230256.0, epsilon = 1.0);
    }

    #[test]
    fn test_planimeter15() {
        // Copied from https://github.com/geographiclib/geographiclib-octave/blob/0662e05a432a040a60ab27c779fa09b554177ba9/inst/geographiclib_test.m#LL728C14-L728C26

        let geoid = Geodesic::wgs84();

        let mut pa = PolygonArea::new(&geoid, Winding::CounterClockwise);
        pa.add_point(2.0, 1.0);
        pa.add_point(1.0, 2.0);
        pa.add_point(3.0, 3.0);
        let (_, area, _) = pa.compute(true);
        assert_relative_eq!(area, 18454562325.45119, epsilon = 1.0e-4);

        // Switching the winding
        let mut pa = PolygonArea::new(&geoid, Winding::Clockwise);
        pa.add_point(2.0, 1.0);
        pa.add_point(1.0, 2.0);
        pa.add_point(3.0, 3.0);
        let (_, area, _) = pa.compute(true);
        assert_relative_eq!(area, -18454562325.45119, epsilon = 1.0e-4);

        // Swaping lat and lon
        let mut pa = PolygonArea::new(&geoid, Winding::CounterClockwise);
        pa.add_point(1.0, 2.0);
        pa.add_point(2.0, 1.0);
        pa.add_point(3.0, 3.0);
        let (_, area, _) = pa.compute(true);
        assert_relative_eq!(area, -18454562325.45119, epsilon = 1.0e-4);
    }

    #[test]
    fn test_planimeter19() {
        // Test degenerate polygons

        // Copied from https://github.com/geographiclib/geographiclib-js/blob/57137fdcf4ba56718c64b909b00331754b6efceb/geodesic/test/geodesictest.js#L801
        // Testing degenrate polygons.
        let geoid = Geodesic::wgs84();

        let mut pa = PolygonArea::new(&geoid, Winding::CounterClockwise);
        let (perimeter, area, _) = pa.clone().compute(true);
        assert_relative_eq!(perimeter, 0.0);
        assert_relative_eq!(area, 0.0);

        let (perimeter, area, _) = pa.test_point(1.0, 1.0, true);
        assert_relative_eq!(perimeter, 0.0);
        assert_relative_eq!(area, 0.0);

        let result = std::panic::catch_unwind(|| {
            let (_, _, _) = pa.test_edge(90.0, 1000.0, true);
        });
        assert!(result.is_err());

        pa.add_point(1.0, 1.0);
        let (perimeter, area, _) = pa.clone().compute(true);
        assert_relative_eq!(perimeter, 0.0);
        assert_relative_eq!(area, 0.0);
    }

    #[test]
    fn test_planimeter21() {
        // Copied From: https://github.com/geographiclib/geographiclib-js/blob/57137fdcf4ba56718c64b909b00331754b6efceb/geodesic/test/geodesictest.js#LL836C13-L836C13

        let g = Geodesic::wgs84();

        let lat = 45.0;
        let azi = 39.2144607176828184218;
        let s = 8420705.40957178156285;
        let r = 39433884866571.4277; // Area for one circuit
        let a0 = g.area(); // Ellipsoid area

        let mut pa_clockwise = PolygonArea::new(&g, Winding::Clockwise);
        let mut pa_counter = PolygonArea::new(&g, Winding::CounterClockwise);

        pa_clockwise.add_point(lat, 60.0);
        pa_clockwise.add_point(lat, 180.0);
        pa_clockwise.add_point(lat, -60.0);
        pa_clockwise.add_point(lat, 60.0);
        pa_clockwise.add_point(lat, 180.0);
        pa_clockwise.add_point(lat, -60.0);

        pa_counter.add_point(lat, 60.0);
        pa_counter.add_point(lat, 180.0);
        pa_counter.add_point(lat, -60.0);
        pa_counter.add_point(lat, 60.0);
        pa_counter.add_point(lat, 180.0);
        pa_counter.add_point(lat, -60.0);

        for i in 3..=4 {
            pa_clockwise.add_point(lat, 60.0);
            pa_clockwise.add_point(lat, 180.0);

            pa_counter.add_point(lat, 60.0);
            pa_counter.add_point(lat, 180.0);

            let (_, area, _) = pa_counter.test_point(lat, -60.0, true);
            assert_relative_eq!(area, i as f64 * r, epsilon = 0.5);

            let (_, area, _) = pa_counter.test_point(lat, -60.0, false);
            assert_relative_eq!(area, i as f64 * r, epsilon = 0.5);

            let (_, area, _) = pa_clockwise.test_point(lat, -60.0, true);
            assert_relative_eq!(area, -i as f64 * r, epsilon = 0.5);

            let (_, area, _) = pa_clockwise.test_point(lat, -60.0, false);
            assert_relative_eq!(area, (-i as f64 * r) + a0, epsilon = 0.5);

            let (_, area, _) = pa_counter.test_edge(azi, s, true);
            assert_relative_eq!(area, i as f64 * r, epsilon = 0.5);

            let (_, area, _) = pa_counter.test_edge(azi, s, false);
            assert_relative_eq!(area, i as f64 * r, epsilon = 0.5);

            let (_, area, _) = pa_clockwise.test_edge(azi, s, true);
            assert_relative_eq!(area, -i as f64 * r, epsilon = 0.5);

            let (_, area, _) = pa_clockwise.test_edge(azi, s, false);
            assert_relative_eq!(area, (-i as f64 * r) + a0, epsilon = 0.5);

            pa_clockwise.add_point(lat, -60.0);
            pa_counter.add_point(lat, -60.0);

            let (_, area, _) = pa_counter.clone().compute(true);
            assert_relative_eq!(area, r * i as f64, epsilon = 0.5);

            let (_, area, _) = pa_counter.clone().compute(false);
            assert_relative_eq!(area, r * i as f64, epsilon = 0.5);

            let (_, area, _) = pa_clockwise.clone().compute(true);
            assert_relative_eq!(area, -(r * i as f64), epsilon = 0.5);

            let (_, area, _) = pa_clockwise.clone().compute(false);
            assert_relative_eq!(area, -(r * i as f64) + a0, epsilon = 0.5);
        }
    }

    #[test]
    fn test_planimeter29() {
        // Check transitdirect vs transit zero handling consistency
        // Copied from: https://github.com/geographiclib/geographiclib-js/blob/57137fdcf4ba56718c64b909b00331754b6efceb/geodesic/test/geodesictest.js#L883

        let geoid = Geodesic::wgs84();
        let mut pa = PolygonArea::new(&geoid, Winding::CounterClockwise);
        pa.add_point(0.0, 0.0);
        pa.add_edge(90.0, 1000.0);
        pa.add_edge(0.0, 1000.0);
        pa.add_edge(-90.0, 1000.0);
        let (_, area, _) = pa.compute(true);
        assert_relative_eq!(area, 1000000.0, epsilon = 0.01);
    }
}