Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A lot of changes you may find useful #3

Open
wants to merge 58 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
9bcd03f
adding rect2, expose vector fields.
igadmg Mar 31, 2024
acffdb4
comparison functions. float64 to T return type changed where that mak…
igadmg Apr 4, 2024
f169279
div by vector.
igadmg Apr 4, 2024
62860e6
fixed tests.
igadmg Apr 5, 2024
446ab90
Added conversion functions.
igadmg Apr 6, 2024
0975c39
more vector and rect functions
igadmg Apr 7, 2024
3597796
rect Shift function
igadmg Apr 7, 2024
a5f4ebf
New functions ShrinkXYWH and Reciprocal
igadmg Apr 8, 2024
717ff45
added center function.
igadmg Apr 10, 2024
dca9347
redefine calmp in min/max functions.
igadmg Apr 11, 2024
cb46783
Added mathex module. Update on math function usage.
igadmg Apr 11, 2024
d561c6f
replace math.Min/Max with min/max
igadmg Apr 11, 2024
a3b1595
Added more mathex functions
igadmg Apr 11, 2024
a73b215
updates on math. hope it is correct ) ... need to write tests
igadmg Apr 11, 2024
c5c95aa
Added npot function and some rect manipulations.
igadmg Apr 12, 2024
49e7ab7
Reciprocal functions fix
igadmg Apr 13, 2024
427d6aa
rect2 shiftxy
igadmg Apr 17, 2024
2c7155b
Merge remote-tracking branch 'origin/main' into master_patched
igadmg Apr 18, 2024
c62959a
Fixed build
igadmg Apr 18, 2024
aed1c31
HorizontalLine and VerticalLine for rect2
igadmg Apr 18, 2024
b4542c6
added some tests for vec2
igadmg Apr 23, 2024
bfbed65
reverted to accessor methods for properties.
igadmg Apr 23, 2024
bd23383
Restored tests
igadmg Apr 23, 2024
e202a0e
added more functions fixed test.
igadmg Apr 23, 2024
0a220b4
Extracted test aux functions to test package.
igadmg Apr 24, 2024
ce8afca
Negated function added.
igadmg Apr 24, 2024
21ab2dd
Merge branch 'EliCDavis:main' into main
igadmg Apr 24, 2024
a68ded5
Merge remote-tracking branch 'origin/main' into master_patched
igadmg Apr 24, 2024
c29fab6
Merge branch 'EliCDavis:main' into main
igadmg Apr 26, 2024
a4ddefc
Merge remote-tracking branch 'origin/main' into master_patched
igadmg Apr 26, 2024
bfd1fe3
added some more math to benchmarks.
igadmg Apr 26, 2024
b4e3011
clamp test
igadmg Apr 27, 2024
dfbd4bf
use A and B instead of manual calcualtion on *Line functions.
igadmg Apr 28, 2024
25e2cc3
Update rect2/rect2.go
igadmg Apr 30, 2024
90ded3b
Dx->AddX Update vector2/vector2.go
igadmg Apr 30, 2024
d680d36
Update rect2/rect2.go
igadmg Apr 30, 2024
a81d4f7
SetPosition Update rect2/rect2.go
igadmg Apr 30, 2024
7ec6c14
Size Update rect2/rect2.go
igadmg Apr 30, 2024
f32906a
Size Update rect2/rect2.go
igadmg Apr 30, 2024
c33b8bc
SetSize Update rect2/rect2.go
igadmg Apr 30, 2024
96511ec
NearZero Update mathex/mathex.go
igadmg Apr 30, 2024
ff0f752
Lerp Update mathex/mathex.go
igadmg Apr 30, 2024
a706f88
Normalize Update mathex/mathex.go
igadmg Apr 30, 2024
62d5e9b
Merge branch 'master_patched' of github.com:igadmg/vector into master…
igadmg Apr 30, 2024
be0597b
api updates.
igadmg Apr 30, 2024
2facace
added some test.
igadmg Apr 30, 2024
af6db31
changed lerp parameter ordering
igadmg Apr 30, 2024
8ab0a34
More tests
igadmg Apr 30, 2024
aba5a45
mathex tests
igadmg Apr 30, 2024
e80bbb9
some comments
igadmg Apr 30, 2024
6405290
Update vector4/vector4.go
igadmg Apr 30, 2024
1c4f639
Update vector4/vector4.go
igadmg Apr 30, 2024
74baa67
Update vector3/vector3.go
igadmg Apr 30, 2024
6770edb
Update vector3/vector3.go
igadmg Apr 30, 2024
0a4fd75
Update vector2/vector2.go
igadmg Apr 30, 2024
752bf49
fixed function renames.
igadmg Apr 30, 2024
f06165c
removed rect2. fixed tests.
igadmg May 1, 2024
f49be6d
Added ScaleByXYI
igadmg May 3, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 65 additions & 14 deletions experiments/add_test.go
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move changes from this benchmark into a new file and benchmark. To improve it, it'd be cool to include more implemented functions in the vector libs

