blob: 4c0f414ad77ab286b79c053d9e2204ff66d43cae [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 "PathOpsCubicIntersectionTestData.h"
8#include "PathOpsQuadIntersectionTestData.h"
9#include "PathOpsTestCommon.h"
10#include "SkGeometry.h"
11#include "SkIntersections.h"
12#include "SkPathOpsRect.h"
13#include "SkReduceOrder.h"
14#include "Test.h"
tfarina@chromium.org78e7b4e2014-01-02 21:45:03 +000015#include "TestClassDef.h"
caryclark@google.com9166dcb2013-04-08 11:50:00 +000016
17static void test(skiatest::Reporter* reporter, const SkDCubic* cubics, const char* name,
18 int firstTest, size_t testCount) {
19 for (size_t index = firstTest; index < testCount; ++index) {
20 const SkDCubic& cubic = cubics[index];
caryclark@google.com8d0a5242013-07-16 16:11:16 +000021 SkASSERT(ValidCubic(cubic));
caryclark@google.com9166dcb2013-04-08 11:50:00 +000022 double precision = cubic.calcPrecision();
caryclark@google.comd892bd82013-06-17 14:10:36 +000023 SkTArray<SkDQuad, true> quads;
caryclark@google.com9166dcb2013-04-08 11:50:00 +000024 CubicToQuads(cubic, precision, quads);
caryclark@google.comcffbcc32013-06-04 17:59:42 +000025 if (quads.count() != 1 && quads.count() != 2) {
caryclark@google.com9166dcb2013-04-08 11:50:00 +000026 SkDebugf("%s [%d] cubic to quadratics failed count=%d\n", name, static_cast<int>(index),
27 quads.count());
28 }
29 REPORTER_ASSERT(reporter, quads.count() == 1);
30 }
31}
32
33static void test(skiatest::Reporter* reporter, const SkDQuad* quadTests, const char* name,
34 int firstTest, size_t testCount) {
35 for (size_t index = firstTest; index < testCount; ++index) {
36 const SkDQuad& quad = quadTests[index];
caryclark@google.com8d0a5242013-07-16 16:11:16 +000037 SkASSERT(ValidQuad(quad));
caryclark@google.com9166dcb2013-04-08 11:50:00 +000038 SkDCubic cubic = quad.toCubic();
39 double precision = cubic.calcPrecision();
caryclark@google.comd892bd82013-06-17 14:10:36 +000040 SkTArray<SkDQuad, true> quads;
caryclark@google.com9166dcb2013-04-08 11:50:00 +000041 CubicToQuads(cubic, precision, quads);
caryclark@google.comcffbcc32013-06-04 17:59:42 +000042 if (quads.count() != 1 && quads.count() != 2) {
caryclark@google.com9166dcb2013-04-08 11:50:00 +000043 SkDebugf("%s [%d] cubic to quadratics failed count=%d\n", name, static_cast<int>(index),
44 quads.count());
45 }
caryclark@google.comcffbcc32013-06-04 17:59:42 +000046 REPORTER_ASSERT(reporter, quads.count() <= 2);
caryclark@google.com9166dcb2013-04-08 11:50:00 +000047 }
48}
49
50static void testC(skiatest::Reporter* reporter, const SkDCubic* cubics, const char* name,
51 int firstTest, size_t testCount) {
52 // test if computed line end points are valid
53 for (size_t index = firstTest; index < testCount; ++index) {
54 const SkDCubic& cubic = cubics[index];
caryclark@google.com8d0a5242013-07-16 16:11:16 +000055 SkASSERT(ValidCubic(cubic));
caryclark@google.com9166dcb2013-04-08 11:50:00 +000056 double precision = cubic.calcPrecision();
caryclark@google.comd892bd82013-06-17 14:10:36 +000057 SkTArray<SkDQuad, true> quads;
caryclark@google.com9166dcb2013-04-08 11:50:00 +000058 CubicToQuads(cubic, precision, quads);
59 if (!AlmostEqualUlps(cubic[0].fX, quads[0][0].fX)
60 || !AlmostEqualUlps(cubic[0].fY, quads[0][0].fY)) {
61 SkDebugf("[%d] unmatched start\n", static_cast<int>(index));
62 REPORTER_ASSERT(reporter, 0);
63 }
64 int last = quads.count() - 1;
65 if (!AlmostEqualUlps(cubic[3].fX, quads[last][2].fX)
66 || !AlmostEqualUlps(cubic[3].fY, quads[last][2].fY)) {
67 SkDebugf("[%d] unmatched end\n", static_cast<int>(index));
68 REPORTER_ASSERT(reporter, 0);
69 }
70 }
71}
72
73static void testC(skiatest::Reporter* reporter, const SkDCubic(* cubics)[2], const char* name,
74 int firstTest, size_t testCount) {
75 for (size_t index = firstTest; index < testCount; ++index) {
76 for (int idx2 = 0; idx2 < 2; ++idx2) {
77 const SkDCubic& cubic = cubics[index][idx2];
caryclark@google.com8d0a5242013-07-16 16:11:16 +000078 SkASSERT(ValidCubic(cubic));
caryclark@google.com9166dcb2013-04-08 11:50:00 +000079 double precision = cubic.calcPrecision();
caryclark@google.comd892bd82013-06-17 14:10:36 +000080 SkTArray<SkDQuad, true> quads;
caryclark@google.com9166dcb2013-04-08 11:50:00 +000081 CubicToQuads(cubic, precision, quads);
82 if (!AlmostEqualUlps(cubic[0].fX, quads[0][0].fX)
83 || !AlmostEqualUlps(cubic[0].fY, quads[0][0].fY)) {
84 SkDebugf("[%d][%d] unmatched start\n", static_cast<int>(index), idx2);
85 REPORTER_ASSERT(reporter, 0);
86 }
87 int last = quads.count() - 1;
88 if (!AlmostEqualUlps(cubic[3].fX, quads[last][2].fX)
89 || !AlmostEqualUlps(cubic[3].fY, quads[last][2].fY)) {
90 SkDebugf("[%d][%d] unmatched end\n", static_cast<int>(index), idx2);
91 REPORTER_ASSERT(reporter, 0);
92 }
93 }
94 }
95}
96
tfarina@chromium.org78e7b4e2014-01-02 21:45:03 +000097DEF_TEST(CubicToQuads, reporter) {
caryclark@google.com9166dcb2013-04-08 11:50:00 +000098 enum {
99 RunAll,
100 RunPointDegenerates,
101 RunNotPointDegenerates,
102 RunLines,
103 RunNotLines,
104 RunModEpsilonLines,
105 RunLessEpsilonLines,
106 RunNegEpsilonLines,
107 RunQuadraticLines,
108 RunQuadraticModLines,
109 RunComputedLines,
110 RunComputedTests,
111 RunNone
112 } run = RunAll;
113 int firstTestIndex = 0;
114#if 0
115 run = RunComputedLines;
116 firstTestIndex = 18;
117#endif
118 int firstPointDegeneratesTest = run == RunAll ? 0 : run == RunPointDegenerates
119 ? firstTestIndex : SK_MaxS32;
120 int firstNotPointDegeneratesTest = run == RunAll ? 0 : run == RunNotPointDegenerates
121 ? firstTestIndex : SK_MaxS32;
122 int firstLinesTest = run == RunAll ? 0 : run == RunLines ? firstTestIndex : SK_MaxS32;
123 int firstNotLinesTest = run == RunAll ? 0 : run == RunNotLines ? firstTestIndex : SK_MaxS32;
124 int firstModEpsilonTest = run == RunAll ? 0 : run == RunModEpsilonLines
125 ? firstTestIndex : SK_MaxS32;
126 int firstLessEpsilonTest = run == RunAll ? 0 : run == RunLessEpsilonLines
127 ? firstTestIndex : SK_MaxS32;
128 int firstNegEpsilonTest = run == RunAll ? 0 : run == RunNegEpsilonLines
129 ? firstTestIndex : SK_MaxS32;
130 int firstQuadraticLineTest = run == RunAll ? 0 : run == RunQuadraticLines
131 ? firstTestIndex : SK_MaxS32;
132 int firstQuadraticModLineTest = run == RunAll ? 0 : run == RunQuadraticModLines
133 ? firstTestIndex : SK_MaxS32;
134 int firstComputedLinesTest = run == RunAll ? 0 : run == RunComputedLines
135 ? firstTestIndex : SK_MaxS32;
136 int firstComputedCubicsTest = run == RunAll ? 0 : run == RunComputedTests
137 ? firstTestIndex : SK_MaxS32;
138
139 test(reporter, pointDegenerates, "pointDegenerates", firstPointDegeneratesTest,
140 pointDegenerates_count);
141 testC(reporter, notPointDegenerates, "notPointDegenerates", firstNotPointDegeneratesTest,
142 notPointDegenerates_count);
143 test(reporter, lines, "lines", firstLinesTest, lines_count);
144 testC(reporter, notLines, "notLines", firstNotLinesTest, notLines_count);
145 testC(reporter, modEpsilonLines, "modEpsilonLines", firstModEpsilonTest, modEpsilonLines_count);
146 test(reporter, lessEpsilonLines, "lessEpsilonLines", firstLessEpsilonTest,
147 lessEpsilonLines_count);
148 test(reporter, negEpsilonLines, "negEpsilonLines", firstNegEpsilonTest, negEpsilonLines_count);
149 test(reporter, quadraticLines, "quadraticLines", firstQuadraticLineTest, quadraticLines_count);
150 test(reporter, quadraticModEpsilonLines, "quadraticModEpsilonLines", firstQuadraticModLineTest,
151 quadraticModEpsilonLines_count);
152 testC(reporter, lines, "computed lines", firstComputedLinesTest, lines_count);
153 testC(reporter, tests, "computed tests", firstComputedCubicsTest, tests_count);
154}
155
156static SkDCubic locals[] = {
157 {{{0, 1}, {1.9274705288631189e-19, 1.0000000000000002},
158 {0.0017190297609673323, 0.99828097023903239},
159 {0.0053709083094631276, 0.99505672974365911}}},
160 {{{14.5975863, 41.632436}, {16.3518929, 26.2639684}, {18.5165519, 7.68775139},
161 {8.03767257, 89.1628526}}},
162 {{{69.7292014, 38.6877352}, {24.7648688, 23.1501713}, {84.9283191, 90.2588441},
163 {80.392774, 61.3533852}}},
164 {{{60.776536520932126, 71.249307306133829}, {87.107894191103014, 22.377669868235323},
165 {1.4974754310666936, 68.069569937917208}, {45.261946574441133, 17.536076632112298}}},
166};
167
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000168static size_t localsCount = SK_ARRAY_COUNT(locals);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000169
170#define DEBUG_CRASH 0
171#define TEST_AVERAGE_END_POINTS 0 // must take const off to test
172extern const bool AVERAGE_END_POINTS;
173
174static void oneOff(skiatest::Reporter* reporter, size_t x) {
175 const SkDCubic& cubic = locals[x];
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000176 SkASSERT(ValidCubic(cubic));
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000177 const SkPoint skcubic[4] = {
178 {static_cast<float>(cubic[0].fX), static_cast<float>(cubic[0].fY)},
179 {static_cast<float>(cubic[1].fX), static_cast<float>(cubic[1].fY)},
180 {static_cast<float>(cubic[2].fX), static_cast<float>(cubic[2].fY)},
181 {static_cast<float>(cubic[3].fX), static_cast<float>(cubic[3].fY)}};
182 SkScalar skinflect[2];
183 int skin = SkFindCubicInflections(skcubic, skinflect);
184 if (false) SkDebugf("%s %d %1.9g\n", __FUNCTION__, skin, skinflect[0]);
caryclark@google.comd892bd82013-06-17 14:10:36 +0000185 SkTArray<SkDQuad, true> quads;
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000186 double precision = cubic.calcPrecision();
187 CubicToQuads(cubic, precision, quads);
188 if (false) SkDebugf("%s quads=%d\n", __FUNCTION__, quads.count());
189}
190
tfarina@chromium.org78e7b4e2014-01-02 21:45:03 +0000191DEF_TEST(CubicsToQuadratics_OneOff_Loop, reporter) {
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000192 for (size_t x = 0; x < localsCount; ++x) {
193 oneOff(reporter, x);
194 }
195}
196
tfarina@chromium.org78e7b4e2014-01-02 21:45:03 +0000197DEF_TEST(CubicsToQuadratics_OneOff_Single, reporter) {
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000198 oneOff(reporter, 0);
199}