Struct libreda_db::layout::prelude::SimplePolygon

source ·
pub struct SimplePolygon<T> { /* private fields */ }
Expand description

A SimplePolygon is a polygon defined by vertices. It does not contain holes but can be self-intersecting.

TODO: Implement Deref for accessing the vertices.

Implementations§

source§

impl<T> SimplePolygon<T>

source

pub fn new_raw(points: Vec<Point<T>>) -> SimplePolygon<T>

Create a new polygon from a list of points. The points are taken as they are, without reordering or simplification.

source

pub fn empty() -> SimplePolygon<T>

Create empty polygon without any vertices.

source

pub fn len(&self) -> usize

Get the number of vertices.

source

pub fn is_empty(&self) -> bool

Check if polygon has no vertices.

source

pub fn iter(&self) -> Iter<'_, Point<T>>

Shortcut for self.points.iter().

source

pub fn points(&self) -> &[Point<T>]

Access the inner list of points.

source§

impl<T> SimplePolygon<T>
where T: PartialOrd,

source

pub fn new(points: Vec<Point<T>>) -> SimplePolygon<T>

Create a new polygon from a list of points. The polygon is normalized by rotating the points.

source

pub fn with_points_mut<R>( &mut self, f: impl FnOnce(&mut Vec<Point<T>>) -> R, ) -> R

Mutably access the inner list of points. If the polygon was normalized, then the modified list of points will be normalized again.

source§

impl<T> SimplePolygon<T>
where T: PartialOrd,

source

pub fn normalize(&mut self)

Rotate the vertices to get the lexicographically smallest polygon. Does not change the orientation.

source

pub fn normalized(self) -> SimplePolygon<T>

Rotate the vertices to get the lexicographically smallest polygon. Does not change the orientation.

source§

impl<T> SimplePolygon<T>
where T: PartialEq,

source

pub fn normalized_eq(&self, other: &SimplePolygon<T>) -> bool

Check if polygons can be made equal by rotating their vertices.

source§

impl<T> SimplePolygon<T>
where T: Copy,

source

pub fn from_rect(rect: &Rect<T>) -> SimplePolygon<T>

Create a new simple polygon from a rectangle.

source§

impl<T> SimplePolygon<T>
where T: Copy,

source

pub fn edges(&self) -> Vec<Edge<T>>

Get all exterior edges of the polygon.

§Examples
use iron_shapes::simple_polygon::SimplePolygon;
use iron_shapes::edge::Edge;
let coords = vec![(0, 0), (1, 0)];

let poly = SimplePolygon::from(coords);

assert_eq!(poly.edges(), vec![Edge::new((0, 0), (1, 0)), Edge::new((1, 0), (0, 0))]);
source

pub fn edges_iter(&self) -> impl Iterator<Item = Edge<T>>

Iterate over all edges.

source§

impl<T> SimplePolygon<T>
where T: CoordinateType,

source

pub fn normalize_orientation<Area>(&mut self)
where Area: Num + PartialOrd + From<T>,

Normalize the points of the polygon such that they are arranged counter-clock-wise.

After normalizing, SimplePolygon::area_doubled_oriented() will return a semi-positive value.

For self-intersecting polygons, the orientation is not clearly defined. For example an 8 shape has not orientation. Here, the oriented area is used to define the orientation.

source

pub fn normalized_orientation<Area>(self) -> SimplePolygon<T>
where Area: Num + PartialOrd + From<T>,

Call normalize_orientation() while taking ownership and returning the result.

source

pub fn orientation<Area>(&self) -> Orientation
where Area: Num + From<T> + PartialOrd,

Get the orientation of the polygon. The orientation is defined by the oriented area. A polygon with a positive area is oriented counter-clock-wise, otherwise it is oriented clock-wise.

§Examples
use iron_shapes::simple_polygon::SimplePolygon;
use iron_shapes::point::Point;
use iron_shapes::types::Orientation;
let coords = vec![(0, 0), (3, 0), (3, 1)];

let poly = SimplePolygon::from(coords);

assert_eq!(poly.orientation::<i64>(), Orientation::CounterClockWise);
source

pub fn convex_hull(&self) -> SimplePolygon<T>
where T: Ord,

Get the convex hull of the polygon.

Implements Andrew’s Monotone Chain algorithm. See: http://geomalgorithms.com/a10-_hull-1.html

source

pub fn is_rectilinear(&self) -> bool

Test if all edges are parallel to the x or y axis.

source

pub fn lower_left_vertex(&self) -> Point<T>

Get the vertex with lowest x-coordinate. Prefer lower y-coordinates to break ties.

§Examples
use iron_shapes::simple_polygon::SimplePolygon;
use iron_shapes::point::Point;
let coords = vec![(0, 0), (1, 0), (-1, 2), (-1, 1)];

let poly = SimplePolygon::from(coords);

assert_eq!(poly.lower_left_vertex(), Point::new(-1, 1));

Trait Implementations§

source§

impl<T> Clone for SimplePolygon<T>
where T: Clone,

source§

fn clone(&self) -> SimplePolygon<T>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<T> Debug for SimplePolygon<T>
where T: Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
source§

impl<'de, T> Deserialize<'de> for SimplePolygon<T>
where T: Deserialize<'de>,

source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<SimplePolygon<T>, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<A, T> DoubledOrientedArea<A> for SimplePolygon<T>
where T: CoordinateType, A: Num + From<T>,

source§

fn area_doubled_oriented(&self) -> A

Calculates the doubled oriented area.

Using doubled area allows to compute in the integers because the area of a polygon with integer coordinates is either integer or half-integer.

The area will be positive if the vertices are listed counter-clockwise, negative otherwise.

Complexity: O(n)

