Skip to content

Commit

Permalink
fix Union CPU implementations for more than 2 unions
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed Sep 28, 2024
1 parent 3f590fa commit 4528eaf
Show file tree
Hide file tree
Showing 15 changed files with 654 additions and 199 deletions.
44 changes: 18 additions & 26 deletions cpu_evaluators.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,26 +120,18 @@ func (u *OpUnion) Evaluate(pos []ms3.Vec, dist []float32, userData any) error {
}
auxDist := vp.Float.Acquire(len(dist))
defer vp.Float.Release(auxDist)
// This algorithm of switching distance buffers avoids needing to allocate
// a new distance buffer for every SDF in the union list.
distCalc, distAccum := dist, auxDist
if len(u.joined)%2 != 0 {
// If odd number of elements we ensure the last accumulated
// distance calculation is stored in dist to avoid needing to copy.
// All we need to do is switch our buffers once before we start.
distCalc, distAccum = distAccum, distCalc
}
for i := range u.joined {
err = evaluateSDF3(u.joined[i], pos, distCalc, userData)
err = evaluateSDF3(u.joined[0], pos, dist, userData)
if err != nil {
return err
}
for _, shape := range u.joined[1:] {
err = evaluateSDF3(shape, pos, auxDist, userData)
if err != nil {
return err
}
for i, d := range distAccum {
distAccum[i] = math32.Min(d, distCalc[i])
for i, d := range dist {
dist[i] = math32.Min(d, auxDist[i])
}
// Buffer switcheroo for next evaluation to not overwrite
// the minumum distance we just calculated.
distCalc, distAccum = distAccum, distCalc
}
return nil
}
Expand Down Expand Up @@ -553,11 +545,11 @@ func (l *line2D) Evaluate(pos []ms2.Vec, dist []float32, userData any) error {
a := l.a
ba := ms2.Sub(l.b, l.a)
dotba := ms2.Dot(ba, ba)
t := l.thick / 2
w := l.width / 2
for i, p := range pos {
pa := ms2.Sub(p, a)
h := ms1.Clamp(ms2.Dot(pa, ba)/dotba, 0, 1)
dist[i] = ms2.Norm(ms2.Sub(pa, ms2.Scale(h, ba))) - t
dist[i] = ms2.Norm(ms2.Sub(pa, ms2.Scale(h, ba))) - w
}
return nil
}
Expand Down Expand Up @@ -705,19 +697,19 @@ func (u *OpUnion2D) Evaluate(pos []ms2.Vec, dist []float32, userData any) error
}
auxDist := vp.Float.Acquire(len(dist))
defer vp.Float.Release(auxDist)
distCalc, distAccum := dist, auxDist
if len(u.joined)%2 != 0 {
distCalc, distAccum = distAccum, distCalc

err = evaluateSDF2(u.joined[0], pos, dist, userData)
if err != nil {
return err
}
for i := range u.joined {
err = evaluateSDF2(u.joined[i], pos, distCalc, userData)
for _, shape := range u.joined[1:] {
err = evaluateSDF2(shape, pos, auxDist, userData)
if err != nil {
return err
}
for i, d := range distAccum {
distAccum[i] = math32.Min(d, distCalc[i])
for i, d := range dist {
dist[i] = math32.Min(d, auxDist[i])
}
distCalc, distAccum = distAccum, distCalc
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion examples/bolt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func run() error {
}
defer fpvis.Close()

err = gsdfaux.Render(sdf, gsdfaux.RenderConfig{
err = gsdfaux.RenderShader3D(sdf, gsdfaux.RenderConfig{
STLOutput: fpstl,
VisualOutput: fpvis,
Resolution: sdf.Bounds().Diagonal() / 200,
Expand Down
2 changes: 1 addition & 1 deletion examples/fibonacci-showerhead/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func run() error {
}
defer fpvis.Close()

err = gsdfaux.Render(object, gsdfaux.RenderConfig{
err = gsdfaux.RenderShader3D(object, gsdfaux.RenderConfig{
STLOutput: fpstl,
VisualOutput: fpvis,
Resolution: object.Bounds().Diagonal() / 200,
Expand Down
9 changes: 7 additions & 2 deletions examples/gasket/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/soypat/glgl/math/ms2"
"github.com/soypat/gsdf"
"github.com/soypat/gsdf/glbuild"
"github.com/soypat/gsdf/gleval"
"github.com/soypat/gsdf/gsdfaux"
)

Expand Down Expand Up @@ -72,7 +73,11 @@ func scene() (glbuild.Shader3D, error) {
poly2 = gsdf.Offset2D(poly2, tol)
if visualization2D != "" {
start := time.Now()
err = gsdfaux.RenderPNGFile(visualization2D, poly2, 500, false, nil)
sdf, err := gleval.NewCPUSDF2(poly2)
if err != nil {
return nil, err
}
err = gsdfaux.RenderPNGFile(visualization2D, sdf, 500, nil)
if err != nil {
return nil, err
}
Expand All @@ -99,7 +104,7 @@ func run() error {
}
defer fpvis.Close()

err = gsdfaux.Render(object, gsdfaux.RenderConfig{
err = gsdfaux.RenderShader3D(object, gsdfaux.RenderConfig{
STLOutput: fpstl,
VisualOutput: fpvis,
Resolution: object.Bounds().Diagonal() / 350,
Expand Down
16 changes: 13 additions & 3 deletions examples/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"github.com/soypat/glgl/math/ms2"
"github.com/soypat/gsdf"
"github.com/soypat/gsdf/glbuild"
"github.com/soypat/gsdf/gleval"
"github.com/soypat/gsdf/gsdfaux"
)

const dim = 20
const filename = "circle.png"
const filename = "image-example.png"

func scene() (glbuild.Shader2D, error) {
s, err := gsdf.NewCircle(dim)
Expand All @@ -36,9 +37,18 @@ func main() {
if err != nil {
log.Fatal(err)
}
err = gsdfaux.RenderPNGFile(filename, s, 1080, useGPU, nil)
var sdf2 gleval.SDF2
if useGPU {
sdf2, err = gsdfaux.MakeGPUSDF2(s)
} else {
sdf2, err = gleval.NewCPUSDF2(s)
}
if err != nil {
log.Fatal(err)
}
err = gsdfaux.RenderPNGFile(filename, sdf2, 1080, nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("PNG file rendered")
fmt.Println("PNG file rendered to", filename)
}
2 changes: 1 addition & 1 deletion examples/npt-flange/flange.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func run() error {
}
defer fpvis.Close()

err = gsdfaux.Render(s, gsdfaux.RenderConfig{
err = gsdfaux.RenderShader3D(s, gsdfaux.RenderConfig{
STLOutput: fpstl,
VisualOutput: fpvis,
Resolution: s.Bounds().Diagonal() / 200,
Expand Down
9 changes: 7 additions & 2 deletions examples/plantpot/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/soypat/glgl/math/ms2"
"github.com/soypat/gsdf"
"github.com/soypat/gsdf/glbuild"
"github.com/soypat/gsdf/gleval"
"github.com/soypat/gsdf/gsdfaux"
)

Expand Down Expand Up @@ -54,7 +55,11 @@ func scenePotBase() (glbuild.Shader3D, error) {
if err != nil {
return nil, err
}
err = gsdfaux.RenderPNGFile(visualization2D, poly2, 1080, false, gsdfaux.ColorConversionInigoQuilez(poly2.Bounds().Diagonal()/3))
sdf, err := gleval.NewCPUSDF2(poly2)
if err != nil {
return nil, err
}
err = gsdfaux.RenderPNGFile(visualization2D, sdf, 1080, nil)
if err != nil {
return nil, err
}
Expand All @@ -79,7 +84,7 @@ func run() error {
}
defer fpvis.Close()

err = gsdfaux.Render(object, gsdfaux.RenderConfig{
err = gsdfaux.RenderShader3D(object, gsdfaux.RenderConfig{
STLOutput: fpstl,
VisualOutput: fpvis,
Resolution: object.Bounds().Diagonal() / 350,
Expand Down
Loading

0 comments on commit 4528eaf

Please sign in to comment.