gdsr 0.0.1-alpha.3

A GDSII reader and writer for Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
use std::f64::consts::PI;

use crate::{AngleInRadians, Movable, Point, Transformable, Transformation};

/// A grid layout that repeats elements in rows and columns with optional transformations.
#[derive(Clone, Debug, PartialEq)]
pub struct Grid {
    origin: Point,
    columns: u32,
    rows: u32,
    spacing_x: Option<Point>,
    spacing_y: Option<Point>,
    magnification: f64,
    angle: AngleInRadians,
    x_reflection: bool,
}

impl Grid {
    /// Creates a new grid with the given parameters.
    #[allow(clippy::too_many_arguments)]
    pub const fn new(
        origin: Point,
        columns: u32,
        rows: u32,
        spacing_x: Option<Point>,
        spacing_y: Option<Point>,
        magnification: f64,
        angle: AngleInRadians,
        x_reflection: bool,
    ) -> Self {
        Self {
            origin,
            columns,
            rows,
            spacing_x,
            spacing_y,
            magnification,
            angle,
            x_reflection,
        }
    }

    /// Returns the origin point of the grid.
    pub const fn origin(&self) -> Point {
        self.origin
    }

    /// Returns the number of columns.
    pub const fn columns(&self) -> u32 {
        self.columns
    }

    /// Returns the number of rows.
    pub const fn rows(&self) -> u32 {
        self.rows
    }

    /// Returns the column spacing vector, if set.
    pub const fn spacing_x(&self) -> Option<Point> {
        self.spacing_x
    }

    /// Returns the row spacing vector, if set.
    pub const fn spacing_y(&self) -> Option<Point> {
        self.spacing_y
    }

    /// Returns the magnification factor.
    pub const fn magnification(&self) -> f64 {
        self.magnification
    }

    /// Returns the rotation angle in radians.
    pub const fn angle(&self) -> f64 {
        self.angle
    }

    /// Returns whether x-axis reflection is enabled.
    pub const fn x_reflection(&self) -> bool {
        self.x_reflection
    }

    /// Sets the origin point.
    pub const fn set_origin(&mut self, origin: Point) {
        self.origin = origin;
    }

    /// Returns a new grid with the given origin.
    #[must_use]
    pub const fn with_origin(mut self, origin: Point) -> Self {
        self.origin = origin;
        self
    }

    /// Sets the number of columns.
    pub const fn set_columns(&mut self, columns: u32) {
        self.columns = columns;
    }

    /// Returns a new grid with the given number of columns.
    #[must_use]
    pub const fn with_columns(mut self, columns: u32) -> Self {
        self.columns = columns;
        self
    }

    /// Sets the number of rows.
    pub const fn set_rows(&mut self, rows: u32) {
        self.rows = rows;
    }

    /// Returns a new grid with the given number of rows.
    #[must_use]
    pub const fn with_rows(mut self, rows: u32) -> Self {
        self.rows = rows;
        self
    }

    /// Sets the column spacing vector.
    pub const fn set_spacing_x(&mut self, spacing_x: Option<Point>) {
        self.spacing_x = spacing_x;
    }

    /// Returns a new grid with the given column spacing vector.
    #[must_use]
    pub const fn with_spacing_x(mut self, spacing_x: Option<Point>) -> Self {
        self.spacing_x = spacing_x;
        self
    }

    /// Sets the row spacing vector.
    pub const fn set_spacing_y(&mut self, spacing_y: Option<Point>) {
        self.spacing_y = spacing_y;
    }

    /// Returns a new grid with the given row spacing vector.
    #[must_use]
    pub const fn with_spacing_y(mut self, spacing_y: Option<Point>) -> Self {
        self.spacing_y = spacing_y;
        self
    }

    /// Sets the magnification factor.
    pub const fn set_magnification(&mut self, magnification: f64) {
        self.magnification = magnification;
    }

    /// Returns a new grid with the given magnification factor.
    #[must_use]
    pub const fn with_magnification(mut self, magnification: f64) -> Self {
        self.magnification = magnification;
        self
    }

    /// Sets the rotation angle in radians.
    pub const fn set_angle(&mut self, angle: AngleInRadians) {
        self.angle = angle;
    }

    /// Returns a new grid with the given rotation angle.
    #[must_use]
    pub const fn with_angle(mut self, angle: AngleInRadians) -> Self {
        self.angle = angle;
        self
    }

