-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
85 lines (79 loc) · 1.74 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
package main
import (
"log"
"math"
"math/rand"
"sort"
"github.com/unixpickle/model3d/model3d"
"github.com/unixpickle/model3d/render3d"
)
const (
AdditionSamples = 1000
KeepSamples = 990
GridSize = 3
)
func main() {
log.Println("Creating rocks...")
mesh := model3d.NewMesh()
for i := 0; i < GridSize; i++ {
for j := 0; j < GridSize; j++ {
rock := model3d.ConvexPolytope{
&model3d.LinearConstraint{
Normal: model3d.Coord3D{X: -1},
Max: 0,
},
&model3d.LinearConstraint{
Normal: model3d.X(1),
Max: 1,
},
&model3d.LinearConstraint{
Normal: model3d.Coord3D{Y: -1},
Max: 0,
},
&model3d.LinearConstraint{
Normal: model3d.Y(1),
Max: 1,
},
&model3d.LinearConstraint{
Normal: model3d.Coord3D{Z: -1},
Max: 0,
},
&model3d.LinearConstraint{
Normal: model3d.Z(1),
Max: 1,
},
}
for i := 0; i < 100; i++ {
rock = AddConstraint(rock)
}
min := model3d.XYZ(float64(i)*1.1, float64(j)*1.1, 0)
mesh.AddMesh(rock.Mesh().Translate(min))
}
}
log.Println("Saving results...")
mesh.SaveGroupedSTL("rock.stl")
render3d.SaveRandomGrid("rendering.png", mesh, 3, 3, 300, nil)
}
func AddConstraint(r model3d.ConvexPolytope) model3d.ConvexPolytope {
lc := &model3d.LinearConstraint{
Normal: model3d.Coord3D{
X: rand.NormFloat64(),
Y: rand.NormFloat64(),
Z: math.Abs(rand.NormFloat64()),
},
}
var dotValues []float64
for len(dotValues) < AdditionSamples {
c := model3d.Coord3D{
X: rand.Float64(),
Y: rand.Float64(),
Z: rand.Float64(),
}
if r.Contains(c) {
dotValues = append(dotValues, c.Dot(lc.Normal))
}
}
sort.Float64s(dotValues)
lc.Max = dotValues[KeepSamples]
return append(r, lc)
}