Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Int3.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 
33 namespace SiliconStudio.Core.Mathematics
34 {
35  /// <summary>
36  /// Represents a three dimensional mathematical vector.
37  /// </summary>
38  [DataContract("Int3")]
39  [DataStyle(DataStyle.Compact)]
40  [StructLayout(LayoutKind.Sequential, Pack = 4)]
41  public struct Int3 : IEquatable<Int3>, IFormattable
42  {
43  /// <summary>
44  /// The size of the <see cref="Int3"/> type, in bytes.
45  /// </summary>
46  public static readonly int SizeInBytes = Marshal.SizeOf(typeof(Int3));
47 
48  /// <summary>
49  /// A <see cref="Int3"/> with all of its components set to zero.
50  /// </summary>
51  public static readonly Int3 Zero = new Int3();
52 
53  /// <summary>
54  /// The X unit <see cref="Int3"/> (1, 0, 0).
55  /// </summary>
56  public static readonly Int3 UnitX = new Int3(1, 0, 0);
57 
58  /// <summary>
59  /// The Y unit <see cref="Int3"/> (0, 1, 0).
60  /// </summary>
61  public static readonly Int3 UnitY = new Int3(0, 1, 0);
62 
63  /// <summary>
64  /// The Z unit <see cref="Int3"/> (0, 0, 1).
65  /// </summary>
66  public static readonly Int3 UnitZ = new Int3(0, 0, 1);
67 
68  /// <summary>
69  /// A <see cref="Int3"/> with all of its components set to one.
70  /// </summary>
71  public static readonly Int3 One = new Int3(1, 1, 1);
72 
73  /// <summary>
74  /// The X component of the vector.
75  /// </summary>
76  [DataMember(0)]
77  public int X;
78 
79  /// <summary>
80  /// The Y component of the vector.
81  /// </summary>
82  [DataMember(1)]
83  public int Y;
84 
85  /// <summary>
86  /// The Z component of the vector.
87  /// </summary>
88  [DataMember(2)]
89  public int Z;
90 
91  /// <summary>
92  /// Initializes a new instance of the <see cref="Int3"/> struct.
93  /// </summary>
94  /// <param name="value">The value that will be assigned to all components.</param>
95  public Int3(int value)
96  {
97  X = value;
98  Y = value;
99  Z = value;
100  }
101 
102  /// <summary>
103  /// Initializes a new instance of the <see cref="Int3"/> struct.
104  /// </summary>
105  /// <param name="x">Initial value for the X component of the vector.</param>
106  /// <param name="y">Initial value for the Y component of the vector.</param>
107  /// <param name="z">Initial value for the Z component of the vector.</param>
108  public Int3(int x, int y, int z)
109  {
110  X = x;
111  Y = y;
112  Z = z;
113  }
114 
115  /// <summary>
116  /// Initializes a new instance of the <see cref="Int3"/> struct.
117  /// </summary>
118  /// <param name="value">A vector containing the values with which to initialize the X and Y components.</param>
119  /// <param name="z">Initial value for the Z component of the vector.</param>
120  public Int3(Vector2 value, int z)
121  {
122  X = (int)value.X;
123  Y = (int)value.Y;
124  Z = z;
125  }
126 
127  /// <summary>
128  /// Initializes a new instance of the <see cref="Int3"/> struct.
129  /// </summary>
130  /// <param name="values">The values to assign to the X, Y, and Z components of the vector. This must be an array with three elements.</param>
131  /// <exception cref="ArgumentNullException">Thrown when <paramref name="values"/> is <c>null</c>.</exception>
132  /// <exception cref="ArgumentOutOfRangeException">Thrown when <paramref name="values"/> contains more or less than three elements.</exception>
133  public Int3(int[] values)
134  {
135  if (values == null)
136  throw new ArgumentNullException("values");
137  if (values.Length != 3)
138  throw new ArgumentOutOfRangeException("values", "There must be three and only three input values for Int3.");
139 
140  X = values[0];
141  Y = values[1];
142  Z = values[2];
143  }
144 
145  /// <summary>
146  /// Gets a value indicting whether this instance is normalized.
147  /// </summary>
148  public bool IsNormalized
149  {
150  get { return Math.Abs((X * X) + (Y * Y) + (Z * Z) - 1f) < MathUtil.ZeroTolerance; }
151  }
152 
153  /// <summary>
154  /// Gets or sets the component at the specified index.
155  /// </summary>
156  /// <value>The value of the X, Y, or Z component, depending on the index.</value>
157  /// <param name="index">The index of the component to access. Use 0 for the X component, 1 for the Y component, and 2 for the Z component.</param>
158  /// <returns>The value of the component at the specified index.</returns>
159  /// <exception cref="System.ArgumentOutOfRangeException">Thrown when the <paramref name="index"/> is out of the range [0, 2].</exception>
160  public int this[int index]
161  {
162  get
163  {
164  switch (index)
165  {
166  case 0: return X;
167  case 1: return Y;
168  case 2: return Z;
169  }
170 
171  throw new ArgumentOutOfRangeException("index", "Indices for Int3 run from 0 to 2, inclusive.");
172  }
173 
174  set
175  {
176  switch (index)
177  {
178  case 0: X = value; break;
179  case 1: Y = value; break;
180  case 2: Z = value; break;
181  default: throw new ArgumentOutOfRangeException("index", "Indices for Int3 run from 0 to 2, inclusive.");
182  }
183  }
184  }
185 
186  /// <summary>
187  /// Calculates the length of the vector.
188  /// </summary>
189  /// <returns>The length of the vector.</returns>
190  /// <remarks>
191  /// <see cref="Int3.LengthSquared"/> may be preferred when only the relative length is needed
192  /// and speed is of the essence.
193  /// </remarks>
194  public int Length()
195  {
196  return (int)Math.Sqrt((X * X) + (Y * Y) + (Z * Z));
197  }
198 
199  /// <summary>
200  /// Calculates the squared length of the vector.
201  /// </summary>
202  /// <returns>The squared length of the vector.</returns>
203  /// <remarks>
204  /// This method may be preferred to <see cref="Int3.Length"/> when only a relative length is needed
205  /// and speed is of the essence.
206  /// </remarks>
207  public int LengthSquared()
208  {
209  return (X * X) + (Y * Y) + (Z * Z);
210  }
211 
212  /// <summary>
213  /// Converts the vector into a unit vector.
214  /// </summary>
215  public void Normalize()
216  {
217  int length = Length();
218  if (length > MathUtil.ZeroTolerance)
219  {
220  int inv = 1 / length;
221  X *= inv;
222  Y *= inv;
223  Z *= inv;
224  }
225  }
226 
227  /// <summary>
228  /// Raises the exponent for each components.
229  /// </summary>
230  /// <param name="exponent">The exponent.</param>
231  public void Pow(int exponent)
232  {
233  X = (int)Math.Pow(X, exponent);
234  Y = (int)Math.Pow(Y, exponent);
235  Z = (int)Math.Pow(Z, exponent);
236  }
237 
238  /// <summary>
239  /// Creates an array containing the elements of the vector.
240  /// </summary>
241  /// <returns>A three-element array containing the components of the vector.</returns>
242  public int[] ToArray()
243  {
244  return new int[] { X, Y, Z };
245  }
246 
247  /// <summary>
248  /// Adds two vectors.
249  /// </summary>
250  /// <param name="left">The first vector to add.</param>
251  /// <param name="right">The second vector to add.</param>
252  /// <param name="result">When the method completes, contains the sum of the two vectors.</param>
253  public static void Add(ref Int3 left, ref Int3 right, out Int3 result)
254  {
255  result = new Int3(left.X + right.X, left.Y + right.Y, left.Z + right.Z);
256  }
257 
258  /// <summary>
259  /// Adds two vectors.
260  /// </summary>
261  /// <param name="left">The first vector to add.</param>
262  /// <param name="right">The second vector to add.</param>
263  /// <returns>The sum of the two vectors.</returns>
264  public static Int3 Add(Int3 left, Int3 right)
265  {
266  return new Int3(left.X + right.X, left.Y + right.Y, left.Z + right.Z);
267  }
268 
269  /// <summary>
270  /// Subtracts two vectors.
271  /// </summary>
272  /// <param name="left">The first vector to subtract.</param>
273  /// <param name="right">The second vector to subtract.</param>
274  /// <param name="result">When the method completes, contains the difference of the two vectors.</param>
275  public static void Subtract(ref Int3 left, ref Int3 right, out Int3 result)
276  {
277  result = new Int3(left.X - right.X, left.Y - right.Y, left.Z - right.Z);
278  }
279 
280  /// <summary>
281  /// Subtracts two vectors.
282  /// </summary>
283  /// <param name="left">The first vector to subtract.</param>
284  /// <param name="right">The second vector to subtract.</param>
285  /// <returns>The difference of the two vectors.</returns>
286  public static Int3 Subtract(Int3 left, Int3 right)
287  {
288  return new Int3(left.X - right.X, left.Y - right.Y, left.Z - right.Z);
289  }
290 
291  /// <summary>
292  /// Scales a vector by the given value.
293  /// </summary>
294  /// <param name="value">The vector to scale.</param>
295  /// <param name="scale">The amount by which to scale the vector.</param>
296  /// <param name="result">When the method completes, contains the scaled vector.</param>
297  public static void Multiply(ref Int3 value, int scale, out Int3 result)
298  {
299  result = new Int3(value.X * scale, value.Y * scale, value.Z * scale);
300  }
301 
302  /// <summary>
303  /// Scales a vector by the given value.
304  /// </summary>
305  /// <param name="value">The vector to scale.</param>
306  /// <param name="scale">The amount by which to scale the vector.</param>
307  /// <returns>The scaled vector.</returns>
308  public static Int3 Multiply(Int3 value, int scale)
309  {
310  return new Int3(value.X * scale, value.Y * scale, value.Z * scale);
311  }
312 
313  /// <summary>
314  /// Modulates a vector with another by performing component-wise multiplication.
315  /// </summary>
316  /// <param name="left">The first vector to modulate.</param>
317  /// <param name="right">The second vector to modulate.</param>
318  /// <param name="result">When the method completes, contains the modulated vector.</param>
319  public static void Modulate(ref Int3 left, ref Int3 right, out Int3 result)
320  {
321  result = new Int3(left.X * right.X, left.Y * right.Y, left.Z * right.Z);
322  }
323 
324  /// <summary>
325  /// Modulates a vector with another by performing component-wise multiplication.
326  /// </summary>
327  /// <param name="left">The first vector to modulate.</param>
328  /// <param name="right">The second vector to modulate.</param>
329  /// <returns>The modulated vector.</returns>
330  public static Int3 Modulate(Int3 left, Int3 right)
331  {
332  return new Int3(left.X * right.X, left.Y * right.Y, left.Z * right.Z);
333  }
334 
335  /// <summary>
336  /// Scales a vector by the given value.
337  /// </summary>
338  /// <param name="value">The vector to scale.</param>
339  /// <param name="scale">The amount by which to scale the vector.</param>
340  /// <param name="result">When the method completes, contains the scaled vector.</param>
341  public static void Divide(ref Int3 value, int scale, out Int3 result)
342  {
343  result = new Int3(value.X / scale, value.Y / scale, value.Z / scale);
344  }
345 
346  /// <summary>
347  /// Scales a vector by the given value.
348  /// </summary>
349  /// <param name="value">The vector to scale.</param>
350  /// <param name="scale">The amount by which to scale the vector.</param>
351  /// <returns>The scaled vector.</returns>
352  public static Int3 Divide(Int3 value, int scale)
353  {
354  return new Int3(value.X / scale, value.Y / scale, value.Z / scale);
355  }
356 
357  /// <summary>
358  /// Reverses the direction of a given vector.
359  /// </summary>
360  /// <param name="value">The vector to negate.</param>
361  /// <param name="result">When the method completes, contains a vector facing in the opposite direction.</param>
362  public static void Negate(ref Int3 value, out Int3 result)
363  {
364  result = new Int3(-value.X, -value.Y, -value.Z);
365  }
366 
367  /// <summary>
368  /// Reverses the direction of a given vector.
369  /// </summary>
370  /// <param name="value">The vector to negate.</param>
371  /// <returns>A vector facing in the opposite direction.</returns>
372  public static Int3 Negate(Int3 value)
373  {
374  return new Int3(-value.X, -value.Y, -value.Z);
375  }
376 
377  /// <summary>
378  /// Restricts a value to be within a specified range.
379  /// </summary>
380  /// <param name="value">The value to clamp.</param>
381  /// <param name="min">The minimum value.</param>
382  /// <param name="max">The maximum value.</param>
383  /// <param name="result">When the method completes, contains the clamped value.</param>
384  public static void Clamp(ref Int3 value, ref Int3 min, ref Int3 max, out Int3 result)
385  {
386  int x = value.X;
387  x = (x > max.X) ? max.X : x;
388  x = (x < min.X) ? min.X : x;
389 
390  int y = value.Y;
391  y = (y > max.Y) ? max.Y : y;
392  y = (y < min.Y) ? min.Y : y;
393 
394  int z = value.Z;
395  z = (z > max.Z) ? max.Z : z;
396  z = (z < min.Z) ? min.Z : z;
397 
398  result = new Int3(x, y, z);
399  }
400 
401  /// <summary>
402  /// Restricts a value to be within a specified range.
403  /// </summary>
404  /// <param name="value">The value to clamp.</param>
405  /// <param name="min">The minimum value.</param>
406  /// <param name="max">The maximum value.</param>
407  /// <returns>The clamped value.</returns>
408  public static Int3 Clamp(Int3 value, Int3 min, Int3 max)
409  {
410  Int3 result;
411  Clamp(ref value, ref min, ref max, out result);
412  return result;
413  }
414 
415  /// <summary>
416  /// Calculates the dot product of two vectors.
417  /// </summary>
418  /// <param name="left">First source vector.</param>
419  /// <param name="right">Second source vector.</param>
420  /// <param name="result">When the method completes, contains the dot product of the two vectors.</param>
421  public static void Dot(ref Int3 left, ref Int3 right, out int result)
422  {
423  result = (left.X * right.X) + (left.Y * right.Y) + (left.Z * right.Z);
424  }
425 
426  /// <summary>
427  /// Calculates the dot product of two vectors.
428  /// </summary>
429  /// <param name="left">First source vector.</param>
430  /// <param name="right">Second source vector.</param>
431  /// <returns>The dot product of the two vectors.</returns>
432  public static int Dot(Int3 left, Int3 right)
433  {
434  return (left.X * right.X) + (left.Y * right.Y) + (left.Z * right.Z);
435  }
436 
437  /// <summary>
438  /// Converts the vector into a unit vector.
439  /// </summary>
440  /// <param name="value">The vector to normalize.</param>
441  /// <param name="result">When the method completes, contains the normalized vector.</param>
442  public static void Normalize(ref Int3 value, out Int3 result)
443  {
444  result = value;
445  result.Normalize();
446  }
447 
448  /// <summary>
449  /// Converts the vector into a unit vector.
450  /// </summary>
451  /// <param name="value">The vector to normalize.</param>
452  /// <returns>The normalized vector.</returns>
453  public static Int3 Normalize(Int3 value)
454  {
455  value.Normalize();
456  return value;
457  }
458 
459  /// <summary>
460  /// Performs a linear interpolation between two vectors.
461  /// </summary>
462  /// <param name="start">Start vector.</param>
463  /// <param name="end">End vector.</param>
464  /// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
465  /// <param name="result">When the method completes, contains the linear interpolation of the two vectors.</param>
466  /// <remarks>
467  /// This method performs the linear interpolation based on the following formula.
468  /// <code>start + (end - start) * amount</code>
469  /// 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.
470  /// </remarks>
471  public static void Lerp(ref Int3 start, ref Int3 end, float amount, out Int3 result)
472  {
473  result.X = (int)(start.X + ((end.X - start.X) * amount));
474  result.Y = (int)(start.Y + ((end.Y - start.Y) * amount));
475  result.Z = (int)(start.Z + ((end.Z - start.Z) * amount));
476  }
477 
478  /// <summary>
479  /// Performs a linear interpolation between two vectors.
480  /// </summary>
481  /// <param name="start">Start vector.</param>
482  /// <param name="end">End vector.</param>
483  /// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
484  /// <returns>The linear interpolation of the two vectors.</returns>
485  /// <remarks>
486  /// This method performs the linear interpolation based on the following formula.
487  /// <code>start + (end - start) * amount</code>
488  /// 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.
489  /// </remarks>
490  public static Int3 Lerp(Int3 start, Int3 end, float amount)
491  {
492  Int3 result;
493  Lerp(ref start, ref end, amount, out result);
494  return result;
495  }
496 
497  /// <summary>
498  /// Performs a cubic interpolation between two vectors.
499  /// </summary>
500  /// <param name="start">Start vector.</param>
501  /// <param name="end">End vector.</param>
502  /// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
503  /// <param name="result">When the method completes, contains the cubic interpolation of the two vectors.</param>
504  public static void SmoothStep(ref Int3 start, ref Int3 end, float amount, out Int3 result)
505  {
506  amount = (amount > 1) ? 1 : ((amount < 0) ? 0 : amount);
507  amount = (amount * amount) * (3 - (2 * amount));
508 
509  result.X = (int)(start.X + ((end.X - start.X) * amount));
510  result.Y = (int)(start.Y + ((end.Y - start.Y) * amount));
511  result.Z = (int)(start.Z + ((end.Z - start.Z) * amount));
512  }
513 
514  /// <summary>
515  /// Performs a cubic interpolation between two vectors.
516  /// </summary>
517  /// <param name="start">Start vector.</param>
518  /// <param name="end">End vector.</param>
519  /// <param name="amount">Value between 0 and 1 indicating the weight of <paramref name="end"/>.</param>
520  /// <returns>The cubic interpolation of the two vectors.</returns>
521  public static Int3 SmoothStep(Int3 start, Int3 end, float amount)
522  {
523  Int3 result;
524  SmoothStep(ref start, ref end, amount, out result);
525  return result;
526  }
527 
528  /// <summary>
529  /// Returns a vector containing the smallest components of the specified vectors.
530  /// </summary>
531  /// <param name="left">The first source vector.</param>
532  /// <param name="right">The second source vector.</param>
533  /// <param name="result">When the method completes, contains an new vector composed of the largest components of the source vectors.</param>
534  public static void Max(ref Int3 left, ref Int3 right, out Int3 result)
535  {
536  result.X = (left.X > right.X) ? left.X : right.X;
537  result.Y = (left.Y > right.Y) ? left.Y : right.Y;
538  result.Z = (left.Z > right.Z) ? left.Z : right.Z;
539  }
540 
541  /// <summary>
542  /// Returns a vector containing the largest components of the specified vectors.
543  /// </summary>
544  /// <param name="left">The first source vector.</param>
545  /// <param name="right">The second source vector.</param>
546  /// <returns>A vector containing the largest components of the source vectors.</returns>
547  public static Int3 Max(Int3 left, Int3 right)
548  {
549  Int3 result;
550  Max(ref left, ref right, out result);
551  return result;
552  }
553 
554  /// <summary>
555  /// Returns a vector containing the smallest components of the specified vectors.
556  /// </summary>
557  /// <param name="left">The first source vector.</param>
558  /// <param name="right">The second source vector.</param>
559  /// <param name="result">When the method completes, contains an new vector composed of the smallest components of the source vectors.</param>
560  public static void Min(ref Int3 left, ref Int3 right, out Int3 result)
561  {
562  result.X = (left.X < right.X) ? left.X : right.X;
563  result.Y = (left.Y < right.Y) ? left.Y : right.Y;
564  result.Z = (left.Z < right.Z) ? left.Z : right.Z;
565  }
566 
567  /// <summary>
568  /// Returns a vector containing the smallest components of the specified vectors.
569  /// </summary>
570  /// <param name="left">The first source vector.</param>
571  /// <param name="right">The second source vector.</param>
572  /// <returns>A vector containing the smallest components of the source vectors.</returns>
573  public static Int3 Min(Int3 left, Int3 right)
574  {
575  Int3 result;
576  Min(ref left, ref right, out result);
577  return result;
578  }
579 
580  /// <summary>
581  /// Adds two vectors.
582  /// </summary>
583  /// <param name="left">The first vector to add.</param>
584  /// <param name="right">The second vector to add.</param>
585  /// <returns>The sum of the two vectors.</returns>
586  public static Int3 operator +(Int3 left, Int3 right)
587  {
588  return new Int3(left.X + right.X, left.Y + right.Y, left.Z + right.Z);
589  }
590 
591  /// <summary>
592  /// Assert a vector (return it unchanged).
593  /// </summary>
594  /// <param name="value">The vector to assert (unchange).</param>
595  /// <returns>The asserted (unchanged) vector.</returns>
596  public static Int3 operator +(Int3 value)
597  {
598  return value;
599  }
600 
601  /// <summary>
602  /// Subtracts two vectors.
603  /// </summary>
604  /// <param name="left">The first vector to subtract.</param>
605  /// <param name="right">The second vector to subtract.</param>
606  /// <returns>The difference of the two vectors.</returns>
607  public static Int3 operator -(Int3 left, Int3 right)
608  {
609  return new Int3(left.X - right.X, left.Y - right.Y, left.Z - right.Z);
610  }
611 
612  /// <summary>
613  /// Reverses the direction of a given vector.
614  /// </summary>
615  /// <param name="value">The vector to negate.</param>
616  /// <returns>A vector facing in the opposite direction.</returns>
617  public static Int3 operator -(Int3 value)
618  {
619  return new Int3(-value.X, -value.Y, -value.Z);
620  }
621 
622  /// <summary>
623  /// Scales a vector by the given value.
624  /// </summary>
625  /// <param name="value">The vector to scale.</param>
626  /// <param name="scale">The amount by which to scale the vector.</param>
627  /// <returns>The scaled vector.</returns>
628  public static Int3 operator *(float scale, Int3 value)
629  {
630  return new Int3((int)(value.X * scale), (int)(value.Y * scale), (int)(value.Z * scale));
631  }
632 
633  /// <summary>
634  /// Scales a vector by the given value.
635  /// </summary>
636  /// <param name="value">The vector to scale.</param>
637  /// <param name="scale">The amount by which to scale the vector.</param>
638  /// <returns>The scaled vector.</returns>
639  public static Int3 operator *(Int3 value, float scale)
640  {
641  return new Int3((int)(value.X * scale),(int)(value.Y * scale),(int)(value.Z * scale));
642  }
643 
644  /// <summary>
645  /// Scales a vector by the given value.
646  /// </summary>
647  /// <param name="value">The vector to scale.</param>
648  /// <param name="scale">The amount by which to scale the vector.</param>
649  /// <returns>The scaled vector.</returns>
650  public static Int3 operator /(Int3 value, float scale)
651  {
652  return new Int3((int)(value.X / scale), (int)(value.Y / scale), (int)(value.Z / scale));
653  }
654 
655  /// <summary>
656  /// Tests for equality between two objects.
657  /// </summary>
658  /// <param name="left">The first value to compare.</param>
659  /// <param name="right">The second value to compare.</param>
660  /// <returns><c>true</c> if <paramref name="left"/> has the same value as <paramref name="right"/>; otherwise, <c>false</c>.</returns>
661  public static bool operator ==(Int3 left, Int3 right)
662  {
663  return left.Equals(right);
664  }
665 
666  /// <summary>
667  /// Tests for inequality between two objects.
668  /// </summary>
669  /// <param name="left">The first value to compare.</param>
670  /// <param name="right">The second value to compare.</param>
671  /// <returns><c>true</c> if <paramref name="left"/> has a different value than <paramref name="right"/>; otherwise, <c>false</c>.</returns>
672  public static bool operator !=(Int3 left, Int3 right)
673  {
674  return !left.Equals(right);
675  }
676 
677  /// <summary>
678  /// Performs an explicit conversion from <see cref="Int3"/> to <see cref="Vector2"/>.
679  /// </summary>
680  /// <param name="value">The value.</param>
681  /// <returns>The result of the conversion.</returns>
682  public static explicit operator Vector2(Int3 value)
683  {
684  return new Vector2(value.X, value.Y);
685  }
686 
687  /// <summary>
688  /// Performs an explicit conversion from <see cref="Int3"/> to <see cref="Vector4"/>.
689  /// </summary>
690  /// <param name="value">The value.</param>
691  /// <returns>The result of the conversion.</returns>
692  public static explicit operator Vector4(Int3 value)
693  {
694  return new Vector4(value.X, value.Y, value.Z, 0);
695  }
696 
697  /// <summary>
698  /// Returns a <see cref="System.String"/> that represents this instance.
699  /// </summary>
700  /// <returns>
701  /// A <see cref="System.String"/> that represents this instance.
702  /// </returns>
703  public override string ToString()
704  {
705  return string.Format(CultureInfo.CurrentCulture, "X:{0} Y:{1} Z:{2}", X, Y, Z);
706  }
707 
708  /// <summary>
709  /// Returns a <see cref="System.String"/> that represents this instance.
710  /// </summary>
711  /// <param name="format">The format.</param>
712  /// <returns>
713  /// A <see cref="System.String"/> that represents this instance.
714  /// </returns>
715  public string ToString(string format)
716  {
717  if (format == null)
718  return ToString();
719 
720  return string.Format(CultureInfo.CurrentCulture, "X:{0} Y:{1} Z:{2}", X.ToString(format, CultureInfo.CurrentCulture),
721  Y.ToString(format, CultureInfo.CurrentCulture), Z.ToString(format, CultureInfo.CurrentCulture));
722  }
723 
724  /// <summary>
725  /// Returns a <see cref="System.String"/> that represents this instance.
726  /// </summary>
727  /// <param name="formatProvider">The format provider.</param>
728  /// <returns>
729  /// A <see cref="System.String"/> that represents this instance.
730  /// </returns>
731  public string ToString(IFormatProvider formatProvider)
732  {
733  return string.Format(formatProvider, "X:{0} Y:{1} Z:{2}", X, Y, Z);
734  }
735 
736  /// <summary>
737  /// Returns a <see cref="System.String"/> that represents this instance.
738  /// </summary>
739  /// <param name="format">The format.</param>
740  /// <param name="formatProvider">The format provider.</param>
741  /// <returns>
742  /// A <see cref="System.String"/> that represents this instance.
743  /// </returns>
744  public string ToString(string format, IFormatProvider formatProvider)
745  {
746  if (format == null)
747  return ToString(formatProvider);
748 
749  return string.Format(formatProvider, "X:{0} Y:{1} Z:{2}", X.ToString(format, formatProvider),
750  Y.ToString(format, formatProvider), Z.ToString(format, formatProvider));
751  }
752 
753  /// <summary>
754  /// Returns a hash code for this instance.
755  /// </summary>
756  /// <returns>
757  /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
758  /// </returns>
759  public override int GetHashCode()
760  {
761  return X.GetHashCode() + Y.GetHashCode() + Z.GetHashCode();
762  }
763 
764  /// <summary>
765  /// Determines whether the specified <see cref="Int3"/> is equal to this instance.
766  /// </summary>
767  /// <param name="other">The <see cref="Int3"/> to compare with this instance.</param>
768  /// <returns>
769  /// <c>true</c> if the specified <see cref="Int3"/> is equal to this instance; otherwise, <c>false</c>.
770  /// </returns>
771  public bool Equals(Int3 other)
772  {
773  return ((float)Math.Abs(other.X - X) < MathUtil.ZeroTolerance &&
774  (float)Math.Abs(other.Y - Y) < MathUtil.ZeroTolerance &&
775  (float)Math.Abs(other.Z - Z) < MathUtil.ZeroTolerance);
776  }
777 
778  /// <summary>
779  /// Determines whether the specified <see cref="System.Object"/> is equal to this instance.
780  /// </summary>
781  /// <param name="value">The <see cref="System.Object"/> to compare with this instance.</param>
782  /// <returns>
783  /// <c>true</c> if the specified <see cref="System.Object"/> is equal to this instance; otherwise, <c>false</c>.
784  /// </returns>
785  public override bool Equals(object value)
786  {
787  if (value == null)
788  return false;
789 
790  if (value.GetType() != GetType())
791  return false;
792 
793  return Equals((Int3)value);
794  }
795 #if WPFInterop
796  /// <summary>
797  /// Performs an implicit conversion from <see cref="SiliconStudio.Core.Mathematics.Int3"/> to <see cref="System.Windows.Media.Media3D.Int3D"/>.
798  /// </summary>
799  /// <param name="value">The value.</param>
800  /// <returns>The result of the conversion.</returns>
801  public static implicit operator System.Windows.Media.Media3D.Int3D(Int3 value)
802  {
803  return new System.Windows.Media.Media3D.Int3D(value.X, value.Y, value.Z);
804  }
805 
806  /// <summary>
807  /// Performs an explicit conversion from <see cref="System.Windows.Media.Media3D.Int3D"/> to <see cref="SiliconStudio.Core.Mathematics.Int3"/>.
808  /// </summary>
809  /// <param name="value">The value.</param>
810  /// <returns>The result of the conversion.</returns>
811  public static explicit operator Int3(System.Windows.Media.Media3D.Int3D value)
812  {
813  return new Int3((float)value.X, (float)value.Y, (float)value.Z);
814  }
815 #endif
816 
817 #if XnaInterop
818  /// <summary>
819  /// Performs an implicit conversion from <see cref="SiliconStudio.Core.Mathematics.Int3"/> to <see cref="Microsoft.Xna.Framework.Int3"/>.
820  /// </summary>
821  /// <param name="value">The value.</param>
822  /// <returns>The result of the conversion.</returns>
823  public static implicit operator Microsoft.Xna.Framework.Int3(Int3 value)
824  {
825  return new Microsoft.Xna.Framework.Int3(value.X, value.Y, value.Z);
826  }
827 
828  /// <summary>
829  /// Performs an implicit conversion from <see cref="Microsoft.Xna.Framework.Int3"/> to <see cref="SiliconStudio.Core.Mathematics.Int3"/>.
830  /// </summary>
831  /// <param name="value">The value.</param>
832  /// <returns>The result of the conversion.</returns>
833  public static implicit operator Int3(Microsoft.Xna.Framework.Int3 value)
834  {
835  return new Int3(value.X, value.Y, value.Z);
836  }
837 #endif
838  }
839 }
static void Multiply(ref Int3 value, int scale, out Int3 result)
Scales a vector by the given value.
Definition: Int3.cs:297
void Pow(int exponent)
Raises the exponent for each components.
Definition: Int3.cs:231
static void Modulate(ref Int3 left, ref Int3 right, out Int3 result)
Modulates a vector with another by performing component-wise multiplication.
Definition: Int3.cs:319
SiliconStudio.Paradox.Games.Mathematics.Vector2 Vector2
override string ToString()
Returns a System.String that represents this instance.
Definition: Int3.cs:703
static Int3 Modulate(Int3 left, Int3 right)
Modulates a vector with another by performing component-wise multiplication.
Definition: Int3.cs:330
FbxDouble3 operator*(double factor, FbxDouble3 vector)
int X
The X component of the vector.
Definition: Int3.cs:77
Int3(Vector2 value, int z)
Initializes a new instance of the Int3 struct.
Definition: Int3.cs:120
static void Negate(ref Int3 value, out Int3 result)
Reverses the direction of a given vector.
Definition: Int3.cs:362
Represents a two dimensional mathematical vector.
Definition: Vector2.cs:42
static void Subtract(ref Int3 left, ref Int3 right, out Int3 result)
Subtracts two vectors.
Definition: Int3.cs:275
string ToString(IFormatProvider formatProvider)
Returns a System.String that represents this instance.
Definition: Int3.cs:731
Int3(int value)
Initializes a new instance of the Int3 struct.
Definition: Int3.cs:95
static void Divide(ref Int3 value, int scale, out Int3 result)
Scales a vector by the given value.
Definition: Int3.cs:341
static Int3 Add(Int3 left, Int3 right)
Adds two vectors.
Definition: Int3.cs:264
static Int3 Min(Int3 left, Int3 right)
Returns a vector containing the smallest components of the specified vectors.
Definition: Int3.cs:573
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t y
Definition: DirectXTexP.h:191
static void SmoothStep(ref Int3 start, ref Int3 end, float amount, out Int3 result)
Performs a cubic interpolation between two vectors.
Definition: Int3.cs:504
const float ZeroTolerance
The value for which all absolute numbers smaller than are considered equal to zero.
Definition: MathUtil.cs:38
static Int3 Max(Int3 left, Int3 right)
Returns a vector containing the largest components of the specified vectors.
Definition: Int3.cs:547
static void Lerp(ref Int3 start, ref Int3 end, float amount, out Int3 result)
Performs a linear interpolation between two vectors.
Definition: Int3.cs:471
bool Equals(Int3 other)
Determines whether the specified Int3 is equal to this instance.
Definition: Int3.cs:771
int LengthSquared()
Calculates the squared length of the vector.
Definition: Int3.cs:207
int[] ToArray()
Creates an array containing the elements of the vector.
Definition: Int3.cs:242
static int Dot(Int3 left, Int3 right)
Calculates the dot product of two vectors.
Definition: Int3.cs:432
Represents a three dimensional mathematical vector.
Definition: Int3.cs:41
int Y
The Y component of the vector.
Definition: Int3.cs:83
void Normalize()
Converts the vector into a unit vector.
Definition: Int3.cs:215
static void Max(ref Int3 left, ref Int3 right, out Int3 result)
Returns a vector containing the smallest components of the specified vectors.
Definition: Int3.cs:534
static Int3 Subtract(Int3 left, Int3 right)
Subtracts two vectors.
Definition: Int3.cs:286
static Int3 Negate(Int3 value)
Reverses the direction of a given vector.
Definition: Int3.cs:372
Int3(int[] values)
Initializes a new instance of the Int3 struct.
Definition: Int3.cs:133
static void Add(ref Int3 left, ref Int3 right, out Int3 result)
Adds two vectors.
Definition: Int3.cs:253
Represents a four dimensional mathematical vector.
Definition: Vector4.cs:42
string ToString(string format)
Returns a System.String that represents this instance.
Definition: Int3.cs:715
static void Clamp(ref Int3 value, ref Int3 min, ref Int3 max, out Int3 result)
Restricts a value to be within a specified range.
Definition: Int3.cs:384
override int GetHashCode()
Returns a hash code for this instance.
Definition: Int3.cs:759
static Int3 Divide(Int3 value, int scale)
Scales a vector by the given value.
Definition: Int3.cs:352
string ToString(string format, IFormatProvider formatProvider)
Returns a System.String that represents this instance.
Definition: Int3.cs:744
Int3(int x, int y, int z)
Initializes a new instance of the Int3 struct.
Definition: Int3.cs:108
int Z
The Z component of the vector.
Definition: Int3.cs:89
float X
The X component of the vector.
Definition: Vector2.cs:73
static Int3 Normalize(Int3 value)
Converts the vector into a unit vector.
Definition: Int3.cs:453
int Length()
Calculates the length of the vector.
Definition: Int3.cs:194
static void Normalize(ref Int3 value, out Int3 result)
Converts the vector into a unit vector.
Definition: Int3.cs:442
static void Dot(ref Int3 left, ref Int3 right, out int result)
Calculates the dot product of two vectors.
Definition: Int3.cs:421
static Int3 Multiply(Int3 value, int scale)
Scales a vector by the given value.
Definition: Int3.cs:308
_In_ size_t _In_ size_t _In_ DXGI_FORMAT format
Definition: DirectXTexP.h:175
static Int3 SmoothStep(Int3 start, Int3 end, float amount)
Performs a cubic interpolation between two vectors.
Definition: Int3.cs:521
override bool Equals(object value)
Determines whether the specified System.Object is equal to this instance.
Definition: Int3.cs:785
static Int3 Clamp(Int3 value, Int3 min, Int3 max)
Restricts a value to be within a specified range.
Definition: Int3.cs:408
static void Min(ref Int3 left, ref Int3 right, out Int3 result)
Returns a vector containing the smallest components of the specified vectors.
Definition: Int3.cs:560
DataStyle
Specifies the style used for textual serialization when an array/list or a dictionary/map must be ser...
Definition: DataStyle.cs:9
static Int3 Lerp(Int3 start, Int3 end, float amount)
Performs a linear interpolation between two vectors.
Definition: Int3.cs:490
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ float size_t size_t z
Definition: DirectXTexP.h:191