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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
use std::io::Write;

use crate::config::gds_file_types::{GDSDataType, GDSRecord, combine_record_and_data_type};
use crate::error::GdsError;
use crate::traits::ToGds;
use crate::utils::io::{
    validate_data_type, validate_layer, write_element_tail_to_file, write_points_to_file,
    write_u16_array_to_file,
};
use crate::{DataType, Dimensions, Layer, LayerMapping, Movable, Point, Transformable, Unit};

#[derive(Clone, Copy, PartialEq, Eq, Default, Debug)]
pub enum PathType {
    #[default]
    Square = 0,
    Round = 1,
    Overlap = 2,
}

impl PathType {
    pub const fn new(value: i32) -> Self {
        match value {
            1 => Self::Round,
            2 => Self::Overlap,
            _ => Self::Square,
        }
    }

    pub const fn value(&self) -> u16 {
        *self as u16
    }

    pub fn values() -> Vec<Self> {
        vec![Self::Square, Self::Round, Self::Overlap]
    }
}

/// An open path defined by a sequence of points, with optional width and end cap type.
#[derive(Clone, Debug, PartialEq, Default)]
pub struct Path {
    pub(crate) points: Vec<Point>,
    pub(crate) layer: Layer,
    pub(crate) data_type: DataType,
    pub(crate) r#type: Option<PathType>,
    pub(crate) width: Option<Unit>,
    pub(crate) begin_extension: Option<Unit>,
    pub(crate) end_extension: Option<Unit>,
}

impl Path {
    /// Creates a new path from the given points, layer, data type, optional end cap type, optional width, and optional extensions.
    pub fn new(
        points: impl IntoIterator<Item = Point>,
        layer: Layer,
        data_type: DataType,
        path_type: Option<PathType>,
        width: Option<Unit>,
        begin_extension: Option<Unit>,
        end_extension: Option<Unit>,
    ) -> Self {
        Self {
            points: points.into_iter().collect(),
            layer,
            data_type,
            r#type: path_type,
            width,
            begin_extension,
            end_extension,
        }
    }

    /// Returns the path's points.
    pub fn points(&self) -> &[Point] {
        &self.points
    }

    /// Returns the layer number.
    pub const fn layer(&self) -> Layer {
        self.layer
    }

    /// Returns the data type.
    pub const fn data_type(&self) -> DataType {
        self.data_type
    }

    /// Remaps the layer and data type using the given mapping.
    /// If the current (layer, `data_type`) pair is found in the mapping, it is replaced.
    pub fn remap_layers(&mut self, mapping: &LayerMapping) {
        if let Some(&(new_layer, new_data_type)) = mapping.get(&(self.layer, self.data_type)) {
            self.layer = new_layer;
            self.data_type = new_data_type;
        }
    }

    /// Returns the end cap type, if set.
    pub const fn path_type(&self) -> &Option<PathType> {
        &self.r#type
    }

    /// Returns the path width, if set.
    pub const fn width(&self) -> Option<Unit> {
        self.width
    }

    /// Returns the beginning extension distance, if set.
    pub const fn begin_extension(&self) -> Option<Unit> {
        self.begin_extension
    }

    /// Returns the end extension distance, if set.
    pub const fn end_extension(&self) -> Option<Unit> {
        self.end_extension
    }

    /// Converts all points, width, and extensions to integer units.
    #[must_use]
    pub fn to_integer_unit(self) -> Self {
        Self {
            points: self.points.iter().map(Point::to_integer_unit).collect(),
            width: self.width.map(Unit::to_integer_unit),
            begin_extension: self.begin_extension.map(Unit::to_integer_unit),
            end_extension: self.end_extension.map(Unit::to_integer_unit),
            ..self
        }
    }

