blob: 967dfc7f4e44132007dcbc8128777dc202c8ac76 [file] [log] [blame]
caryclark@google.coma5e55922013-05-07 18:51:31 +00001/*
2 * Copyright 2013 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 */
caryclark@google.com8d0a5242013-07-16 16:11:16 +00007#include "PathOpsTestCommon.h"
caryclark@google.coma5e55922013-05-07 18:51:31 +00008#include "SkIntersections.h"
9#include "SkPathOpsCubic.h"
10#include "SkPathOpsQuad.h"
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000011#include "SkRandom.h"
caryclark@google.coma5e55922013-05-07 18:51:31 +000012#include "SkReduceOrder.h"
13#include "Test.h"
14
caryclarke4097e32014-06-18 07:24:19 -070015static struct quadCubic {
caryclark@google.coma5e55922013-05-07 18:51:31 +000016 SkDCubic cubic;
17 SkDQuad quad;
18 int answerCount;
19 SkDPoint answers[2];
20} quadCubicTests[] = {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000021#if 0 // FIXME : this should not fail (root problem behind skpcarrot_is24 )
22 {{{{1020.08099,672.161987}, {1020.08002,630.73999}, {986.502014,597.161987}, {945.080994,597.161987}}},
23 {{{1020,672}, {1020,640.93396}, {998.03302,618.96698}}}, 1,
24 {{1019.421, 662.449}}},
25#endif
26
skia.committer@gmail.comf54ad6f2013-11-02 07:02:02 +000027 {{{{778, 14089}, {778, 14091.208984375}, {776.20916748046875, 14093}, {774, 14093}}},
28 {{{778, 14089}, {777.99957275390625, 14090.65625}, {776.82843017578125, 14091.828125}}}, 2,
29 {{778, 14089}, {776.82855609581270,14091.828250841330}}},
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +000030
31 {{{{1110, 817}, {1110.55225f, 817}, {1111, 817.447693f}, {1111, 818}}},
32 {{{1110.70715f, 817.292908f}, {1110.41406f, 817.000122f}, {1110, 817}}}, 2,
33 {{1110, 817}, {1110.70715f, 817.292908f}}},
34
35 {{{{1110, 817}, {1110.55225f, 817}, {1111, 817.447693f}, {1111, 818}}},
36 {{{1111, 818}, {1110.99988f, 817.585876f}, {1110.70715f, 817.292908f}}}, 2,
37 {{1110.70715f, 817.292908f}, {1111, 818}}},
38
39 {{{{55, 207}, {52.238574981689453, 207}, {50, 204.76142883300781}, {50, 202}}},
40 {{{55, 207}, {52.929431915283203, 206.99949645996094},
41 {51.464466094970703, 205.53553771972656}}}, 2,
42 {{55, 207}, {51.464466094970703, 205.53553771972656}}},
43
skia.committer@gmail.comb0a05892013-10-03 07:01:37 +000044 {{{{49, 47}, {49, 74.614250183105469}, {26.614250183105469, 97}, {-1, 97}}},
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000045 {{{-8.659739592076221e-015, 96.991401672363281}, {20.065492630004883, 96.645187377929688},
46 {34.355339050292969, 82.355339050292969}}}, 2,
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +000047 {{34.355339050292969,82.355339050292969}, {34.28654835573549, 82.424006509351585}}},
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000048
skia.committer@gmail.com2b34fe02013-05-08 07:01:40 +000049 {{{{10,234}, {10,229.58172607421875}, {13.581720352172852,226}, {18,226}}},
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000050 {{{18,226}, {14.686291694641113,226}, {12.342399597167969,228.3424072265625}}}, 1,
51 {{18,226}, {0,0}}},
caryclark@google.coma2bbc6e2013-11-01 17:36:03 +000052
skia.committer@gmail.com2b34fe02013-05-08 07:01:40 +000053 {{{{10,234}, {10,229.58172607421875}, {13.581720352172852,226}, {18,226}}},
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000054 {{{12.342399597167969,228.3424072265625}, {10,230.68629455566406}, {10,234}}}, 1,
55 {{10,234}, {0,0}}},
caryclark@google.coma5e55922013-05-07 18:51:31 +000056};
57
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000058static const int quadCubicTests_count = (int) SK_ARRAY_COUNT(quadCubicTests);
59
60static void cubicQuadIntersection(skiatest::Reporter* reporter, int index) {
61 int iIndex = static_cast<int>(index);
62 const SkDCubic& cubic = quadCubicTests[index].cubic;
63 SkASSERT(ValidCubic(cubic));
64 const SkDQuad& quad = quadCubicTests[index].quad;
65 SkASSERT(ValidQuad(quad));
66 SkReduceOrder reduce1;
67 SkReduceOrder reduce2;
68 int order1 = reduce1.reduce(cubic, SkReduceOrder::kNo_Quadratics);
69 int order2 = reduce2.reduce(quad);
70 if (order1 != 4) {
71 SkDebugf("[%d] cubic order=%d\n", iIndex, order1);
72 REPORTER_ASSERT(reporter, 0);
73 }
74 if (order2 != 3) {
75 SkDebugf("[%d] quad order=%d\n", iIndex, order2);
76 REPORTER_ASSERT(reporter, 0);
77 }
78 SkIntersections i;
79 int roots = i.intersect(cubic, quad);
80 SkASSERT(roots == quadCubicTests[index].answerCount);
81 for (int pt = 0; pt < roots; ++pt) {
82 double tt1 = i[0][pt];
83 SkDPoint xy1 = cubic.ptAtT(tt1);
84 double tt2 = i[1][pt];
85 SkDPoint xy2 = quad.ptAtT(tt2);
86 if (!xy1.approximatelyEqual(xy2)) {
87 SkDebugf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
88 __FUNCTION__, iIndex, pt, tt1, xy1.fX, xy1.fY, tt2, xy2.fX, xy2.fY);
89 }
90 REPORTER_ASSERT(reporter, xy1.approximatelyEqual(xy2));
91 bool found = false;
92 for (int idx2 = 0; idx2 < quadCubicTests[index].answerCount; ++idx2) {
93 found |= quadCubicTests[index].answers[idx2].approximatelyEqual(xy1);
94 }
95 if (!found) {
96 SkDebugf("%s [%d,%d] xy1=(%g,%g) != \n",
97 __FUNCTION__, iIndex, pt, xy1.fX, xy1.fY);
98 }
99 REPORTER_ASSERT(reporter, found);
100 }
101 reporter->bumpTestCount();
102}
caryclark@google.coma5e55922013-05-07 18:51:31 +0000103
tfarina@chromium.org78e7b4e2014-01-02 21:45:03 +0000104DEF_TEST(PathOpsCubicQuadIntersection, reporter) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000105 for (int index = 0; index < quadCubicTests_count; ++index) {
106 cubicQuadIntersection(reporter, index);
caryclark@google.coma5e55922013-05-07 18:51:31 +0000107 reporter->bumpTestCount();
108 }
109}
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000110
111DEF_TEST(PathOpsCubicQuadIntersectionOneOff, reporter) {
112 cubicQuadIntersection(reporter, 0);
113}
114
115static bool gPathOpCubicQuadSlopVerbose = false;
116static const int kCubicToQuadSubdivisionDepth = 8; // slots reserved for cubic to quads subdivision
117
118// determine that slop required after quad/quad finds a candidate intersection
119// use the cross of the tangents plus the distance from 1 or 0 as knobs
120DEF_TEST(PathOpsCubicQuadSlop, reporter) {
121 // create a random non-selfintersecting cubic
122 // break it into quadratics
123 // offset the quadratic, measuring the slop required to find the intersection
124 if (!gPathOpCubicQuadSlopVerbose) { // takes a while to run -- so exclude it by default
125 return;
126 }
127 int results[101];
128 sk_bzero(results, sizeof(results));
129 double minCross[101];
130 sk_bzero(minCross, sizeof(minCross));
131 double maxCross[101];
132 sk_bzero(maxCross, sizeof(maxCross));
133 double sumCross[101];
134 sk_bzero(sumCross, sizeof(sumCross));
135 int foundOne = 0;
136 int slopCount = 1;
137 SkRandom ran;
138 for (int index = 0; index < 10000000; ++index) {
139 if (index % 1000 == 999) SkDebugf(".");
140 SkDCubic cubic = {{
141 {ran.nextRangeF(-1000, 1000), ran.nextRangeF(-1000, 1000)},
142 {ran.nextRangeF(-1000, 1000), ran.nextRangeF(-1000, 1000)},
143 {ran.nextRangeF(-1000, 1000), ran.nextRangeF(-1000, 1000)},
144 {ran.nextRangeF(-1000, 1000), ran.nextRangeF(-1000, 1000)}
145 }};
146 SkIntersections i;
147 if (i.intersect(cubic)) {
148 continue;
149 }
150 SkSTArray<kCubicToQuadSubdivisionDepth, double, true> ts;
151 cubic.toQuadraticTs(cubic.calcPrecision(), &ts);
152 double tStart = 0;
153 int tsCount = ts.count();
154 for (int i1 = 0; i1 <= tsCount; ++i1) {
155 const double tEnd = i1 < tsCount ? ts[i1] : 1;
156 SkDCubic part = cubic.subDivide(tStart, tEnd);
157 SkDQuad quad = part.toQuad();
158 SkReduceOrder reducer;
159 int order = reducer.reduce(quad);
160 if (order != 3) {
161 continue;
162 }
163 for (int i2 = 0; i2 < 100; ++i2) {
164 SkDPoint endDisplacement = {ran.nextRangeF(-100, 100), ran.nextRangeF(-100, 100)};
165 SkDQuad nearby = {{
166 {quad[0].fX + endDisplacement.fX, quad[0].fY + endDisplacement.fY},
167 {quad[1].fX + ran.nextRangeF(-100, 100), quad[1].fY + ran.nextRangeF(-100, 100)},
168 {quad[2].fX - endDisplacement.fX, quad[2].fY - endDisplacement.fY}
169 }};
170 order = reducer.reduce(nearby);
171 if (order != 3) {
172 continue;
173 }
174 SkIntersections locals;
175 locals.allowNear(false);
176 locals.intersect(quad, nearby);
177 if (locals.used() != 1) {
178 continue;
179 }
180 // brute force find actual intersection
181 SkDLine cubicLine = {{ {0, 0}, {cubic[0].fX, cubic[0].fY } }};
182 SkIntersections liner;
183 int i3;
184 int found = -1;
185 int foundErr = true;
186 for (i3 = 1; i3 <= 1000; ++i3) {
187 cubicLine[0] = cubicLine[1];
188 cubicLine[1] = cubic.ptAtT(i3 / 1000.);
189 liner.reset();
190 liner.allowNear(false);
191 liner.intersect(nearby, cubicLine);
192 if (liner.used() == 0) {
193 continue;
194 }
195 if (liner.used() > 1) {
196 foundErr = true;
197 break;
198 }
199 if (found > 0) {
200 foundErr = true;
201 break;
202 }
203 foundErr = false;
204 found = i3;
205 }
206 if (foundErr) {
207 continue;
208 }
209 SkDVector dist = liner.pt(0) - locals.pt(0);
210 SkDVector qV = nearby.dxdyAtT(locals[0][0]);
211 double cubicT = (found - 1 + liner[1][0]) / 1000.;
212 SkDVector cV = cubic.dxdyAtT(cubicT);
213 double qxc = qV.crossCheck(cV);
214 double qvLen = qV.length();
215 double cvLen = cV.length();
216 double maxLen = SkTMax(qvLen, cvLen);
217 qxc /= maxLen;
218 double quadT = tStart + (tEnd - tStart) * locals[0][0];
219 double diffT = fabs(cubicT - quadT);
220 int diffIdx = (int) (diffT * 100);
221 results[diffIdx]++;
222 double absQxc = fabs(qxc);
223 if (sumCross[diffIdx] == 0) {
224 minCross[diffIdx] = maxCross[diffIdx] = sumCross[diffIdx] = absQxc;
225 } else {
226 minCross[diffIdx] = SkTMin(minCross[diffIdx], absQxc);
227 maxCross[diffIdx] = SkTMax(maxCross[diffIdx], absQxc);
228 sumCross[diffIdx] += absQxc;
229 }
230 if (diffIdx >= 20) {
231#if 01
232 SkDebugf("cubic={{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}}}"
233 " quad={{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}}}"
234 " {{{%1.9g,%1.9g}, {%1.9g,%1.9g}}}"
skia.committer@gmail.coma1ed7ae2014-04-15 03:04:18 +0000235 " qT=%1.9g cT=%1.9g dist=%1.9g cross=%1.9g\n",
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000236 cubic[0].fX, cubic[0].fY, cubic[1].fX, cubic[1].fY,
237 cubic[2].fX, cubic[2].fY, cubic[3].fX, cubic[3].fY,
238 nearby[0].fX, nearby[0].fY, nearby[1].fX, nearby[1].fY,
skia.committer@gmail.coma1ed7ae2014-04-15 03:04:18 +0000239 nearby[2].fX, nearby[2].fY,
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000240 liner.pt(0).fX, liner.pt(0).fY,
241 locals.pt(0).fX, locals.pt(0).fY, quadT, cubicT, dist.length(), qxc);
242#else
243 SkDebugf("qT=%1.9g cT=%1.9g dist=%1.9g cross=%1.9g\n",
244 quadT, cubicT, dist.length(), qxc);
245 SkDebugf("<div id=\"slop%d\">\n", ++slopCount);
246 SkDebugf("{{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}}}\n"
247 "{{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}}}\n"
248 "{{{%1.9g,%1.9g}, {%1.9g,%1.9g}}}\n",
249 cubic[0].fX, cubic[0].fY, cubic[1].fX, cubic[1].fY,
250 cubic[2].fX, cubic[2].fY, cubic[3].fX, cubic[3].fY,
251 nearby[0].fX, nearby[0].fY, nearby[1].fX, nearby[1].fY,
skia.committer@gmail.coma1ed7ae2014-04-15 03:04:18 +0000252 nearby[2].fX, nearby[2].fY,
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000253 liner.pt(0).fX, liner.pt(0).fY,
254 locals.pt(0).fX, locals.pt(0).fY);
255 SkDebugf("</div>\n\n");
256#endif
257 }
258 ++foundOne;
259 }
260 tStart = tEnd;
261 }
262 if (++foundOne >= 100000) {
263 break;
264 }
265 }
266#if 01
267 SkDebugf("slopCount=%d\n", slopCount);
268 int max = 100;
269 while (results[max] == 0) {
270 --max;
271 }
272 for (int i = 0; i <= max; ++i) {
273 if (i > 0 && i % 10 == 0) {
274 SkDebugf("\n");
275 }
276 SkDebugf("%d ", results[i]);
277 }
278 SkDebugf("min\n");
279 for (int i = 0; i <= max; ++i) {
280 if (i > 0 && i % 10 == 0) {
281 SkDebugf("\n");
282 }
283 SkDebugf("%1.9g ", minCross[i]);
284 }
285 SkDebugf("max\n");
286 for (int i = 0; i <= max; ++i) {
287 if (i > 0 && i % 10 == 0) {
288 SkDebugf("\n");
289 }
290 SkDebugf("%1.9g ", maxCross[i]);
291 }
292 SkDebugf("avg\n");
293 for (int i = 0; i <= max; ++i) {
294 if (i > 0 && i % 10 == 0) {
295 SkDebugf("\n");
296 }
297 SkDebugf("%1.9g ", sumCross[i] / results[i]);
298 }
299#else
300 for (int i = 1; i < slopCount; ++i) {
301 SkDebugf(" slop%d,\n", i);
302 }
303#endif
304 SkDebugf("\n");
305}