| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844 |
- /* Copyright (C) <2009-2011> <Thorben Linneweber, Jitter Physics>
- *
- * This software is provided 'as-is', without any express or implied
- * warranty. In no event will the authors be held liable for any damages
- * arising from the use of this software.
- *
- * Permission is granted to anyone to use this software for any purpose,
- * including commercial applications, and to alter it and redistribute it
- * freely, subject to the following restrictions:
- *
- * 1. The origin of this software must not be misrepresented; you must not
- * claim that you wrote the original software. If you use this software
- * in a product, an acknowledgment in the product documentation would be
- * appreciated but is not required.
- * 2. Altered source versions must be plainly marked as such, and must not be
- * misrepresented as being the original software.
- * 3. This notice may not be removed or altered from any source distribution.
- */
- using System;
- namespace TrueSync
- {
- /// <summary>
- /// A vector structure.
- /// </summary>
- [Serializable]
- public struct TSVector
- {
- private static FP ZeroEpsilonSq = TSMath.Epsilon;
- internal static TSVector InternalZero;
- internal static TSVector Arbitrary;
- /// <summary>The X component of the vector.</summary>
- public FP x;
- /// <summary>The Y component of the vector.</summary>
- public FP y;
- /// <summary>The Z component of the vector.</summary>
- public FP z;
- #region Static readonly variables
- /// <summary>
- /// A vector with components (0,0,0);
- /// </summary>
- public static readonly TSVector zero;
- /// <summary>
- /// A vector with components (-1,0,0);
- /// </summary>
- public static readonly TSVector left;
- /// <summary>
- /// A vector with components (1,0,0);
- /// </summary>
- public static readonly TSVector right;
- /// <summary>
- /// A vector with components (0,1,0);
- /// </summary>
- public static readonly TSVector up;
- /// <summary>
- /// A vector with components (0,-1,0);
- /// </summary>
- public static readonly TSVector down;
- /// <summary>
- /// A vector with components (0,0,-1);
- /// </summary>
- public static readonly TSVector back;
- /// <summary>
- /// A vector with components (0,0,1);
- /// </summary>
- public static readonly TSVector forward;
- /// <summary>
- /// A vector with components (1,1,1);
- /// </summary>
- public static readonly TSVector one;
- /// <summary>
- /// A vector with components
- /// (FP.MinValue,FP.MinValue,FP.MinValue);
- /// </summary>
- public static readonly TSVector MinValue;
- /// <summary>
- /// A vector with components
- /// (FP.MaxValue,FP.MaxValue,FP.MaxValue);
- /// </summary>
- public static readonly TSVector MaxValue;
- #endregion
- #region Private static constructor
- static TSVector()
- {
- one = new TSVector(1, 1, 1);
- zero = new TSVector(0, 0, 0);
- left = new TSVector(-1, 0, 0);
- right = new TSVector(1, 0, 0);
- up = new TSVector(0, 1, 0);
- down = new TSVector(0, -1, 0);
- back = new TSVector(0, 0, -1);
- forward = new TSVector(0, 0, 1);
- MinValue = new TSVector(FP.MinValue);
- MaxValue = new TSVector(FP.MaxValue);
- Arbitrary = new TSVector(1, 1, 1);
- InternalZero = zero;
- }
- #endregion
- public static TSVector Abs(TSVector other) {
- return new TSVector(FP.Abs(other.x), FP.Abs(other.y), FP.Abs(other.z));
- }
- /// <summary>
- /// Gets the squared length of the vector.
- /// </summary>
- /// <returns>Returns the squared length of the vector.</returns>
- public FP sqrMagnitude {
- get {
- return (((this.x * this.x) + (this.y * this.y)) + (this.z * this.z));
- }
- }
- /// <summary>
- /// Gets the length of the vector.
- /// </summary>
- /// <returns>Returns the length of the vector.</returns>
- public FP magnitude {
- get {
- FP num = ((this.x * this.x) + (this.y * this.y)) + (this.z * this.z);
- return FP.Sqrt(num);
- }
- }
- public static TSVector ClampMagnitude(TSVector vector, FP maxLength) {
- return Normalize(vector) * maxLength;
- }
- /// <summary>
- /// Gets a normalized version of the vector.
- /// </summary>
- /// <returns>Returns a normalized version of the vector.</returns>
- public TSVector normalized {
- get {
- TSVector result = new TSVector(this.x, this.y, this.z);
- result.Normalize();
- return result;
- }
- }
- /// <summary>
- /// Constructor initializing a new instance of the structure
- /// </summary>
- /// <param name="x">The X component of the vector.</param>
- /// <param name="y">The Y component of the vector.</param>
- /// <param name="z">The Z component of the vector.</param>
- public TSVector(int x,int y,int z)
- {
- this.x = (FP)x;
- this.y = (FP)y;
- this.z = (FP)z;
- }
- public TSVector(FP x, FP y, FP z)
- {
- this.x = x;
- this.y = y;
- this.z = z;
- }
- /// <summary>
- /// Multiplies each component of the vector by the same components of the provided vector.
- /// </summary>
- public void Scale(TSVector other) {
- this.x = x * other.x;
- this.y = y * other.y;
- this.z = z * other.z;
- }
- /// <summary>
- /// Sets all vector component to specific values.
- /// </summary>
- /// <param name="x">The X component of the vector.</param>
- /// <param name="y">The Y component of the vector.</param>
- /// <param name="z">The Z component of the vector.</param>
- public void Set(FP x, FP y, FP z)
- {
- this.x = x;
- this.y = y;
- this.z = z;
- }
- /// <summary>
- /// Constructor initializing a new instance of the structure
- /// </summary>
- /// <param name="xyz">All components of the vector are set to xyz</param>
- public TSVector(FP xyz)
- {
- this.x = xyz;
- this.y = xyz;
- this.z = xyz;
- }
- public static TSVector Lerp(TSVector from, TSVector to, FP percent) {
- return from + (to - from) * percent;
- }
- /// <summary>
- /// Builds a string from the JVector.
- /// </summary>
- /// <returns>A string containing all three components.</returns>
- #region public override string ToString()
- public override string ToString() {
- return string.Format("({0:f1}, {1:f1}, {2:f1})", x.AsFloat(), y.AsFloat(), z.AsFloat());
- }
- #endregion
- /// <summary>
- /// Tests if an object is equal to this vector.
- /// </summary>
- /// <param name="obj">The object to test.</param>
- /// <returns>Returns true if they are euqal, otherwise false.</returns>
- #region public override bool Equals(object obj)
- public override bool Equals(object obj)
- {
- if (!(obj is TSVector)) return false;
- TSVector other = (TSVector)obj;
- return (((x == other.x) && (y == other.y)) && (z == other.z));
- }
- #endregion
- /// <summary>
- /// Multiplies each component of the vector by the same components of the provided vector.
- /// </summary>
- public static TSVector Scale(TSVector vecA, TSVector vecB) {
- TSVector result;
- result.x = vecA.x * vecB.x;
- result.y = vecA.y * vecB.y;
- result.z = vecA.z * vecB.z;
- return result;
- }
- /// <summary>
- /// Tests if two JVector are equal.
- /// </summary>
- /// <param name="value1">The first value.</param>
- /// <param name="value2">The second value.</param>
- /// <returns>Returns true if both values are equal, otherwise false.</returns>
- #region public static bool operator ==(JVector value1, JVector value2)
- public static bool operator ==(TSVector value1, TSVector value2)
- {
- return (((value1.x == value2.x) && (value1.y == value2.y)) && (value1.z == value2.z));
- }
- #endregion
- /// <summary>
- /// Tests if two JVector are not equal.
- /// </summary>
- /// <param name="value1">The first value.</param>
- /// <param name="value2">The second value.</param>
- /// <returns>Returns false if both values are equal, otherwise true.</returns>
- #region public static bool operator !=(JVector value1, JVector value2)
- public static bool operator !=(TSVector value1, TSVector value2)
- {
- if ((value1.x == value2.x) && (value1.y == value2.y))
- {
- return (value1.z != value2.z);
- }
- return true;
- }
- #endregion
- /// <summary>
- /// Gets a vector with the minimum x,y and z values of both vectors.
- /// </summary>
- /// <param name="value1">The first value.</param>
- /// <param name="value2">The second value.</param>
- /// <returns>A vector with the minimum x,y and z values of both vectors.</returns>
- #region public static JVector Min(JVector value1, JVector value2)
- public static TSVector Min(TSVector value1, TSVector value2)
- {
- TSVector result;
- TSVector.Min(ref value1, ref value2, out result);
- return result;
- }
- /// <summary>
- /// Gets a vector with the minimum x,y and z values of both vectors.
- /// </summary>
- /// <param name="value1">The first value.</param>
- /// <param name="value2">The second value.</param>
- /// <param name="result">A vector with the minimum x,y and z values of both vectors.</param>
- public static void Min(ref TSVector value1, ref TSVector value2, out TSVector result)
- {
- result.x = (value1.x < value2.x) ? value1.x : value2.x;
- result.y = (value1.y < value2.y) ? value1.y : value2.y;
- result.z = (value1.z < value2.z) ? value1.z : value2.z;
- }
- #endregion
- /// <summary>
- /// Gets a vector with the maximum x,y and z values of both vectors.
- /// </summary>
- /// <param name="value1">The first value.</param>
- /// <param name="value2">The second value.</param>
- /// <returns>A vector with the maximum x,y and z values of both vectors.</returns>
- #region public static JVector Max(JVector value1, JVector value2)
- public static TSVector Max(TSVector value1, TSVector value2)
- {
- TSVector result;
- TSVector.Max(ref value1, ref value2, out result);
- return result;
- }
-
- public static FP Distance(TSVector v1, TSVector v2) {
- 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));
- }
- /// <summary>
- /// Gets a vector with the maximum x,y and z values of both vectors.
- /// </summary>
- /// <param name="value1">The first value.</param>
- /// <param name="value2">The second value.</param>
- /// <param name="result">A vector with the maximum x,y and z values of both vectors.</param>
- public static void Max(ref TSVector value1, ref TSVector value2, out TSVector result)
- {
- result.x = (value1.x > value2.x) ? value1.x : value2.x;
- result.y = (value1.y > value2.y) ? value1.y : value2.y;
- result.z = (value1.z > value2.z) ? value1.z : value2.z;
- }
- #endregion
- /// <summary>
- /// Sets the length of the vector to zero.
- /// </summary>
- #region public void MakeZero()
- public void MakeZero()
- {
- x = FP.Zero;
- y = FP.Zero;
- z = FP.Zero;
- }
- #endregion
- /// <summary>
- /// Checks if the length of the vector is zero.
- /// </summary>
- /// <returns>Returns true if the vector is zero, otherwise false.</returns>
- #region public bool IsZero()
- public bool IsZero()
- {
- return (this.sqrMagnitude == FP.Zero);
- }
- /// <summary>
- /// Checks if the length of the vector is nearly zero.
- /// </summary>
- /// <returns>Returns true if the vector is nearly zero, otherwise false.</returns>
- public bool IsNearlyZero()
- {
- return (this.sqrMagnitude < ZeroEpsilonSq);
- }
- #endregion
- /// <summary>
- /// Transforms a vector by the given matrix.
- /// </summary>
- /// <param name="position">The vector to transform.</param>
- /// <param name="matrix">The transform matrix.</param>
- /// <returns>The transformed vector.</returns>
- #region public static JVector Transform(JVector position, JMatrix matrix)
- public static TSVector Transform(TSVector position, TSMatrix matrix)
- {
- TSVector result;
- TSVector.Transform(ref position, ref matrix, out result);
- return result;
- }
- /// <summary>
- /// Transforms a vector by the given matrix.
- /// </summary>
- /// <param name="position">The vector to transform.</param>
- /// <param name="matrix">The transform matrix.</param>
- /// <param name="result">The transformed vector.</param>
- public static void Transform(ref TSVector position, ref TSMatrix matrix, out TSVector result)
- {
- FP num0 = ((position.x * matrix.M11) + (position.y * matrix.M21)) + (position.z * matrix.M31);
- FP num1 = ((position.x * matrix.M12) + (position.y * matrix.M22)) + (position.z * matrix.M32);
- FP num2 = ((position.x * matrix.M13) + (position.y * matrix.M23)) + (position.z * matrix.M33);
- result.x = num0;
- result.y = num1;
- result.z = num2;
- }
- /// <summary>
- /// Transforms a vector by the transposed of the given Matrix.
- /// </summary>
- /// <param name="position">The vector to transform.</param>
- /// <param name="matrix">The transform matrix.</param>
- /// <param name="result">The transformed vector.</param>
- public static void TransposedTransform(ref TSVector position, ref TSMatrix matrix, out TSVector result)
- {
- FP num0 = ((position.x * matrix.M11) + (position.y * matrix.M12)) + (position.z * matrix.M13);
- FP num1 = ((position.x * matrix.M21) + (position.y * matrix.M22)) + (position.z * matrix.M23);
- FP num2 = ((position.x * matrix.M31) + (position.y * matrix.M32)) + (position.z * matrix.M33);
- result.x = num0;
- result.y = num1;
- result.z = num2;
- }
- #endregion
- /// <summary>
- /// Calculates the dot product of two vectors.
- /// </summary>
- /// <param name="vector1">The first vector.</param>
- /// <param name="vector2">The second vector.</param>
- /// <returns>Returns the dot product of both vectors.</returns>
- #region public static FP Dot(JVector vector1, JVector vector2)
- public static FP Dot(TSVector vector1, TSVector vector2)
- {
- return TSVector.Dot(ref vector1, ref vector2);
- }
- /// <summary>
- /// Calculates the dot product of both vectors.
- /// </summary>
- /// <param name="vector1">The first vector.</param>
- /// <param name="vector2">The second vector.</param>
- /// <returns>Returns the dot product of both vectors.</returns>
- public static FP Dot(ref TSVector vector1, ref TSVector vector2)
- {
- return ((vector1.x * vector2.x) + (vector1.y * vector2.y)) + (vector1.z * vector2.z);
- }
- #endregion
- // Projects a vector onto another vector.
- public static TSVector Project(TSVector vector, TSVector onNormal)
- {
- FP sqrtMag = Dot(onNormal, onNormal);
- if (sqrtMag < TSMath.Epsilon)
- return zero;
- else
- return onNormal * Dot(vector, onNormal) / sqrtMag;
- }
- // Projects a vector onto a plane defined by a normal orthogonal to the plane.
- public static TSVector ProjectOnPlane(TSVector vector, TSVector planeNormal)
- {
- return vector - Project(vector, planeNormal);
- }
- // Returns the angle in degrees between /from/ and /to/. This is always the smallest
- public static FP Angle(TSVector from, TSVector to)
- {
- return TSMath.Acos(TSMath.Clamp(Dot(from.normalized, to.normalized), -FP.ONE, FP.ONE)) * TSMath.Rad2Deg;
- }
- // The smaller of the two possible angles between the two vectors is returned, therefore the result will never be greater than 180 degrees or smaller than -180 degrees.
- // If you imagine the from and to vectors as lines on a piece of paper, both originating from the same point, then the /axis/ vector would point up out of the paper.
- // The measured angle between the two vectors would be positive in a clockwise direction and negative in an anti-clockwise direction.
- public static FP SignedAngle(TSVector from, TSVector to, TSVector axis)
- {
- TSVector fromNorm = from.normalized, toNorm = to.normalized;
- FP unsignedAngle = TSMath.Acos(TSMath.Clamp(Dot(fromNorm, toNorm), -FP.ONE, FP.ONE)) * TSMath.Rad2Deg;
- FP sign = TSMath.Sign(Dot(axis, Cross(fromNorm, toNorm)));
- return unsignedAngle * sign;
- }
- /// <summary>
- /// Adds two vectors.
- /// </summary>
- /// <param name="value1">The first vector.</param>
- /// <param name="value2">The second vector.</param>
- /// <returns>The sum of both vectors.</returns>
- #region public static void Add(JVector value1, JVector value2)
- public static TSVector Add(TSVector value1, TSVector value2)
- {
- TSVector result;
- TSVector.Add(ref value1, ref value2, out result);
- return result;
- }
- /// <summary>
- /// Adds to vectors.
- /// </summary>
- /// <param name="value1">The first vector.</param>
- /// <param name="value2">The second vector.</param>
- /// <param name="result">The sum of both vectors.</param>
- public static void Add(ref TSVector value1, ref TSVector value2, out TSVector result)
- {
- FP num0 = value1.x + value2.x;
- FP num1 = value1.y + value2.y;
- FP num2 = value1.z + value2.z;
- result.x = num0;
- result.y = num1;
- result.z = num2;
- }
- #endregion
- /// <summary>
- /// Divides a vector by a factor.
- /// </summary>
- /// <param name="value1">The vector to divide.</param>
- /// <param name="scaleFactor">The scale factor.</param>
- /// <returns>Returns the scaled vector.</returns>
- public static TSVector Divide(TSVector value1, FP scaleFactor) {
- TSVector result;
- TSVector.Divide(ref value1, scaleFactor, out result);
- return result;
- }
- /// <summary>
- /// Divides a vector by a factor.
- /// </summary>
- /// <param name="value1">The vector to divide.</param>
- /// <param name="scaleFactor">The scale factor.</param>
- /// <param name="result">Returns the scaled vector.</param>
- public static void Divide(ref TSVector value1, FP scaleFactor, out TSVector result) {
- result.x = value1.x / scaleFactor;
- result.y = value1.y / scaleFactor;
- result.z = value1.z / scaleFactor;
- }
- /// <summary>
- /// Subtracts two vectors.
- /// </summary>
- /// <param name="value1">The first vector.</param>
- /// <param name="value2">The second vector.</param>
- /// <returns>The difference of both vectors.</returns>
- #region public static JVector Subtract(JVector value1, JVector value2)
- public static TSVector Subtract(TSVector value1, TSVector value2)
- {
- TSVector result;
- TSVector.Subtract(ref value1, ref value2, out result);
- return result;
- }
- /// <summary>
- /// Subtracts to vectors.
- /// </summary>
- /// <param name="value1">The first vector.</param>
- /// <param name="value2">The second vector.</param>
- /// <param name="result">The difference of both vectors.</param>
- public static void Subtract(ref TSVector value1, ref TSVector value2, out TSVector result)
- {
- FP num0 = value1.x - value2.x;
- FP num1 = value1.y - value2.y;
- FP num2 = value1.z - value2.z;
- result.x = num0;
- result.y = num1;
- result.z = num2;
- }
- #endregion
- /// <summary>
- /// The cross product of two vectors.
- /// </summary>
- /// <param name="vector1">The first vector.</param>
- /// <param name="vector2">The second vector.</param>
- /// <returns>The cross product of both vectors.</returns>
- #region public static JVector Cross(JVector vector1, JVector vector2)
- public static TSVector Cross(TSVector vector1, TSVector vector2)
- {
- TSVector result;
- TSVector.Cross(ref vector1, ref vector2, out result);
- return result;
- }
- /// <summary>
- /// The cross product of two vectors.
- /// </summary>
- /// <param name="vector1">The first vector.</param>
- /// <param name="vector2">The second vector.</param>
- /// <param name="result">The cross product of both vectors.</param>
- public static void Cross(ref TSVector vector1, ref TSVector vector2, out TSVector result)
- {
- FP num3 = (vector1.y * vector2.z) - (vector1.z * vector2.y);
- FP num2 = (vector1.z * vector2.x) - (vector1.x * vector2.z);
- FP num = (vector1.x * vector2.y) - (vector1.y * vector2.x);
- result.x = num3;
- result.y = num2;
- result.z = num;
- }
- #endregion
- /// <summary>
- /// Gets the hashcode of the vector.
- /// </summary>
- /// <returns>Returns the hashcode of the vector.</returns>
- #region public override int GetHashCode()
- public override int GetHashCode()
- {
- return x.GetHashCode() ^ y.GetHashCode() ^ z.GetHashCode();
- }
- #endregion
- /// <summary>
- /// Inverses the direction of the vector.
- /// </summary>
- #region public static JVector Negate(JVector value)
- public void Negate()
- {
- this.x = -this.x;
- this.y = -this.y;
- this.z = -this.z;
- }
- /// <summary>
- /// Inverses the direction of a vector.
- /// </summary>
- /// <param name="value">The vector to inverse.</param>
- /// <returns>The negated vector.</returns>
- public static TSVector Negate(TSVector value)
- {
- TSVector result;
- TSVector.Negate(ref value,out result);
- return result;
- }
- /// <summary>
- /// Inverses the direction of a vector.
- /// </summary>
- /// <param name="value">The vector to inverse.</param>
- /// <param name="result">The negated vector.</param>
- public static void Negate(ref TSVector value, out TSVector result)
- {
- FP num0 = -value.x;
- FP num1 = -value.y;
- FP num2 = -value.z;
- result.x = num0;
- result.y = num1;
- result.z = num2;
- }
- #endregion
- /// <summary>
- /// Normalizes the given vector.
- /// </summary>
- /// <param name="value">The vector which should be normalized.</param>
- /// <returns>A normalized vector.</returns>
- #region public static JVector Normalize(JVector value)
- public static TSVector Normalize(TSVector value)
- {
- TSVector result;
- TSVector.Normalize(ref value, out result);
- return result;
- }
- /// <summary>
- /// Normalizes this vector.
- /// </summary>
- public void Normalize()
- {
- FP num2 = ((this.x * this.x) + (this.y * this.y)) + (this.z * this.z);
- FP num = FP.One / FP.Sqrt(num2);
- this.x *= num;
- this.y *= num;
- this.z *= num;
- }
- /// <summary>
- /// Normalizes the given vector.
- /// </summary>
- /// <param name="value">The vector which should be normalized.</param>
- /// <param name="result">A normalized vector.</param>
- public static void Normalize(ref TSVector value, out TSVector result)
- {
- FP num2 = ((value.x * value.x) + (value.y * value.y)) + (value.z * value.z);
- FP num = FP.One / FP.Sqrt(num2);
- result.x = value.x * num;
- result.y = value.y * num;
- result.z = value.z * num;
- }
- #endregion
- #region public static void Swap(ref JVector vector1, ref JVector vector2)
- /// <summary>
- /// Swaps the components of both vectors.
- /// </summary>
- /// <param name="vector1">The first vector to swap with the second.</param>
- /// <param name="vector2">The second vector to swap with the first.</param>
- public static void Swap(ref TSVector vector1, ref TSVector vector2)
- {
- FP temp;
- temp = vector1.x;
- vector1.x = vector2.x;
- vector2.x = temp;
- temp = vector1.y;
- vector1.y = vector2.y;
- vector2.y = temp;
- temp = vector1.z;
- vector1.z = vector2.z;
- vector2.z = temp;
- }
- #endregion
- /// <summary>
- /// Multiply a vector with a factor.
- /// </summary>
- /// <param name="value1">The vector to multiply.</param>
- /// <param name="scaleFactor">The scale factor.</param>
- /// <returns>Returns the multiplied vector.</returns>
- #region public static JVector Multiply(JVector value1, FP scaleFactor)
- public static TSVector Multiply(TSVector value1, FP scaleFactor)
- {
- TSVector result;
- TSVector.Multiply(ref value1, scaleFactor, out result);
- return result;
- }
- /// <summary>
- /// Multiply a vector with a factor.
- /// </summary>
- /// <param name="value1">The vector to multiply.</param>
- /// <param name="scaleFactor">The scale factor.</param>
- /// <param name="result">Returns the multiplied vector.</param>
- public static void Multiply(ref TSVector value1, FP scaleFactor, out TSVector result)
- {
- result.x = value1.x * scaleFactor;
- result.y = value1.y * scaleFactor;
- result.z = value1.z * scaleFactor;
- }
- #endregion
- /// <summary>
- /// Calculates the cross product of two vectors.
- /// </summary>
- /// <param name="value1">The first vector.</param>
- /// <param name="value2">The second vector.</param>
- /// <returns>Returns the cross product of both.</returns>
- #region public static JVector operator %(JVector value1, JVector value2)
- public static TSVector operator %(TSVector value1, TSVector value2)
- {
- TSVector result; TSVector.Cross(ref value1, ref value2, out result);
- return result;
- }
- #endregion
- /// <summary>
- /// Calculates the dot product of two vectors.
- /// </summary>
- /// <param name="value1">The first vector.</param>
- /// <param name="value2">The second vector.</param>
- /// <returns>Returns the dot product of both.</returns>
- #region public static FP operator *(JVector value1, JVector value2)
- public static FP operator *(TSVector value1, TSVector value2)
- {
- return TSVector.Dot(ref value1, ref value2);
- }
- #endregion
- /// <summary>
- /// Multiplies a vector by a scale factor.
- /// </summary>
- /// <param name="value1">The vector to scale.</param>
- /// <param name="value2">The scale factor.</param>
- /// <returns>Returns the scaled vector.</returns>
- #region public static JVector operator *(JVector value1, FP value2)
- public static TSVector operator *(TSVector value1, FP value2)
- {
- TSVector result;
- TSVector.Multiply(ref value1, value2,out result);
- return result;
- }
- #endregion
- /// <summary>
- /// Multiplies a vector by a scale factor.
- /// </summary>
- /// <param name="value2">The vector to scale.</param>
- /// <param name="value1">The scale factor.</param>
- /// <returns>Returns the scaled vector.</returns>
- #region public static JVector operator *(FP value1, JVector value2)
- public static TSVector operator *(FP value1, TSVector value2)
- {
- TSVector result;
- TSVector.Multiply(ref value2, value1, out result);
- return result;
- }
- #endregion
- /// <summary>
- /// Subtracts two vectors.
- /// </summary>
- /// <param name="value1">The first vector.</param>
- /// <param name="value2">The second vector.</param>
- /// <returns>The difference of both vectors.</returns>
- #region public static JVector operator -(JVector value1, JVector value2)
- public static TSVector operator -(TSVector value1, TSVector value2)
- {
- TSVector result; TSVector.Subtract(ref value1, ref value2, out result);
- return result;
- }
- #endregion
- /// <summary>
- /// Adds two vectors.
- /// </summary>
- /// <param name="value1">The first vector.</param>
- /// <param name="value2">The second vector.</param>
- /// <returns>The sum of both vectors.</returns>
- #region public static JVector operator +(JVector value1, JVector value2)
- public static TSVector operator +(TSVector value1, TSVector value2)
- {
- TSVector result; TSVector.Add(ref value1, ref value2, out result);
- return result;
- }
- #endregion
- /// <summary>
- /// Divides a vector by a factor.
- /// </summary>
- /// <param name="value1">The vector to divide.</param>
- /// <param name="scaleFactor">The scale factor.</param>
- /// <returns>Returns the scaled vector.</returns>
- public static TSVector operator /(TSVector value1, FP value2) {
- TSVector result;
- TSVector.Divide(ref value1, value2, out result);
- return result;
- }
- public TSVector2 ToTSVector2() {
- return new TSVector2(this.x, this.y);
- }
- public TSVector4 ToTSVector4()
- {
- return new TSVector4(this.x, this.y, this.z, FP.One);
- }
- }
- }
|