    /// Expands this path to the polygon vertices representing its filled area.
    ///
    /// Returns `None` if the path has no width or fewer than 2 points.
    /// `arc_segments` controls the number of segments used for round end caps.
    pub fn to_polygon_points(&self, arc_segments: usize) -> Option<Vec<Point>> {
        let width = self.width?;
        if self.points.len() < 2 {
            return None;
        }

        let half_width = width.absolute_value() / 2.0;
        if half_width <= 0.0 {
            return None;
        }

        let path_type = self.r#type.unwrap_or_default();
        let begin_ext = self.begin_extension.map_or(0.0, |u| u.absolute_value());
        let end_ext = self.end_extension.map_or(0.0, |u| u.absolute_value());

        let units = self.points[0].units().0;
        let centerline: Vec<(f64, f64)> = self
            .points
            .iter()
            .map(|p| (p.x().float_value(), p.y().float_value()))
            .collect();

        let half_width_scaled = half_width / units;
        let begin_ext_scaled = begin_ext / units;
        let end_ext_scaled = end_ext / units;

        let expanded = crate::geometry::expand_path_to_polygon(
            &centerline,
            half_width_scaled,
            path_type,
            begin_ext_scaled,
            end_ext_scaled,
            arc_segments,
        );

        if expanded.is_empty() {
            return None;
        }

        Some(
            expanded
                .into_iter()
                .map(|(x, y)| Point::float(x, y, units))
                .collect(),
        )
    }

    /// Converts all points, width, and extensions to float units.
    #[must_use]
    pub fn to_float_unit(self) -> Self {
        Self {
            points: self.points.iter().map(Point::to_float_unit).collect(),
            width: self.width.map(Unit::to_float_unit),
            begin_extension: self.begin_extension.map(Unit::to_float_unit),
            end_extension: self.end_extension.map(Unit::to_float_unit),
            ..self
        }
    }
}

impl std::fmt::Display for Path {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(
            f,
            "Path with {} points on layer {} with data type {}, {:?} and width {}",
            self.points().len(),
            self.layer(),
            self.data_type(),
            self.path_type().unwrap_or_default(),
            self.width().unwrap_or_default()
        )?;
        if let Some(begin_ext) = self.begin_extension() {
            write!(f, ", begin_extension {begin_ext}")?;
        }
        if let Some(end_ext) = self.end_extension() {
            write!(f, ", end_extension {end_ext}")?;
        }
        Ok(())
    }
}

impl Transformable for Path {
    fn transform_impl(mut self, transformation: &crate::Transformation) -> Self {
        self.points = self
            .points()
            .iter()
            .map(|point| point.transform(transformation))
            .collect();

        self
    }
}

impl Movable for Path {
    fn move_to(self, target: Point) -> Self {
        let Some(first_point) = self.points().first() else {
            return self;
        };
        let delta = target - *first_point;
        self.move_by(delta)
    }
}

impl Dimensions for Path {
    fn bounding_box(&self) -> (Point, Point) {
        crate::geometry::bounding_box(&self.points)
    }
}

