1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534 |
- #region --- License ---
- /*
- Copyright (c) 2006 - 2008 The Open Toolkit library.
- Permission is hereby granted, free of charge, to any person obtaining a copy of
- this software and associated documentation files (the "Software"), to deal in
- the Software without restriction, including without limitation the rights to
- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
- of the Software, and to permit persons to whom the Software is furnished to do
- so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in all
- copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- SOFTWARE.
- */
- #endregion --- License ---
- using System;
- using System.Runtime.InteropServices;
- using System.Linq;
- using System.Collections.Generic;
- namespace MatterHackers.VectorMath
- {
- /// <summary>
- /// Represents a 3D vector using three double-precision floating-point numbers.
- /// </summary>
-
- [Serializable]
- [StructLayout(LayoutKind.Sequential)]
- public struct Vector3 : IEquatable<Vector3>
- {
- #region Fields
- /// <summary>
- /// The X component of the Vector3.
- /// </summary>
- public double X;
- /// <summary>
- /// The Y component of the Vector3.
- /// </summary>
- public double Y;
- /// <summary>
- /// The Z component of the Vector3.
- /// </summary>
- public double Z;
- #endregion Fields
- #region Constructors
- /// <summary>
- /// Constructs a new Vector3.
- /// </summary>
- /// <param name="x">The x component of the Vector3.</param>
- /// <param name="y">The y component of the Vector3.</param>
- /// <param name="z">The z component of the Vector3.</param>
- public Vector3(double x, double y, double z)
- {
- this.X = x;
- this.Y = y;
- this.Z = z;
- }
- /// <summary>
- /// Constructs a new instance from the given Vector2d.
- /// </summary>
- /// <param name="v">The Vector2d to copy components from.</param>
- public Vector3(Vector2 v, double z = 0)
- {
- X = v.X;
- Y = v.Y;
- this.Z = z;
- }
- /// <summary>
- /// Constructs a new instance from the given Vector3d.
- /// </summary>
- /// <param name="v">The Vector3d to copy components from.</param>
- public Vector3(Vector3 v)
- {
- X = v.X;
- Y = v.Y;
- Z = v.Z;
- }
- public Vector3(Vector3Float v)
- {
- X = v.X;
- Y = v.Y;
- Z = v.Z;
- }
- public Vector3(double[] doubleArray)
- {
- X = doubleArray[0];
- Y = doubleArray[1];
- Z = doubleArray[2];
- }
- /// <summary>
- /// Constructs a new instance from the given Vector4d.
- /// </summary>
- /// <param name="v">The Vector4d to copy components from.</param>
- public Vector3(Vector4 v)
- {
- X = v.X;
- Y = v.Y;
- Z = v.Z;
- }
- #endregion Constructors
- #region Properties
- public double this[int index]
- {
- get
- {
- switch (index)
- {
- case 0:
- return X;
- case 1:
- return Y;
- case 2:
- return Z;
- default:
- return 0;
- }
- }
- set
- {
- switch (index)
- {
- case 0:
- X = value;
- break;
- case 1:
- Y = value;
- break;
- case 2:
- Z = value;
- break;
- default:
- throw new Exception();
- }
- }
- }
- public void Set(double x, double y, double z)
- {
- X = x;
- Y = y;
- Z = z;
- }
- #endregion Properties
- #region Public Members
- #region Instance
- #region public double Length
- /// <summary>
- /// Gets the length (magnitude) of the vector.
- /// </summary>
- /// <see cref="LengthFast"/>
- /// <seealso cref="LengthSquared"/>
-
- public double Length
- {
- get
- {
- return System.Math.Sqrt(X * X + Y * Y + Z * Z);
- }
- }
- public double DistanceToSegment(Vector3 start, Vector3 end)
- {
- var segmentDelta = end - start;
- var segmentLength = segmentDelta.Length;
- var segmentNormal = segmentDelta.GetNormal();
- var deltaToStart = this - start;
- var distanceFromStart = segmentNormal.Dot(deltaToStart);
- if (distanceFromStart >= 0 && distanceFromStart < segmentLength)
- {
- var perpendicular = segmentNormal.GetPerpendicular(new Vector3(0, 0, 1));
- var distanceFromLine = Math.Abs(deltaToStart.Dot(perpendicular));
- return distanceFromLine;
- }
-
- if (distanceFromStart < 0)
- {
- return deltaToStart.Length;
- }
- var deltaToEnd = this - end;
- return deltaToEnd.Length;
- }
- #endregion public double Length
- #region public double LengthSquared
- /// <summary>
- /// Gets the square of the vector length (magnitude).
- /// </summary>
- /// <remarks>
- /// This property avoids the costly square root operation required by the Length property. This makes it more suitable
- /// for comparisons.
- /// </remarks>
- /// <see cref="Length"/>
- /// <seealso cref="LengthFast"/>
-
- public double LengthSquared
- {
- get
- {
- return X * X + Y * Y + Z * Z;
- }
- }
- #endregion public double LengthSquared
- #region public void Normalize()
- /// <summary>
- /// Returns a normalized Vector of this.
- /// </summary>
- /// <returns></returns>
- public Vector3 GetNormal()
- {
- Vector3 temp = this;
- temp.Normalize();
- return temp;
- }
- /// <summary>
- /// Scales the Vector3d to unit length.
- /// </summary>
- public void Normalize()
- {
- double length = this.Length;
- if (length != 0)
- {
- double scale = 1.0 / this.Length;
- X *= scale;
- Y *= scale;
- Z *= scale;
- }
- }
- #endregion public void Normalize()
- public bool IsValid()
- {
- if (double.IsNaN(X) || double.IsInfinity(X)
- || double.IsNaN(Y) || double.IsInfinity(Y)
- || double.IsNaN(Z) || double.IsInfinity(Z))
- {
- return false;
- }
- return true;
- }
- #region public double[] ToArray()
- public double[] ToArray()
- {
- return new double[] { X, Y, Z };
- }
- #endregion public double[] ToArray()
- #endregion Instance
- #region Static
- #region Fields
- /// <summary>
- /// Defines a unit-length Vector3d that points towards the X-axis.
- /// </summary>
- public static readonly Vector3 UnitX = new Vector3(1, 0, 0);
- /// <summary>
- /// Defines a unit-length Vector3d that points towards the Y-axis.
- /// </summary>
- public static readonly Vector3 UnitY = new Vector3(0, 1, 0);
- /// <summary>
- /// /// Defines a unit-length Vector3d that points towards the Z-axis.
- /// </summary>
- public static readonly Vector3 UnitZ = new Vector3(0, 0, 1);
- /// <summary>
- /// Defines a zero-length Vector3.
- /// </summary>
- public static readonly Vector3 Zero = new Vector3(0, 0, 0);
- /// <summary>
- /// Defines an instance with all components set to 1.
- /// </summary>
- public static readonly Vector3 One = new Vector3(1, 1, 1);
- /// <summary>
- /// Defines an instance with all components set to positive infinity.
- /// </summary>
- public static readonly Vector3 PositiveInfinity = new Vector3(double.PositiveInfinity, double.PositiveInfinity, double.PositiveInfinity);
- /// <summary>
- /// Defines an instance with all components set to negative infinity.
- /// </summary>
- public static readonly Vector3 NegativeInfinity = new Vector3(double.NegativeInfinity, double.NegativeInfinity, double.NegativeInfinity);
- /// <summary>
- /// Defines the size of the Vector3d struct in bytes.
- /// </summary>
- public static readonly int SizeInBytes = Marshal.SizeOf(new Vector3());
- #endregion Fields
- #region Add
- /// <summary>
- /// Adds two vectors.
- /// </summary>
- /// <param name="a">Left operand.</param>
- /// <param name="b">Right operand.</param>
- /// <returns>Result of operation.</returns>
- public static Vector3 Add(Vector3 a, Vector3 b)
- {
- Add(ref a, ref b, out a);
- return a;
- }
- /// <summary>
- /// Adds two vectors.
- /// </summary>
- /// <param name="a">Left operand.</param>
- /// <param name="b">Right operand.</param>
- /// <param name="result">Result of operation.</param>
- public static void Add(ref Vector3 a, ref Vector3 b, out Vector3 result)
- {
- result = new Vector3(a.X + b.X, a.Y + b.Y, a.Z + b.Z);
- }
- #endregion Add
- #region Subtract
- /// <summary>
- /// Subtract one Vector from another
- /// </summary>
- /// <param name="a">First operand</param>
- /// <param name="b">Second operand</param>
- /// <returns>Result of subtraction</returns>
- public static Vector3 Subtract(Vector3 a, Vector3 b)
- {
- Subtract(ref a, ref b, out a);
- return a;
- }
- /// <summary>
- /// Subtract one Vector from another
- /// </summary>
- /// <param name="a">First operand</param>
- /// <param name="b">Second operand</param>
- /// <param name="result">Result of subtraction</param>
- public static void Subtract(ref Vector3 a, ref Vector3 b, out Vector3 result)
- {
- result = new Vector3(a.X - b.X, a.Y - b.Y, a.Z - b.Z);
- }
- #endregion Subtract
- #region Multiply
- /// <summary>
- /// Multiplies a vector by a scalar.
- /// </summary>
- /// <param name="vector">Left operand.</param>
- /// <param name="scale">Right operand.</param>
- /// <returns>Result of the operation.</returns>
- public static Vector3 Multiply(Vector3 vector, double scale)
- {
- Multiply(ref vector, scale, out vector);
- return vector;
- }
- /// <summary>
- /// Multiplies a vector by a scalar.
- /// </summary>
- /// <param name="vector">Left operand.</param>
- /// <param name="scale">Right operand.</param>
- /// <param name="result">Result of the operation.</param>
- public static void Multiply(ref Vector3 vector, double scale, out Vector3 result)
- {
- result = new Vector3(vector.X * scale, vector.Y * scale, vector.Z * scale);
- }
- /// <summary>
- /// Multiplies a vector by the components a vector (scale).
- /// </summary>
- /// <param name="vector">Left operand.</param>
- /// <param name="scale">Right operand.</param>
- /// <returns>Result of the operation.</returns>
- public static Vector3 Multiply(Vector3 vector, Vector3 scale)
- {
- Multiply(ref vector, ref scale, out vector);
- return vector;
- }
- /// <summary>
- /// Multiplies a vector by the components of a vector (scale).
- /// </summary>
- /// <param name="vector">Left operand.</param>
- /// <param name="scale">Right operand.</param>
- /// <param name="result">Result of the operation.</param>
- public static void Multiply(ref Vector3 vector, ref Vector3 scale, out Vector3 result)
- {
- result = new Vector3(vector.X * scale.X, vector.Y * scale.Y, vector.Z * scale.Z);
- }
- #endregion Multiply
- #region Divide
- /// <summary>
- /// Divides a vector by a scalar.
- /// </summary>
- /// <param name="vector">Left operand.</param>
- /// <param name="scale">Right operand.</param>
- /// <returns>Result of the operation.</returns>
- public static Vector3 Divide(Vector3 vector, double scale)
- {
- Divide(ref vector, scale, out vector);
- return vector;
- }
- /// <summary>
- /// Divides a vector by a scalar.
- /// </summary>
- /// <param name="vector">Left operand.</param>
- /// <param name="scale">Right operand.</param>
- /// <param name="result">Result of the operation.</param>
- public static void Divide(ref Vector3 vector, double scale, out Vector3 result)
- {
- Multiply(ref vector, 1 / scale, out result);
- }
- /// <summary>
- /// Divides a vector by the components of a vector (scale).
- /// </summary>
- /// <param name="vector">Left operand.</param>
- /// <param name="scale">Right operand.</param>
- /// <returns>Result of the operation.</returns>
- public static Vector3 Divide(Vector3 vector, Vector3 scale)
- {
- Divide(ref vector, ref scale, out vector);
- return vector;
- }
- /// <summary>
- /// Divide a vector by the components of a vector (scale).
- /// </summary>
- /// <param name="vector">Left operand.</param>
- /// <param name="scale">Right operand.</param>
- /// <param name="result">Result of the operation.</param>
- public static void Divide(ref Vector3 vector, ref Vector3 scale, out Vector3 result)
- {
- result = new Vector3(vector.X / scale.X, vector.Y / scale.Y, vector.Z / scale.Z);
- }
- #endregion Divide
- #region ComponentMin
- /// <summary>
- /// Calculate the component-wise minimum of two vectors
- /// </summary>
- /// <param name="a">First operand</param>
- /// <param name="b">Second operand</param>
- /// <returns>The component-wise minimum</returns>
- public static Vector3 ComponentMin(Vector3 a, Vector3 b)
- {
- a.X = a.X < b.X ? a.X : b.X;
- a.Y = a.Y < b.Y ? a.Y : b.Y;
- a.Z = a.Z < b.Z ? a.Z : b.Z;
- return a;
- }
- public static Vector3 Parse(string s)
- {
- var result = Vector3.Zero;
- var values = s.Split(',').Select(sValue =>
- {
- double.TryParse(sValue, out double number);
- return number;
- }).ToArray();
- for (int i = 0; i < Math.Min(3, values.Length); i++)
- {
- result[i] = values[i];
- }
- return result;
- }
- /// <summary>
- /// Calculate the component-wise minimum of two vectors
- /// </summary>
- /// <param name="a">First operand</param>
- /// <param name="b">Second operand</param>
- /// <param name="result">The component-wise minimum</param>
- public static void ComponentMin(ref Vector3 a, ref Vector3 b, out Vector3 result)
- {
- result.X = a.X < b.X ? a.X : b.X;
- result.Y = a.Y < b.Y ? a.Y : b.Y;
- result.Z = a.Z < b.Z ? a.Z : b.Z;
- }
- #endregion ComponentMin
- #region ComponentMax
- /// <summary>
- /// Calculate the component-wise maximum of two vectors
- /// </summary>
- /// <param name="a">First operand</param>
- /// <param name="b">Second operand</param>
- /// <returns>The component-wise maximum</returns>
- public static Vector3 ComponentMax(Vector3 a, Vector3 b)
- {
- a.X = a.X > b.X ? a.X : b.X;
- a.Y = a.Y > b.Y ? a.Y : b.Y;
- a.Z = a.Z > b.Z ? a.Z : b.Z;
- return a;
- }
- public static Vector3 ComponentMax(Vector3 a, double b)
- {
- a.X = a.X > b ? a.X : b;
- a.Y = a.Y > b ? a.Y : b;
- a.Z = a.Z > b ? a.Z : b;
- return a;
- }
- /// <summary>
- /// Calculate the component-wise maximum of two vectors
- /// </summary>
- /// <param name="a">First operand</param>
- /// <param name="b">Second operand</param>
- /// <param name="result">The component-wise maximum</param>
- public static void ComponentMax(ref Vector3 a, ref Vector3 b, out Vector3 result)
- {
- result.X = a.X > b.X ? a.X : b.X;
- result.Y = a.Y > b.Y ? a.Y : b.Y;
- result.Z = a.Z > b.Z ? a.Z : b.Z;
- }
- #endregion ComponentMax
- #region Min
- /// <summary>
- /// Returns the Vector3d with the minimum magnitude
- /// </summary>
- /// <param name="left">Left operand</param>
- /// <param name="right">Right operand</param>
- /// <returns>The minimum Vector3</returns>
- public static Vector3 Min(Vector3 left, Vector3 right)
- {
- return left.LengthSquared < right.LengthSquared ? left : right;
- }
- #endregion Min
- #region Max
- /// <summary>
- /// Returns the Vector3d with the minimum magnitude
- /// </summary>
- /// <param name="left">Left operand</param>
- /// <param name="right">Right operand</param>
- /// <returns>The minimum Vector3</returns>
- public static Vector3 Max(Vector3 left, Vector3 right)
- {
- return left.LengthSquared >= right.LengthSquared ? left : right;
- }
- #endregion Max
- #region Clamp
- /// <summary>
- /// Clamp a vector to the given minimum and maximum vectors
- /// </summary>
- /// <param name="vec">Input vector</param>
- /// <param name="min">Minimum vector</param>
- /// <param name="max">Maximum vector</param>
- /// <returns>The clamped vector</returns>
- public static Vector3 Clamp(Vector3 vec, Vector3 min, Vector3 max)
- {
- vec.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X;
- vec.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y;
- vec.Z = vec.Z < min.Z ? min.Z : vec.Z > max.Z ? max.Z : vec.Z;
- return vec;
- }
- /// <summary>
- /// Clamp a vector to the given minimum and maximum vectors
- /// </summary>
- /// <param name="vec">Input vector</param>
- /// <param name="min">Minimum vector</param>
- /// <param name="max">Maximum vector</param>
- /// <param name="result">The clamped vector</param>
- public static void Clamp(ref Vector3 vec, ref Vector3 min, ref Vector3 max, out Vector3 result)
- {
- result.X = vec.X < min.X ? min.X : vec.X > max.X ? max.X : vec.X;
- result.Y = vec.Y < min.Y ? min.Y : vec.Y > max.Y ? max.Y : vec.Y;
- result.Z = vec.Z < min.Z ? min.Z : vec.Z > max.Z ? max.Z : vec.Z;
- }
- #endregion Clamp
- #region Normalize
- /// <summary>
- /// Scale a vector to unit length
- /// </summary>
- /// <param name="vec">The input vector</param>
- /// <returns>The normalized vector</returns>
- public static Vector3 Normalize(Vector3 vec)
- {
- double scale = 1.0 / vec.Length;
- vec.X *= scale;
- vec.Y *= scale;
- vec.Z *= scale;
- return vec;
- }
- /// <summary>
- /// Scale a vector to unit length
- /// </summary>
- /// <param name="vec">The input vector</param>
- /// <param name="result">The normalized vector</param>
- public static void Normalize(ref Vector3 vec, out Vector3 result)
- {
- double scale = 1.0 / vec.Length;
- result.X = vec.X * scale;
- result.Y = vec.Y * scale;
- result.Z = vec.Z * scale;
- }
- #endregion Normalize
- #region Utility
- /// <summary>
- /// Checks if 3 points are collinear (all lie on the same line).
- /// </summary>
- /// <param name="a"></param>
- /// <param name="b"></param>
- /// <param name="c"></param>
- /// <param name="epsilon"></param>
- /// <returns></returns>
- public static bool Collinear(Vector3 a, Vector3 b, Vector3 c, double epsilon = .000001)
- {
- // Return true if a, b, and c all lie on the same line.
- return Math.Abs((b - a).Cross(c - a).Length) < epsilon;
- }
- public static Vector3 Abs(Vector3 a)
- {
- return new Vector3(Math.Abs(a.X), Math.Abs(a.Y), Math.Abs(a.Z));
- }
- /// <summary>
- /// Given an arbitrary vector find a perpendicular from the infinite perpendiculars that are available
- /// </summary>
- /// <param name="a">The vector to find a perpendicular for</param>
- /// <returns>A perpendicular vector to a</returns>
- public Vector3 GetPerpendicular()
- {
- if (this.X != 0)
- {
- return new Vector3(-(this.Y + this.Z) / this.X, 1, 1);
- }
- else if (this.Y != 0)
- {
- return new Vector3(1, -(this.X + this.Z) / this.Y, 1);
- }
- else if (this.Z != 0)
- {
- return new Vector3(1, 1, -(this.X + this.Y) / this.Z);
- }
- // the input vector has no length (no vector is perpendicular to it)
- return default(Vector3);
- }
- public Vector3 GetPerpendicular(Vector3 b)
- {
- return GetPerpendicular(this, b);
- }
- public static Vector3 GetPerpendicular(Vector3 a, Vector3 b)
- {
- if (!Collinear(a, b, Zero))
- {
- return a.Cross(b);
- }
- else
- {
- Vector3 zOne = new Vector3(0, 0, 100000);
- if (!Collinear(a, b, zOne))
- {
- return Vector3Ex.Cross(a - zOne, b - zOne);
- }
- else
- {
- Vector3 xOne = new Vector3(1000000, 0, 0);
- return Vector3Ex.Cross(a - xOne, b - xOne);
- }
- }
- }
- #endregion Utility
- #region Lerp
- /// <summary>
- /// Returns a new Vector that is the linear blend of the 2 given Vectors
- /// </summary>
- /// <param name="a">First input vector</param>
- /// <param name="b">Second input vector</param>
- /// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
- /// <returns>a when blend=0, b when blend=1, and a linear combination otherwise</returns>
- public static Vector3 Lerp(Vector3 a, Vector3 b, double blend)
- {
- if (blend == 0)
- {
- return a;
- }
- if (blend == 1)
- {
- return b;
- }
- a.X = blend * (b.X - a.X) + a.X;
- a.Y = blend * (b.Y - a.Y) + a.Y;
- a.Z = blend * (b.Z - a.Z) + a.Z;
- return a;
- }
- /// <summary>
- /// Returns a new Vector that is the linear blend of the 2 given Vectors
- /// </summary>
- /// <param name="a">First input vector</param>
- /// <param name="b">Second input vector</param>
- /// <param name="blend">The blend factor. a when blend=0, b when blend=1.</param>
- /// <param name="result">a when blend=0, b when blend=1, and a linear combination otherwise</param>
- public static void Lerp(ref Vector3 a, ref Vector3 b, double blend, out Vector3 result)
- {
- result.X = blend * (b.X - a.X) + a.X;
- result.Y = blend * (b.Y - a.Y) + a.Y;
- result.Z = blend * (b.Z - a.Z) + a.Z;
- }
- #endregion Lerp
- #region Barycentric
- /// <summary>
- /// Interpolate 3 Vectors using Barycentric coordinates
- /// </summary>
- /// <param name="a">First input Vector</param>
- /// <param name="b">Second input Vector</param>
- /// <param name="c">Third input Vector</param>
- /// <param name="u">First Barycentric Coordinate</param>
- /// <param name="v">Second Barycentric Coordinate</param>
- /// <returns>a when u=v=0, b when u=1,v=0, c when u=0,v=1, and a linear combination of a,b,c otherwise</returns>
- public static Vector3 BaryCentric(Vector3 a, Vector3 b, Vector3 c, double u, double v)
- {
- return a + u * (b - a) + v * (c - a);
- }
- /// <summary>Interpolate 3 Vectors using Barycentric coordinates</summary>
- /// <param name="a">First input Vector.</param>
- /// <param name="b">Second input Vector.</param>
- /// <param name="c">Third input Vector.</param>
- /// <param name="u">First Barycentric Coordinate.</param>
- /// <param name="v">Second Barycentric Coordinate.</param>
- /// <param name="result">Output Vector. a when u=v=0, b when u=1,v=0, c when u=0,v=1, and a linear combination of a,b,c otherwise</param>
- public static void BaryCentric(ref Vector3 a, ref Vector3 b, ref Vector3 c, double u, double v, out Vector3 result)
- {
- result = a; // copy
- Vector3 temp = b; // copy
- Subtract(ref temp, ref a, out temp);
- Multiply(ref temp, u, out temp);
- Add(ref result, ref temp, out result);
- temp = c; // copy
- Subtract(ref temp, ref a, out temp);
- Multiply(ref temp, v, out temp);
- Add(ref result, ref temp, out result);
- }
- #endregion Barycentric
- #region CalculateAngle
- /// <summary>
- /// Calculates the angle (in radians) between two vectors.
- /// </summary>
- /// <param name="first">The first vector.</param>
- /// <param name="second">The second vector.</param>
- /// <returns>Angle (in radians) between the vectors.</returns>
- /// <remarks>Note that the returned angle is never bigger than the constant Pi.</remarks>
- public static double CalculateAngle(Vector3 first, Vector3 second)
- {
- return System.Math.Acos((first.Dot(second)) / (first.Length * second.Length));
- }
- /// <summary>Calculates the angle (in radians) between two vectors.</summary>
- /// <param name="first">The first vector.</param>
- /// <param name="second">The second vector.</param>
- /// <param name="result">Angle (in radians) between the vectors.</param>
- /// <remarks>Note that the returned angle is never bigger than the constant Pi.</remarks>
- public static void CalculateAngle(ref Vector3 first, ref Vector3 second, out double result)
- {
- double temp;
- first.Dot(ref second, out temp);
- result = System.Math.Acos(temp / (first.Length * second.Length));
- }
- #endregion CalculateAngle
- #endregion Static
- #region Swizzle
- /// <summary>
- /// Gets or sets an OpenTK.Vector2d with the X and Y components of this instance.
- /// </summary>
-
- public Vector2 Xy { get { return new Vector2(X, Y); } set { X = value.X; Y = value.Y; } }
- #endregion Swizzle
- #region Operators
- /// <summary>
- /// Adds two instances.
- /// </summary>
- /// <param name="left">The first instance.</param>
- /// <param name="right">The second instance.</param>
- /// <returns>The result of the calculation.</returns>
- public static Vector3 operator +(Vector3 left, Vector3 right)
- {
- left.X += right.X;
- left.Y += right.Y;
- left.Z += right.Z;
- return left;
- }
- /// <summary>
- /// Subtracts two instances.
- /// </summary>
- /// <param name="left">The first instance.</param>
- /// <param name="right">The second instance.</param>
- /// <returns>The result of the calculation.</returns>
- public static Vector3 operator -(Vector3 left, Vector3 right)
- {
- left.X -= right.X;
- left.Y -= right.Y;
- left.Z -= right.Z;
- return left;
- }
- /// <summary>
- /// Negates an instance.
- /// </summary>
- /// <param name="vec">The instance.</param>
- /// <returns>The result of the calculation.</returns>
- public static Vector3 operator -(Vector3 vec)
- {
- vec.X = -vec.X;
- vec.Y = -vec.Y;
- vec.Z = -vec.Z;
- return vec;
- }
- /// <summary>
- /// Component wise multiply two vectors together, x*x, y*y, z*z.
- /// </summary>
- /// <param name="vecA"></param>
- /// <param name="vecB"></param>
- /// <returns></returns>
- public static Vector3 operator *(Vector3 vecA, Vector3 vecB)
- {
- vecA.X *= vecB.X;
- vecA.Y *= vecB.Y;
- vecA.Z *= vecB.Z;
- return vecA;
- }
- /// <summary>
- /// Multiplies an instance by a scalar.
- /// </summary>
- /// <param name="vec">The instance.</param>
- /// <param name="scale">The scalar.</param>
- /// <returns>The result of the calculation.</returns>
- public static Vector3 operator *(Vector3 vec, double scale)
- {
- vec.X *= scale;
- vec.Y *= scale;
- vec.Z *= scale;
- return vec;
- }
- /// <summary>
- /// Multiplies an instance by a scalar.
- /// </summary>
- /// <param name="scale">The scalar.</param>
- /// <param name="vec">The instance.</param>
- /// <returns>The result of the calculation.</returns>
- public static Vector3 operator *(double scale, Vector3 vec)
- {
- vec.X *= scale;
- vec.Y *= scale;
- vec.Z *= scale;
- return vec;
- }
- /// <summary>
- /// Creates a new vector which is the numerator divide by each component of the vector.
- /// </summary>
- /// <param name="numerator"></param>
- /// <param name="vec"></param>
- /// <returns>The result of the calculation.</returns>
- public static Vector3 operator /(double numerator, Vector3 vec)
- {
- return new Vector3((numerator / vec.X), (numerator / vec.Y), (numerator / vec.Z));
- }
- /// <summary>
- /// Divides an instance by a scalar.
- /// </summary>
- /// <param name="vec">The instance.</param>
- /// <param name="scale">The scalar.</param>
- /// <returns>The result of the calculation.</returns>
- public static Vector3 operator /(Vector3 vec, double scale)
- {
- double mult = 1 / scale;
- vec.X *= mult;
- vec.Y *= mult;
- vec.Z *= mult;
- return vec;
- }
- /// <summary>
- /// Compares two instances for equality.
- /// </summary>
- /// <param name="left">The first instance.</param>
- /// <param name="right">The second instance.</param>
- /// <returns>True, if left equals right; false otherwise.</returns>
- public static bool operator ==(Vector3 left, Vector3 right)
- {
- return left.Equals(right);
- }
- /// <summary>
- /// Compares two instances for inequality.
- /// </summary>
- /// <param name="left">The first instance.</param>
- /// <param name="right">The second instance.</param>
- /// <returns>True, if left does not equa lright; false otherwise.</returns>
- public static bool operator !=(Vector3 left, Vector3 right)
- {
- return !left.Equals(right);
- }
- #endregion Operators
- #region Overrides
- #region public override string ToString()
- /// <summary>
- /// Returns a System.String that represents the current Vector3.
- /// </summary>
- /// <returns></returns>
- public override string ToString()
- {
- return String.Format($"[{X:0.####}, {Y:0.####}, {Z:0.####}]");
- }
- #endregion public override string ToString()
- #region public override int GetHashCode()
- /// <summary>
- /// Returns the hashcode for this instance.
- /// </summary>
- /// <returns>A System.Int32 containing the unique hashcode for this instance.</returns>
- public override int GetHashCode()
- {
- return new { X, Y, Z }.GetHashCode();
- }
- /// <summary>
- /// return a 64 bit hash code proposed by Jon Skeet
- // http://stackoverflow.com/questions/8094867/good-gethashcode-override-for-list-of-foo-objects-respecting-the-order
- /// </summary>
- /// <returns></returns>
- public ulong GetLongHashCode(ulong hash = 14695981039346656037)
- {
- hash = Vector4.GetLongHashCode(X, hash);
- hash = Vector4.GetLongHashCode(Y, hash);
- hash = Vector4.GetLongHashCode(Z, hash);
- return hash;
- }
- #endregion public override int GetHashCode()
- #region public override bool Equals(object obj)
- /// <summary>
- /// Indicates whether this instance and a specified object are equal.
- /// </summary>
- /// <param name="obj">The object to compare to.</param>
- /// <returns>True if the instances are equal; false otherwise.</returns>
- public override bool Equals(object obj)
- {
- if (!(obj is Vector3))
- return false;
- return this.Equals((Vector3)obj);
- }
- /// <summary>
- /// Indicates whether this instance and a specified object are equal within an error range.
- /// </summary>
- /// <param name="OtherVector"></param>
- /// <param name="ErrorValue"></param>
- /// <returns>True if the instances are equal; false otherwise.</returns>
- public bool Equals(Vector3 OtherVector, double ErrorValue)
- {
- if ((X < OtherVector.X + ErrorValue && X > OtherVector.X - ErrorValue) &&
- (Y < OtherVector.Y + ErrorValue && Y > OtherVector.Y - ErrorValue) &&
- (Z < OtherVector.Z + ErrorValue && Z > OtherVector.Z - ErrorValue))
- {
- return true;
- }
- return false;
- }
- #endregion public override bool Equals(object obj)
- #endregion Overrides
- #endregion Public Members
- #region IEquatable<Vector3> Members
- /// <summary>Indicates whether the current vector is equal to another vector.</summary>
- /// <param name="other">A vector to compare with this vector.</param>
- /// <returns>true if the current vector is equal to the vector parameter; otherwise, false.</returns>
- public bool Equals(Vector3 other)
- {
- return
- X == other.X &&
- Y == other.Y &&
- Z == other.Z;
- }
- #endregion IEquatable<Vector3> Members
- #region IConvertable
- public TypeCode GetTypeCode()
- {
- throw new NotImplementedException();
- }
- public bool ToBoolean(IFormatProvider provider)
- {
- throw new NotImplementedException();
- }
- public char ToChar(IFormatProvider provider)
- {
- throw new NotImplementedException();
- }
- public sbyte ToSByte(IFormatProvider provider)
- {
- throw new NotImplementedException();
- }
- public byte ToByte(IFormatProvider provider)
- {
- throw new NotImplementedException();
- }
- public short ToInt16(IFormatProvider provider)
- {
- throw new NotImplementedException();
- }
- public ushort ToUInt16(IFormatProvider provider)
- {
- throw new NotImplementedException();
- }
- public int ToInt32(IFormatProvider provider)
- {
- throw new NotImplementedException();
- }
- public uint ToUInt32(IFormatProvider provider)
- {
- throw new NotImplementedException();
- }
- public long ToInt64(IFormatProvider provider)
- {
- throw new NotImplementedException();
- }
- public ulong ToUInt64(IFormatProvider provider)
- {
- throw new NotImplementedException();
- }
- public float ToSingle(IFormatProvider provider)
- {
- throw new NotImplementedException();
- }
- public double ToDouble(IFormatProvider provider)
- {
- throw new NotImplementedException();
- }
- public decimal ToDecimal(IFormatProvider provider)
- {
- throw new NotImplementedException();
- }
- public DateTime ToDateTime(IFormatProvider provider)
- {
- throw new NotImplementedException();
- }
- public string ToString(IFormatProvider provider)
- {
- throw new NotImplementedException();
- }
- public object ToType(Type conversionType, IFormatProvider provider)
- {
- throw new NotImplementedException();
- }
- #endregion IConvertable
- public static double ComponentMax(Vector3 vector3)
- {
- return Math.Max(vector3.X, Math.Max(vector3.Y, vector3.Z));
- }
- public static double ComponentMin(Vector3 vector3)
- {
- return Math.Min(vector3.X, Math.Min(vector3.Y, vector3.Z));
- }
- }
- public static class Vector3Ex
- {
- #region Dot
- /// <summary>
- /// Calculate the dot (scalar) product of two vectors
- /// </summary>
- /// <param name="left">First operand</param>
- /// <param name="right">Second operand</param>
- /// <returns>The dot product of the two inputs</returns>
- public static double Dot(this Vector3 left, Vector3 right)
- {
- return left.X * right.X + left.Y * right.Y + left.Z * right.Z;
- }
- public static float[] ToFloatArray(this List<Vector3> list)
- {
- var array = new float[list.Count * 3];
- for (var i=0; i<list.Count; i++)
- {
- array[i * 3 + 0] = (float)list[i].X;
- array[i * 3 + 1] = (float)list[i].Y;
- array[i * 3 + 2] = (float)list[i].Z;
- }
- return array;
- }
- /// <summary>
- /// Calculate the dot (scalar) product of two vectors
- /// </summary>
- /// <param name="left">First operand</param>
- /// <param name="right">Second operand</param>
- /// <param name="result">The dot product of the two inputs</param>
- public static void Dot(this Vector3 left, ref Vector3 right, out double result)
- {
- result = left.X * right.X + left.Y * right.Y + left.Z * right.Z;
- }
- #endregion Dot
- #region Cross
- /// <summary>
- /// Calculate the cross (vector) product of two vectors
- /// </summary>
- /// <param name="left">First operand</param>
- /// <param name="right">Second operand</param>
- /// <returns>The cross product of the two inputs</returns>
- public static Vector3 Cross(this Vector3 left, Vector3 right)
- {
- Vector3 result;
- left.Cross(ref right, out result);
- return result;
- }
- /// <summary>
- /// Calculate the cross (vector) product of two vectors
- /// </summary>
- /// <param name="left">First operand</param>
- /// <param name="right">Second operand</param>
- /// <returns>The cross product of the two inputs</returns>
- /// <param name="result">The cross product of the two inputs</param>
- public static void Cross(this Vector3 left, ref Vector3 right, out Vector3 result)
- {
- result = new Vector3(left.Y * right.Z - left.Z * right.Y,
- left.Z * right.X - left.X * right.Z,
- left.X * right.Y - left.Y * right.X);
- }
- #endregion Cross
- #region Transform
- /// <summary>Transform a direction vector by the given Matrix
- /// Assumes the matrix has a bottom row of (0,0,0,1), that is the translation part is ignored.
- /// </summary>
- /// <param name="vec">The vector to transform</param>
- /// <param name="mat">The desired transformation</param>
- /// <returns>The transformed vector</returns>
- public static Vector3 TransformVector(this Vector3 vec, Matrix4X4 mat)
- {
- return new Vector3(
- vec.Dot(new Vector3(mat.Column0)),
- vec.Dot(new Vector3(mat.Column1)),
- vec.Dot(new Vector3(mat.Column2)));
- }
- /// <summary>Transform a direction vector by the given Matrix
- /// Assumes the matrix has a bottom row of (0,0,0,1), that is the translation part is ignored.
- /// </summary>
- /// <param name="vec">The vector to transform</param>
- /// <param name="mat">The desired transformation</param>
- /// <param name="result">The transformed vector</param>
- public static void TransformVector(this Vector3 vec, ref Matrix4X4 mat, out Vector3 result)
- {
- result.X = vec.X * mat.Row0.X +
- vec.Y * mat.Row1.X +
- vec.Z * mat.Row2.X;
- result.Y = vec.X * mat.Row0.Y +
- vec.Y * mat.Row1.Y +
- vec.Z * mat.Row2.Y;
- result.Z = vec.X * mat.Row0.Z +
- vec.Y * mat.Row1.Z +
- vec.Z * mat.Row2.Z;
- }
- /// This calculates the inverse of the given matrix, use TransformNormalInverse if you
- /// already have the inverse to avoid this extra calculation
- /// <param name="normal">The normal to transform</param>
- /// <param name="mat">The desired transformation</param>
- /// <returns>The transformed normal</returns>
- public static Vector3 TransformNormal(this Vector3 normal, Matrix4X4 mat)
- {
- Vector3 result;
- TransformNormal(normal, ref mat, out result);
- return result;
- }
- /// <summary>Transform a Normal by the given Matrix</summary>
- /// <remarks>
- /// This calculates the inverse of the given matrix, use TransformNormal if you have
- /// a point on the plane (fastest) or TransformNormalInverse if you
- /// have the inverse but not a point on the plane - to avoid this extra calculation
- /// </remarks>
- /// <param name="normal">The normal to transform</param>
- /// <param name="mat">The desired transformation</param>
- /// <param name="result">The transformed normal</param>
- public static void TransformNormal(this Vector3 normal, ref Matrix4X4 mat, out Vector3 result)
- {
- Matrix4X4 Inverse = Matrix4X4.Invert(mat);
- TransformNormalInverse(normal, ref Inverse, out result);
- }
- /// <summary>Transform a Normal by the (transpose of the) given Matrix</summary>
- /// <remarks>
- /// This version doesn't calculate the inverse matrix.
- /// Use this version if you already have the inverse of the desired transform to hand
- /// </remarks>
- /// <param name="normal">The normal to transform</param>
- /// <param name="invMat">The inverse of the desired transformation</param>
- /// <returns>The transformed normal</returns>
- public static Vector3 TransformNormalInverse(this Vector3 normal, Matrix4X4 invMat)
- {
- return new Vector3(
- normal.Dot(new Vector3(invMat.Row0)),
- normal.Dot(new Vector3(invMat.Row1)),
- normal.Dot(new Vector3(invMat.Row2)));
- }
- /// <summary>Transform a Normal by the (transpose of the) given Matrix</summary>
- /// <remarks>
- /// This version doesn't calculate the inverse matrix.
- /// Use this version if you already have the inverse of the desired transform to hand
- /// </remarks>
- /// <param name="normal">The normal to transform</param>
- /// <param name="invMat">The inverse of the desired transformation</param>
- /// <param name="result">The transformed normal</param>
- public static void TransformNormalInverse(this Vector3 normal, ref Matrix4X4 invMat, out Vector3 result)
- {
- result.X = normal.X * invMat.Row0.X +
- normal.Y * invMat.Row0.Y +
- normal.Z * invMat.Row0.Z;
- result.Y = normal.X * invMat.Row1.X +
- normal.Y * invMat.Row1.Y +
- normal.Z * invMat.Row1.Z;
- result.Z = normal.X * invMat.Row2.X +
- normal.Y * invMat.Row2.Y +
- normal.Z * invMat.Row2.Z;
- }
- /// <summary>Transform a Position by the given Matrix</summary>
- /// <param name="pos">The position to transform</param>
- /// <param name="mat">The desired transformation</param>
- /// <returns>The transformed position</returns>
- public static Vector3 TransformPosition(this Vector3 pos, Matrix4X4 mat)
- {
- return new Vector3(
- pos.Dot(new Vector3(mat.Column0)) + mat.Row3.X,
- pos.Dot(new Vector3(mat.Column1)) + mat.Row3.Y,
- pos.Dot(new Vector3(mat.Column2)) + mat.Row3.Z);
- }
- /// <summary>Transform a Position by the given Matrix</summary>
- /// <param name="pos">The position to transform</param>
- /// <param name="mat">The desired transformation</param>
- /// <param name="result">The transformed position</param>
- public static void TransformPosition(this Vector3 pos, ref Matrix4X4 mat, out Vector3 result)
- {
- result.X = pos.X * mat.Row0.X +
- pos.Y * mat.Row1.X +
- pos.Z * mat.Row2.X +
- mat.Row3.X;
- result.Y = pos.X * mat.Row0.Y +
- pos.Y * mat.Row1.Y +
- pos.Z * mat.Row2.Y +
- mat.Row3.Y;
- result.Z = pos.X * mat.Row0.Z +
- pos.Y * mat.Row1.Z +
- pos.Z * mat.Row2.Z +
- mat.Row3.Z;
- }
- /// <summary>
- /// Transform all the vectors in the array by the given Matrix.
- /// </summary>
- /// <param name="boundsVerts"></param>
- /// <param name="rotationQuaternion"></param>
- public static void Transform(this Vector3[] vecArray, Matrix4X4 mat)
- {
- for (int i = 0; i < vecArray.Length; i++)
- {
- vecArray[i] = Transform(vecArray[i], mat);
- }
- }
- /// <summary>Transform a Vector by the given Matrix</summary>
- /// <param name="vec">The vector to transform</param>
- /// <param name="mat">The desired transformation</param>
- /// <returns>The transformed vector</returns>
- public static Vector3 Transform(this Vector3 vec, Matrix4X4 mat)
- {
- return new Vector3(
- vec.X * mat.Row0.X + vec.Y * mat.Row1.X + vec.Z * mat.Row2.X + mat.Row3.X,
- vec.X * mat.Row0.Y + vec.Y * mat.Row1.Y + vec.Z * mat.Row2.Y + mat.Row3.Y,
- vec.X * mat.Row0.Z + vec.Y * mat.Row1.Z + vec.Z * mat.Row2.Z + mat.Row3.Z);
- }
- /// <summary>Transform a Vector by the given Matrix</summary>
- /// <param name="vec">The vector to transform</param>
- /// <param name="mat">The desired transformation</param>
- /// <param name="result">The transformed vector</param>
- public static void Transform(this Vector3 vec, ref Matrix4X4 mat, out Vector3 result)
- {
- result = new Vector3(
- vec.X * mat.Row0.X + vec.Y * mat.Row1.X + vec.Z * mat.Row2.X + mat.Row3.X,
- vec.X * mat.Row0.Y + vec.Y * mat.Row1.Y + vec.Z * mat.Row2.Y + mat.Row3.Y,
- vec.X * mat.Row0.Z + vec.Y * mat.Row1.Z + vec.Z * mat.Row2.Z + mat.Row3.Z);
- }
- /// <summary>
- /// Transforms a vector by a quaternion rotation.
- /// </summary>
- /// <param name="vec">The vector to transform.</param>
- /// <param name="quat">The quaternion to rotate the vector by.</param>
- /// <returns>The result of the operation.</returns>
- public static Vector3 Transform(this Vector3 vec, Quaternion quat)
- {
- Vector3 result;
- Transform(vec, ref quat, out result);
- return result;
- }
- /// <summary>
- /// Transforms a vector by a quaternion rotation.
- /// </summary>
- /// <param name="vec">The vector to transform.</param>
- /// <param name="quat">The quaternion to rotate the vector by.</param>
- /// <param name="result">The result of the operation.</param>
- public static void Transform(this Vector3 vec, ref Quaternion quat, out Vector3 result)
- {
- // Since vec.W == 0, we can optimize quat * vec * quat^-1 as follows:
- // vec + 2.0 * cross(quat.xyz, cross(quat.xyz, vec) + quat.w * vec)
- Vector3 xyz = quat.Xyz, temp, temp2;
- xyz.Cross(ref vec, out temp);
- Vector3.Multiply(ref vec, quat.W, out temp2);
- Vector3.Add(ref temp, ref temp2, out temp);
- xyz.Cross(ref temp, out temp);
- Vector3.Multiply(ref temp, 2, out temp);
- Vector3.Add(ref vec, ref temp, out result);
- }
- /// <summary>
- /// Transform all the vectors in the array by the quaternion rotation.
- /// </summary>
- /// <param name="boundsVerts"></param>
- /// <param name="rotationQuaternion"></param>
- public static void Transform(this Vector3[] vecArray, Quaternion rotationQuaternion)
- {
- for (int i = 0; i < vecArray.Length; i++)
- {
- vecArray[i] = Transform(vecArray[i], rotationQuaternion);
- }
- }
- /// <summary>
- /// Transform a Vector3d by the given Matrix, and project the resulting Vector4 back to a Vector3
- /// </summary>
- /// <param name="vec">The vector to transform</param>
- /// <param name="mat">The desired transformation</param>
- /// <returns>The transformed vector</returns>
- public static Vector3 TransformPerspective(this Vector3 vec, Matrix4X4 mat)
- {
- Vector3 result;
- TransformPerspective(vec, ref mat, out result);
- return result;
- }
- /// <summary>Transform a Vector3d by the given Matrix, and project the resulting Vector4d back to a Vector3d</summary>
- /// <param name="vec">The vector to transform</param>
- /// <param name="mat">The desired transformation</param>
- /// <param name="result">The transformed vector</param>
- public static void TransformPerspective(this Vector3 vec, ref Matrix4X4 mat, out Vector3 result)
- {
- Vector4 v = new Vector4(vec);
- Vector4.Transform(v, ref mat, out v);
- result.X = v.X / v.W;
- result.Y = v.Y / v.W;
- result.Z = v.Z / v.W;
- }
- #endregion Transform
- public static Vector3 Abs(this Vector3 a)
- {
- return Vector3.Abs(a);
- }
- }
- }
|