CCAABB.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #ifndef __CC_AABB_H__
  21. #define __CC_AABB_H__
  22. #include "base/ccMacros.h"
  23. #include "math/CCMath.h"
  24. NS_CC_BEGIN
  25. /**
  26. * @addtogroup _3d
  27. * @{
  28. */
  29. /**
  30. * Axis Aligned Bounding Box (AABB), usually calculate some rough but fast collision detection.
  31. */
  32. class CC_DLL AABB
  33. {
  34. public:
  35. /**
  36. * Constructor.
  37. * @lua new
  38. */
  39. AABB();
  40. /**
  41. * Constructor.
  42. * @lua new
  43. */
  44. AABB(const Vec3& min, const Vec3& max);
  45. /**
  46. * Constructor.
  47. */
  48. AABB(const AABB& box);
  49. /**
  50. * Gets the center point of the bounding box.
  51. */
  52. Vec3 getCenter();
  53. /* Near face, specified counter-clockwise looking towards the origin from the positive z-axis.
  54. * verts[0] : left top front
  55. * verts[1] : left bottom front
  56. * verts[2] : right bottom front
  57. * verts[3] : right top front
  58. *
  59. * Far face, specified counter-clockwise looking towards the origin from the negative z-axis.
  60. * verts[4] : right top back
  61. * verts[5] : right bottom back
  62. * verts[6] : left bottom back
  63. * verts[7] : left top back
  64. */
  65. void getCorners(Vec3 *dst) const;
  66. /**
  67. * Tests whether this bounding box intersects the specified bounding object.
  68. */
  69. bool intersects(const AABB& aabb) const;
  70. /**
  71. * check whether the point is in.
  72. */
  73. bool containPoint(const Vec3& point) const;
  74. /**
  75. * Sets this bounding box to the smallest bounding box
  76. * that contains both this bounding object and the specified bounding box.
  77. */
  78. void merge(const AABB& box);
  79. /**
  80. * Sets this bounding box to the specified values.
  81. */
  82. void set(const Vec3& min, const Vec3& max);
  83. /**
  84. * Reset min and max value.If you invoke this method, isEmpty() shall return true.
  85. */
  86. void reset();
  87. /**
  88. * check the AABB object is empty(reset).
  89. */
  90. bool isEmpty() const;
  91. /**
  92. * update the _min and _max from the given point.
  93. */
  94. void updateMinMax(const Vec3* point, ssize_t num);
  95. /**
  96. * Transforms the bounding box by the given transformation matrix.
  97. */
  98. void transform(const Mat4& mat);
  99. /* --- --- --- --- Add by XuJJ start --- --- --- --- */
  100. void Encapsulate (const Vec3& inPoint);
  101. // void Encapsulate (const AABB& aabb);
  102. void Encapsulate (const AABB& other);
  103. void GetVertices( Vec3 outVertices[8] ) const;
  104. /* --- --- --- --- Add by XuJJ end --- --- --- --- */
  105. public:
  106. Vec3 _min;
  107. Vec3 _max;
  108. void Init ();
  109. };
  110. /* --- --- --- --- Add by XuJJ start --- --- --- --- */
  111. /// This version is much slower but works correctly with non-uniform scale
  112. void TransformAABBSlow (const AABB& aabb, const Mat4& transform, AABB& result);
  113. inline void AABB::Encapsulate (const Vec3& inPoint)
  114. {
  115. _min = Min (_min, inPoint);
  116. _max = Max (_max, inPoint);
  117. }
  118. //inline void AABB::Encapsulate (const AABB& aabb)
  119. //{
  120. // Encapsulate (aabb.GetCenter()+aabb.GetExtent());
  121. // Encapsulate (aabb.GetCenter()-aabb.GetExtent());
  122. //}
  123. inline void AABB::Encapsulate (const AABB& other)
  124. {
  125. _min = Min (_min, other._min);
  126. _max = Max (_max, other._max);
  127. }
  128. /* --- --- --- --- Add by XuJJ end --- --- --- --- */
  129. /* --- --- --- --- Add by QY --- --- --- --- */
  130. inline void AABB::Init ()
  131. {
  132. _min = Vec3::infinityVec;
  133. _max = -Vec3::infinityVec;
  134. }
  135. /* --- --- --- --- Add by QY --- --- --- --- */
  136. // end of 3d group
  137. /// @}
  138. NS_CC_END
  139. #endif