generated from soypat/go-module-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
primitives.go
341 lines (293 loc) · 9.73 KB
/
primitives.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
package gsdf
import (
"errors"
"github.com/chewxy/math32"
"github.com/soypat/glgl/math/ms3"
"github.com/soypat/gsdf/glbuild"
)
// NewBoundsBoxFrame creates a BoxFrame from a bb ([ms3.Box]) such that the BoxFrame envelops the bb.
// Useful for debugging bounding boxes of [glbuild.Shader3D] primitives and operations.
func NewBoundsBoxFrame(bb ms3.Box) (glbuild.Shader3D, error) {
size := bb.Size()
frameThickness := size.Max() / 256
// Bounding box's frames protrude.
size = ms3.AddScalar(2*frameThickness, size)
bounding, err := NewBoxFrame(size.X, size.Y, size.Z, frameThickness)
if err != nil {
return nil, err
}
center := bb.Center()
bounding = Translate(bounding, center.X, center.Y, center.Z)
return bounding, nil
}
type sphere struct {
r float32
}
// NewSphere creates a sphere centered at the origin of radius r.
func NewSphere(r float32) (glbuild.Shader3D, error) {
valid := r > 0
if !valid {
return nil, errors.New("zero or negative sphere radius")
}
return &sphere{r: r}, nil
}
func (s *sphere) ForEachChild(userData any, fn func(userData any, s *glbuild.Shader3D) error) error {
return nil
}
func (s *sphere) AppendShaderName(b []byte) []byte {
b = append(b, "sphere"...)
b = glbuild.AppendFloat(b, 'n', 'p', s.r)
return b
}
func (s *sphere) AppendShaderBody(b []byte) []byte {
b = append(b, "return length(p)-"...)
b = glbuild.AppendFloat(b, '-', '.', s.r)
b = append(b, ';')
return b
}
func (u *sphere) AppendShaderObjects(objects []glbuild.ShaderObject) []glbuild.ShaderObject {
return objects
}
func (s *sphere) Bounds() ms3.Box {
return ms3.Box{
Min: ms3.Vec{X: -s.r, Y: -s.r, Z: -s.r},
Max: ms3.Vec{X: s.r, Y: s.r, Z: s.r},
}
}
// NewBox creates a box centered at the origin with x,y,z dimensions and a rounding parameter to round edges.
func NewBox(x, y, z, round float32) (glbuild.Shader3D, error) {
if round < 0 || round > x/2 || round > y/2 || round > z/2 {
return nil, errors.New("invalid box rounding value")
} else if x <= 0 || y <= 0 || z <= 0 {
return nil, errors.New("zero or negative box dimension")
}
return &box{dims: ms3.Vec{X: x, Y: y, Z: z}, round: round}, nil
}
type box struct {
dims ms3.Vec
round float32
}
func (s *box) ForEachChild(userData any, fn func(userData any, s *glbuild.Shader3D) error) error {
return nil
}
func (s *box) AppendShaderName(b []byte) []byte {
b = append(b, "box"...)
arr := s.dims.Array()
b = glbuild.AppendFloats(b, 0, 'n', 'p', arr[:]...)
b = glbuild.AppendFloat(b, 'n', 'p', s.round)
return b
}
func (s *box) AppendShaderBody(b []byte) []byte {
b = glbuild.AppendFloatDecl(b, "r", s.round)
b = glbuild.AppendVec3Decl(b, "d", ms3.Scale(0.5, s.dims)) // Inigo's SDF is x2 size.
b = append(b, `vec3 q = abs(p)-d+r;
return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0)-r;`...)
return b
}
func (u *box) AppendShaderObjects(objects []glbuild.ShaderObject) []glbuild.ShaderObject {
return objects
}
func (s *box) Bounds() ms3.Box {
return ms3.NewCenteredBox(ms3.Vec{}, s.dims)
}
// NewCylinder creates a cylinder centered at the origin with given radius and height.
// The cylinder's axis points in z direction.
func NewCylinder(r, h, rounding float32) (glbuild.Shader3D, error) {
okRounding := rounding >= 0 && rounding < r && rounding < h/2
if !okRounding {
return nil, errors.New("invalid cylinder rounding")
}
if r > 0 && h > 0 {
return &cylinder{r: r, h: h, round: rounding}, nil
}
return nil, errors.New("bad cylinder dimension")
}
type cylinder struct {
r float32
h float32
round float32
}
func (s *cylinder) Bounds() ms3.Box {
return ms3.Box{
Min: ms3.Vec{X: -s.r, Y: -s.r, Z: -s.h / 2},
Max: ms3.Vec{X: s.r, Y: s.r, Z: s.h / 2},
}
}
func (s *cylinder) ForEachChild(userData any, fn func(userData any, s *glbuild.Shader3D) error) error {
return nil
}
func (s *cylinder) AppendShaderName(b []byte) []byte {
b = append(b, "cyl"...)
b = glbuild.AppendFloats(b, 0, 'n', 'p', s.r, s.h, s.round)
return b
}
func (s *cylinder) AppendShaderBody(b []byte) []byte {
r, h, round := s.args()
b = append(b, "p = p.xzy;\n"...)
b = glbuild.AppendFloatDecl(b, "r", r)
b = glbuild.AppendFloatDecl(b, "h", h) // Correct height for rounding effect.
if s.round == 0 {
b = append(b, `vec2 d = abs(vec2(length(p.xz),p.y)) - vec2(r,h);
return min(max(d.x,d.y),0.0) + length(max(d,0.0));`...)
} else {
b = glbuild.AppendFloatDecl(b, "rd", round)
b = append(b, `vec2 d = vec2( length(p.xz)-r+rd, abs(p.y) - h );
return min(max(d.x,d.y),0.0) + length(max(d,0.0)) - rd;`...)
}
return b
}
func (c *cylinder) args() (r, h, round float32) {
return c.r, (c.h - 2*c.round) / 2, c.round
}
func (u *cylinder) AppendShaderObjects(objects []glbuild.ShaderObject) []glbuild.ShaderObject {
return objects
}
// NewHexagonalPrism creates a hexagonal prism given a face-to-face dimension and height.
// The hexagon's length is in the z axis.
func NewHexagonalPrism(face2Face, h float32) (glbuild.Shader3D, error) {
if face2Face <= 0 || h <= 0 {
return nil, errors.New("invalid hexagonal prism parameter")
}
return &hex{side: face2Face, h: h}, nil
}
type hex struct {
side float32
h float32
}
func (s *hex) Bounds() ms3.Box {
l := s.side
lx := l / tribisect
return ms3.Box{
Min: ms3.Vec{X: -lx, Y: -l, Z: -s.h},
Max: ms3.Vec{X: lx, Y: l, Z: s.h},
}
}
func (s *hex) ForEachChild(userData any, fn func(userData any, s *glbuild.Shader3D) error) error {
return nil
}
func (s *hex) AppendShaderName(b []byte) []byte {
b = append(b, "hex"...)
b = glbuild.AppendFloats(b, 0, 'n', 'p', s.side, s.h)
return b
}
func (s *hex) AppendShaderBody(b []byte) []byte {
b = glbuild.AppendFloatDecl(b, "_h", s.h)
b = glbuild.AppendFloatDecl(b, "side", s.side)
b = append(b, `vec2 h = vec2(side, _h);
const vec3 k = vec3(-0.8660254038, 0.5, 0.57735);
p = abs(p);
p.xy -= 2.0*min(dot(k.xy, p.xy), 0.0)*k.xy;
vec2 aux = p.xy-vec2(clamp(p.x,-k.z*h.x,k.z*h.x), h.x);
vec2 d = vec2( length(aux)*sign(p.y-h.x), p.z-h.y );
return min(max(d.x,d.y),0.0) + length(max(d,0.0));`...)
return b
}
func (u *hex) AppendShaderObjects(objects []glbuild.ShaderObject) []glbuild.ShaderObject {
return objects
}
// NewTriangularPrism creates a 3D triangular prism with a given triangle cross-sectional height (2D)
// and a extrude length. The prism's extrude axis is in the z axis direction.
func NewTriangularPrism(triHeight, extrudeLength float32) (glbuild.Shader3D, error) {
if extrudeLength > 0 && !math32.IsInf(extrudeLength, 1) {
tri, err := NewEquilateralTriangle(triHeight)
if err != nil {
return nil, err
}
return Extrude(tri, extrudeLength)
}
return nil, errors.New("bad triangular prism extrude length")
}
type torus struct {
rLesser, rGreater float32
}
// NewTorus creates a 3D torus given 2 radii to define the radius
// across (greaterRadius) and the "solid" radius (lesserRadius).
// If the radius were cut and stretched straight to form a cylinder the lesser
// radius would be the radius of the cylinder.
// The torus' axis is in the z axis.
func NewTorus(greaterRadius, lesserRadius float32) (glbuild.Shader3D, error) {
if greaterRadius < 2*lesserRadius {
return nil, errors.New("too large torus lesser radius")
} else if greaterRadius <= 0 || lesserRadius <= 0 {
return nil, errors.New("invalid torus parameter")
}
return &torus{rLesser: lesserRadius, rGreater: greaterRadius}, nil
}
func (s *torus) ForEachChild(userData any, fn func(userData any, s *glbuild.Shader3D) error) error {
return nil
}
func (s *torus) AppendShaderName(b []byte) []byte {
b = append(b, "torus"...)
b = glbuild.AppendFloat(b, 'n', 'p', s.rLesser)
b = glbuild.AppendFloat(b, 'n', 'p', s.rGreater)
return b
}
func (s *torus) AppendShaderBody(b []byte) []byte {
b = glbuild.AppendFloatDecl(b, "t1", s.rGreater) // Counteract rounding effect.
b = glbuild.AppendFloatDecl(b, "t2", s.rLesser)
b = append(b, `p = p.xzy;
vec2 t = vec2(t1, t2);
vec2 q = vec2(length(p.xz)-t.x,p.y);
return length(q)-t.y;`...)
return b
}
func (s *torus) Bounds() ms3.Box {
R := s.rLesser + s.rGreater
return ms3.Box{
Min: ms3.Vec{X: -R, Y: -R, Z: -s.rLesser},
Max: ms3.Vec{X: R, Y: R, Z: s.rLesser},
}
}
func (u *torus) AppendShaderObjects(objects []glbuild.ShaderObject) []glbuild.ShaderObject {
return objects
}
// NewBoxFrame creates a framed box with the frame being composed of square beams of thickness e.
func NewBoxFrame(dimX, dimY, dimZ, e float32) (glbuild.Shader3D, error) {
e /= 2
if dimX <= 0 || dimY <= 0 || dimZ <= 0 || e <= 0 {
return nil, errors.New("negative or zero BoxFrame dimension")
}
d := ms3.Vec{X: dimX, Y: dimY, Z: dimZ}
if 2*e > d.Min() {
return nil, errors.New("BoxFrame edge thickness too large")
}
return &boxframe{dims: d, e: e}, nil
}
type boxframe struct {
dims ms3.Vec
e float32
}
func (bf *boxframe) ForEachChild(userData any, fn func(userData any, s *glbuild.Shader3D) error) error {
return nil
}
func (bf *boxframe) AppendShaderName(b []byte) []byte {
b = append(b, "boxframe"...)
arr := bf.dims.Array()
b = glbuild.AppendFloats(b, 0, 'n', 'p', arr[:]...)
b = glbuild.AppendFloat(b, 'n', 'p', bf.e)
return b
}
func (bf *boxframe) AppendShaderBody(b []byte) []byte {
e, bb := bf.args()
b = glbuild.AppendFloatDecl(b, "e", e)
b = glbuild.AppendVec3Decl(b, "b", bb)
b = append(b, `p = abs(p)-b;
vec3 q = abs(p+e)-e;
return min(min(
length(max(vec3(p.x,q.y,q.z),0.0))+min(max(p.x,max(q.y,q.z)),0.0),
length(max(vec3(q.x,p.y,q.z),0.0))+min(max(q.x,max(p.y,q.z)),0.0)),
length(max(vec3(q.x,q.y,p.z),0.0))+min(max(q.x,max(q.y,p.z)),0.0));`...)
return b
}
func (bf *boxframe) Bounds() ms3.Box {
return ms3.NewCenteredBox(ms3.Vec{}, bf.dims)
}
func (bf *boxframe) args() (e float32, b ms3.Vec) {
dd, e := bf.dims, bf.e
dd = ms3.Scale(0.5, dd)
dd = ms3.AddScalar(-2*e, dd)
return e, dd
}
func (u *boxframe) AppendShaderObjects(objects []glbuild.ShaderObject) []glbuild.ShaderObject {
return objects
}