Skip to content

Commit

Permalink
added example
Browse files Browse the repository at this point in the history
  • Loading branch information
EliCDavis committed Dec 27, 2023
1 parent 851e4fd commit 55044e0
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 11 deletions.
95 changes: 90 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,17 @@ Collection of **generic, immutable** vector math functions I've written overtime
Below is an example on how to implement the different sign distance field functions in a generic fashion to work for both int, int64, float32, and float64.

```go
package sdf
package main

import (
"image"
"image/color"
"image/png"
"math"
"os"

"github.com/EliCDavis/vector"
"github.com/EliCDavis/vector/vector2"
"github.com/EliCDavis/vector/vector3"
)

Expand All @@ -91,15 +96,39 @@ func Box[T vector.Number](pos vector3.Vector[T], bounds vector3.Vector[T]) Field
}
}

func Union[T vector.Number](a, b Field[T]) Field[T] {
func Union[T vector.Number](fields ...Field[T]) Field[T] {
return func(v vector3.Vector[T]) float64 {
return math.Min(a(v), b(v))
min := math.MaxFloat64

for _, f := range fields {
fv := f(v)
if fv < min {
min = fv
}
}

return min
}
}

func Intersect[T vector.Number](a, b Field[T]) Field[T] {
func Intersect[T vector.Number](fields ...Field[T]) Field[T] {
return func(v vector3.Vector[T]) float64 {
return math.Max(a(v), b(v))
max := -math.MaxFloat64

for _, f := range fields {
fv := f(v)
if fv > max {
max = fv
}
}

return max
}
}

func Subtract[T vector.Number](minuend, subtrahend Field[T]) Field[T] {
return func(f vector3.Vector[T]) float64 {
return math.Max(minuend(f), -subtrahend(f))
}
}

Expand All @@ -108,4 +137,60 @@ func Translate[T vector.Number](field Field[T], translation vector3.Vector[T]) F
return field(v.Sub(translation))
}
}

func main() {
dimension := 512
quarterDim := float64(dimension) / 4.

middleCoord := vector2.
Fill(dimension).
Scale(0.5).
ToFloat64()

middleCord3D := vector3.New(middleCoord.X(), middleCoord.Y(), 0)

smallRing := Subtract(
Sphere(middleCord3D, 100),
Sphere(middleCord3D, 50),
)

field := Intersect(
Subtract(
Sphere(middleCord3D, 200),
Sphere(middleCord3D, 100),
),
Union(
Translate(smallRing, vector3.New(1., 1., 0.).Scale(quarterDim)),
Translate(smallRing, vector3.New(1., -1., 0.).Scale(quarterDim)),
Translate(smallRing, vector3.New(-1., 1., 0.).Scale(quarterDim)),
Translate(smallRing, vector3.New(-1., -1., 0.).Scale(quarterDim)),
Box(middleCord3D, middleCord3D),
),
)

img := image.NewRGBA(image.Rectangle{
image.Point{0, 0},
image.Point{dimension, dimension},
})

for x := 0; x < dimension; x++ {
for y := 0; y < dimension; y++ {
v := field(vector3.New(x, y, 0).ToFloat64())
byteVal := (v / float64(dimension/20)) * 255
var c color.Color
if v > 0 {
c = color.RGBA{R: 0, G: 0, B: byte(byteVal), A: 255}
} else {
c = color.RGBA{R: 0, G: byte(-byteVal), B: 0, A: 255}
}
img.Set(x, y, c)
}
}

f, _ := os.Create("field.png")
png.Encode(f, img)
}
```


