blob: 04797b4ef3341dd4b9b0fb222a760efe2e0721a0 [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 "PathOpsTestCommon.h"
9#include "SkIntersections.h"
10#include "SkPathOpsRect.h"
11#include "SkReduceOrder.h"
12#include "Test.h"
13
14const int firstCubicIntersectionTest = 9;
15
16static void standardTestCases(skiatest::Reporter* reporter) {
17 for (size_t index = firstCubicIntersectionTest; index < tests_count; ++index) {
18 int iIndex = static_cast<int>(index);
19 const SkDCubic& cubic1 = tests[index][0];
20 const SkDCubic& cubic2 = tests[index][1];
21 SkReduceOrder reduce1, reduce2;
22 int order1 = reduce1.reduce(cubic1, SkReduceOrder::kNo_Quadratics,
23 SkReduceOrder::kFill_Style);
24 int order2 = reduce2.reduce(cubic2, SkReduceOrder::kNo_Quadratics,
25 SkReduceOrder::kFill_Style);
26 const bool showSkipped = false;
27 if (order1 < 4) {
28 if (showSkipped) {
29 SkDebugf("%s [%d] cubic1 order=%d\n", __FUNCTION__, iIndex, order1);
30 }
31 continue;
32 }
33 if (order2 < 4) {
34 if (showSkipped) {
35 SkDebugf("%s [%d] cubic2 order=%d\n", __FUNCTION__, iIndex, order2);
36 }
37 continue;
38 }
39 SkIntersections tIntersections;
40 tIntersections.intersect(cubic1, cubic2);
41 if (!tIntersections.used()) {
42 if (showSkipped) {
43 SkDebugf("%s [%d] no intersection\n", __FUNCTION__, iIndex);
44 }
45 continue;
46 }
47 if (tIntersections.isCoincident(0)) {
48 if (showSkipped) {
49 SkDebugf("%s [%d] coincident\n", __FUNCTION__, iIndex);
50 }
51 continue;
52 }
53 for (int pt = 0; pt < tIntersections.used(); ++pt) {
54 double tt1 = tIntersections[0][pt];
caryclark@google.com4fdbb222013-07-23 15:27:41 +000055 SkDPoint xy1 = cubic1.ptAtT(tt1);
caryclark@google.com9166dcb2013-04-08 11:50:00 +000056 double tt2 = tIntersections[1][pt];
caryclark@google.com4fdbb222013-07-23 15:27:41 +000057 SkDPoint xy2 = cubic2.ptAtT(tt2);
caryclark@google.com9166dcb2013-04-08 11:50:00 +000058 if (!xy1.approximatelyEqual(xy2)) {
59 SkDebugf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
60 __FUNCTION__, (int)index, pt, tt1, xy1.fX, xy1.fY, tt2, xy2.fX, xy2.fY);
61 }
62 REPORTER_ASSERT(reporter, xy1.approximatelyEqual(xy2));
63 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +000064 reporter->bumpTestCount();
caryclark@google.com9166dcb2013-04-08 11:50:00 +000065 }
66}
67
68static const SkDCubic testSet[] = {
69// FIXME: uncommenting these two will cause this to fail
70// this results in two curves very nearly but not exactly coincident
71#if 0
72{{{67.426548091427676, 37.993772624988935}, {23.483695892376684, 90.476863174921306},
73 {35.597065061143162, 79.872482633158796}, {75.38634169631932, 18.244890038969412}}},
74{{{67.4265481, 37.9937726}, {23.4836959, 90.4768632}, {35.5970651, 79.8724826},
75 {75.3863417, 18.24489}}},
76#endif
77
78{{{0, 0}, {0, 1}, {1, 1}, {1, 0}}},
79{{{1, 0}, {0, 0}, {0, 1}, {1, 1}}},
80
81{{{0, 1}, {4, 5}, {1, 0}, {5, 3}}},
82{{{0, 1}, {3, 5}, {1, 0}, {5, 4}}},
83
84{{{0, 1}, {1, 6}, {1, 0}, {1, 0}}},
85{{{0, 1}, {0, 1}, {1, 0}, {6, 1}}},
86
87{{{0, 1}, {3, 4}, {1, 0}, {5, 1}}},
88{{{0, 1}, {1, 5}, {1, 0}, {4, 3}}},
89
90{{{0, 1}, {1, 2}, {1, 0}, {6, 1}}},
91{{{0, 1}, {1, 6}, {1, 0}, {2, 1}}},
92
93{{{0, 1}, {0, 5}, {1, 0}, {4, 0}}},
94{{{0, 1}, {0, 4}, {1, 0}, {5, 0}}},
95
96{{{0, 1}, {3, 4}, {1, 0}, {3, 0}}},
97{{{0, 1}, {0, 3}, {1, 0}, {4, 3}}},
98
99{{{0, 0}, {1, 2}, {3, 4}, {4, 4}}},
100{{{0, 0}, {1, 2}, {3, 4}, {4, 4}}},
101{{{4, 4}, {3, 4}, {1, 2}, {0, 0}}},
102
103{{{0, 1}, {2, 3}, {1, 0}, {1, 0}}},
104{{{0, 1}, {0, 1}, {1, 0}, {3, 2}}},
105
106{{{0, 2}, {0, 1}, {1, 0}, {1, 0}}},
107{{{0, 1}, {0, 1}, {2, 0}, {1, 0}}},
108
109{{{0, 1}, {0, 2}, {1, 0}, {1, 0}}},
110{{{0, 1}, {0, 1}, {1, 0}, {2, 0}}},
111
112{{{0, 1}, {1, 6}, {1, 0}, {2, 0}}},
113{{{0, 1}, {0, 2}, {1, 0}, {6, 1}}},
114
115{{{0, 1}, {5, 6}, {1, 0}, {1, 0}}},
116{{{0, 1}, {0, 1}, {1, 0}, {6, 5}}},
117
118{{{95.837747722788592, 45.025976907939643}, {16.564570095652982, 0.72959763963222402},
119 {63.209855865319199, 68.047528419665767}, {57.640240647662544, 59.524565264361243}}},
120{{{51.593891741518817, 38.53849970667553}, {62.34752929878772, 74.924924725166022},
121 {74.810149322641152, 34.17966562983564}, {29.368398119401373, 94.66719277886078}}},
122
123{{{39.765160968417838, 33.060396198677083}, {5.1922921581157908, 66.854301452103215},
124 {31.619281802149157, 25.269248720849514}, {81.541621071073038, 70.025341524754353}}},
125{{{46.078911165743556, 48.259962651999651}, {20.24450549867214, 49.403916182650214},
126 {0.26325131778756683, 24.46489805563581}, {15.915006546264051, 83.515023059917155}}},
127
128{{{65.454505973241524, 93.881892270353575}, {45.867360264932437, 92.723972719499827},
129 {2.1464054482739447, 74.636369140183717}, {33.774068594804994, 40.770872887582925}}},
130{{{72.963387832494163, 95.659300729473728}, {11.809496633619768, 82.209921247423594},
131 {13.456139067865974, 57.329313623406605}, {36.060621606214262, 70.867335643091849}}},
132
133{{{32.484981432782945, 75.082940782924624}, {42.467313093350882, 48.131159948246157},
134 {3.5963115764764657, 43.208665839959245}, {79.442476890721579, 89.709102357602262}}},
135{{{18.98573861410177, 93.308887208490106}, {40.405250173250792, 91.039661826118675},
136 {8.0467721950480584, 42.100282172719147}, {40.883324221187891, 26.030185504830527}}},
137
138{{{7.5374809128872498, 82.441702896003477}, {22.444346930107265, 22.138854312775123},
139 {66.76091829629658, 50.753805856571446}, {78.193478508942519, 97.7932997968948}}},
140{{{97.700573130371311, 53.53260215070685}, {87.72443481149358, 84.575876772671876},
141 {19.215031396232092, 47.032676472809484}, {11.989686410869325, 10.659507480757082}}},
142
143{{{26.192053931854691, 9.8504326817814416}, {10.174241480498686, 98.476562741434464},
144 {21.177712558385782, 33.814968789841501}, {75.329030899018534, 55.02231980442177}}},
145{{{56.222082700683771, 24.54395039218662}, {95.589995289030483, 81.050822735322086},
146 {28.180450866082897, 28.837706255185282}, {60.128952916771617, 87.311672180570511}}},
147
148{{{42.449716172390481, 52.379709366885805}, {27.896043159019225, 48.797373636065686},
149 {92.770268299044233, 89.899302036454571}, {12.102066544863426, 99.43241951960718}}},
150{{{45.77532924980639, 45.958701495993274}, {37.458701356062065, 68.393691335056758},
151 {37.569326692060258, 27.673713456687381}, {60.674866037757539, 62.47349659096146}}},
152
153{{{67.426548091427676, 37.993772624988935}, {23.483695892376684, 90.476863174921306},
154 {35.597065061143162, 79.872482633158796}, {75.38634169631932, 18.244890038969412}}},
155{{{61.336508189019057, 82.693132843213675}, {44.639380902349664, 54.074825790745592},
156 {16.815615499771951, 20.049704667203923}, {41.866884958868326, 56.735503699973002}}},
157
158{{{18.1312339, 31.6473732}, {95.5711034, 63.5350219}, {92.3283165, 62.0158945},
159 {18.5656052, 32.1268808}}},
160{{{97.402018, 35.7169972}, {33.1127443, 25.8935163}, {1.13970027, 54.9424981},
161 {56.4860195, 60.529264}}},
162};
163
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000164const size_t testSetCount = SK_ARRAY_COUNT(testSet);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000165
166static const SkDCubic newTestSet[] = {
skia.committer@gmail.comb0a05892013-10-03 07:01:37 +0000167#if 0 // FIXME: asserts coincidence, not working yet
168{{{195, 785}, {124.30755615234375, 785}, {67, 841.85986328125}, {67, 912}}},
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000169{{{67, 913}, {67, 842.30755615234375}, {123.85984039306641, 785}, {194, 785}}},
170#endif
171
172{{{399,657}, {399,661.970581}, {403.029449,666}, {408,666}}},
173{{{406,666}, {402.686279,666}, {400,663.313721}, {400,660}}},
174
175{{{0,5}, {3,5}, {3,0}, {3,2}}},
176{{{0,3}, {2,3}, {5,0}, {5,3}}},
177
caryclark@google.com570863f2013-09-16 15:55:01 +0000178{{{132, 11419}, {130.89543151855469, 11419}, {130, 11418.1044921875}, {130, 11417}}},
179
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +0000180{{{3, 4}, {1, 5}, {4, 3}, {6, 4}}},
181{{{3, 4}, {4, 6}, {4, 3}, {5, 1}}},
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000182
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +0000183{{{130.04275512695312, 11417.413085937500 },
184 {130.23312377929687, 11418.319335937500 },
185 {131.03707885742187, 11419.000000000000 },
186 {132.00000000000000, 11419.000000000000 }}},
187
188{{{132.00000000000000, 11419.000000000000 },
189 {130.89543151855469, 11419.000000000000 },
190 {130.00000000000000, 11418.104492187500 },
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000191 {130.00000000000000, 11417.000000000000 }}},
192
robertphillips@google.com8dcea7d2013-07-15 15:38:55 +0000193{{{1.0516976506771041, 2.9684399028541346 },
194 {1.0604363140895228, 2.9633503074444141 },
195 {1.0692548215065762, 2.9580354426587459 },
196 {1.0781560339512140, 2.9525043684031349 }}},
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000197
robertphillips@google.com8dcea7d2013-07-15 15:38:55 +0000198{{{1.0523038101345104, 2.9523755204833737 },
199 {1.0607035288264237, 2.9580853881628375 },
200 {1.0690530472271964, 2.9633896794787749 },
201 {1.0773566568712512, 2.9682969775000219 }}},
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000202
robertphillips@google.com8dcea7d2013-07-15 15:38:55 +0000203{{{1.0386522625066592, 2.9759024812329078 },
204 {1.0559713690392631, 2.9661782500838885 },
205 {1.0736041309019990, 2.9555348259177858 },
206 {1.0915734362784633, 2.9440446879826569 }}},
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000207
robertphillips@google.com8dcea7d2013-07-15 15:38:55 +0000208{{{1.0396670794879301, 2.9435062123457261 },
209 {1.0565690546812769, 2.9557413250983462 },
210 {1.0732616463413533, 2.9663369676594282 },
211 {1.0897791867435489, 2.9753618045797472 }}},
212
213{{{0.8685656183311091, 3.0409266475785208 },
214 {0.99189542936395292, 3.0212163698184424 },
215 {1.1302108367493320, 2.9265646471747306 },
216 {1.2952305904872474, 2.7940808546473788 }}},
217
218{{{0.85437872843682727, 2.7536036928549055 },
219 {1.0045584590592620, 2.9493041024831705 },
220 {1.1336998329885613, 3.0248027987251747 },
221 {1.2593809752247314, 3.0152560315809107 }}},
222
223{{{0, 1}, {1, 6}, {1, 0}, {6, 2}}},
224{{{0, 1}, {2, 6}, {1, 0}, {6, 1}}},
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000225
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000226{{{134,11414}, {131.990234375,11414}, {130.32666015625,11415.482421875}, {130.04275512695312,11417.4130859375}}},
227{{{132,11419}, {130.89543151855469,11419}, {130,11418.1044921875}, {130,11417}}},
228
229{{{132,11419}, {130.89543151855469,11419}, {130,11418.1044921875}, {130,11417}}},
230{{{130.04275512695312,11417.4130859375}, {130.23312377929687,11418.3193359375}, {131.03707885742187,11419}, {132,11419}}},
231
caryclark@google.coma5e55922013-05-07 18:51:31 +0000232{{{0, 1}, {2, 3}, {5, 1}, {4, 3}}},
233{{{1, 5}, {3, 4}, {1, 0}, {3, 2}}},
234
caryclark@google.comb3f09212013-04-17 15:49:16 +0000235{{{3, 5}, {1, 6}, {5, 0}, {3, 1}}},
236{{{0, 5}, {1, 3}, {5, 3}, {6, 1}}},
237
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000238{{{0, 1}, {1, 5}, {1, 0}, {1, 0}}},
239{{{0, 1}, {0, 1}, {1, 0}, {5, 1}}},
240
241{{{1, 3}, {5, 6}, {5, 3}, {5, 4}}},
242{{{3, 5}, {4, 5}, {3, 1}, {6, 5}}},
243
244{{{0, 5}, {0, 5}, {5, 4}, {6, 4}}},
245{{{4, 5}, {4, 6}, {5, 0}, {5, 0}}},
246
247{{{0, 4}, {1, 3}, {5, 4}, {4, 2}}},
248{{{4, 5}, {2, 4}, {4, 0}, {3, 1}}},
249
250{{{0, 2}, {1, 5}, {3, 2}, {4, 1}}},
251{{{2, 3}, {1, 4}, {2, 0}, {5, 1}}},
252
253{{{0, 2}, {2, 3}, {5, 1}, {3, 2}}},
254{{{1, 5}, {2, 3}, {2, 0}, {3, 2}}},
255
256{{{2, 6}, {4, 5}, {1, 0}, {6, 1}}},
257{{{0, 1}, {1, 6}, {6, 2}, {5, 4}}},
258
259{{{0, 1}, {1, 2}, {6, 5}, {5, 4}}},
260{{{5, 6}, {4, 5}, {1, 0}, {2, 1}}},
261
262{{{2.5119999999999996, 1.5710000000000002}, {2.6399999999999983, 1.6599999999999997},
263 {2.8000000000000007, 1.8000000000000003}, {3, 2}}},
264{{{2.4181876227114887, 1.9849772580462195}, {2.8269904869227211, 2.009330650246834},
265 {3.2004679292461624, 1.9942047174679169}, {3.4986199496818058, 2.0035994597094731}}},
266
267{{{2, 3}, {1, 4}, {1, 0}, {6, 0}}},
268{{{0, 1}, {0, 6}, {3, 2}, {4, 1}}},
269
270{{{0, 2}, {1, 5}, {1, 0}, {6, 1}}},
271{{{0, 1}, {1, 6}, {2, 0}, {5, 1}}},
272
273{{{0, 1}, {1, 5}, {2, 1}, {4, 0}}},
274{{{1, 2}, {0, 4}, {1, 0}, {5, 1}}},
275
276{{{0, 1}, {3, 5}, {2, 1}, {3, 1}}},
277{{{1, 2}, {1, 3}, {1, 0}, {5, 3}}},
278
279{{{0, 1}, {2, 5}, {6, 0}, {5, 3}}},
280{{{0, 6}, {3, 5}, {1, 0}, {5, 2}}},
281
282{{{0, 1}, {3, 6}, {1, 0}, {5, 2}}},
283{{{0, 1}, {2, 5}, {1, 0}, {6, 3}}},
284
285{{{1, 2}, {5, 6}, {1, 0}, {1, 0}}},
286{{{0, 1}, {0, 1}, {2, 1}, {6, 5}}},
287
288{{{0, 6}, {1, 2}, {1, 0}, {1, 0}}},
289{{{0, 1}, {0, 1}, {6, 0}, {2, 1}}},
290
291{{{0, 2}, {0, 1}, {3, 0}, {1, 0}}},
292{{{0, 3}, {0, 1}, {2, 0}, {1, 0}}},
293};
294
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000295const size_t newTestSetCount = SK_ARRAY_COUNT(newTestSet);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000296
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000297static void oneOff(skiatest::Reporter* reporter, const SkDCubic& cubic1, const SkDCubic& cubic2,
298 bool coin) {
caryclark@google.com8d0a5242013-07-16 16:11:16 +0000299 SkASSERT(ValidCubic(cubic1));
300 SkASSERT(ValidCubic(cubic2));
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000301#if ONE_OFF_DEBUG
302 SkDebugf("computed quadratics given\n");
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000303 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000304 cubic1[0].fX, cubic1[0].fY, cubic1[1].fX, cubic1[1].fY,
305 cubic1[2].fX, cubic1[2].fY, cubic1[3].fX, cubic1[3].fY);
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000306 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000307 cubic2[0].fX, cubic2[0].fY, cubic2[1].fX, cubic2[1].fY,
308 cubic2[2].fX, cubic2[2].fY, cubic2[3].fX, cubic2[3].fY);
309#endif
caryclark@google.comd892bd82013-06-17 14:10:36 +0000310 SkTArray<SkDQuad, true> quads1;
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000311 CubicToQuads(cubic1, cubic1.calcPrecision(), quads1);
312#if ONE_OFF_DEBUG
313 SkDebugf("computed quadratics set 1\n");
314 for (int index = 0; index < quads1.count(); ++index) {
315 const SkDQuad& q = quads1[index];
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000316 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].fX, q[0].fY,
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000317 q[1].fX, q[1].fY, q[2].fX, q[2].fY);
318 }
319#endif
caryclark@google.comd892bd82013-06-17 14:10:36 +0000320 SkTArray<SkDQuad, true> quads2;
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000321 CubicToQuads(cubic2, cubic2.calcPrecision(), quads2);
322#if ONE_OFF_DEBUG
323 SkDebugf("computed quadratics set 2\n");
324 for (int index = 0; index < quads2.count(); ++index) {
325 const SkDQuad& q = quads2[index];
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000326 SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].fX, q[0].fY,
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000327 q[1].fX, q[1].fY, q[2].fX, q[2].fY);
328 }
329#endif
330 SkIntersections intersections;
331 intersections.intersect(cubic1, cubic2);
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000332 REPORTER_ASSERT(reporter, !coin || intersections.used() == 2);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000333 double tt1, tt2;
334 SkDPoint xy1, xy2;
335 for (int pt3 = 0; pt3 < intersections.used(); ++pt3) {
336 tt1 = intersections[0][pt3];
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000337 xy1 = cubic1.ptAtT(tt1);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000338 tt2 = intersections[1][pt3];
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000339 xy2 = cubic2.ptAtT(tt2);
caryclark@google.com5ec2b1e2013-07-15 20:50:24 +0000340 const SkDPoint& iPt = intersections.pt(pt3);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000341#if ONE_OFF_DEBUG
342 SkDebugf("%s t1=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n",
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000343 __FUNCTION__, tt1, xy1.fX, xy1.fY, iPt.fX,
344 iPt.fY, xy2.fX, xy2.fY, tt2);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000345#endif
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000346 REPORTER_ASSERT(reporter, xy1.approximatelyEqual(iPt));
347 REPORTER_ASSERT(reporter, xy2.approximatelyEqual(iPt));
348 REPORTER_ASSERT(reporter, xy1.approximatelyEqual(xy2));
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000349 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000350 reporter->bumpTestCount();
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000351}
352
353static void oneOff(skiatest::Reporter* reporter, int outer, int inner) {
354 const SkDCubic& cubic1 = testSet[outer];
355 const SkDCubic& cubic2 = testSet[inner];
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000356 oneOff(reporter, cubic1, cubic2, false);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000357}
358
359static void newOneOff(skiatest::Reporter* reporter, int outer, int inner) {
360 const SkDCubic& cubic1 = newTestSet[outer];
361 const SkDCubic& cubic2 = newTestSet[inner];
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000362 oneOff(reporter, cubic1, cubic2, false);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000363}
364
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000365static void oneOffTests(skiatest::Reporter* reporter) {
366 for (size_t outer = 0; outer < testSetCount - 1; ++outer) {
367 for (size_t inner = outer + 1; inner < testSetCount; ++inner) {
368 oneOff(reporter, outer, inner);
369 }
370 }
371 for (size_t outer = 0; outer < newTestSetCount - 1; ++outer) {
372 for (size_t inner = outer + 1; inner < newTestSetCount; ++inner) {
caryclark@google.com5ec2b1e2013-07-15 20:50:24 +0000373 newOneOff(reporter, outer, inner);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000374 }
375 }
376}
377
378#define DEBUG_CRASH 0
379
380static void CubicIntersection_RandTest(skiatest::Reporter* reporter) {
381 srand(0);
382 const int tests = 10000000;
caryclark@google.comdb60de72013-04-11 12:33:23 +0000383#if !defined(SK_BUILD_FOR_WIN) && !defined(SK_BUILD_FOR_ANDROID)
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000384 unsigned seed = 0;
385#endif
386 for (int test = 0; test < tests; ++test) {
387 SkDCubic cubic1, cubic2;
388 for (int i = 0; i < 4; ++i) {
389 cubic1[i].fX = static_cast<double>(SK_RAND(seed)) / RAND_MAX * 100;
390 cubic1[i].fY = static_cast<double>(SK_RAND(seed)) / RAND_MAX * 100;
391 cubic2[i].fX = static_cast<double>(SK_RAND(seed)) / RAND_MAX * 100;
392 cubic2[i].fY = static_cast<double>(SK_RAND(seed)) / RAND_MAX * 100;
393 }
394 #if DEBUG_CRASH
395 char str[1024];
396 snprintf(str, sizeof(str),
397 "{{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}}},\n"
398 "{{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}}},\n",
399 cubic1[0].fX, cubic1[0].fY, cubic1[1].fX, cubic1[1].fY, cubic1[2].fX, cubic1[2].fY,
400 cubic1[3].fX, cubic1[3].fY,
401 cubic2[0].fX, cubic2[0].fY, cubic2[1].fX, cubic2[1].fY, cubic2[2].fX, cubic2[2].fY,
402 cubic2[3].fX, cubic2[3].fY);
403 #endif
404 SkDRect rect1, rect2;
405 rect1.setBounds(cubic1);
406 rect2.setBounds(cubic2);
407 bool boundsIntersect = rect1.fLeft <= rect2.fRight && rect2.fLeft <= rect2.fRight
408 && rect1.fTop <= rect2.fBottom && rect2.fTop <= rect1.fBottom;
409 if (test == -1) {
410 SkDebugf("ready...\n");
411 }
412 SkIntersections intersections2;
413 int newIntersects = intersections2.intersect(cubic1, cubic2);
414 if (!boundsIntersect && newIntersects) {
415 #if DEBUG_CRASH
416 SkDebugf("%s %d unexpected intersection boundsIntersect=%d "
417 " newIntersects=%d\n%s %s\n", __FUNCTION__, test, boundsIntersect,
418 newIntersects, __FUNCTION__, str);
419 #endif
420 REPORTER_ASSERT(reporter, 0);
421 }
422 for (int pt = 0; pt < intersections2.used(); ++pt) {
423 double tt1 = intersections2[0][pt];
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000424 SkDPoint xy1 = cubic1.ptAtT(tt1);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000425 double tt2 = intersections2[1][pt];
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000426 SkDPoint xy2 = cubic2.ptAtT(tt2);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000427 REPORTER_ASSERT(reporter, xy1.approximatelyEqual(xy2));
428 }
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000429 reporter->bumpTestCount();
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000430 }
431}
432
433static void intersectionFinder(int index0, int index1, double t1Seed, double t2Seed,
434 double t1Step, double t2Step) {
435 const SkDCubic& cubic1 = newTestSet[index0];
436 const SkDCubic& cubic2 = newTestSet[index1];
437 SkDPoint t1[3], t2[3];
438 bool toggle = true;
439 do {
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000440 t1[0] = cubic1.ptAtT(t1Seed - t1Step);
441 t1[1] = cubic1.ptAtT(t1Seed);
442 t1[2] = cubic1.ptAtT(t1Seed + t1Step);
443 t2[0] = cubic2.ptAtT(t2Seed - t2Step);
444 t2[1] = cubic2.ptAtT(t2Seed);
445 t2[2] = cubic2.ptAtT(t2Seed + t2Step);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000446 double dist[3][3];
447 dist[1][1] = t1[1].distance(t2[1]);
448 int best_i = 1, best_j = 1;
449 for (int i = 0; i < 3; ++i) {
450 for (int j = 0; j < 3; ++j) {
451 if (i == 1 && j == 1) {
452 continue;
453 }
454 dist[i][j] = t1[i].distance(t2[j]);
455 if (dist[best_i][best_j] > dist[i][j]) {
456 best_i = i;
457 best_j = j;
458 }
459 }
460 }
461 if (best_i == 0) {
462 t1Seed -= t1Step;
463 } else if (best_i == 2) {
464 t1Seed += t1Step;
465 }
466 if (best_j == 0) {
467 t2Seed -= t2Step;
468 } else if (best_j == 2) {
469 t2Seed += t2Step;
470 }
471 if (best_i == 1 && best_j == 1) {
472 if ((toggle ^= true)) {
473 t1Step /= 2;
474 } else {
475 t2Step /= 2;
476 }
477 }
478 } while (!t1[1].approximatelyEqual(t2[1]));
479 t1Step = t2Step = 0.1;
480 double t10 = t1Seed - t1Step * 2;
481 double t12 = t1Seed + t1Step * 2;
482 double t20 = t2Seed - t2Step * 2;
483 double t22 = t2Seed + t2Step * 2;
484 SkDPoint test;
485 while (!approximately_zero(t1Step)) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000486 test = cubic1.ptAtT(t10);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000487 t10 += t1[1].approximatelyEqual(test) ? -t1Step : t1Step;
488 t1Step /= 2;
489 }
490 t1Step = 0.1;
491 while (!approximately_zero(t1Step)) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000492 test = cubic1.ptAtT(t12);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000493 t12 -= t1[1].approximatelyEqual(test) ? -t1Step : t1Step;
494 t1Step /= 2;
495 }
496 while (!approximately_zero(t2Step)) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000497 test = cubic2.ptAtT(t20);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000498 t20 += t2[1].approximatelyEqual(test) ? -t2Step : t2Step;
499 t2Step /= 2;
500 }
501 t2Step = 0.1;
502 while (!approximately_zero(t2Step)) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000503 test = cubic2.ptAtT(t22);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000504 t22 -= t2[1].approximatelyEqual(test) ? -t2Step : t2Step;
505 t2Step /= 2;
506 }
507#if ONE_OFF_DEBUG
508 SkDebugf("%s t1=(%1.9g<%1.9g<%1.9g) t2=(%1.9g<%1.9g<%1.9g)\n", __FUNCTION__,
509 t10, t1Seed, t12, t20, t2Seed, t22);
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000510 SkDPoint p10 = cubic1.ptAtT(t10);
511 SkDPoint p1Seed = cubic1.ptAtT(t1Seed);
512 SkDPoint p12 = cubic1.ptAtT(t12);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000513 SkDebugf("%s p1=(%1.9g,%1.9g)<(%1.9g,%1.9g)<(%1.9g,%1.9g)\n", __FUNCTION__,
514 p10.fX, p10.fY, p1Seed.fX, p1Seed.fY, p12.fX, p12.fY);
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000515 SkDPoint p20 = cubic2.ptAtT(t20);
516 SkDPoint p2Seed = cubic2.ptAtT(t2Seed);
517 SkDPoint p22 = cubic2.ptAtT(t22);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000518 SkDebugf("%s p2=(%1.9g,%1.9g)<(%1.9g,%1.9g)<(%1.9g,%1.9g)\n", __FUNCTION__,
519 p20.fX, p20.fY, p2Seed.fX, p2Seed.fY, p22.fX, p22.fY);
520#endif
521}
522
523static void CubicIntersection_IntersectionFinder() {
524// double t1Seed = 0.87;
525// double t2Seed = 0.87;
526 double t1Step = 0.000001;
527 double t2Step = 0.000001;
528 intersectionFinder(0, 1, 0.855895664, 0.864850875, t1Step, t2Step);
529 intersectionFinder(0, 1, 0.865207906, 0.865207887, t1Step, t2Step);
530 intersectionFinder(0, 1, 0.865213351, 0.865208087, t1Step, t2Step);
531}
532
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000533static const SkDCubic selfSet[] = {
534 {{{2, 3}, {0, 4}, {3, 2}, {5, 3}}},
535 {{{3, 6}, {2, 3}, {4, 0}, {3, 2}}},
536 {{{0, 2}, {2, 3}, {5, 1}, {3, 2}}},
537 {{{0, 2}, {3, 5}, {5, 0}, {4, 2}}},
538 {{{3.34, 8.98}, {1.95, 10.27}, {3.76, 7.65}, {4.96, 10.64}}},
539 {{{3.13, 2.74}, {1.08, 4.62}, {3.71, 0.94}, {2.01, 3.81}}},
540 {{{6.71, 3.14}, {7.99, 2.75}, {8.27, 1.96}, {6.35, 3.57}}},
541 {{{12.81, 7.27}, {7.22, 6.98}, {12.49, 8.97}, {11.42, 6.18}}},
542};
543size_t selfSetCount = SK_ARRAY_COUNT(selfSet);
544
545static void selfOneOff(skiatest::Reporter* reporter, int index) {
546 const SkDCubic& cubic = selfSet[index];
547#if ONE_OFF_DEBUG
548 int idx2;
549 double max[3];
550 int ts = cubic.findMaxCurvature(max);
551 for (idx2 = 0; idx2 < ts; ++idx2) {
552 SkDebugf("%s max[%d]=%1.9g (%1.9g, %1.9g)\n", __FUNCTION__, idx2,
553 max[idx2], cubic.ptAtT(max[idx2]).fX, cubic.ptAtT(max[idx2]).fY);
554 }
555 SkTArray<double, true> ts1;
556 SkTArray<SkDQuad, true> quads1;
557 cubic.toQuadraticTs(cubic.calcPrecision(), &ts1);
558 for (idx2 = 0; idx2 < ts1.count(); ++idx2) {
559 SkDebugf("%s t[%d]=%1.9g\n", __FUNCTION__, idx2, ts1[idx2]);
560 }
561 CubicToQuads(cubic, cubic.calcPrecision(), quads1);
562 for (idx2 = 0; idx2 < quads1.count(); ++idx2) {
563 const SkDQuad& q = quads1[idx2];
564 SkDebugf(" {{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}}},\n",
565 q[0].fX, q[0].fY, q[1].fX, q[1].fY, q[2].fX, q[2].fY);
566 }
567 SkDebugf("\n");
568#endif
569 SkIntersections i;
570 int result = i.intersect(cubic);
571 REPORTER_ASSERT(reporter, result == 1);
572 REPORTER_ASSERT(reporter, i.used() == 1);
573 REPORTER_ASSERT(reporter, !approximately_equal(i[0][0], i[1][0]));
574 SkDPoint pt1 = cubic.ptAtT(i[0][0]);
575 SkDPoint pt2 = cubic.ptAtT(i[1][0]);
576 REPORTER_ASSERT(reporter, pt1.approximatelyEqual(pt2));
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000577 reporter->bumpTestCount();
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000578}
579
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000580static void cubicIntersectionSelfTest(skiatest::Reporter* reporter) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000581 size_t firstFail = 0;
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000582 for (size_t index = firstFail; index < selfSetCount; ++index) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000583 selfOneOff(reporter, index);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000584 }
585}
586
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000587static const SkDCubic coinSet[] = {
skia.committer@gmail.comb0a05892013-10-03 07:01:37 +0000588 {{{317, 711}, {322.52285766601562, 711}, {327, 715.4771728515625}, {327, 721}}},
589 {{{324.07107543945312, 713.928955078125}, {324.4051513671875, 714.26300048828125},
590 {324.71566772460937, 714.62060546875}, {325, 714.9990234375}}},
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000591
592 {{{2, 3}, {0, 4}, {3, 2}, {5, 3}}},
593 {{{2, 3}, {0, 4}, {3, 2}, {5, 3}}},
594};
595
596size_t coinSetCount = SK_ARRAY_COUNT(coinSet);
597
598static void coinOneOff(skiatest::Reporter* reporter, int index) {
599 const SkDCubic& cubic1 = coinSet[index];
600 const SkDCubic& cubic2 = coinSet[index + 1];
601 oneOff(reporter, cubic1, cubic2, true);
602}
603
604static void cubicIntersectionCoinTest(skiatest::Reporter* reporter) {
605 size_t firstFail = 0;
606 for (size_t index = firstFail; index < coinSetCount; index += 2) {
607 coinOneOff(reporter, index);
608 }
609}
610
611static void PathOpsCubicCoinOneOffTest(skiatest::Reporter* reporter) {
612 coinOneOff(reporter, 0);
613}
614
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000615static void PathOpsCubicIntersectionOneOffTest(skiatest::Reporter* reporter) {
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000616 newOneOff(reporter, 0, 1);
617}
618
619static void PathOpsCubicSelfOneOffTest(skiatest::Reporter* reporter) {
620 selfOneOff(reporter, 0);
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000621}
622
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000623static void PathOpsCubicIntersectionTest(skiatest::Reporter* reporter) {
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000624 oneOffTests(reporter);
625 cubicIntersectionSelfTest(reporter);
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000626 cubicIntersectionCoinTest(reporter);
caryclark@google.com9166dcb2013-04-08 11:50:00 +0000627 standardTestCases(reporter);
628 if (false) CubicIntersection_IntersectionFinder();
629 if (false) CubicIntersection_RandTest(reporter);
630}
631
632#include "TestClassDef.h"
caryclark@google.comad65a3e2013-04-15 19:13:59 +0000633DEFINE_TESTCLASS_SHORT(PathOpsCubicIntersectionTest)
caryclark@google.comfa2aeee2013-07-15 13:29:13 +0000634
635DEFINE_TESTCLASS_SHORT(PathOpsCubicIntersectionOneOffTest)
caryclark@google.com4fdbb222013-07-23 15:27:41 +0000636
637DEFINE_TESTCLASS_SHORT(PathOpsCubicSelfOneOffTest)
caryclark@google.com7eaa53d2013-10-02 14:49:34 +0000638
639DEFINE_TESTCLASS_SHORT(PathOpsCubicCoinOneOffTest)