OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 #include "PathOpsTestCommon.h" | 7 #include "PathOpsTestCommon.h" |
8 #include "SkIntersections.h" | 8 #include "SkIntersections.h" |
9 #include "SkPathOpsCubic.h" | 9 #include "SkPathOpsCubic.h" |
10 #include "SkPathOpsQuad.h" | 10 #include "SkPathOpsQuad.h" |
| 11 #include "SkRandom.h" |
11 #include "SkReduceOrder.h" | 12 #include "SkReduceOrder.h" |
12 #include "Test.h" | 13 #include "Test.h" |
13 | 14 |
14 static struct lineCubic { | 15 static struct lineCubic { |
15 SkDCubic cubic; | 16 SkDCubic cubic; |
16 SkDQuad quad; | 17 SkDQuad quad; |
17 int answerCount; | 18 int answerCount; |
18 SkDPoint answers[2]; | 19 SkDPoint answers[2]; |
19 } quadCubicTests[] = { | 20 } quadCubicTests[] = { |
| 21 #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 |
20 {{{{778, 14089}, {778, 14091.208984375}, {776.20916748046875, 14093}, {774,
14093}}}, | 27 {{{{778, 14089}, {778, 14091.208984375}, {776.20916748046875, 14093}, {774,
14093}}}, |
21 {{{778, 14089}, {777.99957275390625, 14090.65625}, {776.82843017578125, 140
91.828125}}}, 2, | 28 {{{778, 14089}, {777.99957275390625, 14090.65625}, {776.82843017578125, 140
91.828125}}}, 2, |
22 {{778, 14089}, {776.82855609581270,14091.828250841330}}}, | 29 {{778, 14089}, {776.82855609581270,14091.828250841330}}}, |
23 | 30 |
24 {{{{1110, 817}, {1110.55225f, 817}, {1111, 817.447693f}, {1111, 818}}}, | 31 {{{{1110, 817}, {1110.55225f, 817}, {1111, 817.447693f}, {1111, 818}}}, |
25 {{{1110.70715f, 817.292908f}, {1110.41406f, 817.000122f}, {1110, 817}}}, 2, | 32 {{{1110.70715f, 817.292908f}, {1110.41406f, 817.000122f}, {1110, 817}}}, 2, |
26 {{1110, 817}, {1110.70715f, 817.292908f}}}, | 33 {{1110, 817}, {1110.70715f, 817.292908f}}}, |
27 | 34 |
28 {{{{1110, 817}, {1110.55225f, 817}, {1111, 817.447693f}, {1111, 818}}}, | 35 {{{{1110, 817}, {1110.55225f, 817}, {1111, 817.447693f}, {1111, 818}}}, |
29 {{{1111, 818}, {1110.99988f, 817.585876f}, {1110.70715f, 817.292908f}}}, 2, | 36 {{{1111, 818}, {1110.99988f, 817.585876f}, {1110.70715f, 817.292908f}}}, 2, |
(...skipping 11 matching lines...) Expand all Loading... |
41 | 48 |
42 {{{{10,234}, {10,229.58172607421875}, {13.581720352172852,226}, {18,226}}}, | 49 {{{{10,234}, {10,229.58172607421875}, {13.581720352172852,226}, {18,226}}}, |
43 {{{18,226}, {14.686291694641113,226}, {12.342399597167969,228.3424072265625
}}}, 1, | 50 {{{18,226}, {14.686291694641113,226}, {12.342399597167969,228.3424072265625
}}}, 1, |
44 {{18,226}, {0,0}}}, | 51 {{18,226}, {0,0}}}, |
45 | 52 |
46 {{{{10,234}, {10,229.58172607421875}, {13.581720352172852,226}, {18,226}}}, | 53 {{{{10,234}, {10,229.58172607421875}, {13.581720352172852,226}, {18,226}}}, |
47 {{{12.342399597167969,228.3424072265625}, {10,230.68629455566406}, {10,234}
}}, 1, | 54 {{{12.342399597167969,228.3424072265625}, {10,230.68629455566406}, {10,234}
}}, 1, |
48 {{10,234}, {0,0}}}, | 55 {{10,234}, {0,0}}}, |
49 }; | 56 }; |
50 | 57 |
51 static const size_t quadCubicTests_count = SK_ARRAY_COUNT(quadCubicTests); | 58 static const int quadCubicTests_count = (int) SK_ARRAY_COUNT(quadCubicTests); |
| 59 |
| 60 static 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 } |
52 | 103 |
53 DEF_TEST(PathOpsCubicQuadIntersection, reporter) { | 104 DEF_TEST(PathOpsCubicQuadIntersection, reporter) { |
54 for (size_t index = 0; index < quadCubicTests_count; ++index) { | 105 for (int index = 0; index < quadCubicTests_count; ++index) { |
55 int iIndex = static_cast<int>(index); | 106 cubicQuadIntersection(reporter, index); |
56 const SkDCubic& cubic = quadCubicTests[index].cubic; | 107 reporter->bumpTestCount(); |
57 SkASSERT(ValidCubic(cubic)); | 108 } |
58 const SkDQuad& quad = quadCubicTests[index].quad; | 109 } |
59 SkASSERT(ValidQuad(quad)); | 110 |
60 SkReduceOrder reduce1; | 111 DEF_TEST(PathOpsCubicQuadIntersectionOneOff, reporter) { |
61 SkReduceOrder reduce2; | 112 cubicQuadIntersection(reporter, 0); |
62 int order1 = reduce1.reduce(cubic, SkReduceOrder::kNo_Quadratics); | 113 } |
63 int order2 = reduce2.reduce(quad); | 114 |
64 if (order1 != 4) { | 115 static bool gPathOpCubicQuadSlopVerbose = false; |
65 SkDebugf("[%d] cubic order=%d\n", iIndex, order1); | 116 static const int kCubicToQuadSubdivisionDepth = 8; // slots reserved for cubic t
o quads subdivision |
66 REPORTER_ASSERT(reporter, 0); | 117 |
67 } | 118 // determine that slop required after quad/quad finds a candidate intersection |
68 if (order2 != 3) { | 119 // use the cross of the tangents plus the distance from 1 or 0 as knobs |
69 SkDebugf("[%d] quad order=%d\n", iIndex, order2); | 120 DEF_TEST(PathOpsCubicQuadSlop, reporter) { |
70 REPORTER_ASSERT(reporter, 0); | 121 // create a random non-selfintersecting cubic |
71 } | 122 // break it into quadratics |
| 123 // offset the quadratic, measuring the slop required to find the intersectio
n |
| 124 if (!gPathOpCubicQuadSlopVerbose) { // takes a while to run -- so exclude i
t 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 }}; |
72 SkIntersections i; | 146 SkIntersections i; |
73 int roots = i.intersect(cubic, quad); | 147 if (i.intersect(cubic)) { |
74 SkASSERT(roots == quadCubicTests[index].answerCount); | 148 continue; |
75 for (int pt = 0; pt < roots; ++pt) { | 149 } |
76 double tt1 = i[0][pt]; | 150 SkSTArray<kCubicToQuadSubdivisionDepth, double, true> ts; |
77 SkDPoint xy1 = cubic.ptAtT(tt1); | 151 cubic.toQuadraticTs(cubic.calcPrecision(), &ts); |
78 double tt2 = i[1][pt]; | 152 double tStart = 0; |
79 SkDPoint xy2 = quad.ptAtT(tt2); | 153 int tsCount = ts.count(); |
80 if (!xy1.approximatelyEqual(xy2)) { | 154 for (int i1 = 0; i1 <= tsCount; ++i1) { |
81 SkDebugf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n", | 155 const double tEnd = i1 < tsCount ? ts[i1] : 1; |
82 __FUNCTION__, iIndex, pt, tt1, xy1.fX, xy1.fY, tt2, xy2.fX,
xy2.fY); | 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; |
83 } | 162 } |
84 REPORTER_ASSERT(reporter, xy1.approximatelyEqual(xy2)); | 163 for (int i2 = 0; i2 < 100; ++i2) { |
85 bool found = false; | 164 SkDPoint endDisplacement = {ran.nextRangeF(-100, 100), ran.nextR
angeF(-100, 100)}; |
86 for (int idx2 = 0; idx2 < quadCubicTests[index].answerCount; ++idx2)
{ | 165 SkDQuad nearby = {{ |
87 found |= quadCubicTests[index].answers[idx2].approximatelyEqual(
xy1); | 166 {quad[0].fX + endDisplacement.fX, quad[0].fY + endDispla
cement.fY}, |
| 167 {quad[1].fX + ran.nextRangeF(-100, 100), quad[1].fY + ra
n.nextRangeF(-100, 100)}, |
| 168 {quad[2].fX - endDisplacement.fX, quad[2].fY - endDispla
cement.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}}}" |
| 235 " qT=%1.9g cT=%1.9g dist=%1.9g cross=%1.9g\n", |
| 236 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, |
| 239 nearby[2].fX, nearby[2].fY, |
| 240 liner.pt(0).fX, liner.pt(0).fY, |
| 241 locals.pt(0).fX, locals.pt(0).fY, quadT, cubicT, dist.le
ngth(), 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, |
| 252 nearby[2].fX, nearby[2].fY, |
| 253 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; |
88 } | 259 } |
89 if (!found) { | 260 tStart = tEnd; |
90 SkDebugf("%s [%d,%d] xy1=(%g,%g) != \n", | 261 } |
91 __FUNCTION__, iIndex, pt, xy1.fX, xy1.fY); | 262 if (++foundOne >= 100000) { |
92 } | 263 break; |
93 REPORTER_ASSERT(reporter, found); | 264 } |
94 } | 265 } |
95 reporter->bumpTestCount(); | 266 #if 01 |
96 } | 267 SkDebugf("slopCount=%d\n", slopCount); |
97 } | 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 } |
OLD | NEW |