Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Ray.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under MIT License. See LICENSE.md for details.
3 //
4 // -----------------------------------------------------------------------------
5 // Original code from SlimMath project. http://code.google.com/p/slimmath/
6 // Greetings to SlimDX Group. Original code published with the following license:
7 // -----------------------------------------------------------------------------
8 /*
9 * Copyright (c) 2007-2011 SlimDX Group
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 */
29 using System;
30 using System.Globalization;
31 using System.Runtime.InteropServices;
32 using System.ComponentModel;
33 using SiliconStudio.Core.Serialization;
34 
35 namespace SiliconStudio.Core.Mathematics
36 {
37  /// <summary>
38  /// Represents a three dimensional line based on a point in space and a direction.
39  /// </summary>
40  [DataContract]
41  [StructLayout(LayoutKind.Sequential, Pack = 4)]
42  public struct Ray : IEquatable<Ray>, IFormattable
43  {
44  /// <summary>
45  /// The position in three dimensional space where the ray starts.
46  /// </summary>
47  public Vector3 Position;
48 
49  /// <summary>
50  /// The normalized direction in which the ray points.
51  /// </summary>
53 
54  /// <summary>
55  /// Initializes a new instance of the <see cref="SiliconStudio.Core.Mathematics.Ray"/> struct.
56  /// </summary>
57  /// <param name="position">The position in three dimensional space of the origin of the ray.</param>
58  /// <param name="direction">The normalized direction of the ray.</param>
59  public Ray(Vector3 position, Vector3 direction)
60  {
61  this.Position = position;
62  this.Direction = direction;
63  }
64 
65  /// <summary>
66  /// Determines if there is an intersection between the current object and a point.
67  /// </summary>
68  /// <param name="point">The point to test.</param>
69  /// <returns>Whether the two objects intersected.</returns>
70  public bool Intersects(ref Vector3 point)
71  {
72  return Collision.RayIntersectsPoint(ref this, ref point);
73  }
74 
75  /// <summary>
76  /// Determines if there is an intersection between the current object and a <see cref="SiliconStudio.Core.Mathematics.Ray"/>.
77  /// </summary>
78  /// <param name="ray">The ray to test.</param>
79  /// <returns>Whether the two objects intersected.</returns>
80  public bool Intersects(ref Ray ray)
81  {
82  Vector3 point;
83  return Collision.RayIntersectsRay(ref this, ref ray, out point);
84  }
85 
86  /// <summary>
87  /// Determines if there is an intersection between the current object and a <see cref="SiliconStudio.Core.Mathematics.Ray"/>.
88  /// </summary>
89  /// <param name="ray">The ray to test.</param>
90  /// <param name="point">When the method completes, contains the point of intersection,
91  /// or <see cref="SiliconStudio.Core.Mathematics.Vector3.Zero"/> if there was no intersection.</param>
92  /// <returns>Whether the two objects intersected.</returns>
93  public bool Intersects(ref Ray ray, out Vector3 point)
94  {
95  return Collision.RayIntersectsRay(ref this, ref ray, out point);
96  }
97 
98  /// <summary>
99  /// Determines if there is an intersection between the current object and a <see cref="SiliconStudio.Core.Mathematics.Plane"/>.
100  /// </summary>
101  /// <param name="plane">The plane to test</param>
102  /// <returns>Whether the two objects intersected.</returns>
103  public bool Intersects(ref Plane plane)
104  {
105  float distance;
106  return Collision.RayIntersectsPlane(ref this, ref plane, out distance);
107  }
108 
109  /// <summary>
110  /// Determines if there is an intersection between the current object and a <see cref="SiliconStudio.Core.Mathematics.Plane"/>.
111  /// </summary>
112  /// <param name="plane">The plane to test.</param>
113  /// <param name="distance">When the method completes, contains the distance of the intersection,
114  /// or 0 if there was no intersection.</param>
115  /// <returns>Whether the two objects intersected.</returns>
116  public bool Intersects(ref Plane plane, out float distance)
117  {
118  return Collision.RayIntersectsPlane(ref this, ref plane, out distance);
119  }
120 
121  /// <summary>
122  /// Determines if there is an intersection between the current object and a <see cref="SiliconStudio.Core.Mathematics.Plane"/>.
123  /// </summary>
124  /// <param name="plane">The plane to test.</param>
125  /// <param name="point">When the method completes, contains the point of intersection,
126  /// or <see cref="SiliconStudio.Core.Mathematics.Vector3.Zero"/> if there was no intersection.</param>
127  /// <returns>Whether the two objects intersected.</returns>
128  public bool Intersects(ref Plane plane, out Vector3 point)
129  {
130  return Collision.RayIntersectsPlane(ref this, ref plane, out point);
131  }
132 
133  /// <summary>
134  /// Determines if there is an intersection between the current object and a triangle.
135  /// </summary>
136  /// <param name="vertex1">The first vertex of the triangle to test.</param>
137  /// <param name="vertex2">The second vertex of the triangle to test.</param>
138  /// <param name="vertex3">The third vertex of the triangle to test.</param>
139  /// <returns>Whether the two objects intersected.</returns>
140  public bool Intersects(ref Vector3 vertex1, ref Vector3 vertex2, ref Vector3 vertex3)
141  {
142  float distance;
143  return Collision.RayIntersectsTriangle(ref this, ref vertex1, ref vertex2, ref vertex3, out distance);
144  }
145 
146  /// <summary>
147  /// Determines if there is an intersection between the current object and a triangle.
148  /// </summary>
149  /// <param name="vertex1">The first vertex of the triangle to test.</param>
150  /// <param name="vertex2">The second vertex of the triangle to test.</param>
151  /// <param name="vertex3">The third vertex of the triangle to test.</param>
152  /// <param name="distance">When the method completes, contains the distance of the intersection,
153  /// or 0 if there was no intersection.</param>
154  /// <returns>Whether the two objects intersected.</returns>
155  public bool Intersects(ref Vector3 vertex1, ref Vector3 vertex2, ref Vector3 vertex3, out float distance)
156  {
157  return Collision.RayIntersectsTriangle(ref this, ref vertex1, ref vertex2, ref vertex3, out distance);
158  }
159 
160  /// <summary>
161  /// Determines if there is an intersection between the current object and a triangle.
162  /// </summary>
163  /// <param name="vertex1">The first vertex of the triangle to test.</param>
164  /// <param name="vertex2">The second vertex of the triangle to test.</param>
165  /// <param name="vertex3">The third vertex of the triangle to test.</param>
166  /// <param name="point">When the method completes, contains the point of intersection,
167  /// or <see cref="SiliconStudio.Core.Mathematics.Vector3.Zero"/> if there was no intersection.</param>
168  /// <returns>Whether the two objects intersected.</returns>
169  public bool Intersects(ref Vector3 vertex1, ref Vector3 vertex2, ref Vector3 vertex3, out Vector3 point)
170  {
171  return Collision.RayIntersectsTriangle(ref this, ref vertex1, ref vertex2, ref vertex3, out point);
172  }
173 
174  /// <summary>
175  /// Determines if there is an intersection between the current object and a <see cref="SiliconStudio.Core.Mathematics.BoundingBox"/>.
176  /// </summary>
177  /// <param name="box">The box to test.</param>
178  /// <returns>Whether the two objects intersected.</returns>
179  public bool Intersects(ref BoundingBox box)
180  {
181  float distance;
182  return Collision.RayIntersectsBox(ref this, ref box, out distance);
183  }
184 
185  /// <summary>
186  /// Determines if there is an intersection between the current object and a <see cref="SiliconStudio.Core.Mathematics.BoundingBox"/>.
187  /// </summary>
188  /// <param name="box">The box to test.</param>
189  /// <param name="distance">When the method completes, contains the distance of the intersection,
190  /// or 0 if there was no intersection.</param>
191  /// <returns>Whether the two objects intersected.</returns>
192  public bool Intersects(ref BoundingBox box, out float distance)
193  {
194  return Collision.RayIntersectsBox(ref this, ref box, out distance);
195  }
196 
197  /// <summary>
198  /// Determines if there is an intersection between the current object and a <see cref="SiliconStudio.Core.Mathematics.BoundingBox"/>.
199  /// </summary>
200  /// <param name="box">The box to test.</param>
201  /// <param name="point">When the method completes, contains the point of intersection,
202  /// or <see cref="SiliconStudio.Core.Mathematics.Vector3.Zero"/> if there was no intersection.</param>
203  /// <returns>Whether the two objects intersected.</returns>
204  public bool Intersects(ref BoundingBox box, out Vector3 point)
205  {
206  return Collision.RayIntersectsBox(ref this, ref box, out point);
207  }
208 
209  /// <summary>
210  /// Determines if there is an intersection between the current object and a <see cref="SiliconStudio.Core.Mathematics.BoundingSphere"/>.
211  /// </summary>
212  /// <param name="sphere">The sphere to test.</param>
213  /// <returns>Whether the two objects intersected.</returns>
214  public bool Intersects(ref BoundingSphere sphere)
215  {
216  float distance;
217  return Collision.RayIntersectsSphere(ref this, ref sphere, out distance);
218  }
219 
220  /// <summary>
221  /// Determines if there is an intersection between the current object and a <see cref="SiliconStudio.Core.Mathematics.BoundingSphere"/>.
222  /// </summary>
223  /// <param name="sphere">The sphere to test.</param>
224  /// <param name="distance">When the method completes, contains the distance of the intersection,
225  /// or 0 if there was no intersection.</param>
226  /// <returns>Whether the two objects intersected.</returns>
227  public bool Intersects(ref BoundingSphere sphere, out float distance)
228  {
229  return Collision.RayIntersectsSphere(ref this, ref sphere, out distance);
230  }
231 
232  /// <summary>
233  /// Determines if there is an intersection between the current object and a <see cref="SiliconStudio.Core.Mathematics.BoundingSphere"/>.
234  /// </summary>
235  /// <param name="sphere">The sphere to test.</param>
236  /// <param name="point">When the method completes, contains the point of intersection,
237  /// or <see cref="SiliconStudio.Core.Mathematics.Vector3.Zero"/> if there was no intersection.</param>
238  /// <returns>Whether the two objects intersected.</returns>
239  public bool Intersects(ref BoundingSphere sphere, out Vector3 point)
240  {
241  return Collision.RayIntersectsSphere(ref this, ref sphere, out point);
242  }
243 
244  /// <summary>
245  /// Tests for equality between two objects.
246  /// </summary>
247  /// <param name="left">The first value to compare.</param>
248  /// <param name="right">The second value to compare.</param>
249  /// <returns><c>true</c> if <paramref name="left"/> has the same value as <paramref name="right"/>; otherwise, <c>false</c>.</returns>
250  public static bool operator ==(Ray left, Ray right)
251  {
252  return left.Equals(right);
253  }
254 
255  /// <summary>
256  /// Tests for inequality between two objects.
257  /// </summary>
258  /// <param name="left">The first value to compare.</param>
259  /// <param name="right">The second value to compare.</param>
260  /// <returns><c>true</c> if <paramref name="left"/> has a different value than <paramref name="right"/>; otherwise, <c>false</c>.</returns>
261  public static bool operator !=(Ray left, Ray right)
262  {
263  return !left.Equals(right);
264  }
265 
266  /// <summary>
267  /// Returns a <see cref="System.String"/> that represents this instance.
268  /// </summary>
269  /// <returns>
270  /// A <see cref="System.String"/> that represents this instance.
271  /// </returns>
272  public override string ToString()
273  {
274  return string.Format(CultureInfo.CurrentCulture, "Position:{0} Direction:{1}", Position.ToString(), Direction.ToString());
275  }
276 
277  /// <summary>
278  /// Returns a <see cref="System.String"/> that represents this instance.
279  /// </summary>
280  /// <param name="format">The format.</param>
281  /// <returns>
282  /// A <see cref="System.String"/> that represents this instance.
283  /// </returns>
284  public string ToString(string format)
285  {
286  return string.Format(CultureInfo.CurrentCulture, "Position:{0} Direction:{1}", Position.ToString(format, CultureInfo.CurrentCulture),
287  Direction.ToString(format, CultureInfo.CurrentCulture));
288  }
289 
290  /// <summary>
291  /// Returns a <see cref="System.String"/> that represents this instance.
292  /// </summary>
293  /// <param name="formatProvider">The format provider.</param>
294  /// <returns>
295  /// A <see cref="System.String"/> that represents this instance.
296  /// </returns>
297  public string ToString(IFormatProvider formatProvider)
298  {
299  return string.Format(formatProvider, "Position:{0} Direction:{1}", Position.ToString(), Direction.ToString());
300  }
301 
302  /// <summary>
303  /// Returns a <see cref="System.String"/> that represents this instance.
304  /// </summary>
305  /// <param name="format">The format.</param>
306  /// <param name="formatProvider">The format provider.</param>
307  /// <returns>
308  /// A <see cref="System.String"/> that represents this instance.
309  /// </returns>
310  public string ToString(string format, IFormatProvider formatProvider)
311  {
312  return string.Format(formatProvider, "Position:{0} Direction:{1}", Position.ToString(format, formatProvider),
313  Direction.ToString(format, formatProvider));
314  }
315 
316  /// <summary>
317  /// Returns a hash code for this instance.
318  /// </summary>
319  /// <returns>
320  /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
321  /// </returns>
322  public override int GetHashCode()
323  {
324  return Position.GetHashCode() + Direction.GetHashCode();
325  }
326 
327  /// <summary>
328  /// Determines whether the specified <see cref="SiliconStudio.Core.Mathematics.Vector4"/> is equal to this instance.
329  /// </summary>
330  /// <param name="value">The <see cref="SiliconStudio.Core.Mathematics.Vector4"/> to compare with this instance.</param>
331  /// <returns>
332  /// <c>true</c> if the specified <see cref="SiliconStudio.Core.Mathematics.Vector4"/> is equal to this instance; otherwise, <c>false</c>.
333  /// </returns>
334  public bool Equals(Ray value)
335  {
336  return Position == value.Position && Direction == value.Direction;
337  }
338 
339  /// <summary>
340  /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
341  /// </summary>
342  /// <param name="value">The <see cref="System.Object"/> to compare with this instance.</param>
343  /// <returns>
344  /// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
345  /// </returns>
346  public override bool Equals(object value)
347  {
348  if (value == null)
349  return false;
350 
351  if (value.GetType() != GetType())
352  return false;
353 
354  return Equals((Ray)value);
355  }
356 
357 #if SlimDX1xInterop
358  /// <summary>
359  /// Performs an implicit conversion from <see cref="SiliconStudio.Core.Mathematics.Ray"/> to <see cref="SlimDX.Ray"/>.
360  /// </summary>
361  /// <param name="value">The value.</param>
362  /// <returns>The result of the conversion.</returns>
363  public static implicit operator SlimDX.Ray(Ray value)
364  {
365  return new SlimDX.Ray(value.Position, value.Direction);
366  }
367 
368  /// <summary>
369  /// Performs an implicit conversion from <see cref="SlimDX.Ray"/> to <see cref="SiliconStudio.Core.Mathematics.Ray"/>.
370  /// </summary>
371  /// <param name="value">The value.</param>
372  /// <returns>The result of the conversion.</returns>
373  public static implicit operator Ray(SlimDX.Ray value)
374  {
375  return new Ray(value.Position, value.Direction);
376  }
377 #endif
378 
379 #if XnaInterop
380  /// <summary>
381  /// Performs an implicit conversion from <see cref="SiliconStudio.Core.Mathematics.Ray"/> to <see cref="Microsoft.Xna.Framework.Ray"/>.
382  /// </summary>
383  /// <param name="value">The value.</param>
384  /// <returns>The result of the conversion.</returns>
385  public static implicit operator Microsoft.Xna.Framework.Ray(Ray value)
386  {
387  return new Microsoft.Xna.Framework.Ray(value.Position, value.Direction);
388  }
389 
390  /// <summary>
391  /// Performs an implicit conversion from <see cref="Microsoft.Xna.Framework.Ray"/> to <see cref="SiliconStudio.Core.Mathematics.Ray"/>.
392  /// </summary>
393  /// <param name="value">The value.</param>
394  /// <returns>The result of the conversion.</returns>
395  public static implicit operator Ray(Microsoft.Xna.Framework.Ray value)
396  {
397  return new Ray(value.Position, value.Direction);
398  }
399 #endif
400  }
401 }
Represents an axis-aligned bounding box in three dimensional space.
Definition: BoundingBox.cs:42
bool Intersects(ref BoundingBox box)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.BoundingBox.
Definition: Ray.cs:179
override bool Equals(object value)
Determines whether the specified System.Object is equal to this instance.
Definition: Ray.cs:346
bool Intersects(ref BoundingBox box, out Vector3 point)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.BoundingBox.
Definition: Ray.cs:204
bool Intersects(ref Ray ray, out Vector3 point)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.Ray.
Definition: Ray.cs:93
bool Intersects(ref Vector3 vertex1, ref Vector3 vertex2, ref Vector3 vertex3)
Determines if there is an intersection between the current object and a triangle. ...
Definition: Ray.cs:140
string ToString(string format)
Returns a System.String that represents this instance.
Definition: Ray.cs:284
Ray(Vector3 position, Vector3 direction)
Initializes a new instance of the SiliconStudio.Core.Mathematics.Ray struct.
Definition: Ray.cs:59
bool Intersects(ref BoundingSphere sphere, out float distance)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.BoundingSphere.
Definition: Ray.cs:227
bool Intersects(ref Vector3 vertex1, ref Vector3 vertex2, ref Vector3 vertex3, out Vector3 point)
Determines if there is an intersection between the current object and a triangle. ...
Definition: Ray.cs:169
Represents a three dimensional mathematical vector.
Definition: Vector3.cs:42
string ToString(string format, IFormatProvider formatProvider)
Returns a System.String that represents this instance.
Definition: Ray.cs:310
bool Equals(Ray value)
Determines whether the specified SiliconStudio.Core.Mathematics.Vector4 is equal to this instance...
Definition: Ray.cs:334
Represents a bounding sphere in three dimensional space.
Represents a plane in three dimensional space.
Definition: Plane.cs:42
bool Intersects(ref BoundingBox box, out float distance)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.BoundingBox.
Definition: Ray.cs:192
Vector3 Direction
The normalized direction in which the ray points.
Definition: Ray.cs:52
Represents a three dimensional line based on a point in space and a direction.
Definition: Ray.cs:42
bool Intersects(ref Plane plane)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.Plane.
Definition: Ray.cs:103
bool Intersects(ref Vector3 point)
Determines if there is an intersection between the current object and a point.
Definition: Ray.cs:70
override int GetHashCode()
Returns a hash code for this instance.
Definition: Ray.cs:322
bool Intersects(ref Vector3 vertex1, ref Vector3 vertex2, ref Vector3 vertex3, out float distance)
Determines if there is an intersection between the current object and a triangle. ...
Definition: Ray.cs:155
string ToString(IFormatProvider formatProvider)
Returns a System.String that represents this instance.
Definition: Ray.cs:297
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
Definition: DirectXTexP.h:175
bool Intersects(ref Plane plane, out Vector3 point)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.Plane.
Definition: Ray.cs:128
bool Intersects(ref BoundingSphere sphere, out Vector3 point)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.BoundingSphere.
Definition: Ray.cs:239
bool Intersects(ref Ray ray)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.Ray.
Definition: Ray.cs:80
bool Intersects(ref Plane plane, out float distance)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.Plane.
Definition: Ray.cs:116
override string ToString()
Returns a System.String that represents this instance.
Definition: Ray.cs:272
Vector3 Position
The position in three dimensional space where the ray starts.
Definition: Ray.cs:47
bool Intersects(ref BoundingSphere sphere)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.BoundingSphere.
Definition: Ray.cs:214