2
0

CmRay.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. {
  70. return Math::intersects(*this, p);
  71. }
  72. /** Tests whether this ray intersects the given sphere.
  73. @returns A pair structure where the first element indicates whether
  74. an intersection occurs, and if true, the second element will
  75. indicate the distance along the ray at which it intersects.
  76. This can be converted to a point in space by calling getPoint().
  77. */
  78. std::pair<bool, float> intersects(const Sphere& s) const
  79. {
  80. return Math::intersects(*this, s);
  81. }
  82. /** Tests whether this ray intersects the given box.
  83. @returns A pair structure where the first element indicates whether
  84. an intersection occurs, and if true, the second element will
  85. indicate the distance along the ray at which it intersects.
  86. This can be converted to a point in space by calling getPoint().
  87. */
  88. std::pair<bool, float> intersects(const AxisAlignedBox& box) const
  89. {
  90. return Math::intersects(*this, box);
  91. }
  92. };
  93. /** @} */
  94. /** @} */
  95. }
  96. #endif