| Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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/gpu/tessellate/GrWangsFormula.h" |
| 11 | #include "tests/Test.h" |
| 12 | |
| 13 | constexpr static int kIntolerance = 4; // 1/4 pixel max error. |
| 14 | |
| 15 | const SkPoint kSerp[4] = { |
| 16 | {285.625f, 499.687f}, {411.625f, 808.188f}, {1064.62f, 135.688f}, {1042.63f, 585.187f}}; |
| 17 | |
| 18 | const SkPoint kLoop[4] = { |
| 19 | {635.625f, 614.687f}, {171.625f, 236.188f}, {1064.62f, 135.688f}, {516.625f, 570.187f}}; |
| 20 | |
| 21 | const SkPoint kQuad[4] = { |
| 22 | {460.625f, 557.187f}, {707.121f, 209.688f}, {779.628f, 577.687f}}; |
| 23 | |
| Chris Dalton | 4dd3c8c | 2020-10-30 22:45:58 -0600 | [diff] [blame] | 24 | static float wangs_formula_quadratic_reference_impl(float intolerance, const SkPoint p[3]) { |
| 25 | float k = (2 * 1) / 8.f * intolerance; |
| 26 | return sqrtf(k * (p[0] - p[1]*2 + p[2]).length()); |
| Chris Dalton | fc396a8 | 2020-09-23 20:11:26 -0600 | [diff] [blame] | 27 | } |
| 28 | |
| Chris Dalton | 4dd3c8c | 2020-10-30 22:45:58 -0600 | [diff] [blame] | 29 | static float wangs_formula_cubic_reference_impl(float intolerance, const SkPoint p[4]) { |
| 30 | float k = (3 * 2) / 8.f * intolerance; |
| 31 | return sqrtf(k * std::max((p[0] - p[1]*2 + p[2]).length(), |
| 32 | (p[1] - p[2]*2 + p[3]).length())); |
| Chris Dalton | fc396a8 | 2020-09-23 20:11:26 -0600 | [diff] [blame] | 33 | } |
| 34 | |
| Tyler Denniston | 573923c | 2020-12-10 11:47:40 -0500 | [diff] [blame] | 35 | // Returns number of segments for linearized quadratic rational. This is an analogue |
| 36 | // to Wang's formula, taken from: |
| 37 | // |
| 38 | // J. Zheng, T. Sederberg. "Estimating Tessellation Parameter Intervals for |
| 39 | // Rational Curves and Surfaces." ACM Transactions on Graphics 19(1). 2000. |
| 40 | // See Thm 3, Corollary 1. |
| 41 | // |
| 42 | // Input points should be in projected space. |
| 43 | static float wangs_formula_conic_reference_impl(float intolerance, |
| 44 | const SkPoint P[3], |
| 45 | const float w) { |
| 46 | // Compute center of bounding box in projected space |
| 47 | float min_x = P[0].fX, max_x = min_x, |
| 48 | min_y = P[0].fY, max_y = min_y; |
| 49 | for (int i = 1; i < 3; i++) { |
| 50 | min_x = std::min(min_x, P[i].fX); |
| 51 | max_x = std::max(max_x, P[i].fX); |
| 52 | min_y = std::min(min_y, P[i].fY); |
| 53 | max_y = std::max(max_y, P[i].fY); |
| 54 | } |
| 55 | const SkPoint C = SkPoint::Make(0.5f * (min_x + max_x), 0.5f * (min_y + max_y)); |
| 56 | |
| 57 | // Translate control points and compute max length |
| 58 | SkPoint tP[3] = {P[0] - C, P[1] - C, P[2] - C}; |
| 59 | float max_len = 0; |
| 60 | for (int i = 0; i < 3; i++) { |
| 61 | max_len = std::max(max_len, tP[i].length()); |
| 62 | } |
| 63 | SkASSERT(max_len > 0); |
| 64 | |
| 65 | // Compute delta = parametric step size of linearization |
| 66 | const float eps = 1 / intolerance; |
| 67 | const float r_minus_eps = std::max(0.f, max_len - eps); |
| 68 | const float min_w = std::min(w, 1.f); |
| 69 | const float numer = 4 * min_w * eps; |
| 70 | const float denom = |
| 71 | (tP[2] - tP[1] * 2 * w + tP[0]).length() + r_minus_eps * std::abs(1 - 2 * w + 1); |
| 72 | const float delta = sqrtf(numer / denom); |
| 73 | |
| 74 | // Return corresponding num segments in the interval [tmin,tmax] |
| 75 | constexpr float tmin = 0, tmax = 1; |
| 76 | SkASSERT(delta > 0); |
| 77 | return (tmax - tmin) / delta; |
| 78 | } |
| 79 | |
| Chris Dalton | fc396a8 | 2020-09-23 20:11:26 -0600 | [diff] [blame] | 80 | static void for_random_matrices(SkRandom* rand, std::function<void(const SkMatrix&)> f) { |
| Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 81 | SkMatrix m; |
| 82 | m.setIdentity(); |
| 83 | f(m); |
| 84 | |
| 85 | for (int i = -10; i <= 30; ++i) { |
| 86 | for (int j = -10; j <= 30; ++j) { |
| 87 | m.setScaleX(std::ldexp(1 + rand->nextF(), i)); |
| 88 | m.setSkewX(0); |
| 89 | m.setSkewY(0); |
| 90 | m.setScaleY(std::ldexp(1 + rand->nextF(), j)); |
| 91 | f(m); |
| 92 | |
| 93 | m.setScaleX(std::ldexp(1 + rand->nextF(), i)); |
| 94 | m.setSkewX(std::ldexp(1 + rand->nextF(), (j + i) / 2)); |
| 95 | m.setSkewY(std::ldexp(1 + rand->nextF(), (j + i) / 2)); |
| 96 | m.setScaleY(std::ldexp(1 + rand->nextF(), j)); |
| 97 | f(m); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| Chris Dalton | fc396a8 | 2020-09-23 20:11:26 -0600 | [diff] [blame] | 102 | static void for_random_beziers(int numPoints, SkRandom* rand, |
| Tyler Denniston | 4be95c5 | 2020-12-02 14:22:12 -0500 | [diff] [blame] | 103 | std::function<void(const SkPoint[])> f, |
| 104 | int maxExponent = 30) { |
| Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 105 | SkASSERT(numPoints <= 4); |
| 106 | SkPoint pts[4]; |
| Tyler Denniston | 4be95c5 | 2020-12-02 14:22:12 -0500 | [diff] [blame] | 107 | for (int i = -10; i <= maxExponent; ++i) { |
| Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 108 | for (int j = 0; j < numPoints; ++j) { |
| 109 | pts[j].set(std::ldexp(1 + rand->nextF(), i), std::ldexp(1 + rand->nextF(), i)); |
| 110 | } |
| 111 | f(pts); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | // Ensure the optimized "*_log2" versions return the same value as ceil(std::log2(f)). |
| 116 | DEF_TEST(WangsFormula_log2, r) { |
| 117 | // Constructs a cubic such that the 'length' term in wang's formula == term. |
| 118 | // |
| 119 | // f = sqrt(k * length(max(abs(p0 - p1*2 + p2), |
| 120 | // abs(p1 - p2*2 + p3)))); |
| 121 | auto setupCubicLengthTerm = [](int seed, SkPoint pts[], float term) { |
| 122 | memset(pts, 0, sizeof(SkPoint) * 4); |
| 123 | |
| 124 | SkPoint term2d = (seed & 1) ? |
| 125 | SkPoint::Make(term, 0) : SkPoint::Make(.5f, std::sqrt(3)/2) * term; |
| 126 | seed >>= 1; |
| 127 | |
| 128 | if (seed & 1) { |
| 129 | term2d.fX = -term2d.fX; |
| 130 | } |
| 131 | seed >>= 1; |
| 132 | |
| 133 | if (seed & 1) { |
| 134 | std::swap(term2d.fX, term2d.fY); |
| 135 | } |
| 136 | seed >>= 1; |
| 137 | |
| 138 | switch (seed % 4) { |
| 139 | case 0: |
| 140 | pts[0] = term2d; |
| 141 | pts[3] = term2d * .75f; |
| 142 | return; |
| 143 | case 1: |
| 144 | pts[1] = term2d * -.5f; |
| 145 | return; |
| 146 | case 2: |
| 147 | pts[1] = term2d * -.5f; |
| 148 | return; |
| 149 | case 3: |
| 150 | pts[3] = term2d; |
| 151 | pts[0] = term2d * .75f; |
| 152 | return; |
| 153 | } |
| 154 | }; |
| 155 | |
| 156 | // Constructs a quadratic such that the 'length' term in wang's formula == term. |
| 157 | // |
| 158 | // f = sqrt(k * length(p0 - p1*2 + p2)); |
| 159 | auto setupQuadraticLengthTerm = [](int seed, SkPoint pts[], float term) { |
| 160 | memset(pts, 0, sizeof(SkPoint) * 3); |
| 161 | |
| 162 | SkPoint term2d = (seed & 1) ? |
| 163 | SkPoint::Make(term, 0) : SkPoint::Make(.5f, std::sqrt(3)/2) * term; |
| 164 | seed >>= 1; |
| 165 | |
| 166 | if (seed & 1) { |
| 167 | term2d.fX = -term2d.fX; |
| 168 | } |
| 169 | seed >>= 1; |
| 170 | |
| 171 | if (seed & 1) { |
| 172 | std::swap(term2d.fX, term2d.fY); |
| 173 | } |
| 174 | seed >>= 1; |
| 175 | |
| 176 | switch (seed % 3) { |
| 177 | case 0: |
| 178 | pts[0] = term2d; |
| 179 | return; |
| 180 | case 1: |
| 181 | pts[1] = term2d * -.5f; |
| 182 | return; |
| 183 | case 2: |
| 184 | pts[2] = term2d; |
| 185 | return; |
| 186 | } |
| 187 | }; |
| 188 | |
| Chris Dalton | fc396a8 | 2020-09-23 20:11:26 -0600 | [diff] [blame] | 189 | // GrWangsFormula::cubic and ::quadratic both use rsqrt instead of sqrt for speed. Linearization |
| 190 | // is all approximate anyway, so as long as we are within ~1/2 tessellation segment of the |
| 191 | // reference value we are good enough. |
| 192 | constexpr static float kTessellationTolerance = 1/128.f; |
| 193 | |
| Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 194 | for (int level = 0; level < 30; ++level) { |
| 195 | float epsilon = std::ldexp(SK_ScalarNearlyZero, level * 2); |
| 196 | SkPoint pts[4]; |
| 197 | |
| 198 | { |
| 199 | // Test cubic boundaries. |
| 200 | // f = sqrt(k * length(max(abs(p0 - p1*2 + p2), |
| 201 | // abs(p1 - p2*2 + p3)))); |
| 202 | constexpr static float k = (3 * 2) / (8 * (1.f/kIntolerance)); |
| 203 | float x = std::ldexp(1, level * 2) / k; |
| 204 | setupCubicLengthTerm(level << 1, pts, x - epsilon); |
| Chris Dalton | fc396a8 | 2020-09-23 20:11:26 -0600 | [diff] [blame] | 205 | float referenceValue = wangs_formula_cubic_reference_impl(kIntolerance, pts); |
| 206 | REPORTER_ASSERT(r, std::ceil(std::log2(referenceValue)) == level); |
| 207 | float c = GrWangsFormula::cubic(kIntolerance, pts); |
| 208 | REPORTER_ASSERT(r, SkScalarNearlyEqual(c/referenceValue, 1, kTessellationTolerance)); |
| Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 209 | REPORTER_ASSERT(r, GrWangsFormula::cubic_log2(kIntolerance, pts) == level); |
| 210 | setupCubicLengthTerm(level << 1, pts, x + epsilon); |
| Chris Dalton | fc396a8 | 2020-09-23 20:11:26 -0600 | [diff] [blame] | 211 | referenceValue = wangs_formula_cubic_reference_impl(kIntolerance, pts); |
| 212 | REPORTER_ASSERT(r, std::ceil(std::log2(referenceValue)) == level + 1); |
| 213 | c = GrWangsFormula::cubic(kIntolerance, pts); |
| 214 | REPORTER_ASSERT(r, SkScalarNearlyEqual(c/referenceValue, 1, kTessellationTolerance)); |
| Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 215 | REPORTER_ASSERT(r, GrWangsFormula::cubic_log2(kIntolerance, pts) == level + 1); |
| 216 | } |
| 217 | |
| 218 | { |
| 219 | // Test quadratic boundaries. |
| 220 | // f = std::sqrt(k * Length(p0 - p1*2 + p2)); |
| 221 | constexpr static float k = 2 / (8 * (1.f/kIntolerance)); |
| 222 | float x = std::ldexp(1, level * 2) / k; |
| 223 | setupQuadraticLengthTerm(level << 1, pts, x - epsilon); |
| Chris Dalton | fc396a8 | 2020-09-23 20:11:26 -0600 | [diff] [blame] | 224 | float referenceValue = wangs_formula_quadratic_reference_impl(kIntolerance, pts); |
| 225 | REPORTER_ASSERT(r, std::ceil(std::log2(referenceValue)) == level); |
| 226 | float q = GrWangsFormula::quadratic(kIntolerance, pts); |
| 227 | REPORTER_ASSERT(r, SkScalarNearlyEqual(q/referenceValue, 1, kTessellationTolerance)); |
| Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 228 | REPORTER_ASSERT(r, GrWangsFormula::quadratic_log2(kIntolerance, pts) == level); |
| 229 | setupQuadraticLengthTerm(level << 1, pts, x + epsilon); |
| Chris Dalton | fc396a8 | 2020-09-23 20:11:26 -0600 | [diff] [blame] | 230 | referenceValue = wangs_formula_quadratic_reference_impl(kIntolerance, pts); |
| 231 | REPORTER_ASSERT(r, std::ceil(std::log2(referenceValue)) == level+1); |
| 232 | q = GrWangsFormula::quadratic(kIntolerance, pts); |
| 233 | REPORTER_ASSERT(r, SkScalarNearlyEqual(q/referenceValue, 1, kTessellationTolerance)); |
| Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 234 | REPORTER_ASSERT(r, GrWangsFormula::quadratic_log2(kIntolerance, pts) == level + 1); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | auto check_cubic_log2 = [&](const SkPoint* pts) { |
| Chris Dalton | fc396a8 | 2020-09-23 20:11:26 -0600 | [diff] [blame] | 239 | float f = std::max(1.f, wangs_formula_cubic_reference_impl(kIntolerance, pts)); |
| Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 240 | int f_log2 = GrWangsFormula::cubic_log2(kIntolerance, pts); |
| 241 | REPORTER_ASSERT(r, SkScalarCeilToInt(std::log2(f)) == f_log2); |
| Chris Dalton | fc396a8 | 2020-09-23 20:11:26 -0600 | [diff] [blame] | 242 | float c = std::max(1.f, GrWangsFormula::cubic(kIntolerance, pts)); |
| 243 | REPORTER_ASSERT(r, SkScalarNearlyEqual(c/f, 1, kTessellationTolerance)); |
| Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 244 | }; |
| 245 | |
| 246 | auto check_quadratic_log2 = [&](const SkPoint* pts) { |
| Chris Dalton | fc396a8 | 2020-09-23 20:11:26 -0600 | [diff] [blame] | 247 | float f = std::max(1.f, wangs_formula_quadratic_reference_impl(kIntolerance, pts)); |
| Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 248 | int f_log2 = GrWangsFormula::quadratic_log2(kIntolerance, pts); |
| 249 | REPORTER_ASSERT(r, SkScalarCeilToInt(std::log2(f)) == f_log2); |
| Chris Dalton | fc396a8 | 2020-09-23 20:11:26 -0600 | [diff] [blame] | 250 | float q = std::max(1.f, GrWangsFormula::quadratic(kIntolerance, pts)); |
| 251 | REPORTER_ASSERT(r, SkScalarNearlyEqual(q/f, 1, kTessellationTolerance)); |
| Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 252 | }; |
| 253 | |
| 254 | SkRandom rand; |
| 255 | |
| 256 | for_random_matrices(&rand, [&](const SkMatrix& m) { |
| 257 | SkPoint pts[4]; |
| 258 | m.mapPoints(pts, kSerp, 4); |
| 259 | check_cubic_log2(pts); |
| 260 | |
| 261 | m.mapPoints(pts, kLoop, 4); |
| 262 | check_cubic_log2(pts); |
| 263 | |
| 264 | m.mapPoints(pts, kQuad, 3); |
| 265 | check_quadratic_log2(pts); |
| 266 | }); |
| 267 | |
| 268 | for_random_beziers(4, &rand, [&](const SkPoint pts[]) { |
| 269 | check_cubic_log2(pts); |
| 270 | }); |
| 271 | |
| 272 | for_random_beziers(3, &rand, [&](const SkPoint pts[]) { |
| 273 | check_quadratic_log2(pts); |
| 274 | }); |
| 275 | } |
| 276 | |
| 277 | // Ensure using transformations gives the same result as pre-transforming all points. |
| 278 | DEF_TEST(WangsFormula_vectorXforms, r) { |
| 279 | auto check_cubic_log2_with_transform = [&](const SkPoint* pts, const SkMatrix& m){ |
| 280 | SkPoint ptsXformed[4]; |
| 281 | m.mapPoints(ptsXformed, pts, 4); |
| 282 | int expected = GrWangsFormula::cubic_log2(kIntolerance, ptsXformed); |
| 283 | int actual = GrWangsFormula::cubic_log2(kIntolerance, pts, GrVectorXform(m)); |
| 284 | REPORTER_ASSERT(r, actual == expected); |
| 285 | }; |
| 286 | |
| 287 | auto check_quadratic_log2_with_transform = [&](const SkPoint* pts, const SkMatrix& m) { |
| 288 | SkPoint ptsXformed[3]; |
| 289 | m.mapPoints(ptsXformed, pts, 3); |
| 290 | int expected = GrWangsFormula::quadratic_log2(kIntolerance, ptsXformed); |
| 291 | int actual = GrWangsFormula::quadratic_log2(kIntolerance, pts, GrVectorXform(m)); |
| 292 | REPORTER_ASSERT(r, actual == expected); |
| 293 | }; |
| 294 | |
| 295 | SkRandom rand; |
| 296 | |
| 297 | for_random_matrices(&rand, [&](const SkMatrix& m) { |
| 298 | check_cubic_log2_with_transform(kSerp, m); |
| 299 | check_cubic_log2_with_transform(kLoop, m); |
| 300 | check_quadratic_log2_with_transform(kQuad, m); |
| 301 | |
| 302 | for_random_beziers(4, &rand, [&](const SkPoint pts[]) { |
| 303 | check_cubic_log2_with_transform(pts, m); |
| 304 | }); |
| 305 | |
| 306 | for_random_beziers(3, &rand, [&](const SkPoint pts[]) { |
| 307 | check_quadratic_log2_with_transform(pts, m); |
| 308 | }); |
| 309 | }); |
| Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 310 | } |
| Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 311 | |
| Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 312 | DEF_TEST(WangsFormula_worst_case_cubic, r) { |
| 313 | { |
| 314 | SkPoint worstP[] = {{0,0}, {100,100}, {0,0}, {0,0}}; |
| 315 | REPORTER_ASSERT(r, GrWangsFormula::worst_case_cubic(kIntolerance, 100, 100) == |
| Chris Dalton | fc396a8 | 2020-09-23 20:11:26 -0600 | [diff] [blame] | 316 | wangs_formula_cubic_reference_impl(kIntolerance, worstP)); |
| Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 317 | REPORTER_ASSERT(r, GrWangsFormula::worst_case_cubic_log2(kIntolerance, 100, 100) == |
| 318 | GrWangsFormula::cubic_log2(kIntolerance, worstP)); |
| 319 | } |
| 320 | { |
| 321 | SkPoint worstP[] = {{100,100}, {100,100}, {200,200}, {100,100}}; |
| 322 | REPORTER_ASSERT(r, GrWangsFormula::worst_case_cubic(kIntolerance, 100, 100) == |
| Chris Dalton | fc396a8 | 2020-09-23 20:11:26 -0600 | [diff] [blame] | 323 | wangs_formula_cubic_reference_impl(kIntolerance, worstP)); |
| Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 324 | REPORTER_ASSERT(r, GrWangsFormula::worst_case_cubic_log2(kIntolerance, 100, 100) == |
| 325 | GrWangsFormula::cubic_log2(kIntolerance, worstP)); |
| 326 | } |
| 327 | auto check_worst_case_cubic = [&](const SkPoint* pts) { |
| 328 | SkRect bbox; |
| 329 | bbox.setBoundsNoCheck(pts, 4); |
| 330 | float worst = GrWangsFormula::worst_case_cubic(kIntolerance, bbox.width(), bbox.height()); |
| 331 | int worst_log2 = GrWangsFormula::worst_case_cubic_log2(kIntolerance, bbox.width(), |
| 332 | bbox.height()); |
| Chris Dalton | fc396a8 | 2020-09-23 20:11:26 -0600 | [diff] [blame] | 333 | float actual = wangs_formula_cubic_reference_impl(kIntolerance, pts); |
| Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 334 | REPORTER_ASSERT(r, worst >= actual); |
| 335 | REPORTER_ASSERT(r, std::ceil(std::log2(std::max(1.f, worst))) == worst_log2); |
| Chris Dalton | b96995d | 2020-06-04 16:44:29 -0600 | [diff] [blame] | 336 | }; |
| 337 | SkRandom rand; |
| 338 | for (int i = 0; i < 100; ++i) { |
| 339 | for_random_beziers(4, &rand, [&](const SkPoint pts[]) { |
| 340 | check_worst_case_cubic(pts); |
| 341 | }); |
| 342 | } |
| Chris Dalton | f6bf516 | 2020-05-13 19:18:46 -0600 | [diff] [blame] | 343 | } |
| Tyler Denniston | 4be95c5 | 2020-12-02 14:22:12 -0500 | [diff] [blame] | 344 | |
| 345 | // Ensure Wang's formula for quads produces max error within tolerance. |
| 346 | DEF_TEST(WangsFormula_quad_within_tol, r) { |
| 347 | // Wang's formula and the quad math starts to lose precision with very large |
| 348 | // coordinate values, so limit the magnitude a bit to prevent test failures |
| 349 | // due to loss of precision. |
| 350 | constexpr int maxExponent = 15; |
| 351 | SkRandom rand; |
| 352 | for_random_beziers(3, &rand, [&r](const SkPoint pts[]) { |
| 353 | const int nsegs = static_cast<int>( |
| 354 | std::ceil(wangs_formula_quadratic_reference_impl(kIntolerance, pts))); |
| 355 | |
| 356 | const float tdelta = 1.f / nsegs; |
| 357 | for (int j = 0; j < nsegs; ++j) { |
| 358 | const float tmin = j * tdelta, tmax = (j + 1) * tdelta; |
| 359 | |
| 360 | // Get section of quad in [tmin,tmax] |
| 361 | const SkPoint* sectionPts; |
| 362 | SkPoint tmp0[5]; |
| 363 | SkPoint tmp1[5]; |
| 364 | if (tmin == 0) { |
| 365 | if (tmax == 1) { |
| 366 | sectionPts = pts; |
| 367 | } else { |
| 368 | SkChopQuadAt(pts, tmp0, tmax); |
| 369 | sectionPts = tmp0; |
| 370 | } |
| 371 | } else { |
| 372 | SkChopQuadAt(pts, tmp0, tmin); |
| 373 | if (tmax == 1) { |
| 374 | sectionPts = tmp0 + 2; |
| 375 | } else { |
| 376 | SkChopQuadAt(tmp0 + 2, tmp1, (tmax - tmin) / (1 - tmin)); |
| 377 | sectionPts = tmp1; |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | // For quads, max distance from baseline is always at t=0.5. |
| 382 | SkPoint p; |
| 383 | p = SkEvalQuadAt(sectionPts, 0.5f); |
| 384 | |
| 385 | // Get distance of p to baseline |
| 386 | const SkPoint n = {sectionPts[2].fY - sectionPts[0].fY, |
| 387 | sectionPts[0].fX - sectionPts[2].fX}; |
| 388 | const float d = std::abs((p - sectionPts[0]).dot(n)) / n.length(); |
| 389 | |
| 390 | // Check distance is within specified tolerance |
| 391 | REPORTER_ASSERT(r, d <= (1.f / kIntolerance) + SK_ScalarNearlyZero); |
| 392 | } |
| 393 | }, maxExponent); |
| 394 | } |
| Tyler Denniston | 573923c | 2020-12-10 11:47:40 -0500 | [diff] [blame] | 395 | |
| 396 | // Ensure the specialized version for rational quads reduces to regular Wang's |
| 397 | // formula when all weights are equal to one |
| 398 | DEF_TEST(WangsFormula_rational_quad_reduces, r) { |
| 399 | constexpr static float kTessellationTolerance = 1 / 128.f; |
| 400 | |
| 401 | SkRandom rand; |
| 402 | for (int i = 0; i < 100; ++i) { |
| 403 | for_random_beziers(3, &rand, [&r](const SkPoint pts[]) { |
| 404 | const float rational_nsegs = wangs_formula_conic_reference_impl(kIntolerance, pts, 1.f); |
| 405 | const float integral_nsegs = wangs_formula_quadratic_reference_impl(kIntolerance, pts); |
| 406 | REPORTER_ASSERT( |
| 407 | r, SkScalarNearlyEqual(rational_nsegs, integral_nsegs, kTessellationTolerance)); |
| 408 | }); |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | // Ensure the rational quad version (used for conics) produces max error within tolerance. |
| 413 | DEF_TEST(WangsFormula_conic_within_tol, r) { |
| 414 | constexpr int maxExponent = 15; |
| 415 | |
| 416 | SkRandom rand; |
| 417 | for (int i = -10; i <= 10; ++i) { |
| 418 | const float w = std::ldexp(1 + rand.nextF(), i); |
| 419 | for_random_beziers( |
| 420 | 3, &rand, |
| 421 | [&r, w](const SkPoint pts[]) { |
| 422 | const SkPoint projPts[3] = {pts[0], pts[1] * (1.f / w), pts[2]}; |
| 423 | const int nsegs = static_cast<int>(std::ceil( |
| 424 | wangs_formula_conic_reference_impl(kIntolerance, projPts, w))); |
| 425 | |
| 426 | const SkConic conic(projPts[0], projPts[1], projPts[2], w); |
| 427 | const float tdelta = 1.f / nsegs; |
| 428 | for (int j = 0; j < nsegs; ++j) { |
| 429 | const float tmin = j * tdelta, tmax = (j + 1) * tdelta, |
| 430 | tmid = 0.5f * (tmin + tmax); |
| 431 | |
| 432 | SkPoint p0, p1, p2; |
| 433 | conic.evalAt(tmin, &p0); |
| 434 | conic.evalAt(tmid, &p1); |
| 435 | conic.evalAt(tmax, &p2); |
| 436 | |
| 437 | // Get distance of p1 to baseline (p0, p2). |
| 438 | const SkPoint n = {p2.fY - p0.fY, p0.fX - p2.fX}; |
| 439 | SkASSERT(n.length() != 0); |
| 440 | const float d = std::abs((p1 - p0).dot(n)) / n.length(); |
| 441 | |
| 442 | // Check distance is within tolerance |
| 443 | REPORTER_ASSERT(r, d <= (1.f / kIntolerance) + SK_ScalarNearlyZero); |
| 444 | } |
| 445 | }, |
| 446 | maxExponent); |
| 447 | } |
| 448 | } |