Skip to content

Commit

Permalink
Merge pull request #8 from DomCR/Transform-class
Browse files Browse the repository at this point in the history
transform class
  • Loading branch information
DomCR authored Sep 24, 2024
2 parents 5119a7d + 825d110 commit d03bb02
Show file tree
Hide file tree
Showing 7 changed files with 516 additions and 0 deletions.
29 changes: 29 additions & 0 deletions CSMath.Tests/AssertUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Xunit;

namespace CSMath.Tests
{
public static class AssertUtils
{
public static void AreEqual<T>(T expected, T actual, string varname = "undefined")
{
switch (expected, actual)
{
case (double d1, double d2):
Assert.Equal(d1, d2, 10);
break;
case (XY xy1, XY xy2):
Assert.True(xy1.IsEqual(xy2, TestVariables.DecimalPrecision), $"Different {varname}");
break;
case (XYZ xyz1, XYZ xyz2):
Assert.True(xyz1.IsEqual(xyz2, TestVariables.DecimalPrecision), $"Different {varname}: expected {xyz1} actual {xyz2}");
break;
case (Quaternion q1, Quaternion q2):
Assert.True(q1.Equals(q2, TestVariables.DecimalPrecision), $"Different {varname}");
break;
default:
Assert.Equal(expected, actual);
break;
}
}
}
}
82 changes: 82 additions & 0 deletions CSMath.Tests/CSMathRandom.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using System;
using System.Linq;

namespace CSMath.Tests
{
public class CSMathRandom : Random
{
public CSMathRandom() : base() { }

public CSMathRandom(int seed) : base(seed) { }

public short NextShort()
{
return (short)Next(short.MinValue, short.MaxValue);
}

public object Next(Type t)
{
object value = Activator.CreateInstance(t);

return setValue(value);
}

public T Next<T>()
where T : struct
{
T value = default(T);

return setValue(value);
}

public XY NextXY()
{
return new XY(this.NextDouble(), this.NextDouble());
}

public XYZ NextXYZ()
{
return new XYZ(this.NextDouble(), this.NextDouble(), this.NextDouble());
}

public string RandomString(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[this.Next(s.Length)]).ToArray());
}

private T setValue<T>(T value)
{
switch (value)
{
case bool:
return (T)Convert.ChangeType(this.Next(0, 1) == 1, typeof(bool));
case byte:
return (T)Convert.ChangeType(this.Next(byte.MinValue, byte.MaxValue), typeof(byte));
case char:
return (T)Convert.ChangeType(this.Next(byte.MinValue, byte.MaxValue), typeof(char));
case short:
return (T)Convert.ChangeType(this.Next(short.MinValue, short.MaxValue), typeof(short));
case ushort:
return (T)Convert.ChangeType(this.Next(ushort.MinValue, ushort.MaxValue), typeof(ushort));
case int:
return (T)Convert.ChangeType(this.Next(int.MinValue, int.MaxValue), typeof(int));
case double:
return (T)Convert.ChangeType(this.NextDouble(), typeof(double));
case long:
return (T)Convert.ChangeType(this.Next(int.MinValue, int.MaxValue), typeof(long));
case ulong:
return (T)Convert.ChangeType(this.Next(0, int.MaxValue), typeof(ulong));
case string:
return (T)Convert.ChangeType(RandomString(10), typeof(string));
case XY:
return (T)Convert.ChangeType(NextXY(), typeof(XY));
case XYZ:
return (T)Convert.ChangeType(NextXYZ(), typeof(XYZ));
default:
throw new NotImplementedException();
}
}
}
}
9 changes: 9 additions & 0 deletions CSMath.Tests/TestVariables.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace CSMath.Tests
{
public static class TestVariables
{
public const double Delta = 0.00001d;

public const int DecimalPrecision = 5;
}
}
112 changes: 112 additions & 0 deletions CSMath.Tests/TransformTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using Xunit;

namespace CSMath.Tests
{
public class TransformTests
{
private CSMathRandom _random = new CSMathRandom();

[Fact()]
public void TranslationTest()
{
XYZ translation = _random.Next<XYZ>();
XYZ scale = _random.Next<XYZ>();
XYZ rotation = _random.Next<XYZ>();

Transform transform = new Transform(translation, scale, rotation);

Assert.Equal(translation, transform.Translation);
}

[Fact()]
public void ApplyTranslationTest()
{
XYZ xyz = XYZ.Zero;

XYZ translation = XYZ.Zero;
XYZ scale = new XYZ(1, 1, 1);
XYZ rotation = XYZ.Zero;

XYZ expected = xyz + translation;

Transform transform = new Transform(translation, scale, rotation);
XYZ result = transform.ApplyTransform(xyz);

AssertUtils.AreEqual(expected, result);
}

[Fact()]
public void ScaleTest()
{
XYZ translation = _random.Next<XYZ>();
XYZ scale = _random.Next<XYZ>();
XYZ rotation = _random.Next<XYZ>();

Transform transform = new Transform(translation, scale, rotation);

AssertUtils.AreEqual(scale, transform.Scale, "Scale");
}

[Fact()]
public void ApplyScaleTest()
{
XYZ xyz = new XYZ(1, 1, 1);

XYZ translation = XYZ.Zero;
XYZ scale = _random.Next<XYZ>();
XYZ rotation = XYZ.Zero;

XYZ expected = xyz * scale;

Transform transform = new Transform(translation, scale, rotation);
XYZ result = transform.ApplyTransform(xyz);

AssertUtils.AreEqual(expected, result);
}

[Fact]
public void RotationTest()
{
XYZ translation = _random.Next<XYZ>();
XYZ scale = _random.Next<XYZ>();
XYZ rotation = _random.Next<XYZ>();

Transform transform = new Transform(translation, scale, rotation);

Assert.Equal(rotation, transform.EulerRotation);
}

[Fact()]
public void ApplyRotationTest()
{
XYZ xyz = XYZ.AxisX;

XYZ translation = XYZ.Zero;
XYZ scale = new XYZ(1, 1, 1);
XYZ rotation = new XYZ(0, 0, 90);

XYZ expected = new XYZ(0, 1, 0);

Transform transform = new Transform(translation, scale, rotation);
XYZ result = transform.ApplyTransform(xyz);

AssertUtils.AreEqual(expected, result);
}

[Fact()]
public void DecomposeTest()
{
XYZ translation = _random.Next<XYZ>();
XYZ scale = _random.Next<XYZ>();
XYZ rotation = new XYZ(90, 0, 0);

Transform transform = new Transform(translation, scale, rotation);

transform.TryDecompose(out XYZ t, out XYZ s, out Quaternion r);

AssertUtils.AreEqual(transform.Translation, t, "translation");
AssertUtils.AreEqual(transform.Scale, s, "scale");
AssertUtils.AreEqual(transform.Quaternion, r, "rotation");
}
}
}
1 change: 1 addition & 0 deletions CSMath/CSMath.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Matrix4.Operators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)MathUtils.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Quaternion.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Transform.cs" />
<Compile Include="$(MSBuildThisFileDirectory)VectorExtensions.cs" />
<Compile Include="$(MSBuildThisFileDirectory)XY.operators.cs" />
<Compile Include="$(MSBuildThisFileDirectory)XY.cs" />
Expand Down
Loading

0 comments on commit d03bb02

Please sign in to comment.