geometry.rs

1.5 kB · rust · 49 lines

1use super::models::CellNd;2use crate::core::cell;3use crate::core::error::Result;4use crate::core::tensor::Tensor;56/// Merges same-shaped cells into one block laid out by the per-axis repetition counts.7///8/// # Errors9///10/// Errors when the cells differ in shape or do not fill the arrangement.11pub fn merge_reps<const N: usize>(cells: &[CellNd<N>], reps: &[usize]) -> Result<CellNd<N>> {12    let inner: Vec<cell::Cell> = cells.iter().map(|c| c.cell.clone()).collect();13    Ok(CellNd {14        cell: cell::merge(&inner, reps)?,15    })16}1718/// Folds two or more cells into one by chained Kronecker combination.19///20/// # Errors21///22/// Errors when the cells differ in shape.23pub fn magic<const N: usize>(cells: &[CellNd<N>]) -> Result<CellNd<N>> {24    let inner: Vec<cell::Cell> = cells.iter().map(|c| c.cell.clone()).collect();25    Ok(CellNd {26        cell: cell::magic(&inner)?,27    })28}2930/// Builds a cell by placing at each mask site the cell its value indexes.31///32/// # Errors33///34/// Errors when the mask and the cells do not fit one another.35pub fn mosaic<const N: usize>(mask: &Tensor, cells: &[CellNd<N>]) -> Result<CellNd<N>> {36    let inner: Vec<cell::Cell> = cells.iter().map(|c| c.cell.clone()).collect();37    Ok(CellNd {38        cell: cell::mosaic(mask, &inner)?,39    })40}4142/// Writes the value into the cell wherever the tiled mask is nonzero.43///44/// # Errors45///46/// Errors when the mask does not tile the cell.47pub fn perforate<const N: usize>(mask: &Tensor, cell: &CellNd<N>, value: u8) -> Result<CellNd<N>> {48    cell.clone().perforate(mask, value)49}