geometry: implement checking if point is contained in shape

This commit is contained in:
Mikolaj Wielgus 2024-04-10 03:01:16 +00:00
parent 6932a698f6
commit f2d0de6607
4 changed files with 46 additions and 3 deletions

View File

@ -1,6 +1,7 @@
#[macro_use] #[macro_use]
mod geometry; mod geometry;
pub mod primitive; pub mod primitive;
mod shape;
pub mod with_rtree; pub mod with_rtree;
pub use geometry::*; pub use geometry::*;

11
src/geometry/polygon.rs Normal file
View File

@ -0,0 +1,11 @@
use enum_dispatch::enum_dispatch;
pub struct PolygonShape {
pub polygon: Polygon,
}
impl ShapeTrait for PolygonShape {
fn contains_point(&self, p: Point) -> bool {
self.polygon.contains(p)
}
}

View File

@ -1,8 +1,11 @@
use enum_dispatch::enum_dispatch; use enum_dispatch::enum_dispatch;
use geo::{point, polygon, EuclideanDistance, Intersects, Point, Polygon, Rotate}; use geo::{point, polygon, Contains, EuclideanDistance, Intersects, Point, Polygon, Rotate};
use rstar::{RTreeObject, AABB}; use rstar::{RTreeObject, AABB};
use crate::math::{self, Circle}; use crate::{
geometry::shape::ShapeTrait,
math::{self, Circle},
};
#[enum_dispatch] #[enum_dispatch]
pub trait PrimitiveShapeTrait { pub trait PrimitiveShapeTrait {
@ -35,7 +38,7 @@ pub trait PrimitiveShapeTrait {
} }
} }
#[enum_dispatch(PrimitiveShapeTrait)] #[enum_dispatch(ShapeTrait, PrimitiveShapeTrait)]
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
pub enum PrimitiveShape { pub enum PrimitiveShape {
// Intentionally in different order to reorder `self.intersects(...)` properly. // Intentionally in different order to reorder `self.intersects(...)` properly.
@ -49,6 +52,12 @@ pub struct DotShape {
pub c: Circle, pub c: Circle,
} }
impl ShapeTrait for DotShape {
fn contains_point(&self, p: Point) -> bool {
p.euclidean_distance(&self.c.pos) <= self.c.r
}
}
impl PrimitiveShapeTrait for DotShape { impl PrimitiveShapeTrait for DotShape {
fn priority(&self) -> u64 { fn priority(&self) -> u64 {
3 3
@ -143,6 +152,12 @@ impl SegShape {
} }
} }
impl ShapeTrait for SegShape {
fn contains_point(&self, p: Point) -> bool {
self.polygon().contains(&p)
}
}
impl PrimitiveShapeTrait for SegShape { impl PrimitiveShapeTrait for SegShape {
fn priority(&self) -> u64 { fn priority(&self) -> u64 {
2 2
@ -252,6 +267,13 @@ impl BendShape {
} }
} }
impl ShapeTrait for BendShape {
fn contains_point(&self, p: Point) -> bool {
let d = p.euclidean_distance(&self.c.pos);
self.between_ends(p) && d >= self.inner_circle().r && d <= self.outer_circle().r
}
}
impl PrimitiveShapeTrait for BendShape { impl PrimitiveShapeTrait for BendShape {
fn priority(&self) -> u64 { fn priority(&self) -> u64 {
1 1

9
src/geometry/shape.rs Normal file
View File

@ -0,0 +1,9 @@
use enum_dispatch::enum_dispatch;
use geo::Point;
use crate::geometry::primitive::PrimitiveShape;
#[enum_dispatch]
pub trait ShapeTrait {
fn contains_point(&self, p: Point) -> bool;
}