summaryrefslogtreecommitdiff
path: root/gerbonara/gerber/tests/test_primitives.py
blob: 3f8df51a0ce87bc3d44d095f8468a7ff9329bc9e (plain)
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
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
#! /usr/bin/env python
# -*- coding: utf-8 -*-

# Author: Hamilton Kibbe <ham@hamiltonkib.be>

import pytest
from operator import add
from ..primitives import *


def test_primitive_smoketest():
    p = Primitive()
    try:
        p.bounding_box
        assert not True, "should have thrown the exception"
    except NotImplementedError:
        pass
    # pytest.raises(NotImplementedError, p.bounding_box)

    p.to_metric()
    p.to_inch()
    # try:
    #    p.offset(1, 1)
    #    assert_false(True, 'should have thrown the exception')
    # except NotImplementedError:
    #    pass


def test_line_angle():
    """ Test Line primitive angle calculation
    """
    cases = [
        ((0, 0), (1, 0), math.radians(0)),
        ((0, 0), (1, 1), math.radians(45)),
        ((0, 0), (0, 1), math.radians(90)),
        ((0, 0), (-1, 1), math.radians(135)),
        ((0, 0), (-1, 0), math.radians(180)),
        ((0, 0), (-1, -1), math.radians(225)),
        ((0, 0), (0, -1), math.radians(270)),
        ((0, 0), (1, -1), math.radians(315)),
    ]
    for start, end, expected in cases:
        l = Line(start, end, 0)
        line_angle = (l.angle + 2 * math.pi) % (2 * math.pi)
        pytest.approx(line_angle, expected)


def test_line_bounds():
    """ Test Line primitive bounding box calculation
    """
    cases = [
        ((0, 0), (1, 1), ((-1, -1), (2, 2))),
        ((-1, -1), (1, 1), ((-2, -2), (2, 2))),
        ((1, 1), (-1, -1), ((-2, -2), (2, 2))),
        ((-1, 1), (1, -1), ((-2, -2), (2, 2))),
    ]

    c = Circle((0, 0), 2)
    r = Rectangle((0, 0), 2, 2)
    for shape in (c, r):
        for start, end, expected in cases:
            l = Line(start, end, shape)
            assert l.bounding_box == expected
    # Test a non-square rectangle
    r = Rectangle((0, 0), 3, 2)
    cases = [
        ((0, 0), (1, 1), ((-1.5, -1), (2.5, 2))),
        ((-1, -1), (1, 1), ((-2.5, -2), (2.5, 2))),
        ((1, 1), (-1, -1), ((-2.5, -2), (2.5, 2))),
        ((-1, 1), (1, -1), ((-2.5, -2), (2.5, 2))),
    ]
    for start, end, expected in cases:
        l = Line(start, end, r)
        assert l.bounding_box == expected


def test_line_vertices():
    c = Circle((0, 0), 2)
    l = Line((0, 0), (1, 1), c)
    assert l.vertices == None

    # All 4 compass points, all 4 quadrants and the case where start == end
    test_cases = [
        ((0, 0), (1, 0), ((-1, -1), (-1, 1), (2, 1), (2, -1))),
        ((0, 0), (1, 1), ((-1, -1), (-1, 1), (0, 2), (2, 2), (2, 0), (1, -1))),
        ((0, 0), (0, 1), ((-1, -1), (-1, 2), (1, 2), (1, -1))),
        ((0, 0), (-1, 1), ((-1, -1), (-2, 0), (-2, 2), (0, 2), (1, 1), (1, -1))),
        ((0, 0), (-1, 0), ((-2, -1), (-2, 1), (1, 1), (1, -1))),
        ((0, 0), (-1, -1), ((-2, -2), (1, -1), (1, 1), (-1, 1), (-2, 0), (0, -2))),
        ((0, 0), (0, -1), ((-1, -2), (-1, 1), (1, 1), (1, -2))),
        ((0, 0), (1, -1), ((-1, -1), (0, -2), (2, -2), (2, 0), (1, 1), (-1, 1))),
        ((0, 0), (0, 0), ((-1, -1), (-1, 1), (1, 1), (1, -1))),
    ]
    r = Rectangle((0, 0), 2, 2)

    for start, end, vertices in test_cases:
        l = Line(start, end, r)
        assert set(vertices) == set(l.vertices)


