rectClipper.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "util/rectClipper.h"
  23. namespace {
  24. inline void
  25. swap(F32& in_one, F32& in_two)
  26. {
  27. F32 temp = in_one;
  28. in_one = in_two;
  29. in_two = temp;
  30. }
  31. }
  32. bool
  33. RectClipper::clipLine(const Point2I& in_rStart,
  34. const Point2I& in_rEnd,
  35. Point2I& out_rStart,
  36. Point2I& out_rEnd) const
  37. {
  38. // Check for trivial rejection
  39. if ((in_rStart.x < m_clipRect.point.x && in_rEnd.x < m_clipRect.point.x) ||
  40. (in_rStart.x >= m_clipRect.point.x + m_clipRect.extent.x &&
  41. in_rEnd.x >= m_clipRect.point.x + m_clipRect.extent.x))
  42. return false;
  43. if ((in_rStart.y < m_clipRect.point.y && in_rEnd.y < m_clipRect.point.y) ||
  44. (in_rStart.y >= m_clipRect.point.y + m_clipRect.extent.y &&
  45. in_rEnd.y >= m_clipRect.point.y + m_clipRect.extent.y))
  46. return false;
  47. F32 x1 = F32(in_rStart.x);
  48. F32 y1 = F32(in_rStart.y);
  49. F32 x2 = F32(in_rEnd.x);
  50. F32 y2 = F32(in_rEnd.y);
  51. // I'm using essentially what's in the Phoenix libs, Liang-Biarsky based, but
  52. // converted to FP math for greater precision on the back end...
  53. //
  54. bool flipped = false;
  55. if (x1 > x2)
  56. {
  57. swap(x1, x2);
  58. swap(y1, y2);
  59. flipped = !flipped;
  60. }
  61. F32 dx = x2 - x1;
  62. F32 dy = y2 - y1;
  63. // Clip x coord
  64. F32 t;
  65. if (x1 < F32(m_clipRect.point.x))
  66. {
  67. t = (F32(m_clipRect.point.x) - x1) / F32(dx);
  68. x1 = F32(m_clipRect.point.x);
  69. y1 += t * dy;
  70. dx = x2 - x1;
  71. dy = y2 - y1;
  72. }
  73. if (x2 >= F32(m_clipRect.point.x + m_clipRect.extent.x))
  74. {
  75. t = (F32(m_clipRect.point.x + m_clipRect.extent.x - 1) - x1) / F32(dx);
  76. x2 = F32(m_clipRect.point.x + m_clipRect.extent.x - 1);
  77. y2 = y1 + (t * dy);
  78. dx = x2 - x1;
  79. dy = y2 - y1;
  80. }
  81. // Recheck trivial rejection condition...
  82. if((y1 > F32(m_clipRect.point.y + m_clipRect.extent.y - 1) &&
  83. y2 > F32(m_clipRect.point.y + m_clipRect.extent.y - 1)) ||
  84. (y1 < F32(m_clipRect.point.y) && y2 < F32(m_clipRect.point.y)))
  85. return false;
  86. if (y1 > y2)
  87. {
  88. swap(x1, x2);
  89. swap(y1, y2);
  90. flipped = !flipped;
  91. }
  92. if (y1 < F32(m_clipRect.point.y))
  93. {
  94. t = (F32(m_clipRect.point.y) - y1) / F32(dy);
  95. y1 = F32(m_clipRect.point.y);
  96. x1 += t * dx;
  97. dx = x2 - x1;
  98. dy = y2 - y1;
  99. }
  100. if (y2 > F32(m_clipRect.point.y + m_clipRect.extent.y - 1))
  101. {
  102. t = (F32(m_clipRect.point.y + m_clipRect.extent.y - 1) - y1) / F32(dy);
  103. y2 = F32(m_clipRect.point.y + m_clipRect.extent.y - 1);
  104. x2 = x1 + (t * dx);
  105. }
  106. if (flipped == true)
  107. {
  108. out_rEnd.x = S32(x1 + 0.5f);
  109. out_rEnd.y = S32(y1 + 0.5f);
  110. out_rStart.x = S32(x2 + 0.5f);
  111. out_rStart.y = S32(y2 + 0.5f);
  112. }
  113. else
  114. {
  115. out_rStart.x = S32(x1 + 0.5f);
  116. out_rStart.y = S32(y1 + 0.5f);
  117. out_rEnd.x = S32(x2 + 0.5f);
  118. out_rEnd.y = S32(y2 + 0.5f);
  119. }
  120. return true;
  121. }
  122. bool
  123. RectClipper::clipRect(const RectI& in_rRect,
  124. RectI& out_rRect) const
  125. {
  126. AssertFatal(in_rRect.isValidRect(), "Inappropriate min/max coords for rectangle");
  127. if (in_rRect.point.x + in_rRect.extent.x - 1 < m_clipRect.point.x ||
  128. in_rRect.point.x > m_clipRect.point.x + m_clipRect.extent.x - 1)
  129. return false;
  130. if (in_rRect.point.y + in_rRect.extent.y - 1 < m_clipRect.point.y ||
  131. in_rRect.point.y > m_clipRect.point.y + m_clipRect.extent.y - 1)
  132. return false;
  133. if (in_rRect.point.x < m_clipRect.point.x) out_rRect.point.x = m_clipRect.point.x;
  134. else out_rRect.point.x = in_rRect.point.x;
  135. if (in_rRect.point.y < m_clipRect.point.y) out_rRect.point.y = m_clipRect.point.y;
  136. else out_rRect.point.y = in_rRect.point.y;
  137. Point2I bottomR;
  138. bottomR.x = getMin(in_rRect.point.x + in_rRect.extent.x - 1,
  139. m_clipRect.point.x + m_clipRect.extent.x - 1);
  140. bottomR.y = getMin(in_rRect.point.y + in_rRect.extent.y - 1,
  141. m_clipRect.point.y + m_clipRect.extent.y - 1);
  142. out_rRect.extent.x = bottomR.x - out_rRect.point.x + 1;
  143. out_rRect.extent.x = bottomR.y - out_rRect.point.y + 1;
  144. return true;
  145. }