2018-08-14 21:21:23 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCubicMap.h"
|
2019-05-06 21:17:19 +00:00
|
|
|
#include "include/core/SkPoint.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "include/private/SkNx.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "src/core/SkGeometry.h"
|
|
|
|
#include "src/pathops/SkPathOpsCubic.h"
|
|
|
|
#include "tests/Test.h"
|
2018-08-15 21:12:08 +00:00
|
|
|
|
|
|
|
static float accurate_t(float A, float B, float C, float D) {
|
|
|
|
double roots[3];
|
|
|
|
SkDEBUGCODE(int count =) SkDCubic::RootsValidT(A, B, C, D, roots);
|
|
|
|
SkASSERT(count == 1);
|
|
|
|
return (float)roots[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
static float accurate_solve(SkPoint p1, SkPoint p2, SkScalar x) {
|
|
|
|
SkPoint array[] = { {0, 0}, p1, p2, {1,1} };
|
|
|
|
SkCubicCoeff coeff(array);
|
|
|
|
|
|
|
|
float t = accurate_t(coeff.fA[0], coeff.fB[0], coeff.fC[0], coeff.fD[0] - x);
|
|
|
|
SkASSERT(t >= 0 && t <= 1);
|
|
|
|
float y = coeff.eval(t)[1];
|
|
|
|
SkASSERT(y >= 0 && y <= 1.0001f);
|
|
|
|
return y;
|
|
|
|
}
|
2018-08-14 21:21:23 +00:00
|
|
|
|
|
|
|
static bool nearly_le(SkScalar a, SkScalar b) {
|
|
|
|
return a <= b || SkScalarNearlyZero(a - b);
|
|
|
|
}
|
|
|
|
|
2018-08-15 21:12:08 +00:00
|
|
|
static void exercise_cubicmap(SkPoint p1, SkPoint p2, skiatest::Reporter* reporter) {
|
|
|
|
const SkScalar MAX_SOLVER_ERR = 0.008f; // found by running w/ current impl
|
|
|
|
|
|
|
|
SkCubicMap cmap(p1, p2);
|
|
|
|
|
2018-08-14 21:21:23 +00:00
|
|
|
SkScalar prev_y = 0;
|
2018-08-15 21:12:08 +00:00
|
|
|
SkScalar dx = 1.0f / 512;
|
|
|
|
for (SkScalar x = dx; x < 1; x += dx) {
|
2018-08-14 21:21:23 +00:00
|
|
|
SkScalar y = cmap.computeYFromX(x);
|
2018-08-15 21:12:08 +00:00
|
|
|
// are we valid and (mostly) monotonic?
|
2019-03-29 17:25:41 +00:00
|
|
|
if (!nearly_le(prev_y, y)) {
|
2018-08-14 21:21:23 +00:00
|
|
|
cmap.computeYFromX(x);
|
|
|
|
REPORTER_ASSERT(reporter, false);
|
|
|
|
}
|
|
|
|
prev_y = y;
|
2018-08-15 21:12:08 +00:00
|
|
|
|
|
|
|
// are we close to the "correct" answer?
|
|
|
|
SkScalar yy = accurate_solve(p1, p2, x);
|
|
|
|
SkScalar diff = SkScalarAbs(yy - y);
|
|
|
|
REPORTER_ASSERT(reporter, diff < MAX_SOLVER_ERR);
|
2018-08-14 21:21:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DEF_TEST(CubicMap, r) {
|
|
|
|
const SkScalar values[] = {
|
2018-08-15 21:12:08 +00:00
|
|
|
0, 1, 0.5f, 0.0000001f, 0.999999f,
|
2018-08-14 21:21:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
for (SkScalar x0 : values) {
|
|
|
|
for (SkScalar y0 : values) {
|
|
|
|
for (SkScalar x1 : values) {
|
|
|
|
for (SkScalar y1 : values) {
|
2018-08-15 21:12:08 +00:00
|
|
|
exercise_cubicmap({ x0, y0 }, { x1, y1 }, r);
|
2018-08-14 21:21:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|