summaryrefslogtreecommitdiff
path: root/gerber/tests/test_primitives.py
blob: f8b8620db1f819bfc55268f21a577ebba8251875 (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
#! /usr/bin/env python
# -*- coding: utf-8 -*-

# Author: Hamilton Kibbe <ham@hamiltonkib.be>
from ..primitives import *
from tests import *


def test_primitive_implementation_warning():
    p = Primitive()
    assert_raises(NotImplementedError, p.bounding_box)


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)
        assert_almost_equal(line_angle, expected)

def test_line_bounds():
    """ Test Line primitive bounding box calculation
    """
    cases = [((0, 0), (1, 1), ((-1, 2), (-1, 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_equal(l.bounding_box, expected)
    # Test a non-square rectangle
    r = Rectangle((0, 0), 3, 2)
    cases = [((0, 0), (1, 1), ((-1.5, 2.5), (-1, 2))),
             ((-1, -1), (1, 1), ((-2.5, 2.5), (-2, 2))),
             ((1, 1), (-1, -1), ((-2.5, 2.5), (-2, 2))),
             ((-1, 1), (1, -1), ((-2.5, 2.5), (-2, 2))),]
    for start, end, expected in cases:
        l = Line(start, end, r)
        assert_equal(l.bounding_box, expected)



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)
        assert_equal(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)
        assert_equal(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, 1.5), (-0.5, 1.5))),
             #TODO: ADD MORE TEST CASES HERE
             ]
    for start, end, center, direction, bounds in cases:
        c = Circle((0,0), 1)
        a = Arc(start, end, center, direction, c)
        assert_equal(a.bounding_box, bounds)


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


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


def test_ellipse_ctor():
    """ Test ellipse creation
    """
    e = Ellipse((2, 2), 3, 2)
    assert_equal(e.position, (2, 2))
    assert_equal(e.width, 3)
    assert_equal(e.height, 2)


def test_ellipse_bounds():
    """ Test ellipse bounding box calculation
    """
    e = Ellipse((2, 2), 4, 2)
    assert_equal(e.bounding_box, ((0, 4), (1, 3)))
    e = Ellipse((2, 2), 4, 2, rotation=90)
    assert_equal(e.bounding_box, ((1, 3), (0, 4)))
    e = Ellipse((2, 2), 4, 2, rotation=180)
    assert_equal(e.bounding_box, ((0, 4), (1, 3)))
    e = Ellipse((2, 2), 4, 2, rotation=270)
    assert_equal(e.bounding_box, ((1, 3), (0, 4)))

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_equal(r.position, pos)
        assert_equal(r.width, width)
        assert_equal(r.height, height)

def test_rectangle_bounds():
    """ Test rectangle bounding box calculation
    """
    r = Rectangle((0,0), 2, 2)
    xbounds, ybounds = r.bounding_box
    assert_array_almost_equal(xbounds, (-1, 1))
    assert_array_almost_equal(ybounds, (-1, 1))
    r = Rectangle((0,0), 2, 2, rotation=45)
    xbounds, ybounds = r.bounding_box
    assert_array_almost_equal(xbounds, (-math.sqrt(2), math.sqrt(2)))
    assert_array_almost_equal(ybounds, (-math.sqrt(2), math.sqrt(2)))
    
def test_diamond_ctor():
    """ Test diamond creation
    """
    test_cases = (((0,0), 1, 1), ((0, 0), 1, 2), ((1,1), 1, 2))
    for pos, width, height in test_cases:
        d = Diamond(pos, width, height)
        assert_equal(d.position, pos)
        assert_equal(d.width, width)
        assert_equal(d.height, height)

def test_diamond_bounds():
    """ Test diamond bounding box calculation
    """
    d = Diamond((0,0), 2, 2)
    xbounds, ybounds = d.bounding_box
    assert_array_almost_equal(xbounds, (-1, 1))
    assert_array_almost_equal(ybounds, (-1, 1))
    d = Diamond((0,0), math.sqrt(2), math.sqrt(2), rotation=45)
    xbounds, ybounds = d.bounding_box
    assert_array_almost_equal(xbounds, (-1, 1))
    assert_array_almost_equal(ybounds, (-1, 1))