§Examples
use iron_shapes::traits::DoubledOrientedArea;
use iron_shapes::simple_polygon::SimplePolygon;
let coords = vec![(0, 0), (3, 0), (3, 1)];

let poly = SimplePolygon::from(coords);

let area: i64 = poly.area_doubled_oriented();
assert_eq!(area, 3);
source§

impl<T> From<&SimplePolygon<T>> for Polygon<T>
where T: Copy,

Create a polygon from a simple polygon.

source§

fn from(simple_polygon: &SimplePolygon<T>) -> Polygon<T>

Converts to this type from the input type.
source§

impl<I, T, P> From<I> for SimplePolygon<T>
where T: Copy + PartialOrd, I: IntoIterator<Item = P>, Point<T>: From<P>,

Create a polygon from a type that is convertible into an iterator of values convertible to Points.

source§

fn from(iter: I) -> SimplePolygon<T>

Converts to this type from the input type.
source§

impl<T> From<SimplePolygon<T>> for Geometry<T>

source§

fn from(x: SimplePolygon<T>) -> Geometry<T>

Converts to this type from the input type.
source§

impl<T> From<SimplePolygon<T>> for Polygon<T>

Create a polygon from a simple polygon.

source§

fn from(simple_polygon: SimplePolygon<T>) -> Polygon<T>

Converts to this type from the input type.
source§

impl<T, P> FromIterator<P> for SimplePolygon<T>
where T: Copy, P: Into<Point<T>>,

Create a polygon from a iterator of values convertible to Points.

source§

fn from_iter<I>(iter: I) -> SimplePolygon<T>
where I: IntoIterator<Item = P>,

Creates a value from an iterator. Read more
source§

impl<T> Hash for SimplePolygon<T>
where T: Hash,

source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<'a, T> IntoIterator for &'a SimplePolygon<T>

§

type Item = &'a Point<T>

The type of the elements being iterated over.
§

type IntoIter = Iter<'a, Point<T>>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> <&'a SimplePolygon<T> as IntoIterator>::IntoIter

Creates an iterator from a value. Read more
source§

impl<T> MapPointwise<T> for SimplePolygon<T>
where T: CoordinateType,

source§

fn transform<F>(&self, tf: F) -> SimplePolygon<T>
where F: Fn(Point<T>) -> Point<T>,

Point wise transformation.
source§

impl<T> PartialEq for SimplePolygon<T>
where T: PartialEq,

source§

fn eq(&self, other: &SimplePolygon<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T> Serialize for SimplePolygon<T>
where T: Serialize,

source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<T> TryBoundingBox<T> for SimplePolygon<T>
where T: Copy + PartialOrd,

source§

fn try_bounding_box(&self) -> Option<Rect<T>>

Return the bounding box of this geometry if a bounding box is defined.
source§

impl<T, Dst> TryCastCoord<T, Dst> for SimplePolygon<T>

§

type Output = SimplePolygon<Dst>

Output type of the cast. This is likely the same geometrical type just with other coordinate types.
source§

fn try_cast(&self) -> Option<<SimplePolygon<T> as TryCastCoord<T, Dst>>::Output>

Try to cast to target data type. Read more
source§

fn cast(&self) -> Self::Output

Cast to target data type. Read more
source§

impl<T> WindingNumber<T> for SimplePolygon<T>
where T: CoordinateType,

source§

fn winding_number(&self, point: Point<T>) -> isize

Calculate the winding number of the polygon around this point.

TODO: Define how point on edges and vertices is handled.

See: http://geomalgorithms.com/a03-_inclusion.html

source§

fn contains_point_non_oriented(&self, point: Point<T>) -> bool

Check if point is inside the polygon, i.e. the polygons winds around the point a non-zero number of times. Read more
source§

fn contains_point(&self, point: Point<T>) -> bool

Check if point is inside the polygon, i.e. the polygon winds around the point an odd number of times. Read more
source§

impl<T> Eq for SimplePolygon<T>
where T: Eq,

source§

impl<T> StructuralPartialEq for SimplePolygon<T>

Auto Trait Implementations§

§

impl<T> Freeze for SimplePolygon<T>

§

impl<T> RefUnwindSafe for SimplePolygon<T>
where T: RefUnwindSafe,

§

impl<T> Send for SimplePolygon<T>
where T: Send,

§

impl<T> Sync for SimplePolygon<T>
where T: Sync,

§

impl<T> Unpin for SimplePolygon<T>
where T: Unpin,

§

impl<T> UnwindSafe for SimplePolygon<T>
where T: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

impl<S, T> Mirror<T> for S
where T: Copy + Zero + Sub<Output = T>, S: MapPointwise<T>,

source§

fn mirror_x(&self) -> S

Return the geometrical object mirrored at the x axis.

source§

fn mirror_y(&self) -> S

Return the geometrical object mirrored at the y axis.

source§

impl<S, T> RotateOrtho<T> for S
where T: Copy + Zero + Sub<Output = T>, S: MapPointwise<T>,

source§

fn rotate_ortho(&self, a: Angle) -> S

Rotate the geometrical shape by a multiple of 90 degrees.
source§

impl<S, T> Scale<T> for S
where T: Copy + Mul<Output = T>, S: MapPointwise<T>,

source§

fn scale(&self, factor: T) -> S

Scale the geometrical shape. Scaling center is the origin (0, 0).
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<S, T> Translate<T> for S
where T: Copy + Add<Output = T>, S: MapPointwise<T>,

source§

fn translate(&self, v: Vector<T>) -> S

Translate the geometrical object by a vector v.
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

source§

impl<T> IdType for T
where T: Debug + Clone + Eq + Hash + 'static,

source§

impl<T> IdTypeMT for T
where T: IdType + Sync + Send,

source§

impl<T> TextType for T
where T: Eq + Hash + Clone + Debug,