Original file line number Diff line number Diff line change
Expand Up @@ -12,64 +12,106 @@ var addResultMFloat64 MVector[float64]

func BenchmarkAddVector(b *testing.B) {
var r vector3.Vector[float64]
a := vector3.New(1., 2., 3.)
va := vector3.New(1., 2., 3.)
vb := vector3.New(2., 3., 4.)
Comment on lines -15 to +16
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move variables inside for loop, and have them take into account n somehow to prevent the compiler to making them a constant.

This goes for all other instances of this in this file


for n := 0; n < b.N; n++ {
r = r.Add(a)
r = r.Add(va).MultByVector(va).
Add(va.Reciprocal()).MultByVector(va.Reciprocal()).
MultByVector(vb.Reciprocal()).Add(vb.Reciprocal())
}
addResultFloat64 = r
runtime.KeepAlive(r)
}

func BenchmarkAddMutableVector(b *testing.B) {
var r MVector[float64]
a := MVector[float64]{1., 2., 3.}
va := MVector[float64]{1., 2., 3.}
vb := MVector[float64]{2., 3., 4.}

for n := 0; n < b.N; n++ {
r = r.Add(a)
r = r.Add(va).MultByVector(va).
Add(va.Reciprocal()).MultByVector(va.Reciprocal()).
MultByVector(vb.Reciprocal()).Add(vb.Reciprocal())
}
addResultMFloat64 = r
runtime.KeepAlive(r)
}

func BenchmarkAddMutableVectorInPlace(b *testing.B) {
var r MVector[float64]
a := MVector[float64]{1., 2., 3.}
va := MVector[float64]{1., 2., 3.}
vb := MVector[float64]{2., 3., 4.}
for n := 0; n < b.N; n++ {
r.AddInPlace(a)
r.AddInPlace(va)
r.MultInPlace(va)
r.AddInPlace(va.Reciprocal())
r.MultInPlace(va.Reciprocal())
r.MultInPlace(vb.Reciprocal())
r.AddInPlace(vb.Reciprocal())
}
addResultMFloat64 = r
runtime.KeepAlive(r)
}

func BenchmarkAddMutableVectorInPlaceUsingPointer(b *testing.B) {
var r *MVector[float64] = &MVector[float64]{1, 2, 3}
a := MVector[float64]{1., 2., 3.}
va := MVector[float64]{1., 2., 3.}
vb := MVector[float64]{2., 3., 4.}

for n := 0; n < b.N; n++ {
r.AddInPlace(a)
r.AddInPlace(va)
r.MultInPlace(va)
r.AddInPlace(va.Reciprocal())
r.MultInPlace(va.Reciprocal())
r.MultInPlace(vb.Reciprocal())
r.AddInPlace(vb.Reciprocal())
}

addResultMFloat64 = *r
runtime.KeepAlive(r)
}

func BenchmarkAddMutableVectorInPlaceDirect(b *testing.B) {
var r MVector[float64]
va := MVector[float64]{1., 2., 3.}
vb := MVector[float64]{2., 3., 4.}
for n := 0; n < b.N; n++ {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

factor out the reciprocal to make it 1:1 in the immutable use-case to make it a fair comparison

r.X = ((r.X+va.X)*va.X+1/va.X)/va.X/vb.X + 1/vb.X
r.Y = ((r.Y+va.Y)*va.Y+1/va.Y)/va.Y/vb.Y + 1/vb.Y
}
Comment on lines +81 to +82
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the z component to make it a fair benchmark

Suggested change
r.Y = ((r.Y+va.Y)*va.Y+1/va.Y)/va.Y/vb.Y + 1/vb.Y
}
r.Y = ((r.Y+va.Y)*va.Y+1/va.Y)/va.Y/vb.Y + 1/vb.Y
r.Z = ((r.Z+va.Z)*va.Z+1/va.Z)/va.Z/vb.Z + 1/vb.Z
}

