spherical_geometry 0.4.0

A package for working with spherical geometry
Documentation
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
628
629
630
631
632
633
634
635
636
637
638
use crate::{GreatCircle, GreatCircleArc, SphericalError, SphericalPoint, VEC_LEN_IS_ZERO};
#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};

/// Specifies the direction in which an edge is defined.
///
/// Flipping the direction will cause a polygon to be the complement of what you would expect to be your polygon (if you wanted to create a small triangle around the North Pole but flipped the edge orientation, you would define the polygon to be everywhere apart from the North Pole).
///
/// # Determining the direction
/// ## Algorithmic method
/// Imagine you are **inside** the sphere and are looking at the polygon. Now imagine a reference point inside the polygon and find the closest edge to it.
/// Now the direction is the same as the direction of the edge with reference to the point chosen.
///
/// ## More intuitive method
/// Imagine you are standing on the **inside** surface of the sphere, your head pointing in the direction of the centre of the sphere.
/// If you were to walk along the edge and the inside of the polygon was on your left choose [Self::CounterClockwise], else choose [Self::Clockwise].
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum EdgeDirection {
    Clockwise,
    CounterClockwise,
}

/// A polygon on a unit sphere, given by its vertices and the edge direction
pub struct Polygon {
    vertices: Vec<SphericalPoint>,
    edges_direction: EdgeDirection,
}

impl Polygon {
    /// Creates a new polygon with the vertices and edges direction provided.
    ///
    /// # Important
    /// If the final vertex is not equal to the first one, it will be added automatically.
    ///
    /// Flipping the direction of the edges will cause a polygon to be the complement of what you would expect to be your polygon (if you wanted to create a small triangle around the North Pole but flipped the edge orientation, you would define the polygon to be everywhere apart from the North Pole).
    ///
    /// # Panics
    /// This function panics if no vertices were provided. That does not constitute a valid polygon, and you should always provide at least two vertices.
    ///
    /// # Errors
    /// If any edge is defined by essentially equal or antipodal points, returns [SphericalError::AntipodalOrTooClosePoints] as in the case of identical or antipodal points the great circle (and therefore also the edge) is not uniquely defined.
    pub fn new(vertices_in: Vec<SphericalPoint>, edges_direction: EdgeDirection) -> Result<Self, SphericalError> {
        let mut vertices = vertices_in;
        if !vertices[0].approximately_equals(&vertices[vertices.len() - 1], VEC_LEN_IS_ZERO) {
            // The last vertex is not the same as the first one -> insert the first one to the back
            vertices.push(vertices[0]);
        }
        for i in 0..vertices.len() - 1 {
            if vertices[i].cartesian().cross(&vertices[i + 1].cartesian()).magnitude_squared() < VEC_LEN_IS_ZERO.powi(2) {
                return Err(SphericalError::AntipodalOrTooClosePoints);
            }
        }
        Ok(Self { vertices, edges_direction })
    }

    /// Returns a reference to the vertices list
    pub fn vertices(&self) -> &Vec<SphericalPoint> {
        &self.vertices
    }

    /// Returns the edges direction
    pub fn edges_direction(&self) -> EdgeDirection {
        self.edges_direction
    }

