|
|
@ -42,8 +42,33 @@ impl Region<Scalar> for Region1 { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
fn nearest(&self, n: &Scalar) -> Option<Scalar> { |
|
|
|
fn nearest(&self, s: &Scalar) -> Option<Scalar> { |
|
|
|
unimplemented!(); |
|
|
|
use Region1::*; |
|
|
|
|
|
|
|
match self { |
|
|
|
|
|
|
|
Empty => None, |
|
|
|
|
|
|
|
Full => Some(*s), |
|
|
|
|
|
|
|
Singleton(n) => Some(*n), |
|
|
|
|
|
|
|
Range(l, u) => match (l < s, s < u) { |
|
|
|
|
|
|
|
(true, true) => Some(*s), |
|
|
|
|
|
|
|
(true, false) => Some(*u), |
|
|
|
|
|
|
|
(false, true) => Some(*l), |
|
|
|
|
|
|
|
_ => None, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
Union(r1, r2) => { |
|
|
|
|
|
|
|
let distance = |a: Scalar, b: Scalar| (a - b).abs(); |
|
|
|
|
|
|
|
match (r1.nearest(s), r2.nearest(s)) { |
|
|
|
|
|
|
|
(None, None) => None, |
|
|
|
|
|
|
|
(Some(n), None) | (None, Some(n)) => Some(n), |
|
|
|
|
|
|
|
(Some(n1), Some(n2)) => Some({ |
|
|
|
|
|
|
|
if distance(*s, n1) <= distance(*s, n2) { |
|
|
|
|
|
|
|
n1 |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
n2 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -153,6 +178,15 @@ impl Region<Point2> for Region2 { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl Region2 { |
|
|
|
impl Region2 { |
|
|
|
|
|
|
|
pub fn union(r1: Region2, r2: Region2) -> Region2 { |
|
|
|
|
|
|
|
use Region2::*; |
|
|
|
|
|
|
|
match (r1, r2) { |
|
|
|
|
|
|
|
(Empty, r) | (r, Empty) => r, |
|
|
|
|
|
|
|
(Full, _) | (_, Full) => Full, |
|
|
|
|
|
|
|
(r1, r2) => Union(Box::new(r1), Box::new(r2)), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn intersect(&self, other: &Region2) -> Region2 { |
|
|
|
pub fn intersect(&self, other: &Region2) -> Region2 { |
|
|
|
use Region2::*; |
|
|
|
use Region2::*; |
|
|
|
match (self, other) { |
|
|
|
match (self, other) { |
|
|
@ -173,7 +207,9 @@ impl Region2 { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
(Line(l1), Line(l2)) => l1.intersect(l2), |
|
|
|
(Line(l1), Line(l2)) => l1.intersect(l2), |
|
|
|
_ => unimplemented!(), |
|
|
|
(Union(un1, un2), o) | (o, Union(un1, un2)) => { |
|
|
|
|
|
|
|
Self::union(un1.intersect(o), un2.intersect(o)) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|