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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/utils/SkRandom.h" |
| 9 | #include "src/core/SkGeometry.h" |
| 10 | #include "src/core/SkPointPriv.h" |
| 11 | #include "tests/Test.h" |
Hal Canary | 8a00144 | 2018-09-19 11:31:27 -0400 | [diff] [blame] | 12 | |
Chris Dalton | 91982ee | 2017-07-14 14:04:52 -0600 | [diff] [blame] | 13 | #include <array> |
Chris Dalton | fc31be4 | 2017-11-08 17:04:47 -0700 | [diff] [blame] | 14 | #include <numeric> |
reed@android.com | d8730ea | 2009-02-27 22:06:06 +0000 | [diff] [blame] | 15 | |
reed@google.com | 6fc321a | 2011-07-27 13:54:36 +0000 | [diff] [blame] | 16 | static bool nearly_equal(const SkPoint& a, const SkPoint& b) { |
| 17 | return SkScalarNearlyEqual(a.fX, b.fX) && SkScalarNearlyEqual(a.fY, b.fY); |
| 18 | } |
| 19 | |
reed@google.com | 087d5aa | 2012-02-29 20:59:24 +0000 | [diff] [blame] | 20 | static void testChopCubic(skiatest::Reporter* reporter) { |
| 21 | /* |
| 22 | 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] | 23 | |
reed@google.com | 087d5aa | 2012-02-29 20:59:24 +0000 | [diff] [blame] | 24 | <path stroke="#202020" d="M0,0 C0,0 1,1 2190,5130 C2190,5070 2220,5010 2205,4980" /> |
| 25 | */ |
| 26 | const SkPoint src[] = { |
| 27 | { SkIntToScalar(2190), SkIntToScalar(5130) }, |
| 28 | { SkIntToScalar(2190), SkIntToScalar(5070) }, |
| 29 | { SkIntToScalar(2220), SkIntToScalar(5010) }, |
| 30 | { SkIntToScalar(2205), SkIntToScalar(4980) }, |
| 31 | }; |
| 32 | SkPoint dst[13]; |
| 33 | SkScalar tValues[3]; |
reed@google.com | c256cd1 | 2012-02-29 21:57:36 +0000 | [diff] [blame] | 34 | // make sure we don't assert internally |
reed@google.com | 087d5aa | 2012-02-29 20:59:24 +0000 | [diff] [blame] | 35 | int count = SkChopCubicAtMaxCurvature(src, dst, tValues); |
caryclark@google.com | 42639cd | 2012-06-06 12:03:39 +0000 | [diff] [blame] | 36 | if (false) { // avoid bit rot, suppress warning |
| 37 | REPORTER_ASSERT(reporter, count); |
| 38 | } |
Chris Dalton | 1208e0f | 2018-08-13 00:20:33 -0600 | [diff] [blame] | 39 | // Make sure src and dst can be the same pointer. |
| 40 | SkPoint pts[7]; |
| 41 | for (int i = 0; i < 7; ++i) { |
| 42 | pts[i].set(i, i); |
| 43 | } |
| 44 | SkChopCubicAt(pts, pts, .5f); |
| 45 | for (int i = 0; i < 7; ++i) { |
| 46 | REPORTER_ASSERT(reporter, pts[i].fX == pts[i].fY); |
| 47 | REPORTER_ASSERT(reporter, pts[i].fX == i * .5f); |
| 48 | } |
reed@google.com | 087d5aa | 2012-02-29 20:59:24 +0000 | [diff] [blame] | 49 | } |
| 50 | |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 51 | static void check_pairs(skiatest::Reporter* reporter, int index, SkScalar t, const char name[], |
| 52 | SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1) { |
| 53 | bool eq = SkScalarNearlyEqual(x0, x1) && SkScalarNearlyEqual(y0, y1); |
| 54 | if (!eq) { |
| 55 | SkDebugf("%s [%d %g] p0 [%10.8f %10.8f] p1 [%10.8f %10.8f]\n", |
| 56 | name, index, t, x0, y0, x1, y1); |
| 57 | REPORTER_ASSERT(reporter, eq); |
| 58 | } |
| 59 | } |
| 60 | |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 61 | static void test_evalquadat(skiatest::Reporter* reporter) { |
| 62 | SkRandom rand; |
| 63 | for (int i = 0; i < 1000; ++i) { |
| 64 | SkPoint pts[3]; |
| 65 | for (int j = 0; j < 3; ++j) { |
| 66 | pts[j].set(rand.nextSScalar1() * 100, rand.nextSScalar1() * 100); |
| 67 | } |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 68 | const SkScalar dt = SK_Scalar1 / 128; |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 69 | SkScalar t = dt; |
| 70 | for (int j = 1; j < 128; ++j) { |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 71 | SkPoint r0; |
| 72 | SkEvalQuadAt(pts, t, &r0); |
| 73 | SkPoint r1 = SkEvalQuadAt(pts, t); |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 74 | 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] | 75 | |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 76 | SkVector v0; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 77 | SkEvalQuadAt(pts, t, nullptr, &v0); |
reed | 40b7dd5 | 2015-03-20 06:01:08 -0700 | [diff] [blame] | 78 | SkVector v1 = SkEvalQuadTangentAt(pts, t); |
| 79 | 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] | 80 | |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 81 | t += dt; |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
reed | b640203 | 2015-03-20 13:23:43 -0700 | [diff] [blame] | 86 | static void test_conic_eval_pos(skiatest::Reporter* reporter, const SkConic& conic, SkScalar t) { |
| 87 | SkPoint p0, p1; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 88 | conic.evalAt(t, &p0, nullptr); |
reed | b640203 | 2015-03-20 13:23:43 -0700 | [diff] [blame] | 89 | p1 = conic.evalAt(t); |
| 90 | check_pairs(reporter, 0, t, "conic-pos", p0.fX, p0.fY, p1.fX, p1.fY); |
| 91 | } |
| 92 | |
| 93 | static void test_conic_eval_tan(skiatest::Reporter* reporter, const SkConic& conic, SkScalar t) { |
| 94 | SkVector v0, v1; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 95 | conic.evalAt(t, nullptr, &v0); |
reed | b640203 | 2015-03-20 13:23:43 -0700 | [diff] [blame] | 96 | v1 = conic.evalTangentAt(t); |
| 97 | check_pairs(reporter, 0, t, "conic-tan", v0.fX, v0.fY, v1.fX, v1.fY); |
| 98 | } |
| 99 | |
reed | b640203 | 2015-03-20 13:23:43 -0700 | [diff] [blame] | 100 | static void test_conic(skiatest::Reporter* reporter) { |
| 101 | SkRandom rand; |
| 102 | for (int i = 0; i < 1000; ++i) { |
| 103 | SkPoint pts[3]; |
| 104 | for (int j = 0; j < 3; ++j) { |
| 105 | pts[j].set(rand.nextSScalar1() * 100, rand.nextSScalar1() * 100); |
| 106 | } |
| 107 | for (int k = 0; k < 10; ++k) { |
| 108 | SkScalar w = rand.nextUScalar1() * 2; |
| 109 | SkConic conic(pts, w); |
reed | b640203 | 2015-03-20 13:23:43 -0700 | [diff] [blame] | 110 | |
| 111 | const SkScalar dt = SK_Scalar1 / 128; |
| 112 | SkScalar t = dt; |
| 113 | for (int j = 1; j < 128; ++j) { |
| 114 | test_conic_eval_pos(reporter, conic, t); |
| 115 | test_conic_eval_tan(reporter, conic, t); |
| 116 | t += dt; |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
caryclark | 45398df | 2015-08-25 13:19:06 -0700 | [diff] [blame] | 122 | static void test_quad_tangents(skiatest::Reporter* reporter) { |
| 123 | SkPoint pts[] = { |
| 124 | {10, 20}, {10, 20}, {20, 30}, |
| 125 | {10, 20}, {15, 25}, {20, 30}, |
| 126 | {10, 20}, {20, 30}, {20, 30}, |
| 127 | }; |
| 128 | int count = (int) SK_ARRAY_COUNT(pts) / 3; |
| 129 | for (int index = 0; index < count; ++index) { |
| 130 | SkConic conic(&pts[index * 3], 0.707f); |
| 131 | SkVector start = SkEvalQuadTangentAt(&pts[index * 3], 0); |
| 132 | SkVector mid = SkEvalQuadTangentAt(&pts[index * 3], .5f); |
| 133 | SkVector end = SkEvalQuadTangentAt(&pts[index * 3], 1); |
| 134 | REPORTER_ASSERT(reporter, start.fX && start.fY); |
| 135 | REPORTER_ASSERT(reporter, mid.fX && mid.fY); |
| 136 | REPORTER_ASSERT(reporter, end.fX && end.fY); |
| 137 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(start.cross(mid))); |
| 138 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(mid.cross(end))); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | static void test_conic_tangents(skiatest::Reporter* reporter) { |
| 143 | SkPoint pts[] = { |
| 144 | { 10, 20}, {10, 20}, {20, 30}, |
| 145 | { 10, 20}, {15, 25}, {20, 30}, |
| 146 | { 10, 20}, {20, 30}, {20, 30} |
| 147 | }; |
| 148 | int count = (int) SK_ARRAY_COUNT(pts) / 3; |
| 149 | for (int index = 0; index < count; ++index) { |
| 150 | SkConic conic(&pts[index * 3], 0.707f); |
| 151 | SkVector start = conic.evalTangentAt(0); |
| 152 | SkVector mid = conic.evalTangentAt(.5f); |
| 153 | SkVector end = conic.evalTangentAt(1); |
| 154 | REPORTER_ASSERT(reporter, start.fX && start.fY); |
| 155 | REPORTER_ASSERT(reporter, mid.fX && mid.fY); |
| 156 | REPORTER_ASSERT(reporter, end.fX && end.fY); |
| 157 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(start.cross(mid))); |
| 158 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(mid.cross(end))); |
| 159 | } |
| 160 | } |
| 161 | |
reed | b1b12f8 | 2016-07-13 10:56:53 -0700 | [diff] [blame] | 162 | static void test_this_conic_to_quad(skiatest::Reporter* r, const SkPoint pts[3], SkScalar w) { |
| 163 | SkAutoConicToQuads quadder; |
| 164 | const SkPoint* qpts = quadder.computeQuads(pts, w, 0.25); |
| 165 | const int qcount = quadder.countQuads(); |
| 166 | const int pcount = qcount * 2 + 1; |
| 167 | |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 168 | REPORTER_ASSERT(r, SkPointPriv::AreFinite(qpts, pcount)); |
reed | b1b12f8 | 2016-07-13 10:56:53 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /** |
| 172 | * We need to ensure that when a conic is approximated by quads, that we always return finite |
| 173 | * values in the quads. |
| 174 | * |
| 175 | * Inspired by crbug_627414 |
| 176 | */ |
| 177 | static void test_conic_to_quads(skiatest::Reporter* reporter) { |
| 178 | const SkPoint triples[] = { |
| 179 | { 0, 0 }, { 1, 0 }, { 1, 1 }, |
msarett | 16ef465 | 2016-07-13 13:08:44 -0700 | [diff] [blame] | 180 | { 0, 0 }, { 3.58732e-43f, 2.72084f }, { 3.00392f, 3.00392f }, |
reed | b1b12f8 | 2016-07-13 10:56:53 -0700 | [diff] [blame] | 181 | { 0, 0 }, { 100000, 0 }, { 100000, 100000 }, |
| 182 | { 0, 0 }, { 1e30f, 0 }, { 1e30f, 1e30f }, |
| 183 | }; |
| 184 | const int N = sizeof(triples) / sizeof(SkPoint); |
| 185 | |
| 186 | for (int i = 0; i < N; i += 3) { |
| 187 | const SkPoint* pts = &triples[i]; |
| 188 | |
reed | b1b12f8 | 2016-07-13 10:56:53 -0700 | [diff] [blame] | 189 | SkScalar w = 1e30f; |
| 190 | do { |
| 191 | w *= 2; |
| 192 | test_this_conic_to_quad(reporter, pts, w); |
| 193 | } while (SkScalarIsFinite(w)); |
| 194 | test_this_conic_to_quad(reporter, pts, SK_ScalarNaN); |
| 195 | } |
| 196 | } |
| 197 | |
caryclark | 45398df | 2015-08-25 13:19:06 -0700 | [diff] [blame] | 198 | static void test_cubic_tangents(skiatest::Reporter* reporter) { |
| 199 | SkPoint pts[] = { |
| 200 | { 10, 20}, {10, 20}, {20, 30}, {30, 40}, |
| 201 | { 10, 20}, {15, 25}, {20, 30}, {30, 40}, |
| 202 | { 10, 20}, {20, 30}, {30, 40}, {30, 40}, |
| 203 | }; |
| 204 | int count = (int) SK_ARRAY_COUNT(pts) / 4; |
| 205 | for (int index = 0; index < count; ++index) { |
| 206 | SkConic conic(&pts[index * 3], 0.707f); |
| 207 | SkVector start, mid, end; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 208 | SkEvalCubicAt(&pts[index * 4], 0, nullptr, &start, nullptr); |
| 209 | SkEvalCubicAt(&pts[index * 4], .5f, nullptr, &mid, nullptr); |
| 210 | SkEvalCubicAt(&pts[index * 4], 1, nullptr, &end, nullptr); |
caryclark | 45398df | 2015-08-25 13:19:06 -0700 | [diff] [blame] | 211 | REPORTER_ASSERT(reporter, start.fX && start.fY); |
| 212 | REPORTER_ASSERT(reporter, mid.fX && mid.fY); |
| 213 | REPORTER_ASSERT(reporter, end.fX && end.fY); |
| 214 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(start.cross(mid))); |
| 215 | REPORTER_ASSERT(reporter, SkScalarNearlyZero(mid.cross(end))); |
| 216 | } |
| 217 | } |
| 218 | |
Chris Dalton | 91982ee | 2017-07-14 14:04:52 -0600 | [diff] [blame] | 219 | static void check_cubic_type(skiatest::Reporter* reporter, |
Chris Dalton | fc31be4 | 2017-11-08 17:04:47 -0700 | [diff] [blame] | 220 | const std::array<SkPoint, 4>& bezierPoints, SkCubicType expectedType, |
| 221 | bool undefined = false) { |
| 222 | // Classify the cubic even if the results will be undefined: check for crashes and asserts. |
Chris Dalton | 91982ee | 2017-07-14 14:04:52 -0600 | [diff] [blame] | 223 | SkCubicType actualType = SkClassifyCubic(bezierPoints.data()); |
Chris Dalton | fc31be4 | 2017-11-08 17:04:47 -0700 | [diff] [blame] | 224 | if (!undefined) { |
| 225 | REPORTER_ASSERT(reporter, actualType == expectedType); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | static void check_cubic_around_rect(skiatest::Reporter* reporter, |
| 230 | float x1, float y1, float x2, float y2, |
| 231 | bool undefined = false) { |
| 232 | static constexpr SkCubicType expectations[24] = { |
| 233 | SkCubicType::kLoop, |
| 234 | SkCubicType::kCuspAtInfinity, |
| 235 | SkCubicType::kLocalCusp, |
| 236 | SkCubicType::kLocalCusp, |
| 237 | SkCubicType::kCuspAtInfinity, |
| 238 | SkCubicType::kLoop, |
| 239 | SkCubicType::kCuspAtInfinity, |
| 240 | SkCubicType::kLoop, |
| 241 | SkCubicType::kCuspAtInfinity, |
| 242 | SkCubicType::kLoop, |
| 243 | SkCubicType::kLocalCusp, |
| 244 | SkCubicType::kLocalCusp, |
| 245 | SkCubicType::kLocalCusp, |
| 246 | SkCubicType::kLocalCusp, |
| 247 | SkCubicType::kLoop, |
| 248 | SkCubicType::kCuspAtInfinity, |
| 249 | SkCubicType::kLoop, |
| 250 | SkCubicType::kCuspAtInfinity, |
| 251 | SkCubicType::kLoop, |
| 252 | SkCubicType::kCuspAtInfinity, |
| 253 | SkCubicType::kLocalCusp, |
| 254 | SkCubicType::kLocalCusp, |
| 255 | SkCubicType::kCuspAtInfinity, |
| 256 | SkCubicType::kLoop, |
| 257 | }; |
| 258 | SkPoint points[] = {{x1, y1}, {x2, y1}, {x2, y2}, {x1, y2}}; |
| 259 | std::array<SkPoint, 4> bezier; |
| 260 | for (int i=0; i < 4; ++i) { |
| 261 | bezier[0] = points[i]; |
| 262 | for (int j=0; j < 3; ++j) { |
| 263 | int jidx = (j < i) ? j : j+1; |
| 264 | bezier[1] = points[jidx]; |
| 265 | for (int k=0, kidx=0; k < 2; ++k, ++kidx) { |
| 266 | for (int n = 0; n < 2; ++n) { |
| 267 | kidx = (kidx == i || kidx == jidx) ? kidx+1 : kidx; |
| 268 | } |
| 269 | bezier[2] = points[kidx]; |
| 270 | for (int l = 0; l < 4; ++l) { |
| 271 | if (l != i && l != jidx && l != kidx) { |
| 272 | bezier[3] = points[l]; |
| 273 | break; |
| 274 | } |
| 275 | } |
| 276 | check_cubic_type(reporter, bezier, expectations[i*6 + j*2 + k], undefined); |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | for (int i=0; i < 4; ++i) { |
| 281 | bezier[0] = points[i]; |
| 282 | for (int j=0; j < 3; ++j) { |
| 283 | int jidx = (j < i) ? j : j+1; |
| 284 | bezier[1] = points[jidx]; |
| 285 | bezier[2] = points[jidx]; |
| 286 | for (int k=0, kidx=0; k < 2; ++k, ++kidx) { |
| 287 | for (int n = 0; n < 2; ++n) { |
| 288 | kidx = (kidx == i || kidx == jidx) ? kidx+1 : kidx; |
| 289 | } |
| 290 | bezier[3] = points[kidx]; |
| 291 | check_cubic_type(reporter, bezier, SkCubicType::kSerpentine, undefined); |
| 292 | } |
| 293 | } |
| 294 | } |
Chris Dalton | 91982ee | 2017-07-14 14:04:52 -0600 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | static void test_classify_cubic(skiatest::Reporter* reporter) { |
| 298 | check_cubic_type(reporter, {{{149.325f, 107.705f}, {149.325f, 103.783f}, |
| 299 | {151.638f, 100.127f}, {156.263f, 96.736f}}}, |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 300 | SkCubicType::kSerpentine); |
Chris Dalton | 91982ee | 2017-07-14 14:04:52 -0600 | [diff] [blame] | 301 | check_cubic_type(reporter, {{{225.694f, 223.15f}, {209.831f, 224.837f}, |
| 302 | {195.994f, 230.237f}, {184.181f, 239.35f}}}, |
Chris Dalton | 29011a2 | 2017-09-28 12:08:33 -0600 | [diff] [blame] | 303 | SkCubicType::kSerpentine); |
Chris Dalton | 91982ee | 2017-07-14 14:04:52 -0600 | [diff] [blame] | 304 | check_cubic_type(reporter, {{{4.873f, 5.581f}, {5.083f, 5.2783f}, |
| 305 | {5.182f, 4.8593f}, {5.177f, 4.3242f}}}, |
| 306 | SkCubicType::kSerpentine); |
Chris Dalton | fc31be4 | 2017-11-08 17:04:47 -0700 | [diff] [blame] | 307 | check_cubic_around_rect(reporter, 0, 0, 1, 1); |
| 308 | check_cubic_around_rect(reporter, |
| 309 | -std::numeric_limits<float>::max(), |
| 310 | -std::numeric_limits<float>::max(), |
| 311 | +std::numeric_limits<float>::max(), |
| 312 | +std::numeric_limits<float>::max()); |
| 313 | check_cubic_around_rect(reporter, 1, 1, |
| 314 | +std::numeric_limits<float>::min(), |
| 315 | +std::numeric_limits<float>::max()); |
| 316 | check_cubic_around_rect(reporter, |
| 317 | -std::numeric_limits<float>::min(), |
| 318 | -std::numeric_limits<float>::min(), |
| 319 | +std::numeric_limits<float>::min(), |
| 320 | +std::numeric_limits<float>::min()); |
| 321 | check_cubic_around_rect(reporter, +1, -std::numeric_limits<float>::min(), -1, -1); |
| 322 | check_cubic_around_rect(reporter, |
| 323 | -std::numeric_limits<float>::infinity(), |
| 324 | -std::numeric_limits<float>::infinity(), |
| 325 | +std::numeric_limits<float>::infinity(), |
| 326 | +std::numeric_limits<float>::infinity(), |
| 327 | true); |
| 328 | check_cubic_around_rect(reporter, 0, 0, 1, +std::numeric_limits<float>::infinity(), true); |
| 329 | check_cubic_around_rect(reporter, |
| 330 | -std::numeric_limits<float>::quiet_NaN(), |
| 331 | -std::numeric_limits<float>::quiet_NaN(), |
| 332 | +std::numeric_limits<float>::quiet_NaN(), |
| 333 | +std::numeric_limits<float>::quiet_NaN(), |
| 334 | true); |
| 335 | check_cubic_around_rect(reporter, 0, 0, 1, +std::numeric_limits<float>::quiet_NaN(), true); |
Chris Dalton | 91982ee | 2017-07-14 14:04:52 -0600 | [diff] [blame] | 336 | } |
| 337 | |
Cary Clark | db16001 | 2018-08-31 15:07:51 -0400 | [diff] [blame] | 338 | static void test_cubic_cusps(skiatest::Reporter* reporter) { |
| 339 | std::array<SkPoint, 4> noCusps[] = { |
| 340 | {{{0, 0}, {1, 1}, {2, 2}, {3, 3}}}, |
| 341 | {{{0, 0}, {1, 0}, {1, 1}, {0, 1}}}, |
| 342 | {{{0, 0}, {1, 0}, {2, 1}, {2, 2}}}, |
| 343 | {{{0, 0}, {1, 0}, {1, 1}, {2, 1}}}, |
| 344 | }; |
| 345 | for (auto noCusp : noCusps) { |
| 346 | REPORTER_ASSERT(reporter, SkFindCubicCusp(noCusp.data()) < 0); |
| 347 | } |
| 348 | std::array<SkPoint, 4> cusps[] = { |
| 349 | {{{0, 0}, {1, 1}, {1, 0}, {0, 1}}}, |
| 350 | {{{0, 0}, {1, 1}, {0, 1}, {1, 0}}}, |
| 351 | {{{0, 1}, {1, 0}, {0, 0}, {1, 1}}}, |
| 352 | {{{0, 1}, {1, 0}, {1, 1}, {0, 0}}}, |
| 353 | }; |
| 354 | for (auto cusp : cusps) { |
| 355 | REPORTER_ASSERT(reporter, SkFindCubicCusp(cusp.data()) > 0); |
| 356 | } |
| 357 | } |
| 358 | |
tfarina@chromium.org | e4fafb1 | 2013-12-12 21:11:12 +0000 | [diff] [blame] | 359 | DEF_TEST(Geometry, reporter) { |
Chris Dalton | 1208e0f | 2018-08-13 00:20:33 -0600 | [diff] [blame] | 360 | SkPoint pts[5]; |
reed@android.com | d8730ea | 2009-02-27 22:06:06 +0000 | [diff] [blame] | 361 | |
| 362 | pts[0].set(0, 0); |
| 363 | pts[1].set(100, 50); |
| 364 | pts[2].set(0, 100); |
| 365 | |
Chris Dalton | 1208e0f | 2018-08-13 00:20:33 -0600 | [diff] [blame] | 366 | int count = SkChopQuadAtMaxCurvature(pts, pts); // Ensure src and dst can be the same pointer. |
reed@android.com | d8730ea | 2009-02-27 22:06:06 +0000 | [diff] [blame] | 367 | REPORTER_ASSERT(reporter, count == 1 || count == 2); |
reed@google.com | 6fc321a | 2011-07-27 13:54:36 +0000 | [diff] [blame] | 368 | |
| 369 | pts[0].set(0, 0); |
reed | daee7ea | 2015-03-26 20:22:33 -0700 | [diff] [blame] | 370 | pts[1].set(3, 0); |
| 371 | pts[2].set(3, 3); |
Chris Dalton | 1208e0f | 2018-08-13 00:20:33 -0600 | [diff] [blame] | 372 | SkConvertQuadToCubic(pts, pts); |
reed@google.com | 6fc321a | 2011-07-27 13:54:36 +0000 | [diff] [blame] | 373 | const SkPoint cubic[] = { |
reed | daee7ea | 2015-03-26 20:22:33 -0700 | [diff] [blame] | 374 | { 0, 0, }, { 2, 0, }, { 3, 1, }, { 3, 3 }, |
reed@google.com | 6fc321a | 2011-07-27 13:54:36 +0000 | [diff] [blame] | 375 | }; |
| 376 | for (int i = 0; i < 4; ++i) { |
Chris Dalton | 1208e0f | 2018-08-13 00:20:33 -0600 | [diff] [blame] | 377 | REPORTER_ASSERT(reporter, nearly_equal(cubic[i], pts[i])); |
reed@google.com | 6fc321a | 2011-07-27 13:54:36 +0000 | [diff] [blame] | 378 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 379 | |
reed@google.com | 087d5aa | 2012-02-29 20:59:24 +0000 | [diff] [blame] | 380 | testChopCubic(reporter); |
reed | 65cb2cd | 2015-03-19 10:18:47 -0700 | [diff] [blame] | 381 | test_evalquadat(reporter); |
reed | b640203 | 2015-03-20 13:23:43 -0700 | [diff] [blame] | 382 | test_conic(reporter); |
caryclark | 45398df | 2015-08-25 13:19:06 -0700 | [diff] [blame] | 383 | test_cubic_tangents(reporter); |
| 384 | test_quad_tangents(reporter); |
| 385 | test_conic_tangents(reporter); |
reed | b1b12f8 | 2016-07-13 10:56:53 -0700 | [diff] [blame] | 386 | test_conic_to_quads(reporter); |
Chris Dalton | 91982ee | 2017-07-14 14:04:52 -0600 | [diff] [blame] | 387 | test_classify_cubic(reporter); |
Cary Clark | db16001 | 2018-08-31 15:07:51 -0400 | [diff] [blame] | 388 | test_cubic_cusps(reporter); |
reed@android.com | d8730ea | 2009-02-27 22:06:06 +0000 | [diff] [blame] | 389 | } |