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:
parent
6c35e8ef24
commit
11114ca4ea
9 changed files with 161 additions and 0 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -3,6 +3,7 @@ Binaries/
|
|||
OpenTK.userprefs
|
||||
Source/GlobalAssemblyInfo.cs
|
||||
Version.txt
|
||||
Source/OpenTK/OpenTK.xml
|
||||
|
||||
# OpenTK Resource files that seem like they should be ignored:
|
||||
Source/Compatibility/Properties/Resources.resources
|
||||
|
|
|
@ -259,6 +259,93 @@ namespace OpenTK
|
|||
/// Gets or sets the value at row 4, column 4 of this instance.
|
||||
/// </summary>
|
||||
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
|
||||
|
||||
|
|
|
@ -189,6 +189,17 @@ namespace OpenTK
|
|||
|
||||
#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()
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -289,6 +289,16 @@ namespace OpenTK
|
|||
|
||||
#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()
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -249,6 +249,17 @@ namespace OpenTK
|
|||
|
||||
#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()
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -277,6 +277,16 @@ namespace OpenTK
|
|||
|
||||
#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()
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -275,6 +275,17 @@ namespace OpenTK
|
|||
|
||||
#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()
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -343,6 +343,16 @@ namespace OpenTK
|
|||
|
||||
#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()
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -340,6 +340,16 @@ namespace OpenTK
|
|||
|
||||
#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()
|
||||
|
||||
/// <summary>
|
||||
|
|
Loading…
Reference in a new issue