remove non-static Normalize + return identity on zero vector
parent
80f3711f4c
commit
8209051a3c
|
@ -996,8 +996,8 @@ namespace MoonWorks.Math.Fixed
|
|||
z = Vector3.Normalize(forward);
|
||||
Vector3.Cross(ref forward, ref up, out x);
|
||||
Vector3.Cross(ref x, ref forward, out y);
|
||||
x.Normalize();
|
||||
y.Normalize();
|
||||
x = Vector3.Normalize(x);
|
||||
y = Vector3.Normalize(y);
|
||||
|
||||
result = new Matrix4x4();
|
||||
result.Right = x;
|
||||
|
|
|
@ -214,23 +214,6 @@ namespace MoonWorks.Math.Fixed
|
|||
);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Scales the quaternion magnitude to unit length.
|
||||
/// </summary>
|
||||
public void Normalize()
|
||||
{
|
||||
Fix64 num = Fix64.One / (Fix64.Sqrt(
|
||||
(X * X) +
|
||||
(Y * Y) +
|
||||
(Z * Z) +
|
||||
(W * W)
|
||||
));
|
||||
this.X *= num;
|
||||
this.Y *= num;
|
||||
this.Z *= num;
|
||||
this.W *= num;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="String"/> representation of this <see cref="Quaternion"/> in the format:
|
||||
/// {X:[<see cref="X"/>] Y:[<see cref="Y"/>] Z:[<see cref="Z"/>] W:[<see cref="W"/>]}
|
||||
|
@ -759,12 +742,16 @@ namespace MoonWorks.Math.Fixed
|
|||
/// <param name="result">The unit length quaternion an output parameter.</param>
|
||||
public static void Normalize(ref Quaternion quaternion, out Quaternion result)
|
||||
{
|
||||
Fix64 num = Fix64.One / (Fix64.Sqrt(
|
||||
(quaternion.X * quaternion.X) +
|
||||
(quaternion.Y * quaternion.Y) +
|
||||
(quaternion.Z * quaternion.Z) +
|
||||
(quaternion.W * quaternion.W)
|
||||
));
|
||||
Fix64 lengthSquared = (quaternion.X * quaternion.X) + (quaternion.Y * quaternion.Y) +
|
||||
(quaternion.Z * quaternion.Z) + (quaternion.W * quaternion.W);
|
||||
|
||||
if (lengthSquared == Fix64.Zero)
|
||||
{
|
||||
result = Identity;
|
||||
return;
|
||||
}
|
||||
|
||||
Fix64 num = Fix64.One / Fix64.Sqrt(lengthSquared);
|
||||
result.X = quaternion.X * num;
|
||||
result.Y = quaternion.Y * num;
|
||||
result.Z = quaternion.Z * num;
|
||||
|
|
|
@ -1,105 +0,0 @@
|
|||
namespace MoonWorks.Math.Fixed
|
||||
{
|
||||
public struct Transform2D : System.IEquatable<Transform2D>
|
||||
{
|
||||
public Vector2 Position { get; }
|
||||
public Fix64 Rotation { get; }
|
||||
public Vector2 Scale { get; }
|
||||
|
||||
private bool transformMatrixCalculated;
|
||||
private Matrix3x2 transformMatrix;
|
||||
|
||||
public Matrix3x2 TransformMatrix
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!transformMatrixCalculated)
|
||||
{
|
||||
transformMatrix = CreateTransformMatrix(Position, Rotation, Scale);
|
||||
transformMatrixCalculated = true;
|
||||
}
|
||||
|
||||
return transformMatrix;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsAxisAligned => Rotation % Fix64.PiOver2 == Fix64.Zero;
|
||||
public bool IsUniformScale => Scale.X == Scale.Y || Scale.X == -Scale.Y;
|
||||
|
||||
public static readonly Transform2D Identity = new Transform2D(Vector2.Zero, Fix64.Zero, Vector2.One);
|
||||
|
||||
public Transform2D(Vector2 position)
|
||||
{
|
||||
Position = position;
|
||||
Rotation = Fix64.Zero;
|
||||
Scale = Vector2.One;
|
||||
transformMatrixCalculated = false;
|
||||
transformMatrix = Matrix3x2.Identity;
|
||||
}
|
||||
|
||||
public Transform2D(Vector2 position, Fix64 rotation)
|
||||
{
|
||||
Position = position;
|
||||
Rotation = rotation;
|
||||
Scale = Vector2.One;
|
||||
transformMatrixCalculated = false;
|
||||
transformMatrix = Matrix3x2.Identity;
|
||||
}
|
||||
|
||||
public Transform2D(Vector2 position, Fix64 rotation, Vector2 scale)
|
||||
{
|
||||
Position = position;
|
||||
Rotation = rotation;
|
||||
Scale = scale;
|
||||
transformMatrixCalculated = false;
|
||||
transformMatrix = Matrix3x2.Identity;
|
||||
}
|
||||
|
||||
public Transform2D Compose(Transform2D other)
|
||||
{
|
||||
return new Transform2D(Position + other.Position, Rotation + other.Rotation, Scale * other.Scale);
|
||||
}
|
||||
|
||||
private static Matrix3x2 CreateTransformMatrix(Vector2 position, Fix64 rotation, Vector2 scale)
|
||||
{
|
||||
return
|
||||
Matrix3x2.CreateScale(scale) *
|
||||
Matrix3x2.CreateRotation(rotation) *
|
||||
Matrix3x2.CreateTranslation(position);
|
||||
}
|
||||
|
||||
public bool Equals(Transform2D other)
|
||||
{
|
||||
return
|
||||
Position == other.Position &&
|
||||
Rotation == other.Rotation &&
|
||||
Scale == other.Scale;
|
||||
}
|
||||
|
||||
|
||||
public override bool Equals(System.Object other)
|
||||
{
|
||||
if (other is Transform2D otherTransform)
|
||||
{
|
||||
return Equals(otherTransform);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return System.HashCode.Combine(Position, Rotation, Scale);
|
||||
}
|
||||
|
||||
public static bool operator ==(Transform2D a, Transform2D b)
|
||||
{
|
||||
return a.Equals(b);
|
||||
}
|
||||
|
||||
public static bool operator !=(Transform2D a, Transform2D b)
|
||||
{
|
||||
return !a.Equals(b);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -200,16 +200,6 @@ namespace MoonWorks.Math.Fixed
|
|||
return (X * X) + (Y * Y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Turns this <see cref="Vector2"/> to a unit vector with the same direction.
|
||||
/// </summary>
|
||||
public void Normalize()
|
||||
{
|
||||
Fix64 val = Fix64.One / Fix64.Sqrt((X * X) + (Y * Y));
|
||||
X *= val;
|
||||
Y *= val;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Turns this <see cref="Vector2"/> to an angle in radians.
|
||||
/// </summary>
|
||||
|
@ -423,7 +413,14 @@ namespace MoonWorks.Math.Fixed
|
|||
/// <returns>Unit vector.</returns>
|
||||
public static Vector2 Normalize(Vector2 value)
|
||||
{
|
||||
Fix64 val = Fix64.One / Fix64.Sqrt((value.X * value.X) + (value.Y * value.Y));
|
||||
Fix64 lengthSquared = (value.X * value.X) + (value.Y * value.Y);
|
||||
|
||||
if (lengthSquared == Fix64.Zero)
|
||||
{
|
||||
return Zero;
|
||||
}
|
||||
|
||||
Fix64 val = Fix64.One / Fix64.Sqrt(lengthSquared);
|
||||
value.X *= val;
|
||||
value.Y *= val;
|
||||
return value;
|
||||
|
|
|
@ -309,21 +309,6 @@ namespace MoonWorks.Math.Fixed
|
|||
return (X * X) + (Y * Y) + (Z * Z);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Turns this <see cref="Vector3"/> to a unit vector with the same direction.
|
||||
/// </summary>
|
||||
public void Normalize()
|
||||
{
|
||||
Fix64 factor = Fix64.One / Fix64.Sqrt(
|
||||
(X * X) +
|
||||
(Y * Y) +
|
||||
(Z * Z)
|
||||
);
|
||||
X *= factor;
|
||||
Y *= factor;
|
||||
Z *= factor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="String"/> representation of this <see cref="Vector3"/> in the format:
|
||||
/// {X:[<see cref="X"/>] Y:[<see cref="Y"/>] Z:[<see cref="Z"/>]}
|
||||
|
@ -733,11 +718,14 @@ namespace MoonWorks.Math.Fixed
|
|||
/// <returns>Unit vector.</returns>
|
||||
public static Vector3 Normalize(Vector3 value)
|
||||
{
|
||||
Fix64 factor = Fix64.One / Fix64.Sqrt(
|
||||
(value.X * value.X) +
|
||||
(value.Y * value.Y) +
|
||||
(value.Z * value.Z)
|
||||
);
|
||||
Fix64 lengthSquared = (value.X * value.X) + (value.Y * value.Y) + (value.Z * value.Z);
|
||||
|
||||
if (lengthSquared == Fix64.Zero)
|
||||
{
|
||||
return Zero;
|
||||
}
|
||||
|
||||
Fix64 factor = Fix64.One / Fix64.Sqrt(lengthSquared);
|
||||
return new Vector3(
|
||||
value.X * factor,
|
||||
value.Y * factor,
|
||||
|
|
|
@ -611,7 +611,7 @@ namespace MoonWorks.Math.Float
|
|||
);
|
||||
}
|
||||
Vector3.Cross(ref cameraUpVector, ref vector, out vector3);
|
||||
vector3.Normalize();
|
||||
vector3 = Vector3.Normalize(vector3);
|
||||
Vector3.Cross(ref vector, ref vector3, out vector2);
|
||||
result.M11 = vector3.X;
|
||||
result.M12 = vector3.Y;
|
||||
|
@ -730,16 +730,16 @@ namespace MoonWorks.Math.Float
|
|||
Vector3.Forward;
|
||||
}
|
||||
Vector3.Cross(ref rotateAxis, ref vector, out vector3);
|
||||
vector3.Normalize();
|
||||
vector3 = Vector3.Normalize(vector3);
|
||||
Vector3.Cross(ref vector3, ref rotateAxis, out vector);
|
||||
vector.Normalize();
|
||||
vector = Vector3.Normalize(vector);
|
||||
}
|
||||
else
|
||||
{
|
||||
Vector3.Cross(ref rotateAxis, ref vector2, out vector3);
|
||||
vector3.Normalize();
|
||||
vector3 = Vector3.Normalize(vector3);
|
||||
Vector3.Cross(ref vector3, ref vector4, out vector);
|
||||
vector.Normalize();
|
||||
vector = Vector3.Normalize(vector);
|
||||
}
|
||||
|
||||
result.M11 = vector3.X;
|
||||
|
@ -1701,8 +1701,8 @@ namespace MoonWorks.Math.Float
|
|||
Vector3.Normalize(ref forward, out z);
|
||||
Vector3.Cross(ref forward, ref up, out x);
|
||||
Vector3.Cross(ref x, ref forward, out y);
|
||||
x.Normalize();
|
||||
y.Normalize();
|
||||
x = Vector3.Normalize(x);
|
||||
y = Vector3.Normalize(y);
|
||||
|
||||
result = new Matrix4x4();
|
||||
result.Right = x;
|
||||
|
|
|
@ -1,105 +0,0 @@
|
|||
namespace MoonWorks.Math.Float
|
||||
{
|
||||
public struct Transform2D : System.IEquatable<Transform2D>
|
||||
{
|
||||
public Vector2 Position { get; }
|
||||
public float Rotation { get; }
|
||||
public Vector2 Scale { get; }
|
||||
|
||||
private bool transformMatrixCalculated;
|
||||
private Matrix3x2 transformMatrix;
|
||||
|
||||
public Matrix3x2 TransformMatrix
|
||||
{
|
||||
get
|
||||
{
|
||||
if (!transformMatrixCalculated)
|
||||
{
|
||||
transformMatrix = CreateTransformMatrix(Position, Rotation, Scale);
|
||||
transformMatrixCalculated = true;
|
||||
}
|
||||
|
||||
return transformMatrix;
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsAxisAligned => Rotation % MathHelper.PiOver2 == 0;
|
||||
public bool IsUniformScale => Scale.X == Scale.Y;
|
||||
|
||||
public static readonly Transform2D Identity = new Transform2D(Vector2.Zero, 0, Vector2.One);
|
||||
|
||||
public Transform2D(Vector2 position)
|
||||
{
|
||||
Position = position;
|
||||
Rotation = 0;
|
||||
Scale = Vector2.One;
|
||||
transformMatrixCalculated = false;
|
||||
transformMatrix = Matrix3x2.Identity;
|
||||
}
|
||||
|
||||
public Transform2D(Vector2 position, float rotation)
|
||||
{
|
||||
Position = position;
|
||||
Rotation = rotation;
|
||||
Scale = Vector2.One;
|
||||
transformMatrixCalculated = false;
|
||||
transformMatrix = Matrix3x2.Identity;
|
||||
}
|
||||
|
||||
public Transform2D(Vector2 position, float rotation, Vector2 scale)
|
||||
{
|
||||
Position = position;
|
||||
Rotation = rotation;
|
||||
Scale = scale;
|
||||
transformMatrixCalculated = false;
|
||||
transformMatrix = Matrix3x2.Identity;
|
||||
}
|
||||
|
||||
public Transform2D Compose(Transform2D other)
|
||||
{
|
||||
return new Transform2D(Position + other.Position, Rotation + other.Rotation, Scale * other.Scale);
|
||||
}
|
||||
|
||||
private static Matrix3x2 CreateTransformMatrix(Vector2 position, float rotation, Vector2 scale)
|
||||
{
|
||||
return
|
||||
Matrix3x2.CreateScale(scale) *
|
||||
Matrix3x2.CreateRotation(rotation) *
|
||||
Matrix3x2.CreateTranslation(position);
|
||||
}
|
||||
|
||||
public bool Equals(Transform2D other)
|
||||
{
|
||||
return
|
||||
Position == other.Position &&
|
||||
Rotation == other.Rotation &&
|
||||
Scale == other.Scale;
|
||||
}
|
||||
|
||||
|
||||
public override bool Equals(System.Object other)
|
||||
{
|
||||
if (other is Transform2D otherTransform)
|
||||
{
|
||||
return Equals(otherTransform);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return System.HashCode.Combine(Position, Rotation, Scale);
|
||||
}
|
||||
|
||||
public static bool operator ==(Transform2D a, Transform2D b)
|
||||
{
|
||||
return a.Equals(b);
|
||||
}
|
||||
|
||||
public static bool operator !=(Transform2D a, Transform2D b)
|
||||
{
|
||||
return !a.Equals(b);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -194,16 +194,6 @@ namespace MoonWorks.Math.Float
|
|||
return (X * X) + (Y * Y);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Turns this <see cref="Vector2"/> to a unit vector with the same direction.
|
||||
/// </summary>
|
||||
public void Normalize()
|
||||
{
|
||||
float val = 1.0f / (float) System.Math.Sqrt((X * X) + (Y * Y));
|
||||
X *= val;
|
||||
Y *= val;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Turns this <see cref="Vector2"/> to an angle in radians.
|
||||
/// </summary>
|
||||
|
@ -717,7 +707,14 @@ namespace MoonWorks.Math.Float
|
|||
/// <returns>Unit vector.</returns>
|
||||
public static Vector2 Normalize(Vector2 value)
|
||||
{
|
||||
float val = 1.0f / (float) System.Math.Sqrt((value.X * value.X) + (value.Y * value.Y));
|
||||
float lengthSquared = (value.X * value.X) + (value.Y * value.Y);
|
||||
|
||||
if (lengthSquared == 0)
|
||||
{
|
||||
return Zero;
|
||||
}
|
||||
|
||||
float val = 1.0f / System.MathF.Sqrt(lengthSquared);
|
||||
value.X *= val;
|
||||
value.Y *= val;
|
||||
return value;
|
||||
|
|
|
@ -302,21 +302,6 @@ namespace MoonWorks.Math.Float
|
|||
return (X * X) + (Y * Y) + (Z * Z);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Turns this <see cref="Vector3"/> to a unit vector with the same direction.
|
||||
/// </summary>
|
||||
public void Normalize()
|
||||
{
|
||||
float factor = 1.0f / (float) System.Math.Sqrt(
|
||||
(X * X) +
|
||||
(Y * Y) +
|
||||
(Z * Z)
|
||||
);
|
||||
X *= factor;
|
||||
Y *= factor;
|
||||
Z *= factor;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="String"/> representation of this <see cref="Vector3"/> in the format:
|
||||
/// {X:[<see cref="X"/>] Y:[<see cref="Y"/>] Z:[<see cref="Z"/>]}
|
||||
|
@ -900,11 +885,14 @@ namespace MoonWorks.Math.Float
|
|||
/// <returns>Unit vector.</returns>
|
||||
public static Vector3 Normalize(Vector3 value)
|
||||
{
|
||||
float factor = 1.0f / (float) System.Math.Sqrt(
|
||||
(value.X * value.X) +
|
||||
(value.Y * value.Y) +
|
||||
(value.Z * value.Z)
|
||||
);
|
||||
float lengthSquared = (value.X * value.X) + (value.Y * value.Y) + (value.Z * value.Z);
|
||||
|
||||
if (lengthSquared == 0f)
|
||||
{
|
||||
return Zero;
|
||||
}
|
||||
|
||||
float factor = 1.0f / System.MathF.Sqrt(lengthSquared);
|
||||
return new Vector3(
|
||||
value.X * factor,
|
||||
value.Y * factor,
|
||||
|
|
|
@ -267,23 +267,6 @@ namespace MoonWorks.Math.Float
|
|||
return (X * X) + (Y * Y) + (Z * Z) + (W * W);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Turns this <see cref="Vector4"/> to a unit vector with the same direction.
|
||||
/// </summary>
|
||||
public void Normalize()
|
||||
{
|
||||
float factor = 1.0f / (float) System.Math.Sqrt(
|
||||
(X * X) +
|
||||
(Y * Y) +
|
||||
(Z * Z) +
|
||||
(W * W)
|
||||
);
|
||||
X *= factor;
|
||||
Y *= factor;
|
||||
Z *= factor;
|
||||
W *= factor;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return (
|
||||
|
@ -853,12 +836,15 @@ namespace MoonWorks.Math.Float
|
|||
/// <returns>Unit vector.</returns>
|
||||
public static Vector4 Normalize(Vector4 vector)
|
||||
{
|
||||
float factor = 1.0f / (float) System.Math.Sqrt(
|
||||
(vector.X * vector.X) +
|
||||
(vector.Y * vector.Y) +
|
||||
(vector.Z * vector.Z) +
|
||||
(vector.W * vector.W)
|
||||
);
|
||||
var lengthSquared = (vector.X * vector.X) + (vector.Y * vector.Y) +
|
||||
(vector.Z * vector.Z) + (vector.W * vector.W);
|
||||
|
||||
if (lengthSquared == 0)
|
||||
{
|
||||
return Zero;
|
||||
}
|
||||
|
||||
float factor = 1.0f / System.MathF.Sqrt(lengthSquared);
|
||||
return new Vector4(
|
||||
vector.X * factor,
|
||||
vector.Y * factor,
|
||||
|
@ -870,16 +856,20 @@ namespace MoonWorks.Math.Float
|
|||
/// <summary>
|
||||
/// Creates a new <see cref="Vector4"/> that contains a normalized values from another vector.
|
||||
/// </summary>
|
||||
/// <param name="value">Source <see cref="Vector4"/>.</param>
|
||||
/// <param name="vector">Source <see cref="Vector4"/>.</param>
|
||||
/// <param name="result">Unit vector as an output parameter.</param>
|
||||
public static void Normalize(ref Vector4 vector, out Vector4 result)
|
||||
{
|
||||
float factor = 1.0f / (float) System.Math.Sqrt(
|
||||
(vector.X * vector.X) +
|
||||
(vector.Y * vector.Y) +
|
||||
(vector.Z * vector.Z) +
|
||||
(vector.W * vector.W)
|
||||
);
|
||||
float lengthSquared = (vector.X * vector.X) + (vector.Y * vector.Y) +
|
||||
(vector.Z * vector.Z) + (vector.W * vector.W);
|
||||
|
||||
if (lengthSquared == 0)
|
||||
{
|
||||
result = Zero;
|
||||
return;
|
||||
}
|
||||
|
||||
float factor = 1.0f / System.MathF.Sqrt(lengthSquared);
|
||||
result.X = vector.X * factor;
|
||||
result.Y = vector.Y * factor;
|
||||
result.Z = vector.Z * factor;
|
||||
|
|
Loading…
Reference in New Issue