PathOpsAngleTest.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*
  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. */
  7. #include "include/utils/SkRandom.h"
  8. #include "src/core/SkTSort.h"
  9. #include "src/pathops/SkIntersections.h"
  10. #include "src/pathops/SkOpContour.h"
  11. #include "src/pathops/SkOpSegment.h"
  12. #include "tests/PathOpsTestCommon.h"
  13. #include "tests/Test.h"
  14. static bool gDisableAngleTests = true;
  15. static float next(float f)
  16. {
  17. int fBits = SkFloatAs2sCompliment(f);
  18. ++fBits;
  19. float fNext = Sk2sComplimentAsFloat(fBits);
  20. return fNext;
  21. }
  22. static float prev(float f)
  23. {
  24. int fBits = SkFloatAs2sCompliment(f);
  25. --fBits;
  26. float fNext = Sk2sComplimentAsFloat(fBits);
  27. return fNext;
  28. }
  29. DEF_TEST(PathOpsAngleFindCrossEpsilon, reporter) {
  30. if (gDisableAngleTests) {
  31. return;
  32. }
  33. SkRandom ran;
  34. int maxEpsilon = 0;
  35. for (int index = 0; index < 10000000; ++index) {
  36. SkDLine line = {{{0, 0}, {ran.nextRangeF(0.0001f, 1000), ran.nextRangeF(0.0001f, 1000)}}};
  37. for (int inner = 0; inner < 10; ++inner) {
  38. float t = ran.nextRangeF(0.0001f, 1);
  39. SkDPoint dPt = line.ptAtT(t);
  40. SkPoint pt = dPt.asSkPoint();
  41. float xs[3] = { prev(pt.fX), pt.fX, next(pt.fX) };
  42. float ys[3] = { prev(pt.fY), pt.fY, next(pt.fY) };
  43. for (int xIdx = 0; xIdx < 3; ++xIdx) {
  44. for (int yIdx = 0; yIdx < 3; ++yIdx) {
  45. SkPoint test = { xs[xIdx], ys[yIdx] };
  46. float p1 = SkDoubleToScalar(line[1].fX * test.fY);
  47. float p2 = SkDoubleToScalar(line[1].fY * test.fX);
  48. int p1Bits = SkFloatAs2sCompliment(p1);
  49. int p2Bits = SkFloatAs2sCompliment(p2);
  50. int epsilon = SkTAbs(p1Bits - p2Bits);
  51. if (maxEpsilon < epsilon) {
  52. SkDebugf("line={{0, 0}, {%1.7g, %1.7g}} t=%1.7g pt={%1.7g, %1.7g}"
  53. " epsilon=%d\n",
  54. line[1].fX, line[1].fY, t, test.fX, test.fY, epsilon);
  55. maxEpsilon = epsilon;
  56. }
  57. }
  58. }
  59. }
  60. }
  61. }
  62. DEF_TEST(PathOpsAngleFindQuadEpsilon, reporter) {
  63. if (gDisableAngleTests) {
  64. return;
  65. }
  66. SkRandom ran;
  67. int maxEpsilon = 0;
  68. double maxAngle = 0;
  69. for (int index = 0; index < 100000; ++index) {
  70. SkDLine line = {{{0, 0}, {ran.nextRangeF(0.0001f, 1000), ran.nextRangeF(0.0001f, 1000)}}};
  71. float t = ran.nextRangeF(0.0001f, 1);
  72. SkDPoint dPt = line.ptAtT(t);
  73. float t2 = ran.nextRangeF(0.0001f, 1);
  74. SkDPoint qPt = line.ptAtT(t2);
  75. float t3 = ran.nextRangeF(0.0001f, 1);
  76. SkDPoint qPt2 = line.ptAtT(t3);
  77. qPt.fX += qPt2.fY;
  78. qPt.fY -= qPt2.fX;
  79. QuadPts q = {{line[0], dPt, qPt}};
  80. SkDQuad quad;
  81. quad.debugSet(q.fPts);
  82. // binary search for maximum movement of quad[1] towards test that still has 1 intersection
  83. double moveT = 0.5f;
  84. double deltaT = moveT / 2;
  85. SkDPoint last;
  86. do {
  87. last = quad[1];
  88. quad[1].fX = dPt.fX - line[1].fY * moveT;
  89. quad[1].fY = dPt.fY + line[1].fX * moveT;
  90. SkIntersections i;
  91. i.intersect(quad, line);
  92. REPORTER_ASSERT(reporter, i.used() > 0);
  93. if (i.used() == 1) {
  94. moveT += deltaT;
  95. } else {
  96. moveT -= deltaT;
  97. }
  98. deltaT /= 2;
  99. } while (last.asSkPoint() != quad[1].asSkPoint());
  100. float p1 = SkDoubleToScalar(line[1].fX * last.fY);
  101. float p2 = SkDoubleToScalar(line[1].fY * last.fX);
  102. int p1Bits = SkFloatAs2sCompliment(p1);
  103. int p2Bits = SkFloatAs2sCompliment(p2);
  104. int epsilon = SkTAbs(p1Bits - p2Bits);
  105. if (maxEpsilon < epsilon) {
  106. SkDebugf("line={{0, 0}, {%1.7g, %1.7g}} t=%1.7g/%1.7g/%1.7g moveT=%1.7g"
  107. " pt={%1.7g, %1.7g} epsilon=%d\n",
  108. line[1].fX, line[1].fY, t, t2, t3, moveT, last.fX, last.fY, epsilon);
  109. maxEpsilon = epsilon;
  110. }
  111. double a1 = atan2(line[1].fY, line[1].fX);
  112. double a2 = atan2(last.fY, last.fX);
  113. double angle = fabs(a1 - a2);
  114. if (maxAngle < angle) {
  115. SkDebugf("line={{0, 0}, {%1.7g, %1.7g}} t=%1.7g/%1.7g/%1.7g moveT=%1.7g"
  116. " pt={%1.7g, %1.7g} angle=%1.7g\n",
  117. line[1].fX, line[1].fY, t, t2, t3, moveT, last.fX, last.fY, angle);
  118. maxAngle = angle;
  119. }
  120. }
  121. }
  122. static int find_slop(double x, double y, double rx, double ry) {
  123. int slopBits = 0;
  124. bool less1, less2;
  125. double absX = fabs(x);
  126. double absY = fabs(y);
  127. double length = absX < absY ? absX / 2 + absY : absX + absY / 2;
  128. int exponent;
  129. (void) frexp(length, &exponent);
  130. double epsilon = ldexp(FLT_EPSILON, exponent);
  131. do {
  132. // get the length as the larger plus half the smaller (both same signs)
  133. // find the ulps of the length
  134. // compute the offsets from there
  135. double xSlop = epsilon * slopBits;
  136. double ySlop = x * y < 0 ? -xSlop : xSlop; // OPTIMIZATION: use copysign / _copysign ?
  137. double x1 = x - xSlop;
  138. double y1 = y + ySlop;
  139. double x_ry1 = x1 * ry;
  140. double rx_y1 = rx * y1;
  141. less1 = x_ry1 < rx_y1;
  142. double x2 = x + xSlop;
  143. double y2 = y - ySlop;
  144. double x_ry2 = x2 * ry;
  145. double rx_y2 = rx * y2;
  146. less2 = x_ry2 < rx_y2;
  147. } while (less1 == less2 && ++slopBits);
  148. return slopBits;
  149. }
  150. // from http://stackoverflow.com/questions/1427422/cheap-algorithm-to-find-measure-of-angle-between-vectors
  151. static double diamond_angle(double y, double x)
  152. {
  153. if (y >= 0)
  154. return (x >= 0 ? y/(x+y) : 1-x/(-x+y));
  155. else
  156. return (x < 0 ? 2-y/(-x-y) : 3+x/(x-y));
  157. }
  158. static const double slopTests[][4] = {
  159. // x y rx ry
  160. {-0.058554756452593892, -0.18804585843827226, -0.018568569646021160, -0.059615294434479438},
  161. {-0.0013717412948608398, 0.0041152238845825195, -0.00045837944195925573, 0.0013753175735478074},
  162. {-2.1033774145221198, -1.4046019261273715e-008, -0.70062688352066704, -1.2706324683777995e-008},
  163. };
  164. DEF_TEST(PathOpsAngleFindSlop, reporter) {
  165. if (gDisableAngleTests) {
  166. return;
  167. }
  168. for (int index = 0; index < (int) SK_ARRAY_COUNT(slopTests); ++index) {
  169. const double* slopTest = slopTests[index];
  170. double x = slopTest[0];
  171. double y = slopTest[1];
  172. double rx = slopTest[2];
  173. double ry = slopTest[3];
  174. SkDebugf("%s xy %d=%d\n", __FUNCTION__, index, find_slop(x, y, rx, ry));
  175. SkDebugf("%s rxy %d=%d\n", __FUNCTION__, index, find_slop(rx, ry, x, y));
  176. double angle = diamond_angle(y, x);
  177. double rAngle = diamond_angle(ry, rx);
  178. double diff = fabs(angle - rAngle);
  179. SkDebugf("%s diamond xy=%1.9g rxy=%1.9g diff=%1.9g factor=%d\n", __FUNCTION__,
  180. angle, rAngle, diff, (int) (diff / FLT_EPSILON));
  181. }
  182. }
  183. class PathOpsAngleTester {
  184. public:
  185. static int After(SkOpAngle& lh, SkOpAngle& rh) {
  186. return lh.after(&rh);
  187. }
  188. static int AllOnOneSide(SkOpAngle& lh, SkOpAngle& rh) {
  189. return lh.lineOnOneSide(&rh, false);
  190. }
  191. static int ConvexHullOverlaps(SkOpAngle& lh, SkOpAngle& rh) {
  192. return lh.convexHullOverlaps(&rh);
  193. }
  194. static int Orderable(SkOpAngle& lh, SkOpAngle& rh) {
  195. return lh.orderable(&rh);
  196. }
  197. static int EndsIntersect(SkOpAngle& lh, SkOpAngle& rh) {
  198. return lh.endsIntersect(&rh);
  199. }
  200. static void SetNext(SkOpAngle& lh, SkOpAngle& rh) {
  201. lh.fNext = &rh;
  202. }
  203. };
  204. class PathOpsSegmentTester {
  205. public:
  206. static void DebugReset(SkOpSegment* segment) {
  207. segment->debugReset();
  208. }
  209. };
  210. struct CircleData {
  211. const CubicPts fPts;
  212. const int fPtCount;
  213. SkPoint fShortPts[4];
  214. };
  215. static CircleData circleDataSet[] = {
  216. { {{{313.0155029296875, 207.90290832519531}, {320.05078125, 227.58743286132812}}}, 2, {} },
  217. { {{{313.0155029296875, 207.90290832519531}, {313.98246891063195, 219.33615203830394},
  218. {320.05078125, 227.58743286132812}}}, 3, {} },
  219. };
  220. static const int circleDataSetSize = (int) SK_ARRAY_COUNT(circleDataSet);
  221. DEF_TEST(PathOpsAngleCircle, reporter) {
  222. SkSTArenaAlloc<4096> allocator;
  223. SkOpContourHead contour;
  224. SkOpGlobalState state(&contour, &allocator SkDEBUGPARAMS(false) SkDEBUGPARAMS(nullptr));
  225. contour.init(&state, false, false);
  226. for (int index = 0; index < circleDataSetSize; ++index) {
  227. CircleData& data = circleDataSet[index];
  228. for (int idx2 = 0; idx2 < data.fPtCount; ++idx2) {
  229. data.fShortPts[idx2] = data.fPts.fPts[idx2].asSkPoint();
  230. }
  231. switch (data.fPtCount) {
  232. case 2:
  233. contour.addLine(data.fShortPts);
  234. break;
  235. case 3:
  236. contour.addQuad(data.fShortPts);
  237. break;
  238. case 4:
  239. contour.addCubic(data.fShortPts);
  240. break;
  241. }
  242. }
  243. SkOpSegment* first = contour.first();
  244. first->debugAddAngle(0, 1);
  245. SkOpSegment* next = first->next();
  246. next->debugAddAngle(0, 1);
  247. PathOpsAngleTester::Orderable(*first->debugLastAngle(), *next->debugLastAngle());
  248. }
  249. struct IntersectData {
  250. const CubicPts fPts;
  251. const int fPtCount;
  252. double fTStart;
  253. double fTEnd;
  254. SkPoint fShortPts[4];
  255. };
  256. static IntersectData intersectDataSet1[] = {
  257. { {{{322.935669,231.030273}, {312.832214,220.393295}, {312.832214,203.454178}}}, 3,
  258. 0.865309956, 0.154740299, {} },
  259. { {{{322.12738,233.397751}, {295.718353,159.505829}}}, 2,
  260. 0.345028807, 0.0786326511, {} },
  261. { {{{322.935669,231.030273}, {312.832214,220.393295}, {312.832214,203.454178}}}, 3,
  262. 0.865309956, 1, {} },
  263. { {{{322.12738,233.397751}, {295.718353,159.505829}}}, 2,
  264. 0.345028807, 1, {} },
  265. };
  266. static IntersectData intersectDataSet2[] = {
  267. { {{{364.390686,157.898193}, {375.281769,136.674606}, {396.039917,136.674606}}}, 3,
  268. 0.578520747, 1, {} },
  269. { {{{364.390686,157.898193}, {375.281769,136.674606}, {396.039917,136.674606}}}, 3,
  270. 0.578520747, 0.536512973, {} },
  271. { {{{366.608826,151.196014}, {378.803101,136.674606}, {398.164948,136.674606}}}, 3,
  272. 0.490456543, 1, {} },
  273. };
  274. static IntersectData intersectDataSet3[] = {
  275. { {{{2.000000,0.000000}, {1.33333333,0.66666667}}}, 2, 1, 0, {} },
  276. { {{{1.33333333,0.66666667}, {0.000000,2.000000}}}, 2, 0, 0.25, {} },
  277. { {{{2.000000,2.000000}, {1.33333333,0.66666667}}}, 2, 1, 0, {} },
  278. };
  279. static IntersectData intersectDataSet4[] = {
  280. { {{{1.3333333,0.6666667}, {0.000,2.000}}}, 2, 0.250000006, 0, {} },
  281. { {{{1.000,0.000}, {1.000,1.000}}}, 2, 1, 0, {} },
  282. { {{{1.000,1.000}, {0.000,0.000}}}, 2, 0, 1, {} },
  283. };
  284. static IntersectData intersectDataSet5[] = {
  285. { {{{0.000,0.000}, {1.000,0.000}, {1.000,1.000}}}, 3, 1, 0.666666667, {} },
  286. { {{{0.000,0.000}, {2.000,1.000}, {0.000,2.000}}}, 3, 0.5, 1, {} },
  287. { {{{0.000,0.000}, {2.000,1.000}, {0.000,2.000}}}, 3, 0.5, 0, {} },
  288. };
  289. static IntersectData intersectDataSet6[] = { // pathops_visualizer.htm:3658
  290. { {{{0.000,1.000}, {3.000,4.000}, {1.000,0.000}, {3.000,0.000}}}, 4, 0.0925339054, 0, {} }, // pathops_visualizer.htm:3616
  291. { {{{0.000,1.000}, {0.000,3.000}, {1.000,0.000}, {4.000,3.000}}}, 4, 0.453872386, 0, {} }, // pathops_visualizer.htm:3616
  292. { {{{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
  293. };
  294. static IntersectData intersectDataSet7[] = { // pathops_visualizer.htm:3748
  295. { {{{2.000,1.000}, {0.000,1.000}}}, 2, 0.5, 0, {} }, // pathops_visualizer.htm:3706
  296. { {{{2.000,0.000}, {0.000,2.000}}}, 2, 0.5, 1, {} }, // pathops_visualizer.htm:3706
  297. { {{{0.000,1.000}, {0.000,2.000}, {2.000,0.000}, {2.000,1.000}}}, 4, 0.5, 1, {} }, // pathops_visualizer.htm:3706
  298. }; //
  299. static IntersectData intersectDataSet8[] = { // pathops_visualizer.htm:4194
  300. { {{{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
  301. { {{{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
  302. { {{{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
  303. }; //
  304. static IntersectData intersectDataSet9[] = { // pathops_visualizer.htm:4142
  305. { {{{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
  306. { {{{1.000,5.000}, {3.000,4.000}, {1.000,0.000}, {3.000,2.000}}}, 4, 0.999982974, 1, {} }, // pathops_visualizer.htm:4100
  307. { {{{0.000,1.000}, {2.000,3.000}, {5.000,1.000}, {4.000,3.000}}}, 4, 0.476627072, 1, {} }, // pathops_visualizer.htm:4100
  308. }; //
  309. static IntersectData intersectDataSet10[] = { // pathops_visualizer.htm:4186
  310. { {{{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
  311. { {{{0.000,1.000}, {0.000,1.000}, {1.000,0.000}, {6.000,1.000}}}, 4, 0.473378977, 1, {} }, // pathops_visualizer.htm:4144
  312. { {{{0.000,1.000}, {1.000,6.000}, {1.000,0.000}, {1.000,0.000}}}, 4, 0.788195121, 1, {} }, // pathops_visualizer.htm:4144
  313. }; //
  314. static IntersectData intersectDataSet11[] = { // pathops_visualizer.htm:4704
  315. { {{{979.305,561.000}, {1036.695,291.000}}}, 2, 0.888888874, 0.11111108, {} }, // pathops_visualizer.htm:4662
  316. { {{{1006.695,291.000}, {1023.264,291.000}, {1033.840,304.431}, {1030.318,321.000}}}, 4, 1, 0, {} }, // pathops_visualizer.htm:4662
  317. { {{{979.305,561.000}, {1036.695,291.000}}}, 2, 0.888888874, 1, {} }, // pathops_visualizer.htm:4662
  318. }; //
  319. static IntersectData intersectDataSet12[] = { // pathops_visualizer.htm:5481
  320. { {{{67.000,912.000}, {67.000,913.000}}}, 2, 1, 0, {} }, // pathops_visualizer.htm:5439
  321. { {{{67.000,913.000}, {67.000,917.389}, {67.224,921.726}, {67.662,926.000}}}, 4, 0, 1, {} }, // pathops_visualizer.htm:5439
  322. { {{{194.000,1041.000}, {123.860,1041.000}, {67.000,983.692}, {67.000,913.000}}}, 4, 1, 0, {} }, // pathops_visualizer.htm:5439
  323. }; //
  324. static IntersectData intersectDataSet13[] = { // pathops_visualizer.htm:5735
  325. { {{{6.000,0.000}, {0.000,4.000}}}, 2, 0.625, 0.25, {} }, // pathops_visualizer.htm:5693
  326. { {{{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
  327. { {{{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
  328. }; //
  329. static IntersectData intersectDataSet14[] = { // pathops_visualizer.htm:5875
  330. { {{{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
  331. { {{{1.000,2.000}, {0.000,2.000}, {1.000,0.000}, {6.000,4.000}}}, 4, 0.0756502184, 0, {} }, // pathops_visualizer.htm:5833
  332. { {{{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
  333. }; //
  334. static IntersectData intersectDataSet15[] = { // pathops_visualizer.htm:6580
  335. { {{{490.435,879.407}, {405.593,909.436}}}, 2, 0.500554405, 1, {} }, // pathops_visualizer.htm:6538
  336. { {{{447.967,894.438}, {448.007,894.424}, {448.014,894.422}}}, 3, 0, 1, {} }, // pathops_visualizer.htm:6538
  337. { {{{490.435,879.407}, {405.593,909.436}}}, 2, 0.500554405, 0.500000273, {} }, // pathops_visualizer.htm:6538
  338. }; //
  339. static IntersectData intersectDataSet16[] = { // pathops_visualizer.htm:7419
  340. { {{{1.000,4.000}, {4.000,5.000}, {3.000,2.000}, {6.000,3.000}}}, 4, 0.5, 0, {} }, // pathops_visualizer.htm:7377
  341. { {{{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
  342. { {{{5.000,4.000}, {2.000,3.000}}}, 2, 0.5, 0, {} }, // pathops_visualizer.htm:7377
  343. }; //
  344. // from skpi_gino_com_16
  345. static IntersectData intersectDataSet17[] = {
  346. { /*seg=7*/ {{{270.974121f, 770.025879f}, {234.948273f, 734}, {184, 734}}}
  347. , 3, 0.74590454, 0.547660352, {} },
  348. { /*seg=8*/ {{{185, 734}, {252.93103f, 734}, {308, 789.06897f}, {308, 857}}}
  349. , 4, 0.12052623, 0, {} },
  350. { /*seg=7*/ {{{270.974121f, 770.025879f}, {234.948273f, 734}, {184, 734}}}
  351. , 3, 0.74590454, 1, {} },
  352. };
  353. static IntersectData intersectDataSet18[] = {
  354. { /*seg=7*/ {{{270.974121f, 770.025879f}, {234.948273f, 734}, {184, 734}}}
  355. , 3, 0.74590454, 1, {} },
  356. { /*seg=8*/ {{{185, 734}, {252.93103f, 734}, {308, 789.06897f}, {308, 857}}}
  357. , 4, 0.12052623, 0.217351928, {} },
  358. { /*seg=7*/ {{{270.974121f, 770.025879f}, {234.948273f, 734}, {184, 734}}}
  359. , 3, 0.74590454, 0.547660352, {} },
  360. };
  361. static IntersectData intersectDataSet19[] = {
  362. { /*seg=1*/ {{{0, 1}, {3, 5}, {2, 1}, {3, 1}}}
  363. , 4, 0.135148995, 0.134791946, {} },
  364. { /*seg=3*/ {{{1, 2}, {1, 2.15061641f}, {1, 2.21049166f}, {1.01366711f, 2.21379328f}}}
  365. , 4, 0.956740456, 0.894913214, {} },
  366. { /*seg=1*/ {{{0, 1}, {3, 5}, {2, 1}, {3, 1}}}
  367. , 4, 0.135148995, 0.551812363, {} },
  368. };
  369. #define I(x) intersectDataSet##x
  370. static IntersectData* intersectDataSets[] = {
  371. I(1), I(2), I(3), I(4), I(5), I(6), I(7), I(8), I(9), I(10),
  372. I(11), I(12), I(13), I(14), I(15), I(16), I(17), I(18), I(19),
  373. };
  374. #undef I
  375. #define I(x) (int) SK_ARRAY_COUNT(intersectDataSet##x)
  376. static const int intersectDataSetSizes[] = {
  377. I(1), I(2), I(3), I(4), I(5), I(6), I(7), I(8), I(9), I(10),
  378. I(11), I(12), I(13), I(14), I(15), I(16), I(17), I(18), I(19),
  379. };
  380. #undef I
  381. static const int intersectDataSetsSize = (int) SK_ARRAY_COUNT(intersectDataSetSizes);
  382. struct FourPoints {
  383. SkPoint pts[4];
  384. };
  385. DEF_TEST(PathOpsAngleAfter, reporter) {
  386. SkSTArenaAlloc<4096> allocator;
  387. SkOpContourHead contour;
  388. SkOpGlobalState state(&contour, &allocator SkDEBUGPARAMS(false) SkDEBUGPARAMS(nullptr));
  389. contour.init(&state, false, false);
  390. for (int index = intersectDataSetsSize - 1; index >= 0; --index) {
  391. IntersectData* dataArray = intersectDataSets[index];
  392. const int dataSize = intersectDataSetSizes[index];
  393. for (int index2 = 0; index2 < dataSize - 2; ++index2) {
  394. allocator.reset();
  395. contour.reset();
  396. for (int index3 = 0; index3 < 3; ++index3) {
  397. IntersectData& data = dataArray[index2 + index3];
  398. SkPoint* temp = (SkPoint*) allocator.make<FourPoints>();
  399. for (int idx2 = 0; idx2 < data.fPtCount; ++idx2) {
  400. temp[idx2] = data.fPts.fPts[idx2].asSkPoint();
  401. }
  402. switch (data.fPtCount) {
  403. case 2: {
  404. contour.addLine(temp);
  405. } break;
  406. case 3: {
  407. contour.addQuad(temp);
  408. } break;
  409. case 4: {
  410. contour.addCubic(temp);
  411. } break;
  412. }
  413. }
  414. SkOpSegment* seg1 = contour.first();
  415. seg1->debugAddAngle(dataArray[index2 + 0].fTStart, dataArray[index2 + 0].fTEnd);
  416. SkOpSegment* seg2 = seg1->next();
  417. seg2->debugAddAngle(dataArray[index2 + 1].fTStart, dataArray[index2 + 1].fTEnd);
  418. SkOpSegment* seg3 = seg2->next();
  419. seg3->debugAddAngle(dataArray[index2 + 2].fTStart, dataArray[index2 + 2].fTEnd);
  420. SkOpAngle& angle1 = *seg1->debugLastAngle();
  421. SkOpAngle& angle2 = *seg2->debugLastAngle();
  422. SkOpAngle& angle3 = *seg3->debugLastAngle();
  423. PathOpsAngleTester::SetNext(angle1, angle3);
  424. // These data sets are seeded when the set itself fails, so likely the dataset does not
  425. // match the expected result. The tests above return 1 when first added, but
  426. // return 0 after the bug is fixed.
  427. SkDEBUGCODE(int result =) PathOpsAngleTester::After(angle2, angle1);
  428. SkASSERT(result == 0 || result == 1);
  429. }
  430. }
  431. }
  432. void SkOpSegment::debugAddAngle(double startT, double endT) {
  433. SkOpPtT* startPtT = startT == 0 ? fHead.ptT() : startT == 1 ? fTail.ptT()
  434. : this->addT(startT);
  435. SkOpPtT* endPtT = endT == 0 ? fHead.ptT() : endT == 1 ? fTail.ptT()
  436. : this->addT(endT);
  437. SkOpAngle* angle = this->globalState()->allocator()->make<SkOpAngle>();
  438. SkOpSpanBase* startSpan = &fHead;
  439. while (startSpan->ptT() != startPtT) {
  440. startSpan = startSpan->upCast()->next();
  441. }
  442. SkOpSpanBase* endSpan = &fHead;
  443. while (endSpan->ptT() != endPtT) {
  444. endSpan = endSpan->upCast()->next();
  445. }
  446. angle->set(startSpan, endSpan);
  447. if (startT < endT) {
  448. startSpan->upCast()->setToAngle(angle);
  449. endSpan->setFromAngle(angle);
  450. } else {
  451. endSpan->upCast()->setToAngle(angle);
  452. startSpan->setFromAngle(angle);
  453. }
  454. }
  455. DEF_TEST(PathOpsAngleAllOnOneSide, reporter) {
  456. SkSTArenaAlloc<4096> allocator;
  457. SkOpContourHead contour;
  458. SkOpGlobalState state(&contour, &allocator SkDEBUGPARAMS(false) SkDEBUGPARAMS(nullptr));
  459. contour.init(&state, false, false);
  460. SkPoint conicPts[3] = {{494.37100219726562f, 224.66200256347656f},
  461. {494.37360910682298f, 224.6729026561527f},
  462. {494.37600708007813f, 224.68400573730469f}};
  463. SkPoint linePts[2] = {{494.371002f, 224.662003f}, {494.375000f, 224.675995f}};
  464. for (int i = 10; i >= 0; --i) {
  465. SkPoint modLinePts[2] = { linePts[0], linePts[1] };
  466. modLinePts[1].fX += i * .1f;
  467. contour.addLine(modLinePts);
  468. contour.addQuad(conicPts);
  469. // contour.addConic(conicPts, 0.999935746f, &allocator);
  470. SkOpSegment* first = contour.first();
  471. first->debugAddAngle(0, 1);
  472. SkOpSegment* next = first->next();
  473. next->debugAddAngle(0, 1);
  474. /* int result = */
  475. PathOpsAngleTester::AllOnOneSide(*first->debugLastAngle(), *next->debugLastAngle());
  476. // SkDebugf("i=%d result=%d\n", i , result);
  477. // SkDebugf("");
  478. }
  479. }