use crate::Result;
use serde::{Deserialize, Serialize};
use std::f64::consts::{PI, TAU};
pub const ANGLE_TOL: f64 = 1.0e-12;
pub const MIN_FULL_CIRCLE_SEGMENTS: usize = 8;
pub fn arc_segments_for_tol(radius: f64, sweep: f64, tol: f64) -> Result<usize> {
if !(radius > 0.0 && radius.is_finite()) {
return Err(format!("Arc radius must be positive and finite, got {radius}").into());
}
if !(tol > 0.0 && tol.is_finite()) {
return Err(format!("Chordal tolerance must be positive and finite, got {tol}").into());
}
let sweep = sweep.abs().min(TAU);
let max_theta = 4.0 * (tol / (2.0 * radius)).sqrt().min(1.0).asin();
let ratio = sweep / max_theta;
if !ratio.is_finite() || ratio > u32::MAX as f64 {
return Err(format!(
"Tolerance {tol} is too small relative to radius {radius} to tessellate"
)
.into());
}
let n = (ratio * (1.0 - 1.0e-12)).ceil() as usize;
let floor = ((MIN_FULL_CIRCLE_SEGMENTS as f64) * sweep / TAU).ceil() as usize;
Ok(n.max(floor).max(1))
}
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum AngleDir {
Cw,
Ccw,
}
impl AngleDir {
pub fn to_sign(self) -> f64 {
match self {
AngleDir::Cw => -1.0,
AngleDir::Ccw => 1.0,
}
}
pub fn from_sign(sign: f64) -> Self {
if sign < 0.0 {
AngleDir::Cw
} else {
AngleDir::Ccw
}
}
pub fn opposite(self) -> Self {
match self {
AngleDir::Cw => AngleDir::Ccw,
AngleDir::Ccw => AngleDir::Cw,
}
}
pub fn is_cw(self) -> bool {
matches!(self, AngleDir::Cw)
}
pub fn is_ccw(self) -> bool {
matches!(self, AngleDir::Ccw)
}
}
pub fn angle_ccw_to(radians0: f64, radians1: f64) -> f64 {
let t0 = angle_signed_pi(radians0);
let t1 = angle_signed_pi(radians1);
let t1 = if t1 < t0 { t1 + 2.0 * PI } else { t1 };
t1 - t0
}
pub fn angle_cw_to(radians0: f64, radians1: f64) -> f64 {
let t0 = angle_signed_pi(radians0);
let t1 = angle_signed_pi(radians1);
let t1 = if t1 > t0 { t1 - 2.0 * PI } else { t1 };
t0 - t1
}
pub fn angle_in_direction(radians0: f64, radians1: f64, angle_dir: AngleDir) -> f64 {
match angle_dir {
AngleDir::Cw => angle_cw_to(radians0, radians1),
AngleDir::Ccw => angle_ccw_to(radians0, radians1),
}
}
pub fn shortest_angle_between(radians0: f64, radians1: f64) -> f64 {
let cw = angle_in_direction(radians0, radians1, AngleDir::Cw);
let ccw = angle_in_direction(radians0, radians1, AngleDir::Ccw);
if cw < ccw { -cw } else { ccw }
}
pub fn angle_signed_pi(radians: f64) -> f64 {
(radians + PI).rem_euclid(2.0 * PI) - PI
}
pub fn angle_to_2pi(radians: f64) -> f64 {
let mut angle = radians % (2.0 * PI);
if angle < 0.0 {
angle += 2.0 * PI;
}
angle
}
pub fn signed_compliment_2pi(radians: f64) -> f64 {
if radians >= 0.0 {
(-2.0 * PI) + radians
} else {
2.0 * PI + radians
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{Circle2, Iso2};
use approx::assert_relative_eq;
use rand::RngExt;
use test_case::test_case;
#[test_case(8)]
#[test_case(16)]
#[test_case(100)]
fn arc_segments_exact_division(n: usize) {
let tol = 1.0 - (PI / n as f64).cos();
assert_eq!(arc_segments_for_tol(1.0, TAU, tol).unwrap(), n);
}
#[test]
fn arc_segments_are_sufficient_and_not_excessive() {
let radius = 2.5;
for &tol in &[0.5, 0.1, 0.01, 1.0e-4, 1.0e-8] {
let n = arc_segments_for_tol(radius, TAU, tol).unwrap();
let sag = |k: usize| radius * (1.0 - (PI / k as f64).cos());
assert!(sag(n) <= tol, "n={n} insufficient for tol={tol}");
if n > MIN_FULL_CIRCLE_SEGMENTS {
assert!(sag(n - 1) > tol, "n={n} excessive for tol={tol}");
}
}
}
#[test]
fn arc_segments_minimum_guard() {
assert_eq!(arc_segments_for_tol(1.0, TAU, 10.0).unwrap(), 8);
assert_eq!(arc_segments_for_tol(1.0, PI, 10.0).unwrap(), 4);
assert_eq!(arc_segments_for_tol(1.0, PI / 2.0, 10.0).unwrap(), 2);
assert_eq!(arc_segments_for_tol(1.0, 1.0e-6, 10.0).unwrap(), 1);
}
#[test]
fn arc_segments_sweep_is_unsigned_and_clamped() {
let tol = 1.0 - (PI / 16.0).cos();
assert_eq!(
arc_segments_for_tol(1.0, -TAU, tol).unwrap(),
arc_segments_for_tol(1.0, TAU, tol).unwrap()
);
assert_eq!(
arc_segments_for_tol(1.0, 5.0 * TAU, tol).unwrap(),
arc_segments_for_tol(1.0, TAU, tol).unwrap()
);
}
#[test]
fn arc_segments_invalid_arguments() {
assert!(arc_segments_for_tol(1.0, TAU, 0.0).is_err());
assert!(arc_segments_for_tol(1.0, TAU, -1.0).is_err());
assert!(arc_segments_for_tol(1.0, TAU, f64::NAN).is_err());
assert!(arc_segments_for_tol(1.0, TAU, f64::INFINITY).is_err());
assert!(arc_segments_for_tol(0.0, TAU, 0.1).is_err());
assert!(arc_segments_for_tol(-1.0, TAU, 0.1).is_err());
assert!(arc_segments_for_tol(f64::NAN, TAU, 0.1).is_err());
}
#[test]
fn arc_segments_unresolvable_tolerance() {
assert!(arc_segments_for_tol(1.0, TAU, 1.0e-20).is_err());
}
#[test]
fn angle_dir_to_sign() {
assert_eq!(AngleDir::Cw.to_sign(), -1.0);
assert_eq!(AngleDir::Ccw.to_sign(), 1.0);
}
#[test]
fn angle_dir_from_sign() {
assert_eq!(AngleDir::from_sign(-1.0), AngleDir::Cw);
assert_eq!(AngleDir::from_sign(1.0), AngleDir::Ccw);
assert_eq!(AngleDir::from_sign(0.0), AngleDir::Ccw);
}
#[test]
fn angle_dir_opposite() {
assert_eq!(AngleDir::Cw.opposite(), AngleDir::Ccw);
assert_eq!(AngleDir::Ccw.opposite(), AngleDir::Cw);
}
#[test]
fn angle_dir_roundtrip() {
assert_eq!(AngleDir::from_sign(AngleDir::Cw.to_sign()), AngleDir::Cw);
assert_eq!(AngleDir::from_sign(AngleDir::Ccw.to_sign()), AngleDir::Ccw);
}
#[test_case(90.0, -270.0)]
#[test_case(180.0, -180.0)]
#[test_case(270.0, -90.0)]
#[test_case(-91.0, 269.0)]
#[test_case(-181.0, 179.0)]
#[test_case(-271.0, 89.0)]
fn test_signed_compliment_0(angle: f64, compliment: f64) {
let test = signed_compliment_2pi(angle.to_radians());
assert_relative_eq!(test, compliment.to_radians(), epsilon = 1.0e-10);
}
#[test]
fn stress_test_angle_to_2pi() {
let mut rnd = rand::rng();
for _ in 0..1000 {
let angle = rnd.random_range(-8.0 * PI..8.0 * PI);
let test = angle_to_2pi(angle);
assert!(
(0.0..2.0 * PI).contains(&test),
"Failed Angle to 2pi: angle={}, test={}",
angle,
test
);
assert_relative_eq!(f64::sin(angle), f64::sin(test), epsilon = 1.0e-10);
assert_relative_eq!(f64::cos(angle), f64::cos(test), epsilon = 1.0e-10);
}
}
#[test]
fn stress_test_angle_signed_pi() {
let mut rnd = rand::rng();
for _ in 0..1000 {
let angle = rnd.random_range(-8.0 * PI..8.0 * PI);
let test = angle_signed_pi(angle);
assert!(
(-PI..PI).contains(&test),
"Failed Angle Signed Pi: angle={}, test={}",
angle,
test
);
assert_relative_eq!(f64::sin(angle), f64::sin(test), epsilon = 1.0e-10);
assert_relative_eq!(f64::cos(angle), f64::cos(test), epsilon = 1.0e-10);
}
}
#[test]
fn stress_test_angle_in_direction_counterclockwise() {
let mut rnd = rand::rng();
let c = Circle2::new(0.0, 0.0, 1.0);
for _ in 0..1000 {
let start = rnd.random_range(-2.0 * PI..2.0 * PI);
let end = rnd.random_range(-2.0 * PI..2.0 * PI);
let v0 = c.point_at_angle(start);
let v1 = c.point_at_angle(end);
let test = angle_in_direction(start, end, AngleDir::Ccw);
assert!(
(0.0..2.0 * PI).contains(&test),
"Failed Angle in Direction CW: start={}, end={}, test={} not in [0, 2pi]",
start,
end,
test
);
let rot = Iso2::rotation(test);
let test0 = rot * v0;
assert_relative_eq!(test0.x, v1.x, epsilon = 1.0e-10);
assert_relative_eq!(test0.y, v1.y, epsilon = 1.0e-10);
}
}
#[test]
fn stress_test_angle_in_direction_clockwise() {
let mut rnd = rand::rng();
let c = Circle2::new(0.0, 0.0, 1.0);
for _ in 0..1000 {
let start = rnd.random_range(-2.0 * PI..2.0 * PI);
let end = rnd.random_range(-2.0 * PI..2.0 * PI);
let v0 = c.point_at_angle(start);
let v1 = c.point_at_angle(end);
let test = angle_in_direction(start, end, AngleDir::Cw);
assert!(
(0.0..2.0 * PI).contains(&test),
"Failed Angle in Direction CW: start={}, end={}, test={} not in [0, 2pi]",
start,
end,
test
);
let rot = Iso2::rotation(-test);
let test0 = rot * v0;
assert_relative_eq!(test0.x, v1.x, epsilon = 1.0e-10);
assert_relative_eq!(test0.y, v1.y, epsilon = 1.0e-10);
}
}
}