/
silhouette.go
191 lines (167 loc) · 4.26 KB
/
silhouette.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
package main
import (
"fmt"
"math"
"os"
. "github.com/fogleman/fauxgl"
"github.com/nfnt/resize"
)
const (
scale = 2
width = 1600
height = 1600
fovy = 100
near = 0.01
far = 10
)
var (
eye = V(0, -1.1, 0)
center = V(0, 0, 0)
up = V(0, 0, 1)
)
func timed(name string) func() {
if len(name) > 0 {
// fmt.Printf("%s... ", name)
}
// start := time.Now()
return func() {
// fmt.Println(time.Since(start))
}
}
func fineTriangle(buf []*Triangle, t *Triangle, threshold float64) []*Triangle {
v1 := t.V1
v2 := t.V2
v3 := t.V3
p1 := v1.Position
p2 := v2.Position
p3 := v3.Position
d12 := p1.Distance(p2)
d23 := p2.Distance(p3)
d31 := p3.Distance(p1)
max := math.Max(d12, math.Max(d23, d31))
if max <= threshold {
buf = append(buf, t)
return buf
}
if d12 == max {
v := InterpolateVertexes(v1, v2, v3, VectorW{0.5, 0.5, 0, 1})
t1 := NewTriangle(v3, v1, v)
t2 := NewTriangle(v2, v3, v)
buf = fineTriangle(buf, t1, threshold)
buf = fineTriangle(buf, t2, threshold)
} else if d23 == max {
v := InterpolateVertexes(v1, v2, v3, VectorW{0, 0.5, 0.5, 1})
t1 := NewTriangle(v1, v2, v)
t2 := NewTriangle(v3, v1, v)
buf = fineTriangle(buf, t1, threshold)
buf = fineTriangle(buf, t2, threshold)
} else {
v := InterpolateVertexes(v1, v2, v3, VectorW{0.5, 0, 0.5, 1})
t1 := NewTriangle(v2, v3, v)
t2 := NewTriangle(v1, v2, v)
buf = fineTriangle(buf, t1, threshold)
buf = fineTriangle(buf, t2, threshold)
}
return buf
}
func fineMesh(mesh *Mesh, threshold float64) *Mesh {
var triangles []*Triangle
for _, t := range mesh.Triangles {
triangles = fineTriangle(triangles, t, threshold)
}
return NewTriangleMesh(triangles)
}
type Edge struct {
A, B Vector
}
func MakeEdge(a, b Vector) Edge {
if a.Less(b) {
return Edge{a, b}
}
return Edge{b, a}
}
func sharpEdges(mesh *Mesh) *Mesh {
var lines []*Line
other := make(map[Edge]*Triangle)
for _, t := range mesh.Triangles {
p1 := t.V1.Position //.RoundPlaces(6)
p2 := t.V2.Position //.RoundPlaces(6)
p3 := t.V3.Position //.RoundPlaces(6)
e1 := MakeEdge(p1, p2)
e2 := MakeEdge(p2, p3)
e3 := MakeEdge(p3, p1)
for _, e := range []Edge{e1, e2, e3} {
if u, ok := other[e]; ok {
a := math.Acos(t.Normal().Dot(u.Normal()))
if a > Radians(60) {
lines = append(lines, NewLineForPoints(e.A, e.B))
}
}
}
other[e1] = t
other[e2] = t
other[e3] = t
}
return NewLineMesh(lines)
}
func main() {
var done func()
// load a mesh
done = timed("loading mesh")
mesh, err := LoadMesh(os.Args[1])
if err != nil {
panic(err)
}
done()
// fit mesh in a bi-unit cube centered at the origin
done = timed("transforming mesh")
mesh.BiUnitCube()
done()
// fmt.Println(len(mesh.Triangles))
mesh = fineMesh(mesh, 0.01/3)
// fmt.Println(len(mesh.Triangles))
// create a rendering context
context := NewContext(width*scale, height*scale)
// create transformation matrix and light direction
aspect := float64(width) / float64(height)
matrix := LookAt(eye, center, up).Perspective(fovy, aspect, near, far)
// const s = 1.1
// matrix := LookAt(eye, center, up).Orthographic(-aspect*s, aspect*s, -s, s, near, far)
// render
context.Shader = NewSolidColorShader(matrix, Black)
done = timed("rendering mesh")
context.DrawMesh(mesh)
done()
context.ClearColorBufferWith(White)
context.DepthBias = -1e-5
done = timed("rendering mesh")
// context.Shader = NewSolidColorShader(matrix, Color{1, 0, 0, 1})
// context.DrawMesh(sharpEdges(mesh))
context.Shader = NewSolidColorShader(matrix, Black)
// context.DrawMesh(mesh.Silhouette(eye, 1e-3))
for _, line := range mesh.Silhouette(eye, 1e-3).Lines {
info := context.DrawLine(line)
ratio := float64(info.UpdatedPixels) / float64(info.TotalPixels)
if ratio < 0.666 {
continue
}
v1 := matrix.MulPositionW(line.V1.Position)
v1 = v1.DivScalar(v1.W)
v2 := matrix.MulPositionW(line.V2.Position)
v2 = v2.DivScalar(v2.W)
if math.IsNaN(v1.X) || math.IsNaN(v2.X) {
continue
}
fmt.Printf("%g,%g %g,%g\n", v1.X*aspect, v1.Y, v2.X*aspect, v2.Y)
}
done()
// downsample image for antialiasing
done = timed("downsampling image")
image := context.Image()
image = resize.Resize(width, height, image, resize.Bilinear)
done()
// save image
done = timed("writing output")
SavePNG("out-fine.png", image)
done()
}