Skip to content

Commit 42a82f0

Browse files
committed
refactor glbuild.AppendFloats API
1 parent 4d45dd6 commit 42a82f0

File tree

4 files changed

+27
-15
lines changed

4 files changed

+27
-15
lines changed

examples/npt-flange/flange.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ func scene() (gleval.SDF3, error) {
9292

9393
pipe, _ := threads.Nut(threads.NutParms{
9494
Thread: npt,
95-
Style: threads.NutCircular,
95+
Style: threads.NutKnurl,
9696
})
97+
return makeSDF(pipe)
9798
// Base plate which goes bolted to joint.
9899
flange, _ = gsdf.NewCylinder(flangeD/2, flangeH, flangeH/8)
99100
// Make through-hole in flange bottom.

forge/threads/threads.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ func Screw(length float32, thread Threader) (glbuild.Shader3D, error) {
8484
return nil, err
8585
}
8686
params := thread.ThreadParams()
87-
s := screw{}
88-
s.thread = tsdf
89-
s.pitch = params.Pitch
90-
s.length = length / 2
91-
s.length *= 1 + 1e-3
92-
s.taper = params.Taper
93-
s.lead = -s.pitch * float32(params.Starts)
87+
s := screw{
88+
thread: tsdf,
89+
pitch: params.Pitch,
90+
lead: -params.Pitch * float32(params.Starts),
91+
length: length / 2,
92+
taper: params.Taper,
93+
}
9494
return &s, nil
9595
}
9696

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

105105
func (s *screw) AppendShaderName(b []byte) []byte {
106106
b = append(b, "screw_"...)
107+
b = glbuild.AppendFloats(b, 0, 'n', 'd', s.pitch, s.lead, s.length, s.taper)
107108
b = s.thread.AppendShaderName(b)
108109
return b
109110
}

glbuild/glbuild.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func (p *Programmer) writeShaders(w io.Writer, nodes []Shader) (n int, err error
195195
if bodyHash == gotBodyHash {
196196
continue // Shader already written and is identical, skip.
197197
}
198-
return n, fmt.Errorf("duplicate %T shader name %q", node, name)
198+
return n, fmt.Errorf("duplicate %T shader name %q w/ body:\n%s", node, name, body)
199199
} else {
200200
p.names[nameHash] = bodyHash // Not found, add it.
201201
}
@@ -448,7 +448,7 @@ func AppendVec3Decl(b []byte, name string, v ms3.Vec) []byte {
448448
b = append(b, name...)
449449
b = append(b, "=vec3("...)
450450
arr := v.Array()
451-
b = AppendFloats(b, arr[:], ',', '-', '.')
451+
b = AppendFloats(b, ',', '-', '.', arr[:]...)
452452
b = append(b, ')', ';', '\n')
453453
return b
454454
}
@@ -458,7 +458,7 @@ func AppendVec2Decl(b []byte, name string, v ms2.Vec) []byte {
458458
b = append(b, name...)
459459
b = append(b, "=vec2("...)
460460
arr := v.Array()
461-
b = AppendFloats(b, arr[:], ',', '-', '.')
461+
b = AppendFloats(b, ',', '-', '.', arr[:]...)
462462
b = append(b, ')', ';', '\n')
463463
return b
464464
}
@@ -509,7 +509,7 @@ func AppendFloat(b []byte, v float32, neg, decimal byte) []byte {
509509
return b[:end]
510510
}
511511

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

716716
func hash(b []byte, in uint64) uint64 {
717+
// Leaving md5 here since we may need to revert to
718+
// a more entropic hash to avoid collisions...
719+
// though I don't think it'll be necessary.
720+
// var result [16]byte
721+
// h := md5.New()
722+
// h.Write(b)
723+
// h.Sum(result[:0])
724+
// x1 := binary.LittleEndian.Uint64(result[:])
725+
// x2 := binary.LittleEndian.Uint64(result[8:])
726+
// return x1 ^ x2 ^ in
717727
x := in
718728
for len(b) >= 8 {
719-
x = x ^ binary.LittleEndian.Uint64(b)
729+
x ^= binary.LittleEndian.Uint64(b)
720730
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9
721731
x = (x ^ (x >> 27)) * 0x94d049bb133111eb
722-
x = x ^ (x >> 31)
732+
x ^= x >> 31
723733
b = b[8:]
724734
}
725735
for i := range b {

gsdf.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func vecappend(b []byte, v ms3.Vec, sep, neg, decimal byte) []byte {
8282
}
8383

8484
func sliceappend(b []byte, s []float32, sep, neg, decimal byte) []byte {
85-
return glbuild.AppendFloats(b, s, sep, neg, decimal)
85+
return glbuild.AppendFloats(b, sep, neg, decimal, s...)
8686
}
8787

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

0 commit comments

Comments
 (0)