• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "Test.h"
2 
3 #include "SkCubicClipper.h"
4 #include "SkGeometry.h"
5 
6 
PrintCurve(const char * name,const SkPoint crv[4])7 static void PrintCurve(const char *name, const SkPoint crv[4]) {
8     printf("%s: %.10g, %.10g, %.10g, %.10g, %.10g, %.10g, %.10g, %.10g\n",
9             name,
10             (float)crv[0].fX, (float)crv[0].fY,
11             (float)crv[1].fX, (float)crv[1].fY,
12             (float)crv[2].fX, (float)crv[2].fY,
13             (float)crv[3].fX, (float)crv[3].fY);
14 
15 }
16 
17 
CurvesAreEqual(const SkPoint c0[4],const SkPoint c1[4],float tol)18 static bool CurvesAreEqual(const SkPoint c0[4],
19                            const SkPoint c1[4],
20                            float tol) {
21     for (int i = 0; i < 4; i++) {
22         if (SkScalarAbs(c0[i].fX - c1[i].fX) > SkFloatToScalar(tol) ||
23             SkScalarAbs(c0[i].fY - c1[i].fY) > SkFloatToScalar(tol)
24         ) {
25             PrintCurve("c0", c0);
26             PrintCurve("c1", c1);
27             return false;
28         }
29     }
30     return true;
31 }
32 
33 
SetCurve(float x0,float y0,float x1,float y1,float x2,float y2,float x3,float y3,SkPoint crv[4])34 static SkPoint* SetCurve(float x0, float y0,
35                          float x1, float y1,
36                          float x2, float y2,
37                          float x3, float y3,
38                          SkPoint crv[4]) {
39     crv[0].fX = SkFloatToScalar(x0);   crv[0].fY = SkFloatToScalar(y0);
40     crv[1].fX = SkFloatToScalar(x1);   crv[1].fY = SkFloatToScalar(y1);
41     crv[2].fX = SkFloatToScalar(x2);   crv[2].fY = SkFloatToScalar(y2);
42     crv[3].fX = SkFloatToScalar(x3);   crv[3].fY = SkFloatToScalar(y3);
43     return crv;
44 }
45 
46 
TestCubicClipping(skiatest::Reporter * reporter)47 static void TestCubicClipping(skiatest::Reporter* reporter) {
48     static SkPoint crv[4] = {
49         { SkIntToScalar(0), SkIntToScalar(0)  },
50         { SkIntToScalar(2), SkIntToScalar(3)  },
51         { SkIntToScalar(1), SkIntToScalar(10) },
52         { SkIntToScalar(4), SkIntToScalar(12) }
53     };
54 
55     SkCubicClipper clipper;
56     SkPoint clipped[4], shouldbe[4];
57     SkIRect clipRect;
58     bool success;
59     const float tol = SkFloatToScalar(1e-4);
60 
61     // Test no clip, with plenty of room.
62     clipRect.set(-2, -2, 6, 14);
63     clipper.setClip(clipRect);
64     success = clipper.clipCubic(crv, clipped);
65     REPORTER_ASSERT(reporter, success == true);
66     REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
67         0, 0, 2, 3, 1, 10, 4, 12, shouldbe), tol));
68 
69     // Test no clip, touching first point.
70     clipRect.set(-2, 0, 6, 14);
71     clipper.setClip(clipRect);
72     success = clipper.clipCubic(crv, clipped);
73     REPORTER_ASSERT(reporter, success == true);
74     REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
75         0, 0, 2, 3, 1, 10, 4, 12, shouldbe), tol));
76 
77     // Test no clip, touching last point.
78     clipRect.set(-2, -2, 6, 12);
79     clipper.setClip(clipRect);
80     success = clipper.clipCubic(crv, clipped);
81     REPORTER_ASSERT(reporter, success == true);
82     REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
83         0, 0, 2, 3, 1, 10, 4, 12, shouldbe), tol));
84 
85     // Test all clip.
86     clipRect.set(-2, 14, 6, 20);
87     clipper.setClip(clipRect);
88     success = clipper.clipCubic(crv, clipped);
89     REPORTER_ASSERT(reporter, success == false);
90 
91     // Test clip at 1.
92     clipRect.set(-2, 1, 6, 14);
93     clipper.setClip(clipRect);
94     success = clipper.clipCubic(crv, clipped);
95     REPORTER_ASSERT(reporter, success == true);
96     REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
97         0.5126125216, 1,
98         1.841195941,  4.337081432,
99         1.297019958,  10.19801331,
100         4,            12,
101         shouldbe), tol));
102 
103     // Test clip at 2.
104     clipRect.set(-2, 2, 6, 14);
105     clipper.setClip(clipRect);
106     success = clipper.clipCubic(crv, clipped);
107     REPORTER_ASSERT(reporter, success == true);
108     REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
109         00.8412352204, 2,
110         1.767683744,   5.400758266,
111         1.55052948,    10.36701965,
112         4,             12,
113         shouldbe), tol));
114 
115     // Test clip at 11.
116     clipRect.set(-2, -2, 6, 11);
117     clipper.setClip(clipRect);
118     success = clipper.clipCubic(crv, clipped);
119     REPORTER_ASSERT(reporter, success == true);
120     REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
121         0,           0,
122         1.742904663, 2.614356995,
123         1.207521796, 8.266430855,
124         3.026495695, 11,
125         shouldbe), tol));
126 
127     // Test clip at 10.
128     clipRect.set(-2, -2, 6, 10);
129     clipper.setClip(clipRect);
130     success = clipper.clipCubic(crv, clipped);
131     REPORTER_ASSERT(reporter, success == true);
132     REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve(
133         0,           0,
134         1.551193237, 2.326789856,
135         1.297736168, 7.059780121,
136         2.505550385, 10,
137         shouldbe), tol));
138 }
139 
140 
141 
142 
143 #include "TestClassDef.h"
144 DEFINE_TESTCLASS("CubicClipper", CubicClippingTestClass, TestCubicClipping)
145