Completed the OpenTK.Math Vector[234] overhaul. Added LengthFast and NormalizeFast functions. Removed overloads. Made the pointer conversions explicit.

This commit is contained in:
the_fiddler 2007-10-24 23:40:06 +00:00
parent 1d8e5545da
commit 95da49b8e8
3 changed files with 201 additions and 224 deletions

View file

@ -160,7 +160,7 @@ namespace OpenTK.Math
{ {
get get
{ {
return OpenTK.Math.Functions.InverseSqrtFast(X * X + Y * Y); return 1.0f / OpenTK.Math.Functions.InverseSqrtFast(X * X + Y * Y);
} }
} }
@ -195,8 +195,10 @@ namespace OpenTK.Math
/// <returns>The normalized version of the current vector.</returns> /// <returns>The normalized version of the current vector.</returns>
public Vector2 Normalize() public Vector2 Normalize()
{ {
float length = this.Length; float scale = 1.0f / this.Length;
return new Vector2(X / length, Y / Length); X *= scale;
Y *= scale;
return this;
} }
#endregion #endregion
@ -204,14 +206,14 @@ namespace OpenTK.Math
#region public Vector2 NormalizeFast() #region public Vector2 NormalizeFast()
/// <summary> /// <summary>
/// Scales the Vector2 to unit length. /// Scales the Vector2 to approximately unit length.
/// </summary> /// </summary>
/// <returns>The normalized version of the current vector.</returns> /// <returns>The normalized version of the current vector.</returns>
public Vector2 NormalizeFast() public Vector2 NormalizeFast()
{ {
float length = this.LengthFast; float scale = Functions.InverseSqrtFast(X * X + Y * Y);
this.X = X / length; X *= scale;
this.Y = Y / length; Y *= scale;
return this; return this;
} }
@ -249,11 +251,19 @@ namespace OpenTK.Math
} }
[CLSCompliant(false)] [CLSCompliant(false)]
unsafe public static implicit operator float*(Vector2 v) unsafe public static explicit operator float*(Vector2 v)
{ {
return &v.X; return &v.X;
} }
public static explicit operator IntPtr(Vector2 v)
{
unsafe
{
return (IntPtr)(&v.X);
}
}
#endregion #endregion
#region public override string ToString() #region public override string ToString()

View file

@ -11,9 +11,9 @@ using System.Runtime.InteropServices;
namespace OpenTK.Math namespace OpenTK.Math
{ {
/// <summary> /// <summary>
/// Represents a three-dimensional vector. /// Represents a three-dimensional vector.
/// </summary> /// </summary>
[StructLayout(LayoutKind.Sequential)] [StructLayout(LayoutKind.Sequential)]
public struct Vector3 public struct Vector3
{ {
@ -88,51 +88,24 @@ namespace OpenTK.Math
#region Functions #region Functions
#region Add #region public Vector3 Add(Vector3 right)
/// <summary>
/// Adds the given Vector2 to the current Vector3.
/// </summary>
/// <param name="right">The right operand of the addition.</param>
/// <returns>A new Vector3 containing the result of the addition.</returns>
public Vector3 Add(Vector2 right)
{
return new Vector3(X + right.X, Y + right.Y, Z);
}
/// <summary> /// <summary>
/// Adds the given Vector3 to the current Vector3. /// Adds the given Vector3 to the current Vector3.
/// </summary> /// </summary>
/// <param name="right">The right operand of the addition.</param> /// <param name="right">The right operand of the addition.</param>
/// <returns>A new Vector3 containing the result of the addition.</returns> /// <returns>The current Vector3, containing the result of the addition.</returns>
public Vector3 Add(Vector3 right) public Vector3 Add(Vector3 right)
{ {
return new Vector3(X + right.X, Y + right.Y, Z + right.Z); X += right.X;
} Y += right.Y;
Z += right.Z;
/// <summary> return this;
/// Adds the given Vector4 to the current Vector3. W-coordinate remains unaffected.
/// </summary>
/// <param name="right">The right operand of the addition.</param>
/// <returns>A new Vector4 containing the result of the addition.</returns>
public Vector4 Add(Vector4 right)
{
return new Vector4(X + right.X, Y + right.Y, Z + right.Z, right.W);
} }
#endregion #endregion
#region Sub #region public Vector3 Sub(Vector3 right)
/// <summary>
/// Subtracts the given Vector2 from the current Vector3.
/// </summary>
/// <param name="right">The right operand of the subtraction.</param>
/// <returns>A new Vector3 containing the result of the subtraction.</returns>
public Vector3 Sub(Vector2 right)
{
return new Vector3(X - right.X, Y - right.Y, Z);
}
/// <summary> /// <summary>
/// Subtracts the given Vector3 from the current Vector3. /// Subtracts the given Vector3 from the current Vector3.
@ -141,32 +114,15 @@ namespace OpenTK.Math
/// <returns>A new Vector3 containing the result of the subtraction.</returns> /// <returns>A new Vector3 containing the result of the subtraction.</returns>
public Vector3 Sub(Vector3 right) public Vector3 Sub(Vector3 right)
{ {
return new Vector3(X - right.X, Y - right.Y, Z - right.Z); X -= right.X;
} Y -= right.Y;
Z -= right.Z;
/// <summary> return this;
/// Subtracts the given Vector4 from the current Vector3.
/// </summary>
/// <param name="right">The right operand of the subtraction.</param>
/// <returns>A new Vector4 containing the result of the subtraction.</returns>
public Vector4 Sub(Vector4 right)
{
return new Vector4(X - right.X, Y - right.Y, Z - right.Z, -right.W);
} }
#endregion #endregion
#region Dot #region public float Dot(Vector3 right)
/// <summary>
/// Computes the dot product between the current Vector3 and the given Vector2.
/// </summary>
/// <param name="right">The right operand of the dot product.</param>
/// <returns>A float containing the result of the dot product.</returns>
public float Dot(Vector2 right)
{
return X * right.X + Y * right.Y;
}
/// <summary> /// <summary>
/// Computes the dot product between the current Vector3 and the given Vector3. /// Computes the dot product between the current Vector3 and the given Vector3.
@ -178,31 +134,25 @@ namespace OpenTK.Math
return X * right.X + Y * right.Y + Z * right.Z; return X * right.X + Y * right.Y + Z * right.Z;
} }
/// <summary>
/// Computes the dot product between the current Vector3 and the given Vector4.
/// </summary>
/// <param name="right">The right operand of the dot product.</param>
/// <returns>A float containing the result of the dot product.</returns>
public float Dot(Vector4 right)
{
return X * right.X + Y * right.Y + Z * right.Z;
}
#endregion #endregion
#region Cross #region public Vector3 Cross(Vector3 right)
/// <summary> /// <summary>
/// Computes the cross product between the current and the given Vector3. /// Computes the cross product between the current and the given Vector3. The current Vector3 is set to the result of the computation.
/// </summary> /// </summary>
/// <param name="right">The right operand of the cross product</param> /// <param name="right">The right operand of the cross product</param>
/// <returns>A new Vector3 containing the result of the computation.</returns> /// <returns>The current </returns>
public Vector3 Cross(Vector3 right) public Vector3 Cross(Vector3 right)
{ {
return new Vector3( float
Y * right.Z - Z * right.Y, x = Y * right.Z - Z * right.Y,
Z * right.X - X * right.Z, y = Z * right.X - X * right.Z,
X * right.Y - Y * right.X); z = X * right.Y - Y * right.X;
X = x;
Y = y;
Z = z;
return this;
} }
#endregion #endregion
@ -210,13 +160,37 @@ namespace OpenTK.Math
#region public float Length #region public float Length
/// <summary> /// <summary>
/// Gets the length of the vector. /// Gets the length (magnitude) of the vector.
/// </summary> /// </summary>
/// <see cref="FastLength"/>
/// <seealso cref="LengthSquared"/>
public float Length public float Length
{ {
get get
{ {
return (float)System.Math.Sqrt(this.LengthSquared); return (float)System.Math.Sqrt(X * X + Y * Y + Z * Z);
}
}
#endregion
#region public float LengthFast
/// <summary>
/// Gets an approximation of the vector length (magnitude).
/// </summary>
/// <remarks>
/// This property uses an approximation of the square root function to calculate vector magnitude, with
/// an upper error bound of 0.001.
/// </remarks>
/// <see cref="Length"/>
/// <seealso cref="LengthSquared"/>
/// <seealso cref="OpenTK.Math.FastSqrt"/>
public float LengthFast
{
get
{
return 1.0f / OpenTK.Math.Functions.InverseSqrtFast(X * X + Y * Y + Z * Z);
} }
} }
@ -225,8 +199,14 @@ namespace OpenTK.Math
#region public float LengthSquared #region public float LengthSquared
/// <summary> /// <summary>
/// Gets the square of the vector length. /// Gets the square of the vector length (magnitude).
/// </summary> /// </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="FastLength"/>
public float LengthSquared public float LengthSquared
{ {
get get
@ -245,8 +225,28 @@ namespace OpenTK.Math
/// <returns>The normalized version of the current vector.</returns> /// <returns>The normalized version of the current vector.</returns>
public Vector3 Normalize() public Vector3 Normalize()
{ {
float length = this.Length; float scale = 1.0f / this.Length;
return new Vector3(X / length, Y / Length, Z / length); X *= scale;
Y *= scale;
Z *= scale;
return this;
}
#endregion
#region public Vector3 NormalizeFast()
/// <summary>
/// Scales the Vector3 to approximately unit length.
/// </summary>
/// <returns>The normalized version of the current vector.</returns>
public Vector3 NormalizeFast()
{
float scale = Functions.InverseSqrtFast(X * X + Y * Y + Z * Z);
X *= scale;
Y *= scale;
Z *= scale;
return this;
} }
#endregion #endregion
@ -259,10 +259,13 @@ namespace OpenTK.Math
/// <param name="sx">The scale of the X component.</param> /// <param name="sx">The scale of the X component.</param>
/// <param name="sy">The scale of the Y component.</param> /// <param name="sy">The scale of the Y component.</param>
/// <param name="sz">The scale of the Z component.</param> /// <param name="sz">The scale of the Z component.</param>
/// <returns>A new, scaled Vector3.</returns> /// <returns>The current Vector3, scaled.</returns>
public Vector3 Scale(float sx, float sy, float sz) public Vector3 Scale(float sx, float sy, float sz)
{ {
return new Vector3(X * sx, Y * sy, Z * sz); this.X = X * sx;
this.Y = Y * sy;
this.Z = Z * sz;
return this;
} }
#endregion #endregion
@ -271,42 +274,34 @@ namespace OpenTK.Math
#region Operator overloads #region Operator overloads
public static Vector3 operator +(Vector3 left, Vector2 right)
{
return left.Add(right);
}
public static Vector3 operator +(Vector3 left, Vector3 right) public static Vector3 operator +(Vector3 left, Vector3 right)
{ {
return left.Add(right); return new Vector3(left.Add(right));
}
public static Vector4 operator +(Vector3 left, Vector4 right)
{
return left.Add(right);
}
public static Vector3 operator -(Vector3 left, Vector2 right)
{
return left.Sub(right);
} }
public static Vector3 operator -(Vector3 left, Vector3 right) public static Vector3 operator -(Vector3 left, Vector3 right)
{ {
return left.Sub(right); return new Vector3(left.Sub(right));
}
public static Vector4 operator -(Vector3 left, Vector4 right)
{
return left.Sub(right);
} }
[CLSCompliant(false)] [CLSCompliant(false)]
unsafe public static implicit operator float*(Vector3 v) unsafe public static explicit operator float*(Vector3 v)
{ {
return &v.X; return &v.X;
} }
public static explicit operator IntPtr(Vector3 v)
{
unsafe
{
return (IntPtr)(&v.X);
}
}
#endregion
#region Static functions
#endregion #endregion
public override string ToString() public override string ToString()

View file

@ -98,27 +98,7 @@ namespace OpenTK.Math
#region Functions #region Functions
#region Add #region public Vector4 Add(Vector4 right)
/// <summary>
/// Adds the given Vector2 to the current Vector4.
/// </summary>
/// <param name="right">The right operand of the addition.</param>
/// <returns>A new Vector4 containing the result of the addition.</returns>
public Vector4 Add(Vector2 right)
{
return new Vector4(X + right.X, Y + right.Y, Z, W);
}
/// <summary>
/// Adds the given Vector3 to the current Vector4.
/// </summary>
/// <param name="right">The right operand of the addition.</param>
/// <returns>A new Vector4 containing the result of the addition.</returns>
public Vector4 Add(Vector3 right)
{
return new Vector4(X + right.X, Y + right.Y, Z + right.Z, W);
}
/// <summary> /// <summary>
/// Adds the given Vector4 to the current Vector4. W-coordinate remains unaffected. /// Adds the given Vector4 to the current Vector4. W-coordinate remains unaffected.
@ -127,32 +107,16 @@ namespace OpenTK.Math
/// <returns>A new Vector4 containing the result of the addition.</returns> /// <returns>A new Vector4 containing the result of the addition.</returns>
public Vector4 Add(Vector4 right) public Vector4 Add(Vector4 right)
{ {
return new Vector4(X + right.X, Y + right.Y, Z + right.Z, W + right.W); X += right.X;
Y += right.Y;
Z += right.Z;
W += right.W;
return this;
} }
#endregion #endregion
#region Sub #region public Vector4 Sub(Vector4 right)
/// <summary>
/// Subtracts the given Vector2 from the current Vector4.
/// </summary>
/// <param name="right">The right operand of the subtraction.</param>
/// <returns>A new Vector4 containing the result of the subtraction.</returns>
public Vector4 Sub(Vector2 right)
{
return new Vector4(X - right.X, Y - right.Y, Z, W);
}
/// <summary>
/// Subtracts the given Vector3 from the current Vector4.
/// </summary>
/// <param name="right">The right operand of the subtraction.</param>
/// <returns>A new Vector4 containing the result of the subtraction.</returns>
public Vector4 Sub(Vector3 right)
{
return new Vector4(X - right.X, Y - right.Y, Z - right.Z, W);
}
/// <summary> /// <summary>
/// Subtracts the given Vector4 from the current Vector4. /// Subtracts the given Vector4 from the current Vector4.
@ -161,32 +125,16 @@ namespace OpenTK.Math
/// <returns>A new Vector4 containing the result of the subtraction.</returns> /// <returns>A new Vector4 containing the result of the subtraction.</returns>
public Vector4 Sub(Vector4 right) public Vector4 Sub(Vector4 right)
{ {
return new Vector4(X - right.X, Y - right.Y, Z - right.Z, W - right.W); X -= right.X;
Y -= right.Y;
Z -= right.Z;
W -= right.W;
return this;
} }
#endregion #endregion
#region Dot #region public float Dot(Vector4 right)
/// <summary>
/// Computes the dot product between the current Vector4 and the given Vector2.
/// </summary>
/// <param name="right">The right operand of the dot product.</param>
/// <returns>A float containing the result of the dot product.</returns>
public float Dot(Vector2 right)
{
return X * right.X + Y * right.Y;
}
/// <summary>
/// Computes the dot product between the current Vector4 and the given Vector3.
/// </summary>
/// <param name="right">The right operand of the dot product.</param>
/// <returns>A float containing the result of the dot product.</returns>
public float Dot(Vector3 right)
{
return X * right.X + Y * right.Y + Z * right.Z;
}
/// <summary> /// <summary>
/// Computes the dot product between the current Vector4 and the given Vector4. /// Computes the dot product between the current Vector4 and the given Vector4.
@ -199,34 +147,41 @@ namespace OpenTK.Math
} }
#endregion #endregion
/*
#region Cross
/// <summary>
/// Computes the cross product between the current and the given Vector3.
/// </summary>
/// <param name="right">The right operand of the cross product</param>
/// <returns>A new Vector3 containing the result of the computation.</returns>
public Vector3 Cross(Vector3 right)
{
return new Vector3(
Y * right.Z - Z * right.Y,
Z * right.X - X * right.Z,
X * right.Y - Y * right.X);
}
#endregion
*/
#region public float Length #region public float Length
/// <summary> /// <summary>
/// Gets the length of the vector. /// Gets the length (magnitude) of the vector.
/// </summary> /// </summary>
/// <see cref="FastLength"/>
/// <seealso cref="LengthSquared"/>
public float Length public float Length
{ {
get get
{ {
return (float)System.Math.Sqrt(this.LengthSquared); return (float)System.Math.Sqrt(X * X + Y * Y + Z * Z + W * W);
}
}
#endregion
#region public float LengthFast
/// <summary>
/// Gets an approximation of the vector length (magnitude).
/// </summary>
/// <remarks>
/// This property uses an approximation of the square root function to calculate vector magnitude, with
/// an upper error bound of 0.001.
/// </remarks>
/// <see cref="Length"/>
/// <seealso cref="LengthSquared"/>
/// <seealso cref="OpenTK.Math.FastSqrt"/>
public float LengthFast
{
get
{
return 1.0f / OpenTK.Math.Functions.InverseSqrtFast(X * X + Y * Y + Z * Z + W * W);
} }
} }
@ -235,8 +190,14 @@ namespace OpenTK.Math
#region public float LengthSquared #region public float LengthSquared
/// <summary> /// <summary>
/// Gets the square of the vector length. /// Gets the square of the vector length (magnitude).
/// </summary> /// </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="FastLength"/>
public float LengthSquared public float LengthSquared
{ {
get get
@ -255,13 +216,35 @@ namespace OpenTK.Math
/// <returns>The normalized version of the current vector.</returns> /// <returns>The normalized version of the current vector.</returns>
public Vector4 Normalize() public Vector4 Normalize()
{ {
float length = this.Length; float scale = 1.0f / this.Length;
return new Vector4(X / length, Y / Length, Z / length, W / length); X *= scale;
Y *= scale;
Z *= scale;
W *= scale;
return this;
} }
#endregion #endregion
#region public Vector2 Scale(float sx, float sy, float sz, float sw) #region public Vector4 NormalizeFast()
/// <summary>
/// Scales the Vector4 to approximately unit length.
/// </summary>
/// <returns>The normalized version of the current vector.</returns>
public Vector4 NormalizeFast()
{
float scale = Functions.InverseSqrtFast(X * X + Y * Y + Z * Z);
X *= scale;
Y *= scale;
Z *= scale;
W *= scale;
return this;
}
#endregion
#region public Vector4 Scale(float sx, float sy, float sz, float sw)
/// <summary> /// <summary>
/// Scales the current Vector4 by the given amounts. /// Scales the current Vector4 by the given amounts.
@ -270,7 +253,7 @@ namespace OpenTK.Math
/// <param name="sy">The scale of the Y component.</param> /// <param name="sy">The scale of the Y component.</param>
/// <param name="sz">The scale of the Z component.</param> /// <param name="sz">The scale of the Z component.</param>
/// <param name="sw">The scale of the Z component.</param> /// <param name="sw">The scale of the Z component.</param>
/// <returns>A new, scaled Vector4.</returns> /// <returns>The current Vector4, scaled.</returns>
public Vector4 Scale(float sx, float sy, float sz, float sw) public Vector4 Scale(float sx, float sy, float sz, float sw)
{ {
return new Vector4(X * sx, Y * sy, Z * sz, W * sw); return new Vector4(X * sx, Y * sy, Z * sz, W * sw);
@ -282,42 +265,31 @@ namespace OpenTK.Math
#region Operator overloads #region Operator overloads
public static Vector4 operator +(Vector4 left, Vector2 right)
{
return left.Add(right);
}
public static Vector4 operator +(Vector4 left, Vector3 right)
{
return left.Add(right);
}
public static Vector4 operator +(Vector4 left, Vector4 right) public static Vector4 operator +(Vector4 left, Vector4 right)
{ {
return left.Add(right); return left.Add(right);
} }
public static Vector4 operator -(Vector4 left, Vector2 right)
{
return left.Sub(right);
}
public static Vector4 operator -(Vector4 left, Vector3 right)
{
return left.Sub(right);
}
public static Vector4 operator -(Vector4 left, Vector4 right) public static Vector4 operator -(Vector4 left, Vector4 right)
{ {
return left.Sub(right); return left.Sub(right);
} }
[CLSCompliant(false)] [CLSCompliant(false)]
unsafe public static implicit operator float*(Vector4 v) unsafe public static explicit operator float*(Vector4 v)
{ {
return &v.X; return &v.X;
} }
public static explicit operator IntPtr(Vector4 v)
{
unsafe
{
return (IntPtr)(&v.X);
}
}
#endregion #endregion
public override string ToString() public override string ToString()