Skip to content

Commit

Permalink
refactor glbuild.AppendFloats API
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed Aug 17, 2024
1 parent 4d45dd6 commit 42a82f0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
3 changes: 2 additions & 1 deletion examples/npt-flange/flange.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,9 @@ func scene() (gleval.SDF3, error) {

pipe, _ := threads.Nut(threads.NutParms{
Thread: npt,
Style: threads.NutCircular,
Style: threads.NutKnurl,
})
return makeSDF(pipe)
// Base plate which goes bolted to joint.
flange, _ = gsdf.NewCylinder(flangeD/2, flangeH, flangeH/8)
// Make through-hole in flange bottom.
Expand Down
15 changes: 8 additions & 7 deletions forge/threads/threads.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ func Screw(length float32, thread Threader) (glbuild.Shader3D, error) {
return nil, err
}
params := thread.ThreadParams()
s := screw{}
s.thread = tsdf
s.pitch = params.Pitch
s.length = length / 2
s.length *= 1 + 1e-3
s.taper = params.Taper
s.lead = -s.pitch * float32(params.Starts)
s := screw{
thread: tsdf,
pitch: params.Pitch,
lead: -params.Pitch * float32(params.Starts),
length: length / 2,
taper: params.Taper,
}
return &s, nil
}

Expand All @@ -104,6 +104,7 @@ func (s *screw) ForEach2DChild(userData any, fn func(any, *glbuild.Shader2D) err

func (s *screw) AppendShaderName(b []byte) []byte {
b = append(b, "screw_"...)
b = glbuild.AppendFloats(b, 0, 'n', 'd', s.pitch, s.lead, s.length, s.taper)
b = s.thread.AppendShaderName(b)
return b
}
Expand Down
22 changes: 16 additions & 6 deletions glbuild/glbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func (p *Programmer) writeShaders(w io.Writer, nodes []Shader) (n int, err error
if bodyHash == gotBodyHash {
continue // Shader already written and is identical, skip.
}
return n, fmt.Errorf("duplicate %T shader name %q", node, name)
return n, fmt.Errorf("duplicate %T shader name %q w/ body:\n%s", node, name, body)
} else {
p.names[nameHash] = bodyHash // Not found, add it.
}
Expand Down Expand Up @@ -448,7 +448,7 @@ func AppendVec3Decl(b []byte, name string, v ms3.Vec) []byte {
b = append(b, name...)
b = append(b, "=vec3("...)
arr := v.Array()
b = AppendFloats(b, arr[:], ',', '-', '.')
b = AppendFloats(b, ',', '-', '.', arr[:]...)
b = append(b, ')', ';', '\n')
return b
}
Expand All @@ -458,7 +458,7 @@ func AppendVec2Decl(b []byte, name string, v ms2.Vec) []byte {
b = append(b, name...)
b = append(b, "=vec2("...)
arr := v.Array()
b = AppendFloats(b, arr[:], ',', '-', '.')
b = AppendFloats(b, ',', '-', '.', arr[:]...)
b = append(b, ')', ';', '\n')
return b
}
Expand Down Expand Up @@ -509,7 +509,7 @@ func AppendFloat(b []byte, v float32, neg, decimal byte) []byte {
return b[:end]
}

func AppendFloats(b []byte, s []float32, sep, neg, decimal byte) []byte {
func AppendFloats(b []byte, sep, neg, decimal byte, s ...float32) []byte {
for i, v := range s {
b = AppendFloat(b, v, neg, decimal)
if sep != 0 && i != len(s)-1 {
Expand Down Expand Up @@ -714,12 +714,22 @@ func (nos2 *nameOverloadShader2D) Evaluate(pos []ms2.Vec, dist []float32, userDa
}

func hash(b []byte, in uint64) uint64 {
// Leaving md5 here since we may need to revert to
// a more entropic hash to avoid collisions...
// though I don't think it'll be necessary.
// var result [16]byte
// h := md5.New()
// h.Write(b)
// h.Sum(result[:0])
// x1 := binary.LittleEndian.Uint64(result[:])
// x2 := binary.LittleEndian.Uint64(result[8:])
// return x1 ^ x2 ^ in
x := in
for len(b) >= 8 {
x = x ^ binary.LittleEndian.Uint64(b)
x ^= binary.LittleEndian.Uint64(b)
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9
x = (x ^ (x >> 27)) * 0x94d049bb133111eb
x = x ^ (x >> 31)
x ^= x >> 31
b = b[8:]
}
for i := range b {
Expand Down
2 changes: 1 addition & 1 deletion gsdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func vecappend(b []byte, v ms3.Vec, sep, neg, decimal byte) []byte {
}

func sliceappend(b []byte, s []float32, sep, neg, decimal byte) []byte {
return glbuild.AppendFloats(b, s, sep, neg, decimal)
return glbuild.AppendFloats(b, sep, neg, decimal, s...)
}

func appendDistanceDecl(b []byte, s glbuild.Shader, name, input string) []byte {
Expand Down

0 comments on commit 42a82f0

Please sign in to comment.