Skip to content

Commit

Permalink
cleanup + extra BB's for paths don't help!
Browse files Browse the repository at this point in the history
  • Loading branch information
aeplay committed Dec 3, 2016
1 parent c387a8f commit 560d6c4
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 35 deletions.
35 changes: 1 addition & 34 deletions lib/descartes/src/intersect.rs
@@ -1,4 +1,4 @@
use super::{N, V2, P2, RoughlyComparable, Curve, FiniteCurve, THICKNESS, WithUniqueOrthogonal};
use super::{N, P2, RoughlyComparable, Curve, FiniteCurve, THICKNESS, WithUniqueOrthogonal, HasBoundingBox};
use super::primitives::{Line, Circle, Segment};
use super::nalgebra::{Dot, Norm};
use super::path::Path;
Expand Down Expand Up @@ -128,39 +128,6 @@ impl<'a> Intersect for (&'a Circle, &'a Circle) {
}
}

struct BoundingBox {
min: P2,
max: P2
}

impl BoundingBox{
fn overlaps(&self, other: &BoundingBox) -> bool {
self.max.x >= other.min.x && other.max.x >= self.min.x
&& self.max.y >= other.min.y && other.max.y >= self.min.y
}
}

trait HasBoundingBox {
fn bounding_box(&self) -> BoundingBox;
}

impl HasBoundingBox for Segment {
fn bounding_box(&self) -> BoundingBox {
if self.is_linear() {
BoundingBox{
min: P2::new(self.start.x.min(self.end.x), self.start.y.min(self.end.y)),
max: P2::new(self.start.x.max(self.end.x), self.start.y.max(self.end.y)),
}
} else {
let half_diagonal = V2::new(self.radius(), self.radius());
BoundingBox{
min: self.center() - half_diagonal,
max: self.center() + half_diagonal
}
}
}
}

// TODO: optimize: use something better than .includes()
impl<'a> Intersect for (&'a Segment, &'a Segment) {
fn intersect(&self) -> Vec<Intersection> {
Expand Down
43 changes: 43 additions & 0 deletions lib/descartes/src/lib.rs
Expand Up @@ -171,4 +171,47 @@ pub trait FiniteCurve : Curve {

pub trait Shape {
fn contains(&self, point: P2) -> bool;
}

#[derive(Copy, Clone)]
pub struct BoundingBox {
min: P2,
max: P2
}

impl BoundingBox{
pub fn infinite() -> Self {
BoundingBox{
min: P2::new(std::f32::INFINITY, std::f32::INFINITY),
max: P2::new(std::f32::INFINITY, std::f32::INFINITY),
}
}

pub fn overlaps(&self, other: &BoundingBox) -> bool {
self.max.x >= other.min.x && other.max.x >= self.min.x
&& self.max.y >= other.min.y && other.max.y >= self.min.y
}
}

impl ::std::iter::Sum<BoundingBox> for BoundingBox {
fn sum<I: IntoIterator<Item=BoundingBox>>(iter: I) -> Self {
let mut bb = BoundingBox::infinite();

for other_bb in iter {
bb.min = P2::new(
bb.min.x.min(other_bb.min.x),
bb.min.y.min(other_bb.min.y),
);
bb.max = P2::new(
bb.max.x.max(other_bb.max.x),
bb.max.y.max(other_bb.max.y),
);
}

bb
}
}

pub trait HasBoundingBox {
fn bounding_box(&self) -> BoundingBox;
}
20 changes: 19 additions & 1 deletion lib/descartes/src/primitives.rs
Expand Up @@ -2,7 +2,8 @@ use super::{
N, P2, V2, THICKNESS,
Curve, FiniteCurve,
WithUniqueOrthogonal, angle_along_to,
RoughlyComparable, Intersect, Intersection
RoughlyComparable, Intersect, Intersection,
HasBoundingBox, BoundingBox
};
use ::nalgebra::{Dot, Norm, rotate, Vector1, Rotation2};

Expand Down Expand Up @@ -329,4 +330,21 @@ impl<'a> RoughlyComparable for &'a Segment {
&& self.start_direction().is_roughly(other.start_direction())
&& self.end_direction().is_roughly(other.end_direction())
}
}

impl HasBoundingBox for Segment {
fn bounding_box(&self) -> BoundingBox {
if self.is_linear() {
BoundingBox{
min: P2::new(self.start.x.min(self.end.x), self.start.y.min(self.end.y)),
max: P2::new(self.start.x.max(self.end.x), self.start.y.max(self.end.y)),
}
} else {
let half_diagonal = V2::new(self.radius(), self.radius());
BoundingBox{
min: self.center() - half_diagonal,
max: self.center() + half_diagonal
}
}
}
}

0 comments on commit 560d6c4

Please sign in to comment.