    /// Checks if the polygon contains the given point
    ///
    /// # Errors
    /// This function does not produce its own errors, but it will propagate inner errors out, see below. That should however never happen - if it does, it is a bug in an implementation in the library, so please report it should you encounter it.
    ///
    /// If any of the edges fails to be constructed as a [GreatCircleArc], returns the corresponding error. This should however never happen, as that is checked when the polygon is constructed.
    ///
    /// Also, if any intersections fail the corresponding error will be returned. This should however also never happen.
    pub fn contains_point(&self, point: &SphericalPoint) -> Result<bool, SphericalError> {
        let tiebreaker_lim = 10e-5;
        // Algorithm description:
        // 1) Find the closest edge by finding an intersection with each of the edges with a great circle perpendicular to it. Use the clamped intersection, returning one of the endpoints in case of a miss.
        // 2) Determine if the closest edge is in the correct orientation

        // Step 1
        let mut closest_edge_i = 0;
        let mut closest_edge_dist_metric = f32::INFINITY;
        let mut tiebreaker = 0.0;
        for i in 0..self.vertices.len() - 1 {
            let edge = GreatCircleArc::new(self.vertices[i], self.vertices[i + 1])?;
            if edge.contains_point(point) {
                return Ok(true);
            }
            let (tiebreaker_dist, edge_distance_metric) = match edge.perpendicular_circle_through_point(point) {
                Ok(circle) => {
                    let closest_point = edge.closest_point_to_point_with_circle(&circle, point)?;
                    let unclamped_dist = GreatCircle::from_arc(&edge)
                        .intersect_great_circle(&circle)?
                        .iter()
                        .map(|p| p.minus_cotan_distance(point))
                        .min_by(|a, b| a.total_cmp(b))
                        .unwrap();
                    (unclamped_dist, closest_point.minus_cotan_distance(point))
                }
                Err(SphericalError::AntipodalOrTooClosePoints) => {
                    // The point is essentially the pole of the arc, so it is basically PI/2 radians away -> distance metric = -1/tan(PI/2) = 0
                    (f32::INFINITY, 0.0)
                }
                Err(err) => return Err(err),
            };
            if (edge_distance_metric - closest_edge_dist_metric).abs() < tiebreaker_lim {
                if tiebreaker < tiebreaker_dist {
                    closest_edge_i = i;
                    closest_edge_dist_metric = edge_distance_metric;
                    tiebreaker = tiebreaker_dist;
                }
            } else if edge_distance_metric < closest_edge_dist_metric {
                closest_edge_i = i;
                closest_edge_dist_metric = edge_distance_metric;
                tiebreaker = tiebreaker_dist;
            }
        }

        // Step 2
        let closest_edge_normal = self.vertices[closest_edge_i].cartesian().cross(&self.vertices[closest_edge_i + 1].cartesian());
        let cos_angle = closest_edge_normal.dot(&point.cartesian());
        let is_inside = match self.edges_direction {
            EdgeDirection::Clockwise => cos_angle >= 0.0,
            EdgeDirection::CounterClockwise => cos_angle <= 0.0,
        };

        Ok(is_inside)
    }

    /// Returns the intersections of the edges of the polygon with a given great circle arc
    ///
    /// # Errors
    /// This function does not generate its own errors, but may propagate the following:
    ///  - If any of the edges fails to be constructed as a [GreatCircleArc], returns the corresponding error (see [GreatCircleArc::new]). This should however never happen, as that is checked when the polygon is constructed.
    ///  - If an edge fails to be intersected with the arc, it returns the corresponding error (refer to [GreatCircleArc::intersect_great_circle_arc] for more details).
    pub fn great_circle_arc_intersections(&self, arc: &GreatCircleArc) -> Result<Vec<SphericalPoint>, SphericalError> {
        let mut intersections = Vec::new();

        for i in 0..self.vertices.len() - 1 {
            let edge = GreatCircleArc::new(self.vertices[i], self.vertices[i + 1])?;
            let ints = edge.intersect_great_circle_arc(arc)?;
            for int in ints {
                if intersections
                    .iter()
                    .any(|intersection: &SphericalPoint| intersection.approximately_equals(&int, crate::IDENTICAL_POINTS))
                {
                    continue;
                }
                intersections.push(int);
            }
        }

        Ok(intersections)
    }

    /// Checks if there exists an intersection of the edges of the polygon with the provided [GreatCircleArc]
    ///
    /// This function will in theory return errors less often than [Self::great_circle_arc_intersections] as it handles the cases when arcs are parallel
    ///
    /// # Errors
    /// This function does not generate its own errors, but may propagate the following:
    ///  - If any of the edges fails to be constructed as a [GreatCircleArc], returns the corresponding error (see [GreatCircleArc::new]). This should however never happen, as that is checked when the polygon is constructed.
    ///  - If an edge fails to be intersected with the arc, it returns the corresponding error (refer to [GreatCircleArc::intersects_great_circle_arc] for more details). Handles parallel circles as infinite intersections (returns `Ok(true)`) though.
    pub fn intersects_great_circle_arc(&self, arc: &GreatCircleArc) -> Result<bool, SphericalError> {
        for i in 0..self.vertices.len() - 1 {
            let edge = GreatCircleArc::new(self.vertices[i], self.vertices[i + 1])?;
            if edge.intersects_great_circle_arc(arc)? {
                return Ok(true);
            }
        }
        Ok(false)
    }

