Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Plane.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 plane in three dimensional space.
39  /// </summary>
40  [DataContract]
41  [StructLayout(LayoutKind.Sequential, Pack = 4)]
42  public struct Plane : IEquatable<Plane>, IFormattable
43  {
44  /// <summary>
45  /// The normal vector of the plane.
46  /// </summary>
47  public Vector3 Normal;
48 
49  /// <summary>
50  /// The distance of the plane along its normal from the origin.
51  /// </summary>
52  public float D;
53 
54  /// <summary>
55  /// Initializes a new instance of the <see cref="SiliconStudio.Core.Mathematics.Plane"/> struct.
56  /// </summary>
57  /// <param name="value">The value that will be assigned to all components.</param>
58  public Plane(float value)
59  {
60  Normal.X = Normal.Y = Normal.Z = D = value;
61  }
62 
63  /// <summary>
64  /// Initializes a new instance of the <see cref="SiliconStudio.Core.Mathematics.Plane"/> struct.
65  /// </summary>
66  /// <param name="a">The X component of the normal.</param>
67  /// <param name="b">The Y component of the normal.</param>
68  /// <param name="c">The Z component of the normal.</param>
69  /// <param name="d">The distance of the plane along its normal from the origin.</param>
70  public Plane(float a, float b, float c, float d)
71  {
72  Normal.X = a;
73  Normal.Y = b;
74  Normal.Z = c;
75  D = d;
76  }
77 
78  /// <summary>
79  /// Initializes a new instance of the <see cref="T:SiliconStudio.Core.Mathematics.Plane" /> class.
80  /// </summary>
81  /// <param name="point">Any point that lies along the plane.</param>
82  /// <param name="normal">The normal vector to the plane.</param>
83  public Plane(Vector3 point, Vector3 normal)
84  {
85  this.Normal = normal;
86  this.D = -Vector3.Dot(normal, point);
87  }
88 
89  /// <summary>
90  /// Initializes a new instance of the <see cref="SiliconStudio.Core.Mathematics.Plane"/> struct.
91  /// </summary>
92  /// <param name="value">The normal of the plane.</param>
93  /// <param name="d">The distance of the plane along its normal from the origin</param>
94  public Plane(Vector3 value, float d)
95  {
96  Normal = value;
97  D = d;
98  }
99 
100  /// <summary>
101  /// Initializes a new instance of the <see cref="SiliconStudio.Core.Mathematics.Plane"/> struct.
102  /// </summary>
103  /// <param name="point1">First point of a triangle defining the plane.</param>
104  /// <param name="point2">Second point of a triangle defining the plane.</param>
105  /// <param name="point3">Third point of a triangle defining the plane.</param>
106  public Plane(Vector3 point1, Vector3 point2, Vector3 point3)
107  {
108  float x1 = point2.X - point1.X;
109  float y1 = point2.Y - point1.Y;
110  float z1 = point2.Z - point1.Z;
111  float x2 = point3.X - point1.X;
112  float y2 = point3.Y - point1.Y;
113  float z2 = point3.Z - point1.Z;
114  float yz = (y1 * z2) - (z1 * y2);
115  float xz = (z1 * x2) - (x1 * z2);
116  float xy = (x1 * y2) - (y1 * x2);
117  float invPyth = 1.0f / (float)(Math.Sqrt((yz * yz) + (xz * xz) + (xy * xy)));
118 
119  Normal.X = yz * invPyth;
120  Normal.Y = xz * invPyth;
121  Normal.Z = xy * invPyth;
122  D = -((Normal.X * point1.X) + (Normal.Y * point1.Y) + (Normal.Z * point1.Z));
123  }
124 
125  /// <summary>
126  /// Initializes a new instance of the <see cref="SiliconStudio.Core.Mathematics.Plane"/> struct.
127  /// </summary>
128  /// <param name="values">The values to assign to the A, B, C, and D components of the plane. This must be an array with four elements.</param>
129  /// <exception cref="ArgumentNullException">Thrown when <paramref name="values"/> is <c>null</c>.</exception>
130  /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="values"/> contains more or less than four elements.</exception>
131  public Plane(float[] values)
132  {
133  if (values == null)
134  throw new ArgumentNullException("values");
135  if (values.Length != 4)
136  throw new ArgumentOutOfRangeException("values", "There must be four and only four input values for Plane.");
137 
138  Normal.X = values[0];
139  Normal.Y = values[1];
140  Normal.Z = values[2];
141  D = values[3];
142  }
143 
144  /// <summary>
145  /// Gets or sets the component at the specified index.
146  /// </summary>
147  /// <value>The value of the A, B, C, or D component, depending on the index.</value>
148  /// <param name="index">The index of the component to access. Use 0 for the A component, 1 for the B component, 2 for the C component, and 3 for the D component.</param>
149  /// <returns>The value of the component at the specified index.</returns>
150  /// <exception cref="System.ArgumentOutOfRangeException">Thrown when the <paramref name="index"/> is out of the range [0, 3].</exception>
151  public float this[int index]
152  {
153  get
154  {
155  switch (index)
156  {
157  case 0: return Normal.X;
158  case 1: return Normal.Y;
159  case 2: return Normal.Z;
160  case 3: return D;
161  }
162 
163  throw new ArgumentOutOfRangeException("index", "Indices for Plane run from 0 to 3, inclusive.");
164  }
165 
166  set
167  {
168  switch (index)
169  {
170  case 0: Normal.X = value; break;
171  case 1: Normal.Y = value; break;
172  case 2: Normal.Z = value; break;
173  case 3: D = value; break;
174  default: throw new ArgumentOutOfRangeException("index", "Indices for Plane run from 0 to 3, inclusive.");
175  }
176  }
177  }
178 
179  /// <summary>
180  /// Changes the coefficients of the normal vector of the plane to make it of unit length.
181  /// </summary>
182  public void Normalize()
183  {
184  float magnitude = 1.0f / (float)(Math.Sqrt((Normal.X * Normal.X) + (Normal.Y * Normal.Y) + (Normal.Z * Normal.Z)));
185 
186  Normal.X *= magnitude;
187  Normal.Y *= magnitude;
188  Normal.Z *= magnitude;
189  D *= magnitude;
190  }
191 
192  /// <summary>
193  /// Creates an array containing the elements of the plane.
194  /// </summary>
195  /// <returns>A four-element array containing the components of the plane.</returns>
196  public float[] ToArray()
197  {
198  return new float[] { Normal.X, Normal.Y, Normal.Z, D };
199  }
200 
201  /// <summary>
202  /// Determines if there is an intersection between the current object and a point.
203  /// </summary>
204  /// <param name="point">The point to test.</param>
205  /// <returns>Whether the two objects intersected.</returns>
207  {
208  return Collision.PlaneIntersectsPoint(ref this, ref point);
209  }
210 
211  /// <summary>
212  /// Determines if there is an intersection between the current object and a <see cref="SiliconStudio.Core.Mathematics.Ray"/>.
213  /// </summary>
214  /// <param name="ray">The ray to test.</param>
215  /// <returns>Whether the two objects intersected.</returns>
216  public bool Intersects(ref Ray ray)
217  {
218  float distance;
219  return Collision.RayIntersectsPlane(ref ray, ref this, out distance);
220  }
221 
222  /// <summary>
223  /// Determines if there is an intersection between the current object and a <see cref="SiliconStudio.Core.Mathematics.Ray"/>.
224  /// </summary>
225  /// <param name="ray">The ray to test.</param>
226  /// <param name="distance">When the method completes, contains the distance of the intersection,
227  /// or 0 if there was no intersection.</param>
228  /// <returns>Whether the two objects intersected.</returns>
229  public bool Intersects(ref Ray ray, out float distance)
230  {
231  return Collision.RayIntersectsPlane(ref ray, ref this, out distance);
232  }
233 
234  /// <summary>
235  /// Determines if there is an intersection between the current object and a <see cref="SiliconStudio.Core.Mathematics.Ray"/>.
236  /// </summary>
237  /// <param name="ray">The ray to test.</param>
238  /// <param name="point">When the method completes, contains the point of intersection,
239  /// or <see cref="SiliconStudio.Core.Mathematics.Vector3.Zero"/> if there was no intersection.</param>
240  /// <returns>Whether the two objects intersected.</returns>
241  public bool Intersects(ref Ray ray, out Vector3 point)
242  {
243  return Collision.RayIntersectsPlane(ref ray, ref this, out point);
244  }
245 
246  /// <summary>
247  /// Determines if there is an intersection between the current object and a <see cref="SiliconStudio.Core.Mathematics.Plane"/>.
248  /// </summary>
249  /// <param name="plane">The plane to test.</param>
250  /// <returns>Whether the two objects intersected.</returns>
251  public bool Intersects(ref Plane plane)
252  {
253  return Collision.PlaneIntersectsPlane(ref this, ref plane);
254  }
255 
256  /// <summary>
257  /// Determines if there is an intersection between the current object and a <see cref="SiliconStudio.Core.Mathematics.Plane"/>.
258  /// </summary>
259  /// <param name="plane">The plane to test.</param>
260  /// <param name="line">When the method completes, contains the line of intersection
261  /// as a <see cref="SiliconStudio.Core.Mathematics.Ray"/>, or a zero ray if there was no intersection.</param>
262  /// <returns>Whether the two objects intersected.</returns>
263  public bool Intersects(ref Plane plane, out Ray line)
264  {
265  return Collision.PlaneIntersectsPlane(ref this, ref plane, out line);
266  }
267 
268  /// <summary>
269  /// Determines if there is an intersection between the current object and a triangle.
270  /// </summary>
271  /// <param name="vertex1">The first vertex of the triangle to test.</param>
272  /// <param name="vertex2">The second vertex of the triagnle to test.</param>
273  /// <param name="vertex3">The third vertex of the triangle to test.</param>
274  /// <returns>Whether the two objects intersected.</returns>
275  public PlaneIntersectionType Intersects(ref Vector3 vertex1, ref Vector3 vertex2, ref Vector3 vertex3)
276  {
277  return Collision.PlaneIntersectsTriangle(ref this, ref vertex1, ref vertex2, ref vertex3);
278  }
279 
280  /// <summary>
281  /// Determines if there is an intersection between the current object and a <see cref="SiliconStudio.Core.Mathematics.BoundingBox"/>.
282  /// </summary>
283  /// <param name="box">The box to test.</param>
284  /// <returns>Whether the two objects intersected.</returns>
286  {
287  return Collision.PlaneIntersectsBox(ref this, ref box);
288  }
289 
290  /// <summary>
291  /// Determines if there is an intersection between the current object and a <see cref="SiliconStudio.Core.Mathematics.BoundingSphere"/>.
292  /// </summary>
293  /// <param name="sphere">The sphere to test.</param>
294  /// <returns>Whether the two objects intersected.</returns>
296  {
297  return Collision.PlaneIntersectsSphere(ref this, ref sphere);
298  }
299 
300  /// <summary>
301  /// Scales the plane by the given scaling factor.
302  /// </summary>
303  /// <param name="value">The plane to scale.</param>
304  /// <param name="scale">The amount by which to scale the plane.</param>
305  /// <param name="result">When the method completes, contains the scaled plane.</param>
306  public static void Multiply(ref Plane value, float scale, out Plane result)
307  {
308  result.Normal.X = value.Normal.X * scale;
309  result.Normal.Y = value.Normal.Y * scale;
310  result.Normal.Z = value.Normal.Z * scale;
311  result.D = value.D * scale;
312  }
313 
314  /// <summary>
315  /// Scales the plane by the given scaling factor.
316  /// </summary>
317  /// <param name="value">The plane to scale.</param>
318  /// <param name="scale">The amount by which to scale the plane.</param>
319  /// <returns>The scaled plane.</returns>
320  public static Plane Multiply(Plane value, float scale)
321  {
322  return new Plane(value.Normal.X * scale, value.Normal.Y * scale, value.Normal.Z * scale, value.D * scale);
323  }
324 
325  /// <summary>
326  /// Calculates the dot product of the specified vector and plane.
327  /// </summary>
328  /// <param name="left">The source plane.</param>
329  /// <param name="right">The source vector.</param>
330  /// <param name="result">When the method completes, contains the dot product of the specified plane and vector.</param>
331  public static void Dot(ref Plane left, ref Vector4 right, out float result)
332  {
333  result = (left.Normal.X * right.X) + (left.Normal.Y * right.Y) + (left.Normal.Z * right.Z) + (left.D * right.W);
334  }
335 
336  /// <summary>
337  /// Calculates the dot product of the specified vector and plane.
338  /// </summary>
339  /// <param name="left">The source plane.</param>
340  /// <param name="right">The source vector.</param>
341  /// <returns>The dot product of the specified plane and vector.</returns>
342  public static float Dot(Plane left, Vector4 right)
343  {
344  return (left.Normal.X * right.X) + (left.Normal.Y * right.Y) + (left.Normal.Z * right.Z) + (left.D * right.W);
345  }
346 
347  /// <summary>
348  /// Calculates the dot product of a specified vector and the normal of the plane plus the distance value of the plane.
349  /// </summary>
350  /// <param name="left">The source plane.</param>
351  /// <param name="right">The source vector.</param>
352  /// <param name="result">When the method completes, contains the dot product of a specified vector and the normal of the Plane plus the distance value of the plane.</param>
353  public static void DotCoordinate(ref Plane left, ref Vector3 right, out float result)
354  {
355  result = (left.Normal.X * right.X) + (left.Normal.Y * right.Y) + (left.Normal.Z * right.Z) + left.D;
356  }
357 
358  /// <summary>
359  /// Calculates the dot product of a specified vector and the normal of the plane plus the distance value of the plane.
360  /// </summary>
361  /// <param name="left">The source plane.</param>
362  /// <param name="right">The source vector.</param>
363  /// <returns>The dot product of a specified vector and the normal of the Plane plus the distance value of the plane.</returns>
364  public static float DotCoordinate(Plane left, Vector3 right)
365  {
366  return (left.Normal.X * right.X) + (left.Normal.Y * right.Y) + (left.Normal.Z * right.Z) + left.D;
367  }
368 
369  /// <summary>
370  /// Calculates the dot product of the specified vector and the normal of the plane.
371  /// </summary>
372  /// <param name="left">The source plane.</param>
373  /// <param name="right">The source vector.</param>
374  /// <param name="result">When the method completes, contains the dot product of the specified vector and the normal of the plane.</param>
375  public static void DotNormal(ref Plane left, ref Vector3 right, out float result)
376  {
377  result = (left.Normal.X * right.X) + (left.Normal.Y * right.Y) + (left.Normal.Z * right.Z);
378  }
379 
380  /// <summary>
381  /// Calculates the dot product of the specified vector and the normal of the plane.
382  /// </summary>
383  /// <param name="left">The source plane.</param>
384  /// <param name="right">The source vector.</param>
385  /// <returns>The dot product of the specified vector and the normal of the plane.</returns>
386  public static float DotNormal(Plane left, Vector3 right)
387  {
388  return (left.Normal.X * right.X) + (left.Normal.Y * right.Y) + (left.Normal.Z * right.Z);
389  }
390 
391  /// <summary>
392  /// Changes the coefficients of the normal vector of the plane to make it of unit length.
393  /// </summary>
394  /// <param name="plane">The source plane.</param>
395  /// <param name="result">When the method completes, contains the normalized plane.</param>
396  public static void Normalize(ref Plane plane, out Plane result)
397  {
398  float magnitude = 1.0f / (float)(Math.Sqrt((plane.Normal.X * plane.Normal.X) + (plane.Normal.Y * plane.Normal.Y) + (plane.Normal.Z * plane.Normal.Z)));
399 
400  result.Normal.X = plane.Normal.X * magnitude;
401  result.Normal.Y = plane.Normal.Y * magnitude;
402  result.Normal.Z = plane.Normal.Z * magnitude;
403  result.D = plane.D * magnitude;
404  }
405 
406  /// <summary>
407  /// Changes the coefficients of the normal vector of the plane to make it of unit length.
408  /// </summary>
409  /// <param name="plane">The source plane.</param>
410  /// <returns>The normalized plane.</returns>
411  public static Plane Normalize(Plane plane)
412  {
413  float magnitude = 1.0f / (float)(Math.Sqrt((plane.Normal.X * plane.Normal.X) + (plane.Normal.Y * plane.Normal.Y) + (plane.Normal.Z * plane.Normal.Z)));
414  return new Plane(plane.Normal.X * magnitude, plane.Normal.Y * magnitude, plane.Normal.Z * magnitude, plane.D * magnitude);
415  }
416 
417  /// <summary>
418  /// Transforms a normalized plane by a quaternion rotation.
419  /// </summary>
420  /// <param name="plane">The normalized source plane.</param>
421  /// <param name="rotation">The quaternion rotation.</param>
422  /// <param name="result">When the method completes, contains the transformed plane.</param>
423  public static void Transform(ref Plane plane, ref Quaternion rotation, out Plane result)
424  {
425  float x2 = rotation.X + rotation.X;
426  float y2 = rotation.Y + rotation.Y;
427  float z2 = rotation.Z + rotation.Z;
428  float wx = rotation.W * x2;
429  float wy = rotation.W * y2;
430  float wz = rotation.W * z2;
431  float xx = rotation.X * x2;
432  float xy = rotation.X * y2;
433  float xz = rotation.X * z2;
434  float yy = rotation.Y * y2;
435  float yz = rotation.Y * z2;
436  float zz = rotation.Z * z2;
437 
438  float x = plane.Normal.X;
439  float y = plane.Normal.Y;
440  float z = plane.Normal.Z;
441 
442  result.Normal.X = ((x * ((1.0f - yy) - zz)) + (y * (xy - wz))) + (z * (xz + wy));
443  result.Normal.Y = ((x * (xy + wz)) + (y * ((1.0f - xx) - zz))) + (z * (yz - wx));
444  result.Normal.Z = ((x * (xz - wy)) + (y * (yz + wx))) + (z * ((1.0f - xx) - yy));
445  result.D = plane.D;
446  }
447 
448  /// <summary>
449  /// Transforms a normalized plane by a quaternion rotation.
450  /// </summary>
451  /// <param name="plane">The normalized source plane.</param>
452  /// <param name="rotation">The quaternion rotation.</param>
453  /// <returns>The transformed plane.</returns>
454  public static Plane Transform(Plane plane, Quaternion rotation)
455  {
456  Plane result;
457  float x2 = rotation.X + rotation.X;
458  float y2 = rotation.Y + rotation.Y;
459  float z2 = rotation.Z + rotation.Z;
460  float wx = rotation.W * x2;
461  float wy = rotation.W * y2;
462  float wz = rotation.W * z2;
463  float xx = rotation.X * x2;
464  float xy = rotation.X * y2;
465  float xz = rotation.X * z2;
466  float yy = rotation.Y * y2;
467  float yz = rotation.Y * z2;
468  float zz = rotation.Z * z2;
469 
470  float x = plane.Normal.X;
471  float y = plane.Normal.Y;
472  float z = plane.Normal.Z;
473 
474  result.Normal.X = ((x * ((1.0f - yy) - zz)) + (y * (xy - wz))) + (z * (xz + wy));
475  result.Normal.Y = ((x * (xy + wz)) + (y * ((1.0f - xx) - zz))) + (z * (yz - wx));
476  result.Normal.Z = ((x * (xz - wy)) + (y * (yz + wx))) + (z * ((1.0f - xx) - yy));
477  result.D = plane.D;
478 
479  return result;
480  }
481 
482  /// <summary>
483  /// Transforms an array of normalized planes by a quaternion rotation.
484  /// </summary>
485  /// <param name="planes">The array of normalized planes to transform.</param>
486  /// <param name="rotation">The quaternion rotation.</param>
487  /// <exception cref="ArgumentNullException">Thrown when <paramref name="planes"/> is <c>null</c>.</exception>
488  public static void Transform(Plane[] planes, ref Quaternion rotation)
489  {
490  if (planes == null)
491  throw new ArgumentNullException("planes");
492 
493  float x2 = rotation.X + rotation.X;
494  float y2 = rotation.Y + rotation.Y;
495  float z2 = rotation.Z + rotation.Z;
496  float wx = rotation.W * x2;
497  float wy = rotation.W * y2;
498  float wz = rotation.W * z2;
499  float xx = rotation.X * x2;
500  float xy = rotation.X * y2;
501  float xz = rotation.X * z2;
502  float yy = rotation.Y * y2;
503  float yz = rotation.Y * z2;
504  float zz = rotation.Z * z2;
505 
506  for (int i = 0; i < planes.Length; ++i)
507  {
508  float x = planes[i].Normal.X;
509  float y = planes[i].Normal.Y;
510  float z = planes[i].Normal.Z;
511 
512  /*
513  * Note:
514  * Factor common arithmetic out of loop.
515  */
516  planes[i].Normal.X = ((x * ((1.0f - yy) - zz)) + (y * (xy - wz))) + (z * (xz + wy));
517  planes[i].Normal.Y = ((x * (xy + wz)) + (y * ((1.0f - xx) - zz))) + (z * (yz - wx));
518  planes[i].Normal.Z = ((x * (xz - wy)) + (y * (yz + wx))) + (z * ((1.0f - xx) - yy));
519  }
520  }
521 
522  /// <summary>
523  /// Transforms a normalized plane by a matrix.
524  /// </summary>
525  /// <param name="plane">The normalized source plane.</param>
526  /// <param name="transformation">The transformation matrix.</param>
527  /// <param name="result">When the method completes, contains the transformed plane.</param>
528  public static void Transform(ref Plane plane, ref Matrix transformation, out Plane result)
529  {
530  float x = plane.Normal.X;
531  float y = plane.Normal.Y;
532  float z = plane.Normal.Z;
533  float d = plane.D;
534 
535  Matrix inverse;
536  Matrix.Invert(ref transformation, out inverse);
537 
538  result.Normal.X = (((x * inverse.M11) + (y * inverse.M12)) + (z * inverse.M13)) + (d * inverse.M14);
539  result.Normal.Y = (((x * inverse.M21) + (y * inverse.M22)) + (z * inverse.M23)) + (d * inverse.M24);
540  result.Normal.Z = (((x * inverse.M31) + (y * inverse.M32)) + (z * inverse.M33)) + (d * inverse.M34);
541  result.D = (((x * inverse.M41) + (y * inverse.M42)) + (z * inverse.M43)) + (d * inverse.M44);
542  }
543 
544  /// <summary>
545  /// Transforms a normalized plane by a matrix.
546  /// </summary>
547  /// <param name="plane">The normalized source plane.</param>
548  /// <param name="transformation">The transformation matrix.</param>
549  /// <returns>When the method completes, contains the transformed plane.</returns>
550  public static Plane Transform(Plane plane, Matrix transformation)
551  {
552  Plane result;
553  float x = plane.Normal.X;
554  float y = plane.Normal.Y;
555  float z = plane.Normal.Z;
556  float d = plane.D;
557 
558  transformation.Invert();
559  result.Normal.X = (((x * transformation.M11) + (y * transformation.M12)) + (z * transformation.M13)) + (d * transformation.M14);
560  result.Normal.Y = (((x * transformation.M21) + (y * transformation.M22)) + (z * transformation.M23)) + (d * transformation.M24);
561  result.Normal.Z = (((x * transformation.M31) + (y * transformation.M32)) + (z * transformation.M33)) + (d * transformation.M34);
562  result.D = (((x * transformation.M41) + (y * transformation.M42)) + (z * transformation.M43)) + (d * transformation.M44);
563 
564  return result;
565  }
566 
567  /// <summary>
568  /// Transforms an array of normalized planes by a matrix.
569  /// </summary>
570  /// <param name="planes">The array of normalized planes to transform.</param>
571  /// <param name="transformation">The transformation matrix.</param>
572  /// <exception cref="ArgumentNullException">Thrown when <paramref name="planes"/> is <c>null</c>.</exception>
573  public static void Transform(Plane[] planes, ref Matrix transformation)
574  {
575  if (planes == null)
576  throw new ArgumentNullException("planes");
577 
578  Matrix inverse;
579  Matrix.Invert(ref transformation, out inverse);
580 
581  for (int i = 0; i < planes.Length; ++i)
582  {
583  Transform(ref planes[i], ref transformation, out planes[i]);
584  }
585  }
586 
587  /// <summary>
588  /// Scales a plane by the given value.
589  /// </summary>
590  /// <param name="scale">The amount by which to scale the plane.</param>
591  /// <param name="plane">The plane to scale.</param>
592  /// <returns>The scaled plane.</returns>
593  public static Plane operator *(float scale, Plane plane)
594  {
595  return new Plane(plane.Normal.X * scale, plane.Normal.Y * scale, plane.Normal.Z * scale, plane.D * scale);
596  }
597 
598  /// <summary>
599  /// Scales a plane by the given value.
600  /// </summary>
601  /// <param name="plane">The plane to scale.</param>
602  /// <param name="scale">The amount by which to scale the plane.</param>
603  /// <returns>The scaled plane.</returns>
604  public static Plane operator *(Plane plane, float scale)
605  {
606  return new Plane(plane.Normal.X * scale, plane.Normal.Y * scale, plane.Normal.Z * scale, plane.D * scale);
607  }
608 
609  /// <summary>
610  /// Tests for equality between two objects.
611  /// </summary>
612  /// <param name="left">The first value to compare.</param>
613  /// <param name="right">The second value to compare.</param>
614  /// <returns><c>true</c> if <paramref name="left"/> has the same value as <paramref name="right"/>; otherwise, <c>false</c>.</returns>
615  public static bool operator ==(Plane left, Plane right)
616  {
617  return left.Equals(right);
618  }
619 
620  /// <summary>
621  /// Tests for inequality between two objects.
622  /// </summary>
623  /// <param name="left">The first value to compare.</param>
624  /// <param name="right">The second value to compare.</param>
625  /// <returns><c>true</c> if <paramref name="left"/> has a different value than <paramref name="right"/>; otherwise, <c>false</c>.</returns>
626  public static bool operator !=(Plane left, Plane right)
627  {
628  return !left.Equals(right);
629  }
630 
631  /// <summary>
632  /// Returns a <see cref="System.String"/> that represents this instance.
633  /// </summary>
634  /// <returns>
635  /// A <see cref="System.String"/> that represents this instance.
636  /// </returns>
637  public override string ToString()
638  {
639  return string.Format(CultureInfo.CurrentCulture, "A:{0} B:{1} C:{2} D:{3}", Normal.X, Normal.Y, Normal.Z, D);
640  }
641 
642  /// <summary>
643  /// Returns a <see cref="System.String"/> that represents this instance.
644  /// </summary>
645  /// <param name="format">The format.</param>
646  /// <returns>
647  /// A <see cref="System.String"/> that represents this instance.
648  /// </returns>
649  public string ToString(string format)
650  {
651  return string.Format(CultureInfo.CurrentCulture, "A:{0} B:{1} C:{2} D:{3}", Normal.X.ToString(format, CultureInfo.CurrentCulture),
652  Normal.Y.ToString(format, CultureInfo.CurrentCulture), Normal.Z.ToString(format, CultureInfo.CurrentCulture), D.ToString(format, CultureInfo.CurrentCulture));
653  }
654 
655  /// <summary>
656  /// Returns a <see cref="System.String"/> that represents this instance.
657  /// </summary>
658  /// <param name="formatProvider">The format provider.</param>
659  /// <returns>
660  /// A <see cref="System.String"/> that represents this instance.
661  /// </returns>
662  public string ToString(IFormatProvider formatProvider)
663  {
664  return string.Format(formatProvider, "A:{0} B:{1} C:{2} D:{3}", Normal.X, Normal.Y, Normal.Z, D);
665  }
666 
667  /// <summary>
668  /// Returns a <see cref="System.String"/> that represents this instance.
669  /// </summary>
670  /// <param name="format">The format.</param>
671  /// <param name="formatProvider">The format provider.</param>
672  /// <returns>
673  /// A <see cref="System.String"/> that represents this instance.
674  /// </returns>
675  public string ToString(string format, IFormatProvider formatProvider)
676  {
677  return string.Format(formatProvider, "A:{0} B:{1} C:{2} D:{3}", Normal.X.ToString(format, formatProvider),
678  Normal.Y.ToString(format, formatProvider), Normal.Z.ToString(format, formatProvider), D.ToString(format, formatProvider));
679  }
680 
681  /// <summary>
682  /// Returns a hash code for this instance.
683  /// </summary>
684  /// <returns>
685  /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
686  /// </returns>
687  public override int GetHashCode()
688  {
689  return Normal.GetHashCode() + D.GetHashCode();
690  }
691 
692  /// <summary>
693  /// Determines whether the specified <see cref="SiliconStudio.Core.Mathematics.Vector4"/> is equal to this instance.
694  /// </summary>
695  /// <param name="value">The <see cref="SiliconStudio.Core.Mathematics.Vector4"/> to compare with this instance.</param>
696  /// <returns>
697  /// <c>true</c> if the specified <see cref="SiliconStudio.Core.Mathematics.Vector4"/> is equal to this instance; otherwise, <c>false</c>.
698  /// </returns>
699  public bool Equals(Plane value)
700  {
701  return Normal == value.Normal && D == value.D;
702  }
703 
704  /// <summary>
705  /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
706  /// </summary>
707  /// <param name="value">The <see cref="System.Object"/> to compare with this instance.</param>
708  /// <returns>
709  /// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
710  /// </returns>
711  public override bool Equals(object value)
712  {
713  if (value == null)
714  return false;
715 
716  if (value.GetType() != GetType())
717  return false;
718 
719  return Equals((Plane)value);
720  }
721 
722 #if SlimDX1xInterop
723  /// <summary>
724  /// Performs an implicit conversion from <see cref="SiliconStudio.Core.Mathematics.Plane"/> to <see cref="SlimDX.Plane"/>.
725  /// </summary>
726  /// <param name="value">The value.</param>
727  /// <returns>The result of the conversion.</returns>
728  public static implicit operator SlimDX.Plane(Plane value)
729  {
730  return new SlimDX.Plane(value.Normal, value.D);
731  }
732 
733  /// <summary>
734  /// Performs an implicit conversion from <see cref="SlimDX.Plane"/> to <see cref="SiliconStudio.Core.Mathematics.Plane"/>.
735  /// </summary>
736  /// <param name="value">The value.</param>
737  /// <returns>The result of the conversion.</returns>
738  public static implicit operator Plane(SlimDX.Plane value)
739  {
740  return new Plane(value.Normal, value.D);
741  }
742 #endif
743 
744 #if XnaInterop
745  /// <summary>
746  /// Performs an implicit conversion from <see cref="SiliconStudio.Core.Mathematics.Plane"/> to <see cref="Microsoft.Xna.Framework.Plane"/>.
747  /// </summary>
748  /// <param name="value">The value.</param>
749  /// <returns>The result of the conversion.</returns>
750  public static implicit operator Microsoft.Xna.Framework.Plane(Plane value)
751  {
752  return new Microsoft.Xna.Framework.Plane(value.Normal, value.D);
753  }
754 
755  /// <summary>
756  /// Performs an implicit conversion from <see cref="Microsoft.Xna.Framework.Plane"/> to <see cref="SiliconStudio.Core.Mathematics.Plane"/>.
757  /// </summary>
758  /// <param name="value">The value.</param>
759  /// <returns>The result of the conversion.</returns>
760  public static implicit operator Plane(Microsoft.Xna.Framework.Plane value)
761  {
762  return new Plane(value.Normal, value.D);
763  }
764 #endif
765  }
766 }
Represents an axis-aligned bounding box in three dimensional space.
Definition: BoundingBox.cs:42
static void DotCoordinate(ref Plane left, ref Vector3 right, out float result)
Calculates the dot product of a specified vector and the normal of the plane plus the distance value ...
Definition: Plane.cs:353
override int GetHashCode()
Returns a hash code for this instance.
Definition: Plane.cs:687
static void Normalize(ref Plane plane, out Plane result)
Changes the coefficients of the normal vector of the plane to make it of unit length.
Definition: Plane.cs:396
override bool Equals(object value)
Determines whether the specified System.Object is equal to this instance.
Definition: Plane.cs:711
FbxDouble3 operator*(double factor, FbxDouble3 vector)
float Y
The Y component of the vector.
Definition: Vector3.cs:84
float M22
Value at row 2 column 2 of the matrix.
Definition: Matrix.cs:97
static void Dot(ref Plane left, ref Vector4 right, out float result)
Calculates the dot product of the specified vector and plane.
Definition: Plane.cs:331
static void Multiply(ref Plane value, float scale, out Plane result)
Scales the plane by the given scaling factor.
Definition: Plane.cs:306
function b
bool Equals(Plane value)
Determines whether the specified SiliconStudio.Core.Mathematics.Vector4 is equal to this instance...
Definition: Plane.cs:699
float D
The distance of the plane along its normal from the origin.
Definition: Plane.cs:52
function a
static void DotNormal(ref Plane left, ref Vector3 right, out float result)
Calculates the dot product of the specified vector and the normal of the plane.
Definition: Plane.cs:375
override string ToString()
Returns a System.String that represents this instance.
Definition: Plane.cs:637
static float DotNormal(Plane left, Vector3 right)
Calculates the dot product of the specified vector and the normal of the plane.
Definition: Plane.cs:386
string ToString(string format, IFormatProvider formatProvider)
Returns a System.String that represents this instance.
Definition: Plane.cs:675
float[] ToArray()
Creates an array containing the elements of the plane.
Definition: Plane.cs:196
static Plane Multiply(Plane value, float scale)
Scales the plane by the given scaling factor.
Definition: Plane.cs:320
float X
The X component of the vector.
Definition: Vector4.cs:83
Plane(Vector3 point, Vector3 normal)
Initializes a new instance of the T:SiliconStudio.Core.Mathematics.Plane class.
Definition: Plane.cs:83
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t y
Definition: DirectXTexP.h:191
static float DotCoordinate(Plane left, Vector3 right)
Calculates the dot product of a specified vector and the normal of the plane plus the distance value ...
Definition: Plane.cs:364
bool Intersects(ref Ray ray, out float distance)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.Ray.
Definition: Plane.cs:229
void Normalize()
Changes the coefficients of the normal vector of the plane to make it of unit length.
Definition: Plane.cs:182
float X
The X component of the vector.
Definition: Vector3.cs:78
float M13
Value at row 1 column 3 of the matrix.
Definition: Matrix.cs:112
Represents a three dimensional mathematical vector.
Definition: Vector3.cs:42
static void Transform(Plane[] planes, ref Matrix transformation)
Transforms an array of normalized planes by a matrix.
Definition: Plane.cs:573
float M23
Value at row 2 column 3 of the matrix.
Definition: Matrix.cs:117
float M42
Value at row 4 column 2 of the matrix.
Definition: Matrix.cs:107
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: Plane.cs:241
Represents a bounding sphere in three dimensional space.
static Plane Transform(Plane plane, Quaternion rotation)
Transforms a normalized plane by a quaternion rotation.
Definition: Plane.cs:454
float M44
Value at row 4 column 4 of the matrix.
Definition: Matrix.cs:147
Represents a plane in three dimensional space.
Definition: Plane.cs:42
Represents a four dimensional mathematical vector.
Definition: Vector4.cs:42
float M14
Value at row 1 column 4 of the matrix.
Definition: Matrix.cs:132
Represents a four dimensional mathematical quaternion.
Definition: Quaternion.cs:45
static void Transform(ref Plane plane, ref Quaternion rotation, out Plane result)
Transforms a normalized plane by a quaternion rotation.
Definition: Plane.cs:423
Vector3 Normal
The normal vector of the plane.
Definition: Plane.cs:47
The normal style (One line per item, structured by space).
Represents a three dimensional line based on a point in space and a direction.
Definition: Ray.cs:42
string ToString(string format)
Returns a System.String that represents this instance.
Definition: Plane.cs:649
Plane(Vector3 value, float d)
Initializes a new instance of the SiliconStudio.Core.Mathematics.Plane struct.
Definition: Plane.cs:94
bool Intersects(ref Ray ray)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.Ray.
Definition: Plane.cs:216
PlaneIntersectionType
Describes the result of an intersection with a plane in three dimensions.
string ToString(IFormatProvider formatProvider)
Returns a System.String that represents this instance.
Definition: Plane.cs:662
Plane(float a, float b, float c, float d)
Initializes a new instance of the SiliconStudio.Core.Mathematics.Plane struct.
Definition: Plane.cs:70
static float Dot(Plane left, Vector4 right)
Calculates the dot product of the specified vector and plane.
Definition: Plane.cs:342
float M12
Value at row 1 column 2 of the matrix.
Definition: Matrix.cs:92
bool Intersects(ref Plane plane, out Ray line)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.Plane.
Definition: Plane.cs:263
static void Transform(Plane[] planes, ref Quaternion rotation)
Transforms an array of normalized planes by a quaternion rotation.
Definition: Plane.cs:488
PlaneIntersectionType Intersects(ref BoundingBox box)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.BoundingBox.
Definition: Plane.cs:285
static Plane Normalize(Plane plane)
Changes the coefficients of the normal vector of the plane to make it of unit length.
Definition: Plane.cs:411
Plane(float value)
Initializes a new instance of the SiliconStudio.Core.Mathematics.Plane struct.
Definition: Plane.cs:58
static Plane Transform(Plane plane, Matrix transformation)
Transforms a normalized plane by a matrix.
Definition: Plane.cs:550
float M34
Value at row 3 column 4 of the matrix.
Definition: Matrix.cs:142
bool Intersects(ref Plane plane)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.Plane.
Definition: Plane.cs:251
float M33
Value at row 3 column 3 of the matrix.
Definition: Matrix.cs:122
float M43
Value at row 4 column 3 of the matrix.
Definition: Matrix.cs:127
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
Definition: DirectXTexP.h:175
float Z
The Z component of the vector.
Definition: Vector4.cs:95
static void Transform(ref Plane plane, ref Matrix transformation, out Plane result)
Transforms a normalized plane by a matrix.
Definition: Plane.cs:528
float Z
The Z component of the vector.
Definition: Vector3.cs:90
PlaneIntersectionType Intersects(ref Vector3 vertex1, ref Vector3 vertex2, ref Vector3 vertex3)
Determines if there is an intersection between the current object and a triangle. ...
Definition: Plane.cs:275
PlaneIntersectionType Intersects(ref BoundingSphere sphere)
Determines if there is an intersection between the current object and a SiliconStudio.Core.Mathematics.BoundingSphere.
Definition: Plane.cs:295
PlaneIntersectionType Intersects(ref Vector3 point)
Determines if there is an intersection between the current object and a point.
Definition: Plane.cs:206
float M32
Value at row 3 column 2 of the matrix.
Definition: Matrix.cs:102
Plane(Vector3 point1, Vector3 point2, Vector3 point3)
Initializes a new instance of the SiliconStudio.Core.Mathematics.Plane struct.
Definition: Plane.cs:106
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t size_t z
Definition: DirectXTexP.h:191
Plane(float[] values)
Initializes a new instance of the SiliconStudio.Core.Mathematics.Plane struct.
Definition: Plane.cs:131
float M24
Value at row 2 column 4 of the matrix.
Definition: Matrix.cs:137
Represents a 4x4 mathematical matrix.
Definition: Matrix.cs:47