summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJovansonlee Cesar <ivanceras@gmail.com>2021-07-06 07:11:04 +0800
committerJovansonlee Cesar <ivanceras@gmail.com>2021-07-06 07:11:04 +0800
commit0963aea5052ae8d4905cf0abb51dfe601c12b7e6 (patch)
tree37c751adf2706cfb4134a40ad806c5961a545e95
parentacad7ac5c46de8c4f39c1f1cf1e073ee08e78604 (diff)
Include circle in intersection test
-rw-r--r--svgbob/src/buffer/cell_buffer.rs4
-rw-r--r--svgbob/src/buffer/fragment_buffer/fragment.rs14
-rw-r--r--svgbob/src/buffer/fragment_buffer/fragment/circle.rs19
-rw-r--r--svgbob/src/buffer/fragment_buffer/fragment/rect.rs1
4 files changed, 34 insertions, 4 deletions
diff --git a/svgbob/src/buffer/cell_buffer.rs b/svgbob/src/buffer/cell_buffer.rs
index dfaae2d..47b8352 100644
--- a/svgbob/src/buffer/cell_buffer.rs
+++ b/svgbob/src/buffer/cell_buffer.rs
@@ -195,9 +195,6 @@ impl CellBuffer {
/// return fragments that are Rect, Circle,
pub fn get_shapes_fragment(&self, settings: &Settings) -> Vec<Fragment> {
let (single_member, _, endorsed_fragments) = self.group_single_members_from_other_fragments(settings);
- dbg!(&single_member);
- dbg!(&endorsed_fragments);
-
endorsed_fragments.into_iter().chain(single_member.into_iter()
.filter(|frag|frag.is_rect() || frag.is_circle())
).collect()
@@ -244,6 +241,7 @@ impl CellBuffer {
}
/// group nodes that can be group and the rest will be fragments
+ /// Note: The grouped fragments is scaled here
fn group_nodes_and_fragments<MSG>(
&self,
settings: &Settings,
diff --git a/svgbob/src/buffer/fragment_buffer/fragment.rs b/svgbob/src/buffer/fragment_buffer/fragment.rs
index f938b04..8eeacac 100644
--- a/svgbob/src/buffer/fragment_buffer/fragment.rs
+++ b/svgbob/src/buffer/fragment_buffer/fragment.rs
@@ -297,7 +297,7 @@ impl Fragment {
}
/// check if this fragment is intersecting with this bounding box
- fn is_intersecting(&self, bbox: AABB<f32>) -> bool {
+ pub fn is_intersecting(&self, bbox: AABB<f32>) -> bool {
let bbox: Polyline<f32> = Polyline::new(
vec![
*bbox.mins(),
@@ -319,6 +319,11 @@ impl Fragment {
proximity(&identity, &polyline, &identity, &bbox, 0.0)
== Proximity::Intersecting
}
+ Fragment::Circle(circle) => {
+ let polyline: Polyline<f32> = circle.clone().into();
+ proximity(&identity, &polyline, &identity, &bbox, 0.0)
+ == Proximity::Intersecting
+ }
_ => false,
}
}
@@ -414,6 +419,13 @@ impl Fragment {
}
}
+ pub fn as_rect(&self) -> Option<&Rect> {
+ match self {
+ Fragment::Rect(ref rect) => Some(rect),
+ _ => None,
+ }
+ }
+
pub fn as_polygon(&self) -> Option<&Polygon> {
match self {
Fragment::Polygon(polygon) => Some(polygon),
diff --git a/svgbob/src/buffer/fragment_buffer/fragment/circle.rs b/svgbob/src/buffer/fragment_buffer/fragment/circle.rs
index e3f9717..f43428a 100644
--- a/svgbob/src/buffer/fragment_buffer/fragment/circle.rs
+++ b/svgbob/src/buffer/fragment_buffer/fragment/circle.rs
@@ -1,4 +1,6 @@
use crate::{fragment::Bounds, util, Cell, Point};
+use ncollide2d::procedural;
+use ncollide2d::shape::Polyline;
use std::{cmp::Ordering, fmt};
use sauron::{
@@ -104,3 +106,20 @@ impl PartialEq for Circle {
self.cmp(other) == Ordering::Equal
}
}
+
+impl Into<Polyline<f32>> for Circle {
+ fn into(self) -> Polyline<f32> {
+ use nalgebra::Point2;
+ let pl = procedural::circle(&(self.radius * 2.0), 64);
+ let mut points = pl.coords().to_vec();
+ let orig_len = points.len();
+ points.dedup();
+
+ let adjusted: Vec<Point2<f32>> = points
+ .into_iter()
+ .map(|p| *Point::new(p.x + self.center.x, p.y + self.center.y))
+ .collect();
+
+ Polyline::new(adjusted, None)
+ }
+}
diff --git a/svgbob/src/buffer/fragment_buffer/fragment/rect.rs b/svgbob/src/buffer/fragment_buffer/fragment/rect.rs
index 0bcad5c..51f8481 100644
--- a/svgbob/src/buffer/fragment_buffer/fragment/rect.rs
+++ b/svgbob/src/buffer/fragment_buffer/fragment/rect.rs
@@ -120,6 +120,7 @@ impl Into<Polyline<f32>> for Rect {
*Point::new(self.end.x, self.start.y),
*self.end,
*Point::new(self.start.x, self.end.y),
+ *self.start,
],
None,
)