Mike Reed | 0d4a183 | 2018-08-14 17:21:23 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkCubicMap.h" |
Ben Wagner | 9707a7e | 2019-05-06 17:17:19 -0400 | [diff] [blame] | 9 | #include "include/core/SkPoint.h" |
| 10 | #include "include/core/SkScalar.h" |
| 11 | #include "include/core/SkTypes.h" |
| 12 | #include "include/private/SkNx.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "src/core/SkGeometry.h" |
| 14 | #include "src/pathops/SkPathOpsCubic.h" |
| 15 | #include "tests/Test.h" |
Mike Reed | 340818e | 2018-08-15 17:12:08 -0400 | [diff] [blame] | 16 | |
| 17 | static float accurate_t(float A, float B, float C, float D) { |
| 18 | double roots[3]; |
| 19 | SkDEBUGCODE(int count =) SkDCubic::RootsValidT(A, B, C, D, roots); |
| 20 | SkASSERT(count == 1); |
| 21 | return (float)roots[0]; |
| 22 | } |
| 23 | |
| 24 | static float accurate_solve(SkPoint p1, SkPoint p2, SkScalar x) { |
| 25 | SkPoint array[] = { {0, 0}, p1, p2, {1,1} }; |
| 26 | SkCubicCoeff coeff(array); |
| 27 | |
| 28 | float t = accurate_t(coeff.fA[0], coeff.fB[0], coeff.fC[0], coeff.fD[0] - x); |
| 29 | SkASSERT(t >= 0 && t <= 1); |
| 30 | float y = coeff.eval(t)[1]; |
| 31 | SkASSERT(y >= 0 && y <= 1.0001f); |
| 32 | return y; |
| 33 | } |
Mike Reed | 0d4a183 | 2018-08-14 17:21:23 -0400 | [diff] [blame] | 34 | |
| 35 | static bool nearly_le(SkScalar a, SkScalar b) { |
| 36 | return a <= b || SkScalarNearlyZero(a - b); |
| 37 | } |
| 38 | |
Mike Reed | 340818e | 2018-08-15 17:12:08 -0400 | [diff] [blame] | 39 | static void exercise_cubicmap(SkPoint p1, SkPoint p2, skiatest::Reporter* reporter) { |
| 40 | const SkScalar MAX_SOLVER_ERR = 0.008f; // found by running w/ current impl |
| 41 | |
| 42 | SkCubicMap cmap(p1, p2); |
| 43 | |
Mike Reed | 0d4a183 | 2018-08-14 17:21:23 -0400 | [diff] [blame] | 44 | SkScalar prev_y = 0; |
Mike Reed | 340818e | 2018-08-15 17:12:08 -0400 | [diff] [blame] | 45 | SkScalar dx = 1.0f / 512; |
| 46 | for (SkScalar x = dx; x < 1; x += dx) { |
Mike Reed | 0d4a183 | 2018-08-14 17:21:23 -0400 | [diff] [blame] | 47 | SkScalar y = cmap.computeYFromX(x); |
Mike Reed | 340818e | 2018-08-15 17:12:08 -0400 | [diff] [blame] | 48 | // are we valid and (mostly) monotonic? |
Florin Malita | 8dd5ea1 | 2019-03-29 13:25:41 -0400 | [diff] [blame] | 49 | if (!nearly_le(prev_y, y)) { |
Mike Reed | 0d4a183 | 2018-08-14 17:21:23 -0400 | [diff] [blame] | 50 | cmap.computeYFromX(x); |
| 51 | REPORTER_ASSERT(reporter, false); |
| 52 | } |
| 53 | prev_y = y; |
Mike Reed | 340818e | 2018-08-15 17:12:08 -0400 | [diff] [blame] | 54 | |
| 55 | // are we close to the "correct" answer? |
| 56 | SkScalar yy = accurate_solve(p1, p2, x); |
| 57 | SkScalar diff = SkScalarAbs(yy - y); |
| 58 | REPORTER_ASSERT(reporter, diff < MAX_SOLVER_ERR); |
Mike Reed | 0d4a183 | 2018-08-14 17:21:23 -0400 | [diff] [blame] | 59 | } |
| 60 | } |
| 61 | |
| 62 | DEF_TEST(CubicMap, r) { |
| 63 | const SkScalar values[] = { |
Mike Reed | 340818e | 2018-08-15 17:12:08 -0400 | [diff] [blame] | 64 | 0, 1, 0.5f, 0.0000001f, 0.999999f, |
Mike Reed | 0d4a183 | 2018-08-14 17:21:23 -0400 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | for (SkScalar x0 : values) { |
| 68 | for (SkScalar y0 : values) { |
| 69 | for (SkScalar x1 : values) { |
| 70 | for (SkScalar y1 : values) { |
Mike Reed | 340818e | 2018-08-15 17:12:08 -0400 | [diff] [blame] | 71 | exercise_cubicmap({ x0, y0 }, { x1, y1 }, r); |
Mike Reed | 0d4a183 | 2018-08-14 17:21:23 -0400 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |