CmPlane.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #include "CmPlane.h"
  25. #include "CmMatrix3.h"
  26. #include "CmAABox.h"
  27. #include "CmRay.h"
  28. namespace CamelotFramework {
  29. //-----------------------------------------------------------------------
  30. Plane::Plane ()
  31. {
  32. normal = Vector3::ZERO;
  33. d = 0.0;
  34. }
  35. //-----------------------------------------------------------------------
  36. Plane::Plane (const Plane& rhs)
  37. {
  38. normal = rhs.normal;
  39. d = rhs.d;
  40. }
  41. //-----------------------------------------------------------------------
  42. Plane::Plane (const Vector3& rkNormal, float fConstant)
  43. {
  44. normal = rkNormal;
  45. d = -fConstant;
  46. }
  47. //---------------------------------------------------------------------
  48. Plane::Plane (float a, float b, float c, float _d)
  49. : normal(a, b, c), d(_d)
  50. {
  51. }
  52. //-----------------------------------------------------------------------
  53. Plane::Plane (const Vector3& rkNormal, const Vector3& rkPoint)
  54. {
  55. redefine(rkNormal, rkPoint);
  56. }
  57. //-----------------------------------------------------------------------
  58. Plane::Plane (const Vector3& rkPoint0, const Vector3& rkPoint1,
  59. const Vector3& rkPoint2)
  60. {
  61. redefine(rkPoint0, rkPoint1, rkPoint2);
  62. }
  63. //-----------------------------------------------------------------------
  64. float Plane::getDistance (const Vector3& rkPoint) const
  65. {
  66. return normal.dot(rkPoint) + d;
  67. }
  68. //-----------------------------------------------------------------------
  69. Plane::Side Plane::getSide (const Vector3& rkPoint) const
  70. {
  71. float fDistance = getDistance(rkPoint);
  72. if ( fDistance < 0.0 )
  73. return Plane::NEGATIVE_SIDE;
  74. if ( fDistance > 0.0 )
  75. return Plane::POSITIVE_SIDE;
  76. return Plane::NO_SIDE;
  77. }
  78. //-----------------------------------------------------------------------
  79. Plane::Side Plane::getSide (const AABox& box) const
  80. {
  81. return getSide(box.getCenter(), box.getHalfSize());
  82. }
  83. //-----------------------------------------------------------------------
  84. Plane::Side Plane::getSide (const Vector3& centre, const Vector3& halfSize) const
  85. {
  86. // Calculate the distance between box centre and the plane
  87. float dist = getDistance(centre);
  88. // Calculate the maximise allows absolute distance for
  89. // the distance between box centre and plane
  90. float maxAbsDist = Math::abs(normal.x * halfSize.x) + Math::abs(normal.y * halfSize.y) + Math::abs(normal.z * halfSize.z);
  91. if (dist < -maxAbsDist)
  92. return Plane::NEGATIVE_SIDE;
  93. if (dist > +maxAbsDist)
  94. return Plane::POSITIVE_SIDE;
  95. return Plane::BOTH_SIDE;
  96. }
  97. //-----------------------------------------------------------------------
  98. void Plane::redefine(const Vector3& rkPoint0, const Vector3& rkPoint1,
  99. const Vector3& rkPoint2)
  100. {
  101. Vector3 kEdge1 = rkPoint1 - rkPoint0;
  102. Vector3 kEdge2 = rkPoint2 - rkPoint0;
  103. normal = kEdge1.cross(kEdge2);
  104. normal.normalize();
  105. d = -normal.dot(rkPoint0);
  106. }
  107. //-----------------------------------------------------------------------
  108. void Plane::redefine(const Vector3& rkNormal, const Vector3& rkPoint)
  109. {
  110. normal = rkNormal;
  111. d = -rkNormal.dot(rkPoint);
  112. }
  113. //-----------------------------------------------------------------------
  114. Vector3 Plane::projectVector(const Vector3& p) const
  115. {
  116. // We know plane normal is unit length, so use simple method
  117. Matrix3 xform;
  118. xform[0][0] = 1.0f - normal.x * normal.x;
  119. xform[0][1] = -normal.x * normal.y;
  120. xform[0][2] = -normal.x * normal.z;
  121. xform[1][0] = -normal.y * normal.x;
  122. xform[1][1] = 1.0f - normal.y * normal.y;
  123. xform[1][2] = -normal.y * normal.z;
  124. xform[2][0] = -normal.z * normal.x;
  125. xform[2][1] = -normal.z * normal.y;
  126. xform[2][2] = 1.0f - normal.z * normal.z;
  127. return xform.transform(p);
  128. }
  129. //-----------------------------------------------------------------------
  130. float Plane::normalize(void)
  131. {
  132. float fLength = normal.length();
  133. // Will also work for zero-sized vectors, but will change nothing
  134. if (fLength > 1e-08f)
  135. {
  136. float fInvLength = 1.0f / fLength;
  137. normal *= fInvLength;
  138. d *= fInvLength;
  139. }
  140. return fLength;
  141. }
  142. std::pair<bool, float> Plane::intersects(const Ray& ray) const
  143. {
  144. float denom = normal.dot(ray.getDirection());
  145. if (Math::abs(denom) < std::numeric_limits<float>::epsilon())
  146. {
  147. // Parallel
  148. return std::pair<bool, float>(false, 0.0f);
  149. }
  150. else
  151. {
  152. float nom = normal.dot(ray.getOrigin()) + d;
  153. float t = -(nom/denom);
  154. return std::pair<bool, float>(t >= 0, t);
  155. }
  156. }
  157. }