blob: 931b61e09c1320f9852e380b8dfd5d6e73a104d7 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com5755a2a2009-03-03 02:56:05 +00008#include "Test.h"
9
reed@android.com0650c6c2009-03-04 14:02:44 +000010#include "SkCubicClipper.h"
turk@google.com5755a2a2009-03-03 02:56:05 +000011#include "SkGeometry.h"
12
13
14static 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.comd4134452011-02-09 02:24:26 +000017 (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.com7d3a58a2009-03-04 01:33:35 +000021
turk@google.com5755a2a2009-03-03 02:56:05 +000022}
23
24
turk@google.com7d3a58a2009-03-04 01:33:35 +000025static 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.com5755a2a2009-03-03 02:56:05 +000038}
39
40
turk@google.com7d3a58a2009-03-04 01:33:35 +000041static 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.com5755a2a2009-03-03 02:56:05 +000046 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.com7d3a58a2009-03-04 01:33:35 +000052
turk@google.com5755a2a2009-03-03 02:56:05 +000053
54static void TestCubicClipping(skiatest::Reporter* reporter) {
turk@google.com7d3a58a2009-03-04 01:33:35 +000055 static SkPoint crv[4] = {
reed@android.comd4134452011-02-09 02:24:26 +000056 { SkIntToScalar(0), SkIntToScalar(0) },
57 { SkIntToScalar(2), SkIntToScalar(3) },
58 { SkIntToScalar(1), SkIntToScalar(10) },
59 { SkIntToScalar(4), SkIntToScalar(12) }
turk@google.com7d3a58a2009-03-04 01:33:35 +000060 };
turk@google.com5755a2a2009-03-03 02:56:05 +000061
turk@google.com7d3a58a2009-03-04 01:33:35 +000062 SkCubicClipper clipper;
63 SkPoint clipped[4], shouldbe[4];
64 SkIRect clipRect;
65 bool success;
reed@android.comd4134452011-02-09 02:24:26 +000066 const float tol = SkFloatToScalar(1e-4);
turk@google.com5755a2a2009-03-03 02:56:05 +000067
turk@google.com7d3a58a2009-03-04 01:33:35 +000068 // 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.com5755a2a2009-03-03 02:56:05 +000075
turk@google.com7d3a58a2009-03-04 01:33:35 +000076 // 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.com5755a2a2009-03-03 02:56:05 +000083
turk@google.com7d3a58a2009-03-04 01:33:35 +000084 // 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.com5755a2a2009-03-03 02:56:05 +000091
turk@google.com7d3a58a2009-03-04 01:33:35 +000092 // 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.com5755a2a2009-03-03 02:56:05 +000097
turk@google.com7d3a58a2009-03-04 01:33:35 +000098 // 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.com5755a2a2009-03-03 02:56:05 +0000109
turk@google.com7d3a58a2009-03-04 01:33:35 +0000110 // 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.com5755a2a2009-03-03 02:56:05 +0000121
turk@google.com7d3a58a2009-03-04 01:33:35 +0000122 // 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.com5755a2a2009-03-03 02:56:05 +0000145}
146
147
148
149
150#include "TestClassDef.h"
151DEFINE_TESTCLASS("CubicClipper", CubicClippingTestClass, TestCubicClipping)