CmRay.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #ifndef __Ray_H_
  25. #define __Ray_H_
  26. // Precompiler options
  27. #include "CmPrerequisitesUtil.h"
  28. #include "CmVector3.h"
  29. namespace CamelotFramework {
  30. /** \addtogroup Core
  31. * @{
  32. */
  33. /** \addtogroup Math
  34. * @{
  35. */
  36. /** Representation of a ray in space, i.e. a line with an origin and direction. */
  37. class CM_UTILITY_EXPORT Ray
  38. {
  39. protected:
  40. Vector3 mOrigin;
  41. Vector3 mDirection;
  42. public:
  43. Ray():mOrigin(Vector3::ZERO), mDirection(Vector3::UNIT_Z) {}
  44. Ray(const Vector3& origin, const Vector3& direction)
  45. :mOrigin(origin), mDirection(direction) {}
  46. /** Sets the origin of the ray. */
  47. void setOrigin(const Vector3& origin) {mOrigin = origin;}
  48. /** Gets the origin of the ray. */
  49. const Vector3& getOrigin(void) const {return mOrigin;}
  50. /** Sets the direction of the ray. */
  51. void setDirection(const Vector3& dir) {mDirection = dir;}
  52. /** Gets the direction of the ray. */
  53. const Vector3& getDirection(void) const {return mDirection;}
  54. /** Gets the position of a point t units along the ray. */
  55. Vector3 getPoint(float t) const {
  56. return Vector3(mOrigin + (mDirection * t));
  57. }
  58. /** Gets the position of a point t units along the ray. */
  59. Vector3 operator*(float t) const {
  60. return getPoint(t);
  61. }
  62. /** Tests whether this ray intersects the given plane.
  63. @returns A pair structure where the first element indicates whether
  64. an intersection occurs, and if true, the second element will
  65. indicate the distance along the ray at which it intersects.
  66. This can be converted to a point in space by calling getPoint().
  67. */
  68. std::pair<bool, float> intersects(const Plane& p) const;
  69. /** Tests whether this ray intersects the given sphere.
  70. @returns A pair structure where the first element indicates whether
  71. an intersection occurs, and if true, the second element will
  72. indicate the distance along the ray at which it intersects.
  73. This can be converted to a point in space by calling getPoint().
  74. */
  75. std::pair<bool, float> intersects(const Sphere& s) const;
  76. /** Tests whether this ray intersects the given box.
  77. @returns A pair structure where the first element indicates whether
  78. an intersection occurs, and if true, the second element will
  79. indicate the distance along the ray at which it intersects.
  80. This can be converted to a point in space by calling getPoint().
  81. */
  82. std::pair<bool, float> intersects(const AABox& box) const;
  83. /** Ray / triangle intersection, returns boolean result and distance.
  84. @param
  85. ray The ray.
  86. @param
  87. a The triangle's first vertex.
  88. @param
  89. b The triangle's second vertex.
  90. @param
  91. c The triangle's third vertex.
  92. @param
  93. normal The triangle plane's normal (passed in rather than calculated
  94. on demand since the caller may already have it), doesn't need
  95. normalised since we don't care.
  96. @param
  97. positiveSide Intersect with "positive side" of the triangle
  98. @param
  99. negativeSide Intersect with "negative side" of the triangle
  100. @returns
  101. If the ray is intersects the triangle, a pair of <b>true</b> and the
  102. distance between intersection point and ray origin returned.
  103. @par
  104. If the ray isn't intersects the triangle, a pair of <b>false</b> and
  105. <b>0</b> returned.
  106. */
  107. std::pair<bool, float> intersects(const Vector3& a,
  108. const Vector3& b, const Vector3& c, const Vector3& normal,
  109. bool positiveSide = true, bool negativeSide = true) const;
  110. };
  111. /** @} */
  112. /** @} */
  113. }
  114. #endif