blob: 8c2263e3a2feccd93f02ef4ce0ac79cd3087d47c [file] [log] [blame]
caryclark@google.com9e49fb62012-08-27 14:11:33 +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 */
caryclark@google.comc6825902012-02-03 22:07:47 +00007#include "CurveIntersection.h"
caryclark@google.com8dcf1142012-07-02 20:27:02 +00008#include "CurveUtilities.h"
caryclark@google.com639df892012-01-10 21:46:10 +00009#include "CubicIntersection_TestData.h"
caryclark@google.com27accef2012-01-25 18:57:23 +000010#include "Intersection_Tests.h"
caryclark@google.com639df892012-01-10 21:46:10 +000011#include "Intersections.h"
12#include "TestUtilities.h"
13
14const int firstCubicIntersectionTest = 9;
15
16void CubicIntersection_Test() {
17 for (size_t index = firstCubicIntersectionTest; index < tests_count; ++index) {
18 const Cubic& cubic1 = tests[index][0];
19 const Cubic& cubic2 = tests[index][1];
20 Cubic reduce1, reduce2;
21 int order1 = reduceOrder(cubic1, reduce1, kReduceOrder_NoQuadraticsAllowed);
22 int order2 = reduceOrder(cubic2, reduce2, kReduceOrder_NoQuadraticsAllowed);
23 if (order1 < 4) {
caryclark@google.com27accef2012-01-25 18:57:23 +000024 printf("%s [%d] cubic1 order=%d\n", __FUNCTION__, (int) index, order1);
25 continue;
caryclark@google.com639df892012-01-10 21:46:10 +000026 }
27 if (order2 < 4) {
caryclark@google.com27accef2012-01-25 18:57:23 +000028 printf("%s [%d] cubic2 order=%d\n", __FUNCTION__, (int) index, order2);
29 continue;
caryclark@google.com639df892012-01-10 21:46:10 +000030 }
caryclark@google.com27accef2012-01-25 18:57:23 +000031 if (implicit_matches(reduce1, reduce2)) {
32 printf("%s [%d] coincident\n", __FUNCTION__, (int) index);
33 continue;
34 }
35 Intersections tIntersections;
caryclark@google.comc6825902012-02-03 22:07:47 +000036 intersect(reduce1, reduce2, tIntersections);
caryclark@google.com27accef2012-01-25 18:57:23 +000037 if (!tIntersections.intersected()) {
38 printf("%s [%d] no intersection\n", __FUNCTION__, (int) index);
39 continue;
40 }
41 for (int pt = 0; pt < tIntersections.used(); ++pt) {
42 double tt1 = tIntersections.fT[0][pt];
43 double tx1, ty1;
44 xy_at_t(cubic1, tt1, tx1, ty1);
45 double tt2 = tIntersections.fT[1][pt];
46 double tx2, ty2;
47 xy_at_t(cubic2, tt2, tx2, ty2);
caryclark@google.com6d0032a2013-01-04 19:41:13 +000048 if (!AlmostEqualUlps(tx1, tx2)) {
caryclark@google.com27accef2012-01-25 18:57:23 +000049 printf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
50 __FUNCTION__, (int)index, pt, tt1, tx1, ty1, tt2, tx2, ty2);
51 }
caryclark@google.com6d0032a2013-01-04 19:41:13 +000052 if (!AlmostEqualUlps(ty1, ty2)) {
caryclark@google.com27accef2012-01-25 18:57:23 +000053 printf("%s [%d,%d] y!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
54 __FUNCTION__, (int)index, pt, tt1, tx1, ty1, tt2, tx2, ty2);
caryclark@google.com639df892012-01-10 21:46:10 +000055 }
56 }
57 }
58}
caryclark@google.com73ca6242013-01-17 21:02:47 +000059
60static void oneOff(const Cubic& cubic1, const Cubic& cubic2) {
61 SkTDArray<Quadratic> quads1;
62 cubic_to_quadratics(cubic1, calcPrecision(cubic1), quads1);
63 for (int index = 0; index < quads1.count(); ++index) {
64 const Quadratic& q = quads1[index];
skia.committer@gmail.com15dd3002013-01-18 07:07:28 +000065 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].x, q[0].y,
caryclark@google.com73ca6242013-01-17 21:02:47 +000066 q[1].x, q[1].y, q[2].x, q[2].y);
67 }
68 SkDebugf("\n");
69 SkTDArray<Quadratic> quads2;
70 cubic_to_quadratics(cubic2, calcPrecision(cubic2), quads2);
71 for (int index = 0; index < quads2.count(); ++index) {
72 const Quadratic& q = quads2[index];
skia.committer@gmail.com15dd3002013-01-18 07:07:28 +000073 SkDebugf("{{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n", q[0].x, q[0].y,
caryclark@google.com73ca6242013-01-17 21:02:47 +000074 q[1].x, q[1].y, q[2].x, q[2].y);
75 }
76 SkDebugf("\n");
77 Intersections intersections2;
78 intersect2(cubic1, cubic2, intersections2);
79 for (int pt = 0; pt < intersections2.used(); ++pt) {
80 double tt1 = intersections2.fT[0][pt];
caryclark@google.com05c4bad2013-01-19 13:22:39 +000081 _Point xy1, xy2;
82 xy_at_t(cubic1, tt1, xy1.x, xy1.y);
caryclark@google.com73ca6242013-01-17 21:02:47 +000083 int pt2 = intersections2.fFlip ? intersections2.used() - pt - 1 : pt;
84 double tt2 = intersections2.fT[1][pt2];
caryclark@google.com05c4bad2013-01-19 13:22:39 +000085 xy_at_t(cubic2, tt2, xy2.x, xy2.y);
caryclark@google.com73ca6242013-01-17 21:02:47 +000086 SkDebugf("%s t1=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n", __FUNCTION__,
caryclark@google.com05c4bad2013-01-19 13:22:39 +000087 tt1, xy1.x, xy1.y, xy2.x, xy2.y, tt2);
88 assert(xy1.approximatelyEqual(xy2));
caryclark@google.com73ca6242013-01-17 21:02:47 +000089 }
90}
91
92static const Cubic testSet[] = {
93{{67.426548091427676, 37.993772624988935}, {23.483695892376684, 90.476863174921306}, {35.597065061143162, 79.872482633158796}, {75.38634169631932, 18.244890038969412}},
94{{61.336508189019057, 82.693132843213675}, {44.639380902349664, 54.074825790745592}, {16.815615499771951, 20.049704667203923}, {41.866884958868326, 56.735503699973002}},
95
96{{67.4265481, 37.9937726}, {23.4836959, 90.4768632}, {35.5970651, 79.8724826}, {75.3863417, 18.24489}},
97{{61.3365082, 82.6931328}, {44.6393809, 54.0748258}, {16.8156155, 20.0497047}, {41.866885, 56.7355037}},
98
99{{18.1312339, 31.6473732}, {95.5711034, 63.5350219}, {92.3283165, 62.0158945}, {18.5656052, 32.1268808}},
100{{97.402018, 35.7169972}, {33.1127443, 25.8935163}, {1.13970027, 54.9424981}, {56.4860195, 60.529264}},
101};
102
103const size_t testSetCount = sizeof(testSet) / sizeof(testSet[0]);
104
105void CubicIntersection_OneOffTest() {
106 for (size_t outer = 0; outer < testSetCount - 1; ++outer) {
107 SkDebugf("%s quads1[%d]\n", __FUNCTION__, outer);
108 const Cubic& cubic1 = testSet[outer];
109 for (size_t inner = outer + 1; inner < testSetCount; ++inner) {
110 SkDebugf("%s quads2[%d]\n", __FUNCTION__, inner);
111 const Cubic& cubic2 = testSet[inner];
112 oneOff(cubic1, cubic2);
113 }
114 }
115}
116
117#define DEBUG_CRASH 1
118
119class CubicChopper {
120public:
121
122// only finds one intersection
123CubicChopper(const Cubic& c1, const Cubic& c2)
124 : cubic1(c1)
125 , cubic2(c2)
126 , depth(0) {
127}
128
129bool intersect(double minT1, double maxT1, double minT2, double maxT2) {
130 Cubic sub1, sub2;
131 // FIXME: carry last subdivide and reduceOrder result with cubic
132 sub_divide(cubic1, minT1, maxT1, sub1);
133 sub_divide(cubic2, minT2, maxT2, sub2);
134 Intersections i;
135 intersect2(sub1, sub2, i);
136 if (i.used() == 0) {
137 return false;
138 }
139 double x1, y1, x2, y2;
140 t1 = minT1 + i.fT[0][0] * (maxT1 - minT1);
141 t2 = minT2 + i.fT[1][0] * (maxT2 - minT2);
142 xy_at_t(cubic1, t1, x1, y1);
143 xy_at_t(cubic2, t2, x2, y2);
144 if (AlmostEqualUlps(x1, x2) && AlmostEqualUlps(y1, y2)) {
145 return true;
146 }
147 double half1 = (minT1 + maxT1) / 2;
148 double half2 = (minT2 + maxT2) / 2;
149 ++depth;
150 bool result;
151 if (depth & 1) {
152 result = intersect(minT1, half1, minT2, maxT2) || intersect(half1, maxT1, minT2, maxT2)
153 || intersect(minT1, maxT1, minT2, half2) || intersect(minT1, maxT1, half2, maxT2);
154 } else {
155 result = intersect(minT1, maxT1, minT2, half2) || intersect(minT1, maxT1, half2, maxT2)
156 || intersect(minT1, half1, minT2, maxT2) || intersect(half1, maxT1, minT2, maxT2);
157 }
158 --depth;
159 return result;
160}
161
162const Cubic& cubic1;
163const Cubic& cubic2;
164double t1;
165double t2;
166int depth;
167};
168
169#define TRY_OLD 0 // old way fails on test == 1
170
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000171void CubicIntersection_RandTestOld() {
caryclark@google.com73ca6242013-01-17 21:02:47 +0000172 srand(0);
173 const int tests = 1000000; // 10000000;
174 double largestFactor = DBL_MAX;
175 for (int test = 0; test < tests; ++test) {
176 Cubic cubic1, cubic2;
177 for (int i = 0; i < 4; ++i) {
178 cubic1[i].x = (double) rand() / RAND_MAX * 100;
179 cubic1[i].y = (double) rand() / RAND_MAX * 100;
180 cubic2[i].x = (double) rand() / RAND_MAX * 100;
181 cubic2[i].y = (double) rand() / RAND_MAX * 100;
182 }
183 if (test == 2513) { // the pair crosses three times, but the quadratic approximation
184 continue; // only sees one -- should be OK to ignore the other two?
185 }
186 if (test == 12932) { // this exposes a weakness when one cubic touches the other but
187 continue; // does not touch the quad approximation. Captured in qc.htm as cubic15
188 }
189 #if DEBUG_CRASH
190 char str[1024];
191 sprintf(str, "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n"
192 "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n",
193 cubic1[0].x, cubic1[0].y, cubic1[1].x, cubic1[1].y, cubic1[2].x, cubic1[2].y,
194 cubic1[3].x, cubic1[3].y,
195 cubic2[0].x, cubic2[0].y, cubic2[1].x, cubic2[1].y, cubic2[2].x, cubic2[2].y,
196 cubic2[3].x, cubic2[3].y);
197 #endif
198 _Rect rect1, rect2;
199 rect1.setBounds(cubic1);
200 rect2.setBounds(cubic2);
201 bool boundsIntersect = rect1.left <= rect2.right && rect2.left <= rect2.right
202 && rect1.top <= rect2.bottom && rect2.top <= rect1.bottom;
203 Intersections i1, i2;
204 #if TRY_OLD
205 bool oldIntersects = intersect(cubic1, cubic2, i1);
206 #else
207 bool oldIntersects = false;
208 #endif
209 if (test == -1) {
210 SkDebugf("ready...\n");
211 }
212 bool newIntersects = intersect2(cubic1, cubic2, i2);
213 if (!boundsIntersect && (oldIntersects || newIntersects)) {
214 SkDebugf("%s %d unexpected intersection boundsIntersect=%d oldIntersects=%d"
215 " newIntersects=%d\n%s %s\n", __FUNCTION__, test, boundsIntersect,
216 oldIntersects, newIntersects, __FUNCTION__, str);
217 assert(0);
218 }
219 if (oldIntersects && !newIntersects) {
220 SkDebugf("%s %d missing intersection oldIntersects=%d newIntersects=%d\n%s %s\n",
221 __FUNCTION__, test, oldIntersects, newIntersects, __FUNCTION__, str);
222 assert(0);
223 }
224 if (!oldIntersects && !newIntersects) {
225 continue;
226 }
227 if (i2.used() > 1) {
228 continue;
229 // just look at single intercepts for simplicity
230 }
231 Intersections self1, self2; // self-intersect checks
232 if (intersect(cubic1, self1)) {
233 continue;
234 }
235 if (intersect(cubic2, self2)) {
236 continue;
237 }
238 // binary search for range necessary to enclose real intersection
239 CubicChopper c(cubic1, cubic2);
240 bool result = c.intersect(0, 1, 0, 1);
241 if (!result) {
242 // FIXME: a failure here probably means that a core routine used by CubicChopper is failing
243 continue;
244 }
245 double delta1 = fabs(c.t1 - i2.fT[0][0]);
246 double delta2 = fabs(c.t2 - i2.fT[1][0]);
247 double calc1 = calcPrecision(cubic1);
248 double calc2 = calcPrecision(cubic2);
249 double factor1 = calc1 / delta1;
250 double factor2 = calc2 / delta2;
251 SkDebugf("%s %d calc1=%1.9g delta1=%1.9g factor1=%1.9g calc2=%1.9g delta2=%1.9g"
252 " factor2=%1.9g\n", __FUNCTION__, test,
253 calc1, delta1, factor1, calc2, delta2, factor2);
254 if (factor1 < largestFactor) {
255 SkDebugf("WE HAVE A WINNER! %1.9g\n", factor1);
256 SkDebugf("%s\n", str);
257 oneOff(cubic1, cubic2);
258 largestFactor = factor1;
259 }
260 if (factor2 < largestFactor) {
261 SkDebugf("WE HAVE A WINNER! %1.9g\n", factor2);
262 SkDebugf("%s\n", str);
263 oneOff(cubic1, cubic2);
264 largestFactor = factor2;
265 }
266 }
267}
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000268
269void CubicIntersection_RandTest() {
270 srand(0);
271 const int tests = 1000000; // 10000000;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000272 for (int test = 0; test < tests; ++test) {
273 Cubic cubic1, cubic2;
274 for (int i = 0; i < 4; ++i) {
275 cubic1[i].x = (double) rand() / RAND_MAX * 100;
276 cubic1[i].y = (double) rand() / RAND_MAX * 100;
277 cubic2[i].x = (double) rand() / RAND_MAX * 100;
278 cubic2[i].y = (double) rand() / RAND_MAX * 100;
279 }
280 #if DEBUG_CRASH
281 char str[1024];
282 sprintf(str, "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n"
283 "{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}},\n",
284 cubic1[0].x, cubic1[0].y, cubic1[1].x, cubic1[1].y, cubic1[2].x, cubic1[2].y,
285 cubic1[3].x, cubic1[3].y,
286 cubic2[0].x, cubic2[0].y, cubic2[1].x, cubic2[1].y, cubic2[2].x, cubic2[2].y,
287 cubic2[3].x, cubic2[3].y);
288 #endif
289 _Rect rect1, rect2;
290 rect1.setBounds(cubic1);
291 rect2.setBounds(cubic2);
292 bool boundsIntersect = rect1.left <= rect2.right && rect2.left <= rect2.right
293 && rect1.top <= rect2.bottom && rect2.top <= rect1.bottom;
caryclark@google.com05c4bad2013-01-19 13:22:39 +0000294 if (test == -1) {
295 SkDebugf("ready...\n");
296 }
297 Intersections intersections2;
298 bool newIntersects = intersect2(cubic1, cubic2, intersections2);
299 if (!boundsIntersect && newIntersects) {
300 SkDebugf("%s %d unexpected intersection boundsIntersect=%d "
301 " newIntersects=%d\n%s %s\n", __FUNCTION__, test, boundsIntersect,
302 newIntersects, __FUNCTION__, str);
303 assert(0);
304 }
305 for (int pt = 0; pt < intersections2.used(); ++pt) {
306 double tt1 = intersections2.fT[0][pt];
307 _Point xy1, xy2;
308 xy_at_t(cubic1, tt1, xy1.x, xy1.y);
309 int pt2 = intersections2.fFlip ? intersections2.used() - pt - 1 : pt;
310 double tt2 = intersections2.fT[1][pt2];
311 xy_at_t(cubic2, tt2, xy2.x, xy2.y);
312 SkDebugf("%s t1=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n", __FUNCTION__,
313 tt1, xy1.x, xy1.y, xy2.x, xy2.y, tt2);
314 assert(xy1.approximatelyEqual(xy2));
315 }
316 }
317}