CCPUPlane.cpp 5.6 KB

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