    /// Sets whether x-axis reflection is enabled.
    pub const fn set_x_reflection(&mut self, x_reflection: bool) {
        self.x_reflection = x_reflection;
    }

    /// Returns a new grid with the given x-axis reflection setting.
    #[must_use]
    pub const fn with_x_reflection(mut self, x_reflection: bool) -> Self {
        self.x_reflection = x_reflection;
        self
    }

    /// Converts origin and spacing points to integer units.
    #[must_use]
    pub fn to_integer_unit(self) -> Self {
        Self {
            origin: self.origin.to_integer_unit(),
            spacing_x: self.spacing_x.as_ref().map(Point::to_integer_unit),
            spacing_y: self.spacing_y.as_ref().map(Point::to_integer_unit),
            ..self
        }
    }

    /// Converts origin and spacing points to float units.
    #[must_use]
    pub fn to_float_unit(self) -> Self {
        Self {
            origin: self.origin.to_float_unit(),
            spacing_x: self.spacing_x.as_ref().map(Point::to_float_unit),
            spacing_y: self.spacing_y.as_ref().map(Point::to_float_unit),
            ..self
        }
    }
}

impl Default for Grid {
    fn default() -> Self {
        Self::new(
            Point::integer(0, 0, 1e-9),
            1,
            1,
            None,
            None,
            1.0,
            0.0,
            false,
        )
    }
}

impl std::fmt::Display for Grid {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        let spacing_x_str = self
            .spacing_x
            .map_or_else(|| "None".to_string(), |p| p.to_string());
        let spacing_y_str = self
            .spacing_y
            .map_or_else(|| "None".to_string(), |p| p.to_string());
        write!(
            f,
            "Grid at {} with {} columns and {} rows, spacing ({}, {}), magnification {:?}, angle {:?}, x_reflection {}",
            self.origin,
            self.columns,
            self.rows,
            spacing_x_str,
            spacing_y_str,
            self.magnification,
            self.angle,
            self.x_reflection,
        )
    }
}

impl Transformable for Grid {
    fn transform_impl(mut self, transformation: &Transformation) -> Self {
        self.origin = transformation.apply_to_point(&self.origin);
        self.spacing_x = self.spacing_x.map(|p| transformation.apply_to_point(&p));
        self.spacing_y = self.spacing_y.map(|p| transformation.apply_to_point(&p));

        // Apply scale and rotation to grid properties
        if let Some(scale) = &transformation.scale {
            self.magnification *= scale.factor();
        }

        if let Some(rotation) = &transformation.rotation {
            self.angle += rotation.angle();
            let result = self.angle % (PI * 2.0);
            self.angle = if result < 0.0 {
                result + PI * 2.0
            } else {
                result
            };
        }

        // Handle reflection
        if transformation.reflection.is_some() {
            self.x_reflection = !self.x_reflection;
        }

        self
    }
}

impl Movable for Grid {
    fn move_to(mut self, target: Point) -> Self {
        self.origin = target;
        self
    }
}

#[cfg(test)]
mod tests {
    use std::f64::consts::FRAC_PI_2;

    use insta::assert_snapshot;

    use super::*;
    use crate::Point;

    fn p(x: i32, y: i32) -> Point {
        Point::integer(x, y, 1e-9)
    }

    fn pf(x: f64, y: f64) -> Point {
        Point::float(x, y, 1e-6)
    }

    fn origin() -> Point {
        p(0, 0)
    }

    /// Standard test grid used across multiple tests.
    fn test_grid() -> Grid {
        Grid::new(
            p(10, 20),
            2,
            2,
            Some(p(5, 0)),
            Some(p(0, 5)),
            1.0,
            0.0,
            false,
        )
    }

    #[test]
    fn test_grid_new_and_getters() {
        let grid = Grid::new(
            p(10, 20),
            3,
            4,
            Some(p(5, 0)),
            Some(p(0, 5)),
            1.5,
            45.0,
            true,
        );

        assert_eq!(grid.origin(), p(10, 20));
        assert_eq!(grid.columns(), 3);
        assert_eq!(grid.rows(), 4);
        assert_eq!(grid.spacing_x(), Some(p(5, 0)));
        assert_eq!(grid.spacing_y(), Some(p(0, 5)));
        assert_eq!(grid.magnification(), 1.5);
        assert_eq!(grid.angle(), 45.0);
        assert!(grid.x_reflection());
    }

