-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
141 lines (120 loc) · 2.93 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
package main
import (
"log"
"math"
"sort"
"github.com/unixpickle/model3d/toolbox3d"
"github.com/unixpickle/model3d/render3d"
"github.com/unixpickle/model3d/model2d"
"github.com/unixpickle/model3d/model3d"
)
func main() {
solid := model3d.JoinedSolid{
NewEggSolid(),
// Stand
&model3d.Cylinder{
P1: model3d.Coord3D{},
P2: model3d.Z(0.3),
Radius: 0.2,
},
}
log.Println("Creating mesh...")
// Fix artifacts on the base
squeeze := &toolbox3d.AxisPinch{
Axis: toolbox3d.AxisZ,
Min: -0.01,
Max: 0.01,
Power: 0.25,
}
mesh := model3d.MarchingCubesConj(solid, 0.005, 8, squeeze)
log.Println("Saving mesh...")
mesh.SaveGroupedSTL("egg.stl")
log.Println("Rendering mesh...")
render3d.SaveRandomGrid("rendering.png", mesh, 3, 3, 300, nil)
}
type EggSolid struct {
RadiusFunc model2d.BezierCurve
RadiusIntegral *RadiusIntegral
MaxRadius float64
Height float64
RidgeInset float64
RidgeFreq float64
RidgeFrac model2d.BezierCurve
}
func NewEggSolid() *EggSolid {
radius := model2d.BezierCurve{
model2d.Coord{},
model2d.Y(0.6),
model2d.XY(1.0, 0.3),
model2d.X(1.0),
}
_, maxRadius := (&toolbox3d.LineSearch{Stops: 100, Recursions: 2}).Maximize(0, 1, radius.EvalX)
maxRadius += 0.02
return &EggSolid{
RadiusFunc: radius,
RadiusIntegral: NewRadiusIntegral(radius),
MaxRadius: maxRadius,
Height: 1.0,
RidgeInset: 0.025,
RidgeFreq: 18.0,
RidgeFrac: radius,
}
}
func (e *EggSolid) Min() model3d.Coord3D {
return model3d.XY(-e.MaxRadius, -e.MaxRadius)
}
func (e *EggSolid) Max() model3d.Coord3D {
return model3d.XYZ(e.MaxRadius, e.MaxRadius, e.Height)
}
func (e *EggSolid) Contains(c model3d.Coord3D) bool {
if !model3d.InBounds(e, c) {
return false
}
c2d := c.XY()
maxRadius := e.RadiusFunc.EvalX(c.Z)
curRadius := c2d.Norm()
if curRadius < maxRadius-e.RidgeInset {
return true
} else if curRadius > maxRadius {
return false
}
xTheta := math.Atan2(c2d.X, c2d.Y)
yTheta := e.RadiusIntegral.EvalX(c.Z)
yOffset := math.Sin(e.RidgeFreq * yTheta)
xOffset := math.Sin(e.RidgeFreq * xTheta)
inset := e.RidgeFrac.EvalX(c.Z) * 0.5 * e.RidgeInset * (2 - math.Abs(xOffset+yOffset))
return curRadius < maxRadius-inset
}
type RadiusIntegral struct {
X []float64
Y []float64
}
func NewRadiusIntegral(radius model2d.BezierCurve) *RadiusIntegral {
res := &RadiusIntegral{}
dx := 0.0001
var sum float64
for x := 0.0; x < 1.0; x += dx {
res.X = append(res.X, x)
res.Y = append(res.Y, sum)
r := radius.EvalX(x)
if r < 1e-5 {
r = 1e-5
}
sum += math.Sqrt(math.Pow(r*dx, 2)+dx*dx) / r
}
return res
}
func (r *RadiusIntegral) EvalX(x float64) float64 {
min := sort.SearchFloat64s(r.X, x)
if min == 0 {
return r.Y[0]
} else if min == len(r.Y) {
return r.Y[len(r.Y)-1]
}
min -= 1
prevX := r.X[min]
nextX := r.X[min+1]
prevFrac := (x - prevX) / (nextX - prevX)
nextFrac := 1 - prevFrac
return prevFrac*r.Y[min] + nextFrac*r.Y[min+1]
}