1
0

CCAABB.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /****************************************************************************
  2. Copyright (c) 2014-2017 Chukong Technologies Inc.
  3. http://www.cocos2d-x.org
  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 deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "3d/CCAABB.h"
  21. NS_CC_BEGIN
  22. AABB::AABB()
  23. {
  24. reset();
  25. }
  26. AABB::AABB(const Vec3& min, const Vec3& max)
  27. {
  28. set(min, max);
  29. }
  30. AABB::AABB(const AABB& box)
  31. {
  32. set(box._min,box._max);
  33. }
  34. Vec3 AABB::getCenter()
  35. {
  36. Vec3 center;
  37. center.x = 0.5f*(_min.x+_max.x);
  38. center.y = 0.5f*(_min.y+_max.y);
  39. center.z = 0.5f*(_min.z+_max.z);
  40. return center;
  41. }
  42. void AABB::getCorners(Vec3 *dst) const
  43. {
  44. assert(dst);
  45. // Near face, specified counter-clockwise looking towards the origin from the positive z-axis.
  46. // Left-top-front.
  47. dst[0].set(_min.x, _max.y, _max.z);
  48. // Left-bottom-front.
  49. dst[1].set(_min.x, _min.y, _max.z);
  50. // Right-bottom-front.
  51. dst[2].set(_max.x, _min.y, _max.z);
  52. // Right-top-front.
  53. dst[3].set(_max.x, _max.y, _max.z);
  54. // Far face, specified counter-clockwise looking towards the origin from the negative z-axis.
  55. // Right-top-back.
  56. dst[4].set(_max.x, _max.y, _min.z);
  57. // Right-bottom-back.
  58. dst[5].set(_max.x, _min.y, _min.z);
  59. // Left-bottom-back.
  60. dst[6].set(_min.x, _min.y, _min.z);
  61. // Left-top-back.
  62. dst[7].set(_min.x, _max.y, _min.z);
  63. }
  64. bool AABB::intersects(const AABB& aabb) const
  65. {
  66. return ((_min.x >= aabb._min.x && _min.x <= aabb._max.x) || (aabb._min.x >= _min.x && aabb._min.x <= _max.x)) &&
  67. ((_min.y >= aabb._min.y && _min.y <= aabb._max.y) || (aabb._min.y >= _min.y && aabb._min.y <= _max.y)) &&
  68. ((_min.z >= aabb._min.z && _min.z <= aabb._max.z) || (aabb._min.z >= _min.z && aabb._min.z <= _max.z));
  69. }
  70. bool AABB::containPoint(const Vec3& point) const
  71. {
  72. if (point.x < _min.x) return false;
  73. if (point.y < _min.y) return false;
  74. if (point.z < _min.z) return false;
  75. if (point.x > _max.x) return false;
  76. if (point.y > _max.y) return false;
  77. if (point.z > _max.z) return false;
  78. return true;
  79. }
  80. void AABB::merge(const AABB& box)
  81. {
  82. // Calculate the new minimum point.
  83. _min.x = std::min(_min.x, box._min.x);
  84. _min.y = std::min(_min.y, box._min.y);
  85. _min.z = std::min(_min.z, box._min.z);
  86. // Calculate the new maximum point.
  87. _max.x = std::max(_max.x, box._max.x);
  88. _max.y = std::max(_max.y, box._max.y);
  89. _max.z = std::max(_max.z, box._max.z);
  90. }
  91. void AABB::set(const Vec3& min, const Vec3& max)
  92. {
  93. this->_min = min;
  94. this->_max = max;
  95. }
  96. void AABB::reset()
  97. {
  98. _min.set(99999.0f, 99999.0f, 99999.0f);
  99. _max.set(-99999.0f, -99999.0f, -99999.0f);
  100. }
  101. bool AABB::isEmpty() const
  102. {
  103. return _min.x > _max.x || _min.y > _max.y || _min.z > _max.z;
  104. }
  105. void AABB::updateMinMax(const Vec3* point, ssize_t num)
  106. {
  107. for (ssize_t i = 0; i < num; i++)
  108. {
  109. // Leftmost point.
  110. if (point[i].x < _min.x)
  111. _min.x = point[i].x;
  112. // Lowest point.
  113. if (point[i].y < _min.y)
  114. _min.y = point[i].y;
  115. // Farthest point.
  116. if (point[i].z < _min.z)
  117. _min.z = point[i].z;
  118. // Rightmost point.
  119. if (point[i].x > _max.x)
  120. _max.x = point[i].x;
  121. // Highest point.
  122. if (point[i].y > _max.y)
  123. _max.y = point[i].y;
  124. // Nearest point.
  125. if (point[i].z > _max.z)
  126. _max.z = point[i].z;
  127. }
  128. }
  129. void AABB::transform(const Mat4& mat)
  130. {
  131. Vec3 corners[8];
  132. // Near face, specified counter-clockwise
  133. // Left-top-front.
  134. corners[0].set(_min.x, _max.y, _max.z);
  135. // Left-bottom-front.
  136. corners[1].set(_min.x, _min.y, _max.z);
  137. // Right-bottom-front.
  138. corners[2].set(_max.x, _min.y, _max.z);
  139. // Right-top-front.
  140. corners[3].set(_max.x, _max.y, _max.z);
  141. // Far face, specified clockwise
  142. // Right-top-back.
  143. corners[4].set(_max.x, _max.y, _min.z);
  144. // Right-bottom-back.
  145. corners[5].set(_max.x, _min.y, _min.z);
  146. // Left-bottom-back.
  147. corners[6].set(_min.x, _min.y, _min.z);
  148. // Left-top-back.
  149. corners[7].set(_min.x, _max.y, _min.z);
  150. // Transform the corners, recalculate the min and max points along the way.
  151. for (int i = 0; i < 8; i++)
  152. mat.transformPoint(&corners[i]);
  153. reset();
  154. updateMinMax(corners, 8);
  155. }
  156. NS_CC_END