    #[test]
    fn test_grid_default() {
        let grid = Grid::default();
        assert_eq!(grid.origin(), p(0, 0));
        assert_eq!(grid.columns(), 1);
        assert_eq!(grid.rows(), 1);
        assert_eq!(grid.spacing_x(), None);
        assert_eq!(grid.spacing_y(), None);
        assert_eq!(grid.magnification(), 1.0);
        assert_eq!(grid.angle(), 0.0);
        assert!(!grid.x_reflection());
    }

    #[test]
    fn test_grid_display() {
        let grid = Grid::new(
            p(10, 20),
            2,
            3,
            Some(p(5, 0)),
            Some(p(0, 5)),
            1.0,
            0.0,
            false,
        );
        assert_snapshot!(format!("{grid}"), @"Grid at Point(10 (1.000e-9), 20 (1.000e-9)) with 2 columns and 3 rows, spacing (Point(5 (1.000e-9), 0 (1.000e-9)), Point(0 (1.000e-9), 5 (1.000e-9))), magnification 1.0, angle 0.0, x_reflection false");

        let grid = Grid::new(p(10, 20), 2, 3, Some(p(5, 0)), None, 1.0, 0.0, false);
        assert_snapshot!(format!("{grid}"), @"Grid at Point(10 (1.000e-9), 20 (1.000e-9)) with 2 columns and 3 rows, spacing (Point(5 (1.000e-9), 0 (1.000e-9)), None), magnification 1.0, angle 0.0, x_reflection false");

        let grid = Grid::new(p(10, 20), 2, 3, None, Some(p(0, 5)), 1.0, 0.0, false);
        assert_snapshot!(format!("{grid}"), @"Grid at Point(10 (1.000e-9), 20 (1.000e-9)) with 2 columns and 3 rows, spacing (None, Point(0 (1.000e-9), 5 (1.000e-9))), magnification 1.0, angle 0.0, x_reflection false");
    }

    #[test]
    fn test_grid_setters_and_with_setters() {
        let mut grid = test_grid();
        grid.set_origin(p(100, 200));
        grid.set_columns(5);
        grid.set_rows(6);
        grid.set_spacing_x(Some(p(10, 0)));
        grid.set_spacing_y(Some(p(0, 10)));
        grid.set_magnification(2.0);
        grid.set_angle(90.0);
        grid.set_x_reflection(true);

        let grid_with = test_grid()
            .with_origin(p(100, 200))
            .with_columns(5)
            .with_rows(6)
            .with_spacing_x(Some(p(10, 0)))
            .with_spacing_y(Some(p(0, 10)))
            .with_magnification(2.0)
            .with_angle(90.0)
            .with_x_reflection(true);

        assert_eq!(grid, grid_with);
        assert_eq!(grid.origin(), p(100, 200));
        assert_eq!(grid.columns(), 5);
        assert_eq!(grid.rows(), 6);
        assert_eq!(grid.spacing_x(), Some(p(10, 0)));
        assert_eq!(grid.spacing_y(), Some(p(0, 10)));
        assert_eq!(grid.magnification(), 2.0);
        assert_eq!(grid.angle(), 90.0);
        assert!(grid.x_reflection());
    }

    #[test]
    fn test_grid_transform_with_scale() {
        let transformed = test_grid().scale(2.0, origin());
        assert_eq!(transformed.magnification, 2.0);
    }

    #[test]
    fn test_grid_transform_with_rotation() {
        let transformed = test_grid().rotate(FRAC_PI_2, origin());
        assert_eq!(transformed.angle, FRAC_PI_2);
    }

    #[test]
    fn test_grid_transform_with_reflection() {
        let transformed = test_grid().reflect(0.0, origin());
        assert!(transformed.x_reflection);
    }

    #[test]
    fn test_grid_transform_with_translation() {
        let transformed = test_grid().translate(p(5, 5));
        assert_eq!(transformed.origin, p(15, 25));
    }

    #[test]
    fn test_grid_move_to() {
        let target = p(100, 200);
        let moved = test_grid().move_to(target);
        assert_eq!(moved.origin, target);
    }

    #[test]
    fn test_grid_angle_normalization() {
        let grid = Grid::new(
            p(10, 20),
            2,
            2,
            Some(p(5, 0)),
            Some(p(0, 5)),
            1.0,
            FRAC_PI_2,
            false,
        );
        let transformed = grid.rotate(PI * 2.0, origin());
        assert!((transformed.angle - FRAC_PI_2).abs() < 0.001);
    }

