-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.go
142 lines (122 loc) · 3.52 KB
/
main.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
package main
import (
"log"
"github.com/unixpickle/model3d/toolbox3d"
"github.com/unixpickle/model3d/model2d"
"github.com/unixpickle/model3d/model3d"
"github.com/unixpickle/model3d/render3d"
)
const (
SideSize = 7.0
BottomThickness = 0.2
WallHeight = 2.0
WallThickness = 0.3
SectionHeight = 1.7
SectionThickness = 0.2
LidHeight = 1.5
LidThickness = 0.2
LidHolderSize = 0.2
LidHolderInset = 0.06
ImageSize = 1024.0
)
func main() {
log.Println("Loading 2D shapes...")
outline := model2d.MeshToCollider(
model2d.MustReadBitmap("outline.png", nil).FlipY().Mesh().SmoothSq(200),
)
sections := model2d.MeshToCollider(
model2d.MustReadBitmap("sections.png", nil).FlipY().Mesh().SmoothSq(100),
)
boxSolid := &BoxSolid{Outline: outline, Sections: sections}
lidSolid := &LidSolid{Outline: outline}
log.Println("Creating box...")
mesh := CreateBoxSqueeze().MarchingCubesSearch(boxSolid, 0.02, 8)
log.Println(" - eliminating co-planar...")
mesh = mesh.EliminateCoplanar(1e-8)
log.Println(" - flattening base...")
mesh = mesh.FlattenBase(0)
log.Println(" - saving...")
mesh.SaveGroupedSTL("box.stl")
log.Println(" - rendering...")
render3d.SaveRandomGrid("rendering_box.png", mesh, 3, 3, 300, nil)
log.Println("Creating lid...")
mesh = CreateLidSqueeze().MarchingCubesSearch(lidSolid, 0.02, 8)
log.Println(" - eliminating co-planar...")
mesh = mesh.EliminateCoplanar(1e-8)
log.Println(" - flattening base...")
mesh = mesh.FlattenBase(0)
log.Println(" - saving...")
mesh.SaveGroupedSTL("lid.stl")
log.Println(" - rendering...")
render3d.SaveRandomGrid("rendering_lid.png", mesh, 3, 3, 300, nil)
}
func CreateBoxSqueeze() *toolbox3d.SmartSqueeze {
squeeze := toolbox3d.NewSmartSqueeze(toolbox3d.AxisZ, 0, 0.05, 0)
squeeze.AddPinch(0)
squeeze.AddPinch(BottomThickness)
squeeze.AddPinch(SectionHeight)
squeeze.AddPinch(WallHeight)
return squeeze
}
func CreateLidSqueeze() *toolbox3d.SmartSqueeze {
squeeze := toolbox3d.NewSmartSqueeze(toolbox3d.AxisZ, 0, 0.05, 0)
squeeze.AddPinch(0)
squeeze.AddPinch(LidThickness)
squeeze.AddPinch(LidHeight)
squeeze.AddPinch(LidHeight + LidHolderSize)
return squeeze
}
type BoxSolid struct {
Outline model2d.Collider
Sections model2d.Collider
}
func (b *BoxSolid) Min() model3d.Coord3D {
return model3d.Coord3D{}
}
func (b *BoxSolid) Max() model3d.Coord3D {
return model3d.XYZ(SideSize, SideSize, WallHeight)
}
func (b *BoxSolid) Contains(c model3d.Coord3D) bool {
if !model3d.InBounds(b, c) {
return false
}
scale := ImageSize / SideSize
c2 := c.XY().Scale(scale)
if !model2d.ColliderContains(b.Outline, c2, 0) {
return false
}
if c.Z < BottomThickness {
return true
}
if b.Outline.CircleCollision(c2, WallThickness*scale) {
return true
}
return c.Z < SectionHeight && b.Sections.CircleCollision(c2, 0.5*SectionThickness*scale)
}
type LidSolid struct {
Outline model2d.Collider
}
func (l *LidSolid) Min() model3d.Coord3D {
return model3d.Coord3D{}
}
func (l *LidSolid) Max() model3d.Coord3D {
return model3d.XYZ(SideSize, SideSize, LidHeight+LidHolderSize)
}
func (l *LidSolid) Contains(c model3d.Coord3D) bool {
if !model3d.InBounds(l, c) {
return false
}
scale := ImageSize / SideSize
c2 := c.XY().Scale(scale)
if !model2d.ColliderContains(l.Outline, c2, 0) {
return false
}
if c.Z < LidThickness {
return true
}
if !model2d.ColliderContains(l.Outline, c2, scale*(WallThickness+LidHolderInset)) {
return c.Z < LidHeight
}
return !model2d.ColliderContains(l.Outline, c2,
scale*(WallThickness+LidHolderInset+LidThickness))
}