use std::fmt;
use crate::Point;
use crate::alloc::{OutOfMemory, try_with_capacity};
use crate::numeric::linspace_with;
use crate::path::{Path, PathCode};
pub const DEFAULT_DENSITY: i32 = 6;
pub const MAX_VERTICES: usize = 1 << 22;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum HatchError {
NegativeDensity,
OddShapeRows,
TooDense,
}
impl fmt::Display for HatchError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::NegativeDensity => "hatch density must not be negative",
Self::OddShapeRows => "shape hatches need an even number of rows",
Self::TooDense => "the hatch pattern is too dense",
})
}
}
impl std::error::Error for HatchError {}
fn count(pattern: &str, chars: &[char], density: usize) -> usize {
pattern
.chars()
.filter(|c| chars.contains(c))
.count()
.saturating_mul(density)
}
#[derive(Default)]
struct Builder {
vertices: Vec<Point>,
codes: Vec<PathCode>,
}
impl Builder {
fn with_room(vertices: usize) -> Result<Self, OutOfMemory> {
Ok(Self {
vertices: try_with_capacity(vertices)?,
codes: try_with_capacity(vertices)?,
})
}
fn segment(&mut self, from: Point, to: Point) {
self.vertices.extend([from, to]);
self.codes.extend([PathCode::MoveTo, PathCode::LineTo]);
}
fn path(self) -> Path {
if self.vertices.is_empty() {
return Path::default();
}
Path::new(self.vertices, self.codes).expect("hatch codes are well formed")
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum HatchLines {
Horizontal,
Vertical,
NorthEast,
SouthEast,
}
impl HatchLines {
pub const ALL: [Self; 4] = [
Self::Horizontal,
Self::Vertical,
Self::NorthEast,
Self::SouthEast,
];
pub const fn characters(self) -> &'static [char] {
match self {
Self::Horizontal => &['-', '+'],
Self::Vertical => &['|', '+'],
Self::NorthEast => &['/', 'x', 'X'],
Self::SouthEast => &['\\', 'x', 'X'],
}
}
pub fn count(self, pattern: &str, density: usize) -> usize {
count(pattern, self.characters(), density)
}
pub fn vertex_count(self, lines: usize) -> usize {
match self {
Self::Horizontal | Self::Vertical => lines.saturating_mul(2),
Self::NorthEast | Self::SouthEast if lines == 0 => 0,
Self::NorthEast | Self::SouthEast => lines.saturating_add(1).saturating_mul(2),
}
}
pub fn path(self, lines: usize) -> Result<Path, OutOfMemory> {
let mut b = Builder::with_room(self.vertex_count(lines))?;
self.draw(lines, &mut b);
Ok(b.path())
}
fn draw(self, lines: usize, b: &mut Builder) {
if lines == 0 {
return;
}
match self {
Self::Horizontal | Self::Vertical => {
let step = 1.0 / lines as f64;
let horizontal = self == Self::Horizontal;
for s in linspace_with(0.0, 1.0, lines, false)
.into_iter()
.map(|s| s + step / 2.0)
{
if horizontal {
b.segment(Point::new(0.0, s), Point::new(1.0, s));
} else {
b.segment(Point::new(s, 0.0), Point::new(s, 1.0));
}
}
}
Self::NorthEast | Self::SouthEast => {
let rising = self == Self::NorthEast;
for s in linspace_with(-0.5, 0.5, lines + 1, true) {
if rising {
b.segment(Point::new(0.0 + s, 0.0 - s), Point::new(1.0 + s, 1.0 - s));
} else {
b.segment(Point::new(0.0 + s, 1.0 + s), Point::new(1.0 + s, 0.0 + s));
}
}
}
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct HatchShapes {
pub shape: Path,
pub size: f64,
pub filled: bool,
pub character: Option<char>,
}
impl HatchShapes {
pub fn small_circles() -> Self {
Self::circles(0.2, false, 'o')
}
pub fn large_circles() -> Self {
Self::circles(0.35, false, 'O')
}
pub fn small_filled_circles() -> Self {
Self::circles(0.1, true, '.')
}
pub fn stars() -> Self {
Self {
shape: star(),
size: 1.0 / 3.0,
filled: true,
character: Some('*'),
}
}
fn circles(size: f64, filled: bool, character: char) -> Self {
Self {
shape: Path::unit_circle(),
size,
filled,
character: Some(character),
}
}
pub fn all() -> [Self; 4] {
[
Self::small_circles(),
Self::large_circles(),
Self::small_filled_circles(),
Self::stars(),
]
}
pub fn count(&self, pattern: &str, density: usize) -> usize {
self.character.map_or(0, |c| count(pattern, &[c], density))
}
pub fn shape_count(rows: usize) -> usize {
if rows == 0 {
return 0;
}
(rows / 2 + 1)
.saturating_mul(rows + 1)
.saturating_add((rows / 2).saturating_mul(rows))
}
pub fn vertex_count(&self, rows: usize) -> usize {
let per_shape = self.shape.len() * if self.filled { 1 } else { 2 };
Self::shape_count(rows).saturating_mul(per_shape)
}
pub fn path(&self, rows: usize) -> Result<Path, OutOfMemory> {
let mut b = Builder::with_room(self.vertex_count(rows))?;
self.draw(rows, &mut b);
Ok(b.path())
}
fn draw(&self, rows: usize, b: &mut Builder) {
if rows == 0 {
return;
}
let offset = 1.0 / rows as f64;
let mut vertices: Vec<Point> = self
.shape
.vertices()
.iter()
.map(|v| (*v * offset) * self.size)
.collect();
let mut codes = self.shape.resolved_codes();
if !self.filled {
let backward: Vec<Point> = vertices.iter().rev().map(|v| *v * 0.9).collect();
vertices.extend(backward);
codes.extend_from_within(..);
}
for row in 0..=rows {
let row_pos = row as f64 * offset;
let cols: Vec<f64> = if row % 2 == 0 {
linspace_with(0.0, 1.0, rows + 1, true)
} else {
linspace_with(offset / 2.0, 1.0 - offset / 2.0, rows, true)
};
for col in cols {
let shift = Point::new(col, row_pos);
b.vertices.extend(vertices.iter().map(|v| *v + shift));
b.codes.extend_from_slice(&codes);
}
}
}
}
fn star() -> Path {
let vertices = Path::unit_regular_star(5, 0.5).vertices().to_vec();
let n = vertices.len();
let codes = (0..n)
.map(|i| match i {
0 => PathCode::MoveTo,
_ if i == n - 1 => PathCode::ClosePoly,
_ => PathCode::LineTo,
})
.collect();
Path::new(vertices, codes).expect("star codes are well formed")
}
pub fn pattern_path(pattern: &str, density: i32) -> Result<Path, HatchError> {
let density = usize::try_from(density).map_err(|_| HatchError::NegativeDensity)?;
let lines = HatchLines::ALL.map(|family| (family, family.count(pattern, density)));
let shapes = HatchShapes::all().map(|shapes| {
let rows = shapes.count(pattern, density);
(shapes, rows)
});
if shapes.iter().any(|(_, rows)| rows % 2 == 1) {
return Err(HatchError::OddShapeRows);
}
let estimate = lines
.iter()
.map(|&(_, n)| n.saturating_add(1).saturating_mul(2))
.chain(shapes.iter().map(|(shapes, rows)| {
let side = rows.saturating_add(1);
let copies = if shapes.filled { 1 } else { 2 };
side.saturating_mul(side)
.saturating_mul(shapes.shape.len() * copies)
}))
.fold(0, usize::saturating_add);
if estimate > MAX_VERTICES {
return Err(HatchError::TooDense);
}
let mut b = Builder::default();
for (family, n) in lines {
family.draw(n, &mut b);
}
for (shapes, rows) in &shapes {
shapes.draw(*rows, &mut b);
}
Ok(b.path())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn horizontal_lines() {
let path = pattern_path("-", 2).expect("valid");
assert_eq!(
path.vertices(),
&[
Point::new(0.0, 0.25),
Point::new(1.0, 0.25),
Point::new(0.0, 0.75),
Point::new(1.0, 0.75)
]
);
assert_eq!(path.code_at(2), PathCode::MoveTo);
}
#[test]
fn diagonals_include_both_corners() {
let path = pattern_path("/", 2).expect("valid");
assert_eq!(path.len(), 6);
assert_eq!(path.vertices()[0], Point::new(-0.5, 0.5));
assert_eq!(path.vertices()[5], Point::new(1.5, 0.5));
assert_eq!(pattern_path("x", 1).expect("valid").len(), 8);
}
#[test]
fn shapes_and_errors() {
let circles = pattern_path("o", 2).expect("valid");
assert_eq!(circles.len(), 8 * 26 * 2);
assert_eq!(pattern_path("o", 1), Err(HatchError::OddShapeRows));
assert_eq!(pattern_path("-", -1), Err(HatchError::NegativeDensity));
assert!(pattern_path("", 6).expect("valid").is_empty());
assert!(pattern_path("/", 0).expect("valid").is_empty());
}
#[test]
fn too_dense_patterns_are_rejected() {
assert_eq!(pattern_path("/", i32::MAX), Err(HatchError::TooDense));
assert_eq!(pattern_path("oo", 4096), Err(HatchError::TooDense));
assert_eq!(
pattern_path(&"x".repeat(1 << 20), 6),
Err(HatchError::TooDense)
);
assert_eq!(pattern_path("/", 1000).expect("valid").len(), 2002);
}
#[test]
fn pieces_count_as_matplotlibs_classes() {
assert_eq!(HatchLines::Horizontal.count("-+|", 3), 6);
assert_eq!(HatchLines::SouthEast.count("\\x", 2), 4);
assert_eq!(HatchLines::NorthEast.vertex_count(0), 0);
assert_eq!(HatchLines::NorthEast.vertex_count(4), 10);
assert_eq!(HatchShapes::shape_count(2), 8);
assert_eq!(HatchShapes::shape_count(0), 0);
let stars = HatchShapes::stars();
assert_eq!(stars.count("**", 3), 6);
assert_eq!(stars.vertex_count(2), 8 * 11);
assert_eq!(HatchShapes::small_circles().vertex_count(2), 8 * 26 * 2);
assert_eq!(stars.path(1).expect("memory").len(), 3 * 11);
}
#[test]
fn pieces_beyond_memory_fail_as_out_of_memory() {
let lines = 2_000_000_000_000_000;
assert!(HatchLines::SouthEast.path(lines).is_err());
assert!(HatchShapes::stars().path(1 << 40).is_err());
}
#[test]
fn the_pattern_is_its_pieces_in_order() {
let pattern = "-|/\\oO.*";
let whole = pattern_path(pattern, 2).expect("valid");
let mut vertices = Vec::new();
for family in HatchLines::ALL {
let piece = family.path(family.count(pattern, 2)).expect("memory");
vertices.extend_from_slice(piece.vertices());
}
for shapes in HatchShapes::all() {
let piece = shapes.path(shapes.count(pattern, 2)).expect("memory");
vertices.extend_from_slice(piece.vertices());
}
assert_eq!(whole.vertices(), vertices);
}
#[test]
fn shapes_of_the_callers_own() {
let square = Path::closed(
[
(-0.5, -0.5),
(0.5, -0.5),
(0.5, 0.5),
(-0.5, 0.5),
(0.0, 0.0),
]
.map(Point::from)
.to_vec(),
);
let shapes = HatchShapes {
shape: square,
size: 0.5,
filled: false,
character: None,
};
assert_eq!(shapes.count("oooo", 6), 0);
let path = shapes.path(2).expect("memory");
assert_eq!(path.len(), shapes.vertex_count(2));
assert_eq!(path.vertices()[0], Point::new(-0.125, -0.125));
}
}