/src/ogre/OgreMain/include/OgreRay.h
Line | Count | Source (jump to first uncovered line) |
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 | | |
7 | | Copyright (c) 2000-2014 Torus Knot Software Ltd |
8 | | |
9 | | Permission is hereby granted, free of charge, to any person obtaining a copy |
10 | | of this software and associated documentation files (the "Software"), to deal |
11 | | in the Software without restriction, including without limitation the rights |
12 | | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 | | copies of the Software, and to permit persons to whom the Software is |
14 | | furnished to do so, subject to the following conditions: |
15 | | |
16 | | The above copyright notice and this permission notice shall be included in |
17 | | all copies or substantial portions of the Software. |
18 | | |
19 | | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 | | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 | | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22 | | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 | | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 | | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
25 | | THE SOFTWARE. |
26 | | ----------------------------------------------------------------------------- |
27 | | */ |
28 | | #ifndef __Ray_H_ |
29 | | #define __Ray_H_ |
30 | | |
31 | | // Precompiler options |
32 | | #include "OgrePrerequisites.h" |
33 | | |
34 | | #include "OgrePlaneBoundedVolume.h" |
35 | | |
36 | | namespace Ogre { |
37 | | |
38 | | /** \addtogroup Core |
39 | | * @{ |
40 | | */ |
41 | | /** \addtogroup Math |
42 | | * @{ |
43 | | */ |
44 | | /** Representation of a ray in space, i.e. a line with an origin and direction. */ |
45 | | class _OgreExport Ray |
46 | | { |
47 | | private: |
48 | | Vector3 mOrigin; |
49 | | Vector3 mDirection; |
50 | | public: |
51 | 0 | Ray():mOrigin(Vector3::ZERO), mDirection(Vector3::UNIT_Z) {} |
52 | | Ray(const Vector3& origin, const Vector3& direction) |
53 | 0 | :mOrigin(origin), mDirection(direction) {} |
54 | | |
55 | | /** Sets the origin of the ray. */ |
56 | 0 | void setOrigin(const Vector3& origin) {mOrigin = origin;} |
57 | | /** Gets the origin of the ray. */ |
58 | 0 | const Vector3& getOrigin(void) const {return mOrigin;} |
59 | | |
60 | | /** Sets the direction of the ray. */ |
61 | 0 | void setDirection(const Vector3& dir) {mDirection = dir;} |
62 | | /** Gets the direction of the ray. */ |
63 | 0 | const Vector3& getDirection(void) const {return mDirection;} |
64 | | |
65 | | /** Gets the position of a point t units along the ray. */ |
66 | 0 | Vector3 getPoint(Real t) const { |
67 | 0 | return Vector3(mOrigin + (mDirection * t)); |
68 | 0 | } |
69 | | |
70 | | /** Gets the position of a point t units along the ray. */ |
71 | 0 | Vector3 operator*(Real t) const { |
72 | 0 | return getPoint(t); |
73 | 0 | } |
74 | | |
75 | | /** Tests whether this ray intersects the given plane. */ |
76 | | RayTestResult intersects(const Plane& p) const |
77 | 0 | { |
78 | 0 | Real denom = p.normal.dotProduct(mDirection); |
79 | 0 | if (Math::Abs(denom) < std::numeric_limits<Real>::epsilon()) |
80 | 0 | { |
81 | | // Parallel |
82 | 0 | return RayTestResult(false, (Real)0); |
83 | 0 | } |
84 | 0 | else |
85 | 0 | { |
86 | 0 | Real nom = p.normal.dotProduct(mOrigin) + p.d; |
87 | 0 | Real t = -(nom / denom); |
88 | 0 | return RayTestResult(t >= 0, (Real)t); |
89 | 0 | } |
90 | 0 | } |
91 | | /** Tests whether this ray intersects the given plane bounded volume. */ |
92 | | RayTestResult intersects(const PlaneBoundedVolume& p) const |
93 | 0 | { |
94 | 0 | return Math::intersects(*this, p.planes, p.outside == Plane::POSITIVE_SIDE); |
95 | 0 | } |
96 | | /** Tests whether this ray intersects the given sphere. */ |
97 | | RayTestResult intersects(const Sphere& s, bool discardInside = true) const |
98 | 0 | { |
99 | 0 | // Adjust ray origin relative to sphere center |
100 | 0 | Vector3 rayorig = mOrigin - s.getCenter(); |
101 | 0 | Real radius = s.getRadius(); |
102 | 0 |
|
103 | 0 | // Check origin inside first |
104 | 0 | if (rayorig.squaredLength() <= radius*radius && discardInside) |
105 | 0 | { |
106 | 0 | return RayTestResult(true, (Real)0); |
107 | 0 | } |
108 | 0 |
|
109 | 0 | // Mmm, quadratics |
110 | 0 | // Build coeffs which can be used with std quadratic solver |
111 | 0 | // ie t = (-b +/- sqrt(b*b + 4ac)) / 2a |
112 | 0 | Real a = mDirection.dotProduct(mDirection); |
113 | 0 | Real b = 2 * rayorig.dotProduct(mDirection); |
114 | 0 | Real c = rayorig.dotProduct(rayorig) - radius*radius; |
115 | 0 |
|
116 | 0 | // Calc determinant |
117 | 0 | Real d = (b*b) - (4 * a * c); |
118 | 0 | if (d < 0) |
119 | 0 | { |
120 | 0 | // No intersection |
121 | 0 | return RayTestResult(false, (Real)0); |
122 | 0 | } |
123 | 0 | else |
124 | 0 | { |
125 | 0 | // BTW, if d=0 there is one intersection, if d > 0 there are 2 |
126 | 0 | // But we only want the closest one, so that's ok, just use the |
127 | 0 | // '-' version of the solver |
128 | 0 | Real t = ( -b - Math::Sqrt(d) ) / (2 * a); |
129 | 0 | if (t < 0) |
130 | 0 | t = ( -b + Math::Sqrt(d) ) / (2 * a); |
131 | 0 | return RayTestResult(true, t); |
132 | 0 | } |
133 | 0 | } |
134 | | /** Tests whether this ray intersects the given box. */ |
135 | | RayTestResult intersects(const AxisAlignedBox& box) const |
136 | 0 | { |
137 | 0 | return Math::intersects(*this, box); |
138 | 0 | } |
139 | | |
140 | | }; |
141 | | |
142 | | inline RayTestResult Math::intersects(const Ray& ray, const Plane& plane) |
143 | 0 | { |
144 | 0 | return ray.intersects(plane); |
145 | 0 | } |
146 | | |
147 | | inline RayTestResult Math::intersects(const Ray& ray, const Sphere& sphere, bool discardInside) |
148 | 0 | { |
149 | 0 | return ray.intersects(sphere, discardInside); |
150 | 0 | } |
151 | | /** @} */ |
152 | | /** @} */ |
153 | | |
154 | | } |
155 | | #endif |