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
// Copyright (c) 2018-2020 Thomas Kramer.
// SPDX-FileCopyrightText: 2018-2022 Thomas Kramer
//
// SPDX-License-Identifier: AGPL-3.0-or-later
//! Describe repetitions of geometrical objects.
//!
//! Regular repetitions (arrays) are based on two lattice vectors, irregular repetitions are based
//! on a list of offsets.
use crate::prelude::{CoordinateType, Vector};
use num_traits::Zero;
/// Describe a equi-spaced n*m two-dimensional repetition as a lattice.
/// The offsets are computed as `(i*a, j*b)` for `i` in `0..n` and `j` in `0..m`.
/// `a` and `b` the distance vectors between two neighbouring points.
#[derive(PartialEq, Eq, Copy, Clone, Debug, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct RegularRepetition<T>
where
T: CoordinateType,
{
/// First lattice vector.
a: Vector<T>,
/// Second lattice vector.
b: Vector<T>,
/// First dimension.
n: u32,
/// Second dimension.
m: u32,
}
impl<T> RegularRepetition<T>
where
T: CoordinateType,
{
/// Create a new lattice based repetition.
///
/// # Parameters
/// * `a, b`: Lattice vectors.
/// * `n, m`: Number of repetitions in directions `a` and `b`.
pub fn new(a: Vector<T>, b: Vector<T>, n: u32, m: u32) -> Self {
RegularRepetition { a, b, n, m }
}
/// Create a repetition along the x and y axis.
///
/// # Example
///
/// ```
/// use iron_shapes::prelude::RegularRepetition;
///
/// let rep = RegularRepetition::new_rectilinear(1, 1, 1, 2);
/// assert_eq!(rep.len(), 2);
/// let offsets: Vec<_> = rep.iter().collect();
///
/// assert_eq!(offsets, [(0, 0).into(), (0, 1).into()]);
/// ```
pub fn new_rectilinear(spacing_x: T, spacing_y: T, num_x: u32, num_y: u32) -> Self {
Self::new(
Vector::new(spacing_x, T::zero()),
Vector::new(T::zero(), spacing_y),
num_x,
num_y,
)
}
/// Iterate over each offsets of this repetition.
pub fn iter(self) -> impl Iterator<Item = Vector<T>> {
let mut current = Vector::zero();
(0..self.m).flat_map(move |_| {
let mut row = current;
current = current + self.b;
(0..self.n).map(move |_| {
let pos = row;
row = row + self.a;
pos
})
})
}
/// Return the number of offsets in this repetition.
pub fn len(&self) -> usize {
(self.n as usize) * (self.m as usize)
}
/// Check if number of repetitions is zero.
pub fn is_empty(&self) -> bool {
self.n == 0 || self.m == 0
}
}
/// Describe a non-equispaced repetition by storing a list of offsets.
#[derive(PartialEq, Eq, Clone, Debug, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct IrregularRepetition<T>
where
T: CoordinateType,
{
/// Offset vectors of the repetition.
offsets: Vec<Vector<T>>,
}
impl<T> IrregularRepetition<T>
where
T: CoordinateType,
{
/// Create a new irregular repetition from a list of offsets.
pub fn new(offsets: Vec<Vector<T>>) -> Self {
IrregularRepetition { offsets }
}
/// Iterate over each offsets of this repetition.
pub fn iter(&self) -> impl Iterator<Item = Vector<T>> + '_ {
self.offsets.iter().copied()
}
/// Return the number of offsets in this repetition.
pub fn len(&self) -> usize {
self.offsets.len()
}
/// Check if there's no repetition at all.
pub fn is_empty(&self) -> bool {
self.offsets.is_empty()
}
}
/// Describe the regular or irregular repetition of a geometrical object.
#[derive(PartialEq, Eq, Clone, Debug, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Repetition<T>
where
T: CoordinateType,
{
/// Lattice based repetition.
Regular(RegularRepetition<T>),
/// Repetition with random offsets.
Irregular(IrregularRepetition<T>),
}
impl<T> Repetition<T>
where
T: CoordinateType,
{
/// Return the number of offsets in this repetition.
pub fn len(&self) -> usize {
match self {
Repetition::Regular(r) => r.len(),
Repetition::Irregular(r) => r.len(),
}
}
/// Check if there is no repetition at all.
pub fn is_empty(&self) -> bool {
match self {
Repetition::Regular(r) => r.is_empty(),
Repetition::Irregular(r) => r.is_empty(),
}
}
/// Iterate over each offsets of this repetition.
pub fn iter(&self) -> Box<dyn Iterator<Item = Vector<T>> + '_> {
match self {
Repetition::Regular(r) => Box::new(r.iter()),
Repetition::Irregular(r) => Box::new(r.iter()),
}
}
}