layouttk 0.1.1

Integer-grid GDSII and OASIS layout processing with hierarchy-aware extraction
Documentation
//! Exact integer reconstruction of horizontal and vertical OASIS trapezoids.
use super::binary::coord;
use crate::{Error, Point, Result};

pub(super) struct Trapezoid {
    width: u64,
    height: u64,
    vertical: bool,
    delta_a: i128,
    delta_b: i128,
}

impl Trapezoid {
    pub fn general(width: u64, height: u64, vertical: bool, a: i64, b: i64) -> Self {
        Self {
            width,
            height,
            vertical,
            delta_a: i128::from(a),
            delta_b: i128::from(b),
        }
    }

    /// The compact types either specify one of eight 45-degree end patterns,
    /// collapse one parallel edge into a triangle tip, or specify a rectangle.
    pub fn compact(kind: u64, width: u64, height: u64) -> Result<Self> {
        const SIGNS: [(i128, i128); 8] = [
            (0, -1),
            (0, 1),
            (1, 0),
            (-1, 0),
            (1, -1),
            (-1, 1),
            (1, 1),
            (-1, -1),
        ];
        let (vertical, pattern) = match kind {
            0..=7 => (false, kind),
            8..=15 => (true, (kind - 8) ^ 1),
            16..=21 => (false, kind - 16),
            22..=23 => (true, 5 - (kind - 22)),
            24 | 25 => return Ok(Self::general(width, height, false, 0, 0)),
            _ => return Err(Error::invalid("invalid OASIS CTRAPEZOID type")),
        };
        let cross = i128::from(if vertical { width } else { height });
        let (a, b) = SIGNS[pattern as usize];
        Ok(Self {
            width,
            height,
            vertical,
            delta_a: a * cross,
            delta_b: b * cross,
        })
    }

    pub fn corners(&self, origin: Point) -> Result<[Point; 4]> {
        // In local coordinates, the parallel edges are on v=0 and v=span.
        // A vertical trapezoid maps local u to +y and local v to -x.
        let (length, span) = if self.vertical {
            (self.height, self.width)
        } else {
            (self.width, self.height)
        };
        let length = i128::from(length);
        let span = i128::from(span);
        let lower = (self.delta_a.min(0).abs(), length - self.delta_b.max(0));
        let upper = (self.delta_a.max(0), length + self.delta_b.min(0));
        if lower.0 > lower.1 || upper.0 > upper.1 {
            return Err(Error::invalid("crossed OASIS trapezoid sides"));
        }
        let coords = [(lower.0, 0), (lower.1, 0), (upper.1, span), (upper.0, span)];
        let mut out = [origin; 4];
        for (point, (u, v)) in out.iter_mut().zip(coords) {
            let (x, y) = if self.vertical { (span - v, u) } else { (u, v) };
            point.x = coord(i128::from(origin.x) + x)?;
            point.y = coord(i128::from(origin.y) + y)?;
        }
        Ok(out)
    }
}

// The unspecified dimension in these triangle/square descriptors is *derived*.
// It replaces the shared geometry width/height modal for following records.
pub(super) fn dimensions(
    kind: u64,
    flags: u8,
    width: Option<u64>,
    height: Option<u64>,
) -> Result<(u64, u64)> {
    let w = || width.ok_or_else(|| Error::invalid("undefined OASIS trapezoid width"));
    let h = || height.ok_or_else(|| Error::invalid("undefined OASIS trapezoid height"));
    let twice = |v: u64| {
        v.checked_mul(2)
            .ok_or_else(|| Error::unsupported("OASIS trapezoid dimension exceeds u64"))
    };
    if matches!(kind, 16..=19 | 22 | 23 | 25) && flags & 32 != 0
        || matches!(kind, 20 | 21) && flags & 64 != 0
    {
        return Err(Error::invalid(
            "explicit dimension forbidden for this OASIS CTRAPEZOID",
        ));
    }
    match kind {
        16..=19 | 25 => Ok((w()?, w()?)),
        20 | 21 => Ok((twice(h()?)?, h()?)),
        22 | 23 => Ok((w()?, twice(w()?)?)),
        0..=15 | 24 => Ok((w()?, h()?)),
        _ => Err(Error::invalid("invalid OASIS CTRAPEZOID type")),
    }
}