Skip to content

Commit

Permalink
use Sincos instead of Sin, Cos
Browse files Browse the repository at this point in the history
  • Loading branch information
egonelbre authored and pwaller committed Oct 26, 2023
1 parent 96f9c18 commit d41b382
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 22 deletions.
6 changes: 4 additions & 2 deletions mgl32/quat.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ func QuatIdent() Quat {
func QuatRotate(angle float32, axis Vec3) Quat {
// angle = (float32(math.Pi) * angle) / 180.0

c, s := float32(math.Cos(float64(angle/2))), float32(math.Sin(float64(angle/2)))
sn, cs := math.Sincos(float64(angle / 2))
s, c := float32(sn), float32(cs)

return Quat{c, axis.Mul(s)}
}
Expand Down Expand Up @@ -233,7 +234,8 @@ func QuatSlerp(q1, q2 Quat, amount float32) Quat {
dot = Clamp(dot, -1, 1)

theta := float32(math.Acos(float64(dot))) * amount
c, s := float32(math.Cos(float64(theta))), float32(math.Sin(float64(theta)))
sn, cs := math.Sincos(float64(theta))
s, c := float32(sn), float32(cs)
rel := q2.Sub(q1.Scale(dot)).Normalize()

return q1.Scale(c).Add(rel.Scale(s))
Expand Down
27 changes: 18 additions & 9 deletions mgl32/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import "math"
// see HomogRotate2D
func Rotate2D(angle float32) Mat2 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float32(math.Sin(float64(angle))), float32(math.Cos(float64(angle)))
sn, cs := math.Sincos(float64(angle))
sin, cos := float32(sn), float32(cs)
return Mat2{cos, sin, -sin, cos}
}

Expand All @@ -24,7 +25,8 @@ func Rotate2D(angle float32) Mat2 {
// [0 s c ]
func Rotate3DX(angle float32) Mat3 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float32(math.Sin(float64(angle))), float32(math.Cos(float64(angle)))
sn, cs := math.Sincos(float64(angle))
sin, cos := float32(sn), float32(cs)
return Mat3{1, 0, 0, 0, cos, sin, 0, -sin, cos}
}

Expand All @@ -37,7 +39,8 @@ func Rotate3DX(angle float32) Mat3 {
// [s 0 c ]
func Rotate3DY(angle float32) Mat3 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float32(math.Sin(float64(angle))), float32(math.Cos(float64(angle)))
sn, cs := math.Sincos(float64(angle))
sin, cos := float32(sn), float32(cs)
return Mat3{cos, 0, -sin, 0, 1, 0, sin, 0, cos}
}

Expand All @@ -50,7 +53,8 @@ func Rotate3DY(angle float32) Mat3 {
// [0 0 1 ]
func Rotate3DZ(angle float32) Mat3 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float32(math.Sin(float64(angle))), float32(math.Cos(float64(angle)))
sn, cs := math.Sincos(float64(angle))
sin, cos := float32(sn), float32(cs)
return Mat3{cos, sin, 0, -sin, cos, 0, 0, 0, 1}
}

Expand All @@ -77,29 +81,33 @@ func Translate3D(Tx, Ty, Tz float32) Mat4 {
// HomogRotate2D is the same as Rotate2D, except homogeneous (3x3 with the extra row/col being all zeroes with a one in the bottom right)
func HomogRotate2D(angle float32) Mat3 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float32(math.Sin(float64(angle))), float32(math.Cos(float64(angle)))
sn, cs := math.Sincos(float64(angle))
sin, cos := float32(sn), float32(cs)
return Mat3{cos, sin, 0, -sin, cos, 0, 0, 0, 1}
}

// HomogRotate3DX is the same as Rotate3DX, except homogeneous (4x4 with the extra row/col being all zeroes with a one in the bottom right)
func HomogRotate3DX(angle float32) Mat4 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float32(math.Sin(float64(angle))), float32(math.Cos(float64(angle)))
sn, cs := math.Sincos(float64(angle))
sin, cos := float32(sn), float32(cs)

return Mat4{1, 0, 0, 0, 0, cos, sin, 0, 0, -sin, cos, 0, 0, 0, 0, 1}
}

