diff options
author | Jovansonlee Cesar <ivanceras@gmail.com> | 2021-07-05 07:33:36 +0800 |
---|---|---|
committer | Jovansonlee Cesar <ivanceras@gmail.com> | 2021-07-05 07:33:36 +0800 |
commit | 362b965f9d612170ed32a7dcd202d7f2d83801ff (patch) | |
tree | 08979bf34114b0a05e2c5ddff01dfa0f99a8c45b | |
parent | 98ad45f90aa6252c191aee3f25f6b4f1e18107fc (diff) |
Initial implementation for testing hit/intersection AABB to any fragment
-rw-r--r-- | svgbob/src/buffer/cell_buffer/cell.rs | 1 | ||||
-rw-r--r-- | svgbob/src/buffer/fragment_buffer/fragment.rs | 47 | ||||
-rw-r--r-- | svgbob/src/buffer/fragment_buffer/fragment/line.rs | 13 | ||||
-rw-r--r-- | svgbob/src/buffer/fragment_buffer/fragment/polygon.rs | 13 | ||||
-rw-r--r-- | svgbob/src/buffer/fragment_buffer/fragment/rect.rs | 16 |
5 files changed, 85 insertions, 5 deletions
diff --git a/svgbob/src/buffer/cell_buffer/cell.rs b/svgbob/src/buffer/cell_buffer/cell.rs index 3768c35..81150fe 100644 --- a/svgbob/src/buffer/cell_buffer/cell.rs +++ b/svgbob/src/buffer/cell_buffer/cell.rs @@ -1,4 +1,5 @@ use crate::{util, Point}; +use ncollide2d::query::PointQuery; use ncollide2d::{ bounding_volume::AABB, math::Isometry, diff --git a/svgbob/src/buffer/fragment_buffer/fragment.rs b/svgbob/src/buffer/fragment_buffer/fragment.rs index e793e19..6cc111e 100644 --- a/svgbob/src/buffer/fragment_buffer/fragment.rs +++ b/svgbob/src/buffer/fragment_buffer/fragment.rs @@ -4,6 +4,13 @@ pub use arc::Arc; pub use circle::Circle; pub use line::Line; pub use marker_line::{Marker, MarkerLine}; +use ncollide2d::query::PointQuery; +use ncollide2d::{ + bounding_volume::AABB, + math::Isometry, + query::{proximity, Proximity}, + shape::{Polyline, Segment}, +}; pub use polygon::{Polygon, PolygonTag}; pub use rect::Rect; use sauron::Node; @@ -285,6 +292,37 @@ impl Fragment { } } + fn hit(&self, start: Point, end: Point) -> bool { + self.is_intersecting(AABB::new(*start, *end)) + } + + /// check if this fragment is intersecting with this bounding box + fn is_intersecting(&self, bbox: AABB<f32>) -> bool { + let bbox: Polyline<f32> = Polyline::new( + vec![ + *bbox.mins(), + *Point::new(bbox.maxs().x, bbox.mins().y), + *bbox.maxs(), + *Point::new(bbox.mins().x, bbox.maxs().y), + ], + None, + ); + let identity = Isometry::identity(); + match self { + Fragment::Line(line) => { + let segment: Segment<f32> = line.clone().into(); + proximity(&identity, &segment, &identity, &bbox, 0.0) + == Proximity::Intersecting + } + Fragment::Rect(rect) => { + let polyline: Polyline<f32> = rect.clone().into(); + proximity(&identity, &polyline, &identity, &bbox, 0.0) + == Proximity::Intersecting + } + _ => false, + } + } + /// recompute the end points of this fragment /// offset by the cell location pub fn absolute_position(&self, cell: Cell) -> Self { @@ -872,4 +910,13 @@ mod tests { lines1.sort(); assert_eq!(lines1, sorted); } + + #[test] + fn test_hit() { + let line1 = line(CellGrid::a(), CellGrid::y()); + assert!(line1.hit(CellGrid::g(), CellGrid::s())); + + let rect1 = rect(CellGrid::a(), CellGrid::y(), false, false); + assert!(!rect1.hit(CellGrid::g(), CellGrid::s())); + } } diff --git a/svgbob/src/buffer/fragment_buffer/fragment/line.rs b/svgbob/src/buffer/fragment_buffer/fragment/line.rs index 743ab97..7e8be28 100644 --- a/svgbob/src/buffer/fragment_buffer/fragment/line.rs +++ b/svgbob/src/buffer/fragment_buffer/fragment/line.rs @@ -3,9 +3,14 @@ use crate::{ fragment::{marker_line, Bounds, Circle, Marker, MarkerLine}, util, Direction, Point, }; +use ncollide2d::query::PointQuery; +use ncollide2d::{ + bounding_volume::AABB, + query::{proximity, Proximity}, + shape::Polyline, +}; use ncollide2d::{ math::Isometry, - query::point_internal::point_query::PointQuery, shape::{Segment, Shape}, }; use std::{cmp::Ordering, fmt}; @@ -530,6 +535,12 @@ impl<MSG> Into<Node<MSG>> for Line { } } +impl Into<Segment<f32>> for Line { + fn into(self) -> Segment<f32> { + Segment::new(*self.start, *self.end) + } +} + impl Eq for Line {} /// This is needed since this struct contains f32 which rust doesn't provide Eq implementation diff --git a/svgbob/src/buffer/fragment_buffer/fragment/polygon.rs b/svgbob/src/buffer/fragment_buffer/fragment/polygon.rs index b255aad..46d751f 100644 --- a/svgbob/src/buffer/fragment_buffer/fragment/polygon.rs +++ b/svgbob/src/buffer/fragment_buffer/fragment/polygon.rs @@ -175,9 +175,8 @@ impl Polygon { impl Bounds for Polygon { fn bounds(&self) -> (Point, Point) { - let points: Vec<Point2<f32>> = - self.points.iter().map(|p| **p).collect(); - let aabb = Polyline::new(points, None).local_aabb(); + let pl: Polyline<f32> = self.clone().into(); + let aabb = pl.local_aabb(); (Point::from(*aabb.mins()), Point::from(*aabb.maxs())) } } @@ -196,6 +195,14 @@ impl fmt::Display for Polygon { } } +impl Into<Polyline<f32>> for Polygon { + fn into(self) -> Polyline<f32> { + let points: Vec<Point2<f32>> = + self.points.iter().map(|p| **p).collect(); + Polyline::new(points, None) + } +} + impl<MSG> Into<Node<MSG>> for Polygon { fn into(self) -> Node<MSG> { polygon( diff --git a/svgbob/src/buffer/fragment_buffer/fragment/rect.rs b/svgbob/src/buffer/fragment_buffer/fragment/rect.rs index 8d07fdf..0bcad5c 100644 --- a/svgbob/src/buffer/fragment_buffer/fragment/rect.rs +++ b/svgbob/src/buffer/fragment_buffer/fragment/rect.rs @@ -1,7 +1,7 @@ use crate::{fragment::Bounds, util, Cell, Point}; use std::fmt; -use ncollide2d::shape::{Segment, Shape}; +use ncollide2d::shape::{Polyline, Segment, Shape}; use sauron::{ html::attributes::*, svg::{attributes::*, *}, @@ -112,6 +112,20 @@ impl fmt::Display for Rect { } } +impl Into<Polyline<f32>> for Rect { + fn into(self) -> Polyline<f32> { + Polyline::new( + vec![ + *self.start, + *Point::new(self.end.x, self.start.y), + *self.end, + *Point::new(self.start.x, self.end.y), + ], + None, + ) + } +} + impl<MSG> Into<Node<MSG>> for Rect { fn into(self) -> Node<MSG> { rect( |