PathOpsCubicIntersectionTest.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /*
  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 "src/core/SkGeometry.h"
  8. #include "src/pathops/SkIntersections.h"
  9. #include "src/pathops/SkPathOpsRect.h"
  10. #include "src/pathops/SkReduceOrder.h"
  11. #include "tests/PathOpsCubicIntersectionTestData.h"
  12. #include "tests/PathOpsTestCommon.h"
  13. #include "tests/Test.h"
  14. #include <stdlib.h>
  15. const int firstCubicIntersectionTest = 9;
  16. static void standardTestCases(skiatest::Reporter* reporter) {
  17. for (size_t index = firstCubicIntersectionTest; index < tests_count; ++index) {
  18. int iIndex = static_cast<int>(index);
  19. const CubicPts& cubic1 = tests[index][0];
  20. const CubicPts& cubic2 = tests[index][1];
  21. SkDCubic c1, c2;
  22. c1.debugSet(cubic1.fPts);
  23. c2.debugSet(cubic2.fPts);
  24. SkReduceOrder reduce1, reduce2;
  25. int order1 = reduce1.reduce(c1, SkReduceOrder::kNo_Quadratics);
  26. int order2 = reduce2.reduce(c2, SkReduceOrder::kNo_Quadratics);
  27. const bool showSkipped = false;
  28. if (order1 < 4) {
  29. if (showSkipped) {
  30. SkDebugf("%s [%d] cubic1 order=%d\n", __FUNCTION__, iIndex, order1);
  31. }
  32. continue;
  33. }
  34. if (order2 < 4) {
  35. if (showSkipped) {
  36. SkDebugf("%s [%d] cubic2 order=%d\n", __FUNCTION__, iIndex, order2);
  37. }
  38. continue;
  39. }
  40. SkIntersections tIntersections;
  41. tIntersections.intersect(c1, c2);
  42. if (!tIntersections.used()) {
  43. if (showSkipped) {
  44. SkDebugf("%s [%d] no intersection\n", __FUNCTION__, iIndex);
  45. }
  46. continue;
  47. }
  48. if (tIntersections.isCoincident(0)) {
  49. if (showSkipped) {
  50. SkDebugf("%s [%d] coincident\n", __FUNCTION__, iIndex);
  51. }
  52. continue;
  53. }
  54. for (int pt = 0; pt < tIntersections.used(); ++pt) {
  55. double tt1 = tIntersections[0][pt];
  56. SkDPoint xy1 = c1.ptAtT(tt1);
  57. double tt2 = tIntersections[1][pt];
  58. SkDPoint xy2 = c2.ptAtT(tt2);
  59. if (!xy1.approximatelyEqual(xy2)) {
  60. SkDebugf("%s [%d,%d] x!= t1=%g (%g,%g) t2=%g (%g,%g)\n",
  61. __FUNCTION__, (int)index, pt, tt1, xy1.fX, xy1.fY, tt2, xy2.fX, xy2.fY);
  62. }
  63. REPORTER_ASSERT(reporter, xy1.approximatelyEqual(xy2));
  64. }
  65. reporter->bumpTestCount();
  66. }
  67. }
  68. static const CubicPts 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. {{{0, 0}, {0, 1}, {1, 1}, {1, 0}}},
  78. {{{1, 0}, {0, 0}, {0, 1}, {1, 1}}},
  79. {{{0, 1}, {4, 5}, {1, 0}, {5, 3}}},
  80. {{{0, 1}, {3, 5}, {1, 0}, {5, 4}}},
  81. {{{0, 1}, {1, 6}, {1, 0}, {1, 0}}},
  82. {{{0, 1}, {0, 1}, {1, 0}, {6, 1}}},
  83. {{{0, 1}, {3, 4}, {1, 0}, {5, 1}}},
  84. {{{0, 1}, {1, 5}, {1, 0}, {4, 3}}},
  85. {{{0, 1}, {1, 2}, {1, 0}, {6, 1}}},
  86. {{{0, 1}, {1, 6}, {1, 0}, {2, 1}}},
  87. {{{0, 1}, {0, 5}, {1, 0}, {4, 0}}},
  88. {{{0, 1}, {0, 4}, {1, 0}, {5, 0}}},
  89. {{{0, 1}, {3, 4}, {1, 0}, {3, 0}}},
  90. {{{0, 1}, {0, 3}, {1, 0}, {4, 3}}},
  91. {{{0, 0}, {1, 2}, {3, 4}, {4, 4}}},
  92. {{{0, 0}, {1, 2}, {3, 4}, {4, 4}}},
  93. {{{4, 4}, {3, 4}, {1, 2}, {0, 0}}},
  94. {{{0, 1}, {2, 3}, {1, 0}, {1, 0}}},
  95. {{{0, 1}, {0, 1}, {1, 0}, {3, 2}}},
  96. {{{0, 2}, {0, 1}, {1, 0}, {1, 0}}},
  97. {{{0, 1}, {0, 1}, {2, 0}, {1, 0}}},
  98. {{{0, 1}, {0, 2}, {1, 0}, {1, 0}}},
  99. {{{0, 1}, {0, 1}, {1, 0}, {2, 0}}},
  100. {{{0, 1}, {1, 6}, {1, 0}, {2, 0}}},
  101. {{{0, 1}, {0, 2}, {1, 0}, {6, 1}}},
  102. {{{0, 1}, {5, 6}, {1, 0}, {1, 0}}},
  103. {{{0, 1}, {0, 1}, {1, 0}, {6, 5}}},
  104. {{{95.837747722788592, 45.025976907939643}, {16.564570095652982, 0.72959763963222402},
  105. {63.209855865319199, 68.047528419665767}, {57.640240647662544, 59.524565264361243}}},
  106. {{{51.593891741518817, 38.53849970667553}, {62.34752929878772, 74.924924725166022},
  107. {74.810149322641152, 34.17966562983564}, {29.368398119401373, 94.66719277886078}}},
  108. {{{39.765160968417838, 33.060396198677083}, {5.1922921581157908, 66.854301452103215},
  109. {31.619281802149157, 25.269248720849514}, {81.541621071073038, 70.025341524754353}}},
  110. {{{46.078911165743556, 48.259962651999651}, {20.24450549867214, 49.403916182650214},
  111. {0.26325131778756683, 24.46489805563581}, {15.915006546264051, 83.515023059917155}}},
  112. {{{65.454505973241524, 93.881892270353575}, {45.867360264932437, 92.723972719499827},
  113. {2.1464054482739447, 74.636369140183717}, {33.774068594804994, 40.770872887582925}}},
  114. {{{72.963387832494163, 95.659300729473728}, {11.809496633619768, 82.209921247423594},
  115. {13.456139067865974, 57.329313623406605}, {36.060621606214262, 70.867335643091849}}},
  116. {{{32.484981432782945, 75.082940782924624}, {42.467313093350882, 48.131159948246157},
  117. {3.5963115764764657, 43.208665839959245}, {79.442476890721579, 89.709102357602262}}},
  118. {{{18.98573861410177, 93.308887208490106}, {40.405250173250792, 91.039661826118675},
  119. {8.0467721950480584, 42.100282172719147}, {40.883324221187891, 26.030185504830527}}},
  120. {{{7.5374809128872498, 82.441702896003477}, {22.444346930107265, 22.138854312775123},
  121. {66.76091829629658, 50.753805856571446}, {78.193478508942519, 97.7932997968948}}},
  122. {{{97.700573130371311, 53.53260215070685}, {87.72443481149358, 84.575876772671876},
  123. {19.215031396232092, 47.032676472809484}, {11.989686410869325, 10.659507480757082}}},
  124. {{{26.192053931854691, 9.8504326817814416}, {10.174241480498686, 98.476562741434464},
  125. {21.177712558385782, 33.814968789841501}, {75.329030899018534, 55.02231980442177}}},
  126. {{{56.222082700683771, 24.54395039218662}, {95.589995289030483, 81.050822735322086},
  127. {28.180450866082897, 28.837706255185282}, {60.128952916771617, 87.311672180570511}}},
  128. {{{42.449716172390481, 52.379709366885805}, {27.896043159019225, 48.797373636065686},
  129. {92.770268299044233, 89.899302036454571}, {12.102066544863426, 99.43241951960718}}},
  130. {{{45.77532924980639, 45.958701495993274}, {37.458701356062065, 68.393691335056758},
  131. {37.569326692060258, 27.673713456687381}, {60.674866037757539, 62.47349659096146}}},
  132. {{{67.426548091427676, 37.993772624988935}, {23.483695892376684, 90.476863174921306},
  133. {35.597065061143162, 79.872482633158796}, {75.38634169631932, 18.244890038969412}}},
  134. {{{61.336508189019057, 82.693132843213675}, {44.639380902349664, 54.074825790745592},
  135. {16.815615499771951, 20.049704667203923}, {41.866884958868326, 56.735503699973002}}},
  136. {{{18.1312339, 31.6473732}, {95.5711034, 63.5350219}, {92.3283165, 62.0158945},
  137. {18.5656052, 32.1268808}}},
  138. {{{97.402018, 35.7169972}, {33.1127443, 25.8935163}, {1.13970027, 54.9424981},
  139. {56.4860195, 60.529264}}},
  140. };
  141. const int testSetCount = (int) SK_ARRAY_COUNT(testSet);
  142. static const CubicPts newTestSet[] = {
  143. { { { 130.0427549999999997, 11417.41309999999976 },{ 130.2331240000000037, 11418.3192999999992 },{ 131.0370790000000056, 11419 },{ 132, 11419 } } },
  144. { { { 132, 11419 },{ 130.8954319999999996, 11419 },{ 130, 11418.10449999999946 },{ 130, 11417 } } },
  145. {{{1,3}, {-1.0564518,1.79032254}, {1.45265341,0.229448318}, {1.45381773,0.22913377}}},
  146. {{{1.45381773,0.22913377}, {1.45425761,0.229014933}, {1.0967741,0.451612949}, {0,1}}},
  147. {{{1.64551306f, 3.57876182f}, {0.298127174f, 3.70454836f}, {-0.809808373f, 6.39524937f}, {-3.66666651f, 13.333334f}}},
  148. {{{1, 2}, {1, 2}, {-3.66666651f, 13.333334f}, {5, 6}}},
  149. {{{0.0660428554,1.65340209}, {-0.251940489,1.43560803}, {-0.782382965,-0.196299091}, {3.33333325,-0.666666627}}},
  150. {{{1,3}, {-1.22353387,1.09411383}, {0.319867611,0.12996155}, {0.886705518,0.107543148}}},
  151. {{{-0.13654758,2.10514426}, {-0.585797966,1.89349782}, {-0.807703257,-0.192306399}, {6,-1}}},
  152. {{{1,4}, {-2.25000453,1.42241001}, {1.1314013,0.0505309105}, {1.87140274,0.0363764353}}},
  153. {{{1.3127951622009277, 2.0637707710266113}, {1.8210518360137939, 1.9148571491241455}, {1.6106204986572266, -0.68700540065765381}, {8.5, -2.5}}},
  154. {{{3, 4}, {0.33333325386047363, 1.3333332538604736}, {3.6666667461395264, -0.66666674613952637}, {3.6666665077209473, -0.66666656732559204}}},
  155. {{{980.026001,1481.276}, {980.026001,1481.276}, {980.02594,1481.27576}, {980.025879,1481.27527}}},
  156. {{{980.025879,1481.27527}, {980.025452,1481.27222}, {980.023743,1481.26038}, {980.02179,1481.24072}}},
  157. {{{1.80943513,3.07782435}, {1.66686702,2.16806936}, {1.68301272,0}, {3,0}}},
  158. {{{0,1}, {0,3}, {3,2}, {5,2}}},
  159. {{{3.4386673,2.66977954}, {4.06668949,2.17046738}, {4.78887367,1.59629118}, {6,2}}},
  160. {{{1.71985495,3.49467373}, {2.11620402,2.7201426}, {2.91897964,1.15138781}, {6,3}}},
  161. {{{0,1}, {0.392703831,1.78540766}, {0.219947904,2.05676103}, {0.218561709,2.05630541}}},
  162. {{{0.218561709,2.05630541}, {0.216418028,2.05560064}, {0.624105453,1.40486407}, {4.16666651,1.00000012}}},
  163. {{{0, 1}, {3, 5}, {2, 1}, {3, 1}}},
  164. {{{1.01366711f, 2.21379328f}, {1.09074128f, 2.23241305f}, {1.60246587f, 0.451849401f}, {5, 3}}},
  165. {{{0, 1}, {0.541499972f, 3.16599989f}, {1.08299994f, 2.69299984f}, {2.10083938f, 1.80391729f}}},
  166. {{{0.806384504f, 2.85426903f}, {1.52740121f, 1.99355423f}, {2.81689167f, 0.454222918f}, {5, 1}}},
  167. {{{0, 1}, {1.90192389f, 2.90192389f}, {2.59807634f, 2.79422879f}, {3.1076951f, 2.71539044f}}},
  168. {{{2, 3}, {2.36602545f, 3.36602545f}, {2.330127f, 3.06217766f}, {2.28460979f, 2.67691422f}}},
  169. {{{0, 1}, {1.90192389f, 2.90192389f}, {2.59807634f, 2.79422879f}, {3.1076951f, 2.71539044f}}},
  170. {{{2.28460979f, 2.67691422f}, {2.20577145f, 2.00961876f}, {2.09807634f, 1.09807622f}, {4, 3}}},
  171. {{{0, 1}, {0.8211091160774231, 2.0948121547698975}, {0.91805583238601685, 2.515404224395752}, {0.91621249914169312, 2.5146586894989014}}},
  172. {{{0.91621249914169312, 2.5146586894989014}, {0.91132104396820068, 2.5126807689666748}, {0.21079301834106445, -0.45617169141769409}, {10.5, -1.6666665077209473}}},
  173. {{{42.6237564,68.9841232}, {32.449646,81.963089}, {14.7713947,103.565269}, {12.6310005,105.247002}}},
  174. {{{37.2640038,95.3540039}, {37.2640038,95.3540039}, {11.3710003,83.7339935}, {-25.0779991,124.912003}}},
  175. {{{0,1}, {4,5}, {6,0}, {1,0}}},
  176. {{{0,6}, {0,1}, {1,0}, {5,4}}},
  177. {{{0,1}, {4,6}, {5,1}, {6,2}}},
  178. {{{1,5}, {2,6}, {1,0}, {6,4}}},
  179. {{{322, 896.04803466796875}, {314.09201049804687, 833.4376220703125}, {260.24713134765625, 785}, {195, 785}}},
  180. {{{195, 785}, {265.14016723632812, 785}, {322, 842.30755615234375}, {322, 913}}},
  181. {{{1, 4}, {4, 5}, {3, 2}, {6, 3}}},
  182. {{{2, 3}, {3, 6}, {4, 1}, {5, 4}}},
  183. {{{67, 913}, {67, 917.388916015625}, {67.224380493164063, 921.72576904296875}, {67.662384033203125, 926}}},
  184. {{{194, 1041}, {123.85984039306641, 1041}, {67, 983.69244384765625}, {67, 913}}},
  185. {{{1,4}, {1,5}, {6,0}, {5,1}}},
  186. {{{0,6}, {1,5}, {4,1}, {5,1}}},
  187. {{{0,1}, {4,5}, {6,0}, {1,0}}},
  188. {{{0,6}, {0,1}, {1,0}, {5,4}}},
  189. {{{0,1}, {4,6}, {2,0}, {2,0}}},
  190. {{{0,2}, {0,2}, {1,0}, {6,4}}},
  191. {{{980.9000244140625, 1474.3280029296875}, {980.9000244140625, 1474.3280029296875}, {978.89300537109375, 1471.95703125}, {981.791015625, 1469.487060546875}}},
  192. {{{981.791015625, 1469.487060546875}, {981.791015625, 1469.4859619140625}, {983.3580322265625, 1472.72900390625}, {980.9000244140625, 1474.3280029296875}}},
  193. {{{275,532}, {277.209137,532}, {279,530.209106}, {279,528}}},
  194. {{{278,529}, {278,530.65686}, {276.65686,532}, {275,532}}},
  195. #if 0 // FIXME: asserts coincidence, not working yet
  196. {{{195, 785}, {124.30755615234375, 785}, {67, 841.85986328125}, {67, 912}}},
  197. {{{67, 913}, {67, 842.30755615234375}, {123.85984039306641, 785}, {194, 785}}},
  198. #endif
  199. {{{149,710.001465}, {149.000809,712.209961}, {150.791367,714}, {153,714}}},
  200. {{{154,715}, {151.238571,715}, {149,712.761414}, {149,710}}},
  201. {{{1,2}, {1,2}, {2,0}, {6,0}}},
  202. {{{0,2}, {0,6}, {2,1}, {2,1}}},
  203. {{{0,1}, {2,3}, {5,1}, {4,3}}},
  204. {{{1,5}, {3,4}, {1,0}, {3,2}}},
  205. {{{399,657}, {399,661.970581}, {403.029449,666}, {408,666}}},
  206. {{{406,666}, {402.686279,666}, {400,663.313721}, {400,660}}},
  207. {{{0,5}, {3,5}, {3,0}, {3,2}}},
  208. {{{0,3}, {2,3}, {5,0}, {5,3}}},
  209. {{{132, 11419}, {130.89543151855469, 11419}, {130, 11418.1044921875}, {130, 11417}}},
  210. {{{3, 4}, {1, 5}, {4, 3}, {6, 4}}},
  211. {{{3, 4}, {4, 6}, {4, 3}, {5, 1}}},
  212. {{{130.04275512695312, 11417.413085937500 },
  213. {130.23312377929687, 11418.319335937500 },
  214. {131.03707885742187, 11419.000000000000 },
  215. {132.00000000000000, 11419.000000000000 }}},
  216. {{{132.00000000000000, 11419.000000000000 },
  217. {130.89543151855469, 11419.000000000000 },
  218. {130.00000000000000, 11418.104492187500 },
  219. {130.00000000000000, 11417.000000000000 }}},
  220. {{{1.0516976506771041, 2.9684399028541346 },
  221. {1.0604363140895228, 2.9633503074444141 },
  222. {1.0692548215065762, 2.9580354426587459 },
  223. {1.0781560339512140, 2.9525043684031349 }}},
  224. {{{1.0523038101345104, 2.9523755204833737 },
  225. {1.0607035288264237, 2.9580853881628375 },
  226. {1.0690530472271964, 2.9633896794787749 },
  227. {1.0773566568712512, 2.9682969775000219 }}},
  228. {{{1.0386522625066592, 2.9759024812329078 },
  229. {1.0559713690392631, 2.9661782500838885 },
  230. {1.0736041309019990, 2.9555348259177858 },
  231. {1.0915734362784633, 2.9440446879826569 }}},
  232. {{{1.0396670794879301, 2.9435062123457261 },
  233. {1.0565690546812769, 2.9557413250983462 },
  234. {1.0732616463413533, 2.9663369676594282 },
  235. {1.0897791867435489, 2.9753618045797472 }}},
  236. {{{0.8685656183311091, 3.0409266475785208 },
  237. {0.99189542936395292, 3.0212163698184424 },
  238. {1.1302108367493320, 2.9265646471747306 },
  239. {1.2952305904872474, 2.7940808546473788 }}},
  240. {{{0.85437872843682727, 2.7536036928549055 },
  241. {1.0045584590592620, 2.9493041024831705 },
  242. {1.1336998329885613, 3.0248027987251747 },
  243. {1.2593809752247314, 3.0152560315809107 }}},
  244. {{{0, 1}, {1, 6}, {1, 0}, {6, 2}}},
  245. {{{0, 1}, {2, 6}, {1, 0}, {6, 1}}},
  246. {{{134,11414}, {131.990234375,11414}, {130.32666015625,11415.482421875}, {130.04275512695312,11417.4130859375}}},
  247. {{{132,11419}, {130.89543151855469,11419}, {130,11418.1044921875}, {130,11417}}},
  248. {{{132,11419}, {130.89543151855469,11419}, {130,11418.1044921875}, {130,11417}}},
  249. {{{130.04275512695312,11417.4130859375}, {130.23312377929687,11418.3193359375}, {131.03707885742187,11419}, {132,11419}}},
  250. {{{0, 1}, {2, 3}, {5, 1}, {4, 3}}},
  251. {{{1, 5}, {3, 4}, {1, 0}, {3, 2}}},
  252. {{{3, 5}, {1, 6}, {5, 0}, {3, 1}}},
  253. {{{0, 5}, {1, 3}, {5, 3}, {6, 1}}},
  254. {{{0, 1}, {1, 5}, {1, 0}, {1, 0}}},
  255. {{{0, 1}, {0, 1}, {1, 0}, {5, 1}}},
  256. {{{1, 3}, {5, 6}, {5, 3}, {5, 4}}},
  257. {{{3, 5}, {4, 5}, {3, 1}, {6, 5}}},
  258. {{{0, 5}, {0, 5}, {5, 4}, {6, 4}}},
  259. {{{4, 5}, {4, 6}, {5, 0}, {5, 0}}},
  260. {{{0, 4}, {1, 3}, {5, 4}, {4, 2}}},
  261. {{{4, 5}, {2, 4}, {4, 0}, {3, 1}}},
  262. {{{0, 2}, {1, 5}, {3, 2}, {4, 1}}},
  263. {{{2, 3}, {1, 4}, {2, 0}, {5, 1}}},
  264. {{{0, 2}, {2, 3}, {5, 1}, {3, 2}}},
  265. {{{1, 5}, {2, 3}, {2, 0}, {3, 2}}},
  266. {{{2, 6}, {4, 5}, {1, 0}, {6, 1}}},
  267. {{{0, 1}, {1, 6}, {6, 2}, {5, 4}}},
  268. {{{0, 1}, {1, 2}, {6, 5}, {5, 4}}},
  269. {{{5, 6}, {4, 5}, {1, 0}, {2, 1}}},
  270. {{{2.5119999999999996, 1.5710000000000002}, {2.6399999999999983, 1.6599999999999997},
  271. {2.8000000000000007, 1.8000000000000003}, {3, 2}}},
  272. {{{2.4181876227114887, 1.9849772580462195}, {2.8269904869227211, 2.009330650246834},
  273. {3.2004679292461624, 1.9942047174679169}, {3.4986199496818058, 2.0035994597094731}}},
  274. {{{2, 3}, {1, 4}, {1, 0}, {6, 0}}},
  275. {{{0, 1}, {0, 6}, {3, 2}, {4, 1}}},
  276. {{{0, 2}, {1, 5}, {1, 0}, {6, 1}}},
  277. {{{0, 1}, {1, 6}, {2, 0}, {5, 1}}},
  278. {{{0, 1}, {1, 5}, {2, 1}, {4, 0}}},
  279. {{{1, 2}, {0, 4}, {1, 0}, {5, 1}}},
  280. {{{0, 1}, {3, 5}, {2, 1}, {3, 1}}},
  281. {{{1, 2}, {1, 3}, {1, 0}, {5, 3}}},
  282. {{{0, 1}, {2, 5}, {6, 0}, {5, 3}}},
  283. {{{0, 6}, {3, 5}, {1, 0}, {5, 2}}},
  284. {{{0, 1}, {3, 6}, {1, 0}, {5, 2}}},
  285. {{{0, 1}, {2, 5}, {1, 0}, {6, 3}}},
  286. {{{1, 2}, {5, 6}, {1, 0}, {1, 0}}},
  287. {{{0, 1}, {0, 1}, {2, 1}, {6, 5}}},
  288. {{{0, 6}, {1, 2}, {1, 0}, {1, 0}}},
  289. {{{0, 1}, {0, 1}, {6, 0}, {2, 1}}},
  290. {{{0, 2}, {0, 1}, {3, 0}, {1, 0}}},
  291. {{{0, 3}, {0, 1}, {2, 0}, {1, 0}}},
  292. };
  293. const int newTestSetCount = (int) SK_ARRAY_COUNT(newTestSet);
  294. static void oneOff(skiatest::Reporter* reporter, const CubicPts& cubic1, const CubicPts& cubic2,
  295. bool coin) {
  296. SkDCubic c1, c2;
  297. c1.debugSet(cubic1.fPts);
  298. c2.debugSet(cubic2.fPts);
  299. SkASSERT(ValidCubic(c1));
  300. SkASSERT(ValidCubic(c2));
  301. #if ONE_OFF_DEBUG
  302. SkDebugf("computed quadratics given\n");
  303. SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
  304. 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);
  306. SkDebugf(" {{%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}, {%1.9g,%1.9g}},\n",
  307. 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
  310. SkIntersections intersections;
  311. intersections.intersect(c1, c2);
  312. #if DEBUG_T_SECT_DUMP == 3
  313. SkDebugf("</div>\n\n");
  314. SkDebugf("<script type=\"text/javascript\">\n\n");
  315. SkDebugf("var testDivs = [\n");
  316. for (int index = 1; index <= gDumpTSectNum; ++index) {
  317. SkDebugf("sect%d,\n", index);
  318. }
  319. #endif
  320. if (coin && intersections.used() < 2) {
  321. SkDebugf("");
  322. }
  323. REPORTER_ASSERT(reporter, !coin || intersections.used() >= 2);
  324. double tt1, tt2;
  325. SkDPoint xy1, xy2;
  326. for (int pt3 = 0; pt3 < intersections.used(); ++pt3) {
  327. tt1 = intersections[0][pt3];
  328. xy1 = c1.ptAtT(tt1);
  329. tt2 = intersections[1][pt3];
  330. xy2 = c2.ptAtT(tt2);
  331. const SkDPoint& iPt = intersections.pt(pt3);
  332. #if ONE_OFF_DEBUG
  333. SkDebugf("%s t1=%1.9g (%1.9g, %1.9g) (%1.9g, %1.9g) (%1.9g, %1.9g) t2=%1.9g\n",
  334. __FUNCTION__, tt1, xy1.fX, xy1.fY, iPt.fX,
  335. iPt.fY, xy2.fX, xy2.fY, tt2);
  336. #endif
  337. REPORTER_ASSERT(reporter, xy1.approximatelyEqual(iPt));
  338. REPORTER_ASSERT(reporter, xy2.approximatelyEqual(iPt));
  339. REPORTER_ASSERT(reporter, xy1.approximatelyEqual(xy2));
  340. }
  341. reporter->bumpTestCount();
  342. }
  343. static void oneOff(skiatest::Reporter* reporter, int outer, int inner) {
  344. const CubicPts& cubic1 = testSet[outer];
  345. const CubicPts& cubic2 = testSet[inner];
  346. oneOff(reporter, cubic1, cubic2, false);
  347. }
  348. static void newOneOff(skiatest::Reporter* reporter, int outer, int inner) {
  349. const CubicPts& cubic1 = newTestSet[outer];
  350. const CubicPts& cubic2 = newTestSet[inner];
  351. oneOff(reporter, cubic1, cubic2, false);
  352. }
  353. static void testsOneOff(skiatest::Reporter* reporter, int index) {
  354. const CubicPts& cubic1 = tests[index][0];
  355. const CubicPts& cubic2 = tests[index][1];
  356. oneOff(reporter, cubic1, cubic2, false);
  357. }
  358. static void oneOffTests(skiatest::Reporter* reporter) {
  359. for (int outer = 0; outer < testSetCount - 1; ++outer) {
  360. for (int inner = outer + 1; inner < testSetCount; ++inner) {
  361. oneOff(reporter, outer, inner);
  362. }
  363. }
  364. for (int outer = 0; outer < newTestSetCount - 1; ++outer) {
  365. for (int inner = outer + 1; inner < newTestSetCount; ++inner) {
  366. newOneOff(reporter, outer, inner);
  367. }
  368. }
  369. }
  370. #define DEBUG_CRASH 0
  371. static void CubicIntersection_RandTest(skiatest::Reporter* reporter) {
  372. srand(0);
  373. const int tests = 10000000;
  374. #if !defined(SK_BUILD_FOR_WIN) && !defined(SK_BUILD_FOR_ANDROID)
  375. unsigned seed = 0;
  376. #endif
  377. for (int test = 0; test < tests; ++test) {
  378. CubicPts cubic1, cubic2;
  379. for (int i = 0; i < 4; ++i) {
  380. cubic1.fPts[i].fX = static_cast<double>(SK_RAND(seed)) / RAND_MAX * 100;
  381. cubic1.fPts[i].fY = static_cast<double>(SK_RAND(seed)) / RAND_MAX * 100;
  382. cubic2.fPts[i].fX = static_cast<double>(SK_RAND(seed)) / RAND_MAX * 100;
  383. cubic2.fPts[i].fY = static_cast<double>(SK_RAND(seed)) / RAND_MAX * 100;
  384. }
  385. #if DEBUG_CRASH
  386. char str[1024];
  387. snprintf(str, sizeof(str),
  388. "{{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}}},\n"
  389. "{{{%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}, {%1.9g, %1.9g}}},\n",
  390. cubic1[0].fX, cubic1[0].fY, cubic1[1].fX, cubic1[1].fY, cubic1[2].fX, cubic1[2].fY,
  391. cubic1[3].fX, cubic1[3].fY,
  392. cubic2[0].fX, cubic2[0].fY, cubic2[1].fX, cubic2[1].fY, cubic2[2].fX, cubic2[2].fY,
  393. cubic2[3].fX, cubic2[3].fY);
  394. #endif
  395. SkDRect rect1, rect2;
  396. SkDCubic c1, c2;
  397. c1.debugSet(cubic1.fPts);
  398. c2.debugSet(cubic2.fPts);
  399. rect1.setBounds(c1);
  400. rect2.setBounds(c2);
  401. bool boundsIntersect = rect1.fLeft <= rect2.fRight && rect2.fLeft <= rect2.fRight
  402. && rect1.fTop <= rect2.fBottom && rect2.fTop <= rect1.fBottom;
  403. if (test == -1) {
  404. SkDebugf("ready...\n");
  405. }
  406. SkIntersections intersections2;
  407. int newIntersects = intersections2.intersect(c1, c2);
  408. if (!boundsIntersect && newIntersects) {
  409. #if DEBUG_CRASH
  410. SkDebugf("%s %d unexpected intersection boundsIntersect=%d "
  411. " newIntersects=%d\n%s %s\n", __FUNCTION__, test, boundsIntersect,
  412. newIntersects, __FUNCTION__, str);
  413. #endif
  414. REPORTER_ASSERT(reporter, 0);
  415. }
  416. for (int pt = 0; pt < intersections2.used(); ++pt) {
  417. double tt1 = intersections2[0][pt];
  418. SkDPoint xy1 = c1.ptAtT(tt1);
  419. double tt2 = intersections2[1][pt];
  420. SkDPoint xy2 = c2.ptAtT(tt2);
  421. REPORTER_ASSERT(reporter, xy1.approximatelyEqual(xy2));
  422. }
  423. reporter->bumpTestCount();
  424. }
  425. }
  426. static void intersectionFinder(int index0, int index1, double t1Seed, double t2Seed,
  427. double t1Step, double t2Step) {
  428. const CubicPts& cubic1 = newTestSet[index0];
  429. const CubicPts& cubic2 = newTestSet[index1];
  430. SkDPoint t1[3], t2[3];
  431. bool toggle = true;
  432. SkDCubic c1, c2;
  433. c1.debugSet(cubic1.fPts);
  434. c2.debugSet(cubic2.fPts);
  435. do {
  436. t1[0] = c1.ptAtT(t1Seed - t1Step);
  437. t1[1] = c1.ptAtT(t1Seed);
  438. t1[2] = c1.ptAtT(t1Seed + t1Step);
  439. t2[0] = c2.ptAtT(t2Seed - t2Step);
  440. t2[1] = c2.ptAtT(t2Seed);
  441. t2[2] = c2.ptAtT(t2Seed + t2Step);
  442. double dist[3][3];
  443. dist[1][1] = t1[1].distance(t2[1]);
  444. int best_i = 1, best_j = 1;
  445. for (int i = 0; i < 3; ++i) {
  446. for (int j = 0; j < 3; ++j) {
  447. if (i == 1 && j == 1) {
  448. continue;
  449. }
  450. dist[i][j] = t1[i].distance(t2[j]);
  451. if (dist[best_i][best_j] > dist[i][j]) {
  452. best_i = i;
  453. best_j = j;
  454. }
  455. }
  456. }
  457. if (best_i == 0) {
  458. t1Seed -= t1Step;
  459. } else if (best_i == 2) {
  460. t1Seed += t1Step;
  461. }
  462. if (best_j == 0) {
  463. t2Seed -= t2Step;
  464. } else if (best_j == 2) {
  465. t2Seed += t2Step;
  466. }
  467. if (best_i == 1 && best_j == 1) {
  468. if ((toggle ^= true)) {
  469. t1Step /= 2;
  470. } else {
  471. t2Step /= 2;
  472. }
  473. }
  474. } while (!t1[1].approximatelyEqual(t2[1]));
  475. t1Step = t2Step = 0.1;
  476. double t10 = t1Seed - t1Step * 2;
  477. double t12 = t1Seed + t1Step * 2;
  478. double t20 = t2Seed - t2Step * 2;
  479. double t22 = t2Seed + t2Step * 2;
  480. SkDPoint test;
  481. while (!approximately_zero(t1Step)) {
  482. test = c1.ptAtT(t10);
  483. t10 += t1[1].approximatelyEqual(test) ? -t1Step : t1Step;
  484. t1Step /= 2;
  485. }
  486. t1Step = 0.1;
  487. while (!approximately_zero(t1Step)) {
  488. test = c1.ptAtT(t12);
  489. t12 -= t1[1].approximatelyEqual(test) ? -t1Step : t1Step;
  490. t1Step /= 2;
  491. }
  492. while (!approximately_zero(t2Step)) {
  493. test = c2.ptAtT(t20);
  494. t20 += t2[1].approximatelyEqual(test) ? -t2Step : t2Step;
  495. t2Step /= 2;
  496. }
  497. t2Step = 0.1;
  498. while (!approximately_zero(t2Step)) {
  499. test = c2.ptAtT(t22);
  500. t22 -= t2[1].approximatelyEqual(test) ? -t2Step : t2Step;
  501. t2Step /= 2;
  502. }
  503. #if ONE_OFF_DEBUG
  504. SkDebugf("%s t1=(%1.9g<%1.9g<%1.9g) t2=(%1.9g<%1.9g<%1.9g)\n", __FUNCTION__,
  505. t10, t1Seed, t12, t20, t2Seed, t22);
  506. SkDPoint p10 = c1.ptAtT(t10);
  507. SkDPoint p1Seed = c1.ptAtT(t1Seed);
  508. SkDPoint p12 = c1.ptAtT(t12);
  509. SkDebugf("%s p1=(%1.9g,%1.9g)<(%1.9g,%1.9g)<(%1.9g,%1.9g)\n", __FUNCTION__,
  510. p10.fX, p10.fY, p1Seed.fX, p1Seed.fY, p12.fX, p12.fY);
  511. SkDPoint p20 = c2.ptAtT(t20);
  512. SkDPoint p2Seed = c2.ptAtT(t2Seed);
  513. SkDPoint p22 = c2.ptAtT(t22);
  514. SkDebugf("%s p2=(%1.9g,%1.9g)<(%1.9g,%1.9g)<(%1.9g,%1.9g)\n", __FUNCTION__,
  515. p20.fX, p20.fY, p2Seed.fX, p2Seed.fY, p22.fX, p22.fY);
  516. #endif
  517. }
  518. static void CubicIntersection_IntersectionFinder() {
  519. // double t1Seed = 0.87;
  520. // double t2Seed = 0.87;
  521. double t1Step = 0.000001;
  522. double t2Step = 0.000001;
  523. intersectionFinder(0, 1, 0.855895664, 0.864850875, t1Step, t2Step);
  524. intersectionFinder(0, 1, 0.865207906, 0.865207887, t1Step, t2Step);
  525. intersectionFinder(0, 1, 0.865213351, 0.865208087, t1Step, t2Step);
  526. }
  527. static const CubicPts selfSet[] = {
  528. {{{2, 3}, {0, 4}, {3, 2}, {5, 3}}},
  529. {{{3, 6}, {2, 3}, {4, 0}, {3, 2}}},
  530. {{{0, 2}, {2, 3}, {5, 1}, {3, 2}}},
  531. {{{0, 2}, {3, 5}, {5, 0}, {4, 2}}},
  532. {{{3.34, 8.98}, {1.95, 10.27}, {3.76, 7.65}, {4.96, 10.64}}},
  533. {{{3.13, 2.74}, {1.08, 4.62}, {3.71, 0.94}, {2.01, 3.81}}},
  534. {{{6.71, 3.14}, {7.99, 2.75}, {8.27, 1.96}, {6.35, 3.57}}},
  535. {{{12.81, 7.27}, {7.22, 6.98}, {12.49, 8.97}, {11.42, 6.18}}},
  536. };
  537. int selfSetCount = (int) SK_ARRAY_COUNT(selfSet);
  538. static void selfOneOff(skiatest::Reporter* reporter, int index) {
  539. const CubicPts& cubic = selfSet[index];
  540. SkPoint c[4];
  541. for (int i = 0; i < 4; ++i) {
  542. c[i] = cubic.fPts[i].asSkPoint();
  543. }
  544. SkScalar loopT[3];
  545. SkCubicType cubicType = SkClassifyCubic(c);
  546. int breaks = SkDCubic::ComplexBreak(c, loopT);
  547. SkASSERT(breaks < 2);
  548. if (breaks && cubicType == SkCubicType::kLoop) {
  549. SkIntersections i;
  550. SkPoint twoCubics[7];
  551. SkChopCubicAt(c, twoCubics, loopT[0]);
  552. SkDCubic chopped[2];
  553. chopped[0].set(&twoCubics[0]);
  554. chopped[1].set(&twoCubics[3]);
  555. int result = i.intersect(chopped[0], chopped[1]);
  556. REPORTER_ASSERT(reporter, result == 2);
  557. REPORTER_ASSERT(reporter, i.used() == 2);
  558. for (int index = 0; index < result; ++index) {
  559. SkDPoint pt1 = chopped[0].ptAtT(i[0][index]);
  560. SkDPoint pt2 = chopped[1].ptAtT(i[1][index]);
  561. REPORTER_ASSERT(reporter, pt1.approximatelyEqual(pt2));
  562. reporter->bumpTestCount();
  563. }
  564. }
  565. }
  566. static void cubicIntersectionSelfTest(skiatest::Reporter* reporter) {
  567. int firstFail = 0;
  568. for (int index = firstFail; index < selfSetCount; ++index) {
  569. selfOneOff(reporter, index);
  570. }
  571. }
  572. static const CubicPts coinSet[] = {
  573. {{{72.350448608398438, 27.966041564941406}, {72.58441162109375, 27.861515045166016},
  574. {72.818222045898437, 27.756658554077148}, {73.394996643066406, 27.49799919128418}}},
  575. {{{73.394996643066406, 27.49799919128418}, {72.818222045898437, 27.756658554077148},
  576. {72.58441162109375, 27.861515045166016}, {72.350448608398438, 27.966041564941406}}},
  577. {{{297.04998779296875, 43.928997039794922}, {297.04998779296875, 43.928997039794922},
  578. {300.69699096679688, 45.391998291015625}, {306.92498779296875, 43.08599853515625}}},
  579. {{{297.04998779296875, 43.928997039794922}, {297.04998779296875, 43.928997039794922},
  580. {300.69699096679688, 45.391998291015625}, {306.92498779296875, 43.08599853515625}}},
  581. {{{2, 3}, {0, 4}, {3, 2}, {5, 3}}},
  582. {{{2, 3}, {0, 4}, {3, 2}, {5, 3}}},
  583. {{{317, 711}, {322.52285766601562, 711}, {327, 715.4771728515625}, {327, 721}}},
  584. {{{324.07107543945312, 713.928955078125}, {324.4051513671875, 714.26300048828125},
  585. {324.71566772460937, 714.62060546875}, {325, 714.9990234375}}},
  586. };
  587. static int coinSetCount = (int) SK_ARRAY_COUNT(coinSet);
  588. static void coinOneOff(skiatest::Reporter* reporter, int index) {
  589. const CubicPts& cubic1 = coinSet[index];
  590. const CubicPts& cubic2 = coinSet[index + 1];
  591. oneOff(reporter, cubic1, cubic2, true);
  592. }
  593. static void cubicIntersectionCoinTest(skiatest::Reporter* reporter) {
  594. int firstFail = 0;
  595. for (int index = firstFail; index < coinSetCount; index += 2) {
  596. coinOneOff(reporter, index);
  597. }
  598. }
  599. DEF_TEST(PathOpsCubicCoinOneOff, reporter) {
  600. coinOneOff(reporter, 0);
  601. }
  602. DEF_TEST(PathOpsCubicIntersectionOneOff, reporter) {
  603. newOneOff(reporter, 0, 1);
  604. }
  605. DEF_TEST(PathOpsCubicIntersectionTestsOneOff, reporter) {
  606. testsOneOff(reporter, 10);
  607. }
  608. DEF_TEST(PathOpsCubicSelfOneOff, reporter) {
  609. selfOneOff(reporter, 0);
  610. }
  611. DEF_TEST(PathOpsCubicIntersection, reporter) {
  612. oneOffTests(reporter);
  613. cubicIntersectionSelfTest(reporter);
  614. cubicIntersectionCoinTest(reporter);
  615. standardTestCases(reporter);
  616. if (false) CubicIntersection_IntersectionFinder();
  617. if (false) CubicIntersection_RandTest(reporter);
  618. }