    /// Returns the intersections of the edges of the polygon with a given great circle
    ///
    /// # Errors
    /// This function does not generate its own errors, but may propagate the following:
    ///  - If any of the edges fails to be constructed as a [GreatCircleArc], returns the corresponding error (see [GreatCircleArc::new]). This should however never happen, as that is checked when the polygon is constructed.
    ///  - If an edge fails to be intersected with the circle, it returns the corresponding error (refer to [GreatCircleArc::intersect_great_circle] for more details).
    pub fn great_circle_intersections(&self, circle: &GreatCircle) -> Result<Vec<SphericalPoint>, SphericalError> {
        let mut intersections = Vec::new();

        for i in 0..self.vertices.len() - 1 {
            let edge = GreatCircleArc::new(self.vertices[i], self.vertices[i + 1])?;
            let ints = edge.intersect_great_circle(circle)?;
            for int in ints {
                if intersections
                    .iter()
                    .any(|intersection: &SphericalPoint| intersection.approximately_equals(&int, crate::IDENTICAL_POINTS))
                {
                    continue;
                }
                intersections.push(int);
            }
        }

        Ok(intersections)
    }

    /// Checks if there exists an intersection of the edges of the polygon with the provided [GreatCircle]
    ///
    /// This function will in theory return errors less often than [Self::great_circle_intersections] as it handles the cases when circles are parallel
    ///
    /// # Errors
    /// This function does not generate its own errors, but may propagate the following:
    ///  - If any of the edges fails to be constructed as a [GreatCircleArc], returns the corresponding error (see [GreatCircleArc::new]). This should however never happen, as that is checked when the polygon is constructed.
    ///  - If an edge fails to be intersected with the circle, it returns the corresponding error (refer to [GreatCircleArc::intersects_great_circle] for more details). Handles parallel circles as infinite intersections (returns `Ok(true)`) though.
    pub fn intersects_great_circle(&self, circle: &GreatCircle) -> Result<bool, SphericalError> {
        for i in 0..self.vertices.len() - 1 {
            let edge = GreatCircleArc::new(self.vertices[i], self.vertices[i + 1])?;
            if edge.intersects_great_circle(circle)? {
                return Ok(true);
            }
        }
        Ok(false)
    }
}

#[cfg(feature = "serde")]
impl Serialize for EdgeDirection {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let value: u8 = match self {
            EdgeDirection::Clockwise => 0,
            EdgeDirection::CounterClockwise => 1,
        };
        serializer.serialize_u8(value)
    }
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for EdgeDirection {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = u8::deserialize(deserializer)?;
        match value {
            0 => Ok(EdgeDirection::Clockwise),
            1 => Ok(EdgeDirection::CounterClockwise),
            _ => Err(serde::de::Error::custom("Invalid value for EdgeDirection")),
        }
    }
}

