mirror of https://codeberg.org/topola/topola.git
geometry: implement checking if point is contained in shape
This commit is contained in:
parent
6932a698f6
commit
f2d0de6607
|
|
@ -1,6 +1,7 @@
|
|||
#[macro_use]
|
||||
mod geometry;
|
||||
pub mod primitive;
|
||||
mod shape;
|
||||
pub mod with_rtree;
|
||||
|
||||
pub use geometry::*;
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,8 +1,11 @@
|
|||
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 crate::math::{self, Circle};
|
||||
use crate::{
|
||||
geometry::shape::ShapeTrait,
|
||||
math::{self, Circle},
|
||||
};
|
||||
|
||||
#[enum_dispatch]
|
||||
pub trait PrimitiveShapeTrait {
|
||||
|
|
@ -35,7 +38,7 @@ pub trait PrimitiveShapeTrait {
|
|||
}
|
||||
}
|
||||
|
||||
#[enum_dispatch(PrimitiveShapeTrait)]
|
||||
#[enum_dispatch(ShapeTrait, PrimitiveShapeTrait)]
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub enum PrimitiveShape {
|
||||
// Intentionally in different order to reorder `self.intersects(...)` properly.
|
||||
|
|
@ -49,6 +52,12 @@ pub struct DotShape {
|
|||
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 {
|
||||
fn priority(&self) -> u64 {
|
||||
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 {
|
||||
fn priority(&self) -> u64 {
|
||||
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 {
|
||||
fn priority(&self) -> u64 {
|
||||
1
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
Loading…
Reference in New Issue