forked from sam-vdp/bepuphysics1int
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix3x3Tests.cs
196 lines (158 loc) · 5.98 KB
/
Matrix3x3Tests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using FixMath.NET;
using Xunit;
using Xunit.Abstractions;
using System.Collections.Generic;
using BEPUutilities;
using FloatMatrix3x3 = BEPUutilitiesFloat.Matrix3x3;
using BEPUtests.util;
using System.Linq;
using System.Diagnostics;
using System;
namespace BEPUtests
{
public class Matrix3x3Tests
{
Matrix3x3[] testCases = {
Matrix3x3.Identity,
new Matrix3x3(6770.833m, 0, 0, 0, 13500, 0, 0, 0, 6770.833m),
new Matrix3x3(0.6770833m, 0, 0, 0, 1.35m, 0, 0, 0, 0.6770833m),
new Matrix3x3(0, 0, 0, 0, 13500, 0, 0, 0, 6770.833m),
new Matrix3x3(5, 135, -5, 8, 13500, 20, -5, 100, 6770.833m),
new Matrix3x3(0.1m, 3, 838, -200, 13500, 0.001m, 22, 42, 6770.833m),
new Matrix3x3(-3, 3, 2, -1, -8, -5, 63, 5, 0.833m),
new Matrix3x3(5, 3, 2, -3, 11, 1900, 76, 96, 33.833m),
};
private readonly ITestOutputHelper output;
public Matrix3x3Tests(ITestOutputHelper output)
{
if (output == null)
output = new ConsoleTestOutputHelper();
this.output = output;
}
[Fact]
public void Invert()
{
var maxDelta = 0.001m;
var deltas = new List<decimal>();
// Scalability and edge cases
foreach (var m in testCases)
{
Matrix3x3 testCase = m;
FloatMatrix3x3 floatMatrix = MathConverter.Convert(testCase);
FloatMatrix3x3 expected;
FloatMatrix3x3.Invert(ref floatMatrix, out expected);
Matrix3x3 actual;
if (float.IsInfinity(expected.M11) || float.IsNaN(expected.M11))
expected = new FloatMatrix3x3();
Matrix3x3.Invert(ref testCase, out actual);
bool success = true;
foreach (decimal delta in GetDeltas(expected, actual))
{
deltas.Add(delta);
success &= delta <= maxDelta;
}
Assert.True(success, string.Format("Precision: Matrix3x3Invert({0}): Expected {1} Actual {2}", testCase, expected, actual));
}
output.WriteLine("Max error: {0} ({1} times precision)", deltas.Max(), deltas.Max() / Fix64.Precision);
output.WriteLine("Average precision: {0} ({1} times precision)", deltas.Average(), deltas.Average() / Fix64.Precision);
}
[Fact]
public void BenchmarkInvert()
{
var swf = new Stopwatch();
var swd = new Stopwatch();
var deltas = new List<decimal>();
foreach (var m in testCases)
{
Matrix3x3 testCase = m;
for (int i = 0; i < 10000; i++)
{
FloatMatrix3x3 floatMatrix = MathConverter.Convert(testCase);
FloatMatrix3x3 expected;
swf.Start();
FloatMatrix3x3.Invert(ref floatMatrix, out expected);
swf.Stop();
Matrix3x3 actual;
swd.Start();
Matrix3x3.Invert(ref testCase, out actual);
swd.Stop();
if (float.IsInfinity(expected.M11) || float.IsNaN(expected.M11))
expected = new FloatMatrix3x3();
foreach (decimal delta in GetDeltas(expected, actual))
deltas.Add(delta);
}
}
output.WriteLine("Max error: {0} ({1} times precision)", deltas.Max(), deltas.Max() / Fix64.Precision);
output.WriteLine("Average precision: {0} ({1} times precision)", deltas.Average(), deltas.Average() / Fix64.Precision);
output.WriteLine("Fix64.Invert time = {0}ms, float.Invert time = {1}ms", swf.ElapsedMilliseconds, swd.ElapsedMilliseconds);
}
[Fact]
public void AdaptiveInvert()
{
var maxDelta = 0.001m;
var deltas = new List<decimal>();
// Scalability and edge cases
foreach (var m in testCases)
{
Matrix3x3 testCase = m;
FloatMatrix3x3 floatMatrix = MathConverter.Convert(testCase);
FloatMatrix3x3 expected;
FloatMatrix3x3.AdaptiveInvert(ref floatMatrix, out expected);
Matrix3x3 actual;
Matrix3x3.AdaptiveInvert(ref testCase, out actual);
bool success = true;
foreach (decimal delta in GetDeltas(expected, actual))
{
deltas.Add(delta);
success &= delta <= maxDelta;
}
Assert.True(success, string.Format("Precision: Matrix3x3Invert({0}): Expected {1} Actual {2}", testCase, expected, actual));
}
output.WriteLine("Max error: {0} ({1} times precision)", deltas.Max(), deltas.Max() / Fix64.Precision);
output.WriteLine("Average precision: {0} ({1} times precision)", deltas.Average(), deltas.Average() / Fix64.Precision);
}
[Fact]
public void BenchmarkAdaptiveInvert()
{
var swf = new Stopwatch();
var swd = new Stopwatch();
var deltas = new List<decimal>();
foreach (var m in testCases)
{
Matrix3x3 testCase = m;
for (int i = 0; i < 10000; i++)
{
FloatMatrix3x3 floatMatrix = MathConverter.Convert(testCase);
FloatMatrix3x3 expected;
swf.Start();
FloatMatrix3x3.AdaptiveInvert(ref floatMatrix, out expected);
swf.Stop();
Matrix3x3 actual;
swd.Start();
Matrix3x3.AdaptiveInvert(ref testCase, out actual);
swd.Stop();
foreach (decimal delta in GetDeltas(expected, actual))
deltas.Add(delta);
}
}
output.WriteLine("Max error: {0} ({1} times precision)", deltas.Max(), deltas.Max() / Fix64.Precision);
output.WriteLine("Average precision: {0} ({1} times precision)", deltas.Average(), deltas.Average() / Fix64.Precision);
output.WriteLine("Fix64.AdaptiveInvert time = {0}ms, float.AdaptiveInvert time = {1}ms", swf.ElapsedMilliseconds, swd.ElapsedMilliseconds);
}
decimal[] GetDeltas(FloatMatrix3x3 expected, Matrix3x3 actual)
{
decimal[] result = new decimal[9];
int i = 0;
result[i++] = (decimal)actual.M11 - (decimal)expected.M11;
result[i++] = (decimal)actual.M12 - (decimal)expected.M12;
result[i++] = (decimal)actual.M13 - (decimal)expected.M13;
result[i++] = (decimal)actual.M21 - (decimal)expected.M21;
result[i++] = (decimal)actual.M22 - (decimal)expected.M22;
result[i++] = (decimal)actual.M23 - (decimal)expected.M23;
result[i++] = (decimal)actual.M31 - (decimal)expected.M31;
result[i++] = (decimal)actual.M32 - (decimal)expected.M32;
result[i++] = (decimal)actual.M33 - (decimal)expected.M33;
return result;
}
}
}