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
/*
* 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/>.
*/
//! 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 {
return 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)
}
}
/// 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 {
return 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()
}
}
/// 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()
}
}
/// 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())
}
}
}