impl ToGds for Path {
    fn to_gds_impl(&self, database_units: f64) -> Result<Vec<u8>, GdsError> {
        validate_layer(self.layer())?;
        validate_data_type(self.data_type())?;

        if self.points().len() < 2 {
            return Err(GdsError::ValidationError {
                message: "Path must have at least 2 points".to_string(),
            });
        }

        let mut buffer = Vec::new();

        let path_head = [
            4,
            combine_record_and_data_type(GDSRecord::Path, GDSDataType::NoData),
            6,
            combine_record_and_data_type(GDSRecord::Layer, GDSDataType::TwoByteSignedInteger),
            self.layer().value(),
            6,
            combine_record_and_data_type(GDSRecord::DataType, GDSDataType::TwoByteSignedInteger),
            self.data_type().value(),
        ];

        write_u16_array_to_file(&mut buffer, &path_head)?;

        if let Some(path_type) = self.path_type() {
            let path_type_value = path_type.value();

            let path_type_head = [
                6,
                combine_record_and_data_type(
                    GDSRecord::PathType,
                    GDSDataType::TwoByteSignedInteger,
                ),
                path_type_value,
            ];

            write_u16_array_to_file(&mut buffer, &path_type_head)?;
        }

        if let Some(width) = self.width() {
            let scaled_width = width.scale_to(database_units);
            let width_value = scaled_width.as_integer_unit().value as u32;

            let width_head = [
                8,
                combine_record_and_data_type(GDSRecord::Width, GDSDataType::FourByteSignedInteger),
            ];

            write_u16_array_to_file(&mut buffer, &width_head)?;

            let bytes = width_value.to_be_bytes();

            buffer.write_all(&bytes)?;
        }

        if let Some(begin_ext) = self.begin_extension() {
            let scaled = begin_ext.scale_to(database_units);
            let value = scaled.as_integer_unit().value as u32;
            write_u16_array_to_file(
                &mut buffer,
                &[
                    8,
                    combine_record_and_data_type(
                        GDSRecord::BgnExtn,
                        GDSDataType::FourByteSignedInteger,
                    ),
                ],
            )?;
            buffer.write_all(&value.to_be_bytes())?;
        }

        if let Some(end_ext) = self.end_extension() {
            let scaled = end_ext.scale_to(database_units);
            let value = scaled.as_integer_unit().value as u32;
            write_u16_array_to_file(
                &mut buffer,
                &[
                    8,
                    combine_record_and_data_type(
                        GDSRecord::EndExtn,
                        GDSDataType::FourByteSignedInteger,
                    ),
                ],
            )?;
            buffer.write_all(&value.to_be_bytes())?;
        }

        write_points_to_file(&mut buffer, self.points(), database_units)?;

        write_element_tail_to_file(&mut buffer)?;

        Ok(buffer)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_path_type_new() {
        assert_eq!(PathType::new(0), PathType::Square);
        assert_eq!(PathType::new(1), PathType::Round);
        assert_eq!(PathType::new(2), PathType::Overlap);
        assert_eq!(PathType::new(-1), PathType::Square);
        assert_eq!(PathType::new(999), PathType::Square);
    }

    #[test]
    fn test_path_type_value() {
        assert_eq!(PathType::Square.value(), 0);
        assert_eq!(PathType::Round.value(), 1);
        assert_eq!(PathType::Overlap.value(), 2);
    }

    #[test]
    fn test_path_type_values() {
        let values = PathType::values();
        assert_eq!(values.len(), 3);
        assert!(values.contains(&PathType::Square));
        assert!(values.contains(&PathType::Round));
        assert!(values.contains(&PathType::Overlap));
    }

    #[test]
    fn test_path_type_default() {
        assert_eq!(PathType::default(), PathType::Square);
    }

    #[test]
    fn test_path_type_debug() {
        insta::assert_snapshot!(format!("{:?}", PathType::Square), @"Square");
        insta::assert_snapshot!(format!("{:?}", PathType::Round), @"Round");
        insta::assert_snapshot!(format!("{:?}", PathType::Overlap), @"Overlap");
    }

    #[test]
    fn test_path_type_clone_and_copy() {
        let path_type = PathType::Round;
        let cloned = path_type;
        let copied = path_type;

        assert_eq!(path_type, cloned);
        assert_eq!(path_type, copied);
    }

    #[test]
    fn test_path_type_partial_eq() {
        assert_eq!(PathType::Square, PathType::Square);
        assert_ne!(PathType::Square, PathType::Round);
        assert_ne!(PathType::Round, PathType::Overlap);
    }

    #[test]
    fn test_path_creation() {
        let points = vec![Point::integer(0, 0, 1e-9), Point::integer(100, 100, 1e-9)];
        let path = Path::new(
            points.clone(),
            Layer::new(1),
            DataType::new(2),
            Some(PathType::Round),
            Some(Unit::default_integer(10)),
            Some(Unit::default_integer(5)),
            Some(Unit::default_integer(15)),
        );

        assert_eq!(path.points(), &points);
        assert_eq!(path.layer(), Layer::new(1));
        assert_eq!(path.data_type(), DataType::new(2));
        assert_eq!(path.path_type(), &Some(PathType::Round));
        assert_eq!(path.width(), Some(Unit::default_integer(10)));
        assert_eq!(path.begin_extension(), Some(Unit::default_integer(5)));
        assert_eq!(path.end_extension(), Some(Unit::default_integer(15)));
    }

    #[test]
    fn test_path_default() {
        let path = Path::default();

        assert!(path.points().is_empty());
        assert_eq!(path.layer(), Layer::new(0));
        assert_eq!(path.data_type(), DataType::new(0));
        assert_eq!(path.path_type(), &None);
        assert_eq!(path.width(), None);
        assert_eq!(path.begin_extension(), None);
        assert_eq!(path.end_extension(), None);
    }

    #[test]
    fn test_path_display() {
        let points = vec![Point::integer(0, 0, 1e-9), Point::integer(100, 100, 1e-9)];
        let path = Path::new(
            points,
            Layer::new(5),
            DataType::new(10),
            Some(PathType::Square),
            Some(Unit::default_integer(20)),
            None,
            None,
        );

        insta::assert_snapshot!(path.to_string(), @"Path with 2 points on layer 5 with data type 10, Square and width 20 (1.000e-9)");
    }

    #[test]
    fn test_path_clone_and_partial_eq() {
        let points = vec![Point::integer(0, 0, 1e-9), Point::integer(10, 10, 1e-9)];
        let path1 = Path::new(
            points.clone(),
            Layer::new(1),
            DataType::new(2),
            Some(PathType::Round),
            Some(Unit::default_integer(5)),
            None,
            None,
        );
        let path2 = path1.clone();

        assert_eq!(path1, path2);

        let path3 = Path::new(
            points,
            Layer::new(1),
            DataType::new(2),
            Some(PathType::Square),
            Some(Unit::default_integer(5)),
            None,
            None,
        );
        assert_ne!(path1, path3);
    }

    #[test]
    fn test_path_to_integer_unit() {
        let points = vec![Point::float(1.5, 2.5, 1e-6), Point::float(10.0, 10.0, 1e-6)];
        let path = Path::new(
            points,
            Layer::new(1),
            DataType::new(0),
            Some(PathType::Round),
            Some(Unit::default_float(5.0)),
            None,
            None,
        );
        let converted = path.to_integer_unit();

        for point in converted.points() {
            assert_eq!(*point, point.to_integer_unit());
        }
        assert_eq!(
            converted.width(),
            Some(Unit::default_float(5.0).to_integer_unit())
        );
    }

    #[test]
    fn test_path_to_float_unit() {
        let points = vec![Point::integer(0, 0, 1e-9), Point::integer(100, 100, 1e-9)];
        let path = Path::new(
            points,
            Layer::new(1),
            DataType::new(0),
            None,
            Some(Unit::default_integer(10)),
            None,
            None,
        );
        let converted = path.to_float_unit();

        for point in converted.points() {
            assert_eq!(*point, point.to_float_unit());
        }
        assert_eq!(
            converted.width(),
            Some(Unit::default_integer(10).to_float_unit())
        );
    }

    #[test]
    fn test_path_to_integer_unit_no_width() {
        let points = vec![Point::float(1.0, 2.0, 1e-6)];
        let path = Path::new(
            points,
            Layer::new(0),
            DataType::new(0),
            None,
            None,
            None,
            None,
        );
        let converted = path.to_integer_unit();

        assert_eq!(converted.width(), None);
    }

    #[test]
    fn test_path_with_different_unit_points() {
        let points = vec![Point::integer(0, 0, 1e-9), Point::float(100.0, 100.0, 1e-6)];
        let path = Path::new(
            points,
            Layer::new(0),
            DataType::new(0),
            None,
            None,
            None,
            None,
        );
        assert_eq!(path.points().len(), 2);
    }

    #[test]
    fn test_path_bounding_box() {
        let points = vec![
            Point::integer(0, 0, 1e-9),
            Point::integer(10, 5, 1e-9),
            Point::integer(20, -3, 1e-9),
        ];
        let path = Path::new(
            points,
            Layer::new(1),
            DataType::new(0),
            None,
            None,
            None,
            None,
        );
        let (min, max) = path.bounding_box();
        assert_eq!(min, Point::integer(0, -3, 1e-9));
        assert_eq!(max, Point::integer(20, 5, 1e-9));
    }

    #[test]
    fn test_path_bounding_box_empty() {
        let path = Path::default();
        let (min, max) = path.bounding_box();
        assert_eq!(min, Point::default());
        assert_eq!(max, Point::default());
    }

    #[test]
    fn test_path_bounding_box_single_point() {
        let points = vec![Point::integer(5, 10, 1e-9)];
        let path = Path::new(
            points,
            Layer::new(1),
            DataType::new(0),
            None,
            None,
            None,
            None,
        );
        let (min, max) = path.bounding_box();
        assert_eq!(min, Point::integer(5, 10, 1e-9));
        assert_eq!(max, Point::integer(5, 10, 1e-9));
    }
}