-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from DomCR/Transform-class
transform class
- Loading branch information
Showing
7 changed files
with
516 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.