PolyUtilsTest.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * Copyright 2018 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/utils/SkPolyUtils.h"
  8. #include "tests/Test.h"
  9. DEF_TEST(PolyUtils, reporter) {
  10. SkTDArray<SkPoint> poly;
  11. // init simple index map
  12. uint16_t indexMap[1024];
  13. for (int i = 0; i < 1024; ++i) {
  14. indexMap[i] = i;
  15. }
  16. SkTDArray<uint16_t> triangleIndices;
  17. // skinny triangle
  18. *poly.push() = SkPoint::Make(-100, 55);
  19. *poly.push() = SkPoint::Make(100, 55);
  20. *poly.push() = SkPoint::Make(102.5f, 54.330127f);
  21. REPORTER_ASSERT(reporter, SkGetPolygonWinding(poly.begin(), poly.count()) < 0);
  22. REPORTER_ASSERT(reporter, SkIsConvexPolygon(poly.begin(), poly.count()));
  23. REPORTER_ASSERT(reporter, SkIsSimplePolygon(poly.begin(), poly.count()));
  24. REPORTER_ASSERT(reporter, SkTriangulateSimplePolygon(poly.begin(), indexMap, poly.count(),
  25. &triangleIndices));
  26. // switch winding
  27. poly[2].set(102.5f, 55.330127f);
  28. REPORTER_ASSERT(reporter, SkGetPolygonWinding(poly.begin(), poly.count()) > 0);
  29. REPORTER_ASSERT(reporter, SkIsConvexPolygon(poly.begin(), poly.count()));
  30. REPORTER_ASSERT(reporter, SkIsSimplePolygon(poly.begin(), poly.count()));
  31. triangleIndices.rewind();
  32. REPORTER_ASSERT(reporter, SkTriangulateSimplePolygon(poly.begin(), indexMap, poly.count(),
  33. &triangleIndices));
  34. // make degenerate
  35. poly[2].set(100 + 2.5f, 55);
  36. REPORTER_ASSERT(reporter, SkGetPolygonWinding(poly.begin(), poly.count()) == 0);
  37. // TODO: should these fail?
  38. REPORTER_ASSERT(reporter, SkIsConvexPolygon(poly.begin(), poly.count()));
  39. REPORTER_ASSERT(reporter, SkIsSimplePolygon(poly.begin(), poly.count()));
  40. triangleIndices.rewind();
  41. REPORTER_ASSERT(reporter, !SkTriangulateSimplePolygon(poly.begin(), indexMap, poly.count(),
  42. &triangleIndices));
  43. // giant triangle
  44. poly[0].set(-1.0e+37f, 1.0e+37f);
  45. poly[1].set(1.0e+37f, 1.0e+37f);
  46. poly[2].set(-1.0e+37f, -1.0e+37f);
  47. REPORTER_ASSERT(reporter, SkGetPolygonWinding(poly.begin(), poly.count()) < 0);
  48. REPORTER_ASSERT(reporter, SkIsConvexPolygon(poly.begin(), poly.count()));
  49. REPORTER_ASSERT(reporter, SkIsSimplePolygon(poly.begin(), poly.count()));
  50. triangleIndices.rewind();
  51. REPORTER_ASSERT(reporter, SkTriangulateSimplePolygon(poly.begin(), indexMap, poly.count(),
  52. &triangleIndices));
  53. // teeny triangle
  54. poly[0].set(-1.0e-38f, 1.0e-38f);
  55. poly[1].set(-1.0e-38f, -1.0e-38f);
  56. poly[2].set(1.0e-38f, 1.0e-38f);
  57. REPORTER_ASSERT(reporter, SkGetPolygonWinding(poly.begin(), poly.count()) == 0);
  58. // TODO: should these fail?
  59. REPORTER_ASSERT(reporter, SkIsConvexPolygon(poly.begin(), poly.count()));
  60. REPORTER_ASSERT(reporter, SkIsSimplePolygon(poly.begin(), poly.count()));
  61. triangleIndices.rewind();
  62. REPORTER_ASSERT(reporter, !SkTriangulateSimplePolygon(poly.begin(), indexMap, poly.count(),
  63. &triangleIndices));
  64. // triangle way off in space (relative to size so we don't completely obliterate values)
  65. poly[0].set(-100 + 1.0e+9f, 55 - 1.0e+9f);
  66. poly[1].set(100 + 1.0e+9f, 55 - 1.0e+9f);
  67. poly[2].set(150 + 1.0e+9f, 100 - 1.0e+9f);
  68. REPORTER_ASSERT(reporter, SkGetPolygonWinding(poly.begin(), poly.count()) > 0);
  69. REPORTER_ASSERT(reporter, SkIsConvexPolygon(poly.begin(), poly.count()));
  70. REPORTER_ASSERT(reporter, SkIsSimplePolygon(poly.begin(), poly.count()));
  71. triangleIndices.rewind();
  72. REPORTER_ASSERT(reporter, SkTriangulateSimplePolygon(poly.begin(), indexMap, poly.count(),
  73. &triangleIndices));
  74. ///////////////////////////////////////////////////////////////////////
  75. // round rect
  76. poly.rewind();
  77. *poly.push() = SkPoint::Make(-100, 55);
  78. *poly.push() = SkPoint::Make(100, 55);
  79. *poly.push() = SkPoint::Make(100 + 2.5f, 50 + 4.330127f);
  80. *poly.push() = SkPoint::Make(100 + 3.535534f, 50 + 3.535534f);
  81. *poly.push() = SkPoint::Make(100 + 4.330127f, 50 + 2.5f);
  82. *poly.push() = SkPoint::Make(105, 50);
  83. *poly.push() = SkPoint::Make(105, -50);
  84. *poly.push() = SkPoint::Make(100 + 4.330127f, -50 - 2.5f);
  85. *poly.push() = SkPoint::Make(100 + 3.535534f, -50 - 3.535534f);
  86. *poly.push() = SkPoint::Make(100 + 2.5f, -50 - 4.330127f);
  87. *poly.push() = SkPoint::Make(100, -55);
  88. *poly.push() = SkPoint::Make(-100, -55);
  89. *poly.push() = SkPoint::Make(-100 - 2.5f, -50 - 4.330127f);
  90. *poly.push() = SkPoint::Make(-100 - 3.535534f, -50 - 3.535534f);
  91. *poly.push() = SkPoint::Make(-100 - 4.330127f, -50 - 2.5f);
  92. *poly.push() = SkPoint::Make(-105, -50);
  93. *poly.push() = SkPoint::Make(-105, 50);
  94. *poly.push() = SkPoint::Make(-100 - 4.330127f, 50 + 2.5f);
  95. *poly.push() = SkPoint::Make(-100 - 3.535534f, 50 + 3.535534f);
  96. *poly.push() = SkPoint::Make(-100 - 2.5f, 50 + 4.330127f);
  97. REPORTER_ASSERT(reporter, SkGetPolygonWinding(poly.begin(), poly.count()) < 0);
  98. REPORTER_ASSERT(reporter, SkIsConvexPolygon(poly.begin(), poly.count()));
  99. REPORTER_ASSERT(reporter, SkIsSimplePolygon(poly.begin(), poly.count()));
  100. triangleIndices.rewind();
  101. REPORTER_ASSERT(reporter, SkTriangulateSimplePolygon(poly.begin(), indexMap, poly.count(),
  102. &triangleIndices));
  103. // translate far enough to obliterate some low bits
  104. for (int i = 0; i < poly.count(); ++i) {
  105. poly[i].offset(1.0e+7f, 1.0e+7f);
  106. }
  107. REPORTER_ASSERT(reporter, SkGetPolygonWinding(poly.begin(), poly.count()) < 0);
  108. // Due to floating point error it's no longer convex
  109. REPORTER_ASSERT(reporter, !SkIsConvexPolygon(poly.begin(), poly.count()));
  110. REPORTER_ASSERT(reporter, SkIsSimplePolygon(poly.begin(), poly.count()));
  111. triangleIndices.rewind();
  112. REPORTER_ASSERT(reporter, SkTriangulateSimplePolygon(poly.begin(), indexMap, poly.count(),
  113. &triangleIndices));
  114. // translate a little farther to create some coincident vertices
  115. for (int i = 0; i < poly.count(); ++i) {
  116. poly[i].offset(4.0e+7f, 4.0e+7f);
  117. }
  118. REPORTER_ASSERT(reporter, SkGetPolygonWinding(poly.begin(), poly.count()) < 0);
  119. REPORTER_ASSERT(reporter, SkIsConvexPolygon(poly.begin(), poly.count()));
  120. REPORTER_ASSERT(reporter, SkIsSimplePolygon(poly.begin(), poly.count()));
  121. // This can't handle coincident vertices
  122. triangleIndices.rewind();
  123. REPORTER_ASSERT(reporter, !SkTriangulateSimplePolygon(poly.begin(), indexMap, poly.count(),
  124. &triangleIndices));
  125. // troublesome case -- clipped roundrect
  126. poly.rewind();
  127. *poly.push() = SkPoint::Make(335.928101f, 428.219055f);
  128. *poly.push() = SkPoint::Make(330.414459f, 423.034912f);
  129. *poly.push() = SkPoint::Make(325.749084f, 417.395508f);
  130. *poly.push() = SkPoint::Make(321.931946f, 411.300842f);
  131. *poly.push() = SkPoint::Make(318.963074f, 404.750977f);
  132. *poly.push() = SkPoint::Make(316.842468f, 397.745850f);
  133. *poly.push() = SkPoint::Make(315.570068f, 390.285522f);
  134. *poly.push() = SkPoint::Make(315.145966f, 382.369965f);
  135. *poly.push() = SkPoint::Make(315.570068f, 374.454346f);
  136. *poly.push() = SkPoint::Make(316.842468f, 366.994019f);
  137. *poly.push() = SkPoint::Make(318.963074f, 359.988892f);
  138. *poly.push() = SkPoint::Make(321.931946f, 353.439056f);
  139. *poly.push() = SkPoint::Make(325.749084f, 347.344421f);
  140. *poly.push() = SkPoint::Make(330.414459f, 341.705017f);
  141. *poly.push() = SkPoint::Make(335.928101f, 336.520813f);
  142. *poly.push() = SkPoint::Make(342.289948f, 331.791901f);
  143. *poly.push() = SkPoint::Make(377.312134f, 331.791901f);
  144. *poly.push() = SkPoint::Make(381.195313f, 332.532593f);
  145. *poly.push() = SkPoint::Make(384.464935f, 334.754700f);
  146. *poly.push() = SkPoint::Make(386.687042f, 338.024292f);
  147. *poly.push() = SkPoint::Make(387.427765f, 341.907532f);
  148. *poly.push() = SkPoint::Make(387.427765f, 422.832367f);
  149. *poly.push() = SkPoint::Make(386.687042f, 426.715576f);
  150. *poly.push() = SkPoint::Make(384.464935f, 429.985168f);
  151. *poly.push() = SkPoint::Make(381.195313f, 432.207275f);
  152. *poly.push() = SkPoint::Make(377.312134f, 432.947998f);
  153. *poly.push() = SkPoint::Make(342.289948f, 432.947998f);
  154. REPORTER_ASSERT(reporter, SkGetPolygonWinding(poly.begin(), poly.count()) > 0);
  155. REPORTER_ASSERT(reporter, SkIsConvexPolygon(poly.begin(), poly.count()));
  156. REPORTER_ASSERT(reporter, SkIsSimplePolygon(poly.begin(), poly.count()));
  157. triangleIndices.rewind();
  158. REPORTER_ASSERT(reporter, SkTriangulateSimplePolygon(poly.begin(), indexMap, poly.count(),
  159. &triangleIndices));
  160. // a star is born
  161. poly.rewind();
  162. *poly.push() = SkPoint::Make(0.0f, -50.0f);
  163. *poly.push() = SkPoint::Make(14.43f, -25.0f);
  164. *poly.push() = SkPoint::Make(43.30f, -25.0f);
  165. *poly.push() = SkPoint::Make(28.86f, 0.0f);
  166. *poly.push() = SkPoint::Make(43.30f, 25.0f);
  167. *poly.push() = SkPoint::Make(14.43f, 25.0f);
  168. *poly.push() = SkPoint::Make(0.0f, 50.0f);
  169. *poly.push() = SkPoint::Make(-14.43f, 25.0f);
  170. *poly.push() = SkPoint::Make(-43.30f, 25.0f);
  171. *poly.push() = SkPoint::Make(-28.86f, 0.0f);
  172. *poly.push() = SkPoint::Make(-43.30f, -25.0f);
  173. *poly.push() = SkPoint::Make(-14.43f, -25.0f);
  174. REPORTER_ASSERT(reporter, SkGetPolygonWinding(poly.begin(), poly.count()) > 0);
  175. REPORTER_ASSERT(reporter, !SkIsConvexPolygon(poly.begin(), poly.count()));
  176. REPORTER_ASSERT(reporter, SkIsSimplePolygon(poly.begin(), poly.count()));
  177. triangleIndices.rewind();
  178. REPORTER_ASSERT(reporter, SkTriangulateSimplePolygon(poly.begin(), indexMap, poly.count(),
  179. &triangleIndices));
  180. // many spiked star
  181. {
  182. const SkScalar c = SkIntToScalar(45);
  183. const SkScalar r1 = SkIntToScalar(20);
  184. const SkScalar r2 = SkIntToScalar(3);
  185. const int n = 500;
  186. poly.rewind();
  187. SkScalar rad = 0;
  188. const SkScalar drad = SK_ScalarPI / n;
  189. for (int i = 0; i < n; i++) {
  190. *poly.push() = SkPoint::Make(c + SkScalarCos(rad) * r1, c + SkScalarSin(rad) * r1);
  191. rad += drad;
  192. *poly.push() = SkPoint::Make(c + SkScalarCos(rad) * r2, c + SkScalarSin(rad) * r2);
  193. rad += drad;
  194. }
  195. REPORTER_ASSERT(reporter, SkGetPolygonWinding(poly.begin(), poly.count()) > 0);
  196. REPORTER_ASSERT(reporter, !SkIsConvexPolygon(poly.begin(), poly.count()));
  197. REPORTER_ASSERT(reporter, SkIsSimplePolygon(poly.begin(), poly.count()));
  198. triangleIndices.rewind();
  199. REPORTER_ASSERT(reporter, SkTriangulateSimplePolygon(poly.begin(), indexMap, poly.count(),
  200. &triangleIndices));
  201. }
  202. // self-intersecting polygon
  203. poly.rewind();
  204. *poly.push() = SkPoint::Make(0.0f, -50.0f);
  205. *poly.push() = SkPoint::Make(14.43f, -25.0f);
  206. *poly.push() = SkPoint::Make(43.30f, -25.0f);
  207. *poly.push() = SkPoint::Make(-28.86f, 0.0f);
  208. *poly.push() = SkPoint::Make(43.30f, 25.0f);
  209. *poly.push() = SkPoint::Make(14.43f, 25.0f);
  210. *poly.push() = SkPoint::Make(0.0f, 50.0f);
  211. *poly.push() = SkPoint::Make(-14.43f, 25.0f);
  212. *poly.push() = SkPoint::Make(-43.30f, 25.0f);
  213. *poly.push() = SkPoint::Make(28.86f, 0.0f);
  214. *poly.push() = SkPoint::Make(-43.30f, -25.0f);
  215. *poly.push() = SkPoint::Make(-14.43f, -25.0f);
  216. REPORTER_ASSERT(reporter, SkGetPolygonWinding(poly.begin(), poly.count()) > 0);
  217. REPORTER_ASSERT(reporter, !SkIsConvexPolygon(poly.begin(), poly.count()));
  218. REPORTER_ASSERT(reporter, !SkIsSimplePolygon(poly.begin(), poly.count()));
  219. triangleIndices.rewind();
  220. // running this just to make sure it doesn't crash
  221. // the fact that it succeeds doesn't mean anything since the input is not simple
  222. REPORTER_ASSERT(reporter, SkTriangulateSimplePolygon(poly.begin(), indexMap, poly.count(),
  223. &triangleIndices));
  224. // self-intersecting polygon with coincident point
  225. poly.rewind();
  226. *poly.push() = SkPoint::Make(0.0f, 0.0f);
  227. *poly.push() = SkPoint::Make(-50, -50);
  228. *poly.push() = SkPoint::Make(50, -50);
  229. *poly.push() = SkPoint::Make(0.00000001f, -0.00000001f);
  230. *poly.push() = SkPoint::Make(-50, 50);
  231. *poly.push() = SkPoint::Make(50, 50);
  232. REPORTER_ASSERT(reporter, SkGetPolygonWinding(poly.begin(), poly.count()) == 0);
  233. REPORTER_ASSERT(reporter, !SkIsConvexPolygon(poly.begin(), poly.count()));
  234. REPORTER_ASSERT(reporter, !SkIsSimplePolygon(poly.begin(), poly.count()));
  235. triangleIndices.rewind();
  236. // running this just to make sure it doesn't crash
  237. REPORTER_ASSERT(reporter, !SkTriangulateSimplePolygon(poly.begin(), indexMap, poly.count(),
  238. &triangleIndices));
  239. }
  240. struct PtSet {
  241. const SkPoint* fPts;
  242. int fN;
  243. };
  244. DEF_TEST(IsPolyConvex_experimental, r) {
  245. #define PTSET(array) {array, SK_ARRAY_COUNT(array)}
  246. const SkPoint g0[] = { {0, 0}, {10, 0}, {10, 10}, {0, 10} };
  247. const PtSet convex[] = { PTSET(g0) };
  248. for (auto& set : convex) {
  249. REPORTER_ASSERT(r, SkIsPolyConvex_experimental(set.fPts, set.fN));
  250. }
  251. const SkPoint b0[] = { {0, 0}, {10, 0}, {0, 10}, {10, 10} };
  252. const SkPoint b1[] = {
  253. {24.8219f, 8.05052f},
  254. {26.0616f, 24.4895f}, {8.49582f, 16.815f},
  255. {27.3047f, 7.75211f},
  256. {21.927f, 27.2051f},
  257. };
  258. const SkPoint b2[] = {
  259. {20, 20}, {20, 50}, {80, 50}, {20, 50}, {20, 80},
  260. };
  261. const PtSet concave[] = { PTSET(b0), PTSET(b1), PTSET(b2) };
  262. for (auto& set : concave) {
  263. if (SkIsPolyConvex_experimental(set.fPts, set.fN)) {
  264. REPORTER_ASSERT(r, !SkIsPolyConvex_experimental(set.fPts, set.fN));
  265. }
  266. }
  267. }