OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkGeometry.h" | 8 #include "SkGeometry.h" |
9 #include "Test.h" | 9 #include "Test.h" |
| 10 #include "SkRandom.h" |
10 | 11 |
11 static bool nearly_equal(const SkPoint& a, const SkPoint& b) { | 12 static bool nearly_equal(const SkPoint& a, const SkPoint& b) { |
12 return SkScalarNearlyEqual(a.fX, b.fX) && SkScalarNearlyEqual(a.fY, b.fY); | 13 return SkScalarNearlyEqual(a.fX, b.fX) && SkScalarNearlyEqual(a.fY, b.fY); |
13 } | 14 } |
14 | 15 |
15 static void testChopCubic(skiatest::Reporter* reporter) { | 16 static void testChopCubic(skiatest::Reporter* reporter) { |
16 /* | 17 /* |
17 Inspired by this test, which used to assert that the tValues had dups | 18 Inspired by this test, which used to assert that the tValues had dups |
18 | 19 |
19 <path stroke="#202020" d="M0,0 C0,0 1,1 2190,5130 C2190,5070 2220,5010 2
205,4980" /> | 20 <path stroke="#202020" d="M0,0 C0,0 1,1 2190,5130 C2190,5070 2220,5010 2
205,4980" /> |
20 */ | 21 */ |
21 const SkPoint src[] = { | 22 const SkPoint src[] = { |
22 { SkIntToScalar(2190), SkIntToScalar(5130) }, | 23 { SkIntToScalar(2190), SkIntToScalar(5130) }, |
23 { SkIntToScalar(2190), SkIntToScalar(5070) }, | 24 { SkIntToScalar(2190), SkIntToScalar(5070) }, |
24 { SkIntToScalar(2220), SkIntToScalar(5010) }, | 25 { SkIntToScalar(2220), SkIntToScalar(5010) }, |
25 { SkIntToScalar(2205), SkIntToScalar(4980) }, | 26 { SkIntToScalar(2205), SkIntToScalar(4980) }, |
26 }; | 27 }; |
27 SkPoint dst[13]; | 28 SkPoint dst[13]; |
28 SkScalar tValues[3]; | 29 SkScalar tValues[3]; |
29 // make sure we don't assert internally | 30 // make sure we don't assert internally |
30 int count = SkChopCubicAtMaxCurvature(src, dst, tValues); | 31 int count = SkChopCubicAtMaxCurvature(src, dst, tValues); |
31 if (false) { // avoid bit rot, suppress warning | 32 if (false) { // avoid bit rot, suppress warning |
32 REPORTER_ASSERT(reporter, count); | 33 REPORTER_ASSERT(reporter, count); |
33 } | 34 } |
34 } | 35 } |
35 | 36 |
| 37 static void test_evalquadat(skiatest::Reporter* reporter) { |
| 38 SkRandom rand; |
| 39 for (int i = 0; i < 1000; ++i) { |
| 40 SkPoint pts[3]; |
| 41 for (int j = 0; j < 3; ++j) { |
| 42 pts[j].set(rand.nextSScalar1() * 100, rand.nextSScalar1() * 100); |
| 43 } |
| 44 SkScalar t = 0; |
| 45 const SkScalar dt = SK_Scalar1 / 128; |
| 46 for (int j = 0; j < 128; ++j) { |
| 47 SkPoint r0; |
| 48 SkEvalQuadAt(pts, t, &r0); |
| 49 SkPoint r1 = SkEvalQuadAt(pts, t); |
| 50 bool eq = SkScalarNearlyEqual(r0.fX, r1.fX) && SkScalarNearlyEqual(r
0.fY, r1.fY); |
| 51 if (!eq) { |
| 52 SkDebugf("[%d %g] p0 [%10.8f %10.8f] p1 [%10.8f %10.8f]\n", i, t
, r0.fX, r0.fY, r1.fX, r1.fY); |
| 53 REPORTER_ASSERT(reporter, eq); |
| 54 } |
| 55 t += dt; |
| 56 } |
| 57 } |
| 58 } |
| 59 |
36 DEF_TEST(Geometry, reporter) { | 60 DEF_TEST(Geometry, reporter) { |
37 SkPoint pts[3], dst[5]; | 61 SkPoint pts[3], dst[5]; |
38 | 62 |
39 pts[0].set(0, 0); | 63 pts[0].set(0, 0); |
40 pts[1].set(100, 50); | 64 pts[1].set(100, 50); |
41 pts[2].set(0, 100); | 65 pts[2].set(0, 100); |
42 | 66 |
43 int count = SkChopQuadAtMaxCurvature(pts, dst); | 67 int count = SkChopQuadAtMaxCurvature(pts, dst); |
44 REPORTER_ASSERT(reporter, count == 1 || count == 2); | 68 REPORTER_ASSERT(reporter, count == 1 || count == 2); |
45 | 69 |
46 pts[0].set(0, 0); | 70 pts[0].set(0, 0); |
47 pts[1].set(SkIntToScalar(3), 0); | 71 pts[1].set(SkIntToScalar(3), 0); |
48 pts[2].set(SkIntToScalar(3), SkIntToScalar(3)); | 72 pts[2].set(SkIntToScalar(3), SkIntToScalar(3)); |
49 SkConvertQuadToCubic(pts, dst); | 73 SkConvertQuadToCubic(pts, dst); |
50 const SkPoint cubic[] = { | 74 const SkPoint cubic[] = { |
51 { 0, 0, }, | 75 { 0, 0, }, |
52 { SkIntToScalar(2), 0, }, | 76 { SkIntToScalar(2), 0, }, |
53 { SkIntToScalar(3), SkIntToScalar(1), }, | 77 { SkIntToScalar(3), SkIntToScalar(1), }, |
54 { SkIntToScalar(3), SkIntToScalar(3) }, | 78 { SkIntToScalar(3), SkIntToScalar(3) }, |
55 }; | 79 }; |
56 for (int i = 0; i < 4; ++i) { | 80 for (int i = 0; i < 4; ++i) { |
57 REPORTER_ASSERT(reporter, nearly_equal(cubic[i], dst[i])); | 81 REPORTER_ASSERT(reporter, nearly_equal(cubic[i], dst[i])); |
58 } | 82 } |
59 | 83 |
60 testChopCubic(reporter); | 84 testChopCubic(reporter); |
| 85 test_evalquadat(reporter); |
61 } | 86 } |
OLD | NEW |