def test_chamfer_rectangle_ctor():
    """ Test chamfer 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, chamfer, corners in test_cases:
        r = ChamferRectangle(pos, width, height, chamfer, corners)
        assert_equal(r.position, pos)
        assert_equal(r.width, width)
        assert_equal(r.height, height)
        assert_equal(r.chamfer, chamfer)
        assert_array_almost_equal(r.corners, corners)


def test_chamfer_rectangle_bounds():
    """ Test chamfer rectangle bounding box calculation
    """
    r = ChamferRectangle((0,0), 2, 2, 0.2, (True, True, False, False))
    xbounds, ybounds = r.bounding_box
    assert_array_almost_equal(xbounds, (-1, 1))
    assert_array_almost_equal(ybounds, (-1, 1))
    r = ChamferRectangle((0,0), 2, 2, 0.2, (True, True, False, False), rotation=45)
    xbounds, ybounds = r.bounding_box
    assert_array_almost_equal(xbounds, (-math.sqrt(2), math.sqrt(2)))
    assert_array_almost_equal(ybounds, (-math.sqrt(2), math.sqrt(2)))


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_equal(r.position, pos)
        assert_equal(r.width, width)
        assert_equal(r.height, height)
        assert_equal(r.radius, radius)
        assert_array_almost_equal(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))
    xbounds, ybounds = r.bounding_box
    assert_array_almost_equal(xbounds, (-1, 1))
    assert_array_almost_equal(ybounds, (-1, 1))
    r = RoundRectangle((0,0), 2, 2, 0.2, (True, True, False, False), rotation=45)
    xbounds, ybounds = r.bounding_box
    assert_array_almost_equal(xbounds, (-math.sqrt(2), math.sqrt(2)))
    assert_array_almost_equal(ybounds, (-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_equal(o.position, pos)
        assert_equal(o.width, width)
        assert_equal(o.height, height)


def test_obround_bounds():
    """ Test obround bounding box calculation
    """
    o = Obround((2,2),2,4)
    xbounds, ybounds = o.bounding_box
    assert_array_almost_equal(xbounds, (1, 3))
    assert_array_almost_equal(ybounds, (0, 4))
    o = Obround((2,2),4,2)
    xbounds, ybounds = o.bounding_box
    assert_array_almost_equal(xbounds, (0, 4))
    assert_array_almost_equal(ybounds, (1, 3))


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


def test_obround_subshapes():
    o = Obround((0,0), 1, 4)
    ss = o.subshapes
    assert_array_almost_equal(ss['rectangle'].position, (0, 0))
    assert_array_almost_equal(ss['circle1'].position, (0, 1.5))
    assert_array_almost_equal(ss['circle2'].position, (0, -1.5))
    o = Obround((0,0), 4, 1)
    ss = o.subshapes
    assert_array_almost_equal(ss['rectangle'].position, (0, 0))
    assert_array_almost_equal(ss['circle1'].position, (1.5, 0))
    assert_array_almost_equal(ss['circle2'].position, (-1.5, 0))
    
def test_polygon_ctor():
    """ Test polygon creation
    """
    test_cases = (((0,0), 3, 5),
                  ((0, 0), 5, 6),
                  ((1,1), 7, 7))
    for pos, sides, radius in test_cases:
        p = Polygon(pos, sides, radius)
        assert_equal(p.position, pos)
        assert_equal(p.sides, sides)
        assert_equal(p.radius, radius)
        
def test_polygon_bounds():
    """ Test polygon bounding box calculation
    """
    p = Polygon((2,2), 3, 2)
    xbounds, ybounds = p.bounding_box
    assert_array_almost_equal(xbounds, (0, 4))
    assert_array_almost_equal(ybounds, (0, 4))
    p = Polygon((2,2),3, 4)
    xbounds, ybounds = p.bounding_box
    assert_array_almost_equal(xbounds, (-2, 6))
    assert_array_almost_equal(ybounds, (-2, 6))


def test_region_ctor():
    """ Test Region creation
    """
    points = ((0, 0), (1,0), (1,1), (0,1))
    r = Region(points)
    for i, point in enumerate(points):
        assert_array_almost_equal(r.points[i], point)


def test_region_bounds():
    """ Test region bounding box calculation
    """
    points = ((0, 0), (1,0), (1,1), (0,1))
    r = Region(points)
    xbounds, ybounds = r.bounding_box
    assert_array_almost_equal(xbounds, (0, 1))
    assert_array_almost_equal(ybounds, (0, 1))
    
    
def test_round_butterfly_ctor():
    """ Test round butterfly creation
    """
    test_cases = (((0,0), 3), ((0, 0), 5), ((1,1), 7))
    for pos, diameter in test_cases:
        b = RoundButterfly(pos, diameter)
        assert_equal(b.position, pos)
        assert_equal(b.diameter, diameter)
        assert_equal(b.radius, diameter/2.)

def test_round_butterfly_ctor_validation():
    """ Test RoundButterfly argument validation
    """
    assert_raises(TypeError, RoundButterfly, 3, 5)
    assert_raises(TypeError, RoundButterfly, (3,4,5), 5)

def test_round_butterfly_bounds():
    """ Test RoundButterfly bounding box calculation
    """
    b = RoundButterfly((0, 0), 2)
    xbounds, ybounds = b.bounding_box
    assert_array_almost_equal(xbounds, (-1, 1))
    assert_array_almost_equal(ybounds, (-1, 1))
    
def test_square_butterfly_ctor():
    """ Test SquareButterfly creation
    """
    test_cases = (((0,0), 3), ((0, 0), 5), ((1,1), 7))
    for pos, side in test_cases:
        b = SquareButterfly(pos, side)
        assert_equal(b.position, pos)
        assert_equal(b.side, side)

def test_square_butterfly_ctor_validation():
    """ Test SquareButterfly argument validation
    """
    assert_raises(TypeError, SquareButterfly, 3, 5)
    assert_raises(TypeError, SquareButterfly, (3,4,5), 5)


def test_square_butterfly_bounds():
    """ Test SquareButterfly bounding box calculation
    """
    b = SquareButterfly((0, 0), 2)
    xbounds, ybounds = b.bounding_box
    assert_array_almost_equal(xbounds, (-1, 1))
    assert_array_almost_equal(ybounds, (-1, 1))

def test_donut_ctor():
    """ Test Donut primitive creation
    """
    test_cases = (((0,0), 'round', 3, 5), ((0, 0), 'square', 5, 7),
                  ((1,1), 'hexagon', 7, 9), ((2, 2), 'octagon', 9, 11))
    for pos, shape, in_d, out_d in test_cases:
        d = Donut(pos, shape, in_d, out_d)
        assert_equal(d.position, pos)
        assert_equal(d.shape, shape)
        assert_equal(d.inner_diameter, in_d)
        assert_equal(d.outer_diameter, out_d)

def test_donut_ctor_validation():
    assert_raises(TypeError, Donut, 3, 'round', 5, 7)
    assert_raises(TypeError, Donut, (3, 4, 5), 'round', 5, 7)
    assert_raises(ValueError, Donut, (0, 0), 'triangle', 3, 5)
    assert_raises(ValueError, Donut, (0, 0), 'round', 5, 3)
    
def test_donut_bounds():
    pass

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_equal(d.position, position)
        assert_equal(d.diameter, diameter)
        assert_equal(d.radius, diameter/2.)
    
def test_drill_ctor_validation():
    """ Test drill argument validation
    """
    assert_raises(TypeError, Drill, 3, 5)
    assert_raises(TypeError, Drill, (3,4,5), 5)
    
def test_drill_bounds():
    d = Drill((0, 0), 2)
    xbounds, ybounds = d.bounding_box
    assert_array_almost_equal(xbounds, (-1, 1))
    assert_array_almost_equal(ybounds, (-1, 1))
    d = Drill((1, 2), 2)
    xbounds, ybounds = d.bounding_box
    assert_array_almost_equal(xbounds, (0, 2))
    assert_array_almost_equal(ybounds, (1, 3))