addResultMFloat64 = r
runtime.KeepAlive(r)
}

func BenchmarkAddMutableVectorInPlaceWithReturn(b *testing.B) {
var r MVector[float64]
a := MVector[float64]{1., 2., 3.}
va := MVector[float64]{1., 2., 3.}
vb := MVector[float64]{2., 3., 4.}
for n := 0; n < b.N; n++ {
r.AddInPlaceAndReturn(a)
r.AddInPlaceAndReturn(va)
r.MultInPlaceAndReturn(va)
r.AddInPlaceAndReturn(va.Reciprocal())
r.MultInPlaceAndReturn(va.Reciprocal())
r.MultInPlaceAndReturn(vb.Reciprocal())
r.AddInPlaceAndReturn(vb.Reciprocal())
}
addResultMFloat64 = r
runtime.KeepAlive(r)
}

func BenchmarkAddMutableVectorInPlaceWithReturnUsingPointer(b *testing.B) {
var r *MVector[float64] = &MVector[float64]{1, 2, 3}
a := MVector[float64]{1., 2., 3.}
va := MVector[float64]{1., 2., 3.}
vb := MVector[float64]{2., 3., 4.}

for n := 0; n < b.N; n++ {
r.AddInPlaceAndReturn(a)
r.AddInPlaceAndReturn(va)
r.MultInPlaceAndReturn(va)
r.AddInPlaceAndReturn(va.Reciprocal())
r.MultInPlaceAndReturn(va.Reciprocal())
r.MultInPlaceAndReturn(vb.Reciprocal())
r.AddInPlaceAndReturn(vb.Reciprocal())
}

addResultMFloat64 = *r
Expand All @@ -78,12 +120,21 @@ func BenchmarkAddMutableVectorInPlaceWithReturnUsingPointer(b *testing.B) {

func BenchmarkAddMutableVectorInPlaceTakingPointerUsingPointer(b *testing.B) {
var r *MVector[float64] = &MVector[float64]{1, 2, 3}
a := &MVector[float64]{1., 2., 3.}
va := &MVector[float64]{1., 2., 3.}
vb := &MVector[float64]{2., 3., 4.}

for n := 0; n < b.N; n++ {
r.AddInPlaceTakingPointer(a)
ra := va.Reciprocal()
rb := vb.Reciprocal()
r.AddInPlaceTakingPointer(va)
r.MultInPlaceTakingPointer(va)
r.AddInPlaceTakingPointer(&ra)
r.MultInPlaceTakingPointer(&ra)
r.MultInPlaceTakingPointer(&rb)
r.AddInPlaceTakingPointer(&rb)
}

addResultMFloat64 = *r
runtime.KeepAlive(r)
runtime.KeepAlive(addResultMFloat64)
}
118 changes: 118 additions & 0 deletions experiments/clamp_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package experiments_test

import (
"math/rand"
"runtime"
"testing"

"github.com/EliCDavis/vector"
"github.com/EliCDavis/vector/mathex"
"github.com/EliCDavis/vector/vector2"
"golang.org/x/exp/constraints"
)

type Number interface {
constraints.Integer | constraints.Float | vector.Number
}

var r = rand.New(rand.NewSource(99))

func ClampGenericMinMax[T Number](f, vmin, vmax T) T {
return max(min(f, vmax), vmin)
}

func ClampGenericCompare[T Number](f, vmin, vmax T) T {
if f <= vmin {
return vmin
}
if f >= vmax {
return vmax
}
return f
}

func ClampMinMax(f, vmin, vmax float64) float64 {
return max(min(f, vmax), vmin)
}

func ClampCompare(f, vmin, vmax float64) float64 {
if f <= vmin {
return vmin
}
if f >= vmax {
return vmax
}
return f
}

func BenchmarkClampMathEx(b *testing.B) {
var res int
i := r.Int()
clamp := vector2.New(r.Int(), r.Int())
imin := clamp.MinComponent()
imax := clamp.MaxComponent()
b.ResetTimer()

for n := 0; n < b.N; n++ {
res += mathex.Clamp(i, imin, imax)
}
runtime.KeepAlive(res)
}

func BenchmarkClampGenericMinMax(b *testing.B) {
var res int
i := r.Int()
clamp := vector2.New(r.Int(), r.Int())
imin := clamp.MinComponent()
imax := clamp.MaxComponent()
b.ResetTimer()

for n := 0; n < b.N; n++ {
res += ClampGenericMinMax(i, imin, imax)
}
runtime.KeepAlive(res)
}

func BenchmarkClampGenericCompare(b *testing.B) {
var res int
i := r.Int()
clamp := vector2.New(r.Int(), r.Int())
imin := clamp.MinComponent()
imax := clamp.MaxComponent()
b.ResetTimer()

for n := 0; n < b.N; n++ {
res += ClampGenericCompare(i, imin, imax)
}
runtime.KeepAlive(res)
}

func BenchmarkClampMinMax(b *testing.B) {
var res int
i := r.Int()
clamp := vector2.New(r.Int(), r.Int())
imin := clamp.MinComponent()
imax := clamp.MaxComponent()
b.ResetTimer()

for n := 0; n < b.N; n++ {
res += (int)(ClampMinMax(float64(i), float64(imin), float64(imax)))
}

runtime.KeepAlive(res)
}

func BenchmarkClampCompare(b *testing.B) {
var res int
i := r.Int()
clamp := vector2.New(r.Int(), r.Int())
imin := clamp.MinComponent()
imax := clamp.MaxComponent()
b.ResetTimer()

for n := 0; n < b.N; n++ {
res += (int)(ClampCompare(float64(i), float64(imin), float64(imax)))
}

runtime.KeepAlive(res)
}
35 changes: 35 additions & 0 deletions experiments/mutable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ type MVector[T vector.Number] struct {
Z T
}

func (v MVector[T]) Reciprocal() MVector[float64] {
return MVector[float64]{
X: 1.0 / float64(v.X),
Y: 1.0 / float64(v.Y),
Z: 1.0 / float64(v.Z),
}
}

func (v MVector[T]) Add(a MVector[T]) MVector[T] {
return MVector[T]{
X: v.X + a.X,
Expand All @@ -16,21 +24,48 @@ func (v MVector[T]) Add(a MVector[T]) MVector[T] {
}
}

func (v MVector[T]) MultByVector(a MVector[T]) MVector[T] {
return MVector[T]{
X: v.X * a.X,
Y: v.Y * a.Y,
Z: v.Z * a.Z,
}
}

func (v *MVector[T]) AddInPlace(a MVector[T]) {
v.X += a.X
v.Y += a.Y
v.Z += a.Z
}

func (v *MVector[T]) MultInPlace(a MVector[T]) {
v.X *= a.X
v.Y *= a.Y
v.Z *= a.Z
}

func (v *MVector[T]) AddInPlaceAndReturn(a MVector[T]) MVector[T] {
v.X += a.X
v.Y += a.Y
v.Z += a.Z
return *v
}

func (v *MVector[T]) MultInPlaceAndReturn(a MVector[T]) MVector[T] {
v.X *= a.X
v.Y *= a.Y
v.Z *= a.Z
return *v
}

func (v *MVector[T]) AddInPlaceTakingPointer(a *MVector[T]) {
v.X += a.X
v.Y += a.Y
v.Z += a.Z
}

func (v *MVector[T]) MultInPlaceTakingPointer(a *MVector[T]) {
v.X *= a.X
v.Y *= a.Y
v.Z *= a.Z
}
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
module github.com/EliCDavis/vector

go 1.19
go 1.21
igadmg marked this conversation as resolved.
Show resolved Hide resolved

require github.com/stretchr/testify v1.8.1
require github.com/stretchr/testify v1.9.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8
gopkg.in/yaml.v3 v3.0.1 // indirect
)
13 changes: 4 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8 h1:ESSUROHIBHg7USnszlcdmjBEwdMj9VUvU+OPk4yl2mc=
golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8/go.mod h1:/lliqkxwWAhPjf5oSOIJup2XcqJaw8RGS6k3TGEc7GI=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading