-
Notifications
You must be signed in to change notification settings - Fork 4
/
turtlegraph3d.go
271 lines (238 loc) · 5.68 KB
/
turtlegraph3d.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package genlsystem
import (
"bufio"
"fmt"
"image/color"
"math"
"os"
"github.com/Flokey82/go_gens/vectors"
)
type Bounds3d struct {
minX, minY, minZ float64
maxX, maxY, maxZ float64
}
func (a *Bounds3d) AddPoint(x, y, z float64) {
if a.minX > x {
a.minX = x
}
if a.maxX < x {
a.maxX = x
}
if a.minY > y {
a.minY = y
}
if a.maxY < y {
a.maxY = y
}
if a.minZ > z {
a.minZ = z
}
if a.maxZ < z {
a.maxZ = z
}
}
func (a *Bounds3d) Size() (width, height, depth float64) {
return a.maxX - a.minX, a.maxY - a.minY, a.maxZ - a.minZ
}
type stack3d struct {
x, y, z float64
color color.Color
width float64
forward vectors.Vec3
up vectors.Vec3
prev *stack3d
}
type line3d struct {
x1, y1, x2, y2, z1, z2 float64
color color.Color
width float64
}
// Turtle3d implements a turtle-esque drawing system IN 3D!
// This code is partially inspired by:
// https://github.com/yalue/l_system_3d
// and:
// https://github.com/recp/cglm/blob/master/include/cglm/vec3.h
type Turtle3d struct {
rules map[string]func(*Turtle3d)
cur *stack3d
lines []line3d
boundary *Bounds3d
}
// NewTurtle3d returns a new Turtle3d struct.
func NewTurtle3d(rules map[string]func(*Turtle3d)) *Turtle3d {
return &Turtle3d{
rules: rules,
cur: &stack3d{
color: color.RGBA{0x00, 0x00, 0x00, 0xFF},
width: 1,
forward: vectors.NewVec3(1, 0, 0),
up: vectors.NewVec3(0, 1, 0),
},
lines: []line3d{},
boundary: &Bounds3d{},
}
}
// save position and angle
func (t *Turtle3d) Save() {
t.cur = &stack3d{
x: t.cur.x,
y: t.cur.y,
z: t.cur.z,
color: t.cur.color,
width: t.cur.width,
prev: t.cur, // stackception
up: t.cur.up,
forward: t.cur.forward,
}
}
// restore position and angle
func (t *Turtle3d) Restore() {
if t.cur == nil {
return
}
if p := t.cur.prev; p != nil {
t.cur.prev = nil // stackalypse
t.cur = p
}
}
// Move moves forward without drawing.
func (t *Turtle3d) Move(f float64) {
x, y, z := t.advanceRotateEtc(f)
t.cur.x += x
t.cur.y += y
t.cur.z += z
t.boundary.AddPoint(t.cur.x, t.cur.y, t.cur.z)
}
func (t *Turtle3d) advanceRotateEtc(distance float64) (x, y, z float64) {
change := t.cur.forward.Mul(distance)
x = change.X
y = change.Y
z = change.Z
return
}
// Draw draws a line forward.
func (t *Turtle3d) Draw(f float64) {
sx, sy, sz := t.cur.x, t.cur.y, t.cur.z
x, y, z := t.advanceRotateEtc(f)
t.cur.x += x
t.cur.y += y
t.cur.z += z
t.boundary.AddPoint(t.cur.x, t.cur.y, t.cur.z)
t.lines = append(t.lines, line3d{
x1: sx,
y1: sy,
z1: sz,
x2: t.cur.x,
y2: t.cur.y,
z2: t.cur.z,
color: t.cur.color,
width: t.cur.width,
})
}
// GetPosition gets the current position.
func (t *Turtle3d) GetPosition() (x, y, z float64) {
return t.cur.x, t.cur.y, t.cur.z
}
func degToRadians(degrees float64) float64 {
return degrees * (math.Pi / 180.0)
}
// Rotate is the same as Yaw.
func (t *Turtle3d) Rotate(angle float64) {
glm_vec3_rotate(&t.cur.forward, degToRadians(angle), t.cur.up)
}
func (t *Turtle3d) Pitch(angle float64) {
right := vectors.Cross3(t.cur.forward, t.cur.up)
glm_vec3_rotate(&t.cur.up, degToRadians(angle), right)
glm_vec3_rotate(&t.cur.forward, degToRadians(angle), right)
}
func (t *Turtle3d) Roll(angle float64) {
glm_vec3_rotate(&t.cur.up, degToRadians(angle), t.cur.forward)
}
// GetForward gets the current forward vector
func (t *Turtle3d) GetForward() vectors.Vec3 {
return t.cur.forward
}
// SetColor sets the current color.
func (t *Turtle3d) SetColor(c color.Color) {
t.cur.color = c
}
// GetColor gets the current color.
func (t *Turtle3d) GetColor() color.Color {
return t.cur.color
}
// SetWidth sets the current width.
func (t *Turtle3d) SetWidth(w float64) {
t.cur.width = w
}
// GetWidth gets the current width.
func (t *Turtle3d) GetWidth() float64 {
return t.cur.width
}
// UNLEASH THE TURTLE!
func (t *Turtle3d) Go(fname string, path []string) error {
// draw lines
for _, c := range path {
if f, ok := t.rules[c]; ok {
f(t)
}
}
border := 5.0
offx, offy, offz := t.boundary.minX-border, t.boundary.minY-border, t.boundary.minZ-border
// Generate index for vertex indices.
var vxs [][3]float64
vtxIdx := make(map[[3]float64]int)
for _, line := range t.lines {
start := [3]float64{line.x1 - offx, line.y1 - offy, line.z1 - offz}
if _, ok := vtxIdx[start]; !ok {
vtxIdx[start] = len(vxs)
vxs = append(vxs, start)
}
stop := [3]float64{line.x2 - offx, line.y2 - offy, line.z2 - offz}
if _, ok := vtxIdx[stop]; !ok {
vtxIdx[stop] = len(vxs)
vxs = append(vxs, stop)
}
}
f, err := os.Create(fname)
if err != nil {
return err
}
defer f.Close()
wr := bufio.NewWriter(f)
for _, p := range vxs {
wr.WriteString(fmt.Sprintf("v %f %f %f \n", p[0], p[1], p[2]))
}
for _, line := range t.lines {
start := [3]float64{line.x1 - offx, line.y1 - offy, line.z1 - offz}
stop := [3]float64{line.x2 - offx, line.y2 - offy, line.z2 - offz}
wr.WriteString(fmt.Sprintf("l %d %d \n", vtxIdx[start]+1, vtxIdx[stop]+1))
}
wr.Flush()
// cleanup
t.Cleanup()
return nil
}
func (t *Turtle3d) Cleanup() {
t.cur = &stack3d{
color: color.RGBA{0x00, 0x00, 0x00, 0xFF},
width: 1,
forward: vectors.NewVec3(1, 0, 0),
up: vectors.NewVec3(0, 1, 0),
}
t.lines = nil
t.boundary = new(Bounds3d)
}
func glm_vec3_rotate(v *vectors.Vec3, angle float64, axis vectors.Vec3) {
c := math.Cos(angle)
s := math.Sin(angle)
k := axis.Normalize()
/* Right Hand, Rodrigues' rotation formula:
v = v*cos(t) + (kxv)sin(t) + k*(k.v)(1 - cos(t))
*/
v1 := v.Mul(c)
v2 := vectors.Cross3(k, *v)
v2 = v2.Mul(s)
v1 = vectors.Add3(v1, v2)
v2 = k.Mul(vectors.Dot3(k, *v) * (1.0 - c))
*v = vectors.Add3(v1, v2)
}