File: stroke_path_test_case.py

package info (click to toggle)
python-enable 4.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,220 kB
  • sloc: cpp: 57,417; python: 28,437; makefile: 314; sh: 43
file content (501 lines) | stat: -rw-r--r-- 18,094 bytes parent folder | download | duplicates (2)
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
"""

    Tasks
    -----
    DONE *. Change all rect classes to use a sequence instead of 4 separate args
    *. Implement a clear_rect that takes a rect argument and a color.
    *. Rename the clear() method to ?erase()? so that clear can stick with
       the clear_rect semantics.
    *. Implement arc.

    *. Implement a clear_rects that takes a rect list and a color.
    *. Implement clipping to multiple rectangles.

    Needed Tests
    ------------

    Coordinate system offset.
        DONE 1. Test that aliased line at zero puts full energy in last
                row of buffer.
        DONE 2. Test that antialiased line at zero puts half energy in
                last row of buffer.

    Aliased
        DONE 1. For simple line, test all paths through rendering pipeline.

    Antialiased
        DONE 1. For simple line, test all paths through rendering pipeline.

    Caps
        DONE 1. Each version same for all paths through aliased code.
        2. Each version same for all paths through antialiased code.

    Joins (in join_stroke_path_test_case.py)
        DONE 1. Each version same for all paths through aliased code.
        DONE 2. Each version same for all paths through antialiased code.

    Dashed Lines
        *. Should be tested for all versions of aliasing and all versions
           of join, cap.

    Curved Lines
        DONE *. curve_to

    Note: There are numerous comments in code that refer to implementation
          details (outline, outline_aa, scanline_aa, etc.) from the C++
          code.  These are largely to help keep track of what paths have
          been tested.
"""

import unittest

from numpy import array, alltrue, ravel

from kiva.agg import GraphicsContextArray
import kiva

from test_utils import Utils


class StrokePathTestCase(unittest.TestCase, Utils):

    def test_alias_width_one(self):
        """ The fastest path through the stroke path code is for aliased
            path with width=1.  It is reasonably safe here not to worry
            with testing all the CAP/JOIN combinations because they are
            all rendered the same for this case.

            It is handled by the agg::rasterizer_outline and the
            agg::renderer_primitives classes in C++.

            Energy for an aliased horizontal line of width=1 falls
            within a single line of pixels.  With y=0 for this line, the
            engine should paint a single row of zeros along the
            bottom edge of the bmp array.
        """
        gc = GraphicsContextArray((3, 2), pix_format="rgb24")

        # clear background to white values (255, 255, 255)
        gc.clear((1.0, 1.0, 1.0))

        # single horizontal line across bottom of buffer
        gc.move_to(0, 0)
        gc.line_to(3, 0)

        # These settings allow the fastest path.
        gc. set_stroke_color((0.0, 0.0, 0.0)) # black
        gc.set_antialias(False)
        gc.set_line_width(1)

        gc.stroke_path()

        # test a single color channel.
        desired = array(((255,255,255),
                         (  0,  0,  0)))
        actual = gc.bmp_array[:,:,0]
        self.assertRavelEqual(actual, desired)

    def test_alias_width_two_outline_aa(self):
        """ When  width>1, alias text is drawn using a couple of different
            paths through the underlying C++ code. This test the faster of the
            two which uses the agg::rasterizer_outline_aa C++ code.  It is only
            used when 2<=width<=10, and cap is ROUND or BUTT, and join is MITER

            The C++ classes used in the underlying C++ code for this is
            agg::rasterizer_outline_aa and agg::renderer_outline_aa
        """
        gc = GraphicsContextArray((3, 2), pix_format="rgb24")

        # clear background to white values (255, 255, 255)
        gc.clear((1.0, 1.0, 1.0))

        # single horizontal line across bottom of buffer
        gc.move_to(0, 0)
        gc.line_to(3, 0)

        # Settings allow the 2nd fastest path.
        gc. set_stroke_color((0.0, 0.0, 0.0)) # black
        gc.set_antialias(False)
        gc.set_line_width(2)
        gc.set_line_cap(kiva.CAP_ROUND)
        gc.set_line_join(kiva.JOIN_MITER)

        gc.stroke_path()

        # test a single color channel.
        desired = array(((255, 255, 255),
                         (  0,   0,   0)))
        actual = gc.bmp_array[:,:,0]
        self.assertRavelEqual(actual, desired)

    def test_alias_width_two_scanline_aa(self):
        """ When width > 1, alias text is drawn using a couple of different
            paths through the underlying C++ code. This test the slower of the
            two which uses the agg::rasterizer_scanline_aa C++ code.  We've set
            the line join to bevel to trigger this path.
        """
        gc = GraphicsContextArray((3, 2), pix_format="rgb24")

        # clear background to white values (255, 255, 255)
        gc.clear((1.0, 1.0, 1.0))

        # single horizontal line across bottom of buffer
        gc.move_to(0, 0)
        gc.line_to(3, 0)

        # Settings allow the 2nd fastest path.
        gc. set_stroke_color((0.0, 0.0, 0.0)) # black
        gc.set_antialias(False)
        gc.set_line_width(2)
        gc.set_line_cap(kiva.CAP_ROUND)
        gc.set_line_join(kiva.JOIN_BEVEL)

        gc.stroke_path()

        # test a single color channel.
        desired = array(((255, 255, 255),
                         (  0,   0,   0)))
        actual = gc.bmp_array[:,:,0]
        self.assertRavelEqual(actual, desired)

    #########################################################################
    # Cap Tests
    #########################################################################

    def cap_equality_helper(self, antialias, width, line_cap, line_join,
                                 size=(6,6)):

        gc = GraphicsContextArray(size, pix_format="rgb24")

        # clear background to white values (255, 255, 255)
        gc.clear((1.0, 1.0, 1.0))

        # single horizontal line across bottom of buffer
        gc.move_to(2, 3)
        gc.line_to(4, 3)

        # Settings allow the faster outline path through C++ code
        gc. set_stroke_color((0.0, 0.0, 0.0)) # black
        gc.set_antialias(antialias)
        gc.set_line_width(width)
        gc.set_line_cap(line_cap)
        gc.set_line_join(line_join)

        gc.stroke_path()
        return gc

    def test_alias_cap_round(self):
        """ Round caps should extend beyond the end of the line.  We
            don't really test the shape here.  To do this, a test of
            a wider line would be needed.

            fix me: This is rendering antialiased end points currently.
        """
        gc = GraphicsContextArray((6,6), pix_format="rgb24")

        # clear background to white values (255, 255, 255)
        gc.clear((1.0, 1.0, 1.0))

        # single horizontal line across bottom of buffer
        gc.move_to(2, 3)
        gc.line_to(4, 3)

        # Set up line
        gc. set_stroke_color((0.0, 0.0, 0.0)) # black
        gc.set_antialias(False)
        gc.set_line_width(2)
        gc.set_line_cap(kiva.CAP_ROUND)
        gc.set_line_join(kiva.JOIN_MITER)

        gc.stroke_path()

        desired = array(((255, 255, 255, 255, 255, 255),
                         (255, 255, 255, 255, 255, 255),
                         (255,   0,   0,   0,   0, 255),
                         (255,   0,   0,   0,   0, 255),
                         (255, 255, 255, 255, 255, 255),
                         (255, 255, 255, 255, 255, 255)))

        actual = gc.bmp_array[:,:,0]
        self.assertRavelEqual(desired, actual)

    def test_alias_cap_round_equality(self):
        """ There are two paths that can generate aliased round capped lines.
            This tests that they generate equivalent results

            fix me: This test fails because the two rendering paths
                    render the end caps differently.
        """
        antialias = False
        width = 2
        cap = kiva.CAP_ROUND
        # join=miter allows the faster outline path through C++ code.
        gc1 = self.cap_equality_helper(antialias, width, cap, kiva.JOIN_MITER)

        # join=bevel forces the scanline path through C++ code.
        gc2 = self.cap_equality_helper(antialias, width, cap, kiva.JOIN_BEVEL)

        # Instead of testing against a known desired value, we are simply
        # testing for equality...
        self.assertRavelEqual(gc1.bmp_array[:,:,0], gc2.bmp_array[:,:,0])

    def test_alias_cap_square(self):
        """ Round caps should extend beyond the end of the line. by
            half the width of the line.

            fix me: This is rendering antialiased end points currently.
        """
        gc = GraphicsContextArray((6,6), pix_format="rgb24")

        # clear background to white values (255, 255, 255)
        gc.clear((1.0, 1.0, 1.0))

        # single horizontal line across bottom of buffer
        gc.move_to(2, 3)
        gc.line_to(4, 3)

        # Set up line
        gc. set_stroke_color((0.0, 0.0, 0.0)) # black
        gc.set_antialias(False)
        gc.set_line_width(2)
        gc.set_line_cap(kiva.CAP_SQUARE)
        gc.set_line_join(kiva.JOIN_MITER)

        gc.stroke_path()

        desired = array(((255, 255, 255, 255, 255, 255),
                         (255, 255, 255, 255, 255, 255),
                         (255, 255,   0,   0, 255, 255),
                         (255, 255,   0,   0, 255, 255),
                         (255, 255, 255, 255, 255, 255),
                         (255, 255, 255, 255, 255, 255)))

        actual = gc.bmp_array[:,:,0]
        self.assertRavelEqual(desired, actual)

    def test_alias_cap_square(self):
        """ Square caps should extend beyond the end of the line. by
            half the width of the line.
        """
        gc = GraphicsContextArray((6,6), pix_format="rgb24")

        # clear background to white values (255, 255, 255)
        gc.clear((1.0, 1.0, 1.0))

        # single horizontal line across bottom of buffer
        gc.move_to(2, 3)
        gc.line_to(4, 3)

        # Set up line
        gc. set_stroke_color((0.0, 0.0, 0.0)) # black
        gc.set_antialias(False)
        gc.set_line_width(2)
        gc.set_line_cap(kiva.CAP_SQUARE)
        gc.set_line_join(kiva.JOIN_MITER)

        gc.stroke_path()

        desired = array(((255, 255, 255, 255, 255, 255),
                         (255, 255, 255, 255, 255, 255),
                         (255,   0,   0,   0,   0, 255),
                         (255,   0,   0,   0,   0, 255),
                         (255, 255, 255, 255, 255, 255),
                         (255, 255, 255, 255, 255, 255)))

        actual = gc.bmp_array[:,:,0]
        self.assertRavelEqual(desired, actual)

    def test_alias_cap_butt_equality(self):
        """ There are two paths that can generate aliased butt capped lines.
            This tests that they generate equivalent results
        """
        antialias = False
        width = 2
        cap = kiva.CAP_BUTT
        # join=miter allows the faster outline path through C++ code.
        gc1 = self.cap_equality_helper(antialias, width, cap, kiva.JOIN_MITER)

        # join=bevel forces the scanline path through C++ code.
        gc2 = self.cap_equality_helper(antialias, width, cap, kiva.JOIN_BEVEL)

        # Instead of testing against a known desired value, we are simply
        # testing for equality...
        self.assertRavelEqual(gc1.bmp_array, gc2.bmp_array)

    def test_alias_cap_square(self):
        """ Square caps should extend beyond the end of the line. by
            half the width of the line.
        """
        gc = GraphicsContextArray((6,6), pix_format="rgb24")

        # clear background to white values (255, 255, 255)
        gc.clear((1.0, 1.0, 1.0))

        # single horizontal line across bottom of buffer
        gc.move_to(2, 3)
        gc.line_to(4, 3)

        # Set up line
        gc. set_stroke_color((0.0, 0.0, 0.0)) # black
        gc.set_antialias(False)
        gc.set_line_width(2)
        gc.set_line_cap(kiva.CAP_SQUARE)
        gc.set_line_join(kiva.JOIN_MITER)

        gc.stroke_path()

        desired = array(((255, 255, 255, 255, 255, 255),
                         (255, 255, 255, 255, 255, 255),
                         (255,   0,   0,   0,   0, 255),
                         (255,   0,   0,   0,   0, 255),
                         (255, 255, 255, 255, 255, 255),
                         (255, 255, 255, 255, 255, 255)))

        actual = gc.bmp_array[:,:,0]
        self.assertRavelEqual(desired, actual)

    def test_alias_cap_square_equality(self):
        """ There are two paths that can generate aliased square capped lines.
            This tests that they generate equivalent results.
        """
        antialias = False
        width = 2
        cap = kiva.CAP_SQUARE
        # join=miter allows the faster outline path through C++ code.
        gc1 = self.cap_equality_helper(antialias, width, cap, kiva.JOIN_MITER)

        # join=bevel forces the scanline path through C++ code.
        gc2 = self.cap_equality_helper(antialias, width, cap, kiva.JOIN_BEVEL)

        # Instead of testing against a known desired value, we are simply
        # testing for equality...
        self.assertRavelEqual(gc1.bmp_array, gc2.bmp_array)

    def test_antialias_width_one(self):
        """ An anti-aliased horizontal line of width=1 has its energy
            centered between the bottom row of pixels and the next lower
            row of pixels (which is off the page).  It dumps half
            its energy in each, so we end up with a single line of
            127,127,127 pixel values in the last row of pixels.

            This particular set of flags is handled by the (fast)
            agg::rasterizer_outline_aa path through the C++ code.

        """
        gc = GraphicsContextArray((3, 2), pix_format="rgb24")

        # clear background to white values (255, 255, 255)
        gc.clear((1.0, 1.0, 1.0))

        # single horizontal line across bottom of buffer
        gc.move_to(0, 0)
        gc.line_to(3, 0)

        # Set up stroke
        gc. set_stroke_color((0.0, 0.0, 0.0)) # black
        gc.set_antialias(True)
        gc.set_line_width(1)
        gc.set_line_cap(kiva.CAP_BUTT)
        gc.set_line_join(kiva.JOIN_MITER)

        gc.stroke_path()

        # test a single color channel.
        desired = array(((255, 255, 255),
                         (127, 127, 127)))
        actual = gc.bmp_array[:,:,0]
        self.assertRavelEqual(desired, actual)

    def test_antialias_width_slower_path(self):
        """ An anti-aliased horizontal line of width=1 has its energy
            centered between the bottom row of pixels and the next lower
            row of pixels (which is off the page).  It dumps half
            its energy in each, so we end up with a single line of
            127,127,127 pixel values in the last row of pixels.

            This particular set of flags is handled by the
            agg::rasterizer_scanline_aa path through the C++ code.

        """
        gc = GraphicsContextArray((3, 2), pix_format="rgb24")

        # clear background to white values (255, 255, 255)
        gc.clear((1.0, 1.0, 1.0))

        # single horizontal line across bottom of buffer
        gc.move_to(0, 0)
        gc.line_to(3, 0)

        # Set up stroke
        gc. set_stroke_color((0.0, 0.0, 0.0)) # black
        gc.set_antialias(True)
        gc.set_line_width(1)
        gc.set_line_cap(kiva.CAP_BUTT)
        gc.set_line_join(kiva.JOIN_BEVEL)

        gc.stroke_path()

        # test a single color channel.
        desired = array(((255, 255, 255),
                         (127, 127, 127)))
        actual = gc.bmp_array[:,:,0]
        self.assertRavelEqual(desired, actual)

    def test_curve_to(self):
        """ curve_to

            conv_curve happens early in the agg rendering pipeline,
            so it isn't neccessary to test every combination of
            antialias, line_cap, line_join, etc.  If it works for
            one, we should be in good shape for the others (until
            the implementation is changed of course...)

        """
        gc = GraphicsContextArray((10, 10), pix_format="rgb24")

        # clear background to white values (255, 255, 255)
        gc.clear((1.0, 1.0, 1.0))

        # single horizontal line across bottom of buffer
        x0, y0 = 1.0, 5.0
        x1, y1 = 4.0, 9.0
        x2, y2 = 6.0, 1.0
        x3, y3 = 9.0, 5.0
        gc.move_to(x0, y0)
        gc.curve_to(x1, y1, x2, y2, x3, y3);

        # Set up stroke
        gc. set_stroke_color((0.0, 0.0, 0.0)) # black
        gc.set_antialias(True)
        gc.set_line_width(1)
        gc.set_line_cap(kiva.CAP_BUTT)
        gc.set_line_join(kiva.JOIN_MITER)

        gc.stroke_path()

        gc. set_stroke_color((0.0, 1.0, 1.0))
        gc.move_to(x0, y0)
        gc.line_to(x1, y1)
        gc.move_to(x2, y2)
        gc.line_to(x3, y3)

        gc.stroke_path()
        # test a single color channel.
        # note: This is a "screen capture" from running this
        #       test.  It looks right, but hasn't been check closely.
        desired = array([[255, 255, 255, 230, 255, 255, 255, 255, 255, 255],
                         [255, 255, 231,  25, 212, 255, 255, 255, 255, 255],
                         [255, 252,  65, 128, 255, 255, 255, 255, 255, 255],
                         [255, 103,  26, 143, 229, 255, 255, 255, 255, 255],
                         [179,   2, 115,  96,  23, 189, 255, 255, 204, 255],
                         [255, 205, 255, 255, 189,  23,  97, 116,   2, 179],
                         [255, 255, 255, 255, 255, 229, 142,  25, 103, 255],
                         [255, 255, 255, 255, 255, 255, 127,  66, 252, 255],
                         [255, 255, 255, 255, 255, 212,  26, 231, 255, 255],
                         [255, 255, 255, 255, 255, 255, 231, 255, 255, 255]])
        actual = gc.bmp_array[:,:,0]
        self.assertRavelEqual(desired, actual)


if __name__ == "__main__":
    unittest.main()