Files
sjy01-image-proc/pkg/calculator/quaternion.go
2024-06-07 10:51:20 +08:00

21 lines
454 B
Go

package calculator
import (
"gonum.org/v1/gonum/mat"
)
type Quaternion struct {
W, X, Y, Z float64
}
// ToRotationMatrix converts a quaternion to a rotation matrix.
func (q Quaternion) ToRotationMatrix() *mat.Dense {
w, x, y, z := q.W, q.X, q.Y, q.Z
return mat.NewDense(3, 3, []float64{
1 - 2*y*y - 2*z*z, 2*x*y - 2*w*z, 2*x*z + 2*w*y,
2*x*y + 2*w*z, 1 - 2*x*x - 2*z*z, 2*y*z - 2*w*x,
2*x*z - 2*w*y, 2*y*z + 2*w*x, 1 - 2*x*x - 2*y*y,
})
}