turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 1 | #include "Test.h" |
| 2 | |
reed@android.com | 0650c6c | 2009-03-04 14:02:44 +0000 | [diff] [blame] | 3 | #include "SkCubicClipper.h" |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 4 | #include "SkGeometry.h" |
| 5 | |
| 6 | |
| 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 | crv[0].fX, crv[0].fY, |
| 11 | crv[1].fX, crv[1].fY, |
| 12 | crv[2].fX, crv[2].fY, |
| 13 | crv[3].fX, crv[3].fY); |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 14 | |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 15 | } |
| 16 | |
| 17 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 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; |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 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]) { |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 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 | } |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 45 | |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 46 | |
| 47 | static void TestCubicClipping(skiatest::Reporter* reporter) { |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 48 | static SkPoint crv[4] = { |
| 49 | { SkFloatToScalar(0), SkFloatToScalar(0) }, |
| 50 | { SkFloatToScalar(2), SkFloatToScalar(3) }, |
| 51 | { SkFloatToScalar(1), SkFloatToScalar(10) }, |
| 52 | { SkFloatToScalar(4), SkFloatToScalar(12) } |
| 53 | }; |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 54 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 55 | SkCubicClipper clipper; |
| 56 | SkPoint clipped[4], shouldbe[4]; |
| 57 | SkIRect clipRect; |
| 58 | bool success; |
| 59 | const float tol = 1e-4; |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 60 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 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)); |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 68 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 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)); |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 76 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 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)); |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 84 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 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); |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 90 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 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)); |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 102 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 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)); |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 114 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 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)); |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | #include "TestClassDef.h" |
| 144 | DEFINE_TESTCLASS("CubicClipper", CubicClippingTestClass, TestCubicClipping) |