|
|
|
@ -1,6 +1,6 @@
@@ -1,6 +1,6 @@
|
|
|
|
|
use std::fmt; |
|
|
|
|
|
|
|
|
|
use super::{eqn, Value, Scalar, Expr, Point2, Rot2}; |
|
|
|
|
use super::{eqn, Expr, Point2, Rot2, Scalar, Value}; |
|
|
|
|
|
|
|
|
|
// pub type Vec2 = nalgebra::Vector2<Value>;
|
|
|
|
|
// pub type Point2 = nalgebra::Point2<Value>;
|
|
|
|
@ -39,7 +39,7 @@ impl fmt::Display for Region1 {
@@ -39,7 +39,7 @@ impl fmt::Display for Region1 {
|
|
|
|
|
Singleton(v) => write!(f, "{{ {} }}", v), |
|
|
|
|
Range(l, u) => write!(f, "[ {}, {} ]", l, u), |
|
|
|
|
Intersection(r1, r2) => write!(f, "{} ∩ {}", r1, r2), |
|
|
|
|
Full => write!(f, "ℝ") |
|
|
|
|
Full => write!(f, "ℝ"), |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -119,10 +119,7 @@ impl Region<Scalar> for Region1 {
@@ -119,10 +119,7 @@ impl Region<Scalar> for Region1 {
|
|
|
|
|
}, |
|
|
|
|
_ => None, |
|
|
|
|
}, |
|
|
|
|
Intersection(r1, r2) => { |
|
|
|
|
unimplemented!() |
|
|
|
|
} |
|
|
|
|
/*Union(r1, r2) => {
|
|
|
|
|
Intersection(r1, r2) => unimplemented!(), /*Union(r1, r2) => {
|
|
|
|
|
let distance = |a: Scalar, b: Scalar| (a - b).abs(); |
|
|
|
|
match (r1.nearest(s), r2.nearest(s)) { |
|
|
|
|
(None, None) => None, |
|
|
|
@ -151,7 +148,11 @@ pub struct Line2 {
@@ -151,7 +148,11 @@ pub struct Line2 {
|
|
|
|
|
|
|
|
|
|
impl fmt::Display for Line2 { |
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
|
|
|
|
write!(f, "{{ <x, y> = {} + {} * {} }}", self.start, self.dir, self.extent) |
|
|
|
|
write!( |
|
|
|
|
f, |
|
|
|
|
"{{ <x, y> = {} + {} * {} }}", |
|
|
|
|
self.start, self.dir, self.extent |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -172,7 +173,11 @@ impl Line2 {
@@ -172,7 +173,11 @@ impl Line2 {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn with_extent(self, new_extent: Region1) -> Line2 { |
|
|
|
|
Line2 { start: self.start, dir: self.dir, extent: new_extent } |
|
|
|
|
Line2 { |
|
|
|
|
start: self.start, |
|
|
|
|
dir: self.dir, |
|
|
|
|
extent: new_extent, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn nearest(&self, p: &Point2<Value>) -> Point2<Value> { |
|
|
|
@ -182,7 +187,7 @@ impl Line2 {
@@ -182,7 +187,7 @@ impl Line2 {
|
|
|
|
|
match self.intersect(&perp) { |
|
|
|
|
Region2::Singleton(np) => np, |
|
|
|
|
Region2::Line(l) => l.evaluate_extent().expect("Line2::nearest not found"), |
|
|
|
|
_ => panic!("Line2::nearest not found!") |
|
|
|
|
_ => panic!("Line2::nearest not found!"), |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -208,7 +213,8 @@ impl Line2 {
@@ -208,7 +213,8 @@ impl Line2 {
|
|
|
|
|
b.dir.clone(), |
|
|
|
|
); |
|
|
|
|
let (a_c, a_s, b_c, b_s) = (a_v.cos(), a_v.sin(), b_v.cos(), b_v.sin()); |
|
|
|
|
let t_b = (a_0.x.clone() * a_s.clone() - a_0.y.clone() * a_c.clone() |
|
|
|
|
let t_b = (a_0.x.clone() * a_s.clone() |
|
|
|
|
- a_0.y.clone() * a_c.clone() |
|
|
|
|
- b_0.x.clone() * a_s.clone() |
|
|
|
|
+ b_0.y.clone() * a_c.clone()) |
|
|
|
|
/ (a_s.clone() * b_c.clone() - a_c.clone() * b_s.clone()); |
|
|
|
@ -225,7 +231,11 @@ impl Line2 {
@@ -225,7 +231,11 @@ impl Line2 {
|
|
|
|
|
dir: self.dir, |
|
|
|
|
extent: self.extent.simplify(), |
|
|
|
|
}; |
|
|
|
|
trace!("line {}: simplify evaluate extent: {:?}", new_l, new_l.evaluate_extent()); |
|
|
|
|
trace!( |
|
|
|
|
"line {}: simplify evaluate extent: {:?}", |
|
|
|
|
new_l, |
|
|
|
|
new_l.evaluate_extent() |
|
|
|
|
); |
|
|
|
|
if let Some(p) = new_l.evaluate_extent() { |
|
|
|
|
return Region2::Singleton(p.simplify()); |
|
|
|
|
} |
|
|
|
@ -261,7 +271,7 @@ impl fmt::Display for Region2 {
@@ -261,7 +271,7 @@ impl fmt::Display for Region2 {
|
|
|
|
|
Singleton(v) => write!(f, "{{ {} }}", v), |
|
|
|
|
Line(l) => l.fmt(f), |
|
|
|
|
Intersection(r1, r2) => write!(f, "{} ∩ {}", r1, r2), |
|
|
|
|
Full => write!(f, "ℝ²") |
|
|
|
|
Full => write!(f, "ℝ²"), |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -332,8 +342,7 @@ impl Region<Point2<Scalar>> for Region2 {
@@ -332,8 +342,7 @@ impl Region<Point2<Scalar>> for Region2 {
|
|
|
|
|
Intersection(r1, r2) => { |
|
|
|
|
None |
|
|
|
|
// r1.clone().intersect((**r2).clone()).nearest(p)
|
|
|
|
|
} |
|
|
|
|
/*Union(r1, r2) => {
|
|
|
|
|
} /*Union(r1, r2) => {
|
|
|
|
|
use nalgebra::distance; |
|
|
|
|
match (r1.nearest(p), r2.nearest(p)) { |
|
|
|
|
(None, None) => None, |
|
|
|
@ -357,7 +366,8 @@ impl Region<Point2<Value>> for Region2 {
@@ -357,7 +366,8 @@ impl Region<Point2<Value>> for Region2 {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn contains(&self, p: &Point2<Value>) -> Option<bool> { |
|
|
|
|
self.nearest(p).map(|n| n.simplify() == p.clone().simplify()) |
|
|
|
|
self.nearest(p) |
|
|
|
|
.map(|n| n.simplify() == p.clone().simplify()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn nearest(&self, p: &Point2<Value>) -> Option<Point2<Value>> { |
|
|
|
@ -367,10 +377,7 @@ impl Region<Point2<Value>> for Region2 {
@@ -367,10 +377,7 @@ impl Region<Point2<Value>> for Region2 {
|
|
|
|
|
Full => Some(p.clone()), |
|
|
|
|
Singleton(n) => Some(n.clone()), |
|
|
|
|
Line(line) => Some(line.nearest(p)), |
|
|
|
|
Intersection(r1, r2) => { |
|
|
|
|
r1.clone().intersect((**r2).clone()).nearest(p) |
|
|
|
|
} |
|
|
|
|
/*Union(r1, r2) => {
|
|
|
|
|
Intersection(r1, r2) => r1.clone().intersect((**r2).clone()).nearest(p), /*Union(r1, r2) => {
|
|
|
|
|
use nalgebra::distance; |
|
|
|
|
match (r1.nearest(p), r2.nearest(p)) { |
|
|
|
|
(None, None) => None, |
|
|
|
@ -419,9 +426,7 @@ impl Region2 {
@@ -419,9 +426,7 @@ impl Region2 {
|
|
|
|
|
Region2::intersection(Singleton(n), o) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
(Intersection(r1, r2), o) | (o, Intersection(r1, r2)) => { |
|
|
|
|
r1.intersect(*r2).intersect(o) |
|
|
|
|
} |
|
|
|
|
(Intersection(r1, r2), o) | (o, Intersection(r1, r2)) => r1.intersect(*r2).intersect(o), |
|
|
|
|
(Line(l1), Line(l2)) => l1.intersect(&l2).simplify(), |
|
|
|
|
/*(Union(un1, un2), o) | (o, Union(un1, un2)) => {
|
|
|
|
|
Self::union(un1.intersect(o), un2.intersect(o)) |
|
|
|
|