Skip to content

Instantly share code, notes, and snippets.

@EliCDavis
Created May 12, 2021 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EliCDavis/364d8fff06811c30ca5a00ddb5ef19ab to your computer and use it in GitHub Desktop.
Save EliCDavis/364d8fff06811c30ca5a00ddb5ef19ab to your computer and use it in GitHub Desktop.
Just my working notes with rotation for later reference
package euler
import (
"math"
"github.com/EliCDavis/vector"
)
// https://stackoverflow.com/questions/1568568/how-to-convert-euler-angles-to-directional-vector
type matrix [][]float64
func Multiply3x3(m1, m2 matrix) matrix {
m3 := [][]float64{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}
for i := 0; i < 3; i++ {
for j := 0; j < 3; j++ {
m3[i][j] = 0
for k := 0; k < 3; k++ {
m3[i][j] = m3[i][j] + (m1[i][k] * m2[k][j])
}
}
}
return m3
}
func Multiply3x3by3x1(m matrix, v vector.Vector3) vector.Vector3 {
return vector.NewVector3(
(m[0][0]*v.X())+(m[0][1]*v.Y())+(m[0][2]*v.Z()),
(m[1][0]*v.X())+(m[1][1]*v.Y())+(m[1][2]*v.Z()),
(m[2][0]*v.X())+(m[2][1]*v.Y())+(m[2][2]*v.Z()),
)
}
func Transpose(in matrix) matrix {
return [][]float64{
{in[0][0], in[1][0], in[2][0]},
{in[0][1], in[1][1], in[2][1]},
{in[0][2], in[1][2], in[2][2]},
}
}
func Rx(theta float64) matrix {
return [][]float64{
{1, 0, 0},
{0, math.Cos(theta), -math.Sin(theta)},
{0, math.Sin(theta), math.Cos(theta)},
}
}
func RxT(theta float64) matrix {
return Transpose(Rx(theta))
}
func Ry(theta float64) matrix {
return [][]float64{
{math.Cos(theta), 0, math.Sin(theta)},
{0, 1, 0},
{-math.Sin(theta), 0, math.Cos(theta)},
}
}
func RyT(theta float64) matrix {
return Transpose(Ry(theta))
}
func Rz(theta float64) matrix {
return [][]float64{
{math.Cos(theta), -math.Sin(theta), 0},
{math.Sin(theta), math.Cos(theta), 0},
{0, 0, 1},
}
}
func RzT(theta float64) matrix {
return Transpose(Rz(theta))
}
func ToNormal(inEulerAngle vector.Vector3) vector.Vector3 {
vectorToTransform := vector.Vector3Forward()
Sx := math.Sin(inEulerAngle.X() * (math.Pi / 180.))
Sy := math.Sin(inEulerAngle.Y() * (math.Pi / 180.))
Sz := math.Sin(inEulerAngle.Z() * (math.Pi / 180.))
Cx := math.Cos(inEulerAngle.X() * (math.Pi / 180.))
Cy := math.Cos(inEulerAngle.Y() * (math.Pi / 180.))
Cz := math.Cos(inEulerAngle.Z() * (math.Pi / 180.))
var Mx matrix = make([][]float64, 3)
Mx[0] = []float64{0, 0, 0}
Mx[1] = []float64{0, 0, 0}
Mx[2] = []float64{0, 0, 0}
Mx[0][0] = Cy*Cz - Sx*Sy*Sz
Mx[0][1] = -Cx * Sz
Mx[0][2] = Cz*Sy + Cy*Sx*Sz
Mx[1][0] = Cz*Sx*Sy + Cy*Sz
Mx[1][1] = Cx * Cz
Mx[1][2] = -Cy*Cz*Sx + Sy*Sz
Mx[2][0] = -Cx * Sy
Mx[2][1] = Sx
Mx[2][2] = Cx * Cy
return vector.NewVector3(
(Mx[0][0]*vectorToTransform.X())+(Mx[0][1]*vectorToTransform.Y())+(Mx[0][2]*vectorToTransform.Z()),
(Mx[1][0]*vectorToTransform.X())+(Mx[1][1]*vectorToTransform.Y())+(Mx[1][2]*vectorToTransform.Z()),
(Mx[2][0]*vectorToTransform.X())+(Mx[2][1]*vectorToTransform.Y())+(Mx[2][2]*vectorToTransform.Z()),
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment