1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
3 | #pragma once |
4 | |
5 | #include "Prerequisites/BsPrerequisitesUtil.h" |
6 | #include "Math/BsVector3.h" |
7 | |
8 | namespace bs |
9 | { |
10 | /** @addtogroup Math |
11 | * @{ |
12 | */ |
13 | |
14 | /** A ray in 3D space represented with an origin and direction. */ |
15 | class BS_UTILITY_EXPORT Ray |
16 | { |
17 | public: |
18 | Ray() = default; |
19 | |
20 | Ray(const Vector3& origin, const Vector3& direction) |
21 | :mOrigin(origin), mDirection(direction) |
22 | { } |
23 | |
24 | void setOrigin(const Vector3& origin) { mOrigin = origin; } |
25 | const Vector3& getOrigin() const { return mOrigin; } |
26 | |
27 | void setDirection(const Vector3& dir) { mDirection = dir; } |
28 | const Vector3& getDirection() const {return mDirection;} |
29 | |
30 | /** Gets the position of a point t units along the ray. */ |
31 | Vector3 getPoint(float t) const |
32 | { |
33 | return Vector3(mOrigin + (mDirection * t)); |
34 | } |
35 | |
36 | /** Gets the position of a point t units along the ray. */ |
37 | Vector3 operator*(float t) const |
38 | { |
39 | return getPoint(t); |
40 | } |
41 | |
42 | /** Transforms the ray by the given matrix. */ |
43 | void transform(const Matrix4& matrix); |
44 | |
45 | /** |
46 | * Transforms the ray by the given matrix. |
47 | * |
48 | * @note Provided matrix must be affine. |
49 | */ |
50 | void transformAffine(const Matrix4& matrix); |
51 | |
52 | /** Ray/plane intersection, returns boolean result and distance to intersection point. */ |
53 | std::pair<bool, float> intersects(const Plane& p) const; |
54 | |
55 | /** Ray/sphere intersection, returns boolean result and distance to nearest intersection point. */ |
56 | std::pair<bool, float> intersects(const Sphere& s) const; |
57 | |
58 | /** Ray/axis aligned box intersection, returns boolean result and distance to nearest intersection point. */ |
59 | std::pair<bool, float> intersects(const AABox& box) const; |
60 | |
61 | /** |
62 | * Ray/triangle intersection, returns boolean result and distance to intersection point. |
63 | * |
64 | * @param[in] a Triangle first vertex. |
65 | * @param[in] b Triangle second vertex. |
66 | * @param[in] c Triangle third vertex. |
67 | * @param[in] normal The normal of the triangle. Doesn't need to be normalized. |
68 | * @param[in] positiveSide (optional) Should intersections with the positive side (normal facing) count. |
69 | * @param[in] negativeSide (optional) Should intersections with the negative side (opposite of normal facing) count. |
70 | * @return Boolean result if intersection happened and distance to intersection point. |
71 | */ |
72 | std::pair<bool, float> intersects(const Vector3& a, const Vector3& b, const Vector3& c, |
73 | const Vector3& normal, bool positiveSide = true, bool negativeSide = true) const; |
74 | |
75 | protected: |
76 | Vector3 mOrigin{Vector3::ZERO}; |
77 | Vector3 mDirection{Vector3::UNIT_Z}; |
78 | }; |
79 | |
80 | /** @} */ |
81 | } |
82 | |