Skip to content

Instantly share code, notes, and snippets.

@panmari
Last active August 29, 2015 14:13
Show Gist options
  • Save panmari/eedf62ddf8f5fbbd5f9c to your computer and use it in GitHub Desktop.
Save panmari/eedf62ddf8f5fbbd5f9c to your computer and use it in GitHub Desktop.
func (mat *T) MulVec4(v *vec4.T) vec4.T {
return vec4.T{
mat[0][0]*v[0] + mat[1][0]*v[1] + mat[2][0]*v[2] + mat[3][0]*v[3],
mat[0][1]*v[0] + mat[1][1]*v[1] + mat[2][1]*v[2] + mat[3][1]*v[3],
mat[0][2]*v[0] + mat[1][2]*v[1] + mat[2][2]*v[2] + mat[3][2]*v[3],
mat[0][3]*v[0] + mat[1][3]*v[1] + mat[2][3]*v[2] + mat[3][3]*v[3],
}
}
// Strangely, this is the slowest of the three.
func (mat *T) AltMulVec4(v *vec4.T) vec4.T {
result := *v
mat.TransformVec4(&result)
return result
}
// MulVec3 multiplies v with mat with w as fourth component of the vector.
func (mat *T) TransformVec4(v *vec4.T) {
// use intermediate variables to not alter further computations
x := mat[0][0]*v[0] + mat[1][0]*v[1] + mat[2][0]*v[2] + mat[3][0]*v[3]
y := mat[0][1]*v[0] + mat[1][1]*v[1] + mat[2][1]*v[2] + mat[3][1]*v[3]
z := mat[0][2]*v[0] + mat[1][2]*v[1] + mat[2][2]*v[2] + mat[3][2]*v[3]
v[3] = mat[0][3]*v[0] + mat[1][3]*v[1] + mat[2][3]*v[2] + mat[3][3]*v[3]
v[0] = x
v[1] = y
v[2] = z
}
@panmari
Copy link
Author

panmari commented Jan 9, 2015

PASS
BenchmarkMulVec4    50000000            35.5 ns/op
BenchmarkAltMulVec4 30000000            53.1 ns/op
BenchmarkTransformVec4  50000000            24.5 ns/op
ok      github.com/ungerik/go3d/mat4    4.720s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment