Skip to main content

klayout_core/
instance.rs

1//! Instance: placement of a child cell with a transform, optional repetition,
2//! and provenance.
3
4use crate::cell::CellId;
5use crate::coord::{Bbox, Trans, Vec2};
6use crate::properties::Properties;
7
8#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
9pub struct SourceTag(pub u32);
10
11impl SourceTag {
12    pub const NONE: Self = Self(0);
13}
14
15/// OASIS-style placement repetition. Generalizes KLayout's `CellInstArray`.
16#[derive(Clone, Debug, PartialEq, Eq, Hash)]
17pub enum Repetition {
18    Regular {
19        row: Vec2,
20        col: Vec2,
21        n_rows: u32,
22        n_cols: u32,
23    },
24    Irregular {
25        offsets: Vec<Vec2>,
26    },
27}
28
29impl Repetition {
30    pub fn count(&self) -> usize {
31        match self {
32            Repetition::Regular { n_rows, n_cols, .. } => {
33                (*n_rows as usize) * (*n_cols as usize)
34            }
35            Repetition::Irregular { offsets } => offsets.len() + 1,
36        }
37    }
38
39    pub fn placement_bbox(&self) -> Bbox {
40        let mut b = Bbox::from_point(crate::coord::Point::ZERO);
41        match self {
42            Repetition::Regular {
43                row,
44                col,
45                n_rows,
46                n_cols,
47            } => {
48                if *n_rows == 0 || *n_cols == 0 {
49                    return Bbox::EMPTY;
50                }
51                let r = (*n_rows as i64).saturating_sub(1);
52                let c = (*n_cols as i64).saturating_sub(1);
53                let p = crate::coord::Point::new(
54                    col.x * c + row.x * r,
55                    col.y * c + row.y * r,
56                );
57                b.expand_to(p);
58            }
59            Repetition::Irregular { offsets } => {
60                for o in offsets {
61                    b.expand_to(crate::coord::Point::new(o.x, o.y));
62                }
63            }
64        }
65        b
66    }
67}
68
69#[derive(Clone, Debug, PartialEq, Eq, Hash)]
70pub struct Instance {
71    pub cell: CellId,
72    pub trans: Trans,
73    pub repetition: Option<Repetition>,
74    pub properties: Properties,
75    pub source: SourceTag,
76}
77
78impl Instance {
79    pub fn new(cell: CellId, trans: Trans) -> Self {
80        Self {
81            cell,
82            trans,
83            repetition: None,
84            properties: Properties::new(),
85            source: SourceTag::NONE,
86        }
87    }
88
89    pub fn with_repetition(mut self, r: Repetition) -> Self {
90        self.repetition = Some(r);
91        self
92    }
93}