/// <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 Vector3Float Clamp(Vector3Float vec, Vector3Float min, Vector3Float 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> /// 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 Vector3Float Cross(this Vector3Float left, Vector3Float right) { Vector3Float result; left.Cross(ref right, out result); return(result); }
/// <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 Vector3Float Transform(this Vector3Float vec, Matrix4X4 mat) { Vector3Float result; Transform(vec, ref mat, out result); return(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="norm">The normal to transform</param> /// <param name="invMat">The inverse of the desired transformation</param> /// <returns>The transformed normal</returns> public static Vector3Float TransformNormalInverse(this Vector3Float norm, Matrix4X4 invMat) { return(new Vector3Float( norm.Dot(new Vector3Float(invMat.Row0)), norm.Dot(new Vector3Float(invMat.Row1)), norm.Dot(new Vector3Float(invMat.Row2)))); }
/// <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 Vector3Float Lerp(Vector3Float a, Vector3Float b, float blend) { 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>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 Vector3Float TransformVector(this Vector3Float vec, Matrix4X4 mat) { return(new Vector3Float( vec.Dot(new Vector3Float(mat.Column0)), vec.Dot(new Vector3Float(mat.Column1)), vec.Dot(new Vector3Float(mat.Column2)))); }
/// <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(this Vector3Float first, ref Vector3Float second, out float result) { float temp; first.Dot(ref second, out temp); result = (float)Math.Acos(temp / (first.Length * second.Length)); }
/// <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 Vector3Float ComponentMax(this Vector3Float a, Vector3Float 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); }
/// <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 Vector3Float inVec, ref Matrix4X4 mat, out Vector3Float result) { Vector3 vec = new Vector3(inVec); Vector4 v4 = new Vector4(vec.X, vec.Y, vec.Z, 1.0); Vector4.Transform(v4, ref mat, out v4); result = new Vector3Float(v4.Xyz); }
/// <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 Vector3Float vec, out Vector3Float result) { float scale = 1.0f / vec.Length; result.X = vec.X * scale; result.Y = vec.Y * scale; result.Z = vec.Z * scale; }
/// <summary> /// Scale a vector to unit length /// </summary> /// <param name="vec">The input vector</param> /// <returns>The normalized vector</returns> public static Vector3Float Normalize(Vector3Float vec) { float scale = 1.0f / vec.Length; vec.X *= scale; vec.Y *= scale; vec.Z *= scale; return(vec); }
/// <summary> /// Transform a Vector3d by the given Matrix, and project the resulting Vector4 back to a Vector3Float /// </summary> /// <param name="vec">The vector to transform</param> /// <param name="mat">The desired transformation</param> /// <returns>The transformed vector</returns> public static Vector3Float TransformPerspective(this Vector3Float vec, Matrix4X4 mat) { #if true throw new NotImplementedException(); #else Vector3Float result; TransformPerspective(ref vec, ref mat, out result); return(result); #endif }
/// <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 Vector3Float Transform(this Vector3Float vec, Quaternion quat) { #if true throw new NotImplementedException(); #else Vector3Float result; Transform(ref vec, ref quat, out result); return(result); #endif }
/// <summary> /// Construct a quaternion that rotates from one direction to another /// </summary> /// <param name="startingDirection"></param> /// <param name="endingDirection"></param> public Quaternion(Vector3Float startingDirection, Vector3Float endingDirection) { if ((endingDirection + startingDirection).LengthSquared == 0) { endingDirection += new Vector3Float(.0000001, 0, 0); } this.xyz = new Vector3(endingDirection.Cross(startingDirection)); this.w = Math.Sqrt(Math.Pow(endingDirection.Length, 2) * Math.Pow(startingDirection.Length, 2)) + endingDirection.Dot(startingDirection); Normalize(); }
public void ExpandToInclude(Vector3Float position) { MinXYZ.X = Math.Min(MinXYZ.X, position.X); MinXYZ.Y = Math.Min(MinXYZ.Y, position.Y); MinXYZ.Z = Math.Min(MinXYZ.Z, position.Z); MaxXYZ.X = Math.Max(MaxXYZ.X, position.X); MaxXYZ.Y = Math.Max(MaxXYZ.Y, position.Y); MaxXYZ.Z = Math.Max(MaxXYZ.Z, position.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 Vector3Float TransformPosition(this Vector3Float pos, Matrix4X4 mat) { #if true throw new NotImplementedException(); #else return(new Vector3Float( Vector3Float.Dot(pos, new Vector3Float((float)mat.Column0)) + mat.Row3.x, Vector3Float.Dot(pos, new Vector3Float((float)mat.Column1)) + mat.Row3.y, Vector3Float.Dot(pos, new Vector3Float((float)mat.Column2)) + mat.Row3.z)); #endif }
/// <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 Vector3Float vec, ref Matrix4X4 mat, out Vector3Float result) { #if true throw new NotImplementedException(); #else Vector4 v = new Vector4(vec); Vector4.Transform(ref v, ref mat, out v); result.x = v.x / v.w; result.y = v.y / v.w; result.z = v.z / v.w; #endif }
/// <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 Vector3Float a, ref Vector3Float b, ref Vector3Float c, float u, float v, out Vector3Float result) { result = a; // copy Vector3Float 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); }
/// <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 Vector3Float vec, ref Quaternion quat, out Vector3Float result) { #if true throw new NotImplementedException(); #else // 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) Vector3Float xyz = quat.Xyz, temp, temp2; Vector3Float.Cross(ref xyz, ref vec, out temp); Vector3Float.Multiply(ref vec, quat.W, out temp2); Vector3Float.Add(ref temp, ref temp2, out temp); Vector3Float.Cross(ref xyz, ref temp, out temp); Vector3Float.Multiply(ref temp, 2, out temp); Vector3Float.Add(ref vec, ref temp, out result); #endif }
/// <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 Vector3Float vec, ref Matrix4X4 mat, out Vector3Float result) { #if true throw new NotImplementedException(); #else 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; #endif }
/// <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="norm">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 Vector3Float norm, ref Matrix4X4 invMat, out Vector3Float result) { #if true throw new NotImplementedException(); #else result.x = norm.x * invMat.Row0.x + norm.y * invMat.Row0.y + norm.z * invMat.Row0.z; result.y = norm.x * invMat.Row1.x + norm.y * invMat.Row1.y + norm.z * invMat.Row1.z; result.z = norm.x * invMat.Row2.x + norm.y * invMat.Row2.y + norm.z * invMat.Row2.z; #endif }
/// <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 Vector3Float pos, ref Matrix4X4 mat, out Vector3Float result) { #if true throw new NotImplementedException(); #else 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; #endif }
public Vector3(Vector3Float 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(Vector3Float v) { x = v.x; y = v.y; z = v.z; }
public AxisAlignedBoundingBox(Vector3Float minXYZ, Vector3Float maxXYZ) : this(new Vector3(minXYZ), new Vector3(maxXYZ)) { }
public Vector2(Vector3Float vector) { this.X = vector.X; this.Y = vector.Y; }
public Plane(Vector3Float point0, Vector3Float point1, Vector3Float point2) { this.Normal = new Vector3((point1 - point0).Cross(point2 - point0).GetNormal()); this.DistanceFromOrigin = Normal.Dot(new Vector3(point0)); }
public double GetDistanceFromPlane(Vector3Float positionToCheck) { return(GetDistanceFromPlane(new Vector3(positionToCheck))); }
/// <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 Vector3Float a, ref Vector3Float b, float blend, out Vector3Float 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; }