def test_line_conversion():
    c = Circle((0, 0), 25.4, units="metric")
    l = Line((2.54, 25.4), (254.0, 2540.0), c, units="metric")

    # No effect
    l.to_metric()
    assert l.start == (2.54, 25.4)
    assert l.end == (254.0, 2540.0)
    assert l.aperture.diameter == 25.4

    l.to_inch()
    assert l.start == (0.1, 1.0)
    assert l.end == (10.0, 100.0)
    assert l.aperture.diameter == 1.0

    # No effect
    l.to_inch()
    assert l.start == (0.1, 1.0)
    assert l.end == (10.0, 100.0)
    assert l.aperture.diameter == 1.0

    c = Circle((0, 0), 1.0, units="inch")
    l = Line((0.1, 1.0), (10.0, 100.0), c, units="inch")

    # No effect
    l.to_inch()
    assert l.start == (0.1, 1.0)
    assert l.end == (10.0, 100.0)
    assert l.aperture.diameter == 1.0

    l.to_metric()
    assert l.start == (2.54, 25.4)
    assert l.end == (254.0, 2540.0)
    assert l.aperture.diameter == 25.4

    # No effect
    l.to_metric()
    assert l.start == (2.54, 25.4)
    assert l.end == (254.0, 2540.0)
    assert l.aperture.diameter == 25.4

    r = Rectangle((0, 0), 25.4, 254.0, units="metric")
    l = Line((2.54, 25.4), (254.0, 2540.0), r, units="metric")
    l.to_inch()
    assert l.start == (0.1, 1.0)
    assert l.end == (10.0, 100.0)
    assert l.aperture.width == 1.0
    assert l.aperture.height == 10.0

    r = Rectangle((0, 0), 1.0, 10.0, units="inch")
    l = Line((0.1, 1.0), (10.0, 100.0), r, units="inch")
    l.to_metric()
    assert l.start == (2.54, 25.4)
    assert l.end == (254.0, 2540.0)
    assert l.aperture.width == 25.4
    assert l.aperture.height == 254.0


def test_line_offset():
    c = Circle((0, 0), 1)
    l = Line((0, 0), (1, 1), c)
    l.offset(1, 0)
    assert l.start == (1.0, 0.0)
    assert l.end == (2.0, 1.0)
    l.offset(0, 1)
    assert l.start == (1.0, 1.0)
    assert l.end == (2.0, 2.0)


def test_arc_radius():
    """ Test Arc primitive radius calculation
    """
    cases = [((-3, 4), (5, 0), (0, 0), 5), ((0, 1), (1, 0), (0, 0), 1)]

    for start, end, center, radius in cases:
        a = Arc(start, end, center, "clockwise", 0, "single-quadrant")
        assert a.radius == radius


def test_arc_sweep_angle():
    """ Test Arc primitive sweep angle calculation
    """
    cases = [
        ((1, 0), (0, 1), (0, 0), "counterclockwise", math.radians(90)),
        ((1, 0), (0, 1), (0, 0), "clockwise", math.radians(270)),
        ((1, 0), (-1, 0), (0, 0), "clockwise", math.radians(180)),
        ((1, 0), (-1, 0), (0, 0), "counterclockwise", math.radians(180)),
    ]

    for start, end, center, direction, sweep in cases:
        c = Circle((0, 0), 1)
        a = Arc(start, end, center, direction, c, "single-quadrant")
        assert a.sweep_angle == sweep


