mirror of
https://github.com/bloeys/gglm.git
synced 2025-12-29 13:38:20 +00:00
Ensure Dist/SqrDist funcs inline + DistVec4 + SqrDistVec4
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@ -12,4 +12,5 @@
|
|||||||
*.out
|
*.out
|
||||||
|
|
||||||
# Dependency directories (remove the comment below to include it)
|
# Dependency directories (remove the comment below to include it)
|
||||||
# vendor/
|
vendor/
|
||||||
|
.vscode
|
||||||
|
|||||||
@ -1,2 +1,8 @@
|
|||||||
# gglm
|
# gglm
|
||||||
|
|
||||||
An OpenGL focused Go mathematics inspired by the C++ glm (OpenGL Mathematics) library
|
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.
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
package gglm
|
package gglm
|
||||||
|
|
||||||
|
import "math"
|
||||||
|
|
||||||
func DotVec2(v1, v2 *Vec2) float32 {
|
func DotVec2(v1, v2 *Vec2) float32 {
|
||||||
return v1.X()*v2.X() + v1.Y()*v2.Y()
|
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
|
//DistVec2 returns euclidean distance between v1 and v2
|
||||||
func DistVec2(v1, v2 *Vec2) float32 {
|
func DistVec2(v1, v2 *Vec2) float32 {
|
||||||
|
x := v1.X() - v2.X()
|
||||||
diff := Vec2{
|
y := v1.Y() - v2.Y()
|
||||||
Data: [2]float32{
|
return float32(math.Sqrt(float64(x*x + y*y)))
|
||||||
v1.X() - v2.X(),
|
|
||||||
v1.Y() - v2.Y(),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
return diff.Mag()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//DistVec3 returns euclidean distance between v1 and v2
|
//DistVec3 returns euclidean distance between v1 and v2
|
||||||
func DistVec3(v1, v2 *Vec3) float32 {
|
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{
|
//DistVec4 returns euclidean distance between v1 and v2
|
||||||
Data: [3]float32{
|
func DistVec4(v1, v2 *Vec4) float32 {
|
||||||
v1.X() - v2.X(),
|
|
||||||
v1.Y() - v2.Y(),
|
|
||||||
v1.Z() - v2.Z(),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
//DistVec2 returns the squared euclidean distance between v1 and v2 (avoids a sqrt)
|
||||||
func SqrDistVec2(v1, v2 *Vec2) float32 {
|
func SqrDistVec2(v1, v2 *Vec2) float32 {
|
||||||
|
x := v1.X() - v2.X()
|
||||||
diff := Vec2{
|
y := v1.Y() - v2.Y()
|
||||||
Data: [2]float32{
|
return x*x + y*y
|
||||||
v1.X() - v2.X(),
|
|
||||||
v1.Y() - v2.Y(),
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
return diff.SqrMag()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//DistVec3 returns the squared euclidean distance between v1 and v2 (avoids a sqrt)
|
//DistVec3 returns the squared euclidean distance between v1 and v2 (avoids a sqrt)
|
||||||
func SqrDistVec3(v1, v2 *Vec3) float32 {
|
func SqrDistVec3(v1, v2 *Vec3) float32 {
|
||||||
|
x := v1.X() - v2.X()
|
||||||
diff := Vec3{
|
y := v1.Y() - v2.Y()
|
||||||
Data: [3]float32{
|
z := v1.Z() - v2.Z()
|
||||||
v1.X() - v2.X(),
|
return x*x + y*y + z*z
|
||||||
v1.Y() - v2.Y(),
|
}
|
||||||
v1.Z() - v2.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]
|
||||||
return diff.SqrMag()
|
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
|
||||||
}
|
}
|
||||||
|
|||||||
26
main.go
26
main.go
@ -51,12 +51,36 @@ func main() {
|
|||||||
println(m6.String())
|
println(m6.String())
|
||||||
println(m4.Eq(m6))
|
println(m4.Eq(m6))
|
||||||
|
|
||||||
|
//Vec2
|
||||||
v1 := &gglm.Vec2{Data: [2]float32{1, 2}}
|
v1 := &gglm.Vec2{Data: [2]float32{1, 2}}
|
||||||
v2 := &gglm.Vec2{Data: [2]float32{3, 4}}
|
v2 := &gglm.Vec2{Data: [2]float32{3, 4}}
|
||||||
println(gglm.DistVec2(v1, v2))
|
println(gglm.DistVec2(v1, v2))
|
||||||
println(gglm.SqrDistVec2(v2, v1))
|
println(gglm.SqrDistVec2(v2, v1))
|
||||||
|
|
||||||
println(v1.Eq(v2))
|
println(v1.Eq(v2))
|
||||||
v2.Set(1, 2)
|
v2.Set(1, 2)
|
||||||
println(v1.Eq(v2))
|
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))
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user