![field.png](./field.png)
Binary file added field.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 8 additions & 2 deletions vector2/vector2.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ func One[T vector.Number]() Vector[T] {

// Lerp linearly interpolates between a and b by t
func Lerp[T vector.Number](a, b Vector[T], t float64) Vector[T] {
return b.Sub(a).Scale(t).Add(a)
return Vector[T]{
x: T((float64(b.x-a.x) * t) + float64(a.x)),
y: T((float64(b.y-a.y) * t) + float64(a.y)),
}
}

func Min[T vector.Number](a, b Vector[T]) Vector[T] {
Expand Down Expand Up @@ -121,7 +124,10 @@ func Midpoint[T vector.Number](a, b Vector[T]) Vector[T] {
// center = b0.5 - a0.5 + a
// center = b0.5 + a0.5
// center = 0.5(b + a)
return b.Add(a).Scale(0.5)
return Vector[T]{
x: T(float64(a.x+b.x) * 0.5),
y: T(float64(a.y+b.y) * 0.5),
}
}

// Builds a vector from the data found from the passed in array to the best of
Expand Down
12 changes: 10 additions & 2 deletions vector3/vector3.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ func Average[T vector.Number](vectors []Vector[T]) Vector[T] {

// Lerp linearly interpolates between a and b by t
func Lerp[T vector.Number](a, b Vector[T], t float64) Vector[T] {
return b.Sub(a).Scale(t).Add(a)
return Vector[T]{
x: T((float64(b.x-a.x) * t) + float64(a.x)),
y: T((float64(b.y-a.y) * t) + float64(a.y)),
z: T((float64(b.z-a.z) * t) + float64(a.z)),
}
}

func Min[T vector.Number](a, b Vector[T]) Vector[T] {
Expand Down Expand Up @@ -151,7 +155,11 @@ func Midpoint[T vector.Number](a, b Vector[T]) Vector[T] {
// center = b0.5 - a0.5 + a
// center = b0.5 + a0.5
// center = 0.5(b + a)
return b.Add(a).Scale(0.5)
return Vector[T]{
x: T(float64(a.x+b.x) * 0.5),
y: T(float64(a.y+b.y) * 0.5),
z: T(float64(a.z+b.z) * 0.5),
}
}

// Builds a vector from the data found from the passed in array to the best of
Expand Down
31 changes: 29 additions & 2 deletions vector4/vector4.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,31 @@ func Average[T vector.Number](vectors []Vector[T]) Vector[T] {
return center.DivByConstant(float64(len(vectors)))
}

// Lerp linearly interpolates between a and b by t
// func Lerp[T vector.Number](a, b Vector[T], t float64) Vector[T] {

// // (b - a) * t + a
// // bt - at + a
// // bt - a(1 - t)
// tm1 := 1. - t
// return Vector[T]{
// x: T((float64(b.x) * t) - (float64(a.x) * tm1)),
// y: T((float64(b.y) * t) - (float64(a.y) * tm1)),
// z: T((float64(b.z) * t) - (float64(a.z) * tm1)),
// w: T((float64(b.w) * t) - (float64(a.w) * tm1)),
// }
// }

// Lerp linearly interpolates between a and b by t
func Lerp[T vector.Number](a, b Vector[T], t float64) Vector[T] {
return b.Sub(a).Scale(t).Add(a)

// return b.Sub(a).Scale(t).Add(a)
return Vector[T]{
x: T((float64(b.x-a.x) * t) + float64(a.x)),
y: T((float64(b.y-a.y) * t) + float64(a.y)),
z: T((float64(b.z-a.z) * t) + float64(a.z)),
w: T((float64(b.w-a.w) * t) + float64(a.w)),
}
}

func (v Vector[T]) Scale(t float64) Vector[T] {
Expand Down Expand Up @@ -152,7 +174,12 @@ func Midpoint[T vector.Number](a, b Vector[T]) Vector[T] {
// center = b0.5 - a0.5 + a
// center = b0.5 + a0.5
// center = 0.5(b + a)
return b.Add(a).Scale(0.5)
return Vector[T]{
x: T(float64(a.x+b.x) * 0.5),
y: T(float64(a.y+b.y) * 0.5),
z: T(float64(a.z+b.z) * 0.5),
w: T(float64(a.w+b.w) * 0.5),
}
}

// Builds a vector from the data found from the passed in array to the best of
Expand Down
12 changes: 12 additions & 0 deletions vector4/vector4_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,3 +449,15 @@ func TestMaxMinComponents(t *testing.T) {
})
}
}

var result vector4.Float64

func BenchmarkLerp(b *testing.B) {
var r vector4.Float64
a := vector4.New(1., 2., 3., 7.)
c := vector4.New(4., 5., 6., 8.)
for i := 0; i < b.N; i++ {
r = vector4.Lerp(a, c, float64(i)/float64(b.N))
}
result = r
}

0 comments on commit 55044e0

Please sign in to comment.