def test_arc_bounds():
    """ Test Arc primitive bounding box calculation
    """
    cases = [
        (( 1,  0), ( 0,  1), (0, 0), "clockwise",        ((-1.5, -1.5), (1.5, 1.5))),
        (( 1,  0), ( 0,  1), (0, 0), "counterclockwise", ((-0.5, -0.5), (1.5, 1.5))),
        (( 0,  1), (-1,  0), (0, 0), "clockwise",        ((-1.5, -1.5), (1.5, 1.5))),
        (( 0,  1), (-1,  0), (0, 0), "counterclockwise", ((-1.5, -0.5), (0.5, 1.5))),
        ((-1,  0), ( 0, -1), (0, 0), "clockwise",        ((-1.5, -1.5), (1.5, 1.5))),
        ((-1,  0), ( 0, -1), (0, 0), "counterclockwise", ((-1.5, -1.5), (0.5, 0.5))),
        (( 0, -1), ( 1,  0), (0, 0), "clockwise",        ((-1.5, -1.5), (1.5, 1.5))),
        (( 0, -1), ( 1,  0), (0, 0), "counterclockwise", ((-0.5, -1.5), (1.5, 0.5))),
        # Arcs with the same start and end point render a full circle
        (( 1,  0), ( 1,  0), (0, 0), "clockwise",        ((-1.5, -1.5), (1.5, 1.5))),
        (( 1,  0), ( 1,  0), (0, 0), "counterclockwise", ((-1.5, -1.5), (1.5, 1.5))),
    ]
    for start, end, center, direction, bounds in cases:
        c = Circle((0, 0), 1)
        a = Arc(start, end, center, direction, c, "multi-quadrant")
        assert a.bounding_box == bounds


def test_arc_bounds_no_aperture():
    """ Test Arc primitive bounding box calculation ignoring aperture
    """
    cases = [
        ((1, 0), (0, 1), (0, 0), "clockwise", ((-1.0, -1.0), (1.0, 1.0))),
        ((1, 0), (0, 1), (0, 0), "counterclockwise", ((0.0, 0.0), (1.0, 1.0))),
        ((0, 1), (-1, 0), (0, 0), "clockwise", ((-1.0, -1.0), (1.0, 1.0))),
        ((0, 1), (-1, 0), (0, 0), "counterclockwise", ((-1.0, 0.0), (0.0, 1.0))),
        ((-1, 0), (0, -1), (0, 0), "clockwise", ((-1.0, -1.0), (1.0, 1.0))),
        ((-1, 0), (0, -1), (0, 0), "counterclockwise", ((-1.0, -1.0), (0.0, 0.0))),
        ((0, -1), (1, 0), (0, 0), "clockwise", ((-1.0, -1.0), (1.0, 1.0))),
        ((0, -1), (1, 0), (0, 0), "counterclockwise", ((-0.0, -1.0), (1.0, 0.0))),
        # Arcs with the same start and end point render a full circle
        ((1, 0), (1, 0), (0, 0), "clockwise", ((-1.0, -1.0), (1.0, 1.0))),
        ((1, 0), (1, 0), (0, 0), "counterclockwise", ((-1.0, -1.0), (1.0, 1.0))),
    ]
    for start, end, center, direction, bounds in cases:
        c = Circle((0, 0), 1)
        a = Arc(start, end, center, direction, c, "multi-quadrant")
        assert a.bounding_box_no_aperture == bounds


def test_arc_conversion():
    c = Circle((0, 0), 25.4, units="metric")
    a = Arc(
        (2.54, 25.4),
        (254.0, 2540.0),
        (25400.0, 254000.0),
        "clockwise",
        c,
        "single-quadrant",
        units="metric",
    )

    # No effect
    a.to_metric()
    assert a.start == (2.54, 25.4)
    assert a.end == (254.0, 2540.0)
    assert a.center == (25400.0, 254000.0)
    assert a.aperture.diameter == 25.4

    a.to_inch()
    assert a.start == (0.1, 1.0)
    assert a.end == (10.0, 100.0)
    assert a.center == (1000.0, 10000.0)
    assert a.aperture.diameter == 1.0

    # no effect
    a.to_inch()
    assert a.start == (0.1, 1.0)
    assert a.end == (10.0, 100.0)
    assert a.center == (1000.0, 10000.0)
    assert a.aperture.diameter == 1.0

    c = Circle((0, 0), 1.0, units="inch")
    a = Arc(
        (0.1, 1.0),
        (10.0, 100.0),
        (1000.0, 10000.0),
        "clockwise",
        c,
        "single-quadrant",
        units="inch",
    )
    a.to_metric()
    assert a.start == (2.54, 25.4)
    assert a.end == (254.0, 2540.0)
    assert a.center == (25400.0, 254000.0)
    assert a.aperture.diameter == 25.4


