diff --git a/.gitignore b/.gitignore index 66fd13c..30ba4b4 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,5 @@ *.out # Dependency directories (remove the comment below to include it) -# vendor/ +vendor/ +.vscode diff --git a/README.md b/README.md index b525534..a1d58b9 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,8 @@ # gglm + An OpenGL focused Go mathematics inspired by the C++ glm (OpenGL Mathematics) library + +## Notes + +You can check compiler inlining decisions using `go run -gcflags "-m" .`. Some functions look a bit weird compared to similar ones +because we are trying to reduce function complexity so the compiler inlines. diff --git a/gglm/geometric.go b/gglm/geometric.go index 4f6521d..810db86 100755 --- a/gglm/geometric.go +++ b/gglm/geometric.go @@ -1,5 +1,7 @@ package gglm +import "math" + func DotVec2(v1, v2 *Vec2) float32 { return v1.X()*v2.X() + v1.Y()*v2.Y() } @@ -24,54 +26,50 @@ func Cross(v1, v2 *Vec3) *Vec3 { //DistVec2 returns euclidean distance between v1 and v2 func DistVec2(v1, v2 *Vec2) float32 { - - diff := Vec2{ - Data: [2]float32{ - v1.X() - v2.X(), - v1.Y() - v2.Y(), - }, - } - - return diff.Mag() + x := v1.X() - v2.X() + y := v1.Y() - v2.Y() + return float32(math.Sqrt(float64(x*x + y*y))) } //DistVec3 returns euclidean distance between v1 and v2 func DistVec3(v1, v2 *Vec3) float32 { + x := v1.X() - v2.X() + y := v1.Y() - v2.Y() + z := v1.Z() - v2.Z() + return float32(math.Sqrt(float64(x*x + y*y + z*z))) +} - diff := Vec3{ - Data: [3]float32{ - v1.X() - v2.X(), - v1.Y() - v2.Y(), - v1.Z() - v2.Z(), - }, - } +//DistVec4 returns euclidean distance between v1 and v2 +func DistVec4(v1, v2 *Vec4) float32 { - return diff.Mag() + //Using X() etc won't let the function inline + x := v1.Data[0] - v2.Data[0] + y := v1.Data[1] - v2.Data[1] + z := v1.Data[2] - v2.Data[2] + w := v1.Data[3] - v2.Data[3] + return float32(math.Sqrt(float64(x*x + y*y + z*z + w*w))) } //DistVec2 returns the squared euclidean distance between v1 and v2 (avoids a sqrt) func SqrDistVec2(v1, v2 *Vec2) float32 { - - diff := Vec2{ - Data: [2]float32{ - v1.X() - v2.X(), - v1.Y() - v2.Y(), - }, - } - - return diff.SqrMag() + x := v1.X() - v2.X() + y := v1.Y() - v2.Y() + return x*x + y*y } //DistVec3 returns the squared euclidean distance between v1 and v2 (avoids a sqrt) func SqrDistVec3(v1, v2 *Vec3) float32 { - - diff := Vec3{ - Data: [3]float32{ - v1.X() - v2.X(), - v1.Y() - v2.Y(), - v1.Z() - v2.Z(), - }, - } - - return diff.SqrMag() + x := v1.X() - v2.X() + y := v1.Y() - v2.Y() + z := v1.Z() - v2.Z() + return x*x + y*y + z*z +} + +//DistVec4 returns the squared euclidean distance between v1 and v2 (avoids a sqrt) +func SqrDistVec4(v1, v2 *Vec4) float32 { + x := v1.Data[0] - v2.Data[0] + y := v1.Data[1] - v2.Data[1] + z := v1.Data[2] - v2.Data[2] + w := v1.Data[3] - v2.Data[3] + return x*x + y*y + z*z + w*w } diff --git a/main.go b/main.go index cbc3f6d..25a1abe 100755 --- a/main.go +++ b/main.go @@ -51,12 +51,36 @@ func main() { println(m6.String()) println(m4.Eq(m6)) + //Vec2 v1 := &gglm.Vec2{Data: [2]float32{1, 2}} v2 := &gglm.Vec2{Data: [2]float32{3, 4}} println(gglm.DistVec2(v1, v2)) println(gglm.SqrDistVec2(v2, v1)) - println(v1.Eq(v2)) v2.Set(1, 2) println(v1.Eq(v2)) + + //Vec3 + v3 := &gglm.Vec3{Data: [3]float32{1, 2, 3}} + v4 := &gglm.Vec3{Data: [3]float32{4, 5, 6}} + println(gglm.DistVec3(v3, v4)) + println(gglm.SqrDistVec3(v4, v3)) + + println(v3.Eq(v4)) + v4.Set(1, 2, 3) + println(v3.Eq(v4)) + + println(gglm.DotVec3(v3, v4)) + + //Vec4 + v5 := &gglm.Vec4{Data: [4]float32{1, 2, 3, 4}} + v6 := &gglm.Vec4{Data: [4]float32{5, 6, 7, 8}} + println(gglm.DistVec4(v5, v6)) + println(gglm.SqrDistVec4(v5, v6)) + + println(v5.Eq(v6)) + v6.Set(1, 2, 3, 4) + println(v5.Eq(v6)) + + println(gglm.DotVec4(v5, v6)) }