use crate::atoms::{Point2, Vector2};
use crate::ops::mesh::predicates::orient2d;
#[inline]
pub fn orient(a: Point2, b: Point2, c: Point2) -> f64 {
orient2d(&[a.x, a.y], &[b.x, b.y], &[c.x, c.y])
}
#[inline]
pub fn rot(v: Vector2, ang: f64) -> Vector2 {
let (s, c) = ang.sin_cos();
Vector2::new(v.x * c - v.y * s, v.x * s + v.y * c)
}
pub fn interior_angle(p: Point2, c: Point2, n: Point2) -> f64 {
let a = n - c;
let b = p - c;
let ang = (a.x * b.y - a.y * b.x).atan2(a.dot(&b));
if ang < 0.0 {
ang + std::f64::consts::TAU
} else {
ang
}
}
pub fn interior_direction(p: Point2, c: Point2, n: Point2, frac: f64) -> Option<Vector2> {
let a = n - c;
let na = a.norm();
if na == 0.0 {
return None;
}
let theta = interior_angle(p, c, n);
Some(rot(a / na, theta * frac))
}
pub fn segments_cross(a1: Point2, a2: Point2, b1: Point2, b2: Point2) -> bool {
let d1 = orient(a1, a2, b1);
let d2 = orient(a1, a2, b2);
let d3 = orient(b1, b2, a1);
let d4 = orient(b1, b2, a2);
if ((d1 > 0.0) != (d2 > 0.0) || d1 == 0.0 || d2 == 0.0)
&& ((d3 > 0.0) != (d4 > 0.0) || d3 == 0.0 || d4 == 0.0)
{
if d1 != 0.0 || d2 != 0.0 || d3 != 0.0 || d4 != 0.0 {
return true;
}
return overlap_1d(a1, a2, b1) || overlap_1d(a1, a2, b2) || overlap_1d(b1, b2, a1);
}
false
}
fn overlap_1d(a: Point2, b: Point2, p: Point2) -> bool {
p.x >= a.x.min(b.x) && p.x <= a.x.max(b.x) && p.y >= a.y.min(b.y) && p.y <= a.y.max(b.y)
}
pub fn quad_is_valid(q: [Point2; 4]) -> bool {
for i in 0..4 {
if orient(q[(i + 3) % 4], q[i], q[(i + 1) % 4]) <= 0.0 {
return false;
}
}
true
}
pub fn quad_quality(q: [Point2; 4]) -> f64 {
let mut worst = f64::INFINITY;
for i in 0..4 {
let prev = q[(i + 3) % 4];
let cur = q[i];
let next = q[(i + 1) % 4];
let a = next - cur;
let b = prev - cur;
let (na, nb) = (a.norm(), b.norm());
if na == 0.0 || nb == 0.0 {
return 0.0;
}
worst = worst.min((a.x * b.y - a.y * b.x) / (na * nb));
}
worst
}
pub fn tri_quality(a: Point2, b: Point2, c: Point2) -> f64 {
let area = orient(a, b, c) * 0.5;
let l = [
(b - a).norm_squared(),
(c - b).norm_squared(),
(a - c).norm_squared(),
];
let lmax = l[0].max(l[1]).max(l[2]);
if lmax == 0.0 {
return 0.0;
}
4.0 / 3.0_f64.sqrt() * area / lmax
}
#[cfg(test)]
mod tests {
use super::*;
use std::f64::consts::{FRAC_PI_2, PI, TAU};
fn p(x: f64, y: f64) -> Point2 {
Point2::new(x, y)
}
#[test]
fn interior_angle_reads_convex_and_reflex_corners() {
let a = interior_angle(p(0.0, 0.0), p(1.0, 0.0), p(1.0, 1.0));
assert!((a - FRAC_PI_2).abs() < 1e-12, "{a}");
let a = interior_angle(p(0.0, 0.0), p(1.0, 0.0), p(2.0, 0.0));
assert!((a - PI).abs() < 1e-12, "{a}");
let a = interior_angle(p(1.0, 1.0), p(1.0, 0.0), p(0.0, 0.0));
assert!((a - 3.0 * FRAC_PI_2).abs() < 1e-12, "{a}");
for &q in &[p(0.5, -1.0), p(-1.0, 0.3), p(2.0, 2.0)] {
let a = interior_angle(p(0.0, 0.0), p(1.0, 0.0), q);
assert!((0.0..TAU).contains(&a));
}
}
#[test]
fn interior_direction_points_into_the_material() {
let d = interior_direction(p(0.0, 0.0), p(1.0, 0.0), p(1.0, 1.0), 0.5).unwrap();
assert!(d.x < 0.0 && d.y > 0.0, "{d:?}");
let d = interior_direction(p(1.0, 1.0), p(1.0, 0.0), p(0.0, 0.0), 0.5).unwrap();
assert!(d.x > 0.0 && d.y < 0.0, "{d:?}");
}
#[test]
fn segment_crossing_is_exact_at_touches() {
assert!(segments_cross(
p(0.0, 0.0),
p(2.0, 2.0),
p(0.0, 2.0),
p(2.0, 0.0)
));
assert!(!segments_cross(
p(0.0, 0.0),
p(1.0, 0.0),
p(0.0, 1.0),
p(1.0, 1.0)
));
assert!(segments_cross(
p(0.0, 0.0),
p(2.0, 0.0),
p(1.0, 0.0),
p(1.0, 1.0)
));
assert!(segments_cross(
p(0.0, 0.0),
p(2.0, 0.0),
p(1.0, 0.0),
p(3.0, 0.0)
));
assert!(!segments_cross(
p(0.0, 0.0),
p(1.0, 0.0),
p(2.0, 0.0),
p(3.0, 0.0)
));
}
#[test]
fn quad_validity_is_convexity() {
let sq = [p(0.0, 0.0), p(1.0, 0.0), p(1.0, 1.0), p(0.0, 1.0)];
assert!(quad_is_valid(sq));
assert!((quad_quality(sq) - 1.0).abs() < 1e-12);
let dart = [p(0.0, 0.0), p(1.0, 0.0), p(0.4, 0.4), p(0.0, 1.0)];
assert!(!quad_is_valid(dart));
assert!(quad_quality(dart) < 0.0);
let cw = [p(0.0, 1.0), p(1.0, 1.0), p(1.0, 0.0), p(0.0, 0.0)];
assert!(!quad_is_valid(cw));
}
}