def test_arc_offset():
    c = Circle((0, 0), 1)
    a = Arc((0, 0), (1, 1), (2, 2), "clockwise", c, "single-quadrant")
    a.offset(1, 0)
    assert a.start == (1.0, 0.0)
    assert a.end == (2.0, 1.0)
    assert a.center == (3.0, 2.0)
    a.offset(0, 1)
    assert a.start == (1.0, 1.0)
    assert a.end == (2.0, 2.0)
    assert a.center == (3.0, 3.0)


def test_circle_radius():
    """ Test Circle primitive radius calculation
    """
    c = Circle((1, 1), 2)
    assert c.radius == 1


def test_circle_hole_radius():
    """ Test Circle primitive hole radius calculation
    """
    c = Circle((1, 1), 4, 2)
    assert c.hole_radius == 1


def test_circle_bounds():
    """ Test Circle bounding box calculation
    """
    c = Circle((1, 1), 2)
    assert c.bounding_box == ((0, 0), (2, 2))


def test_circle_conversion():
    """Circle conversion of units"""
    # Circle initially metric, no hole
    c = Circle((2.54, 25.4), 254.0, units="metric")

    c.to_metric()  # shouldn't do antyhing
    assert c.position == (2.54, 25.4)
    assert c.diameter == 254.0
    assert c.hole_diameter == None

    c.to_inch()
    assert c.position == (0.1, 1.0)
    assert c.diameter == 10.0
    assert c.hole_diameter == None

    # no effect
    c.to_inch()
    assert c.position == (0.1, 1.0)
    assert c.diameter == 10.0
    assert c.hole_diameter == None

    # Circle initially metric, with hole
    c = Circle((2.54, 25.4), 254.0, 127.0, units="metric")

    c.to_metric()  # shouldn't do antyhing
    assert c.position == (2.54, 25.4)
    assert c.diameter == 254.0
    assert c.hole_diameter == 127.0

    c.to_inch()
    assert c.position == (0.1, 1.0)
    assert c.diameter == 10.0
    assert c.hole_diameter == 5.0

    # no effect
    c.to_inch()
    assert c.position == (0.1, 1.0)
    assert c.diameter == 10.0
    assert c.hole_diameter == 5.0

    # Circle initially inch, no hole
    c = Circle((0.1, 1.0), 10.0, units="inch")
    # No effect
    c.to_inch()
    assert c.position == (0.1, 1.0)
    assert c.diameter == 10.0
    assert c.hole_diameter == None

    c.to_metric()
    assert c.position == (2.54, 25.4)
    assert c.diameter == 254.0
    assert c.hole_diameter == None

    # no effect
    c.to_metric()
    assert c.position == (2.54, 25.4)
    assert c.diameter == 254.0
    assert c.hole_diameter == None

    c = Circle((0.1, 1.0), 10.0, 5.0, units="inch")
    # No effect
    c.to_inch()
    assert c.position == (0.1, 1.0)
    assert c.diameter == 10.0
    assert c.hole_diameter == 5.0

    c.to_metric()
    assert c.position == (2.54, 25.4)
    assert c.diameter == 254.0
    assert c.hole_diameter == 127.0

    # no effect
    c.to_metric()
    assert c.position == (2.54, 25.4)
    assert c.diameter == 254.0
    assert c.hole_diameter == 127.0


def test_circle_offset():
    c = Circle((0, 0), 1)
    c.offset(1, 0)
    assert c.position == (1.0, 0.0)
    c.offset(0, 1)
    assert c.position == (1.0, 1.0)


