spatial/r3: add method to obtain a rotation matrix

This commit is contained in:
Dan Kortschak
2021-06-21 13:51:08 +09:30
parent f2a128f642
commit 2600994ee4
2 changed files with 46 additions and 2 deletions

View File

@@ -9,6 +9,7 @@ import (
"testing"
"gonum.org/v1/gonum/floats/scalar"
"gonum.org/v1/gonum/mat"
)
func TestAdd(t *testing.T) {
@@ -236,13 +237,34 @@ func TestRotate(t *testing.T) {
got := Rotate(test.v, test.alpha, test.axis)
if !vecApproxEqual(got, test.want, tol) {
t.Errorf(
"rotate(%v, %v, %v)= %v, want=%v",
"quat rotate(%v, %v, %v)= %v, want=%v",
test.v, test.alpha, test.axis, got, test.want,
)
}
var gotv mat.VecDense
gotv.MulVec(NewRotation(test.alpha, test.axis).Matrix(), vecDense(test.v))
got = vec(gotv)
if !vecApproxEqual(got, test.want, tol) {
t.Errorf(
"matrix rotate(%v, %v, %v)= %v, want=%v",
test.v, test.alpha, test.axis, got, test.want,
)
}
}
}
func vecDense(v Vec) *mat.VecDense {
return mat.NewVecDense(3, []float64{v.X, v.Y, v.Z})
}
func vec(v mat.VecDense) Vec {
if v.Len() != 3 {
panic(mat.ErrShape)
}
return Vec{v.AtVec(0), v.AtVec(1), v.AtVec(2)}
}
func vecIsNaN(v Vec) bool {
return math.IsNaN(v.X) && math.IsNaN(v.Y) && math.IsNaN(v.Z)
}