Ray.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  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. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  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. #pragma once
  24. #include "Vector3.h"
  25. /// Infinite straight line in three-dimensional space
  26. class Ray
  27. {
  28. public:
  29. /// Construct an undefined ray
  30. Ray()
  31. {
  32. }
  33. /// Construct from origin and direction. The direction must be Normalized
  34. Ray(const Vector3& origin, const Vector3& direction) :
  35. origin_(origin),
  36. direction_(direction)
  37. {
  38. }
  39. /// Copy-construct from another ray
  40. Ray(const Ray& ray) :
  41. origin_(ray.origin_),
  42. direction_(ray.direction_)
  43. {
  44. }
  45. /// Assign from another ray
  46. Ray& operator = (const Ray& rhs)
  47. {
  48. origin_ = rhs.origin_;
  49. direction_ = rhs.direction_;
  50. return *this;
  51. }
  52. /// Check for equality with another ray
  53. bool operator == (const Ray& rhs) const { return (origin_ == rhs.origin_) && (direction_ == rhs.direction_); }
  54. /// Check for inequality with another ray
  55. bool operator != (const Ray& rhs) const { return (origin_ != rhs.origin_) || (direction_ != rhs.direction_); }
  56. /// Define from origin and direction. The direction will be Normalized
  57. void Define(const Vector3& origin, const Vector3& direction)
  58. {
  59. origin_ = origin;
  60. direction_ = direction.GetNormalized();
  61. }
  62. /// Project a point on the ray
  63. Vector3 Project(const Vector3& point) const;
  64. /// Return minimum distance to a triangle, or infinity if no hit
  65. float GetDistance(const Vector3& v0, const Vector3& v1, const Vector3& v2) const;
  66. /// Return minimum distance to a triangle mesh defined by vertex and index data
  67. float GetDistance(const void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize, unsigned indexStart, unsigned indexCount) const;
  68. /// Ray origin
  69. Vector3 origin_;
  70. /// Ray direction
  71. Vector3 direction_;
  72. };