CCPUPlane.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /****************************************************************************
  2. Copyright (C) 2013 Henry van Merode. All rights reserved.
  3. Copyright (c) 2015-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  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 FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "CCPUPlane.h"
  23. NS_CC_BEGIN
  24. //-----------------------------------------------------------------------
  25. PUPlane::PUPlane ()
  26. {
  27. // Vec3 default constructor zero
  28. //normal = Vec3::ZERO;
  29. d = 0.0;
  30. }
  31. //-----------------------------------------------------------------------
  32. PUPlane::PUPlane (const PUPlane& rhs)
  33. {
  34. normal = rhs.normal;
  35. d = rhs.d;
  36. }
  37. //-----------------------------------------------------------------------
  38. PUPlane::PUPlane (const Vec3& rkNormal, float fConstant)
  39. {
  40. normal = rkNormal;
  41. d = -fConstant;
  42. }
  43. //---------------------------------------------------------------------
  44. PUPlane::PUPlane (float a, float b, float c, float _d)
  45. : normal(a, b, c), d(_d)
  46. {
  47. }
  48. //-----------------------------------------------------------------------
  49. PUPlane::PUPlane (const Vec3& rkNormal, const Vec3& rkPoint)
  50. {
  51. redefine(rkNormal, rkPoint);
  52. }
  53. //-----------------------------------------------------------------------
  54. PUPlane::PUPlane (const Vec3& rkPoint0, const Vec3& rkPoint1,
  55. const Vec3& rkPoint2)
  56. {
  57. redefine(rkPoint0, rkPoint1, rkPoint2);
  58. }
  59. //-----------------------------------------------------------------------
  60. float PUPlane::getDistance (const Vec3& rkPoint) const
  61. {
  62. return normal.dot(rkPoint) + d;
  63. }
  64. //-----------------------------------------------------------------------
  65. //Plane::Side Plane::getSide (const Vec3& rkPoint) const
  66. //{
  67. // float fDistance = getDistance(rkPoint);
  68. //
  69. // if ( fDistance < 0.0 )
  70. // return Plane::NEGATIVE_SIDE;
  71. //
  72. // if ( fDistance > 0.0 )
  73. // return Plane::POSITIVE_SIDE;
  74. //
  75. // return Plane::NO_SIDE;
  76. //}
  77. //
  78. //
  79. ////-----------------------------------------------------------------------
  80. //Plane::Side Plane::getSide (const AABB& box) const
  81. //{
  82. // if (box.isEmpty())
  83. // return NO_SIDE;
  84. // if (box.isInfinite())
  85. // return BOTH_SIDE;
  86. //
  87. // return getSide(box.getCenter(), box);
  88. //}
  89. ////-----------------------------------------------------------------------
  90. //Plane::Side Plane::getSide (const Vec3& centre, const Vec3& halfSize) const
  91. //{
  92. // // Calculate the distance between box centre and the plane
  93. // float dist = getDistance(centre);
  94. //
  95. // // Calculate the maximise allows absolute distance for
  96. // // the distance between box centre and plane
  97. // float maxAbsDist = normal.absDotProduct(halfSize);
  98. //
  99. // if (dist < -maxAbsDist)
  100. // return Plane::NEGATIVE_SIDE;
  101. //
  102. // if (dist > +maxAbsDist)
  103. // return Plane::POSITIVE_SIDE;
  104. //
  105. // return Plane::BOTH_SIDE;
  106. //}
  107. //-----------------------------------------------------------------------
  108. void PUPlane::redefine(const Vec3& rkPoint0, const Vec3& rkPoint1,
  109. const Vec3& rkPoint2)
  110. {
  111. Vec3 kEdge1 = rkPoint1 - rkPoint0;
  112. Vec3 kEdge2 = rkPoint2 - rkPoint0;
  113. Vec3::cross(kEdge1, kEdge2, &normal);
  114. normal.normalize();
  115. d = -normal.dot(rkPoint0);
  116. }
  117. //-----------------------------------------------------------------------
  118. void PUPlane::redefine(const Vec3& rkNormal, const Vec3& rkPoint)
  119. {
  120. normal = rkNormal;
  121. d = -rkNormal.dot(rkPoint);
  122. }
  123. //-----------------------------------------------------------------------
  124. Vec3 PUPlane::projectVector(const Vec3& p) const
  125. {
  126. // We know plane normal is unit length, so use simple method
  127. //Matrix3 xform;
  128. //xform[0][0] = 1.0f - normal.x * normal.x;
  129. //xform[0][1] = -normal.x * normal.y;
  130. //xform[0][2] = -normal.x * normal.z;
  131. //xform[1][0] = -normal.y * normal.x;
  132. //xform[1][1] = 1.0f - normal.y * normal.y;
  133. //xform[1][2] = -normal.y * normal.z;
  134. //xform[2][0] = -normal.z * normal.x;
  135. //xform[2][1] = -normal.z * normal.y;
  136. //xform[2][2] = 1.0f - normal.z * normal.z;
  137. Mat4 xform;
  138. xform.m[0] = 1.0f - normal.x * normal.x;
  139. xform.m[1] = -normal.x * normal.y;
  140. xform.m[2] = -normal.x * normal.z;
  141. xform.m[4] = -normal.y * normal.x;
  142. xform.m[5] = 1.0f - normal.y * normal.y;
  143. xform.m[6] = -normal.y * normal.z;
  144. xform.m[8] = -normal.z * normal.x;
  145. xform.m[9] = -normal.z * normal.y;
  146. xform.m[10] = 1.0f - normal.z * normal.z;
  147. return xform * p;
  148. }
  149. //-----------------------------------------------------------------------
  150. float PUPlane::normalize(void)
  151. {
  152. float fLength = normal.length();
  153. // Will also work for zero-sized vectors, but will change nothing
  154. // We're not using epsilons because we don't need to.
  155. // Read http://www.ogre3d.org/forums/viewtopic.php?f=4&t=61259
  156. if ( fLength > float(0.0f) )
  157. {
  158. float fInvLength = 1.0f / fLength;
  159. normal *= fInvLength;
  160. d *= fInvLength;
  161. }
  162. return fLength;
  163. }
  164. NS_CC_END