-
-
Notifications
You must be signed in to change notification settings - Fork 966
/
Copy pathRay.cs
399 lines (366 loc) · 18.7 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// Copyright (c) .NET Foundation and Contributors (https://dotnetfoundation.org/ & https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
//
// -----------------------------------------------------------------------------
// Original code from SlimMath project. http://code.google.com/p/slimmath/
// Greetings to SlimDX Group. Original code published with the following license:
// -----------------------------------------------------------------------------
/*
* Copyright (c) 2007-2011 SlimDX Group
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.Globalization;
using System.Runtime.InteropServices;
namespace Stride.Core.Mathematics
{
/// <summary>
/// Represents a three dimensional line based on a point in space and a direction.
/// </summary>
[DataContract]
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct Ray : IEquatable<Ray>, IFormattable
{
/// <summary>
/// The position in three dimensional space where the ray starts.
/// </summary>
public Vector3 Position;
/// <summary>
/// The normalized direction in which the ray points.
/// </summary>
public Vector3 Direction;
/// <summary>
/// Initializes a new instance of the <see cref="Stride.Core.Mathematics.Ray"/> struct.
/// </summary>
/// <param name="position">The position in three dimensional space of the origin of the ray.</param>
/// <param name="direction">The normalized direction of the ray.</param>
public Ray(Vector3 position, Vector3 direction)
{
this.Position = position;
this.Direction = direction;
}
/// <summary>
/// Determines if there is an intersection between the current object and a point.
/// </summary>
/// <param name="point">The point to test.</param>
/// <returns>Whether the two objects intersected.</returns>
public bool Intersects(ref readonly Vector3 point)
{
return CollisionHelper.RayIntersectsPoint(ref this, in point);
}
/// <summary>
/// Determines if there is an intersection between the current object and a <see cref="Stride.Core.Mathematics.Ray"/>.
/// </summary>
/// <param name="ray">The ray to test.</param>
/// <returns>Whether the two objects intersected.</returns>
public bool Intersects(ref readonly Ray ray)
{
Vector3 point;
return CollisionHelper.RayIntersectsRay(ref this, in ray, out point);
}
/// <summary>
/// Determines if there is an intersection between the current object and a <see cref="Stride.Core.Mathematics.Ray"/>.
/// </summary>
/// <param name="ray">The ray to test.</param>
/// <param name="point">When the method completes, contains the point of intersection,
/// or <see cref="Stride.Core.Mathematics.Vector3.Zero"/> if there was no intersection.</param>
/// <returns>Whether the two objects intersected.</returns>
public bool Intersects(ref readonly Ray ray, out Vector3 point)
{
return CollisionHelper.RayIntersectsRay(ref this, in ray, out point);
}
/// <summary>
/// Determines if there is an intersection between the current object and a <see cref="Stride.Core.Mathematics.Plane"/>.
/// </summary>
/// <param name="plane">The plane to test</param>
/// <returns>Whether the two objects intersected.</returns>
public bool Intersects(ref readonly Plane plane)
{
float distance;
return CollisionHelper.RayIntersectsPlane(ref this, in plane, out distance);
}
/// <summary>
/// Determines if there is an intersection between the current object and a <see cref="Stride.Core.Mathematics.Plane"/>.
/// </summary>
/// <param name="plane">The plane to test.</param>
/// <param name="distance">When the method completes, contains the distance of the intersection,
/// or 0 if there was no intersection.</param>
/// <returns>Whether the two objects intersected.</returns>
public bool Intersects(ref readonly Plane plane, out float distance)
{
return CollisionHelper.RayIntersectsPlane(ref this, in plane, out distance);
}
/// <summary>
/// Determines if there is an intersection between the current object and a <see cref="Stride.Core.Mathematics.Plane"/>.
/// </summary>
/// <param name="plane">The plane to test.</param>
/// <param name="point">When the method completes, contains the point of intersection,
/// or <see cref="Stride.Core.Mathematics.Vector3.Zero"/> if there was no intersection.</param>
/// <returns>Whether the two objects intersected.</returns>
public bool Intersects(ref readonly Plane plane, out Vector3 point)
{
return CollisionHelper.RayIntersectsPlane(ref this, in plane, out point);
}
/// <summary>
/// Determines if there is an intersection between the current object and a triangle.
/// </summary>
/// <param name="vertex1">The first vertex of the triangle to test.</param>
/// <param name="vertex2">The second vertex of the triangle to test.</param>
/// <param name="vertex3">The third vertex of the triangle to test.</param>
/// <returns>Whether the two objects intersected.</returns>
public bool Intersects(ref readonly Vector3 vertex1, ref readonly Vector3 vertex2, ref readonly Vector3 vertex3)
{
float distance;
return CollisionHelper.RayIntersectsTriangle(ref this, in vertex1, in vertex2, in vertex3, out distance);
}
/// <summary>
/// Determines if there is an intersection between the current object and a triangle.
/// </summary>
/// <param name="vertex1">The first vertex of the triangle to test.</param>
/// <param name="vertex2">The second vertex of the triangle to test.</param>
/// <param name="vertex3">The third vertex of the triangle to test.</param>
/// <param name="distance">When the method completes, contains the distance of the intersection,
/// or 0 if there was no intersection.</param>
/// <returns>Whether the two objects intersected.</returns>
public bool Intersects(ref readonly Vector3 vertex1, ref readonly Vector3 vertex2, ref readonly Vector3 vertex3, out float distance)
{
return CollisionHelper.RayIntersectsTriangle(ref this, in vertex1, in vertex2, in vertex3, out distance);
}
/// <summary>
/// Determines if there is an intersection between the current object and a triangle.
/// </summary>
/// <param name="vertex1">The first vertex of the triangle to test.</param>
/// <param name="vertex2">The second vertex of the triangle to test.</param>
/// <param name="vertex3">The third vertex of the triangle to test.</param>
/// <param name="point">When the method completes, contains the point of intersection,
/// or <see cref="Stride.Core.Mathematics.Vector3.Zero"/> if there was no intersection.</param>
/// <returns>Whether the two objects intersected.</returns>
public bool Intersects(ref readonly Vector3 vertex1, ref readonly Vector3 vertex2, ref readonly Vector3 vertex3, out Vector3 point)
{
return CollisionHelper.RayIntersectsTriangle(ref this, in vertex1, in vertex2, in vertex3, out point);
}
/// <summary>
/// Determines if there is an intersection between the current object and a <see cref="Stride.Core.Mathematics.BoundingBox"/>.
/// </summary>
/// <param name="box">The box to test.</param>
/// <returns>Whether the two objects intersected.</returns>
public bool Intersects(ref readonly BoundingBox box)
{
float distance;
return CollisionHelper.RayIntersectsBox(ref this, in box, out distance);
}
/// <summary>
/// Determines if there is an intersection between the current object and a <see cref="Stride.Core.Mathematics.BoundingBox"/>.
/// </summary>
/// <param name="box">The box to test.</param>
/// <param name="distance">When the method completes, contains the distance of the intersection,
/// or 0 if there was no intersection.</param>
/// <returns>Whether the two objects intersected.</returns>
public bool Intersects(ref readonly BoundingBox box, out float distance)
{
return CollisionHelper.RayIntersectsBox(ref this, in box, out distance);
}
/// <summary>
/// Determines if there is an intersection between the current object and a <see cref="Stride.Core.Mathematics.BoundingBox"/>.
/// </summary>
/// <param name="box">The box to test.</param>
/// <param name="point">When the method completes, contains the point of intersection,
/// or <see cref="Stride.Core.Mathematics.Vector3.Zero"/> if there was no intersection.</param>
/// <returns>Whether the two objects intersected.</returns>
public bool Intersects(ref readonly BoundingBox box, out Vector3 point)
{
return CollisionHelper.RayIntersectsBox(ref this, in box, out point);
}
/// <summary>
/// Determines if there is an intersection between the current object and a <see cref="Stride.Core.Mathematics.BoundingSphere"/>.
/// </summary>
/// <param name="sphere">The sphere to test.</param>
/// <returns>Whether the two objects intersected.</returns>
public bool Intersects(ref readonly BoundingSphere sphere)
{
float distance;
return CollisionHelper.RayIntersectsSphere(ref this, in sphere, out distance);
}
/// <summary>
/// Determines if there is an intersection between the current object and a <see cref="Stride.Core.Mathematics.BoundingSphere"/>.
/// </summary>
/// <param name="sphere">The sphere to test.</param>
/// <param name="distance">When the method completes, contains the distance of the intersection,
/// or 0 if there was no intersection.</param>
/// <returns>Whether the two objects intersected.</returns>
public bool Intersects(ref readonly BoundingSphere sphere, out float distance)
{
return CollisionHelper.RayIntersectsSphere(ref this, in sphere, out distance);
}
/// <summary>
/// Determines if there is an intersection between the current object and a <see cref="Stride.Core.Mathematics.BoundingSphere"/>.
/// </summary>
/// <param name="sphere">The sphere to test.</param>
/// <param name="point">When the method completes, contains the point of intersection,
/// or <see cref="Stride.Core.Mathematics.Vector3.Zero"/> if there was no intersection.</param>
/// <returns>Whether the two objects intersected.</returns>
public bool Intersects(ref readonly BoundingSphere sphere, out Vector3 point)
{
return CollisionHelper.RayIntersectsSphere(ref this, in sphere, out point);
}
/// <summary>
/// Tests for equality between two objects.
/// </summary>
/// <param name="left">The first value to compare.</param>
/// <param name="right">The second value to compare.</param>
/// <returns><c>true</c> if <paramref name="left"/> has the same value as <paramref name="right"/>; otherwise, <c>false</c>.</returns>
public static bool operator ==(Ray left, Ray right)
{
return left.Equals(right);
}
/// <summary>
/// Tests for inequality between two objects.
/// </summary>
/// <param name="left">The first value to compare.</param>
/// <param name="right">The second value to compare.</param>
/// <returns><c>true</c> if <paramref name="left"/> has a different value than <paramref name="right"/>; otherwise, <c>false</c>.</returns>
public static bool operator !=(Ray left, Ray right)
{
return !left.Equals(right);
}
/// <summary>
/// Returns a <see cref="string"/> that represents this instance.
/// </summary>
/// <returns>
/// A <see cref="string"/> that represents this instance.
/// </returns>
public override string ToString()
{
return string.Format(CultureInfo.CurrentCulture, "Position:{0} Direction:{1}", Position.ToString(), Direction.ToString());
}
/// <summary>
/// Returns a <see cref="string"/> that represents this instance.
/// </summary>
/// <param name="format">The format.</param>
/// <returns>
/// A <see cref="string"/> that represents this instance.
/// </returns>
public string ToString(string format)
{
return string.Format(CultureInfo.CurrentCulture, "Position:{0} Direction:{1}", Position.ToString(format, CultureInfo.CurrentCulture),
Direction.ToString(format, CultureInfo.CurrentCulture));
}
/// <summary>
/// Returns a <see cref="string"/> that represents this instance.
/// </summary>
/// <param name="formatProvider">The format provider.</param>
/// <returns>
/// A <see cref="string"/> that represents this instance.
/// </returns>
public string ToString(IFormatProvider formatProvider)
{
return string.Format(formatProvider, "Position:{0} Direction:{1}", Position.ToString(), Direction.ToString());
}
/// <summary>
/// Returns a <see cref="string"/> that represents this instance.
/// </summary>
/// <param name="format">The format.</param>
/// <param name="formatProvider">The format provider.</param>
/// <returns>
/// A <see cref="string"/> that represents this instance.
/// </returns>
public string ToString(string format, IFormatProvider formatProvider)
{
return string.Format(formatProvider, "Position:{0} Direction:{1}", Position.ToString(format, formatProvider),
Direction.ToString(format, formatProvider));
}
/// <summary>
/// Returns a hash code for this instance.
/// </summary>
/// <returns>
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
/// </returns>
public override int GetHashCode()
{
return Position.GetHashCode() + Direction.GetHashCode();
}
/// <summary>
/// Determines whether the specified <see cref="Stride.Core.Mathematics.Vector4"/> is equal to this instance.
/// </summary>
/// <param name="value">The <see cref="Stride.Core.Mathematics.Vector4"/> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="Stride.Core.Mathematics.Vector4"/> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public bool Equals(Ray value)
{
return Position == value.Position && Direction == value.Direction;
}
/// <summary>
/// Determines whether the specified <see cref="object"/> is equal to this instance.
/// </summary>
/// <param name="value">The <see cref="object"/> to compare with this instance.</param>
/// <returns>
/// <c>true</c> if the specified <see cref="object"/> is equal to this instance; otherwise, <c>false</c>.
/// </returns>
public override bool Equals(object value)
{
if (value == null)
return false;
if (value.GetType() != GetType())
return false;
return Equals((Ray)value);
}
#if SlimDX1xInterop
/// <summary>
/// Performs an implicit conversion from <see cref="Stride.Core.Mathematics.Ray"/> to <see cref="SlimDX.Ray"/>.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator SlimDX.Ray(Ray value)
{
return new SlimDX.Ray(value.Position, value.Direction);
}
/// <summary>
/// Performs an implicit conversion from <see cref="SlimDX.Ray"/> to <see cref="Stride.Core.Mathematics.Ray"/>.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator Ray(SlimDX.Ray value)
{
return new Ray(value.Position, value.Direction);
}
#endif
#if XnaInterop
/// <summary>
/// Performs an implicit conversion from <see cref="Stride.Core.Mathematics.Ray"/> to <see cref="Microsoft.Xna.Framework.Ray"/>.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator Microsoft.Xna.Framework.Ray(Ray value)
{
return new Microsoft.Xna.Framework.Ray(value.Position, value.Direction);
}
/// <summary>
/// Performs an implicit conversion from <see cref="Microsoft.Xna.Framework.Ray"/> to <see cref="Stride.Core.Mathematics.Ray"/>.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>The result of the conversion.</returns>
public static implicit operator Ray(Microsoft.Xna.Framework.Ray value)
{
return new Ray(value.Position, value.Direction);
}
#endif
}
}