Extra maths features

- Added TranslationPart, ScalePart and RotationPart properties to
Matrix4
- Added Normalized() to Vector2/3/4, Quaternion and Matrix
This commit is contained in:
Tom Edwards 2013-02-18 18:46:26 +00:00
parent 6c35e8ef24
commit 11114ca4ea
9 changed files with 161 additions and 0 deletions

1
.gitignore vendored
View file

@ -3,6 +3,7 @@ Binaries/
OpenTK.userprefs OpenTK.userprefs
Source/GlobalAssemblyInfo.cs Source/GlobalAssemblyInfo.cs
Version.txt Version.txt
Source/OpenTK/OpenTK.xml
# OpenTK Resource files that seem like they should be ignored: # OpenTK Resource files that seem like they should be ignored:
Source/Compatibility/Properties/Resources.resources Source/Compatibility/Properties/Resources.resources

View file

@ -259,6 +259,93 @@ namespace OpenTK
/// Gets or sets the value at row 4, column 4 of this instance. /// Gets or sets the value at row 4, column 4 of this instance.
/// </summary> /// </summary>
public float M44 { get { return Row3.W; } set { Row3.W = value; } } public float M44 { get { return Row3.W; } set { Row3.W = value; } }
/// <summary>
/// Returns a copy of this instance with scale transform removed.
/// </summary>
public Matrix4 Normalized()
{
Matrix4 m = this;
m.Normalize();
return m;
}
/// <summary>
/// Removes scale transform from this instance.
/// </summary>
public void Normalize()
{
Row0.Xyz = Row0.Xyz.Normalized();
Row1.Xyz = Row1.Xyz.Normalized();
Row2.Xyz = Row2.Xyz.Normalized();
}
/// <summary>
/// Gets the translation component of this instance.
/// </summary>
public Vector3 TranslationPart { get { return Row3.Xyz; } }
/// <summary>
/// Gets the scale component of this instance.
/// </summary>
public Vector3 ScalePart { get { return new Vector3 (Row0.Length, Row1.Length, Row2.Length); } }
/// <summary>
/// Gets the rotation component of this instance. The Matrix MUST be normalized first.
/// </summary>
public Quaternion RotationPart
{
get
{
Quaternion q = new Quaternion();
// Adapted from Blender
double trace = 0.25 * (Row0[0] + Row1[1] + Row2[2] + 1.0);
if (trace > 0.0f)
{
double sq = Math.Sqrt(trace);
q.W = (float)sq;
sq = 1.0 / (4.0 * sq);
q.X = (float)((Row1[2] - Row2[1]) * sq);
q.Y = (float)((Row2[0] - Row0[2]) * sq);
q.Z = (float)((Row0[1] - Row1[0]) * sq);
}
else if (Row0[0] > Row1[1] && Row0[0] > Row2[2])
{
double sq = 2.0 * Math.Sqrt(1.0 + Row0[0] - Row1[1] - Row2[2]);
q.X = (float)(0.25 * sq);
sq = 1.0 / sq;
q.W = (float)((Row2[1] - Row1[2]) * sq);
q.Y = (float)((Row1[0] + Row0[1]) * sq);
q.Z = (float)((Row2[0] + Row0[2]) * sq);
}
else if (Row1[1] > Row2[2])
{
double sq = 2.0 * Math.Sqrt(1.0 + Row1[1] - Row0[0] - Row2[2]);
q.Y = (float)(0.25 * sq);
sq = 1.0 / sq;
q.W = (float)((Row2[0] - Row0[2]) * sq);
q.X = (float)((Row1[0] + Row0[1]) * sq);
q.Z = (float)((Row2[1] + Row1[2]) * sq);
}
else
{
double sq = 2.0 * Math.Sqrt(1.0 + Row2[2] - Row0[0] - Row1[1]);
q.Z = (float)(0.25 * sq);
sq = 1.0 / sq;
q.W = (float)((Row1[0] - Row0[1]) * sq);
q.X = (float)((Row2[0] + Row0[2]) * sq);
q.Y = (float)((Row2[1] + Row1[2]) * sq);
}
return q.Normalized();
}
}
#endregion #endregion

View file

@ -189,6 +189,17 @@ namespace OpenTK
#endregion #endregion
/// <summary>
/// Returns a copy of the Quaternion scaled to unit length.
/// </summary>
/// <returns></returns>
public Quaternion Normalized()
{
Quaternion q = this;
q.Normalize();
return q;
}
#region public void Normalize() #region public void Normalize()
/// <summary> /// <summary>

View file

@ -289,6 +289,16 @@ namespace OpenTK
#endregion #endregion
/// <summary>
/// Returns a copy of the Vector2 scaled to unit length.
/// </summary>
/// <returns></returns>
public Vector2 Normalized()
{
Vector2 v = this;
v.Normalize();
return v;
}
#region public void Normalize() #region public void Normalize()
/// <summary> /// <summary>

View file

@ -249,6 +249,17 @@ namespace OpenTK
#endregion #endregion
/// <summary>
/// Returns a copy of the Vector2d scaled to unit length.
/// </summary>
/// <returns></returns>
public Vector2d Normalized()
{
Vector2d v = this;
v.Normalize();
return v;
}
#region public void Normalize() #region public void Normalize()
/// <summary> /// <summary>

View file

@ -277,6 +277,16 @@ namespace OpenTK
#endregion #endregion
/// <summary>
/// Returns a copy of the Vector3 scaled to unit length.
/// </summary>
public Vector3 Normalized()
{
Vector3 v = this;
v.Normalize();
return v;
}
#region public void Normalize() #region public void Normalize()
/// <summary> /// <summary>

View file

@ -275,6 +275,17 @@ namespace OpenTK
#endregion #endregion
/// <summary>
/// Returns a copy of the Vector3d scaled to unit length.
/// </summary>
/// <returns></returns>
public Vector3d Normalized()
{
Vector3d v = this;
v.Normalize();
return v;
}
#region public void Normalize() #region public void Normalize()
/// <summary> /// <summary>

View file

@ -343,6 +343,16 @@ namespace OpenTK
#endregion #endregion
/// <summary>
/// Returns a copy of the Vector4 scaled to unit length.
/// </summary>
public Vector4 Normalized()
{
Vector4 v = this;
v.Normalize();
return v;
}
#region public void Normalize() #region public void Normalize()
/// <summary> /// <summary>

View file

@ -340,6 +340,16 @@ namespace OpenTK
#endregion #endregion
/// <summary>
/// Returns a copy of the Vector4d scaled to unid length.
/// </summary>
public Vector4d Normalized()
{
Vector4d v = this;
v.Normalize();
return v;
}
#region public void Normalize() #region public void Normalize()
/// <summary> /// <summary>