use crate::UnitVec2;
use crate::common::{PCoords, SPCoords};
use crate::geom2::{Boundary2, Curve2, CurveGroup2, CurveStation2, Point2, Vector2};
use crate::na::{SVector, Unit};
#[derive(Debug, Clone)]
pub struct AlignSurfMatch2 {
pub point: Point2,
pub normal: UnitVec2,
pub is_on: bool,
pub weight: f64,
pub sigma: f64,
}
impl AlignSurfMatch2 {
pub fn new(point: Point2, normal: UnitVec2, is_on: bool, weight: f64) -> Self {
Self {
point,
normal,
is_on,
weight,
sigma: 0.0,
}
}
pub fn with_sigma(&self, sigma: f64) -> Self {
Self {
sigma,
..self.clone()
}
}
}
impl PCoords<2> for AlignSurfMatch2 {
fn coords(&self) -> SVector<f64, 2> {
self.point.coords()
}
}
impl SPCoords<2> for AlignSurfMatch2 {
fn normal(&self) -> Unit<SVector<f64, 2>> {
self.normal
}
}
impl Default for AlignSurfMatch2 {
fn default() -> Self {
Self {
point: Point2::origin(),
normal: Vector2::x_axis(),
is_on: false,
weight: 0.0,
sigma: 0.0,
}
}
}
pub trait SurfaceTarget2: Sync + Send {
fn find_align_match(&self, p: &Point2) -> AlignSurfMatch2;
}
impl SurfaceTarget2 for Curve2 {
fn find_align_match(&self, p: &Point2) -> AlignSurfMatch2 {
let station = self.at_closest_to_point(p);
let sp = station.surface_point();
let is_on = self.is_closed() || !is_curve_endpoint(&station, self);
AlignSurfMatch2::new(sp.point, sp.normal, is_on, 1.0)
}
}
impl SurfaceTarget2 for CurveGroup2 {
fn find_align_match(&self, p: &Point2) -> AlignSurfMatch2 {
let (m, station) = self.at_closest_to_point(p);
let curve = &self.curves()[m];
let sp = station.surface_point();
let is_on = curve.is_closed() || !is_curve_endpoint(&station, curve);
AlignSurfMatch2::new(sp.point, sp.normal, is_on, 1.0)
}
}
pub(crate) fn is_curve_endpoint(station: &CurveStation2, curve: &Curve2) -> bool {
let last_edge = curve.count() - 2;
(station.index() == 0 && station.fraction() == 0.0)
|| (station.index() == last_edge && station.fraction() == 1.0)
}
impl SurfaceTarget2 for Boundary2 {
fn find_align_match(&self, p: &Point2) -> AlignSurfMatch2 {
let (_, m) = self.at_closest_to_point(p);
let is_on = self.is_closed() || (m.l > 0.0 && m.l < self.length());
AlignSurfMatch2::new(m.point, m.normal, is_on, 1.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::geom2::{BoundaryData2, BoundaryEditor};
use approx::assert_relative_eq;
fn ccw_square_curve(closed: bool) -> Curve2 {
let points =
[(0.0, 0.0), (2.0, 0.0), (2.0, 2.0), (0.0, 2.0)].map(|(x, y)| Point2::new(x, y));
Curve2::from_points(&points, 1e-8, closed).unwrap()
}
#[test]
fn curve_closed_normal_points_outward() {
let curve = ccw_square_curve(true);
let m = curve.find_align_match(&Point2::new(1.0, -5.0));
assert_relative_eq!(
m.normal.into_inner(),
Vector2::new(0.0, -1.0),
epsilon = 1e-8
);
assert!(m.is_on);
}
#[test]
fn curve_closed_seam_is_on() {
let curve = ccw_square_curve(true);
let m = curve.find_align_match(&Point2::new(-5.0, 0.0));
assert!(m.is_on);
}
#[test]
fn curve_open_end_is_not_on() {
let curve = ccw_square_curve(false);
let m = curve.find_align_match(&Point2::new(-5.0, -5.0));
assert!(!m.is_on);
let m = curve.find_align_match(&Point2::new(-5.0, 7.0));
assert!(!m.is_on);
}
#[test]
fn curve_open_interior_is_on() {
let curve = ccw_square_curve(false);
let m = curve.find_align_match(&Point2::new(1.0, -5.0));
assert!(m.is_on);
}
fn ccw_square_boundary(closed: bool) -> Boundary2 {
let mut data = if closed {
BoundaryData2::new_closed()
} else {
BoundaryData2::new_open(Point2::new(0.0, 0.0))
};
let mut cursor = data.get_cursor(None);
cursor.add_seg_xy(2.0, 0.0);
cursor.add_seg_xy(2.0, 2.0);
cursor.add_seg_xy(0.0, 2.0);
if closed {
cursor.add_seg_xy(0.0, 0.0);
}
data.try_to_boundary().unwrap()
}
#[test]
fn boundary_closed_normal_points_outward() {
let boundary = ccw_square_boundary(true);
let m = boundary.find_align_match(&Point2::new(1.0, -5.0));
assert_relative_eq!(
m.normal.into_inner(),
Vector2::new(0.0, -1.0),
epsilon = 1e-8
);
assert!(m.is_on);
}
#[test]
fn boundary_open_end_is_not_on() {
let boundary = ccw_square_boundary(false);
let m = boundary.find_align_match(&Point2::new(-5.0, -5.0));
assert!(!m.is_on);
let m = boundary.find_align_match(&Point2::new(-5.0, 7.0));
assert!(!m.is_on);
}
#[test]
fn boundary_open_interior_is_on() {
let boundary = ccw_square_boundary(false);
let m = boundary.find_align_match(&Point2::new(1.0, -5.0));
assert!(m.is_on);
}
fn mixed_group() -> CurveGroup2 {
let open = Curve2::from_points(
&[Point2::new(10.0, 0.0), Point2::new(12.0, 0.0)],
1e-8,
false,
)
.unwrap();
CurveGroup2::new(vec![ccw_square_curve(true), open]).unwrap()
}
#[test]
fn group_interior_matches_are_on_for_both_members() {
let group = mixed_group();
let m = group.find_align_match(&Point2::new(1.0, -5.0));
assert!(m.is_on);
assert_relative_eq!(m.point, Point2::new(1.0, 0.0), epsilon = 1e-12);
let m = group.find_align_match(&Point2::new(11.0, -5.0));
assert!(m.is_on);
assert_relative_eq!(m.point, Point2::new(11.0, 0.0), epsilon = 1e-12);
}
#[test]
fn group_match_past_an_open_members_end_is_not_on() {
let group = mixed_group();
let m = group.find_align_match(&Point2::new(15.0, 0.0));
assert!(!m.is_on);
assert_relative_eq!(m.point, Point2::new(12.0, 0.0), epsilon = 1e-12);
}
#[test]
fn group_closed_member_never_reports_off() {
let group = mixed_group();
let m = group.find_align_match(&Point2::new(-5.0, 0.0));
assert!(m.is_on);
}
#[test]
fn group_normal_comes_from_the_owning_member() {
let group = mixed_group();
let m = group.find_align_match(&Point2::new(1.0, -5.0));
assert_relative_eq!(
m.normal.into_inner(),
Vector2::new(0.0, -1.0),
epsilon = 1e-12
);
let m = group.find_align_match(&Point2::new(5.0, 1.0));
assert_relative_eq!(
m.normal.into_inner(),
Vector2::new(1.0, 0.0),
epsilon = 1e-12
);
}
}