OffsetSimplePolyTest.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright 2017 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(OffsetSimplePoly, reporter) {
  10. SkTDArray<SkPoint> rrectPoly;
  11. ///////////////////////////////////////////////////////////////////////
  12. // Try convex tests first
  13. // round rect
  14. *rrectPoly.push() = SkPoint::Make(-100, 55);
  15. *rrectPoly.push() = SkPoint::Make(100, 55);
  16. *rrectPoly.push() = SkPoint::Make(100 + 2.5f, 50 + 4.330127f);
  17. *rrectPoly.push() = SkPoint::Make(100 + 3.535534f, 50 + 3.535534f);
  18. *rrectPoly.push() = SkPoint::Make(100 + 4.330127f, 50 + 2.5f);
  19. *rrectPoly.push() = SkPoint::Make(105, 50);
  20. *rrectPoly.push() = SkPoint::Make(105, -50);
  21. *rrectPoly.push() = SkPoint::Make(100 + 4.330127f, -50 - 2.5f);
  22. *rrectPoly.push() = SkPoint::Make(100 + 3.535534f, -50 - 3.535534f);
  23. *rrectPoly.push() = SkPoint::Make(100 + 2.5f, -50 - 4.330127f);
  24. *rrectPoly.push() = SkPoint::Make(100, -55);
  25. *rrectPoly.push() = SkPoint::Make(-100, -55);
  26. *rrectPoly.push() = SkPoint::Make(-100 - 2.5f, -50 - 4.330127f);
  27. *rrectPoly.push() = SkPoint::Make(-100 - 3.535534f, -50 - 3.535534f);
  28. *rrectPoly.push() = SkPoint::Make(-100 - 4.330127f, -50 - 2.5f);
  29. *rrectPoly.push() = SkPoint::Make(-105, -50);
  30. *rrectPoly.push() = SkPoint::Make(-105, 50);
  31. *rrectPoly.push() = SkPoint::Make(-100 - 4.330127f, 50 + 2.5f);
  32. *rrectPoly.push() = SkPoint::Make(-100 - 3.535534f, 50 + 3.535534f);
  33. *rrectPoly.push() = SkPoint::Make(-100 - 2.5f, 50 + 4.330127f);
  34. SkRect bounds;
  35. bounds.setBoundsCheck(rrectPoly.begin(), rrectPoly.count());
  36. REPORTER_ASSERT(reporter, SkIsConvexPolygon(rrectPoly.begin(), rrectPoly.count()));
  37. // inset a little
  38. SkTDArray<SkPoint> offsetPoly;
  39. bool result = SkOffsetSimplePolygon(rrectPoly.begin(), rrectPoly.count(), bounds, 3,
  40. &offsetPoly);
  41. REPORTER_ASSERT(reporter, result);
  42. REPORTER_ASSERT(reporter, SkIsConvexPolygon(offsetPoly.begin(), offsetPoly.count()));
  43. // inset to rect
  44. result = SkOffsetSimplePolygon(rrectPoly.begin(), rrectPoly.count(), bounds, 10, &offsetPoly);
  45. REPORTER_ASSERT(reporter, result);
  46. REPORTER_ASSERT(reporter, SkIsConvexPolygon(offsetPoly.begin(), offsetPoly.count()));
  47. REPORTER_ASSERT(reporter, offsetPoly.count() == 4);
  48. if (offsetPoly.count() == 4) {
  49. REPORTER_ASSERT(reporter, offsetPoly[0].equals(-95, 45));
  50. REPORTER_ASSERT(reporter, offsetPoly[1].equals(95, 45));
  51. REPORTER_ASSERT(reporter, offsetPoly[2].equals(95, -45));
  52. REPORTER_ASSERT(reporter, offsetPoly[3].equals(-95, -45));
  53. }
  54. // just to full inset
  55. // fails, but outputs a line segment
  56. result = SkOffsetSimplePolygon(rrectPoly.begin(), rrectPoly.count(), bounds, 55, &offsetPoly);
  57. REPORTER_ASSERT(reporter, !result);
  58. REPORTER_ASSERT(reporter, !SkIsConvexPolygon(offsetPoly.begin(), offsetPoly.count()));
  59. REPORTER_ASSERT(reporter, offsetPoly.count() == 2);
  60. if (offsetPoly.count() == 2) {
  61. REPORTER_ASSERT(reporter, offsetPoly[0].equals(-50, 0));
  62. REPORTER_ASSERT(reporter, offsetPoly[1].equals(50, 0));
  63. }
  64. // past full inset
  65. result = SkOffsetSimplePolygon(rrectPoly.begin(), rrectPoly.count(), bounds, 75, &offsetPoly);
  66. REPORTER_ASSERT(reporter, !result);
  67. // troublesome case
  68. SkTDArray<SkPoint> clippedRRectPoly;
  69. *clippedRRectPoly.push() = SkPoint::Make(335.928101f, 428.219055f);
  70. *clippedRRectPoly.push() = SkPoint::Make(330.414459f, 423.034912f);
  71. *clippedRRectPoly.push() = SkPoint::Make(325.749084f, 417.395508f);
  72. *clippedRRectPoly.push() = SkPoint::Make(321.931946f, 411.300842f);
  73. *clippedRRectPoly.push() = SkPoint::Make(318.963074f, 404.750977f);
  74. *clippedRRectPoly.push() = SkPoint::Make(316.842468f, 397.745850f);
  75. *clippedRRectPoly.push() = SkPoint::Make(315.570068f, 390.285522f);
  76. *clippedRRectPoly.push() = SkPoint::Make(315.145966f, 382.369965f);
  77. *clippedRRectPoly.push() = SkPoint::Make(315.570068f, 374.454346f);
  78. *clippedRRectPoly.push() = SkPoint::Make(316.842468f, 366.994019f);
  79. *clippedRRectPoly.push() = SkPoint::Make(318.963074f, 359.988892f);
  80. *clippedRRectPoly.push() = SkPoint::Make(321.931946f, 353.439056f);
  81. *clippedRRectPoly.push() = SkPoint::Make(325.749084f, 347.344421f);
  82. *clippedRRectPoly.push() = SkPoint::Make(330.414459f, 341.705017f);
  83. *clippedRRectPoly.push() = SkPoint::Make(335.928101f, 336.520813f);
  84. *clippedRRectPoly.push() = SkPoint::Make(342.289948f, 331.791901f);
  85. *clippedRRectPoly.push() = SkPoint::Make(377.312134f, 331.791901f);
  86. *clippedRRectPoly.push() = SkPoint::Make(381.195313f, 332.532593f);
  87. *clippedRRectPoly.push() = SkPoint::Make(384.464935f, 334.754700f);
  88. *clippedRRectPoly.push() = SkPoint::Make(386.687042f, 338.024292f);
  89. *clippedRRectPoly.push() = SkPoint::Make(387.427765f, 341.907532f);
  90. *clippedRRectPoly.push() = SkPoint::Make(387.427765f, 422.832367f);
  91. *clippedRRectPoly.push() = SkPoint::Make(386.687042f, 426.715576f);
  92. *clippedRRectPoly.push() = SkPoint::Make(384.464935f, 429.985168f);
  93. *clippedRRectPoly.push() = SkPoint::Make(381.195313f, 432.207275f);
  94. *clippedRRectPoly.push() = SkPoint::Make(377.312134f, 432.947998f);
  95. *clippedRRectPoly.push() = SkPoint::Make(342.289948f, 432.947998f);
  96. bounds.setBoundsCheck(clippedRRectPoly.begin(), clippedRRectPoly.count());
  97. REPORTER_ASSERT(reporter, SkIsConvexPolygon(clippedRRectPoly.begin(),
  98. clippedRRectPoly.count()));
  99. result = SkOffsetSimplePolygon(clippedRRectPoly.begin(), clippedRRectPoly.count(), bounds,
  100. 32.3699417f, &offsetPoly);
  101. REPORTER_ASSERT(reporter, result);
  102. REPORTER_ASSERT(reporter, SkIsConvexPolygon(offsetPoly.begin(), offsetPoly.count()));
  103. ////////////////////////////////////////////////////////////////////////////////
  104. // Concave tests
  105. SkTDArray<SkPoint> starPoly;
  106. *starPoly.push() = SkPoint::Make(0.0f, -50.0f);
  107. *starPoly.push() = SkPoint::Make(14.43f, -25.0f);
  108. *starPoly.push() = SkPoint::Make(43.30f, -25.0f);
  109. *starPoly.push() = SkPoint::Make(28.86f, 0.0f);
  110. *starPoly.push() = SkPoint::Make(43.30f, 25.0f);
  111. *starPoly.push() = SkPoint::Make(14.43f, 25.0f);
  112. *starPoly.push() = SkPoint::Make(0.0f, 50.0f);
  113. *starPoly.push() = SkPoint::Make(-14.43f, 25.0f);
  114. *starPoly.push() = SkPoint::Make(-43.30f, 25.0f);
  115. *starPoly.push() = SkPoint::Make(-28.86f, 0.0f);
  116. *starPoly.push() = SkPoint::Make(-43.30f, -25.0f);
  117. *starPoly.push() = SkPoint::Make(-14.43f, -25.0f);
  118. bounds.setBoundsCheck(starPoly.begin(), starPoly.count());
  119. REPORTER_ASSERT(reporter, SkIsSimplePolygon(starPoly.begin(), starPoly.count()));
  120. // try a variety of distances
  121. result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), bounds, 0.1f,
  122. &offsetPoly);
  123. REPORTER_ASSERT(reporter, result);
  124. REPORTER_ASSERT(reporter, SkIsSimplePolygon(offsetPoly.begin(), offsetPoly.count()));
  125. result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), bounds, 5.665f,
  126. &offsetPoly);
  127. REPORTER_ASSERT(reporter, result);
  128. REPORTER_ASSERT(reporter, SkIsSimplePolygon(offsetPoly.begin(), offsetPoly.count()));
  129. result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), bounds, 28,
  130. &offsetPoly);
  131. REPORTER_ASSERT(reporter, result);
  132. REPORTER_ASSERT(reporter, SkIsSimplePolygon(offsetPoly.begin(), offsetPoly.count()));
  133. // down to a point
  134. result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), bounds, 28.866f,
  135. &offsetPoly);
  136. REPORTER_ASSERT(reporter, !result);
  137. // and past
  138. result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), bounds, 50.5f,
  139. &offsetPoly);
  140. REPORTER_ASSERT(reporter, !result);
  141. // and now out
  142. result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), bounds, -0.1f,
  143. &offsetPoly);
  144. REPORTER_ASSERT(reporter, result);
  145. REPORTER_ASSERT(reporter, SkIsSimplePolygon(offsetPoly.begin(), offsetPoly.count()));
  146. result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), bounds, -5.6665f,
  147. &offsetPoly);
  148. REPORTER_ASSERT(reporter, result);
  149. REPORTER_ASSERT(reporter, SkIsSimplePolygon(offsetPoly.begin(), offsetPoly.count()));
  150. result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), bounds, -50,
  151. &offsetPoly);
  152. REPORTER_ASSERT(reporter, result);
  153. REPORTER_ASSERT(reporter, SkIsSimplePolygon(offsetPoly.begin(), offsetPoly.count()));
  154. result = SkOffsetSimplePolygon(starPoly.begin(), starPoly.count(), bounds, -100,
  155. &offsetPoly);
  156. REPORTER_ASSERT(reporter, result);
  157. REPORTER_ASSERT(reporter, SkIsSimplePolygon(offsetPoly.begin(), offsetPoly.count()));
  158. SkTDArray<SkPoint> intersectingPoly;
  159. *intersectingPoly.push() = SkPoint::Make(0.0f, -50.0f);
  160. *intersectingPoly.push() = SkPoint::Make(14.43f, -25.0f);
  161. *intersectingPoly.push() = SkPoint::Make(43.30f, -25.0f);
  162. *intersectingPoly.push() = SkPoint::Make(-28.86f, 0.0f);
  163. *intersectingPoly.push() = SkPoint::Make(43.30f, 25.0f);
  164. *intersectingPoly.push() = SkPoint::Make(14.43f, 25.0f);
  165. *intersectingPoly.push() = SkPoint::Make(0.0f, 50.0f);
  166. *intersectingPoly.push() = SkPoint::Make(-14.43f, 25.0f);
  167. *intersectingPoly.push() = SkPoint::Make(-43.30f, 25.0f);
  168. *intersectingPoly.push() = SkPoint::Make(28.86f, 0.0f);
  169. *intersectingPoly.push() = SkPoint::Make(-43.30f, -25.0f);
  170. *intersectingPoly.push() = SkPoint::Make(-14.43f, -25.0f);
  171. // SkOffsetSimplePolygon now assumes that the input is simple, so we'll just check for that
  172. result = SkIsSimplePolygon(intersectingPoly.begin(), intersectingPoly.count());
  173. REPORTER_ASSERT(reporter, !result);
  174. }