// HomogRotate3DY is the same as Rotate3DY, except homogeneous (4x4 with the extra row/col being all zeroes with a one in the bottom right)
func HomogRotate3DY(angle float32) Mat4 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float32(math.Sin(float64(angle))), float32(math.Cos(float64(angle)))
sn, cs := math.Sincos(float64(angle))
sin, cos := float32(sn), float32(cs)
return Mat4{cos, 0, -sin, 0, 0, 1, 0, 0, sin, 0, cos, 0, 0, 0, 0, 1}
}

// HomogRotate3DZ is the same as Rotate3DZ, except homogeneous (4x4 with the extra row/col being all zeroes with a one in the bottom right)
func HomogRotate3DZ(angle float32) Mat4 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float32(math.Sin(float64(angle))), float32(math.Cos(float64(angle)))
sn, cs := math.Sincos(float64(angle))
sin, cos := float32(sn), float32(cs)
return Mat4{cos, sin, 0, 0, -sin, cos, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}
}

Expand Down Expand Up @@ -158,7 +166,8 @@ func ShearZ3D(shearX, shearY float32) Mat4 {
// [[ 0 , 0 , 0 , 1 ]]
func HomogRotate3D(angle float32, axis Vec3) Mat4 {
x, y, z := axis[0], axis[1], axis[2]
s, c := float32(math.Sin(float64(angle))), float32(math.Cos(float64(angle)))
sn, cs := math.Sincos(float64(angle))
s, c := float32(sn), float32(cs)
k := 1 - c

return Mat4{x*x*k + c, x*y*k + z*s, x*z*k - y*s, 0, x*y*k - z*s, y*y*k + c, y*z*k + x*s, 0, x*z*k + y*s, y*z*k - x*s, z*z*k + c, 0, 0, 0, 0, 1}
Expand Down
6 changes: 4 additions & 2 deletions mgl64/quat.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ func QuatIdent() Quat {
func QuatRotate(angle float64, axis Vec3) Quat {
// angle = (float32(math.Pi) * angle) / 180.0

c, s := float64(math.Cos(float64(angle/2))), float64(math.Sin(float64(angle/2)))
sn, cs := math.Sincos(float64(angle / 2))
s, c := float64(sn), float64(cs)

return Quat{c, axis.Mul(s)}
}
Expand Down Expand Up @@ -235,7 +236,8 @@ func QuatSlerp(q1, q2 Quat, amount float64) Quat {
dot = Clamp(dot, -1, 1)

theta := float64(math.Acos(float64(dot))) * amount
c, s := float64(math.Cos(float64(theta))), float64(math.Sin(float64(theta)))
sn, cs := math.Sincos(float64(theta))
s, c := float64(sn), float64(cs)
rel := q2.Sub(q1.Scale(dot)).Normalize()

return q1.Scale(c).Add(rel.Scale(s))
Expand Down
27 changes: 18 additions & 9 deletions mgl64/transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import "math"
// see HomogRotate2D
func Rotate2D(angle float64) Mat2 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float64(math.Sin(float64(angle))), float64(math.Cos(float64(angle)))
sn, cs := math.Sincos(float64(angle))
sin, cos := float64(sn), float64(cs)
return Mat2{cos, sin, -sin, cos}
}

Expand All @@ -26,7 +27,8 @@ func Rotate2D(angle float64) Mat2 {
// [0 s c ]
func Rotate3DX(angle float64) Mat3 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float64(math.Sin(float64(angle))), float64(math.Cos(float64(angle)))
sn, cs := math.Sincos(float64(angle))
sin, cos := float64(sn), float64(cs)
return Mat3{1, 0, 0, 0, cos, sin, 0, -sin, cos}
}

Expand All @@ -39,7 +41,8 @@ func Rotate3DX(angle float64) Mat3 {
// [s 0 c ]
func Rotate3DY(angle float64) Mat3 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float64(math.Sin(float64(angle))), float64(math.Cos(float64(angle)))
sn, cs := math.Sincos(float64(angle))
sin, cos := float64(sn), float64(cs)
return Mat3{cos, 0, -sin, 0, 1, 0, sin, 0, cos}
}

Expand All @@ -52,7 +55,8 @@ func Rotate3DY(angle float64) Mat3 {
// [0 0 1 ]
func Rotate3DZ(angle float64) Mat3 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float64(math.Sin(float64(angle))), float64(math.Cos(float64(angle)))
sn, cs := math.Sincos(float64(angle))
sin, cos := float64(sn), float64(cs)
return Mat3{cos, sin, 0, -sin, cos, 0, 0, 0, 1}
}

Expand All @@ -79,29 +83,33 @@ func Translate3D(Tx, Ty, Tz float64) Mat4 {
// HomogRotate2D is the same as Rotate2D, except homogeneous (3x3 with the extra row/col being all zeroes with a one in the bottom right)
func HomogRotate2D(angle float64) Mat3 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float64(math.Sin(float64(angle))), float64(math.Cos(float64(angle)))
sn, cs := math.Sincos(float64(angle))
sin, cos := float64(sn), float64(cs)
return Mat3{cos, sin, 0, -sin, cos, 0, 0, 0, 1}
}

// HomogRotate3DX is the same as Rotate3DX, except homogeneous (4x4 with the extra row/col being all zeroes with a one in the bottom right)
func HomogRotate3DX(angle float64) Mat4 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float64(math.Sin(float64(angle))), float64(math.Cos(float64(angle)))
sn, cs := math.Sincos(float64(angle))
sin, cos := float64(sn), float64(cs)

return Mat4{1, 0, 0, 0, 0, cos, sin, 0, 0, -sin, cos, 0, 0, 0, 0, 1}
}

// HomogRotate3DY is the same as Rotate3DY, except homogeneous (4x4 with the extra row/col being all zeroes with a one in the bottom right)
func HomogRotate3DY(angle float64) Mat4 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float64(math.Sin(float64(angle))), float64(math.Cos(float64(angle)))
sn, cs := math.Sincos(float64(angle))
sin, cos := float64(sn), float64(cs)
return Mat4{cos, 0, -sin, 0, 0, 1, 0, 0, sin, 0, cos, 0, 0, 0, 0, 1}
}

// HomogRotate3DZ is the same as Rotate3DZ, except homogeneous (4x4 with the extra row/col being all zeroes with a one in the bottom right)
func HomogRotate3DZ(angle float64) Mat4 {
//angle = (angle * math.Pi) / 180.0
sin, cos := float64(math.Sin(float64(angle))), float64(math.Cos(float64(angle)))
sn, cs := math.Sincos(float64(angle))
sin, cos := float64(sn), float64(cs)
return Mat4{cos, sin, 0, 0, -sin, cos, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}
}

Expand Down Expand Up @@ -160,7 +168,8 @@ func ShearZ3D(shearX, shearY float64) Mat4 {
// [[ 0 , 0 , 0 , 1 ]]
func HomogRotate3D(angle float64, axis Vec3) Mat4 {
x, y, z := axis[0], axis[1], axis[2]
s, c := float64(math.Sin(float64(angle))), float64(math.Cos(float64(angle)))
sn, cs := math.Sincos(float64(angle))
s, c := float64(sn), float64(cs)
k := 1 - c

return Mat4{x*x*k + c, x*y*k + z*s, x*z*k - y*s, 0, x*y*k - z*s, y*y*k + c, y*z*k + x*s, 0, x*z*k + y*s, y*z*k - x*s, z*z*k + c, 0, 0, 0, 0, 1}
Expand Down

0 comments on commit d41b382

Please sign in to comment.