#[cfg(feature = "serde")]
impl Serialize for Polygon {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let data = (&self.vertices, &self.edges_direction);
        data.serialize(serializer)
    }
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for Polygon {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let (vertices, edges_direction) = <(Vec<SphericalPoint>, EdgeDirection)>::deserialize(deserializer)?;
        Polygon::new(vertices, edges_direction).map_err(|e| serde::de::Error::custom(format!("invalid polygon: {:?}", e)))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::f32::consts::PI;

    #[test]
    fn is_point_inside() {
        let polygon_1 = Polygon::new(
            vec![
                SphericalPoint::new(0.0, PI / 3.0),
                SphericalPoint::new(-2.0 * PI / 3.0, PI / 3.0),
                SphericalPoint::new(2.0 * PI / 3.0, PI / 3.0),
            ],
            EdgeDirection::CounterClockwise,
        )
        .expect("The polygon should be constructable");
        let north_pole = SphericalPoint::new(0.0, PI / 2.0);
        assert!(polygon_1.contains_point(&north_pole).expect("It should be possible to determine if the point is inside the polygon"));
        let south_pole = SphericalPoint::new(0.0, -PI / 2.0);
        assert!(!polygon_1.contains_point(&south_pole).expect("It should be possible to determine if the point is inside the polygon"));
    }

    #[test]
    fn intersects_arc() {
        let polygon_1 = Polygon::new(
            vec![
                SphericalPoint::new(0.0, PI / 3.0),
                SphericalPoint::new(-2.0 * PI / 3.0, PI / 3.0),
                SphericalPoint::new(2.0 * PI / 3.0, PI / 3.0),
            ],
            EdgeDirection::CounterClockwise,
        )
        .expect("The polygon should be constructable");

        let arc_1 = GreatCircleArc::new(SphericalPoint::new(0.0, PI / 3.0), SphericalPoint::new(-2.0 * PI / 3.0, PI / 3.0)).expect("The arc should be constructable");
        assert!(polygon_1
            .intersects_great_circle_arc(&arc_1)
            .expect("It should be possible to determine if the arc intersects the polygon"));

        let arc_2 = GreatCircleArc::new(SphericalPoint::new(0.0, 0.0), SphericalPoint::new(0.0, PI / 3.0)).expect("The arc should be constructable");
        assert!(polygon_1
            .intersects_great_circle_arc(&arc_2)
            .expect("It should be possible to determine if the arc intersects the polygon"));

        let arc_3 = GreatCircleArc::new(SphericalPoint::new(0.0, 0.0), SphericalPoint::new(0.0, PI / 2.0)).expect("The arc should be constructable");
        assert!(polygon_1
            .intersects_great_circle_arc(&arc_3)
            .expect("It should be possible to determine if the arc intersects the polygon"));

        let arc_4 = GreatCircleArc::new(SphericalPoint::new(0.0, PI / 4.0), SphericalPoint::new(PI / 6.0, PI / 5.0)).expect("The arc should be constructable");
        assert!(!polygon_1
            .intersects_great_circle_arc(&arc_4)
            .expect("It should be possible to determine if the arc intersects the polygon"));

        let polygon_2 = Polygon::new(
            vec![
                SphericalPoint::new(0.0, 0.0),
                SphericalPoint::new(0.0, 0.5),
                SphericalPoint::new(1.2, 0.5),
                SphericalPoint::new(0.8, 0.25),
                SphericalPoint::new(1.2, 0.0),
            ],
            EdgeDirection::CounterClockwise,
        )
        .expect("The polygon should be constructable");

        let arc_5 = GreatCircleArc::new(SphericalPoint::new(1.0, 0.5), SphericalPoint::new(0.9, -0.15)).expect("The arc should be constructable");
        assert!(polygon_2
            .intersects_great_circle_arc(&arc_5)
            .expect("It should be possible to determine if the arc intersects the polygon"));

        let arc_6 = GreatCircleArc::new(SphericalPoint::new(1.0, -0.5), SphericalPoint::new(0.9, -0.15)).expect("The arc should be constructable");
        assert!(!polygon_2
            .intersects_great_circle_arc(&arc_6)
            .expect("It should be possible to determine if the arc intersects the polygon"));

        let arc_7 = GreatCircleArc::new(SphericalPoint::new(-0.1, -0.3), SphericalPoint::new(0.8, 0.7)).expect("The arc should be constructable");
        assert!(polygon_2
            .intersects_great_circle_arc(&arc_7)
            .expect("It should be possible to determine if the arc intersects the polygon"));
    }

    #[test]
    fn arc_intersections() {
        let polygon_1 = Polygon::new(
            vec![
                SphericalPoint::new(0.0, PI / 3.0),
                SphericalPoint::new(-2.0 * PI / 3.0, PI / 3.0),
                SphericalPoint::new(2.0 * PI / 3.0, PI / 3.0),
            ],
            EdgeDirection::CounterClockwise,
        )
        .expect("The polygon should be constructable");

        let arc_1 = GreatCircleArc::new(SphericalPoint::new(0.0, PI / 3.0), SphericalPoint::new(-2.0 * PI / 3.0, PI / 3.0)).expect("The arc should be constructable");
        assert!(matches!(polygon_1.great_circle_arc_intersections(&arc_1), Err(SphericalError::IdenticalGreatCircles)));

        let arc_2 = GreatCircleArc::new(SphericalPoint::new(0.0, 0.0), SphericalPoint::new(0.0, PI / 3.0)).expect("The arc should be constructable");
        assert_eq!(
            polygon_1
                .great_circle_arc_intersections(&arc_2)
                .expect("It should be possible to determine if the arc intersects the polygon")
                .len(),
            1
        );

        let arc_3 = GreatCircleArc::new(SphericalPoint::new(0.0, 0.0), SphericalPoint::new(0.0, PI / 2.0)).expect("The arc should be constructable");
        assert_eq!(
            polygon_1
                .great_circle_arc_intersections(&arc_3)
                .expect("It should be possible to determine if the arc intersects the polygon")
                .len(),
            1
        );

        let arc_4 = GreatCircleArc::new(SphericalPoint::new(0.0, PI / 4.0), SphericalPoint::new(PI / 6.0, PI / 5.0)).expect("The arc should be constructable");
        assert!(polygon_1
            .great_circle_arc_intersections(&arc_4)
            .expect("It should be possible to determine if the arc intersects the polygon")
            .is_empty());

        let polygon_2 = Polygon::new(
            vec![
                SphericalPoint::new(0.0, 0.0),
                SphericalPoint::new(0.0, 0.5),
                SphericalPoint::new(1.2, 0.5),
                SphericalPoint::new(0.8, 0.25),
                SphericalPoint::new(1.2, 0.0),
            ],
            EdgeDirection::CounterClockwise,
        )
        .expect("The polygon should be constructable");

        let tolerance = 10e-4;
        let arc_5 = GreatCircleArc::new(SphericalPoint::new(1.0, 0.5), SphericalPoint::new(0.9, -0.15)).expect("The arc should be constructable");
        let intersections_2_5 = polygon_2
            .great_circle_arc_intersections(&arc_5)
            .expect("It should be possible to determine if the arc intersects the polygon");
        assert_eq!(intersections_2_5.len(), 3);
        dbg!(&intersections_2_5);
        let expected_i_1 = SphericalPoint::new(0.92165, 0.0);
        let expected_i_2 = SphericalPoint::new(0.94532, 0.16371);
        let expected_i_3 = SphericalPoint::new(0.97795, 0.37422);
        assert!(intersections_2_5.iter().any(|p| expected_i_1.approximately_equals(p, tolerance)));
        assert!(intersections_2_5.iter().any(|p| expected_i_2.approximately_equals(p, tolerance)));
        assert!(intersections_2_5.iter().any(|p| expected_i_3.approximately_equals(p, tolerance)));

        let arc_6 = GreatCircleArc::new(SphericalPoint::new(1.0, -0.5), SphericalPoint::new(0.9, -0.15)).expect("The arc should be constructable");
        let intersections_2_6 = polygon_2
            .great_circle_arc_intersections(&arc_6)
            .expect("It should be possible to determine if the arc intersects the polygon");
        assert!(intersections_2_6.is_empty());

        let arc_7 = GreatCircleArc::new(SphericalPoint::new(-0.1, -0.3), SphericalPoint::new(0.8, 0.7)).expect("The arc should be constructable");
        let intersections_2_7 = polygon_2
            .great_circle_arc_intersections(&arc_7)
            .expect("It should be possible to determine if the arc intersects the polygon");
        dbg!(&intersections_2_7);
        assert_eq!(intersections_2_7.len(), 2);
        let expected_i_1 = SphericalPoint::new(0.13007, 0.0);
        let expected_i_2 = SphericalPoint::new(0.63939, 0.58435);
        assert!(intersections_2_7.iter().any(|p| expected_i_1.approximately_equals(p, tolerance)));
        assert!(intersections_2_7.iter().any(|p| expected_i_2.approximately_equals(p, tolerance)));
    }

    #[test]
    fn intersects_circle() {
        let polygon_1 = Polygon::new(
            vec![
                SphericalPoint::new(0.0, PI / 3.0),
                SphericalPoint::new(-2.0 * PI / 3.0, PI / 3.0),
                SphericalPoint::new(2.0 * PI / 3.0, PI / 3.0),
            ],
            EdgeDirection::CounterClockwise,
        )
        .expect("The polygon should be constructable");

        let circle_1 = GreatCircle::new(SphericalPoint::new(0.0, PI / 3.0), SphericalPoint::new(-2.0 * PI / 3.0, PI / 3.0)).expect("The circle should be constructable");
        assert!(polygon_1
            .intersects_great_circle(&circle_1)
            .expect("It should be possible to determine if the circle intersects the polygon"));

        let circle_2 = GreatCircle::new(SphericalPoint::new(0.0, 0.0), SphericalPoint::new(0.0, PI / 3.0)).expect("The circle should be constructable");
        assert!(polygon_1
            .intersects_great_circle(&circle_2)
            .expect("It should be possible to determine if the circle intersects the polygon"));

        let circle_3 = GreatCircle::new(SphericalPoint::new(0.0, 0.0), SphericalPoint::new(0.0, PI / 2.0)).expect("The circle should be constructable");
        assert!(polygon_1
            .intersects_great_circle(&circle_3)
            .expect("It should be possible to determine if the circle intersects the polygon"));

        let circle_4 = GreatCircle::new(SphericalPoint::new(0.0, PI / 4.0), SphericalPoint::new(PI / 6.0, PI / 5.0)).expect("The circle should be constructable");
        assert!(!polygon_1
            .intersects_great_circle(&circle_4)
            .expect("It should be possible to determine if the circle intersects the polygon"));

        let polygon_2 = Polygon::new(
            vec![
                SphericalPoint::new(0.0, 0.0),
                SphericalPoint::new(0.0, 0.5),
                SphericalPoint::new(1.2, 0.5),
                SphericalPoint::new(0.8, 0.25),
                SphericalPoint::new(1.2, 0.0),
            ],
            EdgeDirection::CounterClockwise,
        )
        .expect("The polygon should be constructable");

        let circle_5 = GreatCircle::new(SphericalPoint::new(1.0, 0.5), SphericalPoint::new(0.9, -0.15)).expect("The circle should be constructable");
        assert!(polygon_2
            .intersects_great_circle(&circle_5)
            .expect("It should be possible to determine if the circle intersects the polygon"));

        let circle_6 = GreatCircle::new(SphericalPoint::new(1.0, -0.5), SphericalPoint::new(0.9, -0.15)).expect("The circle should be constructable");
        assert!(polygon_2
            .intersects_great_circle(&circle_6)
            .expect("It should be possible to determine if the circle intersects the polygon"));

        let circle_7 = GreatCircle::new(SphericalPoint::new(-0.1, -0.3), SphericalPoint::new(0.8, 0.7)).expect("The circle should be constructable");
        assert!(polygon_2
            .intersects_great_circle(&circle_7)
            .expect("It should be possible to determine if the circle intersects the polygon"));

        let circle_8 = GreatCircle::new(SphericalPoint::new(0.0, -0.5), SphericalPoint::new(0.9, -0.2)).expect("The circle should be constructable");
        assert!(!polygon_2
            .intersects_great_circle(&circle_8)
            .expect("It should be possible to determine if the circle intersects the polygon"));
    }

    #[test]
    fn circle_intersections() {
        let tolerance = 10e-4;

        let polygon_1 = Polygon::new(
            vec![
                SphericalPoint::new(0.0, PI / 3.0),
                SphericalPoint::new(-2.0 * PI / 3.0, PI / 3.0),
                SphericalPoint::new(2.0 * PI / 3.0, PI / 3.0),
            ],
            EdgeDirection::CounterClockwise,
        )
        .expect("The polygon should be constructable");

        let circle_1 = GreatCircle::new(SphericalPoint::new(0.0, PI / 3.0), SphericalPoint::new(-2.0 * PI / 3.0, PI / 3.0)).expect("The circle should be constructable");
        assert!(matches!(polygon_1.great_circle_intersections(&circle_1), Err(SphericalError::IdenticalGreatCircles)));

        let circle_2 = GreatCircle::new(SphericalPoint::new(0.0, 0.0), SphericalPoint::new(0.0, PI / 3.0)).expect("The circle should be constructable");
        let expected_i_1 = SphericalPoint::new(0.0, PI / 3.0);
        let expected_i_2 = SphericalPoint::new(PI, 1.28976);
        let intersections_1_2 = polygon_1
            .great_circle_intersections(&circle_2)
            .expect("It should be possible to determine if the circle intersects the polygon");
        assert_eq!(intersections_1_2.len(), 2);
        assert!(intersections_1_2.iter().any(|p| expected_i_1.approximately_equals(p, tolerance)));
        assert!(intersections_1_2.iter().any(|p| expected_i_2.approximately_equals(p, tolerance)));

        let circle_3 = GreatCircle::new(SphericalPoint::new(0.0, 0.0), SphericalPoint::new(0.0, PI / 2.0)).expect("The circle should be constructable");
        let expected_i_1 = SphericalPoint::new(0.0, PI / 3.0);
        let expected_i_2 = SphericalPoint::new(PI, 1.28976);
        let intersections_1_3 = polygon_1
            .great_circle_intersections(&circle_3)
            .expect("It should be possible to determine if the circle intersects the polygon");
        assert_eq!(intersections_1_3.len(), 2);
        assert!(intersections_1_3.iter().any(|p| expected_i_1.approximately_equals(p, tolerance)));
        assert!(intersections_1_3.iter().any(|p| expected_i_2.approximately_equals(p, tolerance)));

        let circle_4 = GreatCircle::new(SphericalPoint::new(0.0, PI / 4.0), SphericalPoint::new(PI / 6.0, PI / 5.0)).expect("The circle should be constructable");
        let intersections_1_4 = polygon_1
            .great_circle_intersections(&circle_4)
            .expect("It should be possible to determine if the circle intersects the polygon");
        assert!(intersections_1_4.is_empty());

        let polygon_2 = Polygon::new(
            vec![
                SphericalPoint::new(0.0, 0.0),
                SphericalPoint::new(0.0, 0.5),
                SphericalPoint::new(1.2, 0.5),
                SphericalPoint::new(0.8, 0.25),
                SphericalPoint::new(1.2, 0.0),
            ],
            EdgeDirection::CounterClockwise,
        )
        .expect("The polygon should be constructable");

        let circle_5 = GreatCircle::new(SphericalPoint::new(1.0, 0.5), SphericalPoint::new(0.9, -0.15)).expect("The circle should be constructable");
        let expected_i_1 = SphericalPoint::new(0.92165, 0.0);
        let expected_i_2 = SphericalPoint::new(0.94532, 0.16371);
        let expected_i_3 = SphericalPoint::new(0.97795, 0.37422);
        let expected_i_4 = SphericalPoint::new(1.00878, 0.54583);
        let intersections_2_5 = polygon_2
            .great_circle_intersections(&circle_5)
            .expect("It should be possible to determine if the circle intersects the polygon");
        assert_eq!(intersections_2_5.len(), 4);
        assert!(intersections_2_5.iter().any(|p| expected_i_1.approximately_equals(p, tolerance)));
        assert!(intersections_2_5.iter().any(|p| expected_i_2.approximately_equals(p, tolerance)));
        assert!(intersections_2_5.iter().any(|p| expected_i_3.approximately_equals(p, tolerance)));
        assert!(intersections_2_5.iter().any(|p| expected_i_4.approximately_equals(p, tolerance)));

        let circle_6 = GreatCircle::new(SphericalPoint::new(1.0, -0.5), SphericalPoint::new(0.9, -0.15)).expect("The circle should be constructable");
        let expected_i_1 = SphericalPoint::new(0.69511, 0.58262);
        let expected_i_2 = SphericalPoint::new(0.86191, 0.0);
        let intersections_2_6 = polygon_2
            .great_circle_intersections(&circle_6)
            .expect("It should be possible to determine if the circle intersects the polygon");
        assert_eq!(intersections_2_6.len(), 2);
        dbg!(&intersections_2_6);
        assert!(intersections_2_6.iter().any(|p| expected_i_1.approximately_equals(p, tolerance)));
        assert!(intersections_2_6.iter().any(|p| expected_i_2.approximately_equals(p, tolerance)));

        let circle_7 = GreatCircle::new(SphericalPoint::new(-0.1, -0.3), SphericalPoint::new(0.8, 0.7)).expect("The circle should be constructable");
        let expected_i_1 = SphericalPoint::new(0.63939, 0.58435);
        let expected_i_2 = SphericalPoint::new(0.13007, 0.0);
        let intersections_2_7 = polygon_2
            .great_circle_intersections(&circle_7)
            .expect("It should be possible to determine if the circle intersects the polygon");
        assert_eq!(intersections_2_7.len(), 2);
        assert!(intersections_2_7.iter().any(|p| expected_i_1.approximately_equals(p, tolerance)));
        assert!(intersections_2_7.iter().any(|p| expected_i_2.approximately_equals(p, tolerance)));

        let circle_8 = GreatCircle::new(SphericalPoint::new(0.0, -0.5), SphericalPoint::new(0.9, -0.2)).expect("The circle should be constructable");
        let intersections_2_8 = polygon_2
            .great_circle_intersections(&circle_8)
            .expect("It should be possible to determine if the circle intersects the polygon");
        assert!(intersections_2_8.is_empty());

        let circle_9 = GreatCircle::new(SphericalPoint::new(1.0, -0.5), SphericalPoint::new(0.90142, -0.15)).expect("The circle should be constructable");
        let expected_i_1 = SphericalPoint::new(0.69950, 0.58243);
        let expected_i_2 = SphericalPoint::new(0.80033, 0.25024);
        let expected_i_3 = SphericalPoint::new(0.86387, 0.0);
        let intersections_2_9 = polygon_2
            .great_circle_intersections(&circle_9)
            .expect("It should be possible to determine if the circle intersects the polygon");
        assert_eq!(intersections_2_9.len(), 3);
        assert!(intersections_2_9.iter().any(|p| expected_i_1.approximately_equals(p, tolerance)));
        assert!(intersections_2_9.iter().any(|p| expected_i_2.approximately_equals(p, tolerance)));
        assert!(intersections_2_9.iter().any(|p| expected_i_3.approximately_equals(p, tolerance)));
    }

    #[cfg(feature = "serde")]
    mod serde_tests {
        use super::*;
        use serde_json;

        #[test]
        fn test_serde() {
            let orig = Polygon::new(
                vec![
                    SphericalPoint::new(0.0, 0.0),
                    SphericalPoint::new(0.0, 0.5),
                    SphericalPoint::new(1.2, 0.5),
                    SphericalPoint::new(0.8, 0.25),
                    SphericalPoint::new(1.2, 0.0),
                ],
                EdgeDirection::CounterClockwise,
            )
            .expect("The polygon should be constructable");
            let ser = serde_json::to_string(&orig).expect("Serialization failed");
            let deser: Polygon = serde_json::from_str(&ser).expect("Deserialization failed");

            assert_eq!(orig.vertices().len(), deser.vertices().len(), "Number of vertices do not match");
            for (orig_p, deser_p) in orig.vertices().iter().zip(deser.vertices().iter()) {
                assert!((orig_p.ra() - deser_p.ra()).abs() < f32::EPSILON, "RA values do not match");
                assert!((orig_p.dec() - deser_p.dec()).abs() < f32::EPSILON, "Dec values do not match");
            }
            assert_eq!(orig.edges_direction(), deser.edges_direction(), "Edge directions do not match");
        }
    }
}