generated from soypat/go-module-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gsdf.go
194 lines (166 loc) · 5 KB
/
gsdf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package gsdf
import (
_ "embed"
"errors"
"fmt"
"unsafe"
"github.com/chewxy/math32"
"github.com/soypat/glgl/math/ms2"
"github.com/soypat/glgl/math/ms3"
"github.com/soypat/gsdf/glbuild"
)
const (
// For an equilateral triangle of side length L the length of bisector is L multiplied this number which is sqrt(1-0.25).
tribisect = 0.8660254037844386467637231707529361834714026269051903140279034897
sqrt2d2 = math32.Sqrt2 / 2
sqrt3 = 1.7320508075688772935274463415058723669428052538103806280558069794
largenum = 1e20
// epstol is used to check for badly conditioned denominators
// such as lengths used for normalization or transformation matrix determinants.
epstol = 6e-7
)
// Flags is a bitmask of values to control the functioning of the [Builder] type.
type Flags uint64
const (
// FlagNoDimensionPanic controls panicking behavior on invalid shape dimension errors.
// If set then these errors do not panic, instead storing the error for later inspection with [Builder.Err].
FlagNoDimensionPanic Flags = 1 << iota
// FlagUseShaderBuffers enforces the use of shader object for all newly built
// SDFs which require a dynamic array(s) to be rendered correctly.
FlagUseShaderBuffers
)
// Builder wraps all SDF primitive and operation logic generation.
// Provides error handling strategies with panics or error accumulation during shape generation.
type Builder struct {
// flags is a bitfield controlling behaviour of Builder.
flags Flags
accumErrs []error
// limVecGPU
limVecGPU int
}
func (bld *Builder) useGPU(_ int) bool {
return bld.flags&FlagUseShaderBuffers != 0 // bld.limVecGPU != 0 && n > bld.limVecGPU || n > 1
}
func makeHashName[T any](dst []byte, name string, vec []T) []byte {
var z T
data := unsafe.Pointer(&vec[0])
sz := len(vec) * int(unsafe.Sizeof(z))
return fmt.Appendf(dst, "%s%x_%x", name, uintptr(data), sz)
}
func (bld *Builder) Flags() Flags {
return bld.flags
}
func (bld *Builder) SetFlags(flags Flags) error {
bld.flags = flags
return nil
}
// Err returns errors accumulated during SDF primitive creation and operations. The returned error implements `Unwrap() []error`.
func (bld *Builder) Err() error {
if len(bld.accumErrs) == 0 {
return nil
}
return errors.Join(bld.accumErrs...)
}
// ClearErrors clears accumulated errors such that [Builder.Err] returns nil on next call.
func (bld *Builder) ClearErrors() {
bld.accumErrs = bld.accumErrs[:0]
}
func (bld *Builder) shapeErrorf(msg string, args ...any) {
if bld.flags&FlagNoDimensionPanic == 0 {
panic(fmt.Sprintf(msg, args...))
}
// bld.stacks = append(bld.stacks, string(debug.Stack()))
bld.accumErrs = append(bld.accumErrs, fmt.Errorf(msg, args...))
}
func (*Builder) nilsdf(msg string) {
panic("nil SDF argument: " + msg)
}
// These interfaces are implemented by all SDF interfaces such as SDF3/2 and Shader3D/2D.
// Using these instead of `any` Aids in catching mistakes at compile time such as passing a Shader3D instead of Shader2D as an argument.
type (
bounder2 = interface{ Bounds() ms2.Box }
bounder3 = interface{ Bounds() ms3.Box }
)
func minf(a, b float32) float32 {
return math32.Min(a, b)
}
func hypotf(a, b float32) float32 {
return math32.Hypot(a, b)
}
func signf(a float32) float32 {
if a == 0 {
return 0
}
return math32.Copysign(1, a)
}
func clampf(v, Min, Max float32) float32 {
// return ms3.Clamp(v, Min, Max)
if v < Min {
return Min
} else if v > Max {
return Max
}
return v
}
func mixf(x, y, a float32) float32 {
return x*(1-a) + y*a
}
func maxf(a, b float32) float32 {
return math32.Max(a, b)
}
func absf(a float32) float32 {
return math32.Abs(a)
}
func hash2vec2(vecs ...[2]ms2.Vec) float32 {
var hashA float32 = 0.0
var hashB float32 = 1.0
for _, v := range vecs {
hashA, hashB = hashAdd(hashA, hashB, v[0].X)
hashA, hashB = hashAdd(hashA, hashB, v[0].Y)
hashA, hashB = hashAdd(hashA, hashB, v[1].X)
hashA, hashB = hashAdd(hashA, hashB, v[1].Y)
}
return hashfint(hashA + hashB)
}
func hashf(values []float32) float32 {
var hashA float32 = 0.0
var hashB float32 = 1.0
for _, num := range values {
hashA, hashB = hashAdd(hashA, hashB, num)
}
return hashfint(hashA + hashB)
}
func hashAdd(a, b, num float32) (aNew, bNew float32) {
const prime = 31.0
a += num
b *= (prime + num)
a = hashfint(a)
b = hashfint(b)
return a, b
}
func hashfint(f float32) float32 {
return float32(int(f*1000000)%1000000) / 1000000 // Keep within [0.0, 1.0)
}
// func hash(b []byte, in uint64) uint64 {
// x := in
// for len(b) >= 8 {
// x ^= binary.LittleEndian.Uint64(b)
// x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9
// x = (x ^ (x >> 27)) * 0x94d049bb133111eb
// x ^= x >> 31
// b = b[8:]
// }
// if len(b) > 0 {
// var buf [8]byte
// copy(buf[:], b)
// x ^= binary.LittleEndian.Uint64(buf[:])
// x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9
// x = (x ^ (x >> 27)) * 0x94d049bb133111eb
// x ^= x >> 31
// }
// return x
// }
func hashshaderptr(s glbuild.Shader) uint64 {
v := *(*[2]uintptr)(unsafe.Pointer(&s))
return (uint64(v[0]) ^ (uint64(v[1]) << 8)) * 0xbf58476d1ce4e5b9
}