2019-02-05 13:03:20 -05:00
|
|
|
// pathfinder/geometry/src/basic/rect.rs
|
|
|
|
//
|
|
|
|
// Copyright © 2019 The Pathfinder Project Developers.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! 2D axis-aligned rectangles, optimized with SIMD.
|
|
|
|
|
2020-04-01 20:20:32 -04:00
|
|
|
use crate::vector::{IntoVector2F, Vector2F, Vector2I};
|
2019-02-05 13:55:01 -05:00
|
|
|
use pathfinder_simd::default::{F32x4, I32x4};
|
2020-04-02 14:59:11 -04:00
|
|
|
use std::ops::{Add, Mul, Sub};
|
2019-02-05 13:03:20 -05:00
|
|
|
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Default)]
|
2019-05-29 22:13:42 -04:00
|
|
|
pub struct RectF(pub F32x4);
|
2019-02-05 13:03:20 -05:00
|
|
|
|
2019-05-29 22:13:42 -04:00
|
|
|
impl RectF {
|
2019-02-05 13:03:20 -05:00
|
|
|
#[inline]
|
2019-06-03 15:39:29 -04:00
|
|
|
pub fn new(origin: Vector2F, size: Vector2F) -> RectF {
|
2019-05-29 22:13:42 -04:00
|
|
|
RectF(origin.0.concat_xy_xy(origin.0 + size.0))
|
2019-02-05 13:03:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-06-03 15:39:29 -04:00
|
|
|
pub fn from_points(origin: Vector2F, lower_right: Vector2F) -> RectF {
|
2019-05-29 22:13:42 -04:00
|
|
|
RectF(origin.0.concat_xy_xy(lower_right.0))
|
2019-02-05 13:03:20 -05:00
|
|
|
}
|
|
|
|
|
2020-01-15 00:31:22 -05:00
|
|
|
// Accessors
|
|
|
|
|
2019-02-05 13:03:20 -05:00
|
|
|
#[inline]
|
2020-01-15 00:31:22 -05:00
|
|
|
pub fn origin(self) -> Vector2F {
|
2019-06-25 17:43:13 -04:00
|
|
|
Vector2F(self.0.xy())
|
2019-02-05 13:03:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-15 00:31:22 -05:00
|
|
|
pub fn size(self) -> Vector2F {
|
2019-06-25 17:43:13 -04:00
|
|
|
Vector2F(self.0.zw() - self.0.xy())
|
2019-02-05 13:03:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-15 00:31:22 -05:00
|
|
|
pub fn origin_x(self) -> f32 {
|
|
|
|
self.0.x()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn origin_y(self) -> f32 {
|
|
|
|
self.0.y()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn width(self) -> f32 {
|
|
|
|
self.0.z() - self.0.x()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn height(self) -> f32 {
|
|
|
|
self.0.w() - self.0.y()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn upper_right(self) -> Vector2F {
|
2019-06-25 17:43:13 -04:00
|
|
|
Vector2F(self.0.zy())
|
2019-02-05 13:03:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-15 00:31:22 -05:00
|
|
|
pub fn lower_left(self) -> Vector2F {
|
2019-06-25 17:43:13 -04:00
|
|
|
Vector2F(self.0.xw())
|
2019-02-05 13:03:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-15 00:31:22 -05:00
|
|
|
pub fn lower_right(self) -> Vector2F {
|
2019-06-25 17:43:13 -04:00
|
|
|
Vector2F(self.0.zw())
|
2019-02-05 13:03:20 -05:00
|
|
|
}
|
|
|
|
|
2020-01-15 00:31:22 -05:00
|
|
|
// Mutators
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn set_origin_x(&mut self, x: f32) {
|
|
|
|
self.0.set_x(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn set_origin_y(&mut self, y: f32) {
|
|
|
|
self.0.set_y(y)
|
|
|
|
}
|
|
|
|
|
2019-02-05 13:03:20 -05:00
|
|
|
#[inline]
|
2020-01-15 00:31:22 -05:00
|
|
|
pub fn contains_point(self, point: Vector2F) -> bool {
|
2019-02-05 13:03:20 -05:00
|
|
|
// self.origin <= point && point <= self.lower_right
|
2019-06-25 17:43:13 -04:00
|
|
|
let point = point.0.to_f32x4();
|
2019-12-29 15:13:58 -05:00
|
|
|
self.0.concat_xy_xy(point).packed_le(point.concat_xy_zw(self.0)).all_true()
|
2019-02-05 13:03:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-15 00:31:22 -05:00
|
|
|
pub fn contains_rect(self, other: RectF) -> bool {
|
2019-02-05 13:03:20 -05:00
|
|
|
// self.origin <= other.origin && other.lower_right <= self.lower_right
|
2019-12-29 15:13:58 -05:00
|
|
|
self.0.concat_xy_zw(other.0).packed_le(other.0.concat_xy_zw(self.0)).all_true()
|
2019-02-05 13:03:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-15 00:31:22 -05:00
|
|
|
pub fn is_empty(self) -> bool {
|
2019-02-05 13:03:20 -05:00
|
|
|
self.origin() == self.lower_right()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-15 00:31:22 -05:00
|
|
|
pub fn union_point(self, point: Vector2F) -> RectF {
|
2019-05-29 22:13:42 -04:00
|
|
|
RectF::from_points(self.origin().min(point), self.lower_right().max(point))
|
2019-02-05 13:03:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-15 00:31:22 -05:00
|
|
|
pub fn union_rect(self, other: RectF) -> RectF {
|
2019-05-29 22:13:42 -04:00
|
|
|
RectF::from_points(
|
2019-04-29 19:46:35 -04:00
|
|
|
self.origin().min(other.origin()),
|
|
|
|
self.lower_right().max(other.lower_right()),
|
|
|
|
)
|
2019-02-05 13:03:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-15 00:31:22 -05:00
|
|
|
pub fn intersects(self, other: RectF) -> bool {
|
2019-02-05 13:03:20 -05:00
|
|
|
// self.origin < other.lower_right && other.origin < self.lower_right
|
2019-12-29 15:13:58 -05:00
|
|
|
self.0.concat_xy_xy(other.0).packed_lt(other.0.concat_zw_zw(self.0)).all_true()
|
2019-02-05 13:03:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2020-01-15 00:31:22 -05:00
|
|
|
pub fn intersection(self, other: RectF) -> Option<RectF> {
|
2019-02-05 13:03:20 -05:00
|
|
|
if !self.intersects(other) {
|
|
|
|
None
|
|
|
|
} else {
|
2019-05-29 22:13:42 -04:00
|
|
|
Some(RectF::from_points(
|
2019-04-29 19:46:35 -04:00
|
|
|
self.origin().max(other.origin()),
|
|
|
|
self.lower_right().min(other.lower_right()),
|
|
|
|
))
|
2019-02-05 13:03:20 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn min_x(self) -> f32 {
|
|
|
|
self.0[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn min_y(self) -> f32 {
|
|
|
|
self.0[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn max_x(self) -> f32 {
|
|
|
|
self.0[2]
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn max_y(self) -> f32 {
|
|
|
|
self.0[3]
|
|
|
|
}
|
2019-02-05 13:55:01 -05:00
|
|
|
|
2020-02-18 17:36:10 -05:00
|
|
|
#[inline]
|
|
|
|
pub fn center(self) -> Vector2F {
|
2020-04-01 20:20:32 -04:00
|
|
|
self.origin() + self.size() * 0.5
|
2019-02-05 13:55:01 -05:00
|
|
|
}
|
|
|
|
|
2020-01-15 00:31:22 -05:00
|
|
|
/// Rounds all points to the nearest integer.
|
|
|
|
#[inline]
|
|
|
|
pub fn round(self) -> RectF {
|
|
|
|
RectF(self.0.to_i32x4().to_f32x4())
|
|
|
|
}
|
|
|
|
|
2019-02-05 13:55:01 -05:00
|
|
|
#[inline]
|
2019-05-29 22:13:42 -04:00
|
|
|
pub fn round_out(self) -> RectF {
|
|
|
|
RectF::from_points(self.origin().floor(), self.lower_right().ceil())
|
2019-02-05 13:55:01 -05:00
|
|
|
}
|
|
|
|
|
2019-02-05 23:10:20 -05:00
|
|
|
#[inline]
|
2020-04-01 20:20:32 -04:00
|
|
|
pub fn dilate<A>(self, amount: A) -> RectF where A: IntoVector2F {
|
|
|
|
let amount = amount.into_vector_2f();
|
2019-05-29 22:13:42 -04:00
|
|
|
RectF::from_points(self.origin() - amount, self.lower_right() + amount)
|
2019-02-05 23:10:20 -05:00
|
|
|
}
|
|
|
|
|
2020-02-07 14:46:20 -05:00
|
|
|
#[inline]
|
2020-04-01 20:20:32 -04:00
|
|
|
pub fn contract<A>(self, amount: A) -> RectF where A: IntoVector2F {
|
|
|
|
let amount = amount.into_vector_2f();
|
2020-02-07 14:46:20 -05:00
|
|
|
RectF::from_points(self.origin() + amount, self.lower_right() - amount)
|
|
|
|
}
|
|
|
|
|
2019-02-05 13:55:01 -05:00
|
|
|
#[inline]
|
2019-05-29 22:13:42 -04:00
|
|
|
pub fn to_i32(&self) -> RectI {
|
|
|
|
RectI(self.0.to_i32x4())
|
2019-02-05 13:55:01 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-28 15:45:23 -04:00
|
|
|
impl Add<Vector2F> for RectF {
|
|
|
|
type Output = RectF;
|
|
|
|
#[inline]
|
|
|
|
fn add(self, other: Vector2F) -> RectF {
|
|
|
|
RectF::new(self.origin() + other, self.size())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 14:59:11 -04:00
|
|
|
impl Add<f32> for RectF {
|
|
|
|
type Output = RectF;
|
|
|
|
#[inline]
|
|
|
|
fn add(self, other: f32) -> RectF {
|
|
|
|
RectF::new(self.origin() + other, self.size())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 20:20:32 -04:00
|
|
|
impl Mul<Vector2F> for RectF {
|
|
|
|
type Output = RectF;
|
|
|
|
#[inline]
|
|
|
|
fn mul(self, factors: Vector2F) -> RectF {
|
|
|
|
RectF(self.0 * factors.0.concat_xy_xy(factors.0))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Mul<f32> for RectF {
|
|
|
|
type Output = RectF;
|
|
|
|
#[inline]
|
|
|
|
fn mul(self, factor: f32) -> RectF {
|
|
|
|
RectF(self.0 * F32x4::splat(factor))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-02 14:59:11 -04:00
|
|
|
impl Sub<Vector2F> for RectF {
|
|
|
|
type Output = RectF;
|
|
|
|
#[inline]
|
|
|
|
fn sub(self, other: Vector2F) -> RectF {
|
|
|
|
RectF::new(self.origin() - other, self.size())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Sub<f32> for RectF {
|
|
|
|
type Output = RectF;
|
|
|
|
#[inline]
|
|
|
|
fn sub(self, other: f32) -> RectF {
|
|
|
|
RectF::new(self.origin() - other, self.size())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 00:31:22 -05:00
|
|
|
/// NB: The origin is inclusive, while the lower right point is exclusive.
|
2019-02-05 13:55:01 -05:00
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Default)]
|
2019-05-29 22:13:42 -04:00
|
|
|
pub struct RectI(pub I32x4);
|
2019-02-05 13:55:01 -05:00
|
|
|
|
2019-05-29 22:13:42 -04:00
|
|
|
impl RectI {
|
2019-02-05 13:55:01 -05:00
|
|
|
#[inline]
|
2019-06-03 15:39:29 -04:00
|
|
|
pub fn new(origin: Vector2I, size: Vector2I) -> RectI {
|
2019-05-29 22:13:42 -04:00
|
|
|
RectI(origin.0.concat_xy_xy(origin.0 + size.0))
|
2019-02-05 13:55:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-06-03 15:39:29 -04:00
|
|
|
pub fn from_points(origin: Vector2I, lower_right: Vector2I) -> RectI {
|
2019-05-29 22:13:42 -04:00
|
|
|
RectI(origin.0.concat_xy_xy(lower_right.0))
|
2019-02-05 13:55:01 -05:00
|
|
|
}
|
|
|
|
|
2020-01-15 00:31:22 -05:00
|
|
|
// Accessors
|
|
|
|
|
2019-02-05 13:55:01 -05:00
|
|
|
#[inline]
|
2019-06-03 15:39:29 -04:00
|
|
|
pub fn origin(&self) -> Vector2I {
|
2019-06-25 17:43:13 -04:00
|
|
|
Vector2I(self.0.xy())
|
2019-02-05 13:55:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-06-03 15:39:29 -04:00
|
|
|
pub fn size(&self) -> Vector2I {
|
2019-06-25 17:43:13 -04:00
|
|
|
Vector2I(self.0.zw() - self.0.xy())
|
2019-02-05 13:55:01 -05:00
|
|
|
}
|
|
|
|
|
2020-01-15 00:31:22 -05:00
|
|
|
#[inline]
|
|
|
|
pub fn origin_x(self) -> i32 {
|
|
|
|
self.0.x()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn origin_y(self) -> i32 {
|
|
|
|
self.0.y()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn width(self) -> i32 {
|
|
|
|
self.0.z() - self.0.x()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn height(self) -> i32 {
|
|
|
|
self.0.w() - self.0.y()
|
|
|
|
}
|
|
|
|
|
2019-02-05 13:55:01 -05:00
|
|
|
#[inline]
|
2019-06-03 15:39:29 -04:00
|
|
|
pub fn upper_right(&self) -> Vector2I {
|
2019-06-25 17:43:13 -04:00
|
|
|
Vector2I(self.0.zy())
|
2019-02-05 13:55:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-06-03 15:39:29 -04:00
|
|
|
pub fn lower_left(&self) -> Vector2I {
|
2019-06-25 17:43:13 -04:00
|
|
|
Vector2I(self.0.xw())
|
2019-02-05 13:55:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-06-03 15:39:29 -04:00
|
|
|
pub fn lower_right(&self) -> Vector2I {
|
2019-06-25 17:43:13 -04:00
|
|
|
Vector2I(self.0.zw())
|
2019-02-05 13:55:01 -05:00
|
|
|
}
|
|
|
|
|
2019-12-14 16:57:55 -05:00
|
|
|
#[inline]
|
|
|
|
pub fn scale(self, factor: i32) -> RectI {
|
|
|
|
RectI(self.0 * I32x4::splat(factor))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn scale_xy(self, factors: Vector2I) -> RectI {
|
|
|
|
RectI(self.0 * factors.0.concat_xy_xy(factors.0))
|
|
|
|
}
|
|
|
|
|
2019-02-05 13:55:01 -05:00
|
|
|
#[inline]
|
|
|
|
pub fn min_x(self) -> i32 {
|
|
|
|
self.0[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn min_y(self) -> i32 {
|
|
|
|
self.0[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn max_x(self) -> i32 {
|
|
|
|
self.0[2]
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn max_y(self) -> i32 {
|
|
|
|
self.0[3]
|
|
|
|
}
|
2019-02-07 19:38:24 -05:00
|
|
|
|
2020-01-15 00:31:22 -05:00
|
|
|
#[inline]
|
|
|
|
pub fn intersects(self, other: RectI) -> bool {
|
|
|
|
// self.origin < other.lower_right && other.origin < self.lower_right
|
|
|
|
self.0.concat_xy_xy(other.0).packed_lt(other.0.concat_zw_zw(self.0)).all_true()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn intersection(self, other: RectI) -> Option<RectI> {
|
|
|
|
if !self.intersects(other) {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(RectI::from_points(
|
|
|
|
self.origin().max(other.origin()),
|
|
|
|
self.lower_right().min(other.lower_right()),
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-07 19:38:24 -05:00
|
|
|
#[inline]
|
2019-06-03 15:39:29 -04:00
|
|
|
pub fn contains_point(&self, point: Vector2I) -> bool {
|
2019-02-07 19:38:24 -05:00
|
|
|
// self.origin <= point && point <= self.lower_right - 1
|
2020-04-01 20:20:32 -04:00
|
|
|
let lower_right = self.lower_right() - 1;
|
2019-06-25 17:43:13 -04:00
|
|
|
self.origin()
|
|
|
|
.0
|
2019-04-29 19:46:35 -04:00
|
|
|
.concat_xy_xy(point.0)
|
|
|
|
.packed_le(point.0.concat_xy_xy(lower_right.0))
|
2019-12-29 15:13:58 -05:00
|
|
|
.all_true()
|
2019-02-07 19:38:24 -05:00
|
|
|
}
|
2019-03-06 14:33:59 -05:00
|
|
|
|
2020-02-07 14:46:20 -05:00
|
|
|
#[inline]
|
|
|
|
pub fn contract(self, amount: Vector2I) -> RectI {
|
|
|
|
RectI::from_points(self.origin() + amount, self.lower_right() - amount)
|
|
|
|
}
|
|
|
|
|
2019-03-06 14:33:59 -05:00
|
|
|
#[inline]
|
2019-05-29 22:13:42 -04:00
|
|
|
pub fn to_f32(&self) -> RectF {
|
|
|
|
RectF(self.0.to_f32x4())
|
2019-03-06 14:33:59 -05:00
|
|
|
}
|
2019-02-05 13:03:20 -05:00
|
|
|
}
|