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 */ 7 8#include "include/utils/SkRandom.h" 9#include "src/core/SkGeometry.h" 10#include "src/core/SkPointPriv.h" 11#include "tests/Test.h" 12 13#include <array> 14#include <numeric> 15 16static bool nearly_equal(const SkPoint& a, const SkPoint& b) { 17 return SkScalarNearlyEqual(a.fX, b.fX) && SkScalarNearlyEqual(a.fY, b.fY); 18} 19 20static void testChopCubic(skiatest::Reporter* reporter) { 21 /* 22 Inspired by this test, which used to assert that the tValues had dups 23 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]; 34 // make sure we don't assert internally 35 int count = SkChopCubicAtMaxCurvature(src, dst, tValues); 36 if (false) { // avoid bit rot, suppress warning 37 REPORTER_ASSERT(reporter, count); 38 } 39 // Make sure src and dst can be the same pointer. 40 { 41 SkPoint pts[7]; 42 for (int i = 0; i < 7; ++i) { 43 pts[i].set(i, i); 44 } 45 SkChopCubicAt(pts, pts, .5f); 46 for (int i = 0; i < 7; ++i) { 47 REPORTER_ASSERT(reporter, pts[i].fX == pts[i].fY); 48 REPORTER_ASSERT(reporter, pts[i].fX == i * .5f); 49 } 50 } 51 52 static const float chopTs[] = { 53 0, 3/83.f, 3/79.f, 3/73.f, 3/71.f, 3/67.f, 3/61.f, 3/59.f, 3/53.f, 3/47.f, 3/43.f, 3/41.f, 54 3/37.f, 3/31.f, 3/29.f, 3/23.f, 3/19.f, 3/17.f, 3/13.f, 3/11.f, 3/7.f, 3/5.f, 1, 55 }; 56 float ones[] = {1,1,1,1,1}; 57 58 // Ensure an odd number of T values so we exercise the single chop code at the end of 59 // SkChopCubicAt form multiple T. 60 static_assert(SK_ARRAY_COUNT(chopTs) % 2 == 1); 61 static_assert(SK_ARRAY_COUNT(ones) % 2 == 1); 62 63 SkRandom rand; 64 for (int iterIdx = 0; iterIdx < 5; ++iterIdx) { 65 SkPoint pts[4] = {{rand.nextF(), rand.nextF()}, {rand.nextF(), rand.nextF()}, 66 {rand.nextF(), rand.nextF()}, {rand.nextF(), rand.nextF()}}; 67 68 SkPoint allChops[4 + SK_ARRAY_COUNT(chopTs)*3]; 69 SkChopCubicAt(pts, allChops, chopTs, SK_ARRAY_COUNT(chopTs)); 70 int i = 3; 71 for (float chopT : chopTs) { 72 // Ensure we chop at approximately the correct points when we chop an entire list. 73 SkPoint expectedPt; 74 SkEvalCubicAt(pts, chopT, &expectedPt, nullptr, nullptr); 75 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(allChops[i].x(), expectedPt.x())); 76 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(allChops[i].y(), expectedPt.y())); 77 if (chopT == 0) { 78 REPORTER_ASSERT(reporter, allChops[i] == pts[0]); 79 } 80 if (chopT == 1) { 81 REPORTER_ASSERT(reporter, allChops[i] == pts[3]); 82 } 83 i += 3; 84 85 // Ensure the middle is exactly degenerate when we chop at two equal points. 86 SkPoint localChops[10]; 87 SkChopCubicAt(pts, localChops, chopT, chopT); 88 REPORTER_ASSERT(reporter, localChops[3] == localChops[4]); 89 REPORTER_ASSERT(reporter, localChops[3] == localChops[5]); 90 REPORTER_ASSERT(reporter, localChops[3] == localChops[6]); 91 if (chopT == 0) { 92 // Also ensure the first curve is exactly p0 when we chop at T=0. 93 REPORTER_ASSERT(reporter, localChops[0] == pts[0]); 94 REPORTER_ASSERT(reporter, localChops[1] == pts[0]); 95 REPORTER_ASSERT(reporter, localChops[2] == pts[0]); 96 REPORTER_ASSERT(reporter, localChops[3] == pts[0]); 97 } 98 if (chopT == 1) { 99 // Also ensure the last curve is exactly p3 when we chop at T=1. 100 REPORTER_ASSERT(reporter, localChops[6] == pts[3]); 101 REPORTER_ASSERT(reporter, localChops[7] == pts[3]); 102 REPORTER_ASSERT(reporter, localChops[8] == pts[3]); 103 REPORTER_ASSERT(reporter, localChops[9] == pts[3]); 104 } 105 } 106 107 // Now test what happens when SkChopCubicAt does 0/0 and gets NaN values. 108 SkPoint oneChops[4 + SK_ARRAY_COUNT(ones)*3]; 109 SkChopCubicAt(pts, oneChops, ones, SK_ARRAY_COUNT(ones)); 110 REPORTER_ASSERT(reporter, oneChops[0] == pts[0]); 111 REPORTER_ASSERT(reporter, oneChops[1] == pts[1]); 112 REPORTER_ASSERT(reporter, oneChops[2] == pts[2]); 113 for (size_t index = 3; index < SK_ARRAY_COUNT(oneChops); ++index) { 114 REPORTER_ASSERT(reporter, oneChops[index] == pts[3]); 115 } 116 } 117} 118 119static void check_pairs(skiatest::Reporter* reporter, int index, SkScalar t, const char name[], 120 SkScalar x0, SkScalar y0, SkScalar x1, SkScalar y1) { 121 bool eq = SkScalarNearlyEqual(x0, x1) && SkScalarNearlyEqual(y0, y1); 122 if (!eq) { 123 SkDebugf("%s [%d %g] p0 [%10.8f %10.8f] p1 [%10.8f %10.8f]\n", 124 name, index, t, x0, y0, x1, y1); 125 REPORTER_ASSERT(reporter, eq); 126 } 127} 128 129static void test_evalquadat(skiatest::Reporter* reporter) { 130 SkRandom rand; 131 for (int i = 0; i < 1000; ++i) { 132 SkPoint pts[3]; 133 for (int j = 0; j < 3; ++j) { 134 pts[j].set(rand.nextSScalar1() * 100, rand.nextSScalar1() * 100); 135 } 136 const SkScalar dt = SK_Scalar1 / 128; 137 SkScalar t = dt; 138 for (int j = 1; j < 128; ++j) { 139 SkPoint r0; 140 SkEvalQuadAt(pts, t, &r0); 141 SkPoint r1 = SkEvalQuadAt(pts, t); 142 check_pairs(reporter, i, t, "quad-pos", r0.fX, r0.fY, r1.fX, r1.fY); 143 144 SkVector v0; 145 SkEvalQuadAt(pts, t, nullptr, &v0); 146 SkVector v1 = SkEvalQuadTangentAt(pts, t); 147 check_pairs(reporter, i, t, "quad-tan", v0.fX, v0.fY, v1.fX, v1.fY); 148 149 t += dt; 150 } 151 } 152} 153 154static void test_conic_eval_pos(skiatest::Reporter* reporter, const SkConic& conic, SkScalar t) { 155 SkPoint p0, p1; 156 conic.evalAt(t, &p0, nullptr); 157 p1 = conic.evalAt(t); 158 check_pairs(reporter, 0, t, "conic-pos", p0.fX, p0.fY, p1.fX, p1.fY); 159} 160 161static void test_conic_eval_tan(skiatest::Reporter* reporter, const SkConic& conic, SkScalar t) { 162 SkVector v0, v1; 163 conic.evalAt(t, nullptr, &v0); 164 v1 = conic.evalTangentAt(t); 165 check_pairs(reporter, 0, t, "conic-tan", v0.fX, v0.fY, v1.fX, v1.fY); 166} 167 168static void test_conic(skiatest::Reporter* reporter) { 169 SkRandom rand; 170 for (int i = 0; i < 1000; ++i) { 171 SkPoint pts[3]; 172 for (int j = 0; j < 3; ++j) { 173 pts[j].set(rand.nextSScalar1() * 100, rand.nextSScalar1() * 100); 174 } 175 for (int k = 0; k < 10; ++k) { 176 SkScalar w = rand.nextUScalar1() * 2; 177 SkConic conic(pts, w); 178 179 const SkScalar dt = SK_Scalar1 / 128; 180 SkScalar t = dt; 181 for (int j = 1; j < 128; ++j) { 182 test_conic_eval_pos(reporter, conic, t); 183 test_conic_eval_tan(reporter, conic, t); 184 t += dt; 185 } 186 } 187 } 188} 189 190static void test_quad_tangents(skiatest::Reporter* reporter) { 191 SkPoint pts[] = { 192 {10, 20}, {10, 20}, {20, 30}, 193 {10, 20}, {15, 25}, {20, 30}, 194 {10, 20}, {20, 30}, {20, 30}, 195 }; 196 int count = (int) SK_ARRAY_COUNT(pts) / 3; 197 for (int index = 0; index < count; ++index) { 198 SkConic conic(&pts[index * 3], 0.707f); 199 SkVector start = SkEvalQuadTangentAt(&pts[index * 3], 0); 200 SkVector mid = SkEvalQuadTangentAt(&pts[index * 3], .5f); 201 SkVector end = SkEvalQuadTangentAt(&pts[index * 3], 1); 202 REPORTER_ASSERT(reporter, start.fX && start.fY); 203 REPORTER_ASSERT(reporter, mid.fX && mid.fY); 204 REPORTER_ASSERT(reporter, end.fX && end.fY); 205 REPORTER_ASSERT(reporter, SkScalarNearlyZero(start.cross(mid))); 206 REPORTER_ASSERT(reporter, SkScalarNearlyZero(mid.cross(end))); 207 } 208} 209 210static void test_conic_tangents(skiatest::Reporter* reporter) { 211 SkPoint pts[] = { 212 { 10, 20}, {10, 20}, {20, 30}, 213 { 10, 20}, {15, 25}, {20, 30}, 214 { 10, 20}, {20, 30}, {20, 30} 215 }; 216 int count = (int) SK_ARRAY_COUNT(pts) / 3; 217 for (int index = 0; index < count; ++index) { 218 SkConic conic(&pts[index * 3], 0.707f); 219 SkVector start = conic.evalTangentAt(0); 220 SkVector mid = conic.evalTangentAt(.5f); 221 SkVector end = conic.evalTangentAt(1); 222 REPORTER_ASSERT(reporter, start.fX && start.fY); 223 REPORTER_ASSERT(reporter, mid.fX && mid.fY); 224 REPORTER_ASSERT(reporter, end.fX && end.fY); 225 REPORTER_ASSERT(reporter, SkScalarNearlyZero(start.cross(mid))); 226 REPORTER_ASSERT(reporter, SkScalarNearlyZero(mid.cross(end))); 227 } 228} 229 230static void test_this_conic_to_quad(skiatest::Reporter* r, const SkPoint pts[3], SkScalar w) { 231 SkAutoConicToQuads quadder; 232 const SkPoint* qpts = quadder.computeQuads(pts, w, 0.25); 233 const int qcount = quadder.countQuads(); 234 const int pcount = qcount * 2 + 1; 235 236 REPORTER_ASSERT(r, SkPointPriv::AreFinite(qpts, pcount)); 237} 238 239/** 240 * We need to ensure that when a conic is approximated by quads, that we always return finite 241 * values in the quads. 242 * 243 * Inspired by crbug_627414 244 */ 245static void test_conic_to_quads(skiatest::Reporter* reporter) { 246 const SkPoint triples[] = { 247 { 0, 0 }, { 1, 0 }, { 1, 1 }, 248 { 0, 0 }, { 3.58732e-43f, 2.72084f }, { 3.00392f, 3.00392f }, 249 { 0, 0 }, { 100000, 0 }, { 100000, 100000 }, 250 { 0, 0 }, { 1e30f, 0 }, { 1e30f, 1e30f }, 251 }; 252 const int N = sizeof(triples) / sizeof(SkPoint); 253 254 for (int i = 0; i < N; i += 3) { 255 const SkPoint* pts = &triples[i]; 256 257 SkScalar w = 1e30f; 258 do { 259 w *= 2; 260 test_this_conic_to_quad(reporter, pts, w); 261 } while (SkScalarIsFinite(w)); 262 test_this_conic_to_quad(reporter, pts, SK_ScalarNaN); 263 } 264} 265 266static void test_cubic_tangents(skiatest::Reporter* reporter) { 267 SkPoint pts[] = { 268 { 10, 20}, {10, 20}, {20, 30}, {30, 40}, 269 { 10, 20}, {15, 25}, {20, 30}, {30, 40}, 270 { 10, 20}, {20, 30}, {30, 40}, {30, 40}, 271 }; 272 int count = (int) SK_ARRAY_COUNT(pts) / 4; 273 for (int index = 0; index < count; ++index) { 274 SkConic conic(&pts[index * 3], 0.707f); 275 SkVector start, mid, end; 276 SkEvalCubicAt(&pts[index * 4], 0, nullptr, &start, nullptr); 277 SkEvalCubicAt(&pts[index * 4], .5f, nullptr, &mid, nullptr); 278 SkEvalCubicAt(&pts[index * 4], 1, nullptr, &end, nullptr); 279 REPORTER_ASSERT(reporter, start.fX && start.fY); 280 REPORTER_ASSERT(reporter, mid.fX && mid.fY); 281 REPORTER_ASSERT(reporter, end.fX && end.fY); 282 REPORTER_ASSERT(reporter, SkScalarNearlyZero(start.cross(mid))); 283 REPORTER_ASSERT(reporter, SkScalarNearlyZero(mid.cross(end))); 284 } 285} 286 287static void check_cubic_type(skiatest::Reporter* reporter, 288 const std::array<SkPoint, 4>& bezierPoints, SkCubicType expectedType, 289 bool undefined = false) { 290 // Classify the cubic even if the results will be undefined: check for crashes and asserts. 291 SkCubicType actualType = SkClassifyCubic(bezierPoints.data()); 292 if (!undefined) { 293 REPORTER_ASSERT(reporter, actualType == expectedType); 294 } 295} 296 297static void check_cubic_around_rect(skiatest::Reporter* reporter, 298 float x1, float y1, float x2, float y2, 299 bool undefined = false) { 300 static constexpr SkCubicType expectations[24] = { 301 SkCubicType::kLoop, 302 SkCubicType::kCuspAtInfinity, 303 SkCubicType::kLocalCusp, 304 SkCubicType::kLocalCusp, 305 SkCubicType::kCuspAtInfinity, 306 SkCubicType::kLoop, 307 SkCubicType::kCuspAtInfinity, 308 SkCubicType::kLoop, 309 SkCubicType::kCuspAtInfinity, 310 SkCubicType::kLoop, 311 SkCubicType::kLocalCusp, 312 SkCubicType::kLocalCusp, 313 SkCubicType::kLocalCusp, 314 SkCubicType::kLocalCusp, 315 SkCubicType::kLoop, 316 SkCubicType::kCuspAtInfinity, 317 SkCubicType::kLoop, 318 SkCubicType::kCuspAtInfinity, 319 SkCubicType::kLoop, 320 SkCubicType::kCuspAtInfinity, 321 SkCubicType::kLocalCusp, 322 SkCubicType::kLocalCusp, 323 SkCubicType::kCuspAtInfinity, 324 SkCubicType::kLoop, 325 }; 326 SkPoint points[] = {{x1, y1}, {x2, y1}, {x2, y2}, {x1, y2}}; 327 std::array<SkPoint, 4> bezier; 328 for (int i=0; i < 4; ++i) { 329 bezier[0] = points[i]; 330 for (int j=0; j < 3; ++j) { 331 int jidx = (j < i) ? j : j+1; 332 bezier[1] = points[jidx]; 333 for (int k=0, kidx=0; k < 2; ++k, ++kidx) { 334 for (int n = 0; n < 2; ++n) { 335 kidx = (kidx == i || kidx == jidx) ? kidx+1 : kidx; 336 } 337 bezier[2] = points[kidx]; 338 for (int l = 0; l < 4; ++l) { 339 if (l != i && l != jidx && l != kidx) { 340 bezier[3] = points[l]; 341 break; 342 } 343 } 344 check_cubic_type(reporter, bezier, expectations[i*6 + j*2 + k], undefined); 345 } 346 } 347 } 348 for (int i=0; i < 4; ++i) { 349 bezier[0] = points[i]; 350 for (int j=0; j < 3; ++j) { 351 int jidx = (j < i) ? j : j+1; 352 bezier[1] = points[jidx]; 353 bezier[2] = points[jidx]; 354 for (int k=0, kidx=0; k < 2; ++k, ++kidx) { 355 for (int n = 0; n < 2; ++n) { 356 kidx = (kidx == i || kidx == jidx) ? kidx+1 : kidx; 357 } 358 bezier[3] = points[kidx]; 359 check_cubic_type(reporter, bezier, SkCubicType::kSerpentine, undefined); 360 } 361 } 362 } 363} 364 365static std::array<SkPoint, 4> kSerpentines[] = { 366 {{{149.325f, 107.705f}, {149.325f, 103.783f}, {151.638f, 100.127f}, {156.263f, 96.736f}}}, 367 {{{225.694f, 223.15f}, {209.831f, 224.837f}, {195.994f, 230.237f}, {184.181f, 239.35f}}}, 368 {{{4.873f, 5.581f}, {5.083f, 5.2783f}, {5.182f, 4.8593f}, {5.177f, 4.3242f}}}, 369 {{{285.625f, 499.687f}, {411.625f, 808.188f}, {1064.62f, 135.688f}, {1042.63f, 585.187f}}} 370}; 371 372static std::array<SkPoint, 4> kLoops[] = { 373 {{{635.625f, 614.687f}, {171.625f, 236.188f}, {1064.62f, 135.688f}, {516.625f, 570.187f}}}, 374 {{{653.050f, 725.049f}, {663.000f, 176.000f}, {1189.000f, 508.000f}, {288.050f, 564.950f}}}, 375 {{{631.050f, 478.049f}, {730.000f, 302.000f}, {870.000f, 350.000f}, {905.050f, 528.950f}}}, 376 {{{631.050f, 478.0499f}, {221.000f, 230.000f}, {1265.000f, 451.000f}, {905.050f, 528.950f}}} 377}; 378 379static std::array<SkPoint, 4> kLinearCubics[] = { 380 {{{0, 0}, {0, 1}, {0, 2}, {0, 3}}}, // 0-degree flat line. 381 {{{0, 0}, {1, 0}, {1, 0}, {0, 0}}}, // 180-degree flat line 382 {{{0, 1}, {0, 0}, {0, 2}, {0, 3}}}, // 180-degree flat line 383 {{{0, 1}, {0, 0}, {0, 3}, {0, 2}}}, // 360-degree flat line 384 {{{0, 0}, {2, 0}, {1, 0}, {64, 0}}}, // 360-degree flat line 385 {{{1, 0}, {0, 0}, {3, 0}, {-64, 0}}} // 360-degree flat line 386}; 387 388static void test_classify_cubic(skiatest::Reporter* reporter) { 389 for (const auto& serp : kSerpentines) { 390 check_cubic_type(reporter, serp, SkCubicType::kSerpentine); 391 } 392 for (const auto& loop : kLoops) { 393 check_cubic_type(reporter, loop, SkCubicType::kLoop); 394 } 395 for (const auto& loop : kLinearCubics) { 396 check_cubic_type(reporter, loop, SkCubicType::kLineOrPoint); 397 } 398 check_cubic_around_rect(reporter, 0, 0, 1, 1); 399 check_cubic_around_rect(reporter, 400 -std::numeric_limits<float>::max(), 401 -std::numeric_limits<float>::max(), 402 +std::numeric_limits<float>::max(), 403 +std::numeric_limits<float>::max()); 404 check_cubic_around_rect(reporter, 1, 1, 405 +std::numeric_limits<float>::min(), 406 +std::numeric_limits<float>::max()); 407 check_cubic_around_rect(reporter, 408 -std::numeric_limits<float>::min(), 409 -std::numeric_limits<float>::min(), 410 +std::numeric_limits<float>::min(), 411 +std::numeric_limits<float>::min()); 412 check_cubic_around_rect(reporter, +1, -std::numeric_limits<float>::min(), -1, -1); 413 check_cubic_around_rect(reporter, 414 -std::numeric_limits<float>::infinity(), 415 -std::numeric_limits<float>::infinity(), 416 +std::numeric_limits<float>::infinity(), 417 +std::numeric_limits<float>::infinity(), 418 true); 419 check_cubic_around_rect(reporter, 0, 0, 1, +std::numeric_limits<float>::infinity(), true); 420 check_cubic_around_rect(reporter, 421 -std::numeric_limits<float>::quiet_NaN(), 422 -std::numeric_limits<float>::quiet_NaN(), 423 +std::numeric_limits<float>::quiet_NaN(), 424 +std::numeric_limits<float>::quiet_NaN(), 425 true); 426 check_cubic_around_rect(reporter, 0, 0, 1, +std::numeric_limits<float>::quiet_NaN(), true); 427} 428 429static std::array<SkPoint, 4> kCusps[] = { 430 {{{0, 0}, {1, 1}, {1, 0}, {0, 1}}}, 431 {{{0, 0}, {1, 1}, {0, 1}, {1, 0}}}, 432 {{{0, 1}, {1, 0}, {0, 0}, {1, 1}}}, 433 {{{0, 1}, {1, 0}, {1, 1}, {0, 0}}}, 434}; 435 436static void test_cubic_cusps(skiatest::Reporter* reporter) { 437 std::array<SkPoint, 4> noCusps[] = { 438 {{{0, 0}, {1, 1}, {2, 2}, {3, 3}}}, 439 {{{0, 0}, {1, 0}, {1, 1}, {0, 1}}}, 440 {{{0, 0}, {1, 0}, {2, 1}, {2, 2}}}, 441 {{{0, 0}, {1, 0}, {1, 1}, {2, 1}}}, 442 }; 443 for (auto noCusp : noCusps) { 444 REPORTER_ASSERT(reporter, SkFindCubicCusp(noCusp.data()) < 0); 445 } 446 for (auto cusp : kCusps) { 447 REPORTER_ASSERT(reporter, SkFindCubicCusp(cusp.data()) > 0); 448 } 449} 450 451static SkMatrix kSkewMatrices[] = { 452 SkMatrix::MakeAll(1,0,0, 0,1,0, 0,0,1), 453 SkMatrix::MakeAll(1,-1,0, 1,1,0, 0,0,1), 454 SkMatrix::MakeAll(.889f,.553f,0, -.443f,.123f,0, 0,0,1), 455}; 456 457static void test_chop_quad_at_midtangent(skiatest::Reporter* reporter, const SkPoint pts[3]) { 458 constexpr float kTolerance = 1e-3f; 459 for (const SkMatrix& m : kSkewMatrices) { 460 SkPoint mapped[3]; 461 m.mapPoints(mapped, pts, 3); 462 float fullRotation = SkMeasureQuadRotation(pts); 463 SkPoint chopped[5]; 464 SkChopQuadAtMidTangent(pts, chopped); 465 float leftRotation = SkMeasureQuadRotation(chopped); 466 float rightRotation = SkMeasureQuadRotation(chopped+2); 467 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(leftRotation, fullRotation/2, kTolerance)); 468 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rightRotation, fullRotation/2, kTolerance)); 469 } 470} 471 472static void test_chop_cubic_at_midtangent(skiatest::Reporter* reporter, const SkPoint pts[4], 473 SkCubicType cubicType) { 474 constexpr float kTolerance = 1e-3f; 475 int n = SK_ARRAY_COUNT(kSkewMatrices); 476 if (cubicType == SkCubicType::kLocalCusp || cubicType == SkCubicType::kLineOrPoint) { 477 // FP precision isn't always enough to get the exact correct T value of the mid-tangent on 478 // cusps and lines. Only test the identity matrix and the matrix with all 1's. 479 n = 2; 480 } 481 for (int i = 0; i < n; ++i) { 482 SkPoint mapped[4]; 483 kSkewMatrices[i].mapPoints(mapped, pts, 4); 484 float fullRotation = SkMeasureNonInflectCubicRotation(mapped); 485 SkPoint chopped[7]; 486 SkChopCubicAtMidTangent(mapped, chopped); 487 float leftRotation = SkMeasureNonInflectCubicRotation(chopped); 488 float rightRotation = SkMeasureNonInflectCubicRotation(chopped+3); 489 if (cubicType == SkCubicType::kLineOrPoint && 490 (SkScalarNearlyEqual(fullRotation, 2*SK_ScalarPI, kTolerance) || 491 SkScalarNearlyEqual(fullRotation, 0, kTolerance))) { 492 // 0- and 360-degree flat lines don't have single points of midtangent. 493 // (tangent == midtangent at every point on these curves except the cusp points.) 494 // Instead verify the promise from SkChopCubicAtMidTangent that neither side will rotate 495 // more than 180 degrees. 496 REPORTER_ASSERT(reporter, std::abs(leftRotation) - kTolerance <= SK_ScalarPI); 497 REPORTER_ASSERT(reporter, std::abs(rightRotation) - kTolerance <= SK_ScalarPI); 498 continue; 499 } 500 float expectedChoppedRotation = fullRotation/2; 501 if (cubicType == SkCubicType::kLocalCusp || 502 (cubicType == SkCubicType::kLineOrPoint && 503 SkScalarNearlyEqual(fullRotation, SK_ScalarPI, kTolerance))) { 504 // If we chop a cubic at a cusp, we lose 180 degrees of rotation. 505 expectedChoppedRotation = (fullRotation - SK_ScalarPI)/2; 506 } 507 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(leftRotation, expectedChoppedRotation, 508 kTolerance)); 509 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(rightRotation, expectedChoppedRotation, 510 kTolerance)); 511 } 512} 513 514static std::array<SkPoint, 3> kQuads[] = { 515 {{{10, 20}, {15, 35}, {30, 40}}}, 516 {{{176.324f, 392.705f}, {719.325f, 205.782f}, {297.263f, 347.735f}}}, 517 {{{652.050f, 602.049f}, {481.000f, 533.000f}, {288.050f, 564.950f}}}, 518 {{{460.625f, 557.187f}, {707.121f, 209.688f}, {779.628f, 577.687f}}}, 519 {{{359.050f, 578.049f}, {759.000f, 274.000f}, {288.050f, 564.950f}}} 520}; 521 522SkPoint lerp(const SkPoint& a, const SkPoint& b, float t) { 523 return a * (1 - t) + b * t; 524} 525 526static void test_measure_rotation(skiatest::Reporter* reporter) { 527 static SkPoint kFlatCubic[4] = {{0, 0}, {0, 1}, {0, 2}, {0, 3}}; 528 REPORTER_ASSERT(reporter, SkScalarNearlyZero(SkMeasureNonInflectCubicRotation(kFlatCubic))); 529 530 static SkPoint kFlatCubic180_1[4] = {{0, 0}, {1, 0}, {3, 0}, {2, 0}}; 531 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkMeasureNonInflectCubicRotation(kFlatCubic180_1), 532 SK_ScalarPI)); 533 534 static SkPoint kFlatCubic180_2[4] = {{0, 1}, {0, 0}, {0, 2}, {0, 3}}; 535 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkMeasureNonInflectCubicRotation(kFlatCubic180_2), 536 SK_ScalarPI)); 537 538 static SkPoint kFlatCubic360[4] = {{0, 1}, {0, 0}, {0, 3}, {0, 2}}; 539 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkMeasureNonInflectCubicRotation(kFlatCubic360), 540 2*SK_ScalarPI)); 541 542 static SkPoint kSquare180[4] = {{0, 0}, {0, 1}, {1, 1}, {1, 0}}; 543 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkMeasureNonInflectCubicRotation(kSquare180), 544 SK_ScalarPI)); 545 546 auto checkQuadRotation = [=](const SkPoint pts[3], float expectedRotation) { 547 float r = SkMeasureQuadRotation(pts); 548 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(r, expectedRotation)); 549 550 SkPoint cubic1[4] = {pts[0], pts[0], pts[1], pts[2]}; 551 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkMeasureNonInflectCubicRotation(cubic1), 552 expectedRotation)); 553 554 SkPoint cubic2[4] = {pts[0], pts[1], pts[1], pts[2]}; 555 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkMeasureNonInflectCubicRotation(cubic2), 556 expectedRotation)); 557 558 SkPoint cubic3[4] = {pts[0], pts[1], pts[2], pts[2]}; 559 REPORTER_ASSERT(reporter, SkScalarNearlyEqual(SkMeasureNonInflectCubicRotation(cubic3), 560 expectedRotation)); 561 }; 562 563 static SkPoint kFlatQuad[4] = {{0, 0}, {0, 1}, {0, 2}}; 564 checkQuadRotation(kFlatQuad, 0); 565 566 static SkPoint kFlatQuad180_1[4] = {{1, 0}, {0, 0}, {2, 0}}; 567 checkQuadRotation(kFlatQuad180_1, SK_ScalarPI); 568 569 static SkPoint kFlatQuad180_2[4] = {{0, 0}, {0, 2}, {0, 1}}; 570 checkQuadRotation(kFlatQuad180_2, SK_ScalarPI); 571 572 static SkPoint kTri120[3] = {{0, 0}, {.5f, std::sqrt(3.f)/2}, {1, 0}}; 573 checkQuadRotation(kTri120, 2*SK_ScalarPI/3); 574} 575 576static void test_chop_at_midtangent(skiatest::Reporter* reporter) { 577 SkPoint chops[10]; 578 for (const auto& serp : kSerpentines) { 579 REPORTER_ASSERT(reporter, SkClassifyCubic(serp.data()) == SkCubicType::kSerpentine); 580 int n = SkChopCubicAtInflections(serp.data(), chops); 581 for (int i = 0; i < n; ++i) { 582 test_chop_cubic_at_midtangent(reporter, chops + i*3, SkCubicType::kSerpentine); 583 } 584 } 585 for (const auto& loop : kLoops) { 586 REPORTER_ASSERT(reporter, SkClassifyCubic(loop.data()) == SkCubicType::kLoop); 587 test_chop_cubic_at_midtangent(reporter, loop.data(), SkCubicType::kLoop); 588 } 589 for (const auto& line : kLinearCubics) { 590 REPORTER_ASSERT(reporter, SkClassifyCubic(line.data()) == SkCubicType::kLineOrPoint); 591 test_chop_cubic_at_midtangent(reporter, line.data(), SkCubicType::kLineOrPoint); 592 } 593 for (const auto& cusp : kCusps) { 594 REPORTER_ASSERT(reporter, SkClassifyCubic(cusp.data()) == SkCubicType::kLocalCusp); 595 test_chop_cubic_at_midtangent(reporter, cusp.data(), SkCubicType::kLocalCusp); 596 } 597 for (const auto& quad : kQuads) { 598 test_chop_quad_at_midtangent(reporter, quad.data()); 599 SkPoint asCubic[4] = { 600 quad[0], lerp(quad[0], quad[1], 2/3.f), lerp(quad[1], quad[2], 1/3.f), quad[2]}; 601 test_chop_cubic_at_midtangent(reporter, asCubic, SkCubicType::kQuadratic); 602 } 603 604 static const SkPoint kExactQuad[4] = {{0,0}, {6,2}, {10,2}, {12,0}}; 605 REPORTER_ASSERT(reporter, SkClassifyCubic(kExactQuad) == SkCubicType::kQuadratic); 606 test_chop_cubic_at_midtangent(reporter, kExactQuad, SkCubicType::kQuadratic); 607 608 static const SkPoint kExactCuspAtInf[4] = {{0,0}, {1,0}, {0,1}, {1,1}}; 609 REPORTER_ASSERT(reporter, SkClassifyCubic(kExactCuspAtInf) == SkCubicType::kCuspAtInfinity); 610 int n = SkChopCubicAtInflections(kExactCuspAtInf, chops); 611 for (int i = 0; i < n; ++i) { 612 test_chop_cubic_at_midtangent(reporter, chops + i*3, SkCubicType::kCuspAtInfinity); 613 } 614} 615 616DEF_TEST(Geometry, reporter) { 617 SkPoint pts[5]; 618 619 pts[0].set(0, 0); 620 pts[1].set(100, 50); 621 pts[2].set(0, 100); 622 623 int count = SkChopQuadAtMaxCurvature(pts, pts); // Ensure src and dst can be the same pointer. 624 REPORTER_ASSERT(reporter, count == 1 || count == 2); 625 626 // This previously crashed because the computed t of max curvature is NaN and SkChopQuadAt 627 // asserts that the passed t is in 0..1. Passes by not asserting. 628 pts[0].set(15.1213f, 7.77647f); 629 pts[1].set(6.2168e+19f, 1.51338e+20f); 630 pts[2].set(1.4579e+19f, 1.55558e+21f); 631 count = SkChopQuadAtMaxCurvature(pts, pts); 632 633 pts[0].set(0, 0); 634 pts[1].set(3, 0); 635 pts[2].set(3, 3); 636 SkConvertQuadToCubic(pts, pts); 637 const SkPoint cubic[] = { 638 { 0, 0, }, { 2, 0, }, { 3, 1, }, { 3, 3 }, 639 }; 640 for (int i = 0; i < 4; ++i) { 641 REPORTER_ASSERT(reporter, nearly_equal(cubic[i], pts[i])); 642 } 643 644 testChopCubic(reporter); 645 test_evalquadat(reporter); 646 test_conic(reporter); 647 test_cubic_tangents(reporter); 648 test_quad_tangents(reporter); 649 test_conic_tangents(reporter); 650 test_conic_to_quads(reporter); 651 test_classify_cubic(reporter); 652 test_cubic_cusps(reporter); 653 test_measure_rotation(reporter); 654 test_chop_at_midtangent(reporter); 655} 656