def test_rectangle_ctor():
    """ Test rectangle creation
    """
    test_cases = (((0, 0), 1, 1), ((0, 0), 1, 2), ((1, 1), 1, 2))
    for pos, width, height in test_cases:
        r = Rectangle(pos, width, height)
        assert r.position == pos
        assert r.width == width
        assert r.height == height


def test_rectangle_hole_radius():
    """ Test rectangle hole diameter calculation
    """
    r = Rectangle((0, 0), 2, 2)
    assert 0 == r.hole_radius

    r = Rectangle((0, 0), 2, 2, 1)
    assert 0.5 == r.hole_radius


def test_rectangle_bounds():
    """ Test rectangle bounding box calculation
    """
    r = Rectangle((0, 0), 2, 2)
    bounds = r.bounding_box
    pytest.approx(bounds[0], (-1, -1))
    pytest.approx(bounds[1], (1, 1))
    r = Rectangle((0, 0), 2, 2, rotation=45)
    bounds = r.bounding_box
    pytest.approx(bounds[0], (-math.sqrt(2), -math.sqrt(2)))
    pytest.approx(bounds[1], (math.sqrt(2), math.sqrt(2)))


def test_rectangle_vertices():
    sqrt2 = math.sqrt(2.0)
    TEST_VECTORS = [
        ((0, 0), 2.0, 2.0, 0.0, ((-1.0, -1.0), (-1.0, 1.0), (1.0, 1.0), (1.0, -1.0))),
        ((0, 0), 2.0, 3.0, 0.0, ((-1.0, -1.5), (-1.0, 1.5), (1.0, 1.5), (1.0, -1.5))),
        ((0, 0), 2.0, 2.0, 90.0, ((-1.0, -1.0), (-1.0, 1.0), (1.0, 1.0), (1.0, -1.0))),
        ((0, 0), 3.0, 2.0, 90.0, ((-1.0, -1.5), (-1.0, 1.5), (1.0, 1.5), (1.0, -1.5))),
        (
            (0, 0),
            2.0,
            2.0,
            45.0,
            ((-sqrt2, 0.0), (0.0, sqrt2), (sqrt2, 0), (0, -sqrt2)),
        ),
    ]
    for pos, width, height, rotation, expected in TEST_VECTORS:
        r = Rectangle(pos, width, height, rotation=rotation)
        for test, expect in zip(sorted(r.vertices), sorted(expected)):
            pytest.approx(test, expect)

    r = Rectangle((0, 0), 2.0, 2.0, rotation=0.0)
    r.rotation = 45.0
    for test, expect in zip(
        sorted(r.vertices),
        sorted(((-sqrt2, 0.0), (0.0, sqrt2), (sqrt2, 0), (0, -sqrt2))),
    ):
        pytest.approx(test, expect)


def test_rectangle_segments():

    r = Rectangle((0, 0), 2.0, 2.0)
    expected = [vtx for segment in r.segments for vtx in segment]
    for vertex in r.vertices:
        assert vertex in expected


