1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
// Copyright (c) 2018-2020 Thomas Kramer.
// SPDX-FileCopyrightText: 2018-2022 Thomas Kramer
//
// SPDX-License-Identifier: AGPL-3.0-or-later
//! This module contains data types and functions for polygons with holes.
use crate::CoordinateType;
use crate::edge::Edge;
use crate::point::Point;
use crate::rect::Rect;
pub use crate::traits::{BoundingBox, DoubledOrientedArea, MapPointwise, WindingNumber};
pub use crate::simple_polygon::*;
use crate::types::*;
use crate::traits::TryCastCoord;
use itertools::Itertools;
use num_traits::{Num, NumCast};
use std::cmp::{Ord, PartialEq};
use std::iter::FromIterator;
/// A polygon possibly with holes. The polygon is defined by a hull and a list of holes
/// which are both `SimplePolygon`s.
#[derive(Clone, Hash, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Polygon<T> {
/// The outer hull of the polygon.
pub exterior: SimplePolygon<T>,
/// A list of holes in the polygon.
pub interiors: Vec<SimplePolygon<T>>,
}
/// Shorthand notation for creating a polygon.
///
/// # Example
/// ```
/// # #[macro_use]
/// # extern crate iron_shapes;
/// # fn main() {
/// use iron_shapes::prelude::*;
/// let p = polygon!((0, 0), (1, 0), (1, 1));
/// assert_eq!(p, Polygon::new(vec![(0, 0), (1, 0), (1, 1)]));
/// # }
/// ```
#[macro_export]
macro_rules! polygon {
($($x:expr),*) => {Polygon::new((vec![$($x),*]))}
}
/// Create a polygon from a `Vec` of values convertible to `Point`s.
impl<'a, T, P> From<&'a Vec<P>> for Polygon<T>
where
T: CoordinateType,
Point<T>: From<&'a P>,
{
fn from(vec: &'a Vec<P>) -> Self {
Polygon {
exterior: vec.into(),
interiors: Vec::new(),
}
}
}
/// Create a polygon from a `Vec` of values convertible to `Point`s.
impl<T, P> From<Vec<P>> for Polygon<T>
where
T: Copy + PartialOrd,
Point<T>: From<P>,
{
fn from(vec: Vec<P>) -> Self {
Polygon {
exterior: vec.into(),
interiors: Vec::new(),
}
}
}
/// Create a polygon from a iterator of values convertible to `Point`s.
impl<T, P> FromIterator<P> for Polygon<T>
where
T: Copy,
P: Into<Point<T>>,
{
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = P>,
{
let exterior: SimplePolygon<T> = SimplePolygon::from_iter(iter);
Polygon {
exterior,
interiors: Vec::new(),
}
}
}
/// Create a polygon from a simple polygon.
impl<T> From<SimplePolygon<T>> for Polygon<T> {
fn from(simple_polygon: SimplePolygon<T>) -> Self {
Polygon {
exterior: simple_polygon,
interiors: Vec::new(),
}
}
}
/// Create a polygon from a simple polygon.
impl<T> From<&SimplePolygon<T>> for Polygon<T>
where
T: Copy,
{
fn from(simple_polygon: &SimplePolygon<T>) -> Self {
simple_polygon.clone().into()
}
}
/// Create a polygon from a rectangle.
impl<T> From<Rect<T>> for Polygon<T>
where
T: Copy,
{
fn from(rect: Rect<T>) -> Self {
Self::from(&rect)
}
}
/// Create a polygon from a rectangle.
impl<T> From<&Rect<T>> for Polygon<T>
where
T: Copy,
{
fn from(rect: &Rect<T>) -> Self {
SimplePolygon::new_raw(vec![
rect.lower_left(),
rect.lower_right(),
rect.upper_right(),
rect.upper_left(),
])
.into()
}
}
/// Trait for the conversion of a geometric shape to a polygon.
pub trait ToPolygon<T> {
/// Convert the geometric object into a polygon.
fn to_polygon(&self) -> Polygon<T>;
}
impl<T> Polygon<T> {
/// Create a new polygon from a sequence of points.
/// Ordering of points is not normalized. This impacts the equality check.
pub fn new_raw(exterior: Vec<Point<T>>) -> Self {
Self {
exterior: SimplePolygon::new_raw(exterior),
interiors: Default::default(),
}
}
/// Create a new polygon from a hull and a list of holes.
/// Ordering of points is not normalized. This impacts the equality check.
pub fn new_raw_with_holes<E, I>(exterior: E, holes: Vec<I>) -> Self
where
E: Into<SimplePolygon<T>>,
I: Into<SimplePolygon<T>>,
{
Polygon {
exterior: exterior.into(),
interiors: holes.into_iter().map(|i| i.into()).collect(),
}
}
/// Create empty polygon without any vertices.
pub fn empty() -> Self {
Polygon {
exterior: SimplePolygon::empty(),
interiors: Vec::new(),
}
}
/// Get the number of vertices.
pub fn len(&self) -> usize {
self.exterior.len()
}
/// Check if polygon has no vertices.
pub fn is_empty(&self) -> bool {
self.exterior.is_empty()
}
}
impl<T: Copy> Polygon<T> {
/// Get all exterior edges of the polygon.
pub fn edges(&self) -> Vec<Edge<T>> {
self.exterior.edges()
}
/// Iterate over all edges of the polygon, including interior edges.
pub fn all_edges_iter(&self) -> impl Iterator<Item = Edge<T>> + '_ {
self.exterior.edges_iter().chain(
self.interiors
.iter()
.flat_map(|interior| interior.edges_iter()),
)
}
}
impl<T: PartialOrd> Polygon<T> {
/// Create a new polygon from a sequence of points.
pub fn new<I>(i: I) -> Self
where
I: Into<Self>,
{
i.into().normalized()
}
/// Create a new polygon from a hull and a list of holes.
pub fn new_with_holes<E, I>(exterior: E, holes: Vec<I>) -> Self
where
E: Into<SimplePolygon<T>>,
I: Into<SimplePolygon<T>>,
{
Polygon {
exterior: exterior.into(),
interiors: holes.into_iter().map(|i| i.into()).collect(),
}
.normalized()
}
/// Reorder vertices and holes to get the lexicographically smallest representation of this polygon.
/// Does not change the orientations.
pub fn normalize(&mut self) {
self.exterior.normalize();
self.interiors.iter_mut().for_each(|p| p.normalize());
self.interiors.sort_by(|a, b| {
a.points()
.partial_cmp(b.points())
.unwrap_or(std::cmp::Ordering::Less)
})
}
/// Reorder vertices and holes to get the lexicographically smallest representation of this polygon.
/// Does not change the orientations.
pub fn normalized(mut self) -> Self {
self.normalize();
self
}
}
impl<T: CoordinateType> Polygon<T> {
/// Get the convex hull of the polygon.
///
/// Implements Andrew's Monotone Chain algorithm.
/// See: <http://geomalgorithms.com/a10-_hull-1.html>
pub fn convex_hull(&self) -> SimplePolygon<T>
where
T: Ord,
{
self.exterior.convex_hull()
}
/// Get the vertex with lowest x-coordinate of the exterior polygon.
/// Prefer lower y-coordinates to break ties.
///
/// # Examples
///
/// ```
/// use iron_shapes::polygon::Polygon;
/// use iron_shapes::point::Point;
/// let coords = vec![(0, 0), (1, 0), (-1, 2), (-1, 1)];
///
/// let poly = Polygon::new(coords);
///
/// assert_eq!(poly.lower_left_vertex(), Point::new(-1, 1));
///
/// ```
pub fn lower_left_vertex(&self) -> Point<T> {
self.exterior.lower_left_vertex()
}
/// Get the orientation of the exterior polygon.
///
/// # Examples
///
/// ```
/// use iron_shapes::polygon::Polygon;
/// use iron_shapes::point::Point;
/// use iron_shapes::types::Orientation;
/// let coords = vec![(0, 0), (3, 0), (3, 1)];
///
/// let poly = Polygon::new(coords);
///
/// assert_eq!(poly.orientation::<i64>(), Orientation::CounterClockWise);
///
/// ```
pub fn orientation<Area>(&self) -> Orientation
where
Area: Num + From<T> + PartialOrd,
{
self.exterior.orientation::<Area>()
}
}
impl<T> TryBoundingBox<T> for Polygon<T>
where
T: Copy + PartialOrd,
{
fn try_bounding_box(&self) -> Option<Rect<T>> {
// TODO: What if holes exceed the exterior boundary?
let bbox = self.exterior.try_bounding_box();
if let Some(bbox) = &bbox {
debug_assert!(
self.interiors.iter()
.filter_map(|p| p.try_bounding_box())
.all(|internal_bbox| bbox.contains_rectangle(&internal_bbox)),
"Bounding boxes of interior polygons exceed the bounding box of the exterior polygon."
);
} else {
// If the bounding box of the hull is not defined there should also be no
// defined bounding boxes for the holes.
let num_internal_bboxes = self
.interiors
.iter()
.filter_map(|p| p.try_bounding_box())
.count();
debug_assert_eq!(
num_internal_bboxes, 0,
"Polygon with empty zero-vertex hull cannot contain holes."
);
}
bbox
}
}
impl<T> WindingNumber<T> for Polygon<T>
where
T: CoordinateType,
{
/// 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>
fn winding_number(&self, point: Point<T>) -> isize {
let ext = self.exterior.winding_number(point);
let int: isize = self.interiors.iter().map(|p| p.winding_number(point)).sum();
ext + int
}
}
impl<T> MapPointwise<T> for Polygon<T>
where
T: CoordinateType,
{
fn transform<F: Fn(Point<T>) -> Point<T>>(&self, tf: F) -> Self {
Polygon {
exterior: self.exterior.transform(&tf),
interiors: self.interiors.iter().map(|p| p.transform(&tf)).collect(),
}
}
}
impl<T, A> DoubledOrientedArea<A> for Polygon<T>
where
T: CoordinateType,
A: Num + From<T>,
{
/// 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::polygon::{Polygon, DoubledOrientedArea};
/// let coords = vec![(0, 0), (3, 0), (3, 1)];
///
/// let poly = Polygon::new(coords);
///
/// let area: i64 = poly.area_doubled_oriented();
/// assert_eq!(area, 3);
///
/// ```
fn area_doubled_oriented(&self) -> A {
let ext: A = self.exterior.area_doubled_oriented();
let int = self
.interiors
.iter()
.map(|p| p.area_doubled_oriented())
.fold(A::zero(), |a, b| a + b);
ext + int
}
}
impl<T: CoordinateType + NumCast, Dst: CoordinateType + NumCast> TryCastCoord<T, Dst>
for Polygon<T>
{
type Output = Polygon<Dst>;
fn try_cast(&self) -> Option<Self::Output> {
if let Some(new_hull) = self.exterior.try_cast() {
let new_holes: Vec<_> = self
.interiors
.iter()
.map(|hole| hole.try_cast())
.while_some()
.collect();
if new_holes.len() == self.interiors.len() {
Some(Polygon::new_with_holes(new_hull, new_holes))
} else {
// Some wholes could not be casted.
None
}
} else {
// The hull could not be casted.
None
}
}
}
#[cfg(test)]
mod tests {
use crate::prelude::*;
#[test]
fn test_create_polygon() {
let coords = vec![(0, 0), (1, 0), (1, 1), (0, 1)];
let poly: Polygon<i32> = (&coords).into();
assert_eq!(poly.exterior.len(), coords.len());
}
#[test]
fn test_area() {
let coords = vec![(0, 0), (1, 0), (1, 1), (0, 1)];
let poly: Polygon<i32> = (&coords).into();
let doubled_area: i64 = poly.area_doubled_oriented();
assert_eq!(doubled_area, 2);
}
#[test]
fn test_orientation() {
use crate::types::Orientation;
let coords = vec![(0, 0), (1, 0), (1, 1)];
let poly: Polygon<i32> = (&coords).into();
assert_eq!(poly.orientation::<i64>(), Orientation::CounterClockWise);
}
#[test]
fn test_bounding_box() {
use crate::rect::Rect;
use crate::traits::TryBoundingBox;
let coords = vec![(1, 0), (-1, -2), (1, 0), (42, 37)];
let poly: Polygon<i32> = (&coords).into();
assert_eq!(poly.try_bounding_box(), Some(Rect::new((-1, -2), (42, 37))))
}
#[test]
fn test_winding_number() {
let coords = vec![(0, 0), (2, 0), (2, 2), (0, 2)];
let poly: Polygon<i32> = (&coords).into();
assert_eq!(poly.winding_number(Point::new(1, 1)), 1);
assert_eq!(poly.winding_number(Point::new(-1, -1)), 0);
assert_eq!(poly.winding_number(Point::new(10, 10)), 0);
// Test point on edges
assert_eq!(poly.winding_number(Point::new(1, 0)), 1); // Bottom edge
assert_eq!(poly.winding_number(Point::new(2, 1)), 0); // Right edge
assert_eq!(poly.winding_number(Point::new(1, 2)), 0); // Top edge
assert_eq!(poly.winding_number(Point::new(0, 1)), 1); // Left edge
// Test point on vertex.
assert_eq!(poly.winding_number(Point::new(0, 0)), 1);
assert_eq!(poly.winding_number(Point::new(2, 0)), 0);
assert_eq!(poly.winding_number(Point::new(2, 2)), 0);
assert_eq!(poly.winding_number(Point::new(0, 2)), 0);
}
#[test]
fn test_convex_hull() {
let poly = Polygon::new(vec![(0, 0), (1, 1), (2, 0), (2, 2), (0, 2)]);
let exp_hull = Polygon::new(vec![(0, 0), (2, 0), (2, 2), (0, 2)])
.exterior
.normalized();
assert_eq!(poly.convex_hull(), exp_hull);
let poly = Polygon::new(vec![(1, 0), (2, 1), (1, 2), (1, 1), (0, 1)]);
let exp_hull = Polygon::new(vec![(1, 0), (2, 1), (1, 2), (0, 1)])
.exterior
.normalized();
assert_eq!(poly.convex_hull(), exp_hull);
// Degenerate case. All x coordinates are the same.
let poly = Polygon::new(vec![(0, 0), (0, 1), (0, 7)]);
let exp_hull = Polygon::new(vec![(0, 0), (0, 7)]).exterior.normalized();
assert_eq!(poly.convex_hull(), exp_hull);
// Degenerate case. All y coordinates are the same.
let poly = Polygon::new(vec![(0, 0), (1, 0), (7, 0)]);
let exp_hull = Polygon::new(vec![(0, 0), (7, 0)]).exterior.normalized();
assert_eq!(poly.convex_hull(), exp_hull);
// Degenerate case. All points are equal.
let poly4 = Polygon::new(vec![(0, 0), (0, 0), (0, 0)]);
let exp_hull4 = Polygon::new(vec![(0, 0)]).exterior.normalized();
assert_eq!(poly4.convex_hull(), exp_hull4);
}
#[test]
fn test_normalized_eq() {
let poly1 = Polygon::new(vec![(0, 0), (1, 0), (1, 1)]);
let poly2 = Polygon::new(vec![(1, 1), (0, 0), (1, 0)]);
let poly3 = Polygon::new(vec![(0, 0), (1, 0), (1, 2)]);
assert_eq!(poly1, poly2);
assert_eq!(poly2, poly1);
assert_ne!(poly1, poly3)
}
}