Skip to main content

Polygon

Struct Polygon 

Source
pub struct Polygon { /* private fields */ }
Expand description

A simple polygon, optionally with holes, on the i16 lattice.

§Invariants

A Polygon can only be constructed through Polygon::new or Polygon::from_outer, which enforce that:

  • Ring 0 is the outer boundary; rings 1.. are holes.
  • The outer ring winds counter-clockwise and holes wind clockwise, so the polygon’s interior is always on the left of every directed edge. Rings supplied the other way round are reversed automatically.
  • Every coordinate is within Point::MIN_COORD..=Point::MAX_COORD.
  • Every ring has at least 3 vertices, no repeated consecutive vertices, and encloses a non-zero area.
  • No two edges cross, and no vertex is a zero-width spike.
  • Every hole lies inside the outer ring.

The uniform “interior on the left” rule is what lets the wavefront treat outer boundary and holes identically — see docs/ALGORITHM.md.

§Vertex and edge numbering

Vertices are numbered 0..n across all rings, outer ring first. Edge i starts at vertex i; see EdgeId.

§Examples

use straight_skeleton::{Point, Polygon};

// A square. Winding is fixed up for you.
let square = Polygon::from_outer(&[
    Point::new(0, 0),
    Point::new(10, 0),
    Point::new(10, 10),
    Point::new(0, 10),
])?;
assert_eq!(square.vertex_count(), 4);
assert_eq!(square.ring_count(), 1);

// A square with a square hole.
let with_hole = Polygon::new(
    &[Point::new(0, 0), Point::new(30, 0), Point::new(30, 30), Point::new(0, 30)],
    &[vec![
        Point::new(10, 10),
        Point::new(20, 10),
        Point::new(20, 20),
        Point::new(10, 20),
    ]],
)?;
assert_eq!(with_hole.ring_count(), 2);
assert_eq!(with_hole.vertex_count(), 8);

Implementations§

Source§

impl Polygon

Source

pub const MAX_VERTICES: usize

The largest number of vertices a polygon may have.

Bounded by VertexId’s u16, minus one so that “one past the end” indices cannot overflow.

Source

pub fn new(outer: &[Point], holes: &[Vec<Point>]) -> Result<Self, PolygonError>

Builds a polygon from an outer ring and a list of holes.

Ring winding is normalised for you: the outer ring is made counter-clockwise and holes clockwise, reversing any ring given the other way round.

§Errors

Returns a PolygonError naming the offending ring if the input is not a simple polygon with holes. See Polygon’s invariants for the full list of checks.

§Examples
use straight_skeleton::{Point, Polygon, PolygonError};

// A ring that crosses itself is rejected, not silently accepted.
let crossed = Polygon::from_outer(&[
    Point::new(0, 0),
    Point::new(10, 10),
    Point::new(10, 0),
    Point::new(0, 4),
]);
assert!(matches!(crossed, Err(PolygonError::SelfIntersection { .. })));
Source

pub fn from_outer(outer: &[Point]) -> Result<Self, PolygonError>

Builds a polygon with no holes.

§Errors

As Polygon::new.

Source

pub fn vertex_count(&self) -> usize

Total number of vertices across all rings.

Source

pub fn ring_count(&self) -> usize

Number of rings: 1 (outer) plus one per hole.

Source

pub fn hole_count(&self) -> usize

Number of holes.

Source

pub fn vertices(&self) -> &[Point]

All vertices, outer ring first, then holes in order.

Source

pub fn vertex(&self, v: VertexId) -> Point

The position of a vertex.

§Panics

Panics if v does not belong to this polygon.

Source

pub fn ring(&self, ring: RingId) -> &[Point]

The vertices of one ring, in order.

§Panics

Panics if ring does not belong to this polygon.

Source

pub fn ring_of(&self, v: VertexId) -> RingId

Which ring a vertex belongs to.

§Panics

Panics if v does not belong to this polygon.

Source

pub fn rings(&self) -> impl Iterator<Item = &[Point]> + '_

Iterates every ring’s vertices in order, outer ring first.

Source

pub fn next_vertex(&self, v: VertexId) -> VertexId

The vertex following v within its ring, wrapping at the ring’s end.

Source

pub fn prev_vertex(&self, v: VertexId) -> VertexId

The vertex preceding v within its ring, wrapping at the ring’s start.

Source

pub fn edge(&self, e: EdgeId) -> (Point, Point)

The endpoints of an edge, in direction order.

The polygon’s interior lies to the left of start -> end.

§Panics

Panics if e does not belong to this polygon.

Source

pub fn edge_count(&self) -> usize

Total number of edges, which equals the number of vertices.

Source

pub fn edge_ids(&self) -> impl Iterator<Item = EdgeId> + '_

Iterates every edge id.

Source

pub fn vertex_ids(&self) -> impl Iterator<Item = VertexId> + '_

Iterates every vertex id.

Source

pub fn is_reflex(&self, v: VertexId) -> bool

Whether the interior angle at v exceeds 180°, i.e. v is a reflex (“notch”) corner.

Reflex vertices are the only ones that can trigger split events, so this drives the algorithm’s main branch.

Source

pub fn signed_area2(&self) -> i64

Twice the signed area of the polygon: the outer ring’s area minus every hole’s. Always positive for a valid polygon.

Trait Implementations§

Source§

impl Clone for Polygon

Source§

fn clone(&self) -> Polygon

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Polygon

Source§

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

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

impl<'de> Deserialize<'de> for Polygon

Source§

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

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

impl Eq for Polygon

Source§

impl PartialEq for Polygon

Source§

fn eq(&self, other: &Polygon) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl Serialize for Polygon

Source§

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

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

impl StructuralPartialEq for Polygon

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

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

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> ToOwned for T
where T: Clone,

Source§

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<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

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>,

Source§

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.