forked from nkast/MonoGame
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathRay.cs
237 lines (209 loc) · 8.9 KB
/
Ray.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// MIT License - Copyright (C) The Mono.Xna Team
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.
using System;
using System.Diagnostics;
using System.Runtime.Serialization;
namespace Microsoft.Xna.Framework
{
/// <summary>
/// Represents a ray with an origin and a direction in 3D space.
/// </summary>
[DataContract]
[DebuggerDisplay("{DebugDisplayString,nq}")]
public struct Ray : IEquatable<Ray>
{
#region Public Fields
/// <summary>
/// The direction of this <see cref="Ray"/>.
/// </summary>
[DataMember]
public Vector3 Direction;
/// <summary>
/// The origin of this <see cref="Ray"/>.
/// </summary>
[DataMember]
public Vector3 Position;
#endregion
#region Public Constructors
/// <summary>
/// Create a <see cref="Ray"/>.
/// </summary>
/// <param name="position">The origin of the <see cref="Ray"/>.</param>
/// <param name="direction">The direction of the <see cref="Ray"/>.</param>
public Ray(Vector3 position, Vector3 direction)
{
this.Position = position;
this.Direction = direction;
}
#endregion
#region Public Methods
/// <summary>
/// Check if the specified <see cref="Object"/> is equal to this <see cref="Ray"/>.
/// </summary>
/// <param name="obj">The <see cref="Object"/> to test for equality with this <see cref="Ray"/>.</param>
/// <returns>
/// <code>true</code> if the specified <see cref="Object"/> is equal to this <see cref="Ray"/>,
/// <code>false</code> if it is not.
/// </returns>
public override bool Equals(object obj)
{
return (obj is Ray) && this.Equals((Ray)obj);
}
/// <summary>
/// Check if the specified <see cref="Ray"/> is equal to this <see cref="Ray"/>.
/// </summary>
/// <param name="other">The <see cref="Ray"/> to test for equality with this <see cref="Ray"/>.</param>
/// <returns>
/// <code>true</code> if the specified <see cref="Ray"/> is equal to this <see cref="Ray"/>,
/// <code>false</code> if it is not.
/// </returns>
public bool Equals(Ray other)
{
return this.Position.Equals(other.Position) && this.Direction.Equals(other.Direction);
}
/// <summary>
/// Get a hash code for this <see cref="Ray"/>.
/// </summary>
/// <returns>A hash code for this <see cref="Ray"/>.</returns>
public override int GetHashCode()
{
return Position.GetHashCode() ^ Direction.GetHashCode();
}
/// <summary>
/// Check if this <see cref="Ray"/> intersects the specified <see cref="BoundingBox"/>.
/// </summary>
/// <param name="box">The <see cref="BoundingBox"/> to test for intersection.</param>
/// <returns>
/// The distance along the ray of the intersection or <code>null</code> if this
/// <see cref="Ray"/> does not intersect the <see cref="BoundingBox"/>.
/// </returns>
public float? Intersects(BoundingBox box)
{
float? result;
IntersectsHelper.BoundingBoxIntersectsRay(ref box, ref this, out result);
return result;
}
/// <summary>
/// Check if this <see cref="Ray"/> intersects the specified <see cref="BoundingBox"/>.
/// </summary>
/// <param name="box">The <see cref="BoundingBox"/> to test for intersection.</param>
/// <param name="result">
/// The distance along the ray of the intersection or <code>null</code> if this
/// <see cref="Ray"/> does not intersect the <see cref="BoundingBox"/>.
/// </param>
public void Intersects(ref BoundingBox box, out float? result)
{
IntersectsHelper.BoundingBoxIntersectsRay(ref box, ref this, out result);
}
public float? Intersects(BoundingFrustum frustum)
{
if (frustum == null)
throw new ArgumentNullException("frustum");
float? result;
IntersectsHelper.BoundingFrustumIntersectsRay(frustum, ref this, out result);
return result;
}
/// <summary>
/// Check if this <see cref="Ray"/> intersects the specified <see cref="BoundingSphere"/>.
/// </summary>
/// <param name="sphere">The <see cref="BoundingBox"/> to test for intersection.</param>
/// <returns>
/// The distance along the ray of the intersection or <code>null</code> if this
/// <see cref="Ray"/> does not intersect the <see cref="BoundingSphere"/>.
/// </returns>
public float? Intersects(BoundingSphere sphere)
{
float? result;
IntersectsHelper.BoundingSphereIntersectsRay(ref sphere, ref this, out result);
return result;
}
/// <summary>
/// Check if this <see cref="Ray"/> intersects the specified <see cref="Plane"/>.
/// </summary>
/// <param name="plane">The <see cref="Plane"/> to test for intersection.</param>
/// <returns>
/// The distance along the ray of the intersection or <code>null</code> if this
/// <see cref="Ray"/> does not intersect the <see cref="Plane"/>.
/// </returns>
public float? Intersects(Plane plane)
{
float? result;
IntersectsHelper.PlaneIntersectsRay(ref plane, ref this, out result);
return result;
}
/// <summary>
/// Check if this <see cref="Ray"/> intersects the specified <see cref="Plane"/>.
/// </summary>
/// <param name="plane">The <see cref="Plane"/> to test for intersection.</param>
/// <param name="result">
/// The distance along the ray of the intersection or <code>null</code> if this
/// <see cref="Ray"/> does not intersect the <see cref="Plane"/>.
/// </param>
public void Intersects(ref Plane plane, out float? result)
{
IntersectsHelper.PlaneIntersectsRay(ref plane, ref this, out result);
}
/// <summary>
/// Check if this <see cref="Ray"/> intersects the specified <see cref="BoundingSphere"/>.
/// </summary>
/// <param name="sphere">The <see cref="BoundingBox"/> to test for intersection.</param>
/// <param name="result">
/// The distance along the ray of the intersection or <code>null</code> if this
/// <see cref="Ray"/> does not intersect the <see cref="BoundingSphere"/>.
/// </param>
public void Intersects(ref BoundingSphere sphere, out float? result)
{
IntersectsHelper.BoundingSphereIntersectsRay(ref sphere, ref this, out result);
}
/// <summary>
/// Check if two rays are not equal.
/// </summary>
/// <param name="left">A ray to check for inequality.</param>
/// <param name="right">A ray to check for inequality.</param>
/// <returns><code>true</code> if the two rays are not equal, <code>false</code> if they are.</returns>
public static bool operator !=(Ray left, Ray right)
{
return !left.Equals(right);
}
/// <summary>
/// Check if two rays are equal.
/// </summary>
/// <param name="left">A ray to check for equality.</param>
/// <param name="right">A ray to check for equality.</param>
/// <returns><code>true</code> if the two rays are equals, <code>false</code> if they are not.</returns>
public static bool operator ==(Ray left, Ray right)
{
return left.Equals(right);
}
internal string DebugDisplayString
{
get
{
return string.Concat(
"Pos( ", this.Position.DebugDisplayString, " ) \r\n",
"Dir( ", this.Direction.DebugDisplayString, " )"
);
}
}
/// <summary>
/// Get a <see cref="String"/> representation of this <see cref="Ray"/>.
/// </summary>
/// <returns>A <see cref="String"/> representation of this <see cref="Ray"/>.</returns>
public override string ToString()
{
return "{{Position:" + Position.ToString() + " Direction:" + Direction.ToString() + "}}";
}
/// <summary>
/// Deconstruction method for <see cref="Ray"/>.
/// </summary>
/// <param name="position">Receives the start position of the ray.</param>
/// <param name="direction">Receives the direction of the ray.</param>
public void Deconstruct(out Vector3 position, out Vector3 direction)
{
position = Position;
direction = Direction;
}
#endregion
}
}