-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotationmatrix_test.go
More file actions
188 lines (157 loc) · 4.19 KB
/
rotationmatrix_test.go
File metadata and controls
188 lines (157 loc) · 4.19 KB
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
package nvector_test
import (
"context"
"math"
"testing"
. "github.com/ezzatron/nvector-go"
"github.com/ezzatron/nvector-go/internal/equality"
"github.com/ezzatron/nvector-go/internal/rapidgen"
"github.com/ezzatron/nvector-go/internal/testapi"
"pgregory.net/rapid"
)
func Test_FromRotationMatrix(t *testing.T) {
client, err := testapi.NewClient()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
client.Close()
})
ctx := context.Background()
t.Run("it matches the reference implementation", func(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
r := rapidgen.RotationMatrix().Draw(t, "r")
want, err := client.FromRotationMatrix(ctx, r)
if err != nil {
t.Fatal(err)
}
got := FromRotationMatrix(r)
if eq, ineq := equality.EqualToVector(got, want, 1e-15); !eq {
equality.ReportInequalities(t, ineq)
}
})
})
}
func Test_ToRotationMatrix(t *testing.T) {
client, err := testapi.NewClient()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
client.Close()
})
ctx := context.Background()
t.Run("it matches the reference implementation", func(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
type inputs struct {
V Vector
F Matrix
}
i := rapid.Custom(func(t *rapid.T) inputs {
return inputs{
V: rapidgen.UnitVector().Draw(t, "nVector"),
F: rapidgen.RotationMatrix().Draw(t, "coordFrame"),
}
}).Filter(func(i inputs) bool {
// Avoid situations where very close to poles
// Python implementation rounds to zero in these cases, which causes
// the Y axis to be [0, 1, 0] instead of the calculated value,
// producing very different results.
v := i.V.Transform(i.F)
yDirNorm := math.Hypot(-v.Z, v.Y)
if yDirNorm > 0 && yDirNorm <= 1e-100 {
return false
}
return true
}).Draw(t, "inputs")
v := i.V
f := i.F
want, err := client.ToRotationMatrix(ctx, v, f)
if err != nil {
t.Fatal(err)
}
got := ToRotationMatrix(v, f)
if eq, ineq := equality.EqualToMatrix(got, want, 1e-14); !eq {
equality.ReportInequalities(t, ineq)
}
})
})
t.Run("it handles the poles", func(t *testing.T) {
v := Vector{X: 0, Y: 0, Z: 1}
f := ZAxisNorth
want := Matrix{
-1, 0, 0,
0, 1, -0,
0, 0, -1,
}
got := ToRotationMatrix(v, f)
if eq, ineq := equality.EqualToMatrix(got, want, 1e-14); !eq {
equality.ReportInequalities(t, ineq)
}
})
}
func Test_RoundTrip(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
v := rapidgen.UnitVector().Draw(t, "nVector")
f := rapidgen.RotationMatrix().Draw(t, "coordFrame")
got := FromRotationMatrix(ToRotationMatrix(v, f))
if eq, ineq := equality.EqualToVector(got, v, 1e-14); !eq {
equality.ReportInequalities(t, ineq)
}
})
}
func Test_ToRotationMatrixUsingWanderAzimuth(t *testing.T) {
client, err := testapi.NewClient()
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() {
client.Close()
})
ctx := context.Background()
t.Run("it matches the reference implementation", func(t *testing.T) {
rapid.Check(t, func(t *rapid.T) {
type inputs struct {
V Vector
W float64
F Matrix
}
i := rapid.Custom(func(t *rapid.T) inputs {
return inputs{
V: rapidgen.UnitVector().Draw(t, "nVector"),
W: rapidgen.Radians().Draw(t, "wanderAzimuth"),
F: rapidgen.RotationMatrix().Draw(t, "coordFrame"),
}
}).Filter(func(i inputs) bool {
// Avoid situations where components of the xyz2R matrix are close
// to zero. The Python implementation rounds to zero in these cases,
// which produces very different results.
l := ToGeodeticCoordinates(i.V, i.F)
r := EulerXYZToRotationMatrix(
EulerXYZ{l.Longitude, -l.Latitude, i.W},
)
for _, n := range []float64{
r.XX, r.XY, r.XZ,
r.YX, r.YY, r.YZ,
r.ZX, r.ZY, r.ZZ,
} {
if n != 0 && n < 1e-15 && n > -1e-15 {
return false
}
}
return true
}).Draw(t, "inputs")
v := i.V
w := i.W
f := i.F
want, err := client.ToRotationMatrixUsingWanderAzimuth(ctx, v, w, f)
if err != nil {
t.Fatal(err)
}
got := ToRotationMatrixUsingWanderAzimuth(v, w, f)
if eq, ineq := equality.EqualToMatrix(got, want, 1e-14); !eq {
equality.ReportInequalities(t, ineq)
}
})
})
}