1 //
2 // Copyright (c) 2008-2017 the Urho3D project.
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to deal
6 // in the Software without restriction, including without limitation the rights
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 // copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 // THE SOFTWARE.
21 //
22 
23 #pragma once
24 
25 #include "../Math/Vector3.h"
26 #include "../Math/Matrix3x4.h"
27 
28 namespace Urho3D
29 {
30 
31 class BoundingBox;
32 class Frustum;
33 class Plane;
34 class Sphere;
35 
36 /// Infinite straight line in three-dimensional space.
37 class URHO3D_API Ray
38 {
39 public:
40     /// Construct a degenerate ray with zero origin and direction.
Ray()41     Ray()
42     {
43     }
44 
45     /// Construct from origin and direction. The direction will be normalized.
Ray(const Vector3 & origin,const Vector3 & direction)46     Ray(const Vector3& origin, const Vector3& direction)
47     {
48         Define(origin, direction);
49     }
50 
51     /// Copy-construct from another ray.
Ray(const Ray & ray)52     Ray(const Ray& ray) :
53         origin_(ray.origin_),
54         direction_(ray.direction_)
55     {
56     }
57 
58     /// Assign from another ray.
59     Ray& operator =(const Ray& rhs)
60     {
61         origin_ = rhs.origin_;
62         direction_ = rhs.direction_;
63         return *this;
64     }
65 
66     /// Check for equality with another ray.
67     bool operator ==(const Ray& rhs) const { return origin_ == rhs.origin_ && direction_ == rhs.direction_; }
68 
69     /// Check for inequality with another ray.
70     bool operator !=(const Ray& rhs) const { return origin_ != rhs.origin_ || direction_ != rhs.direction_; }
71 
72     /// Define from origin and direction. The direction will be normalized.
Define(const Vector3 & origin,const Vector3 & direction)73     void Define(const Vector3& origin, const Vector3& direction)
74     {
75         origin_ = origin;
76         direction_ = direction.Normalized();
77     }
78 
79     /// Project a point on the ray.
Project(const Vector3 & point)80     Vector3 Project(const Vector3& point) const
81     {
82         Vector3 offset = point - origin_;
83         return origin_ + offset.DotProduct(direction_) * direction_;
84     }
85 
86     /// Return distance of a point from the ray.
Distance(const Vector3 & point)87     float Distance(const Vector3& point) const
88     {
89         Vector3 projected = Project(point);
90         return (point - projected).Length();
91     }
92 
93     /// Return closest point to another ray.
94     Vector3 ClosestPoint(const Ray& ray) const;
95     /// Return hit distance to a plane, or infinity if no hit.
96     float HitDistance(const Plane& plane) const;
97     /// Return hit distance to a bounding box, or infinity if no hit.
98     float HitDistance(const BoundingBox& box) const;
99     /// Return hit distance to a frustum, or infinity if no hit. If solidInside parameter is true (default) rays originating from inside return zero distance, otherwise the distance to the closest plane.
100     float HitDistance(const Frustum& frustum, bool solidInside = true) const;
101     /// Return hit distance to a sphere, or infinity if no hit.
102     float HitDistance(const Sphere& sphere) const;
103     /// Return hit distance to a triangle, or infinity if no hit. Optionally return hit normal and hit barycentric coordinate at intersect point.
104     float HitDistance(const Vector3& v0, const Vector3& v1, const Vector3& v2, Vector3* outNormal = 0, Vector3* outBary = 0) const;
105     /// Return hit distance to non-indexed geometry data, or infinity if no hit. Optionally return hit normal and hit uv coordinates at intersect point.
106     float HitDistance
107         (const void* vertexData, unsigned vertexStride, unsigned vertexStart, unsigned vertexCount, Vector3* outNormal = 0,
108             Vector2* outUV = 0, unsigned uvOffset = 0) const;
109     /// Return hit distance to indexed geometry data, or infinity if no hit. Optionally return hit normal and hit uv coordinates at intersect point.
110     float HitDistance(const void* vertexData, unsigned vertexStride, const void* indexData, unsigned indexSize, unsigned indexStart,
111         unsigned indexCount, Vector3* outNormal = 0, Vector2* outUV = 0, unsigned uvOffset = 0) const;
112     /// Return whether ray is inside non-indexed geometry.
113     bool InsideGeometry(const void* vertexData, unsigned vertexSize, unsigned vertexStart, unsigned vertexCount) const;
114     /// Return whether ray is inside indexed geometry.
115     bool InsideGeometry(const void* vertexData, unsigned vertexSize, const void* indexData, unsigned indexSize, unsigned indexStart,
116         unsigned indexCount) const;
117     /// Return transformed by a 3x4 matrix. This may result in a non-normalized direction.
118     Ray Transformed(const Matrix3x4& transform) const;
119 
120     /// Ray origin.
121     Vector3 origin_;
122     /// Ray direction.
123     Vector3 direction_;
124 };
125 
126 }
127