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
//! Types for Gerber code generation related to coordinates.

use std::convert::{From, Into};
use std::i64;
use std::io::Write;
use std::num::FpCategory;

use conv::TryFrom;
use num_rational::Ratio;

use crate::errors::{GerberError, GerberResult};
use crate::traits::PartialGerberCode;

// Helper macros

/// Automatically implement `PartialGerberCode` trait for struct types
/// that are based on `x` and `y` attributes.
macro_rules! impl_xy_partial_gerbercode {
    ($class:ty, $x:expr, $y: expr) => {
        impl<W: Write> PartialGerberCode<W> for $class {
            fn serialize_partial(&self, writer: &mut W) -> GerberResult<()> {
                if let Some(x) = self.x {
                    write!(writer, "{}{}", $x, x.gerber(&self.format)?)?;
                }
                if let Some(y) = self.y {
                    write!(writer, "{}{}", $y, y.gerber(&self.format)?)?;
                }
                Ok(())
            }
        }
    };
}

// Types

/// The coordinate format specifies the number of integer and decimal places in
/// a coordinate number. For example, the `24` format specifies 2 integer and 4
/// decimal places. The number of decimal places must be 4, 5 or 6. The number
/// of integer places must be not more than 6. Thus the longest representable
/// coordinate number is `nnnnnn.nnnnnn`.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct CoordinateFormat {
    pub integer: u8,
    pub decimal: u8,
}

impl CoordinateFormat {
    pub fn new(integer: u8, decimal: u8) -> Self {
        CoordinateFormat { integer, decimal }
    }
}

/// Coordinate numbers are integers conforming to the rules set by the FS
/// command.
///
/// Coordinate numbers are integers. Explicit decimal points are not allowed.
///
/// A coordinate number must have at least one character. Zero therefore must
/// be encoded as `0`.
///
/// The value is stored as a 64 bit integer with 6 decimal places.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct CoordinateNumber {
    nano: i64,
}

impl CoordinateNumber {
    pub fn new(nano: i64) -> Self {
        CoordinateNumber { nano }
    }
}

const DECIMAL_PLACES_CHARS: u8 = 6;
const DECIMAL_PLACES_FACTOR: i64 = 1_000_000;

impl TryFrom<f64> for CoordinateNumber {
    type Err = GerberError;
    fn try_from(val: f64) -> Result<Self, Self::Err> {
        match val.classify() {
            FpCategory::Nan => Err(GerberError::ConversionError("Value is NaN".into())),
            FpCategory::Infinite => Err(GerberError::ConversionError("Value is infinite".into())),
            FpCategory::Zero | FpCategory::Subnormal => Ok(CoordinateNumber { nano: 0 }),
            FpCategory::Normal => {
                let multiplied = val * DECIMAL_PLACES_FACTOR as f64;
                if (multiplied > i64::MAX as f64) || (multiplied < i64::MIN as f64) {
                    Err(GerberError::ConversionError(
                        "Value is out of bounds".into(),
                    ))
                } else {
                    Ok(CoordinateNumber {
                        nano: multiplied as i64,
                    })
                }
            }
        }
    }
}

impl Into<f64> for CoordinateNumber {
    fn into(self) -> f64 {
        (self.nano as f64) / DECIMAL_PLACES_FACTOR as f64
    }
}

macro_rules! impl_from_integer {
    ($class:ty) => {
        impl From<$class> for CoordinateNumber {
            fn from(val: $class) -> Self {
                CoordinateNumber {
                    nano: val as i64 * DECIMAL_PLACES_FACTOR,
                }
            }
        }
    };
}

// These are the types we can safely multiply with DECIMAL_PLACES_FACTOR
// without the risk of an overflow.
impl_from_integer!(i8);
impl_from_integer!(i16);
impl_from_integer!(i32);
impl_from_integer!(u8);
impl_from_integer!(u16);

impl CoordinateNumber {
    pub fn gerber(&self, format: &CoordinateFormat) -> Result<String, GerberError> {
        if format.decimal > DECIMAL_PLACES_CHARS {
            return Err(GerberError::CoordinateFormatError(
                "Invalid precision: Too high!".into(),
            ));
        }
        if self.nano.abs() >= 10_i64.pow((format.integer + DECIMAL_PLACES_CHARS) as u32) {
            return Err(GerberError::CoordinateFormatError(
                "Number is too large for chosen format!".into(),
            ));
        }

        let divisor: i64 = 10_i64.pow((DECIMAL_PLACES_CHARS - format.decimal) as u32);
        let number: i64 = Ratio::new(self.nano, divisor).round().to_integer();
        Ok(number.to_string())
    }
}

/// Coordinates are part of an operation.
///
/// Coordinates are modal. If an X is omitted, the X coordinate of the
/// current point is used. Similar for Y.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Coordinates {
    pub x: Option<CoordinateNumber>,
    pub y: Option<CoordinateNumber>,
    pub format: CoordinateFormat,
}