def test_rectangle_conversion():
    """Test converting rectangles between units"""

    # Initially metric no hole
    r = Rectangle((2.54, 25.4), 254.0, 2540.0, units="metric")

    r.to_metric()
    assert r.position == (2.54, 25.4)
    assert r.width == 254.0
    assert r.height == 2540.0

    r.to_inch()
    assert r.position == (0.1, 1.0)
    assert r.width == 10.0
    assert r.height == 100.0

    r.to_inch()
    assert r.position == (0.1, 1.0)
    assert r.width == 10.0
    assert r.height == 100.0

    # Initially metric with hole
    r = Rectangle((2.54, 25.4), 254.0, 2540.0, 127.0, units="metric")

    r.to_metric()
    assert r.position == (2.54, 25.4)
    assert r.width == 254.0
    assert r.height == 2540.0
    assert r.hole_diameter == 127.0

    r.to_inch()
    assert r.position == (0.1, 1.0)
    assert r.width == 10.0
    assert r.height == 100.0
    assert r.hole_diameter == 5.0

    r.to_inch()
    assert r.position == (0.1, 1.0)
    assert r.width == 10.0
    assert r.height == 100.0
    assert r.hole_diameter == 5.0

    # Initially inch, no hole
    r = Rectangle((0.1, 1.0), 10.0, 100.0, units="inch")
    r.to_inch()
    assert r.position == (0.1, 1.0)
    assert r.width == 10.0
    assert r.height == 100.0

    r.to_metric()
    assert r.position == (2.54, 25.4)
    assert r.width == 254.0
    assert r.height == 2540.0

    r.to_metric()
    assert r.position == (2.54, 25.4)
    assert r.width == 254.0
    assert r.height == 2540.0

    # Initially inch with hole
    r = Rectangle((0.1, 1.0), 10.0, 100.0, 5.0, units="inch")
    r.to_inch()
    assert r.position == (0.1, 1.0)
    assert r.width == 10.0
    assert r.height == 100.0
    assert r.hole_diameter == 5.0

    r.to_metric()
    assert r.position == (2.54, 25.4)
    assert r.width == 254.0
    assert r.height == 2540.0
    assert r.hole_diameter == 127.0

    r.to_metric()
    assert r.position == (2.54, 25.4)
    assert r.width == 254.0
    assert r.height == 2540.0
    assert r.hole_diameter == 127.0


def test_rectangle_offset():
    r = Rectangle((0, 0), 1, 2)
    r.offset(1, 0)
    assert r.position == (1.0, 0.0)
    r.offset(0, 1)
    assert r.position == (1.0, 1.0)


def test_round_rectangle_ctor():
    """ Test round rectangle creation
    """
    test_cases = (
        ((0, 0), 1, 1, 0.2, (True, True, False, False)),
        ((0, 0), 1, 2, 0.3, (True, True, True, True)),
        ((1, 1), 1, 2, 0.4, (False, False, False, False)),
    )
    for pos, width, height, radius, corners in test_cases:
        r = RoundRectangle(pos, width, height, radius, corners)
        assert r.position == pos
        assert r.width == width
        assert r.height == height
        assert r.radius == radius
        pytest.approx(r.corners, corners)


def test_round_rectangle_bounds():
    """ Test round rectangle bounding box calculation
    """
    r = RoundRectangle((0, 0), 2, 2, 0.2, (True, True, False, False))
    bounds = r.bounding_box
    pytest.approx(bounds[0], (-1, -1))
    pytest.approx(bounds[1], (1, 1))
    r = RoundRectangle((0, 0), 2, 2, 0.2, (True, True, False, False), rotation=45)
    bounds = r.bounding_box
    pytest.approx(bounds[0], (-math.sqrt(2), -math.sqrt(2)))
    pytest.approx(bounds[1], (math.sqrt(2), math.sqrt(2)))


def test_obround_ctor():
    """ Test obround creation
    """
    test_cases = (((0, 0), 1, 1), ((0, 0), 1, 2), ((1, 1), 1, 2))
    for pos, width, height in test_cases:
        o = Obround(pos, width, height)
        assert o.position == pos
        assert o.width == width
        assert o.height == height


def test_obround_bounds():
    """ Test obround bounding box calculation
    """
    o = Obround((2, 2), 2, 4)
    bounds = o.bounding_box
    pytest.approx(bounds[0], (1, 0))
    pytest.approx(bounds[1], (3, 4))
    o = Obround((2, 2), 4, 2)
    bounds = o.bounding_box
    pytest.approx(bounds[0], (0, 1))
    pytest.approx(bounds[1], (4, 3))


def test_obround_orientation():
    o = Obround((0, 0), 2, 1)
    assert o.orientation == "horizontal"
    o = Obround((0, 0), 1, 2)
    assert o.orientation == "vertical"


