-
Notifications
You must be signed in to change notification settings - Fork 12
/
attachments.go
117 lines (107 loc) · 3.14 KB
/
attachments.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
package main
import (
"math"
"strings"
"github.com/unixpickle/model3d/toolbox3d"
"github.com/unixpickle/model3d/model2d"
"github.com/unixpickle/model3d/model3d"
)
const (
ImageAttachmentCornerRounding = 0.02
ImageAttachmentThickness = 0.08
ImageAttachmentEngravingDepth = 0.01
)
func Attachment(name string) model3d.Solid {
if name == "sphere" {
return sphereAttachment()
} else if name == "heart" {
return heartAttachment()
} else {
mesh, engraving := readAttachmentMeshes(name)
collider2d := model2d.MeshToCollider(mesh)
solid2d := model2d.NewColliderSolid(collider2d)
th := ImageAttachmentThickness/2 - ImageAttachmentCornerRounding
solid3d := model3d.ProfileSolid(solid2d, -th, th)
collider3d := model3d.ProfileCollider(collider2d, -th, th)
baseSolid := model3d.JoinedSolid{
solid3d,
model3d.NewColliderSolidHollow(collider3d, ImageAttachmentCornerRounding),
}
if engraving != nil {
engraving2d := model2d.NewColliderSolid(model2d.MeshToCollider(engraving))
engraving3d := model3d.ProfileSolid(
engraving2d,
ImageAttachmentThickness/2-ImageAttachmentEngravingDepth,
ImageAttachmentThickness/2+1e-5,
)
return &model3d.SubtractedSolid{Positive: baseSolid, Negative: engraving3d}
}
return baseSolid
}
}
func readAttachmentMeshes(files string) (mesh *model2d.Mesh, engraving *model2d.Mesh) {
baseImage, engravingPath := splitBaseAndEngraving(files)
mesh = model2d.MustReadBitmap(baseImage, nil).Mesh().SmoothSq(50)
if engravingPath != "" {
engraving = model2d.MustReadBitmap(engravingPath, nil).Mesh().SmoothSq(50)
}
size := mesh.Max().Sub(mesh.Min())
scale := 0.5 / math.Max(size.X, size.Y)
mesh = mesh.Scale(scale)
if engraving != nil {
engraving = engraving.Scale(scale)
}
mesh = mesh.MapCoords(flipXY)
if engraving != nil {
engraving = engraving.MapCoords(flipXY)
}
center := mesh.Min().Mid(mesh.Max()).Scale(-1)
mesh = mesh.Translate(center)
if engraving != nil {
engraving = engraving.Translate(center)
}
return
}
func flipXY(c model2d.Coord) model2d.Coord {
return model2d.XY(c.Y, c.X)
}
func splitBaseAndEngraving(files string) (string, string) {
if strings.Contains(files, ":") {
parts := strings.Split(files, ":")
if len(parts) != 2 {
panic("expected exactly 0 or 1 ':' character")
}
return parts[0], parts[1]
}
return files, ""
}
func sphereAttachment() model3d.Solid {
return &model3d.Sphere{Radius: 0.15}
}
func heartAttachment() model3d.Solid {
shape := model2d.BezierCurve{
model2d.XY(0, 0),
model2d.XY(-3, 0),
model2d.XY(-1, 7),
model2d.XY(4, 2),
model2d.XY(4, 0),
}
mesh := model2d.NewMesh()
for t := 0.0; t < 1.0; t += 0.01 {
nextT := math.Min(1.0, t+0.01)
mesh.Add(&model2d.Segment{
shape.Eval(t),
shape.Eval(nextT),
})
mesh.Add(&model2d.Segment{
shape.Eval(t).Mul(model2d.XY(1, -1)),
shape.Eval(nextT).Mul(model2d.XY(1, -1)),
})
}
mesh = mesh.Scale(0.4 / 7.0)
solid2d := model2d.NewColliderSolid(model2d.MeshToCollider(mesh))
sdf2d := model2d.MeshToSDF(mesh)
spheres := toolbox3d.NewHeightMap(solid2d.Min(), solid2d.Max(), 1024)
spheres.AddSpheresSDF(sdf2d, 2000, 1e-5, 0.08)
return toolbox3d.HeightMapToSolidBidir(spheres)
}