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