epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 Google Inc. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
| 6 | */ |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 7 | |
reed@android.com | d8730ea | 2009-02-27 22:06:06 +0000 | [diff] [blame] | 8 | #include "SkGeometry.h" |
tfarina@chromium.org | 8f6884a | 2014-01-24 20:56:26 +0000 | [diff] [blame] | 9 | #include "Test.h" |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 10 | #include "SkRandom.h" |
reed@android.com | d8730ea | 2009-02-27 22:06:06 +0000 | [diff] [blame] | 11 | |
reed@google.com | 6fc321a | 2011-07-27 13:54:36 +0000 | [diff] [blame] | 12 | static bool nearly_equal(const SkPoint& a, const SkPoint& b) { |
| 13 | return SkScalarNearlyEqual(a.fX, b.fX) && SkScalarNearlyEqual(a.fY, b.fY); |
| 14 | } |
| 15 | |
reed@google.com | 087d5aa | 2012-02-29 20:59:24 +0000 | [diff] [blame] | 16 | static void testChopCubic(skiatest::Reporter* reporter) { |
| 17 | /* |
| 18 | Inspired by this test, which used to assert that the tValues had dups |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 19 | |
reed@google.com | 087d5aa | 2012-02-29 20:59:24 +0000 | [diff] [blame] | 20 | <path stroke="#202020" d="M0,0 C0,0 1,1 2190,5130 C2190,5070 2220,5010 2205,4980" /> |
| 21 | */ |
| 22 | const SkPoint src[] = { |
| 23 | { SkIntToScalar(2190), SkIntToScalar(5130) }, |
| 24 | { SkIntToScalar(2190), SkIntToScalar(5070) }, |
| 25 | { SkIntToScalar(2220), SkIntToScalar(5010) }, |
| 26 | { SkIntToScalar(2205), SkIntToScalar(4980) }, |
| 27 | }; |
| 28 | SkPoint dst[13]; |
| 29 | SkScalar tValues[3]; |
reed@google.com | c256cd1 | 2012-02-29 21:57:36 +0000 | [diff] [blame] | 30 | // make sure we don't assert internally |
reed@google.com | 087d5aa | 2012-02-29 20:59:24 +0000 | [diff] [blame] | 31 | int count = SkChopCubicAtMaxCurvature(src, dst, tValues); |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 32 | if (false) { // avoid bit rot, suppress warning |
| 33 | REPORTER_ASSERT(reporter, count); |
| 34 | } |
reed@google.com | 087d5aa | 2012-02-29 20:59:24 +0000 | [diff] [blame] | 35 | } |
| 36 | |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 37 | static void check_pairs(skiatest::Reporter* reporter, int index, SkScalar t, const char name[], |
| 38 | SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1) { |
| 39 | bool eq = SkScalarNearlyEqual(x0, x1) && SkScalarNearlyEqual(y0, y1); |
| 40 | if (!eq) { |
| 41 | SkDebugf("%s [%d %g] p0 [%10.8f %10.8f] p1 [%10.8f %10.8f]\n", |
| 42 | name, index, t, x0, y0, x1, y1); |
| 43 | REPORTER_ASSERT(reporter, eq); |
| 44 | } |
| 45 | } |
| 46 | |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 47 | static void test_evalquadat(skiatest::Reporter* reporter) { |
| 48 | SkRandom rand; |
| 49 | for (int i = 0; i < 1000; ++i) { |
| 50 | SkPoint pts[3]; |
| 51 | for (int j = 0; j < 3; ++j) { |
| 52 | pts[j].set(rand.nextSScalar1() * 100, rand.nextSScalar1() * 100); |
| 53 | } |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 54 | const SkScalar dt = SK_Scalar1 / 128; |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 55 | SkScalar t = dt; |
| 56 | for (int j = 1; j < 128; ++j) { |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 57 | SkPoint r0; |
| 58 | SkEvalQuadAt(pts, t, &r0); |
| 59 | SkPoint r1 = SkEvalQuadAt(pts, t); |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 60 | check_pairs(reporter, i, t, "quad-pos", r0.fX, r0.fY, r1.fX, r1.fY); |
reed | 6b9ef90 | 2015-03-24 19:24:34 -0700 | [diff] [blame] | 61 | |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 62 | SkVector v0; |
| 63 | SkEvalQuadAt(pts, t, NULL, &v0); |
| 64 | SkVector v1 = SkEvalQuadTangentAt(pts, t); |
| 65 | check_pairs(reporter, i, t, "quad-tan", v0.fX, v0.fY, v1.fX, v1.fY); |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 66 | |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 67 | t += dt; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
reed | b640203 | 2015-03-20 13:23:43 -0700 | [diff] [blame] | 72 | static void test_conic_eval_pos(skiatest::Reporter* reporter, const SkConic& conic, SkScalar t) { |
| 73 | SkPoint p0, p1; |
| 74 | conic.evalAt(t, &p0, NULL); |
| 75 | p1 = conic.evalAt(t); |
| 76 | check_pairs(reporter, 0, t, "conic-pos", p0.fX, p0.fY, p1.fX, p1.fY); |
| 77 | } |
| 78 | |
| 79 | static void test_conic_eval_tan(skiatest::Reporter* reporter, const SkConic& conic, SkScalar t) { |
| 80 | SkVector v0, v1; |
| 81 | conic.evalAt(t, NULL, &v0); |
| 82 | v1 = conic.evalTangentAt(t); |
| 83 | check_pairs(reporter, 0, t, "conic-tan", v0.fX, v0.fY, v1.fX, v1.fY); |
| 84 | } |
| 85 | |
reed | b640203 | 2015-03-20 13:23:43 -0700 | [diff] [blame] | 86 | static void test_conic(skiatest::Reporter* reporter) { |
| 87 | SkRandom rand; |
| 88 | for (int i = 0; i < 1000; ++i) { |
| 89 | SkPoint pts[3]; |
| 90 | for (int j = 0; j < 3; ++j) { |
| 91 | pts[j].set(rand.nextSScalar1() * 100, rand.nextSScalar1() * 100); |
| 92 | } |
| 93 | for (int k = 0; k < 10; ++k) { |
| 94 | SkScalar w = rand.nextUScalar1() * 2; |
| 95 | SkConic conic(pts, w); |
reed | b640203 | 2015-03-20 13:23:43 -0700 | [diff] [blame] | 96 | |
| 97 | const SkScalar dt = SK_Scalar1 / 128; |
| 98 | SkScalar t = dt; |
| 99 | for (int j = 1; j < 128; ++j) { |
| 100 | test_conic_eval_pos(reporter, conic, t); |
| 101 | test_conic_eval_tan(reporter, conic, t); |
| 102 | t += dt; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 108 | DEF_TEST(Geometry, reporter) { |
reed@android.com | d8730ea | 2009-02-27 22:06:06 +0000 | [diff] [blame] | 109 | SkPoint pts[3], dst[5]; |
| 110 | |
| 111 | pts[0].set(0, 0); |
| 112 | pts[1].set(100, 50); |
| 113 | pts[2].set(0, 100); |
| 114 | |
| 115 | int count = SkChopQuadAtMaxCurvature(pts, dst); |
| 116 | REPORTER_ASSERT(reporter, count == 1 || count == 2); |
reed@google.com | 6fc321a | 2011-07-27 13:54:36 +0000 | [diff] [blame] | 117 | |
| 118 | pts[0].set(0, 0); |
reed | daee7ea | 2015-03-26 20:22:33 -0700 | [diff] [blame] | 119 | pts[1].set(3, 0); |
| 120 | pts[2].set(3, 3); |
reed@google.com | 6fc321a | 2011-07-27 13:54:36 +0000 | [diff] [blame] | 121 | SkConvertQuadToCubic(pts, dst); |
| 122 | const SkPoint cubic[] = { |
reed | daee7ea | 2015-03-26 20:22:33 -0700 | [diff] [blame] | 123 | { 0, 0, }, { 2, 0, }, { 3, 1, }, { 3, 3 }, |
reed@google.com | 6fc321a | 2011-07-27 13:54:36 +0000 | [diff] [blame] | 124 | }; |
| 125 | for (int i = 0; i < 4; ++i) { |
| 126 | REPORTER_ASSERT(reporter, nearly_equal(cubic[i], dst[i])); |
| 127 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 128 | |
reed@google.com | 087d5aa | 2012-02-29 20:59:24 +0000 | [diff] [blame] | 129 | testChopCubic(reporter); |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 130 | test_evalquadat(reporter); |
reed | b640203 | 2015-03-20 13:23:43 -0700 | [diff] [blame] | 131 | test_conic(reporter); |
reed@android.com | d8730ea | 2009-02-27 22:06:06 +0000 | [diff] [blame] | 132 | } |