This repository has been archived by the owner on Aug 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
form3_test.go
240 lines (224 loc) · 5.92 KB
/
form3_test.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
package render_test
import (
"io"
"os"
"testing"
"github.com/fogleman/fauxgl"
"github.com/nfnt/resize"
"github.com/soypat/sdf"
form2 "github.com/soypat/sdf/form2/must2"
form3 "github.com/soypat/sdf/form3/must3"
"github.com/soypat/sdf/form3/obj3/thread"
"github.com/soypat/sdf/internal/d3"
"github.com/soypat/sdf/render"
"gonum.org/v1/gonum/spatial/r3"
"gonum.org/v1/plot/cmpimg"
)
const (
// imgDelta a normalized imgDelta parameter to describe how close the matching
// should be performed (imgDelta=0: perfect match, imgDelta=1, loose match)
imgDelta = 0
quality = 200
)
type viewConfig struct {
// what position (point) to look at
lookat r3.Vec
// which way is up (direction)
up r3.Vec
// where the camera/eye located at (point)
eyepos r3.Vec
far float64
near float64
}
func BenchmarkCylinder(b *testing.B) {
for i := 0; i < b.N; i++ {
cylinderToSTL(b, "cyl_bench.stl")
}
}
func TestForm3Gen(t *testing.T) {
var defaultView = viewConfig{
up: r3.Vec{Z: 1},
eyepos: d3.Elem(3),
near: 1,
far: 10,
}
for _, test := range []struct {
name string
defacto string
view viewConfig
formFunc func(t testing.TB, stlpath string)
}{
{
name: "knurl ",
defacto: "testdata/defactoKnurl.png",
formFunc: knurlToSTL,
view: defaultView,
},
{
name: "bolt",
defacto: "testdata/defactoBolt.png",
formFunc: boltToSTL,
view: defaultView,
},
{
name: "hex",
defacto: "testdata/defactoHex.png",
formFunc: hexToSTL,
view: defaultView,
},
{
name: "cylinder",
defacto: "testdata/defactoCylinder.png",
formFunc: cylinderToSTL,
view: defaultView,
},
{
name: "box",
defacto: "testdata/defactoBox.png",
formFunc: boxToSTL,
view: defaultView,
},
{
name: "sphere",
defacto: "testdata/defactoSphere.png",
formFunc: sphereToSTL,
view: defaultView,
},
} {
stlPath := "test_" + test.name + ".stl"
gotPng := "test_" + test.name + ".png"
test.formFunc(t, stlPath)
stlToPNG(t, stlPath, gotPng, test.view)
if !equalImages(t, gotPng, test.defacto) {
t.Errorf("%s rendered image does not match expected image", test.name)
t.Fatal("ending run here. remove this line after bugs eliminated")
}
if !t.Failed() {
// If test has not failed we remove the generated STL and PNG files.
os.Remove(stlPath)
os.Remove(gotPng)
}
}
}
func cylinderToSTL(t testing.TB, filename string) {
object := form3.Cylinder(10, 4, 1)
err := render.CreateSTL(filename, render.NewOctreeRenderer(object, quality))
if err != nil {
t.Fatal(err)
}
}
func boxToSTL(t testing.TB, filename string) {
object := form3.Box(r3.Vec{X: 1, Y: 2, Z: 1}, .3)
err := render.CreateSTL(filename, render.NewOctreeRenderer(object, quality))
if err != nil {
t.Fatal(err)
}
}
func hexToSTL(t testing.TB, filename string) {
object := sdf.Extrude3D(form2.Polygon(form2.Nagon(6, 1)), 1)
err := render.CreateSTL(filename, render.NewOctreeRenderer(object, quality))
if err != nil {
t.Fatal(err)
}
}
func knurlToSTL(t testing.TB, filename string) {
object, _ := thread.KnurledHead(.5, 1, .1)
err := render.CreateSTL(filename, render.NewOctreeRenderer(object, quality))
if err != nil {
t.Fatal(err)
}
}
func boltToSTL(t testing.TB, filename string) {
object, err := thread.Bolt(thread.BoltParms{
Thread: thread.ISO{D: 16, P: 2}, // M16x2
Style: thread.NutHex,
Tolerance: 0.1,
TotalLength: 60.0,
ShankLength: 10.0,
})
if err != nil {
t.Fatal(err)
}
// object, _ = obj3.Bolt(obj3.BoltParms{
// Thread: "M16x2",
// Style: obj3.CylinderHex,
// Tolerance: 0.1,
// TotalLength: 60.0,
// ShankLength: 10.0,
// })
err = render.CreateSTL(filename, render.NewOctreeRenderer(object, quality))
if err != nil {
t.Fatal(err)
}
}
func sphereToSTL(t testing.TB, filename string) {
object := form3.Sphere(1)
err := render.CreateSTL(filename, render.NewOctreeRenderer(object, quality))
if err != nil {
t.Fatal(err)
}
}
func stlToPNG(t testing.TB, stlName, outputname string, view viewConfig) {
mesh, err := fauxgl.LoadSTL(stlName)
if err != nil {
t.Fatal(err)
}
const (
width, height = 1920, 1080 // output width and height in pixels
scale = 1 // optional supersampling
fovy = 30 // vertical field of view in degrees
)
var (
far = view.far
near = view.near
eye = fauxgl.V(view.eyepos.X, view.eyepos.Y, view.eyepos.Z) // camera position
center = fauxgl.V(view.lookat.X, view.lookat.Y, view.lookat.Z) // view center position
up = fauxgl.V(view.up.X, view.up.Y, view.up.Z) // up vector
light = fauxgl.V(-0.75, 1, 0.25).Normalize() // light direction
color = fauxgl.HexColor("#468966") // object color
)
// fit mesh in a bi-unit cube centered at the origin
mesh.BiUnitCube()
// create a rendering context
context := fauxgl.NewContext(width*scale, height*scale)
context.ClearColorBufferWith(fauxgl.HexColor("#FFF8E3"))
// create transformation matrix and light direction
aspect := float64(width) / float64(height)
matrix := fauxgl.LookAt(eye, center, up).Perspective(fovy, aspect, near, far)
// use builtin phong shader
shader := fauxgl.NewPhongShader(matrix, light, eye)
shader.ObjectColor = color
context.Shader = shader
// render
context.DrawMesh(mesh)
// downsample image for antialiasing
image := context.Image()
image = resize.Resize(width, height, image, resize.Bilinear)
err = fauxgl.SavePNG(outputname, image)
if err != nil {
t.Fatal(err)
}
}
func equalImages(t *testing.T, png1, png2 string) bool {
fp1, err := os.Open(png1)
if err != nil {
t.Fatal(err)
}
fp2, err := os.Open(png2)
if err != nil {
t.Fatal(err)
}
b1, err := io.ReadAll(fp1)
if err != nil {
t.Fatal(err)
}
b2, err := io.ReadAll(fp2)
if err != nil {
t.Fatal(err)
}
equal, err := cmpimg.EqualApprox("png", b1, b2, imgDelta)
if err != nil {
t.Fatal(err)
}
return equal
}