epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame^] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 8 | #include "Test.h" |
| 9 | |
reed@android.com | 0650c6c | 2009-03-04 14:02:44 +0000 | [diff] [blame] | 10 | #include "SkCubicClipper.h" |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 11 | #include "SkGeometry.h" |
| 12 | |
| 13 | |
| 14 | static void PrintCurve(const char *name, const SkPoint crv[4]) { |
| 15 | printf("%s: %.10g, %.10g, %.10g, %.10g, %.10g, %.10g, %.10g, %.10g\n", |
| 16 | name, |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 17 | (float)crv[0].fX, (float)crv[0].fY, |
| 18 | (float)crv[1].fX, (float)crv[1].fY, |
| 19 | (float)crv[2].fX, (float)crv[2].fY, |
| 20 | (float)crv[3].fX, (float)crv[3].fY); |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 21 | |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 25 | static bool CurvesAreEqual(const SkPoint c0[4], |
| 26 | const SkPoint c1[4], |
| 27 | float tol) { |
| 28 | for (int i = 0; i < 4; i++) { |
| 29 | if (SkScalarAbs(c0[i].fX - c1[i].fX) > SkFloatToScalar(tol) || |
| 30 | SkScalarAbs(c0[i].fY - c1[i].fY) > SkFloatToScalar(tol) |
| 31 | ) { |
| 32 | PrintCurve("c0", c0); |
| 33 | PrintCurve("c1", c1); |
| 34 | return false; |
| 35 | } |
| 36 | } |
| 37 | return true; |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 41 | static SkPoint* SetCurve(float x0, float y0, |
| 42 | float x1, float y1, |
| 43 | float x2, float y2, |
| 44 | float x3, float y3, |
| 45 | SkPoint crv[4]) { |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 46 | crv[0].fX = SkFloatToScalar(x0); crv[0].fY = SkFloatToScalar(y0); |
| 47 | crv[1].fX = SkFloatToScalar(x1); crv[1].fY = SkFloatToScalar(y1); |
| 48 | crv[2].fX = SkFloatToScalar(x2); crv[2].fY = SkFloatToScalar(y2); |
| 49 | crv[3].fX = SkFloatToScalar(x3); crv[3].fY = SkFloatToScalar(y3); |
| 50 | return crv; |
| 51 | } |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 52 | |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 53 | |
| 54 | static void TestCubicClipping(skiatest::Reporter* reporter) { |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 55 | static SkPoint crv[4] = { |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 56 | { SkIntToScalar(0), SkIntToScalar(0) }, |
| 57 | { SkIntToScalar(2), SkIntToScalar(3) }, |
| 58 | { SkIntToScalar(1), SkIntToScalar(10) }, |
| 59 | { SkIntToScalar(4), SkIntToScalar(12) } |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 60 | }; |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 61 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 62 | SkCubicClipper clipper; |
| 63 | SkPoint clipped[4], shouldbe[4]; |
| 64 | SkIRect clipRect; |
| 65 | bool success; |
reed@android.com | d413445 | 2011-02-09 02:24:26 +0000 | [diff] [blame] | 66 | const float tol = SkFloatToScalar(1e-4); |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 67 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 68 | // Test no clip, with plenty of room. |
| 69 | clipRect.set(-2, -2, 6, 14); |
| 70 | clipper.setClip(clipRect); |
| 71 | success = clipper.clipCubic(crv, clipped); |
| 72 | REPORTER_ASSERT(reporter, success == true); |
| 73 | REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve( |
| 74 | 0, 0, 2, 3, 1, 10, 4, 12, shouldbe), tol)); |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 75 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 76 | // Test no clip, touching first point. |
| 77 | clipRect.set(-2, 0, 6, 14); |
| 78 | clipper.setClip(clipRect); |
| 79 | success = clipper.clipCubic(crv, clipped); |
| 80 | REPORTER_ASSERT(reporter, success == true); |
| 81 | REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve( |
| 82 | 0, 0, 2, 3, 1, 10, 4, 12, shouldbe), tol)); |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 83 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 84 | // Test no clip, touching last point. |
| 85 | clipRect.set(-2, -2, 6, 12); |
| 86 | clipper.setClip(clipRect); |
| 87 | success = clipper.clipCubic(crv, clipped); |
| 88 | REPORTER_ASSERT(reporter, success == true); |
| 89 | REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve( |
| 90 | 0, 0, 2, 3, 1, 10, 4, 12, shouldbe), tol)); |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 91 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 92 | // Test all clip. |
| 93 | clipRect.set(-2, 14, 6, 20); |
| 94 | clipper.setClip(clipRect); |
| 95 | success = clipper.clipCubic(crv, clipped); |
| 96 | REPORTER_ASSERT(reporter, success == false); |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 97 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 98 | // Test clip at 1. |
| 99 | clipRect.set(-2, 1, 6, 14); |
| 100 | clipper.setClip(clipRect); |
| 101 | success = clipper.clipCubic(crv, clipped); |
| 102 | REPORTER_ASSERT(reporter, success == true); |
| 103 | REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve( |
| 104 | 0.5126125216, 1, |
| 105 | 1.841195941, 4.337081432, |
| 106 | 1.297019958, 10.19801331, |
| 107 | 4, 12, |
| 108 | shouldbe), tol)); |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 109 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 110 | // Test clip at 2. |
| 111 | clipRect.set(-2, 2, 6, 14); |
| 112 | clipper.setClip(clipRect); |
| 113 | success = clipper.clipCubic(crv, clipped); |
| 114 | REPORTER_ASSERT(reporter, success == true); |
| 115 | REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve( |
| 116 | 00.8412352204, 2, |
| 117 | 1.767683744, 5.400758266, |
| 118 | 1.55052948, 10.36701965, |
| 119 | 4, 12, |
| 120 | shouldbe), tol)); |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 121 | |
turk@google.com | 7d3a58a | 2009-03-04 01:33:35 +0000 | [diff] [blame] | 122 | // Test clip at 11. |
| 123 | clipRect.set(-2, -2, 6, 11); |
| 124 | clipper.setClip(clipRect); |
| 125 | success = clipper.clipCubic(crv, clipped); |
| 126 | REPORTER_ASSERT(reporter, success == true); |
| 127 | REPORTER_ASSERT(reporter, CurvesAreEqual(clipped, SetCurve( |
| 128 | 0, 0, |
| 129 | 1.742904663, 2.614356995, |
| 130 | 1.207521796, 8.266430855, |
| 131 | 3.026495695, 11, |
| 132 | shouldbe), 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), tol)); |
turk@google.com | 5755a2a | 2009-03-03 02:56:05 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | #include "TestClassDef.h" |
| 151 | DEFINE_TESTCLASS("CubicClipper", CubicClippingTestClass, TestCubicClipping) |