    #[test]
    fn test_grid_1x1() {
        let grid = Grid::new(
            p(5, 10),
            1,
            1,
            Some(p(10, 0)),
            Some(p(0, 10)),
            1.0,
            0.0,
            false,
        );
        assert_eq!(grid.columns(), 1);
        assert_eq!(grid.rows(), 1);
        assert_eq!(grid.origin(), p(5, 10));
    }

    #[test]
    fn test_grid_zero_spacing() {
        let grid = Grid::default()
            .with_spacing_x(Some(p(0, 0)))
            .with_spacing_y(Some(p(0, 0)));
        assert_eq!(grid.spacing_x(), Some(p(0, 0)));
        assert_eq!(grid.spacing_y(), Some(p(0, 0)));
    }

    #[test]
    fn test_grid_negative_spacing() {
        let grid = Grid::default()
            .with_spacing_x(Some(p(-10, 0)))
            .with_spacing_y(Some(p(0, -10)));
        assert_eq!(grid.spacing_x(), Some(p(-10, 0)));
        assert_eq!(grid.spacing_y(), Some(p(0, -10)));
    }

    #[test]
    fn test_grid_transform_rotation_then_reflection() {
        let transformed = test_grid()
            .rotate(FRAC_PI_2, origin())
            .reflect(0.0, origin());
        assert!((transformed.angle() - FRAC_PI_2).abs() < 0.001);
        assert!(transformed.x_reflection());
    }

    #[test]
    fn test_grid_to_integer_unit() {
        let grid = Grid::new(
            pf(1.5, 2.5),
            2,
            3,
            Some(pf(10.0, 0.0)),
            Some(pf(0.0, 10.0)),
            1.0,
            0.0,
            false,
        );
        let converted = grid.to_integer_unit();

        assert_eq!(converted.origin(), pf(1.5, 2.5).to_integer_unit());
        assert_eq!(converted.spacing_x(), Some(pf(10.0, 0.0).to_integer_unit()));
        assert_eq!(converted.spacing_y(), Some(pf(0.0, 10.0).to_integer_unit()));
        assert_eq!(converted.columns(), 2);
        assert_eq!(converted.rows(), 3);
    }

    #[test]
    fn test_grid_to_float_unit() {
        let grid = Grid::new(
            p(10, 20),
            2,
            3,
            Some(p(5, 0)),
            Some(p(0, 5)),
            1.0,
            0.0,
            false,
        );
        let converted = grid.to_float_unit();

        assert_eq!(converted.origin(), p(10, 20).to_float_unit());
        assert_eq!(converted.spacing_x(), Some(p(5, 0).to_float_unit()));
        assert_eq!(converted.spacing_y(), Some(p(0, 5).to_float_unit()));
    }

    #[test]
    fn test_grid_to_integer_unit_none_spacing() {
        let grid = Grid::new(pf(1.0, 2.0), 2, 2, None, None, 1.0, 0.0, false);
        let converted = grid.to_integer_unit();
        assert_eq!(converted.spacing_x(), None);
        assert_eq!(converted.spacing_y(), None);
    }

    #[test]
    fn test_grid_zero_dimensions() {
        let grid = Grid::default().with_columns(0).with_rows(3);
        assert_eq!(grid.columns(), 0);
        assert_eq!(grid.rows(), 3);

        let grid = Grid::default().with_columns(3).with_rows(0);
        assert_eq!(grid.columns(), 3);
        assert_eq!(grid.rows(), 0);

        let grid = Grid::default().with_columns(0).with_rows(0);
        assert_eq!(grid.columns(), 0);
        assert_eq!(grid.rows(), 0);
    }

    #[test]
    fn test_grid_edge_magnification() {
        let grid = Grid::default().with_magnification(0.0);
        assert_eq!(grid.magnification(), 0.0);

        let grid = Grid::default().with_magnification(-2.0);
        assert_eq!(grid.magnification(), -2.0);
    }

    #[test]
    fn test_grid_transform_edge_magnification_with_scale() {
        let grid = test_grid().with_magnification(0.0);
        let transformed = grid.scale(2.0, origin());
        assert_eq!(transformed.magnification(), 0.0);

        let grid = test_grid().with_magnification(-1.0);
        let transformed = grid.scale(3.0, origin());
        assert_eq!(transformed.magnification(), -3.0);
    }
}