use std::ops::Range;
use pltrs_core::{Affine2D, Path, Point, Rgba};
use crate::collection::PathCollection;
#[derive(Clone, Copy, Debug)]
pub struct QuadMesh<'a> {
pub master_transform: Affine2D,
pub columns: usize,
pub coordinates: &'a [Point],
pub facecolors: &'a [Rgba],
pub edgecolors: &'a [Rgba],
pub antialiased: bool,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct QuadMeshData {
pub master_transform: Affine2D,
pub columns: usize,
pub coordinates: Vec<Point>,
pub facecolors: Vec<Rgba>,
pub edgecolors: Vec<Rgba>,
pub antialiased: bool,
}
impl QuadMeshData {
pub fn view(&self) -> QuadMesh<'_> {
QuadMesh {
master_transform: self.master_transform,
columns: self.columns,
coordinates: &self.coordinates,
facecolors: &self.facecolors,
edgecolors: &self.edgecolors,
antialiased: self.antialiased,
}
}
}
const NUMPY_CORNERS: [(usize, usize); 4] = [(0, 0), (0, 1), (1, 1), (1, 0)];
const AGG_CORNERS: [(usize, usize); 4] = [(0, 0), (1, 0), (1, 1), (0, 1)];
impl QuadMesh<'_> {
fn vertex(&self, row: usize, col: usize) -> Point {
self.coordinates[row * (self.columns + 1) + col]
}
pub(crate) fn cell_count(&self) -> usize {
let stride = self.columns + 1;
let rows = (self.coordinates.len() / stride).saturating_sub(1);
rows * self.columns
}
fn outline(&self, index: usize, corners: [(usize, usize); 4]) -> [Point; 5] {
let (r, c) = (index / self.columns, index % self.columns);
let [a, b, d, e] = corners.map(|(dr, dc)| self.vertex(r + dr, c + dc));
[a, b, d, e, a]
}
pub fn cell_paths(&self) -> Vec<Path> {
(0..self.cell_count())
.map(|cell| Path::polyline(self.outline(cell, NUMPY_CORNERS).to_vec()))
.collect()
}
pub(crate) fn agg_cell_paths(&self) -> Vec<Path> {
let mut paths = Vec::new();
self.agg_cell_paths_into(0..self.cell_count(), &mut paths);
paths
}
pub(crate) fn agg_cell_paths_into(&self, cells: Range<usize>, paths: &mut Vec<Path>) {
paths.truncate(cells.len());
for (slot, cell) in cells.enumerate() {
let outline = self.outline(cell, AGG_CORNERS);
match paths.get_mut(slot) {
Some(path) => path.vertices_mut().copy_from_slice(&outline),
None => paths.push(Path::polyline(outline.to_vec())),
}
}
}
pub(crate) fn collection<'p>(
&self,
paths: &'p [Path],
linewidths: &'p [f64],
antialiased: &'p [bool],
) -> PathCollection<'p>
where
Self: 'p,
{
PathCollection {
master_transform: self.master_transform,
paths,
facecolors: self.facecolors,
edgecolors: self.edgecolors,
linewidths,
antialiased,
..PathCollection::default()
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn cells_in_matplotlib_and_agg_order() {
let coordinates: Vec<Point> = (0..2)
.flat_map(|r| (0..3).map(move |c| Point::new(f64::from(c), f64::from(r))))
.collect();
let mesh = QuadMesh {
master_transform: Affine2D::IDENTITY,
columns: 2,
coordinates: &coordinates,
facecolors: &[],
edgecolors: &[],
antialiased: false,
};
let numpy = mesh.cell_paths();
assert_eq!(numpy.len(), 2);
assert_eq!(
numpy[1].vertices(),
&[
Point::new(1.0, 0.0),
Point::new(2.0, 0.0),
Point::new(2.0, 1.0),
Point::new(1.0, 1.0),
Point::new(1.0, 0.0)
]
);
assert_eq!(mesh.agg_cell_paths()[1].vertices()[1], Point::new(1.0, 1.0));
assert_eq!(mesh.cell_count(), 2);
let mut paths = mesh.agg_cell_paths();
mesh.agg_cell_paths_into(1..2, &mut paths);
assert_eq!(paths, &mesh.agg_cell_paths()[1..]);
let empty = QuadMesh {
coordinates: &coordinates[..3],
..mesh
};
assert_eq!(empty.cell_paths(), Vec::<Path>::new());
let owned = QuadMeshData {
columns: 2,
coordinates: coordinates.clone(),
..QuadMeshData::default()
};
assert_eq!(owned.view().cell_paths(), numpy);
}
}