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
/
Copy pathgenerate_examples.go
202 lines (187 loc) · 4.82 KB
/
generate_examples.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
package main
import (
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"text/template"
"time"
"github.com/fogleman/fauxgl"
"github.com/nfnt/resize"
"github.com/soypat/sdf/internal/d3"
"gonum.org/v1/gonum/spatial/r3"
)
const (
// Scale down images relative to Full HD resolution.
FHDscaler = 0.4
width, height = int(1920. * FHDscaler), int(1080. * FHDscaler) // output width and height in pixels
figFolder = "fig"
)
var examples = []struct {
Name string
Dir string
resultSTL string
view viewConfig
// Following values Set during execution
PNGResult string
STLSize string
ExecutionTime string
}{
// Add new examples here!
{
Name: "Metric spacers M3,M4,M6,M8,M16",
Dir: "metric-spacers",
resultSTL: "spacers.stl",
view: defaultView,
},
{
Name: "ADZ Nagano sensor cover",
Dir: "adz-sensor-cover",
resultSTL: "cover.stl",
view: defaultView,
},
{
Name: "NPT Flange",
Dir: "npt-flange",
resultSTL: "npt_flange.stl",
view: defaultView,
},
{
Name: "ATX Bench power supply mod",
Dir: "atx-bench-supply",
resultSTL: "atx_bench.stl",
view: defaultView,
},
{
Name: "PCB spacer",
Dir: "pcb-spacer",
resultSTL: "pcb_base.stl",
view: defaultView,
},
{
Name: "PCB support",
Dir: "pcb-support",
resultSTL: "support.stl",
view: defaultView,
},
}
func main() {
dir, _ := os.Getwd()
os.Mkdir(figFolder, 0777)
for i, example := range examples {
cmd := exec.Command("go", "run", ".")
cmd.Dir = filepath.Join(dir, example.Dir)
tstart := time.Now()
output, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("%s\nexample %s failed:%s", string(output), example.Name, err)
}
stlName := filepath.Join(cmd.Dir, example.resultSTL)
examples[i].ExecutionTime = fmt.Sprintf("%gs", time.Since(tstart).Round(time.Second/2).Seconds())
examples[i].STLSize = getHumanSize(stlName)
pngName := example.Dir + ".png"
examplePNG := filepath.Join(dir, figFolder, pngName)
_, err = os.Stat(examplePNG)
if os.IsNotExist(err) {
// if image has not yet been generated, generate it.
err = stlToPNG(stlName, examplePNG, example.view)
if err != nil {
log.Fatal(err)
}
} else if err != nil {
log.Fatal(err)
}
examples[i].PNGResult = filepath.Join(figFolder, pngName)
}
fp, err := os.Open("README.tmpl")
if err != nil {
log.Fatal(err)
}
b, err := io.ReadAll(fp)
if err != nil {
log.Fatal(err)
}
output, err := os.Create("README.md")
if err != nil {
log.Fatal(err)
}
template.Must(template.New("examples").Parse(string(b))).Execute(output, examples)
}
var defaultView = viewConfig{
up: r3.Vec{Z: 1},
eyepos: d3.Elem(2.4), // iso view.
near: 1,
far: 10,
}
func stlToPNG(stlName, outputname string, view viewConfig) error {
mesh, err := fauxgl.LoadSTL(stlName)
if err != nil {
return err
}
const (
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(uint(width), uint(height), image, resize.Bilinear)
return fauxgl.SavePNG(outputname, image)
}
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 getHumanSize(fileName string) (size string) {
const (
kB = 1000
MB = 1000 * kB
GB = 1000 * MB
)
info, err := os.Stat(fileName)
if err != nil {
log.Fatal(err)
}
bytes := info.Size()
switch {
case bytes < 10*kB:
size = fmt.Sprintf("%dB", bytes)
case bytes < 10*MB:
size = fmt.Sprintf("%dkB", bytes/kB)
case bytes < 10*GB:
size = fmt.Sprintf("%dMB", bytes/MB)
default:
size = fmt.Sprintf("%dGB", bytes/GB)
}
return size
}