blob: 3b223ae89fb600bc90a3fd5df25b218bb1d0835d [file] [log] [blame]
caryclark@google.com9166dcb2013-04-08 11:50:00 +00001/*
2 * Copyright 2012 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#include "SkLineParameters.h"
8#include "Test.h"
9
10// tests to verify that distance calculations are coded correctly
11static const SkDCubic tests[] = {
12 {{{0, 0}, {1, 1}, {2, 2}, {0, 3}}},
13 {{{0, 0}, {1, 1}, {2, 2}, {3, 0}}},
14 {{{0, 0}, {5, 0}, {-2, 4}, {3, 4}}},
15 {{{0, 2}, {1, 0}, {2, 0}, {3, 0}}},
16 {{{0, .2}, {1, 0}, {2, 0}, {3, 0}}},
17 {{{0, .02}, {1, 0}, {2, 0}, {3, 0}}},
18 {{{0, .002}, {1, 0}, {2, 0}, {3, 0}}},
19 {{{0, .0002}, {1, 0}, {2, 0}, {3, 0}}},
20 {{{0, .00002}, {1, 0}, {2, 0}, {3, 0}}},
21 {{{0, FLT_EPSILON * 2}, {1, 0}, {2, 0}, {3, 0}}},
22};
23
24static const double answers[][2] = {
25 {1, 2},
26 {1, 2},
27 {4, 4},
28 {1.1094003924, 0.5547001962},
29 {0.133038021, 0.06651901052},
30 {0.0133330370, 0.006666518523},
31 {0.001333333037, 0.0006666665185},
32 {0.000133333333, 6.666666652e-05},
33 {1.333333333e-05, 6.666666667e-06},
34 {1.5894571940104115e-07, 7.9472859700520577e-08},
35};
36
caryclark@google.comad65a3e2013-04-15 19:13:59 +000037static const size_t tests_count = SK_ARRAY_COUNT(tests);
caryclark@google.com9166dcb2013-04-08 11:50:00 +000038
caryclark@google.comad65a3e2013-04-15 19:13:59 +000039static void PathOpsLineParametersTest(skiatest::Reporter* reporter) {
caryclark@google.com9166dcb2013-04-08 11:50:00 +000040 for (size_t index = 0; index < tests_count; ++index) {
41 SkLineParameters lineParameters;
42 const SkDCubic& cubic = tests[index];
caryclark@google.comcffbcc32013-06-04 17:59:42 +000043 lineParameters.cubicEndPoints(cubic, 0, 3);
caryclark@google.com9166dcb2013-04-08 11:50:00 +000044 double denormalizedDistance[2];
45 denormalizedDistance[0] = lineParameters.controlPtDistance(cubic, 1);
46 denormalizedDistance[1] = lineParameters.controlPtDistance(cubic, 2);
47 double normalSquared = lineParameters.normalSquared();
48 size_t inner;
49 for (inner = 0; inner < 2; ++inner) {
50 double distSq = denormalizedDistance[inner];
51 distSq *= distSq;
52 double answersSq = answers[index][inner];
53 answersSq *= answersSq;
54 if (AlmostEqualUlps(distSq, normalSquared * answersSq)) {
55 continue;
56 }
57 SkDebugf("%s [%d,%d] denormalizedDistance:%g != answer:%g"
58 " distSq:%g answerSq:%g normalSquared:%g\n",
59 __FUNCTION__, static_cast<int>(index), (int)inner,
60 denormalizedDistance[inner], answers[index][inner],
61 distSq, answersSq, normalSquared);
62 }
63 lineParameters.normalize();
64 double normalizedDistance[2];
65 normalizedDistance[0] = lineParameters.controlPtDistance(cubic, 1);
66 normalizedDistance[1] = lineParameters.controlPtDistance(cubic, 2);
67 for (inner = 0; inner < 2; ++inner) {
68 if (AlmostEqualUlps(fabs(normalizedDistance[inner]), answers[index][inner])) {
69 continue;
70 }
71 SkDebugf("%s [%d,%d] normalizedDistance:%1.10g != answer:%g\n",
72 __FUNCTION__, static_cast<int>(index), (int)inner,
73 normalizedDistance[inner], answers[index][inner]);
74 REPORTER_ASSERT(reporter, 0);
75 }
76 }
77}
78
79#include "TestClassDef.h"
caryclark@google.comad65a3e2013-04-15 19:13:59 +000080DEFINE_TESTCLASS_SHORT(PathOpsLineParametersTest)