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" |
Chris Dalton | 91982ee | 2017-07-14 14:04:52 -0600 | [diff] [blame] | 11 | #include <array> |
reed@android.com | d8730ea | 2009-02-27 22:06:06 +0000 | [diff] [blame] | 12 | |
reed@google.com | 6fc321a | 2011-07-27 13:54:36 +0000 | [diff] [blame] | 13 | static bool nearly_equal(const SkPoint& a, const SkPoint& b) { |
| 14 | return SkScalarNearlyEqual(a.fX, b.fX) && SkScalarNearlyEqual(a.fY, b.fY); |
| 15 | } |
| 16 | |
reed@google.com | 087d5aa | 2012-02-29 20:59:24 +0000 | [diff] [blame] | 17 | static void testChopCubic(skiatest::Reporter* reporter) { |
| 18 | /* |
| 19 | 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] | 20 | |
reed@google.com | 087d5aa | 2012-02-29 20:59:24 +0000 | [diff] [blame] | 21 | <path stroke="#202020" d="M0,0 C0,0 1,1 2190,5130 C2190,5070 2220,5010 2205,4980" /> |
| 22 | */ |
| 23 | const SkPoint src[] = { |
| 24 | { SkIntToScalar(2190), SkIntToScalar(5130) }, |
| 25 | { SkIntToScalar(2190), SkIntToScalar(5070) }, |
| 26 | { SkIntToScalar(2220), SkIntToScalar(5010) }, |
| 27 | { SkIntToScalar(2205), SkIntToScalar(4980) }, |
| 28 | }; |
| 29 | SkPoint dst[13]; |
| 30 | SkScalar tValues[3]; |
reed@google.com | c256cd1 | 2012-02-29 21:57:36 +0000 | [diff] [blame] | 31 | // make sure we don't assert internally |
reed@google.com | 087d5aa | 2012-02-29 20:59:24 +0000 | [diff] [blame] | 32 | int count = SkChopCubicAtMaxCurvature(src, dst, tValues); |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 33 | if (false) { // avoid bit rot, suppress warning |
| 34 | REPORTER_ASSERT(reporter, count); |
| 35 | } |
reed@google.com | 087d5aa | 2012-02-29 20:59:24 +0000 | [diff] [blame] | 36 | } |
| 37 | |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 38 | static void check_pairs(skiatest::Reporter* reporter, int index, SkScalar t, const char name[], |
| 39 | SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1) { |
| 40 | bool eq = SkScalarNearlyEqual(x0, x1) && SkScalarNearlyEqual(y0, y1); |
| 41 | if (!eq) { |
| 42 | SkDebugf("%s [%d %g] p0 [%10.8f %10.8f] p1 [%10.8f %10.8f]\n", |
| 43 | name, index, t, x0, y0, x1, y1); |
| 44 | REPORTER_ASSERT(reporter, eq); |
| 45 | } |
| 46 | } |
| 47 | |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 48 | static void test_evalquadat(skiatest::Reporter* reporter) { |
| 49 | SkRandom rand; |
| 50 | for (int i = 0; i < 1000; ++i) { |
| 51 | SkPoint pts[3]; |
| 52 | for (int j = 0; j < 3; ++j) { |
| 53 | pts[j].set(rand.nextSScalar1() * 100, rand.nextSScalar1() * 100); |
| 54 | } |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 55 | const SkScalar dt = SK_Scalar1 / 128; |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 56 | SkScalar t = dt; |
| 57 | for (int j = 1; j < 128; ++j) { |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 58 | SkPoint r0; |
| 59 | SkEvalQuadAt(pts, t, &r0); |
| 60 | SkPoint r1 = SkEvalQuadAt(pts, t); |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 61 | check_pairs(reporter, i, t, "quad-pos", r0.fX, r0.fY, r1.fX, r1.fY); |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 62 | |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 63 | SkVector v0; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 64 | SkEvalQuadAt(pts, t, nullptr, &v0); |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 65 | SkVector v1 = SkEvalQuadTangentAt(pts, t); |
| 66 | 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] | 67 | |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 68 | t += dt; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
reed | b640203 | 2015-03-20 13:23:43 -0700 | [diff] [blame] | 73 | static void test_conic_eval_pos(skiatest::Reporter* reporter, const SkConic& conic, SkScalar t) { |
| 74 | SkPoint p0, p1; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 75 | conic.evalAt(t, &p0, nullptr); |
reed | b640203 | 2015-03-20 13:23:43 -0700 | [diff] [blame] | 76 | p1 = conic.evalAt(t); |
| 77 | check_pairs(reporter, 0, t, "conic-pos", p0.fX, p0.fY, p1.fX, p1.fY); |
| 78 | } |
| 79 | |
| 80 | static void test_conic_eval_tan(skiatest::Reporter* reporter, const SkConic& conic, SkScalar t) { |
| 81 | SkVector v0, v1; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 82 | conic.evalAt(t, nullptr, &v0); |
reed | b640203 | 2015-03-20 13:23:43 -0700 | [diff] [blame] | 83 | v1 = conic.evalTangentAt(t); |
| 84 | check_pairs(reporter, 0, t, "conic-tan", v0.fX, v0.fY, v1.fX, v1.fY); |
| 85 | } |
| 86 | |
reed | b640203 | 2015-03-20 13:23:43 -0700 | [diff] [blame] | 87 | static void test_conic(skiatest::Reporter* reporter) { |
| 88 | SkRandom rand; |
| 89 | for (int i = 0; i < 1000; ++i) { |
| 90 | SkPoint pts[3]; |
| 91 | for (int j = 0; j < 3; ++j) { |
| 92 | pts[j].set(rand.nextSScalar1() * 100, rand.nextSScalar1() * 100); |
| 93 | } |
| 94 | for (int k = 0; k < 10; ++k) { |
| 95 | SkScalar w = rand.nextUScalar1() * 2; |
| 96 | SkConic conic(pts, w); |
reed | b640203 | 2015-03-20 13:23:43 -0700 | [diff] [blame] | 97 | |
| 98 | const SkScalar dt = SK_Scalar1 / 128; |
| 99 | SkScalar t = dt; |
| 100 | for (int j = 1; j < 128; ++j) { |
| 101 | test_conic_eval_pos(reporter, conic, t); |
| 102 | test_conic_eval_tan(reporter, conic, t); |
| 103 | t += dt; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
caryclark | 45398df | 2015-08-25 13:19:06 -0700 | [diff] [blame] | 109 | static void test_quad_tangents(skiatest::Reporter* reporter) { |
| 110 | SkPoint pts[] = { |
| 111 | {10, 20}, {10, 20}, {20, 30}, |
| 112 | {10, 20}, {15, 25}, {20, 30}, |
| 113 | {10, 20}, {20, 30}, {20, 30}, |
| 114 | }; |
| 115 | int count = (int) SK_ARRAY_COUNT(pts) / 3; |
| 116 | for (int index = 0; index < count; ++index) { |
| 117 | SkConic conic(&pts[index * 3], 0.707f); |
| 118 | SkVector start = SkEvalQuadTangentAt(&pts[index * 3], 0); |
| 119 | SkVector mid = SkEvalQuadTangentAt(&pts[index * 3], .5f); |
| 120 | SkVector end = SkEvalQuadTangentAt(&pts[index * 3], 1); |
| 121 | REPORTER_ASSERT(reporter, start.fX && start.fY); |
| 122 | REPORTER_ASSERT(reporter, mid.fX && mid.fY); |
| 123 | REPORTER_ASSERT(reporter, end.fX && end.fY); |
| 124 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(start.cross(mid))); |
| 125 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(mid.cross(end))); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | static void test_conic_tangents(skiatest::Reporter* reporter) { |
| 130 | SkPoint pts[] = { |
| 131 | { 10, 20}, {10, 20}, {20, 30}, |
| 132 | { 10, 20}, {15, 25}, {20, 30}, |
| 133 | { 10, 20}, {20, 30}, {20, 30} |
| 134 | }; |
| 135 | int count = (int) SK_ARRAY_COUNT(pts) / 3; |
| 136 | for (int index = 0; index < count; ++index) { |
| 137 | SkConic conic(&pts[index * 3], 0.707f); |
| 138 | SkVector start = conic.evalTangentAt(0); |
| 139 | SkVector mid = conic.evalTangentAt(.5f); |
| 140 | SkVector end = conic.evalTangentAt(1); |
| 141 | REPORTER_ASSERT(reporter, start.fX && start.fY); |
| 142 | REPORTER_ASSERT(reporter, mid.fX && mid.fY); |
| 143 | REPORTER_ASSERT(reporter, end.fX && end.fY); |
| 144 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(start.cross(mid))); |
| 145 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(mid.cross(end))); |
| 146 | } |
| 147 | } |
| 148 | |
reed | b1b12f8 | 2016-07-13 10:56:53 -0700 | [diff] [blame] | 149 | static void test_this_conic_to_quad(skiatest::Reporter* r, const SkPoint pts[3], SkScalar w) { |
| 150 | SkAutoConicToQuads quadder; |
| 151 | const SkPoint* qpts = quadder.computeQuads(pts, w, 0.25); |
| 152 | const int qcount = quadder.countQuads(); |
| 153 | const int pcount = qcount * 2 + 1; |
| 154 | |
| 155 | REPORTER_ASSERT(r, SkPointsAreFinite(qpts, pcount)); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * We need to ensure that when a conic is approximated by quads, that we always return finite |
| 160 | * values in the quads. |
| 161 | * |
| 162 | * Inspired by crbug_627414 |
| 163 | */ |
| 164 | static void test_conic_to_quads(skiatest::Reporter* reporter) { |
| 165 | const SkPoint triples[] = { |
| 166 | { 0, 0 }, { 1, 0 }, { 1, 1 }, |
msarett | 16ef465 | 2016-07-13 13:08:44 -0700 | [diff] [blame] | 167 | { 0, 0 }, { 3.58732e-43f, 2.72084f }, { 3.00392f, 3.00392f }, |
reed | b1b12f8 | 2016-07-13 10:56:53 -0700 | [diff] [blame] | 168 | { 0, 0 }, { 100000, 0 }, { 100000, 100000 }, |
| 169 | { 0, 0 }, { 1e30f, 0 }, { 1e30f, 1e30f }, |
| 170 | }; |
| 171 | const int N = sizeof(triples) / sizeof(SkPoint); |
| 172 | |
| 173 | for (int i = 0; i < N; i += 3) { |
| 174 | const SkPoint* pts = &triples[i]; |
| 175 | |
| 176 | SkRect bounds; |
| 177 | bounds.set(pts, 3); |
| 178 | |
| 179 | SkScalar w = 1e30f; |
| 180 | do { |
| 181 | w *= 2; |
| 182 | test_this_conic_to_quad(reporter, pts, w); |
| 183 | } while (SkScalarIsFinite(w)); |
| 184 | test_this_conic_to_quad(reporter, pts, SK_ScalarNaN); |
| 185 | } |
| 186 | } |
| 187 | |
caryclark | 45398df | 2015-08-25 13:19:06 -0700 | [diff] [blame] | 188 | static void test_cubic_tangents(skiatest::Reporter* reporter) { |
| 189 | SkPoint pts[] = { |
| 190 | { 10, 20}, {10, 20}, {20, 30}, {30, 40}, |
| 191 | { 10, 20}, {15, 25}, {20, 30}, {30, 40}, |
| 192 | { 10, 20}, {20, 30}, {30, 40}, {30, 40}, |
| 193 | }; |
| 194 | int count = (int) SK_ARRAY_COUNT(pts) / 4; |
| 195 | for (int index = 0; index < count; ++index) { |
| 196 | SkConic conic(&pts[index * 3], 0.707f); |
| 197 | SkVector start, mid, end; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 198 | SkEvalCubicAt(&pts[index * 4], 0, nullptr, &start, nullptr); |
| 199 | SkEvalCubicAt(&pts[index * 4], .5f, nullptr, &mid, nullptr); |
| 200 | SkEvalCubicAt(&pts[index * 4], 1, nullptr, &end, nullptr); |
caryclark | 45398df | 2015-08-25 13:19:06 -0700 | [diff] [blame] | 201 | REPORTER_ASSERT(reporter, start.fX && start.fY); |
| 202 | REPORTER_ASSERT(reporter, mid.fX && mid.fY); |
| 203 | REPORTER_ASSERT(reporter, end.fX && end.fY); |
| 204 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(start.cross(mid))); |
| 205 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(mid.cross(end))); |
| 206 | } |
| 207 | } |
| 208 | |
Chris Dalton | 91982ee | 2017-07-14 14:04:52 -0600 | [diff] [blame] | 209 | static void check_cubic_type(skiatest::Reporter* reporter, |
| 210 | const std::array<SkPoint, 4>& bezierPoints, SkCubicType expectedType) { |
| 211 | SkCubicType actualType = SkClassifyCubic(bezierPoints.data()); |
| 212 | REPORTER_ASSERT(reporter, actualType == expectedType); |
| 213 | } |
| 214 | |
| 215 | static void test_classify_cubic(skiatest::Reporter* reporter) { |
| 216 | check_cubic_type(reporter, {{{149.325f, 107.705f}, {149.325f, 103.783f}, |
| 217 | {151.638f, 100.127f}, {156.263f, 96.736f}}}, |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 218 | SkCubicType::kSerpentine); |
Chris Dalton | 91982ee | 2017-07-14 14:04:52 -0600 | [diff] [blame] | 219 | check_cubic_type(reporter, {{{225.694f, 223.15f}, {209.831f, 224.837f}, |
| 220 | {195.994f, 230.237f}, {184.181f, 239.35f}}}, |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 221 | SkCubicType::kSerpentine); |
Chris Dalton | 91982ee | 2017-07-14 14:04:52 -0600 | [diff] [blame] | 222 | check_cubic_type(reporter, {{{4.873f, 5.581f}, {5.083f, 5.2783f}, |
| 223 | {5.182f, 4.8593f}, {5.177f, 4.3242f}}}, |
| 224 | SkCubicType::kSerpentine); |
| 225 | } |
| 226 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 227 | DEF_TEST(Geometry, reporter) { |
reed@android.com | d8730ea | 2009-02-27 22:06:06 +0000 | [diff] [blame] | 228 | SkPoint pts[3], dst[5]; |
| 229 | |
| 230 | pts[0].set(0, 0); |
| 231 | pts[1].set(100, 50); |
| 232 | pts[2].set(0, 100); |
| 233 | |
| 234 | int count = SkChopQuadAtMaxCurvature(pts, dst); |
| 235 | REPORTER_ASSERT(reporter, count == 1 || count == 2); |
reed@google.com | 6fc321a | 2011-07-27 13:54:36 +0000 | [diff] [blame] | 236 | |
| 237 | pts[0].set(0, 0); |
reed | daee7ea | 2015-03-26 20:22:33 -0700 | [diff] [blame] | 238 | pts[1].set(3, 0); |
| 239 | pts[2].set(3, 3); |
reed@google.com | 6fc321a | 2011-07-27 13:54:36 +0000 | [diff] [blame] | 240 | SkConvertQuadToCubic(pts, dst); |
| 241 | const SkPoint cubic[] = { |
reed | daee7ea | 2015-03-26 20:22:33 -0700 | [diff] [blame] | 242 | { 0, 0, }, { 2, 0, }, { 3, 1, }, { 3, 3 }, |
reed@google.com | 6fc321a | 2011-07-27 13:54:36 +0000 | [diff] [blame] | 243 | }; |
| 244 | for (int i = 0; i < 4; ++i) { |
| 245 | REPORTER_ASSERT(reporter, nearly_equal(cubic[i], dst[i])); |
| 246 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 247 | |
reed@google.com | 087d5aa | 2012-02-29 20:59:24 +0000 | [diff] [blame] | 248 | testChopCubic(reporter); |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 249 | test_evalquadat(reporter); |
reed | b640203 | 2015-03-20 13:23:43 -0700 | [diff] [blame] | 250 | test_conic(reporter); |
caryclark | 45398df | 2015-08-25 13:19:06 -0700 | [diff] [blame] | 251 | test_cubic_tangents(reporter); |
| 252 | test_quad_tangents(reporter); |
| 253 | test_conic_tangents(reporter); |
reed | b1b12f8 | 2016-07-13 10:56:53 -0700 | [diff] [blame] | 254 | test_conic_to_quads(reporter); |
Chris Dalton | 91982ee | 2017-07-14 14:04:52 -0600 | [diff] [blame] | 255 | test_classify_cubic(reporter); |
reed@android.com | d8730ea | 2009-02-27 22:06:06 +0000 | [diff] [blame] | 256 | } |