Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Vector4.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 
34 namespace SiliconStudio.Core.Mathematics
35 {
36  /// <summary>
37  /// Represents a four dimensional mathematical vector.
38  /// </summary>
39  [DataContract("float4")]
40  [DataStyle(DataStyle.Compact)]
41  [StructLayout(LayoutKind.Sequential, Pack = 4)]
42  public struct Vector4 : IEquatable<Vector4>, IFormattable
43  {
44  /// <summary>
45  /// The size of the <see cref="SiliconStudio.Core.Mathematics.Vector4"/> type, in bytes.
46  /// </summary>
47  public static readonly int SizeInBytes = Marshal.SizeOf(typeof(Vector4));
48 
49  /// <summary>
50  /// A <see cref="SiliconStudio.Core.Mathematics.Vector4"/> with all of its components set to zero.
51  /// </summary>
52  public static readonly Vector4 Zero = new Vector4();
53 
54  /// <summary>
55  /// The X unit <see cref="SiliconStudio.Core.Mathematics.Vector4"/> (1, 0, 0, 0).
56  /// </summary>
57  public static readonly Vector4 UnitX = new Vector4(1.0f, 0.0f, 0.0f, 0.0f);
58 
59  /// <summary>
60  /// The Y unit <see cref="SiliconStudio.Core.Mathematics.Vector4"/> (0, 1, 0, 0).
61  /// </summary>
62  public static readonly Vector4 UnitY = new Vector4(0.0f, 1.0f, 0.0f, 0.0f);
63 
64  /// <summary>
65  /// The Z unit <see cref="SiliconStudio.Core.Mathematics.Vector4"/> (0, 0, 1, 0).
66  /// </summary>
67  public static readonly Vector4 UnitZ = new Vector4(0.0f, 0.0f, 1.0f, 0.0f);
68 
69  /// <summary>
70  /// The W unit <see cref="SiliconStudio.Core.Mathematics.Vector4"/> (0, 0, 0, 1).
71  /// </summary>
72  public static readonly Vector4 UnitW = new Vector4(0.0f, 0.0f, 0.0f, 1.0f);
73 
74  /// <summary>
75  /// A <see cref="SiliconStudio.Core.Mathematics.Vector4"/> with all of its components set to one.
76  /// </summary>
77  public static readonly Vector4 One = new Vector4(1.0f, 1.0f, 1.0f, 1.0f);
78 
79  /// <summary>
80  /// The X component of the vector.
81  /// </summary>
82  [DataMember(0)]
83  public float X;
84 
85  /// <summary>
86  /// The Y component of the vector.
87  /// </summary>
88  [DataMember(1)]
89  public float Y;
90 
91  /// <summary>
92  /// The Z component of the vector.
93  /// </summary>
94  [DataMember(2)]
95  public float Z;
96 
97  /// <summary>
98  /// The W component of the vector.
99  /// </summary>
100  [DataMember(3)]
101  public float W;
102 
103  /// <summary>
104  /// Initializes a new instance of the <see cref="SiliconStudio.Core.Mathematics.Vector4"/> struct.
105  /// </summary>
106  /// <param name="value">The value that will be assigned to all components.</param>
107  public Vector4(float value)
108  {
109  X = value;
110  Y = value;
111  Z = value;
112  W = value;
113  }
114 
115  /// <summary>
116  /// Initializes a new instance of the <see cref="SiliconStudio.Core.Mathematics.Vector4"/> struct.
117  /// </summary>
118  /// <param name="x">Initial value for the X component of the vector.</param>
119  /// <param name="y">Initial value for the Y component of the vector.</param>
120  /// <param name="z">Initial value for the Z component of the vector.</param>
121  /// <param name="w">Initial value for the W component of the vector.</param>
122  public Vector4(float x, float y, float z, float w)
123  {
124  X = x;
125  Y = y;
126  Z = z;
127  W = w;
128  }
129 
130  /// <summary>
131  /// Initializes a new instance of the <see cref="SiliconStudio.Core.Mathematics.Vector4"/> struct.
132  /// </summary>
133  /// <param name="value">A vector containing the values with which to initialize the X, Y, and Z components.</param>
134  /// <param name="w">Initial value for the W component of the vector.</param>
135  public Vector4(Vector3 value, float w)
136  {
137  X = value.X;
138  Y = value.Y;
139  Z = value.Z;
140  W = w;
141  }
142 
143  /// <summary>
144  /// Initializes a new instance of the <see cref="SiliconStudio.Core.Mathematics.Vector4"/> struct.
145  /// </summary>
146  /// <param name="value">A vector containing the values with which to initialize the X and Y components.</param>
147  /// <param name="z">Initial value for the Z component of the vector.</param>
148  /// <param name="w">Initial value for the W component of the vector.</param>
149  public Vector4(Vector2 value, float z, float w)
150  {
151  X = value.X;
152  Y = value.Y;
153  Z = z;
154  W = w;
155  }
156 
157  /// <summary>
158  /// Initializes a new instance of the <see cref="SiliconStudio.Core.Mathematics.Vector4"/> struct.
159  /// </summary>
160  /// <param name="values">The values to assign to the X, Y, Z, and W components of the vector. This must be an array with four elements.</param>
161  /// <exception cref="ArgumentNullException">Thrown when <paramref name="values"/> is <c>null</c>.</exception>
162  /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="values"/> contains more or less than four elements.</exception>
163  public Vector4(float[] values)
164  {
165  if (values == null)
166  throw new ArgumentNullException("values");
167  if (values.Length != 4)
168  throw new ArgumentOutOfRangeException("values", "There must be four and only four input values for Vector4.");
169 
170  X = values[0];
171  Y = values[1];
172  Z = values[2];
173  W = values[3];
174  }
175 
176  /// <summary>
177  /// Gets a value indicting whether this instance is normalized.
178  /// </summary>
179  public bool IsNormalized
180  {
181  get { return Math.Abs((X * X) + (Y * Y) + (Z * Z) + (W * W) - 1f) < MathUtil.ZeroTolerance; }
182  }
183 
184  /// <summary>
185  /// Gets or sets the component at the specified index.
186  /// </summary>
187  /// <value>The value of the X, Y, Z, or W component, depending on the index.</value>
188  /// <param name="index">The index of the component to access. Use 0 for the X component, 1 for the Y component, 2 for the Z component, and 3 for the W component.</param>
189  /// <returns>The value of the component at the specified index.</returns>
190  /// <exception cref="System.ArgumentOutOfRangeException">Thrown when the <paramref name="index"/> is out of the range [0, 3].</exception>
191  public float this[int index]
192  {
193  get
194  {
195  switch (index)
196  {
197  case 0: return X;
198  case 1: return Y;
199  case 2: return Z;
200  case 3: return W;
201  }
202 
203  throw new ArgumentOutOfRangeException("index", "Indices for Vector4 run from 0 to 3, inclusive.");
204  }
205 
206  set
207  {
208  switch (index)
209  {
210  case 0: X = value; break;
211  case 1: Y = value; break;
212  case 2: Z = value; break;
213  case 3: W = value; break;
214  default: throw new ArgumentOutOfRangeException("index", "Indices for Vector4 run from 0 to 3, inclusive.");
215  }
216  }
217  }
218 
219  /// <summary>
220  /// Calculates the length of the vector.
221  /// </summary>
222  /// <returns>The length of the vector.</returns>
223  /// <remarks>
224  /// <see cref="SiliconStudio.Core.Mathematics.Vector4.LengthSquared"/> may be preferred when only the relative length is needed
225  /// and speed is of the essence.
226  /// </remarks>
227  public float Length()
228  {
229  return (float)Math.Sqrt((X * X) + (Y * Y) + (Z * Z) + (W * W));
230  }
231 
232  /// <summary>
233  /// Calculates the squared length of the vector.
234  /// </summary>
235  /// <returns>The squared length of the vector.</returns>
236  /// <remarks>
237  /// This method may be preferred to <see cref="SiliconStudio.Core.Mathematics.Vector4.Length"/> when only a relative length is needed
238  /// and speed is of the essence.
239  /// </remarks>
240  public float LengthSquared()
241  {
242  return (X * X) + (Y * Y) + (Z * Z) + (W * W);
243  }
244 
245  /// <summary>
246  /// Converts the vector into a unit vector.
247  /// </summary>
248  public void Normalize()
249  {
250  float length = Length();
251  if (length > MathUtil.ZeroTolerance)
252  {
253  float inverse = 1.0f / length;
254  X *= inverse;
255  Y *= inverse;
256  Z *= inverse;
257  W *= inverse;
258  }
259  }
260 
261  /// <summary>
262  /// Raises the exponent for each components.
263  /// </summary>
264  /// <param name="exponent">The exponent.</param>
265  public void Pow(float exponent)
266  {
267  X = (float)Math.Pow(X, exponent);
268  Y = (float)Math.Pow(Y, exponent);
269  Z = (float)Math.Pow(Z, exponent);
270  W = (float)Math.Pow(W, exponent);
271  }
272 
273  /// <summary>
274  /// Creates an array containing the elements of the vector.
275  /// </summary>
276  /// <returns>A four-element array containing the components of the vector.</returns>
277  public float[] ToArray()
278  {
279  return new float[] { X, Y, Z, W };
280  }
281 
282  /// <summary>
283  /// Adds two vectors.
284  /// </summary>
285  /// <param name="left">The first vector to add.</param>
286  /// <param name="right">The second vector to add.</param>
287  /// <param name="result">When the method completes, contains the sum of the two vectors.</param>
288  public static void Add(ref Vector4 left, ref Vector4 right, out Vector4 result)
289  {
290  result = new Vector4(left.X + right.X, left.Y + right.Y, left.Z + right.Z, left.W + right.W);
291  }
292 
293  /// <summary>
294  /// Adds two vectors.
295  /// </summary>
296  /// <param name="left">The first vector to add.</param>
297  /// <param name="right">The second vector to add.</param>
298  /// <returns>The sum of the two vectors.</returns>
299  public static Vector4 Add(Vector4 left, Vector4 right)
300  {
301  return new Vector4(left.X + right.X, left.Y + right.Y, left.Z + right.Z, left.W + right.W);
302  }
303 
304  /// <summary>
305  /// Subtracts two vectors.
306  /// </summary>
307  /// <param name="left">The first vector to subtract.</param>
308  /// <param name="right">The second vector to subtract.</param>
309  /// <param name="result">When the method completes, contains the difference of the two vectors.</param>
310  public static void Subtract(ref Vector4 left, ref Vector4 right, out Vector4 result)
311  {
312  result = new Vector4(left.X - right.X, left.Y - right.Y, left.Z - right.Z, left.W - right.W);
313  }
314 
315  /// <summary>
316  /// Subtracts two vectors.
317  /// </summary>
318  /// <param name="left">The first vector to subtract.</param>
319  /// <param name="right">The second vector to subtract.</param>
320  /// <returns>The difference of the two vectors.</returns>
321  public static Vector4 Subtract(Vector4 left, Vector4 right)
322  {
323  return new Vector4(left.X - right.X, left.Y - right.Y, left.Z - right.Z, left.W - right.W);
324  }
325 
326  /// <summary>
327  /// Scales a vector by the given value.
328  /// </summary>
329  /// <param name="value">The vector to scale.</param>
330  /// <param name="scale">The amount by which to scale the vector.</param>
331  /// <param name="result">When the method completes, contains the scaled vector.</param>
332  public static void Multiply(ref Vector4 value, float scale, out Vector4 result)
333  {
334  result = new Vector4(value.X * scale, value.Y * scale, value.Z * scale, value.W * scale);
335  }
336 
337  /// <summary>
338  /// Scales a vector by the given value.
339  /// </summary>
340  /// <param name="value">The vector to scale.</param>
341  /// <param name="scale">The amount by which to scale the vector.</param>
342  /// <returns>The scaled vector.</returns>
343  public static Vector4 Multiply(Vector4 value, float scale)
344  {
345  return new Vector4(value.X * scale, value.Y * scale, value.Z * scale, value.W * scale);
346  }
347 
348  /// <summary>
349  /// Modulates a vector with another by performing component-wise multiplication.
350  /// </summary>
351  /// <param name="left">The first vector to modulate.</param>
352  /// <param name="right">The second vector to modulate.</param>
353  /// <param name="result">When the method completes, contains the modulated vector.</param>
354  public static void Modulate(ref Vector4 left, ref Vector4 right, out Vector4 result)
355  {
356  result = new Vector4(left.X * right.X, left.Y * right.Y, left.Z * right.Z, left.W * right.W);
357  }
358 
359  /// <summary>
360  /// Modulates a vector with another by performing component-wise multiplication.
361  /// </summary>
362  /// <param name="left">The first vector to modulate.</param>
363  /// <param name="right">The second vector to modulate.</param>
364  /// <returns>The modulated vector.</returns>
365  public static Vector4 Modulate(Vector4 left, Vector4 right)
366  {
367  return new Vector4(left.X * right.X, left.Y * right.Y, left.Z * right.Z, left.W * right.W);
368  }
369 
370  /// <summary>
371  /// Scales a vector by the given value.
372  /// </summary>
373  /// <param name="value">The vector to scale.</param>
374  /// <param name="scale">The amount by which to scale the vector.</param>
375  /// <param name="result">When the method completes, contains the scaled vector.</param>
376  public static void Divide(ref Vector4 value, float scale, out Vector4 result)
377  {
378  result = new Vector4(value.X / scale, value.Y / scale, value.Z / scale, value.W / scale);
379  }
380 
381  /// <summary>
382  /// Scales a vector by the given value.
383  /// </summary>
384  /// <param name="value">The vector to scale.</param>
385  /// <param name="scale">The amount by which to scale the vector.</param>
386  /// <returns>The scaled vector.</returns>
387  public static Vector4 Divide(Vector4 value, float scale)
388  {
389  return new Vector4(value.X / scale, value.Y / scale, value.Z / scale, value.W / scale);
390  }
391 
392  /// <summary>
393  /// Demodulates a vector with another by performing component-wise division.
394  /// </summary>
395  /// <param name="left">The first vector to demodulate.</param>
396  /// <param name="right">The second vector to demodulate.</param>
397  /// <param name="result">When the method completes, contains the demodulated vector.</param>
398  public static void Demodulate(ref Vector4 left, ref Vector4 right, out Vector4 result)
399  {
400  result = new Vector4(left.X / right.X, left.Y / right.Y, left.Z / right.Z, left.W / right.W);
401  }
402 
403  /// <summary>
404  /// Demodulates a vector with another by performing component-wise division.
405  /// </summary>
406  /// <param name="left">The first vector to demodulate.</param>
407  /// <param name="right">The second vector to demodulate.</param>
408  /// <returns>The demodulated vector.</returns>
409  public static Vector4 Demodulate(Vector4 left, Vector4 right)
410  {
411  return new Vector4(left.X / right.X, left.Y / right.Y, left.Z / right.Z, left.W / right.W);
412  }
413 
414  /// <summary>
415  /// Reverses the direction of a given vector.
416  /// </summary>
417  /// <param name="value">The vector to negate.</param>
418  /// <param name="result">When the method completes, contains a vector facing in the opposite direction.</param>
419  public static void Negate(ref Vector4 value, out Vector4 result)
420  {
421  result = new Vector4(-value.X, -value.Y, -value.Z, -value.W);
422  }
423 
424  /// <summary>
425  /// Reverses the direction of a given vector.
426  /// </summary>
427  /// <param name="value">The vector to negate.</param>
428  /// <returns>A vector facing in the opposite direction.</returns>
429  public static Vector4 Negate(Vector4 value)
430  {
431  return new Vector4(-value.X, -value.Y, -value.Z, -value.W);
432  }
433 
434  /// <summary>
435  /// Returns a <see cref="SiliconStudio.Core.Mathematics.Vector4"/> containing the 4D Cartesian coordinates of a point specified in Barycentric coordinates relative to a 4D triangle.
436  /// </summary>
437  /// <param name="value1">A <see cref="SiliconStudio.Core.Mathematics.Vector4"/> containing the 4D Cartesian coordinates of vertex 1 of the triangle.</param>
438  /// <param name="value2">A <see cref="SiliconStudio.Core.Mathematics.Vector4"/> containing the 4D Cartesian coordinates of vertex 2 of the triangle.</param>
439  /// <param name="value3">A <see cref="SiliconStudio.Core.Mathematics.Vector4"/> containing the 4D Cartesian coordinates of vertex 3 of the triangle.</param>
440  /// <param name="amount1">Barycentric coordinate b2, which expresses the weighting factor toward vertex 2 (specified in <paramref name="value2"/>).</param>
441  /// <param name="amount2">Barycentric coordinate b3, which expresses the weighting factor toward vertex 3 (specified in <paramref name="value3"/>).</param>
442  /// <param name="result">When the method completes, contains the 4D Cartesian coordinates of the specified point.</param>
443  public static void Barycentric(ref Vector4 value1, ref Vector4 value2, ref Vector4 value3, float amount1, float amount2, out Vector4 result)
444  {
445  result = new Vector4((value1.X + (amount1 * (value2.X - value1.X))) + (amount2 * (value3.X - value1.X)),
446  (value1.Y + (amount1 * (value2.Y - value1.Y))) + (amount2 * (value3.Y - value1.Y)),
447  (value1.Z + (amount1 * (value2.Z - value1.Z))) + (amount2 * (value3.Z - value1.Z)),
448  (value1.W + (amount1 * (value2.W - value1.W))) + (amount2 * (value3.W - value1.W)));
449  }
450 
451  /// <summary>
452  /// Returns a <see cref="SiliconStudio.Core.Mathematics.Vector4"/> containing the 4D Cartesian coordinates of a point specified in Barycentric coordinates relative to a 4D triangle.
453  /// </summary>
454  /// <param name="value1">A <see cref="SiliconStudio.Core.Mathematics.Vector4"/> containing the 4D Cartesian coordinates of vertex 1 of the triangle.</param>
455  /// <param name="value2">A <see cref="SiliconStudio.Core.Mathematics.Vector4"/> containing the 4D Cartesian coordinates of vertex 2 of the triangle.</param>
456  /// <param name="value3">A <see cref="SiliconStudio.Core.Mathematics.Vector4"/> containing the 4D Cartesian coordinates of vertex 3 of the triangle.</param>
457  /// <param name="amount1">Barycentric coordinate b2, which expresses the weighting factor toward vertex 2 (specified in <paramref name="value2"/>).</param>
458  /// <param name="amount2">Barycentric coordinate b3, which expresses the weighting factor toward vertex 3 (specified in <paramref name="value3"/>).</param>
459  /// <returns>A new <see cref="SiliconStudio.Core.Mathematics.Vector4"/> containing the 4D Cartesian coordinates of the specified point.</returns>
460  public static Vector4 Barycentric(Vector4 value1, Vector4 value2, Vector4 value3, float amount1, float amount2)
461  {
462  Vector4 result;
463  Barycentric(ref value1, ref value2, ref value3, amount1, amount2, out result);
464  return result;
465  }
466 
467  /// <summary>
468  /// Restricts a value to be within a specified range.
469  /// </summary>
470  /// <param name="value">The value to clamp.</param>
471  /// <param name="min">The minimum value.</param>
472  /// <param name="max">The maximum value.</param>
473  /// <param name="result">When the method completes, contains the clamped value.</param>
474  public static void Clamp(ref Vector4 value, ref Vector4 min, ref Vector4 max, out Vector4 result)
475  {
476  float x = value.X;
477  x = (x > max.X) ? max.X : x;
478  x = (x < min.X) ? min.X : x;
479 
480  float y = value.Y;
481  y = (y > max.Y) ? max.Y : y;
482  y = (y < min.Y) ? min.Y : y;
483 
484  float z = value.Z;
485  z = (z > max.Z) ? max.Z : z;
486  z = (z < min.Z) ? min.Z : z;
487 
488  float w = value.W;
489  w = (w > max.W) ? max.W : w;
490  w = (w < min.W) ? min.W : w;
491 
492  result = new Vector4(x, y, z, w);
493  }
494 
495  /// <summary>
496  /// Restricts a value to be within a specified range.
497  /// </summary>
498  /// <param name="value">The value to clamp.</param>
499  /// <param name="min">The minimum value.</param>
500  /// <param name="max">The maximum value.</param>
501  /// <returns>The clamped value.</returns>
502  public static Vector4 Clamp(Vector4 value, Vector4 min, Vector4 max)
503  {
504  Vector4 result;
505  Clamp(ref value, ref min, ref max, out result);
506  return result;
507  }
508 
509  /// <summary>
510  /// Calculates the distance between two vectors.
511  /// </summary>
512  /// <param name="value1">The first vector.</param>
513  /// <param name="value2">The second vector.</param>
514  /// <param name="result">When the method completes, contains the distance between the two vectors.</param>
515  /// <remarks>
516  /// <see cref="SiliconStudio.Core.Mathematics.Vector4.DistanceSquared(ref Vector4, ref Vector4, out float)"/> may be preferred when only the relative distance is needed
517  /// and speed is of the essence.
518  /// </remarks>
519  public static void Distance(ref Vector4 value1, ref Vector4 value2, out float result)
520  {
521  float x = value1.X - value2.X;
522  float y = value1.Y - value2.Y;
523  float z = value1.Z - value2.Z;
524  float w = value1.W - value2.W;
525 
526  result = (float)Math.Sqrt((x * x) + (y * y) + (z * z) + (w * w));
527  }
528 
529  /// <summary>
530  /// Calculates the distance between two vectors.
531  /// </summary>
532  /// <param name="value1">The first vector.</param>
533  /// <param name="value2">The second vector.</param>
534  /// <returns>The distance between the two vectors.</returns>
535  /// <remarks>
536  /// <see cref="SiliconStudio.Core.Mathematics.Vector4.DistanceSquared(Vector4, Vector4)"/> may be preferred when only the relative distance is needed
537  /// and speed is of the essence.
538  /// </remarks>
539  public static float Distance(Vector4 value1, Vector4 value2)
540  {
541  float x = value1.X - value2.X;
542  float y = value1.Y - value2.Y;
543  float z = value1.Z - value2.Z;
544  float w = value1.W - value2.W;
545 
546  return (float)Math.Sqrt((x * x) + (y * y) + (z * z) + (w * w));
547  }
548 
549  /// <summary>
550  /// Calculates the squared distance between two vectors.
551  /// </summary>
552  /// <param name="value1">The first vector.</param>
553  /// <param name="value2">The second vector.</param>
554  /// <param name="result">When the method completes, contains the squared distance between the two vectors.</param>
555  /// <remarks>Distance squared is the value before taking the square root.
556  /// Distance squared can often be used in place of distance if relative comparisons are being made.
557  /// For example, consider three points A, B, and C. To determine whether B or C is further from A,
558  /// compare the distance between A and B to the distance between A and C. Calculating the two distances
559  /// involves two square roots, which are computationally expensive. However, using distance squared
560  /// provides the same information and avoids calculating two square roots.
561  /// </remarks>
562  public static void DistanceSquared(ref Vector4 value1, ref Vector4 value2, out float result)
563  {
564  float x = value1.X - value2.X;
565  float y = value1.Y - value2.Y;
566  float z = value1.Z - value2.Z;
567  float w = value1.W - value2.W;
568 
569  result = (x * x) + (y * y) + (z * z) + (w * w);
570  }
571 
572  /// <summary>
573  /// Calculates the squared distance between two vectors.
574  /// </summary>
575  /// <param name="value1">The first vector.</param>
576  /// <param name="value2">The second vector.</param>
577  /// <returns>The squared distance between the two vectors.</returns>
578  /// <remarks>Distance squared is the value before taking the square root.
579  /// Distance squared can often be used in place of distance if relative comparisons are being made.
580  /// For example, consider three points A, B, and C. To determine whether B or C is further from A,
581  /// compare the distance between A and B to the distance between A and C. Calculating the two distances
582  /// involves two square roots, which are computationally expensive. However, using distance squared
583  /// provides the same information and avoids calculating two square roots.
584  /// </remarks>
585  public static float DistanceSquared(Vector4 value1, Vector4 value2)
586  {
587  float x = value1.X - value2.X;
588  float y = value1.Y - value2.Y;
589  float z = value1.Z - value2.Z;
590  float w = value1.W - value2.W;
591 
592  return (x * x) + (y * y) + (z * z) + (w * w);
593  }
594 
595  /// <summary>
596  /// Calculates the dot product of two vectors.
597  /// </summary>
598  /// <param name="left">First source vector</param>
599  /// <param name="right">Second source vector.</param>
600  /// <param name="result">When the method completes, contains the dot product of the two vectors.</param>
601  public static void Dot(ref Vector4 left, ref Vector4 right, out float result)
602  {
603  result = (left.X * right.X) + (left.Y * right.Y) + (left.Z * right.Z) + (left.W * right.W);
604  }
605 
606  /// <summary>
607  /// Calculates the dot product of two vectors.
608  /// </summary>
609  /// <param name="left">First source vector.</param>
610  /// <param name="right">Second source vector.</param>
611  /// <returns>The dot product of the two vectors.</returns>
612  public static float Dot(Vector4 left, Vector4 right)
613  {
614  return (left.X * right.X) + (left.Y * right.Y) + (left.Z * right.Z) + (left.W * right.W);
615  }
616 
617  /// <summary>
618  /// Converts the vector into a unit vector.
619  /// </summary>
620  /// <param name="value">The vector to normalize.</param>
621  /// <param name="result">When the method completes, contains the normalized vector.</param>
622  public static void Normalize(ref Vector4 value, out Vector4 result)
623  {
624  Vector4 temp = value;
625  result = temp;
626  result.Normalize();
627  }
628 
629  /// <summary>
630  /// Converts the vector into a unit vector.
631  /// </summary>
632  /// <param name="value">The vector to normalize.</param>
633  /// <returns>The normalized vector.</returns>
634  public static Vector4 Normalize(Vector4 value)
635  {
636  value.Normalize();
637  return value;
638  }
639 
640  /// <summary>
641  /// Performs a linear interpolation between two vectors.
642  /// </summary>
643  /// <param name="start">Start vector.</param>
644  /// <param name="end">End vector.</param>
645  /// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
646  /// <param name="result">When the method completes, contains the linear interpolation of the two vectors.</param>
647  /// <remarks>
648  /// This method performs the linear interpolation based on the following formula.
649  /// <code>start + (end - start) * amount</code>
650  /// Passing <paramref name="amount"/> a value of 0 will cause <paramref name="start"/> to be returned; a value of 1 will cause <paramref name="end"/> to be returned.
651  /// </remarks>
652  public static void Lerp(ref Vector4 start, ref Vector4 end, float amount, out Vector4 result)
653  {
654  result.X = start.X + ((end.X - start.X) * amount);
655  result.Y = start.Y + ((end.Y - start.Y) * amount);
656  result.Z = start.Z + ((end.Z - start.Z) * amount);
657  result.W = start.W + ((end.W - start.W) * amount);
658  }
659 
660  /// <summary>
661  /// Performs a linear interpolation between two vectors.
662  /// </summary>
663  /// <param name="start">Start vector.</param>
664  /// <param name="end">End vector.</param>
665  /// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
666  /// <returns>The linear interpolation of the two vectors.</returns>
667  /// <remarks>
668  /// This method performs the linear interpolation based on the following formula.
669  /// <code>start + (end - start) * amount</code>
670  /// Passing <paramref name="amount"/> a value of 0 will cause <paramref name="start"/> to be returned; a value of 1 will cause <paramref name="end"/> to be returned.
671  /// </remarks>
672  public static Vector4 Lerp(Vector4 start, Vector4 end, float amount)
673  {
674  Vector4 result;
675  Lerp(ref start, ref end, amount, out result);
676  return result;
677  }
678 
679  /// <summary>
680  /// Performs a cubic interpolation between two vectors.
681  /// </summary>
682  /// <param name="start">Start vector.</param>
683  /// <param name="end">End vector.</param>
684  /// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
685  /// <param name="result">When the method completes, contains the cubic interpolation of the two vectors.</param>
686  public static void SmoothStep(ref Vector4 start, ref Vector4 end, float amount, out Vector4 result)
687  {
688  amount = (amount > 1.0f) ? 1.0f : ((amount < 0.0f) ? 0.0f : amount);
689  amount = (amount * amount) * (3.0f - (2.0f * amount));
690 
691  result.X = start.X + ((end.X - start.X) * amount);
692  result.Y = start.Y + ((end.Y - start.Y) * amount);
693  result.Z = start.Z + ((end.Z - start.Z) * amount);
694  result.W = start.W + ((end.W - start.W) * amount);
695  }
696 
697  /// <summary>
698  /// Performs a cubic interpolation between two vectors.
699  /// </summary>
700  /// <param name="start">Start vector.</param>
701  /// <param name="end">End vector.</param>
702  /// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
703  /// <returns>The cubic interpolation of the two vectors.</returns>
704  public static Vector4 SmoothStep(Vector4 start, Vector4 end, float amount)
705  {
706  Vector4 result;
707  SmoothStep(ref start, ref end, amount, out result);
708  return result;
709  }
710 
711  /// <summary>
712  /// Performs a Hermite spline interpolation.
713  /// </summary>
714  /// <param name="value1">First source position vector.</param>
715  /// <param name="tangent1">First source tangent vector.</param>
716  /// <param name="value2">Second source position vector.</param>
717  /// <param name="tangent2">Second source tangent vector.</param>
718  /// <param name="amount">Weighting factor.</param>
719  /// <param name="result">When the method completes, contains the result of the Hermite spline interpolation.</param>
720  public static void Hermite(ref Vector4 value1, ref Vector4 tangent1, ref Vector4 value2, ref Vector4 tangent2, float amount, out Vector4 result)
721  {
722  float squared = amount * amount;
723  float cubed = amount * squared;
724  float part1 = ((2.0f * cubed) - (3.0f * squared)) + 1.0f;
725  float part2 = (-2.0f * cubed) + (3.0f * squared);
726  float part3 = (cubed - (2.0f * squared)) + amount;
727  float part4 = cubed - squared;
728 
729  result = new Vector4((((value1.X * part1) + (value2.X * part2)) + (tangent1.X * part3)) + (tangent2.X * part4),
730  (((value1.Y * part1) + (value2.Y * part2)) + (tangent1.Y * part3)) + (tangent2.Y * part4),
731  (((value1.Z * part1) + (value2.Z * part2)) + (tangent1.Z * part3)) + (tangent2.Z * part4),
732  (((value1.W * part1) + (value2.W * part2)) + (tangent1.W * part3)) + (tangent2.W * part4));
733  }
734 
735  /// <summary>
736  /// Performs a Hermite spline interpolation.
737  /// </summary>
738  /// <param name="value1">First source position vector.</param>
739  /// <param name="tangent1">First source tangent vector.</param>
740  /// <param name="value2">Second source position vector.</param>
741  /// <param name="tangent2">Second source tangent vector.</param>
742  /// <param name="amount">Weighting factor.</param>
743  /// <returns>The result of the Hermite spline interpolation.</returns>
744  public static Vector4 Hermite(Vector4 value1, Vector4 tangent1, Vector4 value2, Vector4 tangent2, float amount)
745  {
746  Vector4 result;
747  Hermite(ref value1, ref tangent1, ref value2, ref tangent2, amount, out result);
748  return result;
749  }
750 
751  /// <summary>
752  /// Performs a Catmull-Rom interpolation using the specified positions.
753  /// </summary>
754  /// <param name="value1">The first position in the interpolation.</param>
755  /// <param name="value2">The second position in the interpolation.</param>
756  /// <param name="value3">The third position in the interpolation.</param>
757  /// <param name="value4">The fourth position in the interpolation.</param>
758  /// <param name="amount">Weighting factor.</param>
759  /// <param name="result">When the method completes, contains the result of the Catmull-Rom interpolation.</param>
760  public static void CatmullRom(ref Vector4 value1, ref Vector4 value2, ref Vector4 value3, ref Vector4 value4, float amount, out Vector4 result)
761  {
762  float squared = amount * amount;
763  float cubed = amount * squared;
764 
765  result.X = 0.5f * ((((2.0f * value2.X) + ((-value1.X + value3.X) * amount)) + (((((2.0f * value1.X) - (5.0f * value2.X)) + (4.0f * value3.X)) - value4.X) * squared)) + ((((-value1.X + (3.0f * value2.X)) - (3.0f * value3.X)) + value4.X) * cubed));
766  result.Y = 0.5f * ((((2.0f * value2.Y) + ((-value1.Y + value3.Y) * amount)) + (((((2.0f * value1.Y) - (5.0f * value2.Y)) + (4.0f * value3.Y)) - value4.Y) * squared)) + ((((-value1.Y + (3.0f * value2.Y)) - (3.0f * value3.Y)) + value4.Y) * cubed));
767  result.Z = 0.5f * ((((2.0f * value2.Z) + ((-value1.Z + value3.Z) * amount)) + (((((2.0f * value1.Z) - (5.0f * value2.Z)) + (4.0f * value3.Z)) - value4.Z) * squared)) + ((((-value1.Z + (3.0f * value2.Z)) - (3.0f * value3.Z)) + value4.Z) * cubed));
768  result.W = 0.5f * ((((2.0f * value2.W) + ((-value1.W + value3.W) * amount)) + (((((2.0f * value1.W) - (5.0f * value2.W)) + (4.0f * value3.W)) - value4.W) * squared)) + ((((-value1.W + (3.0f * value2.W)) - (3.0f * value3.W)) + value4.W) * cubed));
769  }
770 
771  /// <summary>
772  /// Performs a Catmull-Rom interpolation using the specified positions.
773  /// </summary>
774  /// <param name="value1">The first position in the interpolation.</param>
775  /// <param name="value2">The second position in the interpolation.</param>
776  /// <param name="value3">The third position in the interpolation.</param>
777  /// <param name="value4">The fourth position in the interpolation.</param>
778  /// <param name="amount">Weighting factor.</param>
779  /// <returns>A vector that is the result of the Catmull-Rom interpolation.</returns>
780  public static Vector4 CatmullRom(Vector4 value1, Vector4 value2, Vector4 value3, Vector4 value4, float amount)
781  {
782  Vector4 result;
783  CatmullRom(ref value1, ref value2, ref value3, ref value4, amount, out result);
784  return result;
785  }
786 
787  /// <summary>
788  /// Returns a vector containing the smallest components of the specified vectors.
789  /// </summary>
790  /// <param name="left">The first source vector.</param>
791  /// <param name="right">The second source vector.</param>
792  /// <param name="result">When the method completes, contains an new vector composed of the largest components of the source vectors.</param>
793  public static void Max(ref Vector4 left, ref Vector4 right, out Vector4 result)
794  {
795  result.X = (left.X > right.X) ? left.X : right.X;
796  result.Y = (left.Y > right.Y) ? left.Y : right.Y;
797  result.Z = (left.Z > right.Z) ? left.Z : right.Z;
798  result.W = (left.W > right.W) ? left.W : right.W;
799  }
800 
801  /// <summary>
802  /// Returns a vector containing the largest components of the specified vectors.
803  /// </summary>
804  /// <param name="left">The first source vector.</param>
805  /// <param name="right">The second source vector.</param>
806  /// <returns>A vector containing the largest components of the source vectors.</returns>
807  public static Vector4 Max(Vector4 left, Vector4 right)
808  {
809  Vector4 result;
810  Max(ref left, ref right, out result);
811  return result;
812  }
813 
814  /// <summary>
815  /// Returns a vector containing the smallest components of the specified vectors.
816  /// </summary>
817  /// <param name="left">The first source vector.</param>
818  /// <param name="right">The second source vector.</param>
819  /// <param name="result">When the method completes, contains an new vector composed of the smallest components of the source vectors.</param>
820  public static void Min(ref Vector4 left, ref Vector4 right, out Vector4 result)
821  {
822  result.X = (left.X < right.X) ? left.X : right.X;
823  result.Y = (left.Y < right.Y) ? left.Y : right.Y;
824  result.Z = (left.Z < right.Z) ? left.Z : right.Z;
825  result.W = (left.W < right.W) ? left.W : right.W;
826  }
827 
828  /// <summary>
829  /// Returns a vector containing the smallest components of the specified vectors.
830  /// </summary>
831  /// <param name="left">The first source vector.</param>
832  /// <param name="right">The second source vector.</param>
833  /// <returns>A vector containing the smallest components of the source vectors.</returns>
834  public static Vector4 Min(Vector4 left, Vector4 right)
835  {
836  Vector4 result;
837  Min(ref left, ref right, out result);
838  return result;
839  }
840 
841  /// <summary>
842  /// Orthogonalizes a list of vectors.
843  /// </summary>
844  /// <param name="destination">The list of orthogonalized vectors.</param>
845  /// <param name="source">The list of vectors to orthogonalize.</param>
846  /// <remarks>
847  /// <para>Orthogonalization is the process of making all vectors orthogonal to each other. This
848  /// means that any given vector in the list will be orthogonal to any other given vector in the
849  /// list.</para>
850  /// <para>Because this method uses the modified Gram-Schmidt process, the resulting vectors
851  /// tend to be numerically unstable. The numeric stability decreases according to the vectors
852  /// position in the list so that the first vector is the most stable and the last vector is the
853  /// least stable.</para>
854  /// </remarks>
855  /// <exception cref="ArgumentNullException">Thrown when <paramref name="source"/> or <paramref name="destination"/> is <c>null</c>.</exception>
856  /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="destination"/> is shorter in length than <paramref name="source"/>.</exception>
857  public static void Orthogonalize(Vector4[] destination, params Vector4[] source)
858  {
859  //Uses the modified Gram-Schmidt process.
860  //q1 = m1
861  //q2 = m2 - ((q1 â‹… m2) / (q1 â‹… q1)) * q1
862  //q3 = m3 - ((q1 â‹… m3) / (q1 â‹… q1)) * q1 - ((q2 â‹… m3) / (q2 â‹… q2)) * q2
863  //q4 = m4 - ((q1 â‹… m4) / (q1 â‹… q1)) * q1 - ((q2 â‹… m4) / (q2 â‹… q2)) * q2 - ((q3 â‹… m4) / (q3 â‹… q3)) * q3
864  //q5 = ...
865 
866  if (source == null)
867  throw new ArgumentNullException("source");
868  if (destination == null)
869  throw new ArgumentNullException("destination");
870  if (destination.Length < source.Length)
871  throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array.");
872 
873  for (int i = 0; i < source.Length; ++i)
874  {
875  Vector4 newvector = source[i];
876 
877  for (int r = 0; r < i; ++r)
878  {
879  newvector -= (Vector4.Dot(destination[r], newvector) / Vector4.Dot(destination[r], destination[r])) * destination[r];
880  }
881 
882  destination[i] = newvector;
883  }
884  }
885 
886  /// <summary>
887  /// Orthonormalizes a list of vectors.
888  /// </summary>
889  /// <param name="destination">The list of orthonormalized vectors.</param>
890  /// <param name="source">The list of vectors to orthonormalize.</param>
891  /// <remarks>
892  /// <para>Orthonormalization is the process of making all vectors orthogonal to each
893  /// other and making all vectors of unit length. This means that any given vector will
894  /// be orthogonal to any other given vector in the list.</para>
895  /// <para>Because this method uses the modified Gram-Schmidt process, the resulting vectors
896  /// tend to be numerically unstable. The numeric stability decreases according to the vectors
897  /// position in the list so that the first vector is the most stable and the last vector is the
898  /// least stable.</para>
899  /// </remarks>
900  /// <exception cref="ArgumentNullException">Thrown when <paramref name="source"/> or <paramref name="destination"/> is <c>null</c>.</exception>
901  /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="destination"/> is shorter in length than <paramref name="source"/>.</exception>
902  public static void Orthonormalize(Vector4[] destination, params Vector4[] source)
903  {
904  //Uses the modified Gram-Schmidt process.
905  //Because we are making unit vectors, we can optimize the math for orthogonalization
906  //and simplify the projection operation to remove the division.
907  //q1 = m1 / |m1|
908  //q2 = (m2 - (q1 â‹… m2) * q1) / |m2 - (q1 â‹… m2) * q1|
909  //q3 = (m3 - (q1 â‹… m3) * q1 - (q2 â‹… m3) * q2) / |m3 - (q1 â‹… m3) * q1 - (q2 â‹… m3) * q2|
910  //q4 = (m4 - (q1 â‹… m4) * q1 - (q2 â‹… m4) * q2 - (q3 â‹… m4) * q3) / |m4 - (q1 â‹… m4) * q1 - (q2 â‹… m4) * q2 - (q3 â‹… m4) * q3|
911  //q5 = ...
912 
913  if (source == null)
914  throw new ArgumentNullException("source");
915  if (destination == null)
916  throw new ArgumentNullException("destination");
917  if (destination.Length < source.Length)
918  throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array.");
919 
920  for (int i = 0; i < source.Length; ++i)
921  {
922  Vector4 newvector = source[i];
923 
924  for (int r = 0; r < i; ++r)
925  {
926  newvector -= Vector4.Dot(destination[r], newvector) * destination[r];
927  }
928 
929  newvector.Normalize();
930  destination[i] = newvector;
931  }
932  }
933 
934  /// <summary>
935  /// Transforms a 4D vector by the given <see cref="SiliconStudio.Core.Mathematics.Quaternion"/> rotation.
936  /// </summary>
937  /// <param name="vector">The vector to rotate.</param>
938  /// <param name="rotation">The <see cref="SiliconStudio.Core.Mathematics.Quaternion"/> rotation to apply.</param>
939  /// <param name="result">When the method completes, contains the transformed <see cref="SiliconStudio.Core.Mathematics.Vector4"/>.</param>
940  public static void Transform(ref Vector4 vector, ref Quaternion rotation, out Vector4 result)
941  {
942  float x = rotation.X + rotation.X;
943  float y = rotation.Y + rotation.Y;
944  float z = rotation.Z + rotation.Z;
945  float wx = rotation.W * x;
946  float wy = rotation.W * y;
947  float wz = rotation.W * z;
948  float xx = rotation.X * x;
949  float xy = rotation.X * y;
950  float xz = rotation.X * z;
951  float yy = rotation.Y * y;
952  float yz = rotation.Y * z;
953  float zz = rotation.Z * z;
954 
955  result = new Vector4(
956  ((vector.X * ((1.0f - yy) - zz)) + (vector.Y * (xy - wz))) + (vector.Z * (xz + wy)),
957  ((vector.X * (xy + wz)) + (vector.Y * ((1.0f - xx) - zz))) + (vector.Z * (yz - wx)),
958  ((vector.X * (xz - wy)) + (vector.Y * (yz + wx))) + (vector.Z * ((1.0f - xx) - yy)),
959  vector.W);
960  }
961 
962  /// <summary>
963  /// Transforms a 4D vector by the given <see cref="SiliconStudio.Core.Mathematics.Quaternion"/> rotation.
964  /// </summary>
965  /// <param name="vector">The vector to rotate.</param>
966  /// <param name="rotation">The <see cref="SiliconStudio.Core.Mathematics.Quaternion"/> rotation to apply.</param>
967  /// <returns>The transformed <see cref="SiliconStudio.Core.Mathematics.Vector4"/>.</returns>
968  public static Vector4 Transform(Vector4 vector, Quaternion rotation)
969  {
970  Vector4 result;
971  Transform(ref vector, ref rotation, out result);
972  return result;
973  }
974 
975  /// <summary>
976  /// Transforms an array of vectors by the given <see cref="SiliconStudio.Core.Mathematics.Quaternion"/> rotation.
977  /// </summary>
978  /// <param name="source">The array of vectors to transform.</param>
979  /// <param name="rotation">The <see cref="SiliconStudio.Core.Mathematics.Quaternion"/> rotation to apply.</param>
980  /// <param name="destination">The array for which the transformed vectors are stored.
981  /// This array may be the same array as <paramref name="source"/>.</param>
982  /// <exception cref="ArgumentNullException">Thrown when <paramref name="source"/> or <paramref name="destination"/> is <c>null</c>.</exception>
983  /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="destination"/> is shorter in length than <paramref name="source"/>.</exception>
984  public static void Transform(Vector4[] source, ref Quaternion rotation, Vector4[] destination)
985  {
986  if (source == null)
987  throw new ArgumentNullException("source");
988  if (destination == null)
989  throw new ArgumentNullException("destination");
990  if (destination.Length < source.Length)
991  throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array.");
992 
993  float x = rotation.X + rotation.X;
994  float y = rotation.Y + rotation.Y;
995  float z = rotation.Z + rotation.Z;
996  float wx = rotation.W * x;
997  float wy = rotation.W * y;
998  float wz = rotation.W * z;
999  float xx = rotation.X * x;
1000  float xy = rotation.X * y;
1001  float xz = rotation.X * z;
1002  float yy = rotation.Y * y;
1003  float yz = rotation.Y * z;
1004  float zz = rotation.Z * z;
1005 
1006  float num1 = ((1.0f - yy) - zz);
1007  float num2 = (xy - wz);
1008  float num3 = (xz + wy);
1009  float num4 = (xy + wz);
1010  float num5 = ((1.0f - xx) - zz);
1011  float num6 = (yz - wx);
1012  float num7 = (xz - wy);
1013  float num8 = (yz + wx);
1014  float num9 = ((1.0f - xx) - yy);
1015 
1016  for (int i = 0; i < source.Length; ++i)
1017  {
1018  destination[i] = new Vector4(
1019  ((source[i].X * num1) + (source[i].Y * num2)) + (source[i].Z * num3),
1020  ((source[i].X * num4) + (source[i].Y * num5)) + (source[i].Z * num6),
1021  ((source[i].X * num7) + (source[i].Y * num8)) + (source[i].Z * num9),
1022  source[i].W);
1023  }
1024  }
1025 
1026  /// <summary>
1027  /// Transforms a 4D vector by the given <see cref="SiliconStudio.Core.Mathematics.Matrix"/>.
1028  /// </summary>
1029  /// <param name="vector">The source vector.</param>
1030  /// <param name="transform">The transformation <see cref="SiliconStudio.Core.Mathematics.Matrix"/>.</param>
1031  /// <param name="result">When the method completes, contains the transformed <see cref="SiliconStudio.Core.Mathematics.Vector4"/>.</param>
1032  public static void Transform(ref Vector4 vector, ref Matrix transform, out Vector4 result)
1033  {
1034  result = new Vector4(
1035  (vector.X * transform.M11) + (vector.Y * transform.M21) + (vector.Z * transform.M31) + (vector.W * transform.M41),
1036  (vector.X * transform.M12) + (vector.Y * transform.M22) + (vector.Z * transform.M32) + (vector.W * transform.M42),
1037  (vector.X * transform.M13) + (vector.Y * transform.M23) + (vector.Z * transform.M33) + (vector.W * transform.M43),
1038  (vector.X * transform.M14) + (vector.Y * transform.M24) + (vector.Z * transform.M34) + (vector.W * transform.M44));
1039  }
1040 
1041  /// <summary>
1042  /// Transforms a 4D vector by the given <see cref="SiliconStudio.Core.Mathematics.Matrix"/>.
1043  /// </summary>
1044  /// <param name="vector">The source vector.</param>
1045  /// <param name="transform">The transformation <see cref="SiliconStudio.Core.Mathematics.Matrix"/>.</param>
1046  /// <returns>The transformed <see cref="SiliconStudio.Core.Mathematics.Vector4"/>.</returns>
1047  public static Vector4 Transform(Vector4 vector, Matrix transform)
1048  {
1049  Vector4 result;
1050  Transform(ref vector, ref transform, out result);
1051  return result;
1052  }
1053 
1054  /// <summary>
1055  /// Transforms an array of 4D vectors by the given <see cref="SiliconStudio.Core.Mathematics.Matrix"/>.
1056  /// </summary>
1057  /// <param name="source">The array of vectors to transform.</param>
1058  /// <param name="transform">The transformation <see cref="SiliconStudio.Core.Mathematics.Matrix"/>.</param>
1059  /// <param name="destination">The array for which the transformed vectors are stored.
1060  /// This array may be the same array as <paramref name="source"/>.</param>
1061  /// <exception cref="ArgumentNullException">Thrown when <paramref name="source"/> or <paramref name="destination"/> is <c>null</c>.</exception>
1062  /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="destination"/> is shorter in length than <paramref name="source"/>.</exception>
1063  public static void Transform(Vector4[] source, ref Matrix transform, Vector4[] destination)
1064  {
1065  if (source == null)
1066  throw new ArgumentNullException("source");
1067  if (destination == null)
1068  throw new ArgumentNullException("destination");
1069  if (destination.Length < source.Length)
1070  throw new ArgumentOutOfRangeException("destination", "The destination array must be of same length or larger length than the source array.");
1071 
1072  for (int i = 0; i < source.Length; ++i)
1073  {
1074  Transform(ref source[i], ref transform, out destination[i]);
1075  }
1076  }
1077 
1078  /// <summary>
1079  /// Adds two vectors.
1080  /// </summary>
1081  /// <param name="left">The first vector to add.</param>
1082  /// <param name="right">The second vector to add.</param>
1083  /// <returns>The sum of the two vectors.</returns>
1084  public static Vector4 operator +(Vector4 left, Vector4 right)
1085  {
1086  return new Vector4(left.X + right.X, left.Y + right.Y, left.Z + right.Z, left.W + right.W);
1087  }
1088 
1089  /// <summary>
1090  /// Assert a vector (return it unchanged).
1091  /// </summary>
1092  /// <param name="value">The vector to assert (unchange).</param>
1093  /// <returns>The asserted (unchanged) vector.</returns>
1094  public static Vector4 operator +(Vector4 value)
1095  {
1096  return value;
1097  }
1098 
1099  /// <summary>
1100  /// Subtracts two vectors.
1101  /// </summary>
1102  /// <param name="left">The first vector to subtract.</param>
1103  /// <param name="right">The second vector to subtract.</param>
1104  /// <returns>The difference of the two vectors.</returns>
1105  public static Vector4 operator -(Vector4 left, Vector4 right)
1106  {
1107  return new Vector4(left.X - right.X, left.Y - right.Y, left.Z - right.Z, left.W - right.W);
1108  }
1109 
1110  /// <summary>
1111  /// Reverses the direction of a given vector.
1112  /// </summary>
1113  /// <param name="value">The vector to negate.</param>
1114  /// <returns>A vector facing in the opposite direction.</returns>
1115  public static Vector4 operator -(Vector4 value)
1116  {
1117  return new Vector4(-value.X, -value.Y, -value.Z, -value.W);
1118  }
1119 
1120  /// <summary>
1121  /// Scales a vector by the given value.
1122  /// </summary>
1123  /// <param name="value">The vector to scale.</param>
1124  /// <param name="scale">The amount by which to scale the vector.</param>
1125  /// <returns>The scaled vector.</returns>
1126  public static Vector4 operator *(float scale, Vector4 value)
1127  {
1128  return new Vector4(value.X * scale, value.Y * scale, value.Z * scale, value.W * scale);
1129  }
1130 
1131  /// <summary>
1132  /// Scales a vector by the given value.
1133  /// </summary>
1134  /// <param name="value">The vector to scale.</param>
1135  /// <param name="scale">The amount by which to scale the vector.</param>
1136  /// <returns>The scaled vector.</returns>
1137  public static Vector4 operator *(Vector4 value, float scale)
1138  {
1139  return new Vector4(value.X * scale, value.Y * scale, value.Z * scale, value.W * scale);
1140  }
1141 
1142  /// <summary>
1143  /// Scales a vector by the given value.
1144  /// </summary>
1145  /// <param name="value">The vector to scale.</param>
1146  /// <param name="scale">The amount by which to scale the vector.</param>
1147  /// <returns>The scaled vector.</returns>
1148  public static Vector4 operator /(Vector4 value, float scale)
1149  {
1150  return new Vector4(value.X / scale, value.Y / scale, value.Z / scale, value.W / scale);
1151  }
1152 
1153  /// <summary>
1154  /// Tests for equality between two objects.
1155  /// </summary>
1156  /// <param name="left">The first value to compare.</param>
1157  /// <param name="right">The second value to compare.</param>
1158  /// <returns><c>true</c> if <paramref name="left"/> has the same value as <paramref name="right"/>; otherwise, <c>false</c>.</returns>
1159  public static bool operator ==(Vector4 left, Vector4 right)
1160  {
1161  return left.Equals(right);
1162  }
1163 
1164  /// <summary>
1165  /// Tests for inequality between two objects.
1166  /// </summary>
1167  /// <param name="left">The first value to compare.</param>
1168  /// <param name="right">The second value to compare.</param>
1169  /// <returns><c>true</c> if <paramref name="left"/> has a different value than <paramref name="right"/>; otherwise, <c>false</c>.</returns>
1170  public static bool operator !=(Vector4 left, Vector4 right)
1171  {
1172  return !left.Equals(right);
1173  }
1174 
1175  /// <summary>
1176  /// Performs an explicit conversion from <see cref="SiliconStudio.Core.Mathematics.Vector4"/> to <see cref="SiliconStudio.Core.Mathematics.Vector2"/>.
1177  /// </summary>
1178  /// <param name="value">The value.</param>
1179  /// <returns>The result of the conversion.</returns>
1180  public static explicit operator Vector2(Vector4 value)
1181  {
1182  return new Vector2(value.X, value.Y);
1183  }
1184 
1185  /// <summary>
1186  /// Performs an explicit conversion from <see cref="SiliconStudio.Core.Mathematics.Vector4"/> to <see cref="SiliconStudio.Core.Mathematics.Vector3"/>.
1187  /// </summary>
1188  /// <param name="value">The value.</param>
1189  /// <returns>The result of the conversion.</returns>
1190  public static explicit operator Vector3(Vector4 value)
1191  {
1192  return new Vector3(value.X, value.Y, value.Z);
1193  }
1194 
1195  /// <summary>
1196  /// Returns a <see cref="System.String"/> that represents this instance.
1197  /// </summary>
1198  /// <returns>
1199  /// A <see cref="System.String"/> that represents this instance.
1200  /// </returns>
1201  public override string ToString()
1202  {
1203  return string.Format(CultureInfo.CurrentCulture, "X:{0} Y:{1} Z:{2} W:{3}", X, Y, Z, W);
1204  }
1205 
1206  /// <summary>
1207  /// Returns a <see cref="System.String"/> that represents this instance.
1208  /// </summary>
1209  /// <param name="format">The format.</param>
1210  /// <returns>
1211  /// A <see cref="System.String"/> that represents this instance.
1212  /// </returns>
1213  public string ToString(string format)
1214  {
1215  if (format == null)
1216  return ToString();
1217 
1218  return string.Format(CultureInfo.CurrentCulture, "X:{0} Y:{1} Z:{2} W:{3}", X.ToString(format, CultureInfo.CurrentCulture),
1219  Y.ToString(format, CultureInfo.CurrentCulture), Z.ToString(format, CultureInfo.CurrentCulture), W.ToString(format, CultureInfo.CurrentCulture));
1220  }
1221 
1222  /// <summary>
1223  /// Returns a <see cref="System.String"/> that represents this instance.
1224  /// </summary>
1225  /// <param name="formatProvider">The format provider.</param>
1226  /// <returns>
1227  /// A <see cref="System.String"/> that represents this instance.
1228  /// </returns>
1229  public string ToString(IFormatProvider formatProvider)
1230  {
1231  return string.Format(formatProvider, "X:{0} Y:{1} Z:{2} W:{3}", X, Y, Z, W);
1232  }
1233 
1234  /// <summary>
1235  /// Returns a <see cref="System.String"/> that represents this instance.
1236  /// </summary>
1237  /// <param name="format">The format.</param>
1238  /// <param name="formatProvider">The format provider.</param>
1239  /// <returns>
1240  /// A <see cref="System.String"/> that represents this instance.
1241  /// </returns>
1242  public string ToString(string format, IFormatProvider formatProvider)
1243  {
1244  if (format == null)
1245  ToString(formatProvider);
1246 
1247  return string.Format(formatProvider, "X:{0} Y:{1} Z:{2} W:{3}", X.ToString(format, formatProvider),
1248  Y.ToString(format, formatProvider), Z.ToString(format, formatProvider), W.ToString(format, formatProvider));
1249  }
1250 
1251  /// <summary>
1252  /// Returns a hash code for this instance.
1253  /// </summary>
1254  /// <returns>
1255  /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
1256  /// </returns>
1257  public override int GetHashCode()
1258  {
1259  return X.GetHashCode() + Y.GetHashCode() + Z.GetHashCode() + W.GetHashCode();
1260  }
1261 
1262  /// <summary>
1263  /// Determines whether the specified <see cref="SiliconStudio.Core.Mathematics.Vector4"/> is equal to this instance.
1264  /// </summary>
1265  /// <param name="other">The <see cref="SiliconStudio.Core.Mathematics.Vector4"/> to compare with this instance.</param>
1266  /// <returns>
1267  /// <c>true</c> if the specified <see cref="SiliconStudio.Core.Mathematics.Vector4"/> is equal to this instance; otherwise, <c>false</c>.
1268  /// </returns>
1269  public bool Equals(Vector4 other)
1270  {
1271  return ((float)Math.Abs(other.X - X) < MathUtil.ZeroTolerance &&
1272  (float)Math.Abs(other.Y - Y) < MathUtil.ZeroTolerance &&
1273  (float)Math.Abs(other.Z - Z) < MathUtil.ZeroTolerance &&
1274  (float)Math.Abs(other.W - W) < MathUtil.ZeroTolerance);
1275  }
1276 
1277  /// <summary>
1278  /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
1279  /// </summary>
1280  /// <param name="value">The <see cref="System.Object"/> to compare with this instance.</param>
1281  /// <returns>
1282  /// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
1283  /// </returns>
1284  public override bool Equals(object value)
1285  {
1286  if (value == null)
1287  return false;
1288 
1289  if (value.GetType() != GetType())
1290  return false;
1291 
1292  return Equals((Vector4)value);
1293  }
1294 
1295 #if WPFInterop
1296  /// <summary>
1297  /// Performs an implicit conversion from <see cref="SiliconStudio.Core.Mathematics.Vector4"/> to <see cref="System.Windows.Media.Media3D.Point4D"/>.
1298  /// </summary>
1299  /// <param name="value">The value.</param>
1300  /// <returns>The result of the conversion.</returns>
1301  public static implicit operator System.Windows.Media.Media3D.Point4D(Vector4 value)
1302  {
1303  return new System.Windows.Media.Media3D.Point4D(value.X, value.Y, value.Z, value.W);
1304  }
1305 
1306  /// <summary>
1307  /// Performs an explicit conversion from <see cref="System.Windows.Media.Media3D.Point4D"/> to <see cref="SiliconStudio.Core.Mathematics.Vector4"/>.
1308  /// </summary>
1309  /// <param name="value">The value.</param>
1310  /// <returns>The result of the conversion.</returns>
1311  public static explicit operator Vector4(System.Windows.Media.Media3D.Point4D value)
1312  {
1313  return new Vector4((float)value.X, (float)value.Y, (float)value.Z, (float)value.W);
1314  }
1315 #endif
1316 
1317 #if XnaInterop
1318  /// <summary>
1319  /// Performs an implicit conversion from <see cref="SiliconStudio.Core.Mathematics.Vector4"/> to <see cref="Microsoft.Xna.Framework.Vector4"/>.
1320  /// </summary>
1321  /// <param name="value">The value.</param>
1322  /// <returns>The result of the conversion.</returns>
1323  public static implicit operator Microsoft.Xna.Framework.Vector4(Vector4 value)
1324  {
1325  return new Microsoft.Xna.Framework.Vector4(value.X, value.Y, value.Z, value.W);
1326  }
1327 
1328  /// <summary>
1329  /// Performs an implicit conversion from <see cref="Microsoft.Xna.Framework.Vector4"/> to <see cref="SiliconStudio.Core.Mathematics.Vector4"/>.
1330  /// </summary>
1331  /// <param name="value">The value.</param>
1332  /// <returns>The result of the conversion.</returns>
1333  public static implicit operator Vector4(Microsoft.Xna.Framework.Vector4 value)
1334  {
1335  return new Vector4(value.X, value.Y, value.Z, value.W);
1336  }
1337 #endif
1338  }
1339 }
Vector4(Vector2 value, float z, float w)
Initializes a new instance of the SiliconStudio.Core.Mathematics.Vector4 struct.
Definition: Vector4.cs:149
static void Demodulate(ref Vector4 left, ref Vector4 right, out Vector4 result)
Demodulates a vector with another by performing component-wise division.
Definition: Vector4.cs:398
static void SmoothStep(ref Vector4 start, ref Vector4 end, float amount, out Vector4 result)
Performs a cubic interpolation between two vectors.
Definition: Vector4.cs:686
static Vector4 Modulate(Vector4 left, Vector4 right)
Modulates a vector with another by performing component-wise multiplication.
Definition: Vector4.cs:365
void Normalize()
Converts the vector into a unit vector.
Definition: Vector4.cs:248
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
static float Dot(Vector4 left, Vector4 right)
Calculates the dot product of two vectors.
Definition: Vector4.cs:612
FbxDouble3 operator*(double factor, FbxDouble3 vector)
float W
The W component of the vector.
Definition: Vector4.cs:101
static Vector4 SmoothStep(Vector4 start, Vector4 end, float amount)
Performs a cubic interpolation between two vectors.
Definition: Vector4.cs:704
static void Modulate(ref Vector4 left, ref Vector4 right, out Vector4 result)
Modulates a vector with another by performing component-wise multiplication.
Definition: Vector4.cs:354
Represents a two dimensional mathematical vector.
Definition: Vector2.cs:42
static Vector4 Min(Vector4 left, Vector4 right)
Returns a vector containing the smallest components of the specified vectors.
Definition: Vector4.cs:834
float LengthSquared()
Calculates the squared length of the vector.
Definition: Vector4.cs:240
static Vector4 Demodulate(Vector4 left, Vector4 right)
Demodulates a vector with another by performing component-wise division.
Definition: Vector4.cs:409
static void Transform(ref Vector4 vector, ref Matrix transform, out Vector4 result)
Transforms a 4D vector by the given SiliconStudio.Core.Mathematics.Matrix.
Definition: Vector4.cs:1032
float X
The X component of the vector.
Definition: Vector4.cs:83
static void Lerp(ref Vector4 start, ref Vector4 end, float amount, out Vector4 result)
Performs a linear interpolation between two vectors.
Definition: Vector4.cs:652
static void Normalize(ref Vector4 value, out Vector4 result)
Converts the vector into a unit vector.
Definition: Vector4.cs:622
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t y
Definition: DirectXTexP.h:191
static void CatmullRom(ref Vector4 value1, ref Vector4 value2, ref Vector4 value3, ref Vector4 value4, float amount, out Vector4 result)
Performs a Catmull-Rom interpolation using the specified positions.
Definition: Vector4.cs:760
static Vector4 Hermite(Vector4 value1, Vector4 tangent1, Vector4 value2, Vector4 tangent2, float amount)
Performs a Hermite spline interpolation.
Definition: Vector4.cs:744
override int GetHashCode()
Returns a hash code for this instance.
Definition: Vector4.cs:1257
string ToString(IFormatProvider formatProvider)
Returns a System.String that represents this instance.
Definition: Vector4.cs:1229
static Vector4 Multiply(Vector4 value, float scale)
Scales a vector by the given value.
Definition: Vector4.cs:343
static void Dot(ref Vector4 left, ref Vector4 right, out float result)
Calculates the dot product of two vectors.
Definition: Vector4.cs:601
const float ZeroTolerance
The value for which all absolute numbers smaller than are considered equal to zero.
Definition: MathUtil.cs:38
Represents a three dimensional mathematical vector.
Definition: Vector3.cs:42
Vector4(float value)
Initializes a new instance of the SiliconStudio.Core.Mathematics.Vector4 struct.
Definition: Vector4.cs:107
static void Orthonormalize(Vector4[] destination, params Vector4[] source)
Orthonormalizes a list of vectors.
Definition: Vector4.cs:902
static float Distance(Vector4 value1, Vector4 value2)
Calculates the distance between two vectors.
Definition: Vector4.cs:539
static Vector4 Add(Vector4 left, Vector4 right)
Adds two vectors.
Definition: Vector4.cs:299
static Vector4 Clamp(Vector4 value, Vector4 min, Vector4 max)
Restricts a value to be within a specified range.
Definition: Vector4.cs:502
Vector4(Vector3 value, float w)
Initializes a new instance of the SiliconStudio.Core.Mathematics.Vector4 struct.
Definition: Vector4.cs:135
override string ToString()
Returns a System.String that represents this instance.
Definition: Vector4.cs:1201
static void Transform(ref Vector4 vector, ref Quaternion rotation, out Vector4 result)
Transforms a 4D vector by the given SiliconStudio.Core.Mathematics.Quaternion rotation.
Definition: Vector4.cs:940
static Vector4 Normalize(Vector4 value)
Converts the vector into a unit vector.
Definition: Vector4.cs:634
string ToString(string format)
Returns a System.String that represents this instance.
Definition: Vector4.cs:1213
static void Add(ref Vector4 left, ref Vector4 right, out Vector4 result)
Adds two vectors.
Definition: Vector4.cs:288
static void Divide(ref Vector4 value, float scale, out Vector4 result)
Scales a vector by the given value.
Definition: Vector4.cs:376
static void Clamp(ref Vector4 value, ref Vector4 min, ref Vector4 max, out Vector4 result)
Restricts a value to be within a specified range.
Definition: Vector4.cs:474
static void Hermite(ref Vector4 value1, ref Vector4 tangent1, ref Vector4 value2, ref Vector4 tangent2, float amount, out Vector4 result)
Performs a Hermite spline interpolation.
Definition: Vector4.cs:720
static void Transform(Vector4[] source, ref Quaternion rotation, Vector4[] destination)
Transforms an array of vectors by the given SiliconStudio.Core.Mathematics.Quaternion rotation...
Definition: Vector4.cs:984
static void Subtract(ref Vector4 left, ref Vector4 right, out Vector4 result)
Subtracts two vectors.
Definition: Vector4.cs:310
static Vector4 Subtract(Vector4 left, Vector4 right)
Subtracts two vectors.
Definition: Vector4.cs:321
static void Barycentric(ref Vector4 value1, ref Vector4 value2, ref Vector4 value3, float amount1, float amount2, out Vector4 result)
Returns a SiliconStudio.Core.Mathematics.Vector4 containing the 4D Cartesian coordinates of a point s...
Definition: Vector4.cs:443
Represents a four dimensional mathematical vector.
Definition: Vector4.cs:42
static void DistanceSquared(ref Vector4 value1, ref Vector4 value2, out float result)
Calculates the squared distance between two vectors.
Definition: Vector4.cs:562
Represents a four dimensional mathematical quaternion.
Definition: Quaternion.cs:45
float[] ToArray()
Creates an array containing the elements of the vector.
Definition: Vector4.cs:277
static float DistanceSquared(Vector4 value1, Vector4 value2)
Calculates the squared distance between two vectors.
Definition: Vector4.cs:585
static Vector4 CatmullRom(Vector4 value1, Vector4 value2, Vector4 value3, Vector4 value4, float amount)
Performs a Catmull-Rom interpolation using the specified positions.
Definition: Vector4.cs:780
float Length()
Calculates the length of the vector.
Definition: Vector4.cs:227
static void Max(ref Vector4 left, ref Vector4 right, out Vector4 result)
Returns a vector containing the smallest components of the specified vectors.
Definition: Vector4.cs:793
float Y
The Y component of the vector.
Definition: Vector4.cs:89
void Pow(float exponent)
Raises the exponent for each components.
Definition: Vector4.cs:265
Vector4(float[] values)
Initializes a new instance of the SiliconStudio.Core.Mathematics.Vector4 struct.
Definition: Vector4.cs:163
static Vector4 Transform(Vector4 vector, Quaternion rotation)
Transforms a 4D vector by the given SiliconStudio.Core.Mathematics.Quaternion rotation.
Definition: Vector4.cs:968
static void Min(ref Vector4 left, ref Vector4 right, out Vector4 result)
Returns a vector containing the smallest components of the specified vectors.
Definition: Vector4.cs:820
static Vector4 Negate(Vector4 value)
Reverses the direction of a given vector.
Definition: Vector4.cs:429
bool Equals(Vector4 other)
Determines whether the specified SiliconStudio.Core.Mathematics.Vector4 is equal to this instance...
Definition: Vector4.cs:1269
SiliconStudio.Core.Mathematics.Vector3 Vector3
static void Negate(ref Vector4 value, out Vector4 result)
Reverses the direction of a given vector.
Definition: Vector4.cs:419
static void Multiply(ref Vector4 value, float scale, out Vector4 result)
Scales a vector by the given value.
Definition: Vector4.cs:332
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
Definition: DirectXTexP.h:175
static Vector4 Transform(Vector4 vector, Matrix transform)
Transforms a 4D vector by the given SiliconStudio.Core.Mathematics.Matrix.
Definition: Vector4.cs:1047
float Z
The Z component of the vector.
Definition: Vector4.cs:95
Vector4(float x, float y, float z, float w)
Initializes a new instance of the SiliconStudio.Core.Mathematics.Vector4 struct.
Definition: Vector4.cs:122
static void Distance(ref Vector4 value1, ref Vector4 value2, out float result)
Calculates the distance between two vectors.
Definition: Vector4.cs:519
static Vector4 Lerp(Vector4 start, Vector4 end, float amount)
Performs a linear interpolation between two vectors.
Definition: Vector4.cs:672
static Vector4 Max(Vector4 left, Vector4 right)
Returns a vector containing the largest components of the specified vectors.
Definition: Vector4.cs:807
static void Transform(Vector4[] source, ref Matrix transform, Vector4[] destination)
Transforms an array of 4D vectors by the given SiliconStudio.Core.Mathematics.Matrix.
Definition: Vector4.cs:1063
DataStyle
Specifies the style used for textual serialization when an array/list or a dictionary/map must be ser...
Definition: DataStyle.cs:9
override bool Equals(object value)
Determines whether the specified System.Object is equal to this instance.
Definition: Vector4.cs:1284
static Vector4 Barycentric(Vector4 value1, Vector4 value2, Vector4 value3, float amount1, float amount2)
Returns a SiliconStudio.Core.Mathematics.Vector4 containing the 4D Cartesian coordinates of a point s...
Definition: Vector4.cs:460
string ToString(string format, IFormatProvider formatProvider)
Returns a System.String that represents this instance.
Definition: Vector4.cs:1242
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t size_t z
Definition: DirectXTexP.h:191
static Vector4 Divide(Vector4 value, float scale)
Scales a vector by the given value.
Definition: Vector4.cs:387
static void Orthogonalize(Vector4[] destination, params Vector4[] source)
Orthogonalizes a list of vectors.
Definition: Vector4.cs:857
Represents a 4x4 mathematical matrix.
Definition: Matrix.cs:47