blob: cf04528b65eb13f4556396b7aa5e872c938253f8 [file] [log] [blame]
caryclark@google.comad65a3e2013-04-15 19:13:59 +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 */
Mike Kleinc0bd9f92019-04-23 12:05:21 -05007#include "include/utils/SkRandom.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/pathops/SkIntersections.h"
9#include "src/pathops/SkOpContour.h"
10#include "src/pathops/SkOpSegment.h"
11#include "tests/PathOpsTestCommon.h"
12#include "tests/Test.h"
caryclark@google.comad65a3e2013-04-15 19:13:59 +000013
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000014static bool gDisableAngleTests = true;
15
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000016static float next(float f)
17{
18 int fBits = SkFloatAs2sCompliment(f);
19 ++fBits;
20 float fNext = Sk2sComplimentAsFloat(fBits);
21 return fNext;
caryclark@google.comad65a3e2013-04-15 19:13:59 +000022}
23
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000024static float prev(float f)
25{
26 int fBits = SkFloatAs2sCompliment(f);
27 --fBits;
28 float fNext = Sk2sComplimentAsFloat(fBits);
29 return fNext;
30}
31
32DEF_TEST(PathOpsAngleFindCrossEpsilon, reporter) {
33 if (gDisableAngleTests) {
caryclark@google.com07e97fc2013-07-08 17:17:02 +000034 return;
35 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000036 SkRandom ran;
37 int maxEpsilon = 0;
38 for (int index = 0; index < 10000000; ++index) {
39 SkDLine line = {{{0, 0}, {ran.nextRangeF(0.0001f, 1000), ran.nextRangeF(0.0001f, 1000)}}};
40 for (int inner = 0; inner < 10; ++inner) {
41 float t = ran.nextRangeF(0.0001f, 1);
42 SkDPoint dPt = line.ptAtT(t);
43 SkPoint pt = dPt.asSkPoint();
44 float xs[3] = { prev(pt.fX), pt.fX, next(pt.fX) };
45 float ys[3] = { prev(pt.fY), pt.fY, next(pt.fY) };
46 for (int xIdx = 0; xIdx < 3; ++xIdx) {
47 for (int yIdx = 0; yIdx < 3; ++yIdx) {
48 SkPoint test = { xs[xIdx], ys[yIdx] };
49 float p1 = SkDoubleToScalar(line[1].fX * test.fY);
50 float p2 = SkDoubleToScalar(line[1].fY * test.fX);
51 int p1Bits = SkFloatAs2sCompliment(p1);
52 int p2Bits = SkFloatAs2sCompliment(p2);
bungeman60e0fee2015-08-26 05:15:46 -070053 int epsilon = SkTAbs(p1Bits - p2Bits);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000054 if (maxEpsilon < epsilon) {
55 SkDebugf("line={{0, 0}, {%1.7g, %1.7g}} t=%1.7g pt={%1.7g, %1.7g}"
56 " epsilon=%d\n",
57 line[1].fX, line[1].fY, t, test.fX, test.fY, epsilon);
58 maxEpsilon = epsilon;
59 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +000060 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +000061 }
62 }
63 }
64}
65
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000066DEF_TEST(PathOpsAngleFindQuadEpsilon, reporter) {
67 if (gDisableAngleTests) {
68 return;
69 }
70 SkRandom ran;
71 int maxEpsilon = 0;
72 double maxAngle = 0;
73 for (int index = 0; index < 100000; ++index) {
74 SkDLine line = {{{0, 0}, {ran.nextRangeF(0.0001f, 1000), ran.nextRangeF(0.0001f, 1000)}}};
75 float t = ran.nextRangeF(0.0001f, 1);
76 SkDPoint dPt = line.ptAtT(t);
77 float t2 = ran.nextRangeF(0.0001f, 1);
78 SkDPoint qPt = line.ptAtT(t2);
79 float t3 = ran.nextRangeF(0.0001f, 1);
80 SkDPoint qPt2 = line.ptAtT(t3);
81 qPt.fX += qPt2.fY;
82 qPt.fY -= qPt2.fX;
caryclarka35ab3e2016-10-20 08:32:18 -070083 QuadPts q = {{line[0], dPt, qPt}};
84 SkDQuad quad;
85 quad.debugSet(q.fPts);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +000086 // binary search for maximum movement of quad[1] towards test that still has 1 intersection
87 double moveT = 0.5f;
88 double deltaT = moveT / 2;
89 SkDPoint last;
90 do {
91 last = quad[1];
92 quad[1].fX = dPt.fX - line[1].fY * moveT;
93 quad[1].fY = dPt.fY + line[1].fX * moveT;
94 SkIntersections i;
95 i.intersect(quad, line);
96 REPORTER_ASSERT(reporter, i.used() > 0);
97 if (i.used() == 1) {
98 moveT += deltaT;
99 } else {
100 moveT -= deltaT;
101 }
102 deltaT /= 2;
103 } while (last.asSkPoint() != quad[1].asSkPoint());
104 float p1 = SkDoubleToScalar(line[1].fX * last.fY);
105 float p2 = SkDoubleToScalar(line[1].fY * last.fX);
106 int p1Bits = SkFloatAs2sCompliment(p1);
107 int p2Bits = SkFloatAs2sCompliment(p2);
bungeman60e0fee2015-08-26 05:15:46 -0700108 int epsilon = SkTAbs(p1Bits - p2Bits);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000109 if (maxEpsilon < epsilon) {
110 SkDebugf("line={{0, 0}, {%1.7g, %1.7g}} t=%1.7g/%1.7g/%1.7g moveT=%1.7g"
111 " pt={%1.7g, %1.7g} epsilon=%d\n",
112 line[1].fX, line[1].fY, t, t2, t3, moveT, last.fX, last.fY, epsilon);
113 maxEpsilon = epsilon;
114 }
115 double a1 = atan2(line[1].fY, line[1].fX);
116 double a2 = atan2(last.fY, last.fX);
117 double angle = fabs(a1 - a2);
118 if (maxAngle < angle) {
119 SkDebugf("line={{0, 0}, {%1.7g, %1.7g}} t=%1.7g/%1.7g/%1.7g moveT=%1.7g"
120 " pt={%1.7g, %1.7g} angle=%1.7g\n",
121 line[1].fX, line[1].fY, t, t2, t3, moveT, last.fX, last.fY, angle);
122 maxAngle = angle;
123 }
caryclark@google.com07e97fc2013-07-08 17:17:02 +0000124 }
125}
126
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000127static int find_slop(double x, double y, double rx, double ry) {
128 int slopBits = 0;
129 bool less1, less2;
130 double absX = fabs(x);
131 double absY = fabs(y);
132 double length = absX < absY ? absX / 2 + absY : absX + absY / 2;
133 int exponent;
134 (void) frexp(length, &exponent);
135 double epsilon = ldexp(FLT_EPSILON, exponent);
136 do {
137 // get the length as the larger plus half the smaller (both same signs)
138 // find the ulps of the length
139 // compute the offsets from there
140 double xSlop = epsilon * slopBits;
141 double ySlop = x * y < 0 ? -xSlop : xSlop; // OPTIMIZATION: use copysign / _copysign ?
142 double x1 = x - xSlop;
143 double y1 = y + ySlop;
144 double x_ry1 = x1 * ry;
145 double rx_y1 = rx * y1;
146 less1 = x_ry1 < rx_y1;
147 double x2 = x + xSlop;
148 double y2 = y - ySlop;
149 double x_ry2 = x2 * ry;
150 double rx_y2 = rx * y2;
151 less2 = x_ry2 < rx_y2;
152 } while (less1 == less2 && ++slopBits);
153 return slopBits;
154}
155
156// from http://stackoverflow.com/questions/1427422/cheap-algorithm-to-find-measure-of-angle-between-vectors
157static double diamond_angle(double y, double x)
158{
159 if (y >= 0)
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +0000160 return (x >= 0 ? y/(x+y) : 1-x/(-x+y));
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000161 else
skia.committer@gmail.com8f6ef402013-06-05 07:01:06 +0000162 return (x < 0 ? 2-y/(-x-y) : 3+x/(x-y));
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000163}
164
165static const double slopTests[][4] = {
166 // x y rx ry
167 {-0.058554756452593892, -0.18804585843827226, -0.018568569646021160, -0.059615294434479438},
168 {-0.0013717412948608398, 0.0041152238845825195, -0.00045837944195925573, 0.0013753175735478074},
169 {-2.1033774145221198, -1.4046019261273715e-008, -0.70062688352066704, -1.2706324683777995e-008},
170};
171
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000172DEF_TEST(PathOpsAngleFindSlop, reporter) {
173 if (gDisableAngleTests) {
174 return;
175 }
176 for (int index = 0; index < (int) SK_ARRAY_COUNT(slopTests); ++index) {
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000177 const double* slopTest = slopTests[index];
178 double x = slopTest[0];
179 double y = slopTest[1];
180 double rx = slopTest[2];
181 double ry = slopTest[3];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000182 SkDebugf("%s xy %d=%d\n", __FUNCTION__, index, find_slop(x, y, rx, ry));
183 SkDebugf("%s rxy %d=%d\n", __FUNCTION__, index, find_slop(rx, ry, x, y));
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000184 double angle = diamond_angle(y, x);
185 double rAngle = diamond_angle(ry, rx);
186 double diff = fabs(angle - rAngle);
187 SkDebugf("%s diamond xy=%1.9g rxy=%1.9g diff=%1.9g factor=%d\n", __FUNCTION__,
188 angle, rAngle, diff, (int) (diff / FLT_EPSILON));
caryclark@google.comcffbcc32013-06-04 17:59:42 +0000189 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000190}
191
192class PathOpsAngleTester {
193public:
caryclark54359292015-03-26 07:52:43 -0700194 static int After(SkOpAngle& lh, SkOpAngle& rh) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000195 return lh.after(&rh);
196 }
197
caryclark55888e42016-07-18 10:01:36 -0700198 static int AllOnOneSide(SkOpAngle& lh, SkOpAngle& rh) {
Cary Clark9d6049a2018-12-21 13:13:10 -0500199 return lh.lineOnOneSide(&rh, false);
caryclark55888e42016-07-18 10:01:36 -0700200 }
201
caryclark54359292015-03-26 07:52:43 -0700202 static int ConvexHullOverlaps(SkOpAngle& lh, SkOpAngle& rh) {
203 return lh.convexHullOverlaps(&rh);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000204 }
205
caryclark54359292015-03-26 07:52:43 -0700206 static int Orderable(SkOpAngle& lh, SkOpAngle& rh) {
207 return lh.orderable(&rh);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000208 }
209
caryclark54359292015-03-26 07:52:43 -0700210 static int EndsIntersect(SkOpAngle& lh, SkOpAngle& rh) {
211 return lh.endsIntersect(&rh);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000212 }
213
214 static void SetNext(SkOpAngle& lh, SkOpAngle& rh) {
215 lh.fNext = &rh;
216 }
217};
218
219class PathOpsSegmentTester {
220public:
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000221 static void DebugReset(SkOpSegment* segment) {
222 segment->debugReset();
223 }
224};
225
226struct CircleData {
caryclarka35ab3e2016-10-20 08:32:18 -0700227 const CubicPts fPts;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000228 const int fPtCount;
229 SkPoint fShortPts[4];
230};
231
232static CircleData circleDataSet[] = {
233 { {{{313.0155029296875, 207.90290832519531}, {320.05078125, 227.58743286132812}}}, 2, {} },
234 { {{{313.0155029296875, 207.90290832519531}, {313.98246891063195, 219.33615203830394},
235 {320.05078125, 227.58743286132812}}}, 3, {} },
236};
237
238static const int circleDataSetSize = (int) SK_ARRAY_COUNT(circleDataSet);
239
240DEF_TEST(PathOpsAngleCircle, reporter) {
Florin Malita14a64302017-05-24 14:53:44 -0400241 SkSTArenaAlloc<4096> allocator;
caryclark624637c2015-05-11 07:21:27 -0700242 SkOpContourHead contour;
caryclark55888e42016-07-18 10:01:36 -0700243 SkOpGlobalState state(&contour, &allocator SkDEBUGPARAMS(false) SkDEBUGPARAMS(nullptr));
caryclark54359292015-03-26 07:52:43 -0700244 contour.init(&state, false, false);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000245 for (int index = 0; index < circleDataSetSize; ++index) {
246 CircleData& data = circleDataSet[index];
247 for (int idx2 = 0; idx2 < data.fPtCount; ++idx2) {
248 data.fShortPts[idx2] = data.fPts.fPts[idx2].asSkPoint();
249 }
250 switch (data.fPtCount) {
251 case 2:
caryclark55888e42016-07-18 10:01:36 -0700252 contour.addLine(data.fShortPts);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000253 break;
254 case 3:
caryclark55888e42016-07-18 10:01:36 -0700255 contour.addQuad(data.fShortPts);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000256 break;
257 case 4:
caryclark55888e42016-07-18 10:01:36 -0700258 contour.addCubic(data.fShortPts);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000259 break;
260 }
261 }
caryclark54359292015-03-26 07:52:43 -0700262 SkOpSegment* first = contour.first();
caryclark55888e42016-07-18 10:01:36 -0700263 first->debugAddAngle(0, 1);
caryclark54359292015-03-26 07:52:43 -0700264 SkOpSegment* next = first->next();
caryclark55888e42016-07-18 10:01:36 -0700265 next->debugAddAngle(0, 1);
caryclark54359292015-03-26 07:52:43 -0700266 PathOpsAngleTester::Orderable(*first->debugLastAngle(), *next->debugLastAngle());
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000267}
268
269struct IntersectData {
caryclarka35ab3e2016-10-20 08:32:18 -0700270 const CubicPts fPts;
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000271 const int fPtCount;
272 double fTStart;
273 double fTEnd;
274 SkPoint fShortPts[4];
275};
276
277static IntersectData intersectDataSet1[] = {
278 { {{{322.935669,231.030273}, {312.832214,220.393295}, {312.832214,203.454178}}}, 3,
279 0.865309956, 0.154740299, {} },
280 { {{{322.12738,233.397751}, {295.718353,159.505829}}}, 2,
281 0.345028807, 0.0786326511, {} },
282 { {{{322.935669,231.030273}, {312.832214,220.393295}, {312.832214,203.454178}}}, 3,
283 0.865309956, 1, {} },
284 { {{{322.12738,233.397751}, {295.718353,159.505829}}}, 2,
285 0.345028807, 1, {} },
286};
287
288static IntersectData intersectDataSet2[] = {
289 { {{{364.390686,157.898193}, {375.281769,136.674606}, {396.039917,136.674606}}}, 3,
290 0.578520747, 1, {} },
291 { {{{364.390686,157.898193}, {375.281769,136.674606}, {396.039917,136.674606}}}, 3,
292 0.578520747, 0.536512973, {} },
293 { {{{366.608826,151.196014}, {378.803101,136.674606}, {398.164948,136.674606}}}, 3,
294 0.490456543, 1, {} },
295};
296
297static IntersectData intersectDataSet3[] = {
298 { {{{2.000000,0.000000}, {1.33333333,0.66666667}}}, 2, 1, 0, {} },
299 { {{{1.33333333,0.66666667}, {0.000000,2.000000}}}, 2, 0, 0.25, {} },
300 { {{{2.000000,2.000000}, {1.33333333,0.66666667}}}, 2, 1, 0, {} },
301};
302
303static IntersectData intersectDataSet4[] = {
304 { {{{1.3333333,0.6666667}, {0.000,2.000}}}, 2, 0.250000006, 0, {} },
305 { {{{1.000,0.000}, {1.000,1.000}}}, 2, 1, 0, {} },
306 { {{{1.000,1.000}, {0.000,0.000}}}, 2, 0, 1, {} },
307};
308
309static IntersectData intersectDataSet5[] = {
310 { {{{0.000,0.000}, {1.000,0.000}, {1.000,1.000}}}, 3, 1, 0.666666667, {} },
311 { {{{0.000,0.000}, {2.000,1.000}, {0.000,2.000}}}, 3, 0.5, 1, {} },
312 { {{{0.000,0.000}, {2.000,1.000}, {0.000,2.000}}}, 3, 0.5, 0, {} },
313};
314
315static IntersectData intersectDataSet6[] = { // pathops_visualizer.htm:3658
316 { {{{0.000,1.000}, {3.000,4.000}, {1.000,0.000}, {3.000,0.000}}}, 4, 0.0925339054, 0, {} }, // pathops_visualizer.htm:3616
317 { {{{0.000,1.000}, {0.000,3.000}, {1.000,0.000}, {4.000,3.000}}}, 4, 0.453872386, 0, {} }, // pathops_visualizer.htm:3616
318 { {{{0.000,1.000}, {3.000,4.000}, {1.000,0.000}, {3.000,0.000}}}, 4, 0.0925339054, 0.417096368, {} }, // pathops_visualizer.htm:3616
319};
320
321static IntersectData intersectDataSet7[] = { // pathops_visualizer.htm:3748
322 { {{{2.000,1.000}, {0.000,1.000}}}, 2, 0.5, 0, {} }, // pathops_visualizer.htm:3706
323 { {{{2.000,0.000}, {0.000,2.000}}}, 2, 0.5, 1, {} }, // pathops_visualizer.htm:3706
324 { {{{0.000,1.000}, {0.000,2.000}, {2.000,0.000}, {2.000,1.000}}}, 4, 0.5, 1, {} }, // pathops_visualizer.htm:3706
325}; //
326
327static IntersectData intersectDataSet8[] = { // pathops_visualizer.htm:4194
328 { {{{0.000,1.000}, {2.000,3.000}, {5.000,1.000}, {4.000,3.000}}}, 4, 0.311007457, 0.285714286, {} }, // pathops_visualizer.htm:4152
329 { {{{1.000,5.000}, {3.000,4.000}, {1.000,0.000}, {3.000,2.000}}}, 4, 0.589885081, 0.999982974, {} }, // pathops_visualizer.htm:4152
330 { {{{1.000,5.000}, {3.000,4.000}, {1.000,0.000}, {3.000,2.000}}}, 4, 0.589885081, 0.576935809, {} }, // pathops_visualizer.htm:4152
331}; //
332
333static IntersectData intersectDataSet9[] = { // pathops_visualizer.htm:4142
334 { {{{0.000,1.000}, {2.000,3.000}, {5.000,1.000}, {4.000,3.000}}}, 4, 0.476627072, 0.311007457, {} }, // pathops_visualizer.htm:4100
335 { {{{1.000,5.000}, {3.000,4.000}, {1.000,0.000}, {3.000,2.000}}}, 4, 0.999982974, 1, {} }, // pathops_visualizer.htm:4100
336 { {{{0.000,1.000}, {2.000,3.000}, {5.000,1.000}, {4.000,3.000}}}, 4, 0.476627072, 1, {} }, // pathops_visualizer.htm:4100
337}; //
338
339static IntersectData intersectDataSet10[] = { // pathops_visualizer.htm:4186
340 { {{{0.000,1.000}, {1.000,6.000}, {1.000,0.000}, {1.000,0.000}}}, 4, 0.788195121, 0.726275769, {} }, // pathops_visualizer.htm:4144
341 { {{{0.000,1.000}, {0.000,1.000}, {1.000,0.000}, {6.000,1.000}}}, 4, 0.473378977, 1, {} }, // pathops_visualizer.htm:4144
342 { {{{0.000,1.000}, {1.000,6.000}, {1.000,0.000}, {1.000,0.000}}}, 4, 0.788195121, 1, {} }, // pathops_visualizer.htm:4144
343}; //
344
345static IntersectData intersectDataSet11[] = { // pathops_visualizer.htm:4704
346 { {{{979.305,561.000}, {1036.695,291.000}}}, 2, 0.888888874, 0.11111108, {} }, // pathops_visualizer.htm:4662
347 { {{{1006.695,291.000}, {1023.264,291.000}, {1033.840,304.431}, {1030.318,321.000}}}, 4, 1, 0, {} }, // pathops_visualizer.htm:4662
348 { {{{979.305,561.000}, {1036.695,291.000}}}, 2, 0.888888874, 1, {} }, // pathops_visualizer.htm:4662
349}; //
350
351static IntersectData intersectDataSet12[] = { // pathops_visualizer.htm:5481
352 { {{{67.000,912.000}, {67.000,913.000}}}, 2, 1, 0, {} }, // pathops_visualizer.htm:5439
353 { {{{67.000,913.000}, {67.000,917.389}, {67.224,921.726}, {67.662,926.000}}}, 4, 0, 1, {} }, // pathops_visualizer.htm:5439
354 { {{{194.000,1041.000}, {123.860,1041.000}, {67.000,983.692}, {67.000,913.000}}}, 4, 1, 0, {} }, // pathops_visualizer.htm:5439
355}; //
356
357static IntersectData intersectDataSet13[] = { // pathops_visualizer.htm:5735
358 { {{{6.000,0.000}, {0.000,4.000}}}, 2, 0.625, 0.25, {} }, // pathops_visualizer.htm:5693
359 { {{{0.000,1.000}, {0.000,6.000}, {4.000,0.000}, {6.000,1.000}}}, 4, 0.5, 0.833333333, {} }, // pathops_visualizer.htm:5693
360 { {{{0.000,1.000}, {0.000,6.000}, {4.000,0.000}, {6.000,1.000}}}, 4, 0.5, 0.379043969, {} }, // pathops_visualizer.htm:5693
361}; //
362
363static IntersectData intersectDataSet14[] = { // pathops_visualizer.htm:5875
364 { {{{0.000,1.000}, {4.000,6.000}, {2.000,1.000}, {2.000,0.000}}}, 4, 0.0756502183, 0.0594570973, {} }, // pathops_visualizer.htm:5833
365 { {{{1.000,2.000}, {0.000,2.000}, {1.000,0.000}, {6.000,4.000}}}, 4, 0.0756502184, 0, {} }, // pathops_visualizer.htm:5833
366 { {{{0.000,1.000}, {4.000,6.000}, {2.000,1.000}, {2.000,0.000}}}, 4, 0.0756502183, 0.531917258, {} }, // pathops_visualizer.htm:5833
367}; //
368
369static IntersectData intersectDataSet15[] = { // pathops_visualizer.htm:6580
370 { {{{490.435,879.407}, {405.593,909.436}}}, 2, 0.500554405, 1, {} }, // pathops_visualizer.htm:6538
371 { {{{447.967,894.438}, {448.007,894.424}, {448.014,894.422}}}, 3, 0, 1, {} }, // pathops_visualizer.htm:6538
372 { {{{490.435,879.407}, {405.593,909.436}}}, 2, 0.500554405, 0.500000273, {} }, // pathops_visualizer.htm:6538
skia.committer@gmail.coma1ed7ae2014-04-15 03:04:18 +0000373}; //
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000374
375static IntersectData intersectDataSet16[] = { // pathops_visualizer.htm:7419
376 { {{{1.000,4.000}, {4.000,5.000}, {3.000,2.000}, {6.000,3.000}}}, 4, 0.5, 0, {} }, // pathops_visualizer.htm:7377
377 { {{{2.000,3.000}, {3.000,6.000}, {4.000,1.000}, {5.000,4.000}}}, 4, 0.5, 0.112701665, {} }, // pathops_visualizer.htm:7377
378 { {{{5.000,4.000}, {2.000,3.000}}}, 2, 0.5, 0, {} }, // pathops_visualizer.htm:7377
379}; //
380
caryclark54359292015-03-26 07:52:43 -0700381// from skpi_gino_com_16
382static IntersectData intersectDataSet17[] = {
383 { /*seg=7*/ {{{270.974121f, 770.025879f}, {234.948273f, 734}, {184, 734}}}
384 , 3, 0.74590454, 0.547660352, {} },
385 { /*seg=8*/ {{{185, 734}, {252.93103f, 734}, {308, 789.06897f}, {308, 857}}}
386 , 4, 0.12052623, 0, {} },
387 { /*seg=7*/ {{{270.974121f, 770.025879f}, {234.948273f, 734}, {184, 734}}}
388 , 3, 0.74590454, 1, {} },
389};
390
391static IntersectData intersectDataSet18[] = {
392 { /*seg=7*/ {{{270.974121f, 770.025879f}, {234.948273f, 734}, {184, 734}}}
393 , 3, 0.74590454, 1, {} },
394 { /*seg=8*/ {{{185, 734}, {252.93103f, 734}, {308, 789.06897f}, {308, 857}}}
395 , 4, 0.12052623, 0.217351928, {} },
396 { /*seg=7*/ {{{270.974121f, 770.025879f}, {234.948273f, 734}, {184, 734}}}
397 , 3, 0.74590454, 0.547660352, {} },
398};
399
400static IntersectData intersectDataSet19[] = {
401 { /*seg=1*/ {{{0, 1}, {3, 5}, {2, 1}, {3, 1}}}
402 , 4, 0.135148995, 0.134791946, {} },
403 { /*seg=3*/ {{{1, 2}, {1, 2.15061641f}, {1, 2.21049166f}, {1.01366711f, 2.21379328f}}}
404 , 4, 0.956740456, 0.894913214, {} },
405 { /*seg=1*/ {{{0, 1}, {3, 5}, {2, 1}, {3, 1}}}
406 , 4, 0.135148995, 0.551812363, {} },
407};
408
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000409#define I(x) intersectDataSet##x
410
411static IntersectData* intersectDataSets[] = {
412 I(1), I(2), I(3), I(4), I(5), I(6), I(7), I(8), I(9), I(10),
caryclark54359292015-03-26 07:52:43 -0700413 I(11), I(12), I(13), I(14), I(15), I(16), I(17), I(18), I(19),
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000414};
415
416#undef I
417#define I(x) (int) SK_ARRAY_COUNT(intersectDataSet##x)
418
419static const int intersectDataSetSizes[] = {
420 I(1), I(2), I(3), I(4), I(5), I(6), I(7), I(8), I(9), I(10),
caryclark54359292015-03-26 07:52:43 -0700421 I(11), I(12), I(13), I(14), I(15), I(16), I(17), I(18), I(19),
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000422};
423
424#undef I
425
426static const int intersectDataSetsSize = (int) SK_ARRAY_COUNT(intersectDataSetSizes);
427
caryclark54359292015-03-26 07:52:43 -0700428struct FourPoints {
429 SkPoint pts[4];
430};
431
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000432DEF_TEST(PathOpsAngleAfter, reporter) {
Florin Malita14a64302017-05-24 14:53:44 -0400433 SkSTArenaAlloc<4096> allocator;
caryclark624637c2015-05-11 07:21:27 -0700434 SkOpContourHead contour;
caryclark55888e42016-07-18 10:01:36 -0700435 SkOpGlobalState state(&contour, &allocator SkDEBUGPARAMS(false) SkDEBUGPARAMS(nullptr));
caryclark54359292015-03-26 07:52:43 -0700436 contour.init(&state, false, false);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000437 for (int index = intersectDataSetsSize - 1; index >= 0; --index) {
438 IntersectData* dataArray = intersectDataSets[index];
439 const int dataSize = intersectDataSetSizes[index];
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000440 for (int index2 = 0; index2 < dataSize - 2; ++index2) {
caryclark54359292015-03-26 07:52:43 -0700441 allocator.reset();
442 contour.reset();
443 for (int index3 = 0; index3 < 3; ++index3) {
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000444 IntersectData& data = dataArray[index2 + index3];
Herb Derbyecc364c2017-04-19 15:09:48 -0400445 SkPoint* temp = (SkPoint*) allocator.make<FourPoints>();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000446 for (int idx2 = 0; idx2 < data.fPtCount; ++idx2) {
447 temp[idx2] = data.fPts.fPts[idx2].asSkPoint();
448 }
449 switch (data.fPtCount) {
450 case 2: {
caryclark55888e42016-07-18 10:01:36 -0700451 contour.addLine(temp);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000452 } break;
453 case 3: {
caryclark55888e42016-07-18 10:01:36 -0700454 contour.addQuad(temp);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000455 } break;
456 case 4: {
caryclark55888e42016-07-18 10:01:36 -0700457 contour.addCubic(temp);
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000458 } break;
459 }
460 }
caryclark54359292015-03-26 07:52:43 -0700461 SkOpSegment* seg1 = contour.first();
caryclark55888e42016-07-18 10:01:36 -0700462 seg1->debugAddAngle(dataArray[index2 + 0].fTStart, dataArray[index2 + 0].fTEnd);
caryclark54359292015-03-26 07:52:43 -0700463 SkOpSegment* seg2 = seg1->next();
caryclark55888e42016-07-18 10:01:36 -0700464 seg2->debugAddAngle(dataArray[index2 + 1].fTStart, dataArray[index2 + 1].fTEnd);
caryclark54359292015-03-26 07:52:43 -0700465 SkOpSegment* seg3 = seg2->next();
caryclark55888e42016-07-18 10:01:36 -0700466 seg3->debugAddAngle(dataArray[index2 + 2].fTStart, dataArray[index2 + 2].fTEnd);
caryclark54359292015-03-26 07:52:43 -0700467 SkOpAngle& angle1 = *seg1->debugLastAngle();
468 SkOpAngle& angle2 = *seg2->debugLastAngle();
469 SkOpAngle& angle3 = *seg3->debugLastAngle();
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000470 PathOpsAngleTester::SetNext(angle1, angle3);
471 // These data sets are seeded when the set itself fails, so likely the dataset does not
472 // match the expected result. The tests above return 1 when first added, but
473 // return 0 after the bug is fixed.
474 SkDEBUGCODE(int result =) PathOpsAngleTester::After(angle2, angle1);
475 SkASSERT(result == 0 || result == 1);
476 }
477 }
478}
479
caryclark55888e42016-07-18 10:01:36 -0700480void SkOpSegment::debugAddAngle(double startT, double endT) {
caryclark54359292015-03-26 07:52:43 -0700481 SkOpPtT* startPtT = startT == 0 ? fHead.ptT() : startT == 1 ? fTail.ptT()
caryclark29b25632016-08-25 11:27:17 -0700482 : this->addT(startT);
caryclark54359292015-03-26 07:52:43 -0700483 SkOpPtT* endPtT = endT == 0 ? fHead.ptT() : endT == 1 ? fTail.ptT()
caryclark29b25632016-08-25 11:27:17 -0700484 : this->addT(endT);
Herb Derbyecc364c2017-04-19 15:09:48 -0400485 SkOpAngle* angle = this->globalState()->allocator()->make<SkOpAngle>();
caryclark54359292015-03-26 07:52:43 -0700486 SkOpSpanBase* startSpan = &fHead;
487 while (startSpan->ptT() != startPtT) {
488 startSpan = startSpan->upCast()->next();
489 }
490 SkOpSpanBase* endSpan = &fHead;
491 while (endSpan->ptT() != endPtT) {
492 endSpan = endSpan->upCast()->next();
493 }
494 angle->set(startSpan, endSpan);
495 if (startT < endT) {
496 startSpan->upCast()->setToAngle(angle);
497 endSpan->setFromAngle(angle);
498 } else {
499 endSpan->upCast()->setToAngle(angle);
500 startSpan->setFromAngle(angle);
501 }
commit-bot@chromium.org4431e772014-04-14 17:08:59 +0000502}
caryclark55888e42016-07-18 10:01:36 -0700503
504DEF_TEST(PathOpsAngleAllOnOneSide, reporter) {
Florin Malita14a64302017-05-24 14:53:44 -0400505 SkSTArenaAlloc<4096> allocator;
caryclark55888e42016-07-18 10:01:36 -0700506 SkOpContourHead contour;
507 SkOpGlobalState state(&contour, &allocator SkDEBUGPARAMS(false) SkDEBUGPARAMS(nullptr));
508 contour.init(&state, false, false);
509 SkPoint conicPts[3] = {{494.37100219726562f, 224.66200256347656f},
510 {494.37360910682298f, 224.6729026561527f},
511 {494.37600708007813f, 224.68400573730469f}};
512 SkPoint linePts[2] = {{494.371002f, 224.662003f}, {494.375000f, 224.675995f}};
513 for (int i = 10; i >= 0; --i) {
514 SkPoint modLinePts[2] = { linePts[0], linePts[1] };
515 modLinePts[1].fX += i * .1f;
516 contour.addLine(modLinePts);
517 contour.addQuad(conicPts);
518 // contour.addConic(conicPts, 0.999935746f, &allocator);
519 SkOpSegment* first = contour.first();
520 first->debugAddAngle(0, 1);
521 SkOpSegment* next = first->next();
522 next->debugAddAngle(0, 1);
523 /* int result = */
524 PathOpsAngleTester::AllOnOneSide(*first->debugLastAngle(), *next->debugLastAngle());
525 // SkDebugf("i=%d result=%d\n", i , result);
526 // SkDebugf("");
527 }
528}