TrueSync
TSVector.cs
1 /* Copyright (C) <2009-2011> <Thorben Linneweber, Jitter Physics>
2 *
3 * This software is provided 'as-is', without any express or implied
4 * warranty. In no event will the authors be held liable for any damages
5 * arising from the use of this software.
6 *
7 * Permission is granted to anyone to use this software for any purpose,
8 * including commercial applications, and to alter it and redistribute it
9 * freely, subject to the following restrictions:
10 *
11 * 1. The origin of this software must not be misrepresented; you must not
12 * claim that you wrote the original software. If you use this software
13 * in a product, an acknowledgment in the product documentation would be
14 * appreciated but is not required.
15 * 2. Altered source versions must be plainly marked as such, and must not be
16 * misrepresented as being the original software.
17 * 3. This notice may not be removed or altered from any source distribution.
18 */
19 
20 using System;
21 using UnityEngine;
22 
23 namespace TrueSync
24 {
28  [Serializable]
29  public struct TSVector
30  {
31 
32  private static FP ZeroEpsilonSq = TSMath.Epsilon;
33  internal static TSVector InternalZero;
34  internal static TSVector Arbitrary;
35 
37  public FP x;
39  public FP y;
41  public FP z;
42 
43  #region Static readonly variables
44  public static readonly TSVector zero;
51  public static readonly TSVector left;
55  public static readonly TSVector right;
59  public static readonly TSVector up;
63  public static readonly TSVector down;
67  public static readonly TSVector back;
71  public static readonly TSVector forward;
75  public static readonly TSVector one;
80  public static readonly TSVector MinValue;
85  public static readonly TSVector MaxValue;
86  #endregion
87 
88  #region Private static constructor
89  static TSVector()
90  {
91  one = new TSVector(1, 1, 1);
92  zero = new TSVector(0, 0, 0);
93  left = new TSVector(-1, 0, 0);
94  right = new TSVector(1, 0, 0);
95  up = new TSVector(0, 1, 0);
96  down = new TSVector(0, -1, 0);
97  back = new TSVector(0, 0, -1);
98  forward = new TSVector(0, 0, 1);
99  MinValue = new TSVector(FP.MinValue);
100  MaxValue = new TSVector(FP.MaxValue);
101  Arbitrary = new TSVector(1, 1, 1);
102  InternalZero = zero;
103  }
104  #endregion
105 
106  public static TSVector Abs(TSVector other) {
107  return new TSVector(FP.Abs(other.x), FP.Abs(other.y), FP.Abs(other.z));
108  }
109 
114  public FP sqrMagnitude {
115  get {
116  return (((this.x * this.x) + (this.y * this.y)) + (this.z * this.z));
117  }
118  }
119 
124  public FP magnitude {
125  get {
126  FP num = ((this.x * this.x) + (this.y * this.y)) + (this.z * this.z);
127  return FP.Sqrt(num);
128  }
129  }
130 
136  get {
137  TSVector result = new TSVector(this.x, this.y, this.z);
138  result.Normalize();
139 
140  return result;
141  }
142  }
143 
150 
151  public TSVector(int x,int y,int z)
152  {
153  this.x = (FP)x;
154  this.y = (FP)y;
155  this.z = (FP)z;
156  }
157 
158  public TSVector(FP x, FP y, FP z)
159  {
160  this.x = x;
161  this.y = y;
162  this.z = z;
163  }
164 
168  public void Scale(TSVector other) {
169  this.x = x * other.x;
170  this.y = y * other.y;
171  this.z = z * other.z;
172  }
173 
180  public void Set(FP x, FP y, FP z)
181  {
182  this.x = x;
183  this.y = y;
184  this.z = z;
185  }
186 
191  public TSVector(FP xyz)
192  {
193  this.x = xyz;
194  this.y = xyz;
195  this.z = xyz;
196  }
197 
198  public static TSVector Lerp(TSVector from, TSVector to, FP percent) {
199  return from + (to - from) * percent;
200  }
201 
206  #region public override string ToString()
207  public override string ToString() {
208  return string.Format("({0:f1}, {1:f1}, {2:f1})", x.AsFloat(), y.AsFloat(), z.AsFloat());
209  }
210  #endregion
211 
217  #region public override bool Equals(object obj)
218  public override bool Equals(object obj)
219  {
220  if (!(obj is TSVector)) return false;
221  TSVector other = (TSVector)obj;
222 
223  return (((x == other.x) && (y == other.y)) && (z == other.z));
224  }
225  #endregion
226 
230  public static TSVector Scale(TSVector vecA, TSVector vecB) {
231  TSVector result;
232  result.x = vecA.x * vecB.x;
233  result.y = vecA.y * vecB.y;
234  result.z = vecA.z * vecB.z;
235 
236  return result;
237  }
238 
245  #region public static bool operator ==(JVector value1, JVector value2)
246  public static bool operator ==(TSVector value1, TSVector value2)
247  {
248  return (((value1.x == value2.x) && (value1.y == value2.y)) && (value1.z == value2.z));
249  }
250  #endregion
251 
258  #region public static bool operator !=(JVector value1, JVector value2)
259  public static bool operator !=(TSVector value1, TSVector value2)
260  {
261  if ((value1.x == value2.x) && (value1.y == value2.y))
262  {
263  return (value1.z != value2.z);
264  }
265  return true;
266  }
267  #endregion
268 
275  #region public static JVector Min(JVector value1, JVector value2)
276 
277  public static TSVector Min(TSVector value1, TSVector value2)
278  {
279  TSVector result;
280  TSVector.Min(ref value1, ref value2, out result);
281  return result;
282  }
283 
290  public static void Min(ref TSVector value1, ref TSVector value2, out TSVector result)
291  {
292  result.x = (value1.x < value2.x) ? value1.x : value2.x;
293  result.y = (value1.y < value2.y) ? value1.y : value2.y;
294  result.z = (value1.z < value2.z) ? value1.z : value2.z;
295  }
296  #endregion
297 
304  #region public static JVector Max(JVector value1, JVector value2)
305  public static TSVector Max(TSVector value1, TSVector value2)
306  {
307  TSVector result;
308  TSVector.Max(ref value1, ref value2, out result);
309  return result;
310  }
311 
312  public static FP Distance(TSVector v1, TSVector v2) {
313  return FP.Sqrt ((v1.x - v2.x) * (v1.x - v2.x) + (v1.y - v2.y) * (v1.y - v2.y) + (v1.z - v2.z) * (v1.z - v2.z));
314  }
315 
322  public static void Max(ref TSVector value1, ref TSVector value2, out TSVector result)
323  {
324  result.x = (value1.x > value2.x) ? value1.x : value2.x;
325  result.y = (value1.y > value2.y) ? value1.y : value2.y;
326  result.z = (value1.z > value2.z) ? value1.z : value2.z;
327  }
328  #endregion
329 
333  #region public void MakeZero()
334  public void MakeZero()
335  {
336  x = FP.Zero;
337  y = FP.Zero;
338  z = FP.Zero;
339  }
340  #endregion
341 
346  #region public bool IsZero()
347  public bool IsZero()
348  {
349  return (this.sqrMagnitude == FP.Zero);
350  }
351 
356  public bool IsNearlyZero()
357  {
358  return (this.sqrMagnitude < ZeroEpsilonSq);
359  }
360  #endregion
361 
368  #region public static JVector Transform(JVector position, JMatrix matrix)
369  public static TSVector Transform(TSVector position, TSMatrix matrix)
370  {
371  TSVector result;
372  TSVector.Transform(ref position, ref matrix, out result);
373  return result;
374  }
375 
382  public static void Transform(ref TSVector position, ref TSMatrix matrix, out TSVector result)
383  {
384  FP num0 = ((position.x * matrix.M11) + (position.y * matrix.M21)) + (position.z * matrix.M31);
385  FP num1 = ((position.x * matrix.M12) + (position.y * matrix.M22)) + (position.z * matrix.M32);
386  FP num2 = ((position.x * matrix.M13) + (position.y * matrix.M23)) + (position.z * matrix.M33);
387 
388  result.x = num0;
389  result.y = num1;
390  result.z = num2;
391  }
392 
399  public static void TransposedTransform(ref TSVector position, ref TSMatrix matrix, out TSVector result)
400  {
401  FP num0 = ((position.x * matrix.M11) + (position.y * matrix.M12)) + (position.z * matrix.M13);
402  FP num1 = ((position.x * matrix.M21) + (position.y * matrix.M22)) + (position.z * matrix.M23);
403  FP num2 = ((position.x * matrix.M31) + (position.y * matrix.M32)) + (position.z * matrix.M33);
404 
405  result.x = num0;
406  result.y = num1;
407  result.z = num2;
408  }
409  #endregion
410 
417  #region public static FP Dot(JVector vector1, JVector vector2)
418  public static FP Dot(TSVector vector1, TSVector vector2)
419  {
420  return TSVector.Dot(ref vector1, ref vector2);
421  }
422 
423 
430  public static FP Dot(ref TSVector vector1, ref TSVector vector2)
431  {
432  return ((vector1.x * vector2.x) + (vector1.y * vector2.y)) + (vector1.z * vector2.z);
433  }
434  #endregion
435 
442  #region public static void Add(JVector value1, JVector value2)
443  public static TSVector Add(TSVector value1, TSVector value2)
444  {
445  TSVector result;
446  TSVector.Add(ref value1, ref value2, out result);
447  return result;
448  }
449 
456  public static void Add(ref TSVector value1, ref TSVector value2, out TSVector result)
457  {
458  FP num0 = value1.x + value2.x;
459  FP num1 = value1.y + value2.y;
460  FP num2 = value1.z + value2.z;
461 
462  result.x = num0;
463  result.y = num1;
464  result.z = num2;
465  }
466  #endregion
467 
474  public static TSVector Divide(TSVector value1, FP scaleFactor) {
475  TSVector result;
476  TSVector.Divide(ref value1, scaleFactor, out result);
477  return result;
478  }
479 
486  public static void Divide(ref TSVector value1, FP scaleFactor, out TSVector result) {
487  result.x = value1.x / scaleFactor;
488  result.y = value1.y / scaleFactor;
489  result.z = value1.z / scaleFactor;
490  }
491 
498  #region public static JVector Subtract(JVector value1, JVector value2)
499  public static TSVector Subtract(TSVector value1, TSVector value2)
500  {
501  TSVector result;
502  TSVector.Subtract(ref value1, ref value2, out result);
503  return result;
504  }
505 
512  public static void Subtract(ref TSVector value1, ref TSVector value2, out TSVector result)
513  {
514  FP num0 = value1.x - value2.x;
515  FP num1 = value1.y - value2.y;
516  FP num2 = value1.z - value2.z;
517 
518  result.x = num0;
519  result.y = num1;
520  result.z = num2;
521  }
522  #endregion
523 
530  #region public static JVector Cross(JVector vector1, JVector vector2)
531  public static TSVector Cross(TSVector vector1, TSVector vector2)
532  {
533  TSVector result;
534  TSVector.Cross(ref vector1, ref vector2, out result);
535  return result;
536  }
537 
544  public static void Cross(ref TSVector vector1, ref TSVector vector2, out TSVector result)
545  {
546  FP num3 = (vector1.y * vector2.z) - (vector1.z * vector2.y);
547  FP num2 = (vector1.z * vector2.x) - (vector1.x * vector2.z);
548  FP num = (vector1.x * vector2.y) - (vector1.y * vector2.x);
549  result.x = num3;
550  result.y = num2;
551  result.z = num;
552  }
553  #endregion
554 
559  #region public override int GetHashCode()
560  public override int GetHashCode()
561  {
562  return x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode();
563  }
564  #endregion
565 
569  #region public static JVector Negate(JVector value)
570  public void Negate()
571  {
572  this.x = -this.x;
573  this.y = -this.y;
574  this.z = -this.z;
575  }
576 
582  public static TSVector Negate(TSVector value)
583  {
584  TSVector result;
585  TSVector.Negate(ref value,out result);
586  return result;
587  }
588 
594  public static void Negate(ref TSVector value, out TSVector result)
595  {
596  FP num0 = -value.x;
597  FP num1 = -value.y;
598  FP num2 = -value.z;
599 
600  result.x = num0;
601  result.y = num1;
602  result.z = num2;
603  }
604  #endregion
605 
611  #region public static JVector Normalize(JVector value)
612  public static TSVector Normalize(TSVector value)
613  {
614  TSVector result;
615  TSVector.Normalize(ref value, out result);
616  return result;
617  }
618 
622  public void Normalize()
623  {
624  FP num2 = ((this.x * this.x) + (this.y * this.y)) + (this.z * this.z);
625  FP num = FP.One / FP.Sqrt(num2);
626  this.x *= num;
627  this.y *= num;
628  this.z *= num;
629  }
630 
636  public static void Normalize(ref TSVector value, out TSVector result)
637  {
638  FP num2 = ((value.x * value.x) + (value.y * value.y)) + (value.z * value.z);
639  FP num = FP.One / FP.Sqrt(num2);
640  result.x = value.x * num;
641  result.y = value.y * num;
642  result.z = value.z * num;
643  }
644  #endregion
645 
646  #region public static void Swap(ref JVector vector1, ref JVector vector2)
647 
653  public static void Swap(ref TSVector vector1, ref TSVector vector2)
654  {
655  FP temp;
656 
657  temp = vector1.x;
658  vector1.x = vector2.x;
659  vector2.x = temp;
660 
661  temp = vector1.y;
662  vector1.y = vector2.y;
663  vector2.y = temp;
664 
665  temp = vector1.z;
666  vector1.z = vector2.z;
667  vector2.z = temp;
668  }
669  #endregion
670 
677  #region public static JVector Multiply(JVector value1, FP scaleFactor)
678  public static TSVector Multiply(TSVector value1, FP scaleFactor)
679  {
680  TSVector result;
681  TSVector.Multiply(ref value1, scaleFactor, out result);
682  return result;
683  }
684 
691  public static void Multiply(ref TSVector value1, FP scaleFactor, out TSVector result)
692  {
693  result.x = value1.x * scaleFactor;
694  result.y = value1.y * scaleFactor;
695  result.z = value1.z * scaleFactor;
696  }
697  #endregion
698 
705  #region public static JVector operator %(JVector value1, JVector value2)
706  public static TSVector operator %(TSVector value1, TSVector value2)
707  {
708  TSVector result; TSVector.Cross(ref value1, ref value2, out result);
709  return result;
710  }
711  #endregion
712 
719  #region public static FP operator *(JVector value1, JVector value2)
720  public static FP operator *(TSVector value1, TSVector value2)
721  {
722  return TSVector.Dot(ref value1, ref value2);
723  }
724  #endregion
725 
732  #region public static JVector operator *(JVector value1, FP value2)
733  public static TSVector operator *(TSVector value1, FP value2)
734  {
735  TSVector result;
736  TSVector.Multiply(ref value1, value2,out result);
737  return result;
738  }
739  #endregion
740 
747  #region public static JVector operator *(FP value1, JVector value2)
748  public static TSVector operator *(FP value1, TSVector value2)
749  {
750  TSVector result;
751  TSVector.Multiply(ref value2, value1, out result);
752  return result;
753  }
754  #endregion
755 
762  #region public static JVector operator -(JVector value1, JVector value2)
763  public static TSVector operator -(TSVector value1, TSVector value2)
764  {
765  TSVector result; TSVector.Subtract(ref value1, ref value2, out result);
766  return result;
767  }
768  #endregion
769 
776  #region public static JVector operator +(JVector value1, JVector value2)
777  public static TSVector operator +(TSVector value1, TSVector value2)
778  {
779  TSVector result; TSVector.Add(ref value1, ref value2, out result);
780  return result;
781  }
782  #endregion
783 
790  public static TSVector operator /(TSVector value1, FP value2) {
791  TSVector result;
792  TSVector.Divide(ref value1, value2, out result);
793  return result;
794  }
795 
796  public static FP Angle(TSVector a, TSVector b) {
797  return FP.Acos(a.normalized * b.normalized) * FP.Rad2Deg;
798  }
799 
800  }
801 
802 }
static void Subtract(ref TSVector value1, ref TSVector value2, out TSVector result)
Subtracts to vectors.
Definition: TSVector.cs:512
static TSVector Multiply(TSVector value1, FP scaleFactor)
Multiply a vector with a factor.
Definition: TSVector.cs:678
FP y
The Y component of the vector.
Definition: TSVector.cs:39
TSVector normalized
Gets a normalized version of the vector.
Definition: TSVector.cs:135
static void Divide(ref TSVector value1, FP scaleFactor, out TSVector result)
Divides a vector by a factor.
Definition: TSVector.cs:486
static readonly TSVector back
A vector with components (0,0,-1);
Definition: TSVector.cs:67
static void TransposedTransform(ref TSVector position, ref TSMatrix matrix, out TSVector result)
Transforms a vector by the transposed of the given Matrix.
Definition: TSVector.cs:399
static void Negate(ref TSVector value, out TSVector result)
Inverses the direction of a vector.
Definition: TSVector.cs:594
TSVector(int x, int y, int z)
Constructor initializing a new instance of the structure
Definition: TSVector.cs:151
static readonly TSVector MinValue
A vector with components (FP.MinValue,FP.MinValue,FP.MinValue);
Definition: TSVector.cs:80
static void Transform(ref TSVector position, ref TSMatrix matrix, out TSVector result)
Transforms a vector by the given matrix.
Definition: TSVector.cs:382
static void Min(ref TSVector value1, ref TSVector value2, out TSVector result)
Gets a vector with the minimum x,y and z values of both vectors.
Definition: TSVector.cs:290
void Negate()
Inverses the direction of the vector.
Definition: TSVector.cs:570
static readonly TSVector MaxValue
A vector with components (FP.MaxValue,FP.MaxValue,FP.MaxValue);
Definition: TSVector.cs:85
static void Add(ref TSVector value1, ref TSVector value2, out TSVector result)
Adds to vectors.
Definition: TSVector.cs:456
static readonly TSVector up
A vector with components (0,1,0);
Definition: TSVector.cs:59
static void Multiply(ref TSVector value1, FP scaleFactor, out TSVector result)
Multiply a vector with a factor.
Definition: TSVector.cs:691
FP magnitude
Gets the length of the vector.
Definition: TSVector.cs:124
A vector structure.
Definition: TSVector.cs:29
void Scale(TSVector other)
Multiplies each component of the vector by the same components of the provided vector.
Definition: TSVector.cs:168
static TSVector operator%(TSVector value1, TSVector value2)
Calculates the cross product of two vectors.
Definition: TSVector.cs:706
static FP Dot(TSVector vector1, TSVector vector2)
Calculates the dot product of two vectors.
Definition: TSVector.cs:418
static FP Dot(ref TSVector vector1, ref TSVector vector2)
Calculates the dot product of both vectors.
Definition: TSVector.cs:430
static readonly TSVector zero
A vector with components (0,0,0);
Definition: TSVector.cs:47
Contains common math operations.
Definition: TSMath.cs:25
FP z
The Z component of the vector.
Definition: TSVector.cs:41
static TSVector operator/(TSVector value1, FP value2)
Divides a vector by a factor.
Definition: TSVector.cs:790
static readonly TSVector one
A vector with components (1,1,1);
Definition: TSVector.cs:75
static bool operator!=(TSVector value1, TSVector value2)
Tests if two JVector are not equal.
Definition: TSVector.cs:259
static void Cross(ref TSVector vector1, ref TSVector vector2, out TSVector result)
The cross product of two vectors.
Definition: TSVector.cs:544
static void Swap(ref TSVector vector1, ref TSVector vector2)
Swaps the components of both vectors.
Definition: TSVector.cs:653
static TSVector operator-(TSVector value1, TSVector value2)
Subtracts two vectors.
Definition: TSVector.cs:763
static TSVector operator+(TSVector value1, TSVector value2)
Adds two vectors.
Definition: TSVector.cs:777
static TSVector Transform(TSVector position, TSMatrix matrix)
Transforms a vector by the given matrix.
Definition: TSVector.cs:369
static readonly TSVector down
A vector with components (0,-1,0);
Definition: TSVector.cs:63
static readonly TSVector right
A vector with components (1,0,0);
Definition: TSVector.cs:55
static FP operator*(TSVector value1, TSVector value2)
Calculates the dot product of two vectors.
Definition: TSVector.cs:720
static readonly TSVector forward
A vector with components (0,0,1);
Definition: TSVector.cs:71
static TSVector Cross(TSVector vector1, TSVector vector2)
The cross product of two vectors.
Definition: TSVector.cs:531
FP sqrMagnitude
Gets the squared length of the vector.
Definition: TSVector.cs:114
bool IsNearlyZero()
Checks if the length of the vector is nearly zero.
Definition: TSVector.cs:356
static TSVector Normalize(TSVector value)
Normalizes the given vector.
Definition: TSVector.cs:612
void Normalize()
Normalizes this vector.
Definition: TSVector.cs:622
static TSVector Divide(TSVector value1, FP scaleFactor)
Divides a vector by a factor.
Definition: TSVector.cs:474
3x3 Matrix.
Definition: TSMatrix.cs:26
FP x
The X component of the vector.
Definition: TSVector.cs:37
static TSVector Add(TSVector value1, TSVector value2)
Adds two vectors.
Definition: TSVector.cs:443
static readonly TSVector left
A vector with components (-1,0,0);
Definition: TSVector.cs:51
bool IsZero()
Checks if the length of the vector is zero.
Definition: TSVector.cs:347
static TSVector Subtract(TSVector value1, TSVector value2)
Subtracts two vectors.
Definition: TSVector.cs:499
static TSVector Min(TSVector value1, TSVector value2)
Gets a vector with the minimum x,y and z values of both vectors.
Definition: TSVector.cs:277
override int GetHashCode()
Gets the hashcode of the vector.
Definition: TSVector.cs:560
override bool Equals(object obj)
Tests if an object is equal to this vector.
Definition: TSVector.cs:218
static void Normalize(ref TSVector value, out TSVector result)
Normalizes the given vector.
Definition: TSVector.cs:636
void MakeZero()
Sets the length of the vector to zero.
Definition: TSVector.cs:334
TSVector(FP xyz)
Constructor initializing a new instance of the structure
Definition: TSVector.cs:191
override string ToString()
Builds a string from the JVector.
Definition: TSVector.cs:207
void Set(FP x, FP y, FP z)
Sets all vector component to specific values.
Definition: TSVector.cs:180
static TSVector Max(TSVector value1, TSVector value2)
Gets a vector with the maximum x,y and z values of both vectors.
Definition: TSVector.cs:305
static TSVector Negate(TSVector value)
Inverses the direction of a vector.
Definition: TSVector.cs:582
static bool operator==(TSVector value1, TSVector value2)
Tests if two JVector are equal.
Definition: TSVector.cs:246
static TSVector Scale(TSVector vecA, TSVector vecB)
Multiplies each component of the vector by the same components of the provided vector.
Definition: TSVector.cs:230
static FP Epsilon
A small value often used to decide if numeric results are zero.
Definition: TSMath.cs:41
static void Max(ref TSVector value1, ref TSVector value2, out TSVector result)
Gets a vector with the maximum x,y and z values of both vectors.
Definition: TSVector.cs:322