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
//! Rectangular ring geometry.
//!
//! May be useful for drawing structures that enclose other structures,
//! such as guard rings.
use array_map::ArrayMap;
use serde::{Deserialize, Serialize};
use crate::prelude::*;
use crate::transform::TranslateMut;
/// A rectangular ring surrounding an enclosed rectangle.
#[derive(Debug, Default, Copy, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct Ring {
/// Vertical span of top segment.
topv: Span,
/// Vertical span of bottom segment.
botv: Span,
/// Horizontal span of left segment.
lefth: Span,
/// Horizontal span of right segment.
righth: Span,
}
/// Represents the ways [`Ring`] geometry can be specified.
#[derive(Debug, Copy, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub enum RingContents {
/// The ring must fit within the given rectangle.
Outer(Rect),
/// The ring must enclose the given rectangle.
Inner(Rect),
}
impl RingContents {
/// The rectangle stored in this enum variant.
pub fn rect(&self) -> Rect {
match self {
Self::Outer(r) => *r,
Self::Inner(r) => *r,
}
}
/// Returns true if this is a [`RingContents::Outer`] variant.
pub fn is_outer(&self) -> bool {
matches!(self, Self::Outer(_))
}
/// Returns true if this is a [`RingContents::Inner`] variant.
pub fn is_inner(&self) -> bool {
matches!(self, Self::Inner(_))
}
}
/// A utility for constructing a [`Ring`].
#[derive(Debug, Default, Copy, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct RingBuilder {
contents: Option<RingContents>,
widths: ArrayMap<Side, i64, 4>,
}
impl Ring {
/// Creates a new [`RingBuilder`].
#[inline]
pub fn builder() -> RingBuilder {
RingBuilder::new()
}
/// Checks that the ring is valid.
pub(crate) fn is_valid(&self) -> bool {
self.topv.start() > self.botv.stop() && self.righth.start() > self.lefth.stop()
}
/// The horizontal span of the annulus of the ring.
pub fn outer_hspan(&self) -> Span {
Span::new(self.lefth.start(), self.righth.stop())
}
/// The horizontal span of the inner portion of the ring.
pub fn inner_hspan(&self) -> Span {
Span::new(self.lefth.stop(), self.righth.start())
}
/// The vertical span of the annulus of the ring.
pub fn outer_vspan(&self) -> Span {
Span::new(self.botv.start(), self.topv.stop())
}
/// The vertical span of the inner portion of the ring.
pub fn inner_vspan(&self) -> Span {
Span::new(self.botv.stop(), self.topv.start())
}
/// The outer annulus bounding box.
pub fn outer(&self) -> Rect {
Rect::from_spans(self.outer_hspan(), self.outer_vspan())
}
/// The inner rectangle.
pub fn inner(&self) -> Rect {
Rect::from_spans(self.inner_hspan(), self.inner_vspan())
}
/// The annular rectangle on the given side.
#[inline]
pub fn rect(&self, side: Side) -> Rect {
match side {
Side::Top => Rect::from_spans(self.outer_hspan(), self.topv),
Side::Right => Rect::from_spans(self.righth, self.outer_vspan()),
Side::Bot => Rect::from_spans(self.outer_hspan(), self.botv),
Side::Left => Rect::from_spans(self.lefth, self.outer_vspan()),
}
}
/// The annuluar rectangle on the given side, but limited to the width/height of the inner rectangle.
#[inline]
pub fn inner_rect(&self, side: Side) -> Rect {
match side {
Side::Top => Rect::from_spans(self.inner_hspan(), self.topv),
Side::Right => Rect::from_spans(self.righth, self.inner_vspan()),
Side::Bot => Rect::from_spans(self.inner_hspan(), self.botv),
Side::Left => Rect::from_spans(self.lefth, self.inner_vspan()),
}
}
/// The lower left annular corner.
///
/// Shares a corner with the inner rect, but does not have any edges
/// in common with the inner rect.
#[inline]
pub fn corner(&self, corner: Corner) -> Rect {
match corner {
Corner::LowerLeft => Rect::from_spans(self.lefth, self.botv),
Corner::UpperLeft => Rect::from_spans(self.lefth, self.topv),
Corner::LowerRight => Rect::from_spans(self.righth, self.botv),
Corner::UpperRight => Rect::from_spans(self.righth, self.topv),
}
}
/// The left annular rectangle.
#[inline]
pub fn left(&self) -> Rect {
self.rect(Side::Left)
}
/// The right annular rectangle.
#[inline]
pub fn right(&self) -> Rect {
self.rect(Side::Right)
}
/// The top annular rectangle.
#[inline]
pub fn top(&self) -> Rect {
self.rect(Side::Top)
}
/// The bottom annular rectangle.
#[inline]
pub fn bot(&self) -> Rect {
self.rect(Side::Bot)
}
/// All 4 annular rectangles.
///
/// The order is subject to change.
#[inline]
pub fn rects(&self) -> [Rect; 4] {
[self.top(), self.right(), self.bot(), self.left()]
}
/// The [`Rect`]s going in the horizontal direction (ie. the bottom and top rectangles).
#[inline]
pub fn hrects(&self) -> [Rect; 2] {
[self.bot(), self.top()]
}
/// The [`Rect`]s going in the vertical direction (ie. the left and right rectangles).
#[inline]
pub fn vrects(&self) -> [Rect; 2] {
[self.left(), self.right()]
}
/// The 4 inner annular rectangles.
///
/// The order is subject to change.
pub fn inner_rects(&self) -> [Rect; 4] {
[
self.inner_rect(Side::Top),
self.inner_rect(Side::Right),
self.inner_rect(Side::Bot),
self.inner_rect(Side::Left),
]
}
/// The inner annular vertical-going (i.e. left and right) rectangles.
pub fn inner_vrects(&self) -> [Rect; 2] {
[self.inner_rect(Side::Left), self.inner_rect(Side::Right)]
}
/// The inner annular horizontal-going (i.e. top and bottom) rectangles.
pub fn inner_hrects(&self) -> [Rect; 2] {
[self.inner_rect(Side::Bot), self.inner_rect(Side::Top)]
}
/// The [`Rect`]s going in the given direction.
///
/// Also see [`Ring::hrects`] and [`Ring::vrects`].
pub fn dir_rects(&self, dir: Dir) -> [Rect; 2] {
match dir {
Dir::Horiz => self.hrects(),
Dir::Vert => self.vrects(),
}
}
}
impl Bbox for Ring {
#[inline]
fn bbox(&self) -> Option<Rect> {
self.outer().bbox()
}
}
impl Contains<Point> for Ring {
fn contains(&self, other: &Point) -> Containment {
self.rects()
.into_iter()
.map(move |r| r.contains(other))
.max()
.unwrap()
}
}
impl TranslateMut for Ring {
fn translate_mut(&mut self, p: Point) {
self.lefth.translate(p.x);
self.righth.translate(p.x);
self.botv.translate(p.y);
self.topv.translate(p.y);
}
}
impl From<RingBuilder> for Ring {
fn from(value: RingBuilder) -> Self {
let contents = value.contents.unwrap();
let r = contents.rect();
let sign = if contents.is_outer() {
Sign::Pos
} else {
Sign::Neg
};
let topv = Span::with_point_and_length(sign, r.top(), value.widths[Side::Top]);
let righth = Span::with_point_and_length(sign, r.right(), value.widths[Side::Right]);
let lefth = Span::with_point_and_length(!sign, r.left(), value.widths[Side::Left]);
let botv = Span::with_point_and_length(!sign, r.bot(), value.widths[Side::Bot]);
let res = Self {
topv,
botv,
lefth,
righth,
};
if contents.is_outer() {
assert_eq!(res.outer(), r);
} else {
assert_eq!(res.inner(), r);
}
assert!(res.is_valid());
res
}
}
impl RingBuilder {
/// Creates a new [`RingBuilder`].
#[inline]
pub fn new() -> Self {
Self::default()
}
/// Constructs a [`Ring`] from this builder.
#[inline]
pub fn build(&mut self) -> Ring {
Ring::from(*self)
}
/// Set the outer region of the ring.
///
/// Only one of inner and outer may be set.
pub fn outer(&mut self, rect: Rect) -> &mut Self {
self.contents = Some(RingContents::Outer(rect));
self
}
/// Set the inner region of the ring.
///
/// Only one of inner and outer may be set.
pub fn inner(&mut self, rect: Rect) -> &mut Self {
self.contents = Some(RingContents::Inner(rect));
self
}
/// Set the width of the left side of the ring.
pub fn left_width(&mut self, value: i64) -> &mut Self {
self.widths[Side::Left] = value;
self
}
/// Set the width of the right side of the ring.
pub fn right_width(&mut self, value: i64) -> &mut Self {
self.widths[Side::Right] = value;
self
}
/// Set the height of the bottom of the ring.
pub fn bot_height(&mut self, value: i64) -> &mut Self {
self.widths[Side::Bot] = value;
self
}
/// Set the height of the top of the ring.
pub fn top_height(&mut self, value: i64) -> &mut Self {
self.widths[Side::Top] = value;
self
}
/// Sets the widths of the vertical-going parts of the ring to the given value.
pub fn widths(&mut self, value: i64) -> &mut Self {
self.left_width(value);
self.right_width(value)
}
/// Sets the heights of the horizontal-going parts of the ring to the given value.
pub fn heights(&mut self, value: i64) -> &mut Self {
self.top_height(value);
self.bot_height(value)
}
/// Sets the width of all ring edges to the given value.
pub fn uniform_width(&mut self, value: i64) -> &mut Self {
self.widths(value);
self.heights(value)
}
/// Sets the width of segments running in the given direction.
///
/// If `dir` is [`Dir::Vert`], sets the widths of the left/right regions.
/// If `dir` is [`Dir::Horiz`], sets the heights of the top/bottom regions.
pub fn dir_widths(&mut self, dir: Dir, value: i64) -> &mut Self {
match dir {
Dir::Vert => self.widths(value),
Dir::Horiz => self.heights(value),
}
}
/// Set the width of the given side.
pub fn side_width(&mut self, side: Side, value: i64) -> &mut Self {
use Side::*;
match side {
Top => self.top_height(value),
Bot => self.bot_height(value),
Left => self.left_width(value),
Right => self.right_width(value),
}
}
}