-
Notifications
You must be signed in to change notification settings - Fork 0
/
turtle.lisp
184 lines (141 loc) · 5.03 KB
/
turtle.lisp
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
;;;; -*- Mode: Lisp -*-
;;;;
;;;; Copyright (c) 2008-2009 Arturs Grebstelis
;;;;
(in-package #:abop)
(defstruct branch location color normal radius children)
(defstruct (turtle-cfg (:include state) (:type vector))
(mesh 0)
(branch nil)
(vertical 0.0))
(defstruct (turtle (:include turtle-cfg) (:type vector))
(stack nil))
(defvar *turtle* nil)
(defvar *branching* nil)
(defun turtle-set-radius (new-radius)
(setf (state-scale *turtle*) (sfloat (abs new-radius))))
(defun turtle-set-surface (new-surface)
(setf (state-scale *turtle*) (sfloat (sqrt (/ (abs new-surface) pi)))))
(defun turtle-update-orientation (torque angle)
(setf (state-orientation *turtle*)
(turn-space (state-orientation *turtle*) torque angle)))
(defun turn-turtle-around-axis (axis-fn angle)
(turtle-update-orientation
(funcall axis-fn (state-orientation *turtle*))
(to-radians angle)))
(defun turtle-pitch (angle)
(turn-turtle-around-axis #'vector-space-x angle))
(defun turtle-roll (angle)
(turn-turtle-around-axis #'vector-space-y angle))
(defun turtle-turn (angle)
(turn-turtle-around-axis #'vector-space-z angle))
(defun turtle-flip-up ()
(align-x-axis-with-horizon (state-orientation *turtle*)))
(defun get-normal (state)
(vector-space-y (state-orientation state)))
(defun turtle-adjust (vector amount)
(let ((torque (cross-product (point-normalize vector) (get-normal *turtle*))))
(turtle-update-orientation torque (* amount (point-length torque)))))
(defun new-branch ()
(make-branch
:color (state-color *turtle*)
:normal (get-normal *turtle*)
:radius (state-scale *turtle*)
:location (state-location *turtle*)))
(defun add-new-branch ()
(let ((branch (new-branch)))
(if (null (turtle-branch *turtle*))
(push branch *branching*)
(push branch (branch-children (turtle-branch *turtle*))))
(setf (turtle-branch *turtle*) branch)))
(defun finish-branch ()
(setf (turtle-branch *turtle*) nil))
(defun turtle-displacement (amount)
(point-add
(state-location *turtle*)
(point-scale (get-normal *turtle*) amount)))
(defun turtle-move (amount)
(setf (state-location *turtle*) (turtle-displacement amount)))
(defun turtle-forward (amount)
(if (null (turtle-branch *turtle*)) (add-new-branch))
(turtle-move amount)
(add-new-branch))
(defun turtle-tropism (distance vector strength)
(turtle-forward distance)
(turtle-adjust vector strength))
(defun turtle-jump (amount)
(turtle-move amount)
(finish-branch))
(defun turtle-polygon-start ()
(add-mesh "polygons" (state-color *turtle*))
(polygon-start :name "polygons"))
(defun turtle-polygon-point ()
(draw-vertex
(make-vertex
:position (state-location *turtle*)
:normal (vector-space-z (state-orientation *turtle*)))))
(defun turtle-polygon-forward (x)
(turtle-jump x)
(turtle-polygon-point))
(defun turtle-polygon-finish ()
(polygon-finish))
(defun turtle-teleport (point)
(setf (state-location *turtle*) point)
(finish-branch))
(defun turtle-push ()
(push (copy-turtle *turtle*) (turtle-stack *turtle*)))
(defun turtle-pop ()
(setf *turtle* (pop (turtle-stack *turtle*))))
(defun turtle-object (name &optional (scale 1.0) (color +white+))
(turtle-push)
(turtle-set-radius scale)
(setf (state-color *turtle*) color)
(put-object name *turtle*)
(turtle-pop))
(defun turtle-set-color (new-color)
(setf (state-color *turtle*) new-color))
(defvar *branch-map* nil)
(defun select-mesh (color)
(setf-if-nil
(gethash color *branch-map*)
(add-mesh "branching" color)))
(defun create-stub (dst src)
(make-stub
:location (branch-location src)
:normal (branch-normal (or dst src))
:radius (branch-radius (or dst src))))
(defun get-fatest (&optional branch1 branch2)
(if (or (null branch2) (> (branch-radius branch1) (branch-radius branch2)))
branch1
branch2))
(defun branch-distance (branch1 branch2)
(point-distance (branch-location branch1) (branch-location branch2)))
(defun vertical-increasement (branch child)
(/ (branch-distance child branch)
(* 2 pi (branch-radius child))))
(defun calculate-branch-vertical (branch child)
(+ *dst-vertical*
(if (and child (/= 0.0 (branch-radius child)))
(vertical-increasement branch child)
0.0)))
(defvar *next-vertical* 0.0)
(defun draw-branching (branch &optional old-stub)
(when branch
(let* ((*src-vertical* *dst-vertical*)
(*dst-vertical* *next-vertical*)
(mesh (select-mesh (branch-color branch)))
(fatest (reduce #'get-fatest (branch-children branch)))
(*next-vertical* (calculate-branch-vertical branch fatest))
(stub (draw-branch (create-stub fatest branch) old-stub mesh)))
(setf (branch-children branch) (delete fatest (branch-children branch)))
(when (branch-children branch) (draw-branching branch))
(draw-branching fatest stub))))
(defun draw-all-branching ()
(let ((*branch-map* (make-hash-table)))
(mapc #'draw-branching *branching*)))
(defmacro with-turtle (&body body)
`(let ((*turtle* (make-turtle)))
(clean-up-forms
(setf *turtle* nil)
(setf *branching* nil))
,@body))