1 #include <iostream>
2 #include "testlib/testlib_test.h"
3 
4 #ifdef _MSC_VER
5 #  include "vcl_msvc_warnings.h"
6 #endif
7 
8 #include "vgl/vgl_triangle_scan_iterator.h"
9 #include "vgl/vgl_polygon_scan_iterator.h"
10 
11 static void
test_triangle_scan_iterator()12 test_triangle_scan_iterator()
13 {
14   std::cout << "Test triangle scan iterator\n";
15 
16   {
17     float x[3] = { 10.23f, 20.12f, 30.73f };
18     float y[3] = { 20.54f, 10.39f, 30.11f };
19 
20     vgl_triangle_scan_iterator<float> ti;
21     ti.a.x = x[0];
22     ti.a.y = y[0];
23     ti.b.x = x[1];
24     ti.b.y = y[1];
25     ti.c.x = x[2];
26     ti.c.y = y[2];
27 
28     vgl_polygon<float> p(x, y, 3);
29     vgl_polygon_scan_iterator<float> pi(p, false);
30 
31     bool failed = false;
32     ti.reset();
33     bool ti_more = ti.next();
34     pi.reset();
35     bool pi_more = pi.next();
36 
37     while (ti_more && pi_more && !failed)
38     {
39       failed = failed || ti.scany() != pi.scany() || ti.startx() != pi.startx() || ti.endx() != pi.endx();
40       ti_more = ti.next();
41       pi_more = pi.next();
42     }
43 
44     // additional empty scan lines are allowed
45     while (ti_more)
46     {
47       failed = failed || ti.startx() <= ti.endx();
48       ti_more = ti.next();
49     }
50 
51     // additional empty scan lines are allowed
52     while (pi_more)
53     {
54       failed = failed || pi.startx() <= pi.endx();
55       pi_more = pi.next();
56     }
57 
58     TEST("Triangle scan iterator == polygon scan iterator", failed, false);
59   }
60 
61   {
62     float x[3] = { 0.5f, 2.5f, 3.1f };
63     float y[3] = { 0.5f, 0.5f, 5.2f };
64     // .XX.   This triangle rasterizes correctly
65     // ..X.   with two connected components!
66     // ..X.
67     // ....  <= this scan line has no pixels
68     // ...X
69     int points[5][2] = { { 1, 1 }, { 2, 1 }, { 2, 2 }, { 2, 3 }, { 3, 5 } };
70 
71     vgl_triangle_scan_iterator<float> ti;
72     ti.a.x = x[0];
73     ti.a.y = y[0];
74     ti.b.x = x[1];
75     ti.b.y = y[1];
76     ti.c.x = x[2];
77     ti.c.y = y[2];
78 
79     bool pass = true;
80     int i = 0;
81     for (ti.reset(); ti.next();)
82     {
83       int y = ti.scany();
84       for (int x = ti.startx(); x <= ti.endx(); ++x, ++i)
85       {
86         pass = pass && (points[i][0] == x) && (points[i][1] == y);
87       }
88     }
89     TEST("triangle with gap", pass, true);
90   }
91 }
92 
93 TESTMAIN(test_triangle_scan_iterator);
94