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 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
/*
* Copyright (c) 2018-2020 Thomas Kramer.
*
* This file is part of LibrEDA
* (see https://codeberg.org/libreda/iron-shapes).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
//! This module contains data types and functions for basic polygons without holes.
use crate::CoordinateType;
use crate::point::Point;
use crate::edge::Edge;
use crate::rect::Rect;
pub use crate::traits::{DoubledOrientedArea, TryBoundingBox, MapPointwise, WindingNumber};
use crate::types::*;
use std::iter::FromIterator;
use std::cmp::{Ord, PartialEq};
use std::slice::Iter;
use num_traits::NumCast;
use crate::traits::TryCastCoord;
/// 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.
#[derive(Clone, Debug, Hash, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct SimplePolygon<T>
where T: CoordinateType {
/// Vertices of the polygon.
pub points: Vec<Point<T>>
}
/// Shorthand notation for creating a simple polygon.
/// # Example
/// ```
/// # #[macro_use]
/// # extern crate iron_shapes;
/// # fn main() {
/// use iron_shapes::prelude::*;
/// let p = simple_polygon!((0, 0), (1, 0), (1, 1));
/// assert_eq!(p, SimplePolygon::from(vec![(0, 0), (1, 0), (1, 1)]));
/// # }
/// ```
#[macro_export]
macro_rules! simple_polygon {
($($x:expr),*) => {SimplePolygon::new((vec![$($x.into()),*]))}
}
impl<T: CoordinateType> SimplePolygon<T> {
/// Create a new polygon from a list of points.
///
/// The orientation of the points is normalized to counter-clock-wise.
pub fn new(points: Vec<Point<T>>) -> Self {
let mut new = Self::new_raw(points);
// Normalize orientation.
if new.orientation() != Orientation::CounterClockWise {
new.points.reverse();
}
new
}
/// Create a new polygon from a list of points.
/// The points are taken as they are, without reordering
/// or simplification.
pub fn new_raw(points: Vec<Point<T>>) -> Self {
Self {
points
}
}
/// Create a new simple polygon from a rectangle.
pub fn from_rect(rect: &Rect<T>) -> Self {
Self::new(
vec![rect.lower_left(), rect.lower_right(),
rect.upper_right(), rect.upper_left()]
)
}
/// Create empty polygon without any vertices.
pub fn empty() -> Self {
SimplePolygon {
points: Vec::new()
}
}
/// Get the number of vertices.
pub fn len(&self) -> usize {
self.points.len()
}
/// Shortcut for `self.points.iter()`.
pub fn iter(&self) -> Iter<Point<T>> {
self.points.iter()
}
/// 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 {
// Sort S by increasing x and then y-coordinate.
let p: Vec<Point<T>> = {
let mut p: Vec<Point<T>> = self.points.clone();
p.sort();
p
};
assert!(p.len() >= 2);
// minmin = index of P with min x first and min y second
let minmin = 0;
let p_minmin = p[minmin];
// maxmax = index of P with max x first and max y second
let maxmax = p.len() - 1;
let p_maxmax = p[maxmax];
// minmax = index of P with min x first and max y second
let minmax = p.iter().enumerate()
.take_while(|(_, p)| p.x == p_minmin.x)
.last().unwrap().0;
let p_minmax = p[minmax];
// maxmin = index of P with max x first and min y second
let maxmin = p.iter().enumerate().rev()
.take_while(|(_, p)| p.x == p_maxmax.x)
.last().unwrap().0;
let p_maxmin = p[maxmin];
debug_assert!(minmin <= minmax);
debug_assert!(minmax <= maxmin || p_minmax.x == p_maxmin.x);
debug_assert!(maxmin <= maxmax);
debug_assert!(p.iter().all(|&p| p_minmin <= p));
debug_assert!(p.iter().all(|&p| p_maxmax >= p));
debug_assert!(p.iter().all(|&p|
p_minmax.x < p.x || (p_minmax.x == p.x && p_minmax.y >= p.y)
));
debug_assert!(p.iter().all(|&p|
p_maxmin.x > p.x || (p_maxmin.x == p.x && p_maxmin.y <= p.y)
));
// Handle degenerate case where all x coordinates are equal.
if p_minmin.x == p_maxmax.x {
if p_minmin.y == p_maxmax.y {
SimplePolygon::new(vec![p_minmin])
} else {
SimplePolygon::new(vec![p_minmin, p_maxmax])
}
} else {
let build_half_hull =
|l: Edge<T>, points: &[Point<T>]| {
let mut stack = Vec::new();
// Push starting point on stack.
stack.push(l.start);
for &pi in points {
// Skip all points that are not strictly right of `l`.
if l.side_of(pi) == Side::Right {
while stack.len() >= 2 {
let pt1 = stack[stack.len() - 1];
let pt2 = stack[stack.len() - 2];
if Edge::new(pt2, pt1).side_of(pi) == Side::Left {
// `pi` is strictly left of the line defined by the top two elements
// on the stack.
break;
}
stack.pop();
}
stack.push(pi);
}
}
stack
};
// Compute the lower hull.
let l_min = Edge::new(p_minmin, p_maxmin);
let mut lower_half_hull = build_half_hull(l_min, &p[minmax + 1..maxmin]);
// Push p_maxmin on stack if necessary.
if p_maxmin != p_maxmax {
lower_half_hull.push(p_maxmin);
}
// Compute the upper hull.
let l_max = Edge::new(p_maxmax, p_minmax);
let mut upper_half_hull = build_half_hull(l_max, &p[minmax + 1..maxmin]);
// Push p_minmax on stack if necessary.
if p_minmax != p_minmin {
upper_half_hull.push(p_minmax);
}
// Join both hulls.
lower_half_hull.append(&mut upper_half_hull);
SimplePolygon::new(lower_half_hull)
}
}
/// Get an iterator over the polygon points.
/// Point 0 is appended to the end to close the cycle.
fn iter_cycle(&self) -> impl Iterator<Item=&Point<T>> {
self.points.iter()
.cycle()
.take(self.points.len() + 1)
}
/// 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))]);
///
/// ```
pub fn edges(&self) -> Vec<Edge<T>> {
self.edges_iter().collect()
}
/// Iterate over all edges.
fn edges_iter(&self) -> impl Iterator<Item=Edge<T>> + '_ {
self.iter()
.zip(self.iter_cycle().skip(1))
.map(|(a, b)| Edge::new(a, b))
}
/// Test if all edges are parallel to the x or y axis.
pub fn is_rectilinear(&self) -> bool {
self.edges_iter().all(|e| e.is_rectilinear())
}
/// 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));
///
/// ```
pub fn lower_left_vertex(&self) -> Point<T> {
debug_assert!(self.points.len() > 0);
self.lower_left_vertex_with_index().1
}
/// Get the vertex with lowest x-coordinate and its index.
/// Prefer lower y-coordinates to break ties.
fn lower_left_vertex_with_index(&self) -> (usize, Point<T>) {
debug_assert!(self.points.len() > 0);
// Find minimum.
let min = self.points
.iter()
.enumerate()
.min_by(|(_, &p1), (_, &p2)|
p1.partial_cmp(&p2).unwrap());
let (idx, point) = min.unwrap();
(idx, point.clone().into())
}
/// Get the orientation of the polygon,
/// i.e. check if it is wound clock-wise or counter-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(), Orientation::CounterClockWise);
///
/// ```
pub fn orientation(&self) -> Orientation {
// Find the orientation by the polygon area.
let area2 = self.area_doubled_oriented();
if area2 > T::zero() {
Orientation::CounterClockWise
} else if area2 < T::zero() {
Orientation::ClockWise
} else {
debug_assert!(area2 == T::zero());
Orientation::Straight
}
}
/// Get index of previous vertex.
fn prev(&self, i: usize) -> usize {
match i {
0 => self.points.len() - 1,
x => x - 1
}
}
/// Get index of next vertex.
fn next(&self, i: usize) -> usize {
match i {
_ if i == self.points.len() - 1 => 0,
x => x + 1
}
}
}
impl<T> WindingNumber<T> for SimplePolygon<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 edges = self.edges();
let mut winding_number = 0isize;
// Edge Crossing Rules
//
// 1. an upward edge includes its starting endpoint, and excludes its final endpoint;
// 2. a downward edge excludes its starting endpoint, and includes its final endpoint;
// 3. horizontal edges are excluded
// 4. the edge-ray intersection point must be strictly right of the point P.
for e in edges {
if e.start.y <= point.y { // Crosses upward?
if e.end.y > point.y { // Crosses really upward?
// Yes, crosses upward.
if e.side_of(point) == Side::Left {
winding_number += 1;
}
}
} else if e.end.y <= point.y { // Crosses downward?
// Yes, crosses downward.
// `e.start.y > point.y` needs not to be checked anymore.
if e.side_of(point) == Side::Right {
winding_number -= 1;
}
}
}
winding_number
}
}
/// Create a polygon from a type that is convertible into an iterator of values convertible to `Point`s.
impl<I, T, P> From<I> for SimplePolygon<T>
where T: CoordinateType,
I: IntoIterator<Item=P>,
Point<T>: From<P>
{
fn from(iter: I) -> Self {
let points: Vec<Point<T>> = iter.into_iter().map(
|x| x.into()
).collect();
SimplePolygon { points }
}
}
// impl<T: CoordinateType> From<&Rect<T>> for SimplePolygon<T> {
// fn from(rect: &Rect<T>) -> Self {
// Self::new(
// vec![rect.lower_left(), rect.lower_right(),
// rect.upper_right(), rect.upper_left()]
// )
// }
// }
//
// /// Create a polygon from a `Vec` of values convertible to `Point`s.
// impl<'a, T, P> From<&'a Vec<P>> for SimplePolygon<T>
// where T: CoordinateType,
// Point<T>: From<&'a P>
// {
// fn from(vec: &'a Vec<P>) -> Self {
// let points: Vec<Point<T>> = vec.into_iter().map(
// |x| x.into()
// ).collect();
//
// SimplePolygon { points }
// }
// }
//
// /// Create a polygon from a `Vec` of values convertible to `Point`s.
// impl<T, P> From<Vec<P>> for SimplePolygon<T>
// where T: CoordinateType,
// Point<T>: From<P>
// {
// fn from(vec: Vec<P>) -> Self {
// let points: Vec<Point<T>> = vec.into_iter().map(
// |x| x.into()
// ).collect();
//
// SimplePolygon { points }
// }
// }
/// Create a polygon from a iterator of values convertible to `Point`s.
impl<T, P> FromIterator<P> for SimplePolygon<T>
where T: CoordinateType,
P: Into<Point<T>>
{
fn from_iter<I>(iter: I) -> Self
where I: IntoIterator<Item=P>
{
let points: Vec<Point<T>> = iter.into_iter().map(
|x| x.into()
).collect();
assert!(points.len() >= 2, "A polygon needs to have at least two points.");
SimplePolygon { points }
}
}
impl<T> TryBoundingBox<T> for SimplePolygon<T>
where T: CoordinateType {
fn try_bounding_box(&self) -> Option<Rect<T>> {
if self.len() > 0 {
let mut x_min = self.points[0].x;
let mut x_max = x_min;
let mut y_min = self.points[0].y;
let mut y_max = y_min;
for p in self.iter().skip(1) {
if p.x < x_min {
x_min = p.x;
}
if p.x > x_max {
x_max = p.x;
}
if p.y < y_min {
y_min = p.y;
}
if p.y > y_max {
y_max = p.y;
}
}
Some(Rect::new((x_min, y_min), (x_max, y_max)))
} else {
None
}
}
}
impl<T> MapPointwise<T> for SimplePolygon<T>
where T: CoordinateType {
fn transform<F: Fn(Point<T>) -> Point<T>>(&self, tf: F) -> Self {
let points = self.points.iter().map(|&p| tf(p)).collect();
let mut new = SimplePolygon {
points
};
// Make sure the polygon is oriented the same way as before.
// TODO: Could be done more efficiently if the magnification/mirroring of the transformation is known.
if new.orientation() != self.orientation() {
new.points.reverse()
}
new
}
}
impl<T: CoordinateType> DoubledOrientedArea<T> for SimplePolygon<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::traits::DoubledOrientedArea;
/// use iron_shapes::simple_polygon::SimplePolygon;
/// let coords = vec![(0, 0), (3, 0), (3, 1)];
///
/// let poly = SimplePolygon::from(coords);
///
/// assert_eq!(poly.area_doubled_oriented(), 3);
///
/// ```
fn area_doubled_oriented(&self) -> T {
let mut sum = T::zero();
for i in 0..self.points.len() {
sum = sum + self.points[i].x * (self.points[self.next(i)].y -
self.points[self.prev(i)].y);
}
sum
}
}
impl<T> PartialEq for SimplePolygon<T>
where T: CoordinateType {
/// Equality test for simple polygons.
///
/// Two polygons are equal iff a cyclic shift on their vertices can be applied
/// such that the both lists of vertices match exactly.
///
/// Complexity: O(n^2)
///
/// TODO: Normalized ordering of vertices for faster comparison.
fn eq(&self, rhs: &Self) -> bool {
let n = self.len();
if n == rhs.len() {
for i in 0..n {
let l = self.points.iter();
let r = rhs.points.iter().cycle().skip(i).take(n);
if l.eq(r) {
return true;
}
}
false
} else {
false
}
}
}
impl<T: CoordinateType + NumCast, Dst: CoordinateType + NumCast> TryCastCoord<T, Dst> for SimplePolygon<T> {
type Output = SimplePolygon<Dst>;
fn try_cast(&self) -> Option<Self::Output> {
let new_points: Option<Vec<_>> = self.points.iter()
.map(|p| p.try_cast())
.collect();
new_points.map(|p| SimplePolygon::new_raw(p))
}
}
/// Two simple polygons should be the same even if points are shifted cyclical.
#[test]
fn test_partial_eq() {
let p1 = simple_polygon!((0, 0), (0, 1), (1, 1), (1, 0));
let p2 = simple_polygon!((0, 0), (0, 1), (1, 1), (1, 0));
assert_eq!(p1, p2);
let p2 = simple_polygon!((0, 1), (1, 1), (1, 0), (0, 0));
assert_eq!(p1, p2);
}
/// Simple sanity check for computation of bounding box.
#[test]
fn test_bounding_box() {
let p = simple_polygon!((0, 0), (0, 1), (1, 1));
assert_eq!(p.try_bounding_box(), Some(Rect::new((0, 0), (1, 1))));
}