impl Coordinates {
    pub fn new<T, U>(x: T, y: U, format: CoordinateFormat) -> Self
    where
        T: Into<CoordinateNumber>,
        U: Into<CoordinateNumber>,
    {
        Coordinates {
            x: Some(x.into()),
            y: Some(y.into()),
            format,
        }
    }

    pub fn at_x<T>(x: T, format: CoordinateFormat) -> Self
    where
        T: Into<CoordinateNumber>,
    {
        Coordinates {
            x: Some(x.into()),
            y: None,
            format,
        }
    }

    pub fn at_y<T>(y: T, format: CoordinateFormat) -> Self
    where
        T: Into<CoordinateNumber>,
    {
        Coordinates {
            x: None,
            y: Some(y.into()),
            format,
        }
    }
}

impl_xy_partial_gerbercode!(Coordinates, "X", "Y");

/// Coordinate offsets can be used for interpolate operations in circular
/// interpolation mode.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CoordinateOffset {
    pub x: Option<CoordinateNumber>,
    pub y: Option<CoordinateNumber>,
    pub format: CoordinateFormat,
}

impl CoordinateOffset {
    pub fn new<T, U>(x: T, y: U, format: CoordinateFormat) -> Self
    where
        T: Into<CoordinateNumber>,
        U: Into<CoordinateNumber>,
    {
        CoordinateOffset {
            x: Some(x.into()),
            y: Some(y.into()),
            format,
        }
    }

    pub fn at_x<T>(x: T, format: CoordinateFormat) -> Self
    where
        T: Into<CoordinateNumber>,
    {
        CoordinateOffset {
            x: Some(x.into()),
            y: None,
            format,
        }
    }

    pub fn at_y<T>(y: T, format: CoordinateFormat) -> Self
    where
        T: Into<CoordinateNumber>,
    {
        CoordinateOffset {
            x: None,
            y: Some(y.into()),
            format,
        }
    }
}

