Skip to content

Commit

Permalink
add annulus, docs update, and replace Sprintf with Appendf in Array
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed Sep 20, 2024
1 parent d2d02e0 commit 4272b54
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
16 changes: 16 additions & 0 deletions cpu_evaluators.go
Original file line number Diff line number Diff line change
Expand Up @@ -883,3 +883,19 @@ func (s *symmetry2D) Evaluate(pos []ms2.Vec, dist []float32, userData any) error
}
return nil
}

func (s *annulus2D) Evaluate(pos []ms2.Vec, dist []float32, userData any) error {
sdf, err := gleval.AssertSDF2(s.s)
if err != nil {
return err
}
err = sdf.Evaluate(pos, dist, userData)
if err != nil {
return err
}
r := s.r
for i, d := range dist {
dist[i] = math32.Abs(d) - r
}
return nil
}
37 changes: 37 additions & 0 deletions gsdf2d.go
Original file line number Diff line number Diff line change
Expand Up @@ -923,3 +923,40 @@ func (s *symmetry2D) AppendShaderBody(b []byte) []byte {
b = append(b, "(p);"...)
return b
}

// Annulus makes a 2D shape annular by emptying it's center. It is the equivalent of the 3D Shell operation but in 2D.
func Annulus(s glbuild.Shader2D, sub float32) (glbuild.Shader2D, error) {
if s == nil {
return nil, errors.New("nil argument to Annulus")
} else if sub <= 0 {
return nil, errors.New("invalid annular parameter")
}
return &annulus2D{s: s, r: sub}, nil
}

type annulus2D struct {
s glbuild.Shader2D
r float32
}

func (u *annulus2D) Bounds() ms2.Box {
return u.s.Bounds()
}

func (s *annulus2D) ForEach2DChild(userData any, fn func(userData any, s *glbuild.Shader2D) error) error {
return fn(userData, &s.s)
}

func (s *annulus2D) AppendShaderName(b []byte) []byte {
b = append(b, "annulus"...)
b = append(b, '_')
b = s.s.AppendShaderName(b)
return b
}

func (s *annulus2D) AppendShaderBody(b []byte) []byte {
b = glbuild.AppendFloatDecl(b, "r", s.r)
b = glbuild.AppendDistanceDecl(b, "d", "p", s.s)
b = append(b, "return abs(d)-r;"...)
return b
}
8 changes: 3 additions & 5 deletions operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ func (s *offset) AppendShaderBody(b []byte) []byte {
return b
}

// Array is the domain repetition operation. It repeats domain centered around (x,y,z)=(0,0,0)
// Array is the domain repetition operation. It repeats domain centered around the origin (x,y,z)=(0,0,0).
func Array(s glbuild.Shader3D, spacingX, spacingY, spacingZ float32, nx, ny, nz int) (glbuild.Shader3D, error) {
if nx <= 0 || ny <= 0 || nz <= 0 {
return nil, errors.New("invalid array repeat param")
Expand Down Expand Up @@ -434,7 +434,7 @@ func (s *array) AppendShaderBody(b []byte) []byte {
// o is neighbor offset direction (which neighboring tile is closest in 3 directions)
// s is scaling factors in 3 directions.
// rid is the neighboring tile index, which is then corrected for limited repetition using clamp.
body := fmt.Sprintf(`
b = fmt.Appendf(b, `
vec3 s = vec3(%f,%f,%f);
vec3 n = vec3(%d.,%d.,%d.);
vec3 minlim = vec3(0.,0.,0.);
Expand All @@ -453,9 +453,7 @@ for( int i=0; i<2; i++ )
}
return d;`, s.d.X, s.d.Y, s.d.Z,
s.nx-1, s.ny-1, s.nz-1,
largenum, sdf,
)
b = append(b, body...)
largenum, sdf)
return b
}

Expand Down

0 comments on commit 4272b54

Please sign in to comment.