Repository navigation
Expand file tree
/
Copy pathutils.lisp
More file actions
218 lines (192 loc) · 5.81 KB
/
Copy pathutils.lisp
File metadata and controls
218 lines (192 loc) · 5.81 KB
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
(uiop:define-package #:slither/utils
(:use #:cl)
(:local-nicknames (:glfw :org.shirakumo.fraf.glfw))
(:use-reexport #:org.shirakumo.fraf.math.matrices
#:org.shirakumo.fraf.math.vectors)
(:import-from :alexandria
:assoc-value
:when-let
:when-let*
:if-let)
(:import-from :serapeum
:eval-always
:->
:octet
:octet-vector
:make-octet-vector
:octet-vector-p
:octet-vector=
:do-hash-table
:do-each)
(:export
;; Alexandria
:assoc-value
:when-let
:when-let*
:if-let
;; Serapeum
:eval-always
:->
:octet
:octet-vector
:make-octet-vector
:octet-vector-p
:octet-vector=
:do-hash-table
:do-each
;; Local
:copy-value
:continuable
:symbol->camel-case
:function-symbol->global-variable-symbol
:defmemo
:random-color
:degrees->radians
:rotation->vec2
:random-float
:random-element
:clamp
:smoothstep
:ensure-non-keyword-symbol
:lambda-list-bindings
:vec2->rotation
:mat2->mat3
:safe-vscale
:lerp
:rotation-lerp
:anchor
:position-apply-anchor
:radians->degrees
:in-main-thread
:vtan))
(in-package #:slither/utils)
(defun copy-value (value)
(typecase value
(vec (vcopy value))
(sequence (copy-seq value))
(t value)))
(defun safe-vscale (a s)
(if (or (= 0 s)
(= 0 (vx a))
(= 0 (vy a)))
(vec2)
(let ((sum (+ (vx a) (vy a))))
(if (< -1.0e-22 sum 1.0e-22)
a
(vscale a s)))))
(defmacro defmemo (name &body body)
(let ((memo (gensym "MEMO")))
`(let (,memo)
(defun ,name ()
(unless ,memo
(setf ,memo
(progn ,@body)))
,memo))))
(defun clamp (value min max)
(cond
((< value min) min)
((> value max) max)
(t value)))
(defun smoothstep (value edge0 edge1)
(let ((value (clamp (/ (- value edge0) (- edge1 edge0)) 0 1)))
(* value value (- 3.0 (* 2.0 value)))))
(defun lerp (start end step)
(+ (* (- end start) (clamp step 0 1))
start))
(defun rotation-lerp (start end step)
(let ((diff (- (mod (+ (- end start) 540) 360) 180)))
(mod (+ start
(* diff step)
360)
360)))
(defun symbol->camel-case (symbol)
(loop for character across (string symbol)
with should-capitalize = nil
when (char= character #\-)
do (setf should-capitalize t)
else
collect (if should-capitalize
(char-upcase character)
(char-downcase character))
into result
and do (setf should-capitalize nil)
finally (return (coerce result 'string))))
(defmacro continuable (&body body)
`(restart-case
(progn ,@body)
(continue () :report "Continue")))
(defun degrees->radians (degrees)
(float (* degrees (/ pi 180)) 0.0))
(defun rotation->vec2 (degrees)
(let ((radians (degrees->radians degrees)))
(vec2 (cos radians)
(sin radians))))
(defun random-float (&optional (range 1) (precision 10))
(float (* (/ (random precision) precision) range)))
(defun mat2->mat3 (mat2)
(let ((mat3 (meye 3)))
(mtransfer mat3 mat2 :w 2 :h 2)
mat3))
(defun randomly-negate (number)
(case (random 2)
(0 (- number))
(1 number)))
(defun radians->degrees (radians)
(* radians (/ 180 pi)))
(defun vec2->rotation (vec)
(radians->degrees (atan (vx vec)
(vy vec))))
(defmethod angle-towards ((from vec2) (to vec2))
(vec2->rotation (vscale (v- to from) 1)))
(defun random-element (list)
(nth (random (length list)) list))
(defun random-color ()
(vec4 (random-float) (random-float) (random-float) (random-float)))
(defun lambda-list-bindings (lambda-list)
"Gets the binding symbols from a lambda list"
(let (bindings parsing-keys)
(loop for argument in lambda-list
do (let ((binding (cond ((eq argument '&key) (setf parsing-keys t) nil)
((member argument lambda-list-keywords) nil)
((symbolp argument) argument)
((consp argument) (car argument))
(t nil))))
(when binding
(when parsing-keys
(push (intern (symbol-name binding) :keyword) bindings))
(push binding bindings))))
(nreverse bindings)))
(defun ensure-non-keyword-symbol (symbol)
(etypecase symbol
(keyword (intern (symbol-name symbol)))
(symbol symbol)
(string (intern symbol))))
(deftype anchor ()
'(member :center :top :right :left :bottom
:top-left :top-right :bottom-left :bottom-right))
(-> position-apply-anchor (vec2 &key (:size vec2) (:anchor anchor) (:rotation number)) vec2)
(defun position-apply-anchor (position &key size anchor (rotation 0.0))
(let ((offset
(ecase anchor
(:center (vec2 0 0))
(:top (vec2 0 (vy size)))
(:bottom (vec2 0 (- (vy size))))
(:left (vec2 (- (vx size)) 0))
(:right (vec2 (vx size) 0))
(:top-left (vec2 (- (vx size)) (vy size)))
(:top-right (vec2 (vx size) (vy size)))
(:bottom-left (vec2 (- (vx size)) (- (vy size))))
(:bottom-right (vec2 (vx size) (- (vy size)))))))
(v- position
(vrot2 offset (degrees->radians rotation)))))
(defmacro in-main-thread (&body body)
`(if (sb-thread:main-thread-p)
(progn ,@body)
(sb-thread:interrupt-thread (sb-thread:main-thread)
(lambda ()
,@body))))
(-> vtan (vec2) vec2)
(defun vtan (v)
"Return the tangent vector to v"
(vec2 (- (vy v))
(vx v)))