impl_xy_partial_gerbercode!(CoordinateOffset, "I", "J");

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

    use std::f64;
    use std::io::BufWriter;

    use conv::TryFrom;

    use crate::traits::PartialGerberCode;

    #[test]
    /// Test integer to coordinate number conversion
    fn test_from_i8() {
        let a = CoordinateNumber { nano: 13000000 };
        let b = CoordinateNumber::from(13i8);
        assert_eq!(a, b);

        let c = CoordinateNumber { nano: -99000000 };
        let d = CoordinateNumber::from(-99i8);
        assert_eq!(c, d);
    }

    #[test]
    /// Test integer to coordinate number conversion
    fn test_from_i32() {
        let a = CoordinateNumber { nano: 13000000 };
        let b = CoordinateNumber::from(13);
        assert_eq!(a, b);

        let c = CoordinateNumber { nano: -998000000 };
        let d = CoordinateNumber::from(-998);
        assert_eq!(c, d);
    }

    #[test]
    /// Test float to coordinate number conversion
    fn test_try_from_f64_success() {
        let a = CoordinateNumber { nano: 1375000i64 };
        let b = CoordinateNumber::try_from(1.375f64).unwrap();
        assert_eq!(a, b);

        let c = CoordinateNumber {
            nano: 123456888888i64,
        };
        let d = CoordinateNumber::try_from(123456.888888f64).unwrap();
        assert_eq!(c, d);

        let e = CoordinateNumber { nano: 0i64 };
        let f = CoordinateNumber::try_from(0f64).unwrap();
        assert_eq!(e, f);

        let g = CoordinateNumber { nano: -12345678 };
        let h = CoordinateNumber::try_from(-12.345678).unwrap();
        assert_eq!(g, h);
    }

    #[test]
    /// Test failing float to coordinate number conversion
    fn test_try_from_f64_fail() {
        let cn1 = CoordinateNumber::try_from(f64::NAN);
        assert!(cn1.is_err());

        let cn2 = CoordinateNumber::try_from(f64::INFINITY);
        assert!(cn2.is_err());

        let cn3 = CoordinateNumber::try_from(f64::MAX - 1.0);
        assert!(cn3.is_err());

        let cn4 = CoordinateNumber::try_from(f64::MIN + 1.0);
        assert!(cn4.is_err());
    }

    #[test]
    /// Test coordinate number to float conversion
    fn test_into_f64() {
        let a: f64 = CoordinateNumber { nano: 1375000i64 }.into();
        let b = 1.375f64;
        assert_eq!(a, b);

        let c: f64 = CoordinateNumber {
            nano: 123456888888i64,
        }
        .into();
        let d = 123456.888888f64;
        assert_eq!(c, d);

        let e: f64 = CoordinateNumber { nano: 0i64 }.into();
        let f = 0f64;
        assert_eq!(e, f);
    }

    #[test]
    /// Test the coordinate number constructor creates correct
    /// coordinate numbers.
    fn test_coordinate_number_new() {
        let nano = 5;
        let cn1 = CoordinateNumber::new(nano);
        assert_eq!(cn1.nano, nano);
    }

    #[test]
    /// Test coordinate number to string conversion when it's 0
    fn test_formatted_zero() {
        let cf1 = CoordinateFormat::new(6, 6);
        let cf2 = CoordinateFormat::new(2, 4);

        let a = CoordinateNumber { nano: 0 }.gerber(&cf1).unwrap();
        let b = CoordinateNumber { nano: 0 }.gerber(&cf2).unwrap();
        assert_eq!(a, "0".to_string());
        assert_eq!(b, "0".to_string());
    }

    #[test]
    /// Test coordinate number to string conversion when the decimal part is 0
    fn test_formatted_decimal_zero() {
        let cf1 = CoordinateFormat::new(6, 6);
        let cf2 = CoordinateFormat::new(2, 4);

        let a = CoordinateNumber { nano: 10000000 }.gerber(&cf1).unwrap();
        let b = CoordinateNumber { nano: 20000000 }.gerber(&cf2).unwrap();
        assert_eq!(a, "10000000".to_string());
        assert_eq!(b, "200000".to_string());
    }

    #[test]
    /// Test coordinate number to string conversion
    fn test_formatted_65() {
        let cf = CoordinateFormat::new(6, 5);
        let d = CoordinateNumber { nano: 123456789012 }.gerber(&cf).unwrap();
        assert_eq!(d, "12345678901".to_string());
    }

    #[test]
    /// Test coordinate number to string conversion
    fn test_formatted_54() {
        let cf = CoordinateFormat::new(5, 4);
        let d = CoordinateNumber { nano: 12345678901 }.gerber(&cf).unwrap();
        assert_eq!(d, "123456789".to_string());
    }

    #[test]
    /// Test coordinate number to string conversion failure
    fn test_formatted_number_too_large() {
        let cf = CoordinateFormat::new(4, 5);
        let d = CoordinateNumber { nano: 12345000000 }.gerber(&cf);
        assert!(d.is_err());
    }

    #[test]
    /// Test coordinate number to string conversion failure
    fn test_formatted_negative_number_too_large() {
        let cf = CoordinateFormat::new(4, 5);
        let d = CoordinateNumber { nano: -12345000000 }.gerber(&cf);
        assert!(d.is_err());
    }

    #[test]
    /// Test coordinate number to string conversion (rounding of decimal part)
    fn test_formatted_44_rounding() {
        let cf = CoordinateFormat::new(4, 4);
        let d = CoordinateNumber { nano: 1234432199 }.gerber(&cf).unwrap();
        assert_eq!(d, "12344322".to_string());
    }

    #[test]
    /// Test negative coordinate number to string conversion
    fn test_formatted_negative_rounding() {
        let cf = CoordinateFormat::new(6, 4);
        let d = CoordinateNumber {
            nano: -123456789099,
        }
        .gerber(&cf)
        .unwrap();
        assert_eq!(d, "-1234567891".to_string());
    }

    #[test]
    fn test_coordinates_into() {
        let cf = CoordinateFormat::new(2, 4);
        let c1 = Coordinates::new(CoordinateNumber::from(1), CoordinateNumber::from(2), cf);
        let c2 = Coordinates::new(1, 2, cf);
        assert_eq!(c1, c2);
    }

    #[test]
    fn test_coordinates_into_mixed() {
        let cf = CoordinateFormat::new(2, 4);
        let c1 = Coordinates::new(CoordinateNumber::from(1), 2, cf);
        let c2 = Coordinates::new(1, 2, cf);
        assert_eq!(c1, c2);
    }

    #[test]
    fn test_coordinates() {
        macro_rules! assert_coords {
            ($coords:expr, $result:expr) => {{
                assert_partial_code!($coords, $result);
            }};
        }
        let cf44 = CoordinateFormat::new(4, 4);
        let cf46 = CoordinateFormat::new(4, 6);
        assert_coords!(Coordinates::new(10, 20, cf44), "X100000Y200000");
        assert_coords!(
            Coordinates {
                x: None,
                y: None,
                format: cf44
            },
            ""
        ); // TODO should we catch this?
        assert_coords!(Coordinates::at_x(10, cf44), "X100000");
        assert_coords!(Coordinates::at_y(20, cf46), "Y20000000");
        assert_coords!(Coordinates::new(0, -400, cf44), "X0Y-4000000");
    }

    #[test]
    fn test_offset() {
        macro_rules! assert_coords {
            ($coords:expr, $result:expr) => {{
                assert_partial_code!($coords, $result);
            }};
        }
        let cf44 = CoordinateFormat::new(4, 4);
        let cf55 = CoordinateFormat::new(5, 5);
        let cf66 = CoordinateFormat::new(6, 6);
        assert_coords!(CoordinateOffset::new(10, 20, cf44), "I100000J200000");
        assert_coords!(
            CoordinateOffset {
                x: None,
                y: None,
                format: cf44
            },
            ""
        ); // TODO should we catch this?
        assert_coords!(CoordinateOffset::at_x(10, cf66), "I10000000");
        assert_coords!(CoordinateOffset::at_y(20, cf55), "J2000000");
        assert_coords!(CoordinateOffset::new(0, -400, cf44), "I0J-4000000");
    }
}