CmPlane.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. // This file is based on material originally from:
  25. // Geometric Tools, LLC
  26. // Copyright (c) 1998-2010
  27. // Distributed under the Boost Software License, Version 1.0.
  28. // http://www.boost.org/LICENSE_1_0.txt
  29. // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
  30. #ifndef __Plane_H__
  31. #define __Plane_H__
  32. #include "CmPrerequisitesUtil.h"
  33. #include "CmVector3.h"
  34. namespace CamelotFramework {
  35. /** \addtogroup Core
  36. * @{
  37. */
  38. /** \addtogroup Math
  39. * @{
  40. */
  41. /** Defines a plane in 3D space.
  42. @remarks
  43. A plane is defined in 3D space by the equation
  44. Ax + By + Cz + D = 0
  45. @par
  46. This equates to a vector (the normal of the plane, whose x, y
  47. and z components equate to the coefficients A, B and C
  48. respectively), and a constant (D) which is the distance along
  49. the normal you have to go to move the plane back to the origin.
  50. */
  51. class CM_UTILITY_EXPORT Plane
  52. {
  53. public:
  54. /** Default constructor - sets everything to 0.
  55. */
  56. Plane ();
  57. Plane (const Plane& rhs);
  58. /** Construct a plane through a normal, and a distance to move the plane along the normal.*/
  59. Plane (const Vector3& rkNormal, float fConstant);
  60. /** Construct a plane using the 4 constants directly **/
  61. Plane (float a, float b, float c, float d);
  62. Plane (const Vector3& rkNormal, const Vector3& rkPoint);
  63. Plane (const Vector3& rkPoint0, const Vector3& rkPoint1,
  64. const Vector3& rkPoint2);
  65. /** The "positive side" of the plane is the half space to which the
  66. plane normal points. The "negative side" is the other half
  67. space. The flag "no side" indicates the plane itself.
  68. */
  69. enum Side
  70. {
  71. NO_SIDE,
  72. POSITIVE_SIDE,
  73. NEGATIVE_SIDE,
  74. BOTH_SIDE
  75. };
  76. Side getSide (const Vector3& rkPoint) const;
  77. /**
  78. Returns the side where the alignedBox is. The flag BOTH_SIDE indicates an intersecting box.
  79. One corner ON the plane is sufficient to consider the box and the plane intersecting.
  80. */
  81. Side getSide (const AABox& rkBox) const;
  82. /** Returns which side of the plane that the given box lies on.
  83. The box is defined as centre/half-size pairs for effectively.
  84. @param centre The centre of the box.
  85. @param halfSize The half-size of the box.
  86. @returns
  87. POSITIVE_SIDE if the box complete lies on the "positive side" of the plane,
  88. NEGATIVE_SIDE if the box complete lies on the "negative side" of the plane,
  89. and BOTH_SIDE if the box intersects the plane.
  90. */
  91. Side getSide (const Vector3& centre, const Vector3& halfSize) const;
  92. /** This is a pseudodistance. The sign of the return value is
  93. positive if the point is on the positive side of the plane,
  94. negative if the point is on the negative side, and zero if the
  95. point is on the plane.
  96. @par
  97. The absolute value of the return value is the true distance only
  98. when the plane normal is a unit length vector.
  99. */
  100. float getDistance (const Vector3& rkPoint) const;
  101. /** Redefine this plane based on 3 points. */
  102. void redefine(const Vector3& rkPoint0, const Vector3& rkPoint1,
  103. const Vector3& rkPoint2);
  104. /** Redefine this plane based on a normal and a point. */
  105. void redefine(const Vector3& rkNormal, const Vector3& rkPoint);
  106. /** Project a vector onto the plane.
  107. @remarks This gives you the element of the input vector that is perpendicular
  108. to the normal of the plane. You can get the element which is parallel
  109. to the normal of the plane by subtracting the result of this method
  110. from the original vector, since parallel + perpendicular = original.
  111. @param v The input vector
  112. */
  113. Vector3 projectVector(const Vector3& v) const;
  114. /** Normalises the plane.
  115. @remarks
  116. This method normalises the plane's normal and the length scale of d
  117. is as well.
  118. @note
  119. This function will not crash for zero-sized vectors, but there
  120. will be no changes made to their components.
  121. @returns The previous length of the plane's normal.
  122. */
  123. float normalize(void);
  124. Vector3 normal;
  125. float d;
  126. /// Comparison operator
  127. bool operator==(const Plane& rhs) const
  128. {
  129. return (rhs.d == d && rhs.normal == normal);
  130. }
  131. bool operator!=(const Plane& rhs) const
  132. {
  133. return (rhs.d != d || rhs.normal != normal);
  134. }
  135. CM_UTILITY_EXPORT friend std::ostream& operator<< (std::ostream& o, const Plane& p);
  136. };
  137. typedef Vector<Plane>::type PlaneList;
  138. /** @} */
  139. /** @} */
  140. } // namespace CamelotFramework
  141. #endif