def test_obround_subshapes():
    o = Obround((0, 0), 1, 4)
    ss = o.subshapes
    pytest.approx(ss["rectangle"].position, (0, 0))
    pytest.approx(ss["circle1"].position, (0, 1.5))
    pytest.approx(ss["circle2"].position, (0, -1.5))
    o = Obround((0, 0), 4, 1)
    ss = o.subshapes
    pytest.approx(ss["rectangle"].position, (0, 0))
    pytest.approx(ss["circle1"].position, (1.5, 0))
    pytest.approx(ss["circle2"].position, (-1.5, 0))


def test_obround_conversion():
    o = Obround((2.54, 25.4), 254.0, 2540.0, units="metric")

    # No effect
    o.to_metric()
    assert o.position == (2.54, 25.4)
    assert o.width == 254.0
    assert o.height == 2540.0

    o.to_inch()
    assert o.position == (0.1, 1.0)
    assert o.width == 10.0
    assert o.height == 100.0

    # No effect
    o.to_inch()
    assert o.position == (0.1, 1.0)
    assert o.width == 10.0
    assert o.height == 100.0

    o = Obround((0.1, 1.0), 10.0, 100.0, units="inch")

    # No effect
    o.to_inch()
    assert o.position == (0.1, 1.0)
    assert o.width == 10.0
    assert o.height == 100.0

    o.to_metric()
    assert o.position == (2.54, 25.4)
    assert o.width == 254.0
    assert o.height == 2540.0

    # No effect
    o.to_metric()
    assert o.position == (2.54, 25.4)
    assert o.width == 254.0
    assert o.height == 2540.0


def test_obround_offset():
    o = Obround((0, 0), 1, 2)
    o.offset(1, 0)
    assert o.position == (1.0, 0.0)
    o.offset(0, 1)
    assert o.position == (1.0, 1.0)


def test_polygon_ctor():
    """ Test polygon creation
    """
    test_cases = (((0, 0), 3, 5, 0), ((0, 0), 5, 6, 0), ((1, 1), 7, 7, 45))
    for pos, sides, radius, hole_diameter in test_cases:
        p = Polygon(pos, sides, radius, hole_diameter)
        assert p.position == pos
        assert p.sides == sides
        assert p.radius == radius
        assert p.hole_diameter == hole_diameter


def test_polygon_bounds():
    """ Test polygon bounding box calculation
    """
    p = Polygon((2, 2), 3, 2, 0)
    bounds = p.bounding_box
    pytest.approx(bounds[0], (0, 0))
    pytest.approx(bounds[0], (4, 4))
    p = Polygon((2, 2), 3, 4, 0)
    bounds = p.bounding_box
    pytest.approx(bounds[0], (-2, -2))
    pytest.approx(bounds[1], (6, 6))


def test_polygon_conversion():
    p = Polygon((2.54, 25.4), 3, 254.0, 0, units="metric")

    # No effect
    p.to_metric()
    assert p.position == (2.54, 25.4)
    assert p.radius == 254.0

    p.to_inch()
    assert p.position == (0.1, 1.0)
    assert p.radius == 10.0

    # No effect
    p.to_inch()
    assert p.position == (0.1, 1.0)
    assert p.radius == 10.0

    p = Polygon((0.1, 1.0), 3, 10.0, 0, units="inch")

    # No effect
    p.to_inch()
    assert p.position == (0.1, 1.0)
    assert p.radius == 10.0

    p.to_metric()
    assert p.position == (2.54, 25.4)
    assert p.radius == 254.0

    # No effect
    p.to_metric()
    assert p.position == (2.54, 25.4)
    assert p.radius == 254.0


def test_polygon_offset():
    p = Polygon((0, 0), 5, 10, 0)
    p.offset(1, 0)
    assert p.position == (1.0, 0.0)
    p.offset(0, 1)
    assert p.position == (1.0, 1.0)


def test_region_ctor():
    """ Test Region creation
    """
    apt = Circle((0, 0), 0)
    lines = (
        Line((0, 0), (1, 0), apt),
        Line((1, 0), (1, 1), apt),
        Line((1, 1), (0, 1), apt),
        Line((0, 1), (0, 0), apt),
    )
    points = ((0, 0), (1, 0), (1, 1), (0, 1))
    r = Region(lines)
    for i, p in enumerate(lines):
        assert r.primitives[i] == p


