Commit 2c26e7f1 by Ignacio Etcheverry

C#: Basis fixes and cleanup

Fix Basis operator[int]. Now it returns columns instead of rows. Fix Transform2D.AfficeInverse() mutating rather than returning a new Transform2D.
parent aa5b9982
...@@ -123,22 +123,23 @@ namespace Godot ...@@ -123,22 +123,23 @@ namespace Godot
// Calculate cosine // Calculate cosine
real_t cosom = x * b.x + y * b.y + z * b.z + w * b.w; real_t cosom = x * b.x + y * b.y + z * b.z + w * b.w;
var to1 = new real_t[4]; var to1 = new Quat();
// Adjust signs if necessary // Adjust signs if necessary
if (cosom < 0.0) if (cosom < 0.0)
{ {
cosom = -cosom; to1[0] = -b.x; cosom = -cosom;
to1[1] = -b.y; to1.x = -b.x;
to1[2] = -b.z; to1.y = -b.y;
to1[3] = -b.w; to1.z = -b.z;
to1.w = -b.w;
} }
else else
{ {
to1[0] = b.x; to1.x = b.x;
to1[1] = b.y; to1.y = b.y;
to1[2] = b.z; to1.z = b.z;
to1[3] = b.w; to1.w = b.w;
} }
real_t sinom, scale0, scale1; real_t sinom, scale0, scale1;
...@@ -162,10 +163,10 @@ namespace Godot ...@@ -162,10 +163,10 @@ namespace Godot
// Calculate final values // Calculate final values
return new Quat return new Quat
( (
scale0 * x + scale1 * to1[0], scale0 * x + scale1 * to1.x,
scale0 * y + scale1 * to1[1], scale0 * y + scale1 * to1.y,
scale0 * z + scale1 * to1[2], scale0 * z + scale1 * to1.z,
scale0 * w + scale1 * to1[3] scale0 * w + scale1 * to1.w
); );
} }
......
...@@ -71,21 +71,21 @@ namespace Godot ...@@ -71,21 +71,21 @@ namespace Godot
{ {
// Make rotation matrix // Make rotation matrix
// Z vector // Z vector
Vector3 zAxis = eye - target; Vector3 column2 = eye - target;
zAxis.Normalize(); column2.Normalize();
Vector3 yAxis = up; Vector3 column1 = up;
Vector3 xAxis = yAxis.Cross(zAxis); Vector3 column0 = column1.Cross(column2);
// Recompute Y = Z cross X // Recompute Y = Z cross X
yAxis = zAxis.Cross(xAxis); column1 = column2.Cross(column0);
xAxis.Normalize(); column0.Normalize();
yAxis.Normalize(); column1.Normalize();
basis = Basis.CreateFromAxes(xAxis, yAxis, zAxis); basis = new Basis(column0, column1, column2);
origin = eye; origin = eye;
} }
...@@ -94,9 +94,9 @@ namespace Godot ...@@ -94,9 +94,9 @@ namespace Godot
{ {
return new Transform(basis, new Vector3 return new Transform(basis, new Vector3
( (
origin[0] += basis[0].Dot(ofs), origin[0] += basis.Row0.Dot(ofs),
origin[1] += basis[1].Dot(ofs), origin[1] += basis.Row1.Dot(ofs),
origin[2] += basis[2].Dot(ofs) origin[2] += basis.Row2.Dot(ofs)
)); ));
} }
...@@ -104,9 +104,9 @@ namespace Godot ...@@ -104,9 +104,9 @@ namespace Godot
{ {
return new Vector3 return new Vector3
( (
basis[0].Dot(v) + origin.x, basis.Row0.Dot(v) + origin.x,
basis[1].Dot(v) + origin.y, basis.Row1.Dot(v) + origin.y,
basis[2].Dot(v) + origin.z basis.Row2.Dot(v) + origin.z
); );
} }
...@@ -116,9 +116,9 @@ namespace Godot ...@@ -116,9 +116,9 @@ namespace Godot
return new Vector3 return new Vector3
( (
basis[0, 0] * vInv.x + basis[1, 0] * vInv.y + basis[2, 0] * vInv.z, basis.Row0[0] * vInv.x + basis.Row1[0] * vInv.y + basis.Row2[0] * vInv.z,
basis[0, 1] * vInv.x + basis[1, 1] * vInv.y + basis[2, 1] * vInv.z, basis.Row0[1] * vInv.x + basis.Row1[1] * vInv.y + basis.Row2[1] * vInv.z,
basis[0, 2] * vInv.x + basis[1, 2] * vInv.y + basis[2, 2] * vInv.z basis.Row0[2] * vInv.x + basis.Row1[2] * vInv.y + basis.Row2[2] * vInv.z
); );
} }
...@@ -134,9 +134,9 @@ namespace Godot ...@@ -134,9 +134,9 @@ namespace Godot
public static Transform FlipZ { get { return _flipZ; } } public static Transform FlipZ { get { return _flipZ; } }
// Constructors // Constructors
public Transform(Vector3 xAxis, Vector3 yAxis, Vector3 zAxis, Vector3 origin) public Transform(Vector3 column0, Vector3 column1, Vector3 column2, Vector3 origin)
{ {
basis = Basis.CreateFromAxes(xAxis, yAxis, zAxis); basis = new Basis(column0, column1, column2);
this.origin = origin; this.origin = origin;
} }
......
...@@ -53,11 +53,11 @@ namespace Godot ...@@ -53,11 +53,11 @@ namespace Godot
} }
} }
public Vector2 this[int index] public Vector2 this[int rowIndex]
{ {
get get
{ {
switch (index) switch (rowIndex)
{ {
case 0: case 0:
return x; return x;
...@@ -71,7 +71,7 @@ namespace Godot ...@@ -71,7 +71,7 @@ namespace Godot
} }
set set
{ {
switch (index) switch (rowIndex)
{ {
case 0: case 0:
x = value; x = value;
...@@ -88,29 +88,29 @@ namespace Godot ...@@ -88,29 +88,29 @@ namespace Godot
} }
} }
public real_t this[int index, int axis] public real_t this[int rowIndex, int columnIndex]
{ {
get get
{ {
switch (index) switch (rowIndex)
{ {
case 0: case 0:
return x[axis]; return x[columnIndex];
case 1: case 1:
return y[axis]; return y[columnIndex];
default: default:
throw new IndexOutOfRangeException(); throw new IndexOutOfRangeException();
} }
} }
set set
{ {
switch (index) switch (rowIndex)
{ {
case 0: case 0:
x[axis] = value; x[columnIndex] = value;
return; return;
case 1: case 1:
y[axis] = value; y[columnIndex] = value;
return; return;
default: default:
throw new IndexOutOfRangeException(); throw new IndexOutOfRangeException();
...@@ -120,30 +120,23 @@ namespace Godot ...@@ -120,30 +120,23 @@ namespace Godot
public Transform2D AffineInverse() public Transform2D AffineInverse()
{ {
var inv = this;
real_t det = BasisDeterminant(); real_t det = BasisDeterminant();
if (det == 0) if (det == 0)
{ throw new InvalidOperationException("Matrix determinant is zero and cannot be inverted.");
return new Transform2D
(
real_t.NaN, real_t.NaN,
real_t.NaN, real_t.NaN,
real_t.NaN, real_t.NaN
);
}
real_t detInv = 1.0f / det; var inv = this;
real_t temp = this[0, 0]; real_t temp = inv[0, 0];
this[0, 0] = this[1, 1]; inv[0, 0] = inv[1, 1];
this[1, 1] = temp; inv[1, 1] = temp;
this[0] *= new Vector2(detInv, -detInv); real_t detInv = 1.0f / det;
this[1] *= new Vector2(-detInv, detInv);
inv[0] *= new Vector2(detInv, -detInv);
inv[1] *= new Vector2(-detInv, detInv);
this[2] = BasisXform(-this[2]); inv[2] = BasisXform(-inv[2]);
return inv; return inv;
} }
...@@ -293,9 +286,9 @@ namespace Godot ...@@ -293,9 +286,9 @@ namespace Godot
private static readonly Transform2D _flipX = new Transform2D(-1, 0, 0, 1, 0, 0); private static readonly Transform2D _flipX = new Transform2D(-1, 0, 0, 1, 0, 0);
private static readonly Transform2D _flipY = new Transform2D(1, 0, 0, -1, 0, 0); private static readonly Transform2D _flipY = new Transform2D(1, 0, 0, -1, 0, 0);
public static Transform2D Identity { get { return _identity; } } public static Transform2D Identity => _identity;
public static Transform2D FlipX { get { return _flipX; } } public static Transform2D FlipX => _flipX;
public static Transform2D FlipY { get { return _flipY; } } public static Transform2D FlipY => _flipY;
// Constructors // Constructors
public Transform2D(Vector2 xAxis, Vector2 yAxis, Vector2 originPos) public Transform2D(Vector2 xAxis, Vector2 yAxis, Vector2 originPos)
...@@ -324,12 +317,10 @@ namespace Godot ...@@ -324,12 +317,10 @@ namespace Godot
{ {
left.origin = left.Xform(right.origin); left.origin = left.Xform(right.origin);
real_t x0, x1, y0, y1; real_t x0 = left.Tdotx(right.x);
real_t x1 = left.Tdoty(right.x);
x0 = left.Tdotx(right.x); real_t y0 = left.Tdotx(right.y);
x1 = left.Tdoty(right.x); real_t y1 = left.Tdoty(right.y);
y0 = left.Tdotx(right.y);
y1 = left.Tdoty(right.y);
left.x.x = x0; left.x.x = x0;
left.x.y = x1; left.x.y = x1;
...@@ -351,12 +342,7 @@ namespace Godot ...@@ -351,12 +342,7 @@ namespace Godot
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
if (obj is Transform2D) return obj is Transform2D transform2D && Equals(transform2D);
{
return Equals((Transform2D)obj);
}
return false;
} }
public bool Equals(Transform2D other) public bool Equals(Transform2D other)
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType> <DebugType>portable</DebugType>
<Optimize>false</Optimize> <Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath> <OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants> <DefineConstants>DEBUG;</DefineConstants>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment