SkOpCubicHull.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/pathops/SkPathOpsCubic.h"
  8. static bool rotate(const SkDCubic& cubic, int zero, int index, SkDCubic& rotPath) {
  9. double dy = cubic[index].fY - cubic[zero].fY;
  10. double dx = cubic[index].fX - cubic[zero].fX;
  11. if (approximately_zero(dy)) {
  12. if (approximately_zero(dx)) {
  13. return false;
  14. }
  15. rotPath = cubic;
  16. if (dy) {
  17. rotPath[index].fY = cubic[zero].fY;
  18. int mask = other_two(index, zero);
  19. int side1 = index ^ mask;
  20. int side2 = zero ^ mask;
  21. if (approximately_equal(cubic[side1].fY, cubic[zero].fY)) {
  22. rotPath[side1].fY = cubic[zero].fY;
  23. }
  24. if (approximately_equal(cubic[side2].fY, cubic[zero].fY)) {
  25. rotPath[side2].fY = cubic[zero].fY;
  26. }
  27. }
  28. return true;
  29. }
  30. for (int index = 0; index < 4; ++index) {
  31. rotPath[index].fX = cubic[index].fX * dx + cubic[index].fY * dy;
  32. rotPath[index].fY = cubic[index].fY * dx - cubic[index].fX * dy;
  33. }
  34. return true;
  35. }
  36. // Returns 0 if negative, 1 if zero, 2 if positive
  37. static int side(double x) {
  38. return (x > 0) + (x >= 0);
  39. }
  40. /* Given a cubic, find the convex hull described by the end and control points.
  41. The hull may have 3 or 4 points. Cubics that degenerate into a point or line
  42. are not considered.
  43. The hull is computed by assuming that three points, if unique and non-linear,
  44. form a triangle. The fourth point may replace one of the first three, may be
  45. discarded if in the triangle or on an edge, or may be inserted between any of
  46. the three to form a convex quadralateral.
  47. The indices returned in order describe the convex hull.
  48. */
  49. int SkDCubic::convexHull(char order[4]) const {
  50. size_t index;
  51. // find top point
  52. size_t yMin = 0;
  53. for (index = 1; index < 4; ++index) {
  54. if (fPts[yMin].fY > fPts[index].fY || (fPts[yMin].fY == fPts[index].fY
  55. && fPts[yMin].fX > fPts[index].fX)) {
  56. yMin = index;
  57. }
  58. }
  59. order[0] = yMin;
  60. int midX = -1;
  61. int backupYMin = -1;
  62. for (int pass = 0; pass < 2; ++pass) {
  63. for (index = 0; index < 4; ++index) {
  64. if (index == yMin) {
  65. continue;
  66. }
  67. // rotate line from (yMin, index) to axis
  68. // see if remaining two points are both above or below
  69. // use this to find mid
  70. int mask = other_two(yMin, index);
  71. int side1 = yMin ^ mask;
  72. int side2 = index ^ mask;
  73. SkDCubic rotPath;
  74. if (!rotate(*this, yMin, index, rotPath)) { // ! if cbc[yMin]==cbc[idx]
  75. order[1] = side1;
  76. order[2] = side2;
  77. return 3;
  78. }
  79. int sides = side(rotPath[side1].fY - rotPath[yMin].fY);
  80. sides ^= side(rotPath[side2].fY - rotPath[yMin].fY);
  81. if (sides == 2) { // '2' means one remaining point <0, one >0
  82. if (midX >= 0) {
  83. // one of the control points is equal to an end point
  84. order[0] = 0;
  85. order[1] = 3;
  86. if (fPts[1] == fPts[0] || fPts[1] == fPts[3]) {
  87. order[2] = 2;
  88. return 3;
  89. }
  90. if (fPts[2] == fPts[0] || fPts[2] == fPts[3]) {
  91. order[2] = 1;
  92. return 3;
  93. }
  94. // one of the control points may be very nearly but not exactly equal --
  95. double dist1_0 = fPts[1].distanceSquared(fPts[0]);
  96. double dist1_3 = fPts[1].distanceSquared(fPts[3]);
  97. double dist2_0 = fPts[2].distanceSquared(fPts[0]);
  98. double dist2_3 = fPts[2].distanceSquared(fPts[3]);
  99. double smallest1distSq = SkTMin(dist1_0, dist1_3);
  100. double smallest2distSq = SkTMin(dist2_0, dist2_3);
  101. if (approximately_zero(SkTMin(smallest1distSq, smallest2distSq))) {
  102. order[2] = smallest1distSq < smallest2distSq ? 2 : 1;
  103. return 3;
  104. }
  105. }
  106. midX = index;
  107. } else if (sides == 0) { // '0' means both to one side or the other
  108. backupYMin = index;
  109. }
  110. }
  111. if (midX >= 0) {
  112. break;
  113. }
  114. if (backupYMin < 0) {
  115. break;
  116. }
  117. yMin = backupYMin;
  118. backupYMin = -1;
  119. }
  120. if (midX < 0) {
  121. midX = yMin ^ 3; // choose any other point
  122. }
  123. int mask = other_two(yMin, midX);
  124. int least = yMin ^ mask;
  125. int most = midX ^ mask;
  126. order[0] = yMin;
  127. order[1] = least;
  128. // see if mid value is on same side of line (least, most) as yMin
  129. SkDCubic midPath;
  130. if (!rotate(*this, least, most, midPath)) { // ! if cbc[least]==cbc[most]
  131. order[2] = midX;
  132. return 3;
  133. }
  134. int midSides = side(midPath[yMin].fY - midPath[least].fY);
  135. midSides ^= side(midPath[midX].fY - midPath[least].fY);
  136. if (midSides != 2) { // if mid point is not between
  137. order[2] = most;
  138. return 3; // result is a triangle
  139. }
  140. order[2] = midX;
  141. order[3] = most;
  142. return 4; // result is a quadralateral
  143. }