def test_region_bounds():
    """ Test region bounding box calculation
    """
    apt = Circle((0, 0), 0)
    lines = (
        Line((0, 0), (1, 0), apt),
        Line((1, 0), (1, 1), apt),
        Line((1, 1), (0, 1), apt),
        Line((0, 1), (0, 0), apt),
    )
    r = Region(lines)
    bounds = r.bounding_box
    pytest.approx(bounds[0], (0, 0))
    pytest.approx(bounds[1], (1, 1))


def test_region_offset():
    apt = Circle((0, 0), 0)
    lines = (
        Line((0, 0), (1, 0), apt),
        Line((1, 0), (1, 1), apt),
        Line((1, 1), (0, 1), apt),
        Line((0, 1), (0, 0), apt),
    )
    r = Region(lines)
    xlim, ylim = r.bounding_box
    r.offset(0, 1)
    new_xlim, new_ylim = r.bounding_box
    pytest.approx(new_xlim, xlim)
    pytest.approx(new_ylim, tuple([y + 1 for y in ylim]))


def test_drill_ctor():
    """ Test drill primitive creation
    """
    test_cases = (((0, 0), 2), ((1, 1), 3), ((2, 2), 5))
    for position, diameter in test_cases:
        d = Drill(position, diameter)
        assert d.position == position
        assert d.diameter == diameter
        assert d.radius == diameter / 2.0


def test_drill_ctor_validation():
    """ Test drill argument validation
    """
    pytest.raises(TypeError, Drill, 3, 5)
    pytest.raises(TypeError, Drill, (3, 4, 5), 5)


def test_drill_bounds():
    d = Drill((0, 0), 2)
    bounds = d.bounding_box
    pytest.approx(bounds[0], (-1, -1))
    pytest.approx(bounds[1], (1, 1))
    d = Drill((1, 2), 2)
    bounds = d.bounding_box
    pytest.approx(bounds[0], (0, 1))
    pytest.approx(bounds[1], (2, 3))


def test_drill_conversion():
    d = Drill((2.54, 25.4), 254.0, units="metric")

    # No effect
    d.to_metric()
    assert d.position == (2.54, 25.4)
    assert d.diameter == 254.0

    d.to_inch()
    assert d.position == (0.1, 1.0)
    assert d.diameter == 10.0

    # No effect
    d.to_inch()
    assert d.position == (0.1, 1.0)
    assert d.diameter == 10.0

    d = Drill((0.1, 1.0), 10.0, units="inch")

    # No effect
    d.to_inch()
    assert d.position == (0.1, 1.0)
    assert d.diameter == 10.0

    d.to_metric()
    assert d.position == (2.54, 25.4)
    assert d.diameter == 254.0

    # No effect
    d.to_metric()
    assert d.position == (2.54, 25.4)
    assert d.diameter == 254.0


def test_drill_offset():
    d = Drill((0, 0), 1.0)
    d.offset(1, 0)
    assert d.position == (1.0, 0.0)
    d.offset(0, 1)
    assert d.position == (1.0, 1.0)


def test_drill_equality():
    d = Drill((2.54, 25.4), 254.0)
    d1 = Drill((2.54, 25.4), 254.0)
    assert d == d1
    d1 = Drill((2.54, 25.4), 254.2)
    assert d != d1


def test_slot_bounds():
    """ Test Slot primitive bounding box calculation
    """
    cases = [
        (( 0,  0), ( 1,  1), ((-1, -1), (2, 2))),
        ((-1, -1), ( 1,  1), ((-2, -2), (2, 2))),
        (( 1,  1), (-1, -1), ((-2, -2), (2, 2))),
        ((-1,  1), ( 1, -1), ((-2, -2), (2, 2))),
    ]

    for start, end, expected in cases:
        s = Slot(start, end, 2.0)
        assert s.bounding_box == expected