-
Notifications
You must be signed in to change notification settings - Fork 8
/
vec.lisp
291 lines (251 loc) · 10.3 KB
/
vec.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
;;;; By Nikodemus Siivola <[email protected]>, 2009.
;;;;
;;;; Permission is hereby granted, free of charge, to any person
;;;; obtaining a copy of this software and associated documentation files
;;;; (the "Software"), to deal in the Software without restriction,
;;;; including without limitation the rights to use, copy, modify, merge,
;;;; publish, distribute, sublicense, and/or sell copies of the Software,
;;;; and to permit persons to whom the Software is furnished to do so,
;;;; subject to the following conditions:
;;;;
;;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
;;;; IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
;;;; CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
;;;; TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
;;;; SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
(in-package :sb-cga)
(defmacro define-opt-fun (name lambda-list doc &key no-compiler-macro)
(let ((vm/1-name (symbolicate "%%" name "/1"))
(vm-name (symbolicate "%" name))
(form (gensym "FORM")))
`(progn
(eval-when (:compile-toplevel :load-toplevel)
(note-optimizable-fun ',name ',vm/1-name))
(declaim (inline ,name))
(defun ,name ,lambda-list ,doc (,vm-name (alloc-vec) ,@lambda-list))
,@(unless no-compiler-macro
`((define-compiler-macro ,name (&whole ,form ,@lambda-list)
(declare (ignore ,@lambda-list))
(optimize-vec-allocation ,form)))))))
(defmacro define-opt-fun2 (name lambda-list doc)
(let ((vm/1-name (symbolicate "%%" name "/1"))
(vm/2-name (symbolicate "%%" name "/2"))
(vm-name (symbolicate "%" name))
(form (gensym "FORM")))
`(progn
(eval-when (:compile-toplevel :load-toplevel)
(note-optimizable-fun ',name ',vm/1-name ',vm/2-name))
(declaim (inline ,name))
(defun ,name ,lambda-list ,doc (,vm-name (alloc-vec) ,@lambda-list))
(define-compiler-macro ,name (&whole ,form ,@lambda-list)
(declare (ignore ,@lambda-list))
(optimize-vec-allocation ,form)))))
(defmacro define-alloc-fun (name lambda-list &body body)
`(progn
(eval-when (:compile-toplevel :load-toplevel)
(pushnew ',name *alloc-funs*))
(declaim (inline ,name))
(defun ,name ,lambda-list ,@body)))
;;;; CONSTRUCTORS
(declaim (ftype (sfunction () vec) alloc-vec))
(define-alloc-fun alloc-vec ()
"Allocate a zero-initialized VEC."
(make-array 3 :element-type 'single-float :initial-element 0.0))
(declaim (ftype (sfunction (single-float single-float single-float) vec) vec))
(define-alloc-fun vec (x y z)
"Allocate a 3d vector [X, Y, Z]."
(make-array 3 :element-type 'single-float :initial-contents (list x y z)))
;;;; COPYING
(declaim (inline %%copy-vec/1))
(defun %%copy-vec/1 (vec)
;; Not really a copy, but allows optimizing copies away.
vec)
(declaim (ftype (sfunction (vec) vec) copy-vec))
(define-opt-fun copy-vec (vec)
"Allocate a fresh copy of VEC.")
;;;; ARITHMETIC
(declaim (ftype (sfunction (vec vec) vec) vec+))
(define-opt-fun2 vec+ (a b)
"Add VEC A and VEC B, return result as a freshly allocated VEC.")
(declaim (ftype (sfunction (vec vec) vec) vec-))
(define-opt-fun2 vec- (a b)
"Substract VEC B from VEC A, return result as a freshly allocated VEC.")
(declaim (ftype (sfunction (vec single-float) vec) vec*))
(define-opt-fun vec* (a f)
"Multiply VEC A with single-float F, return result as a freshly allocated
VEC.")
(declaim (ftype (sfunction (vec single-float) vec) vec/))
(define-opt-fun vec/ (a f)
"Divide VEC A by single-float F, return result as a freshly allocated VEC.")
;;; FIXME: Unless this is inline SBCL doesn't seem to trust
;;; the declared type!
(declaim (ftype (sfunction (vec vec) single-float) dot-product)
(inline dot-product))
(defun dot-product (a b)
"Compute dot product VEC A and VEC B."
(%dot-product a b))
(declaim (ftype (sfunction (vec vec) vec) hadamard-product))
(define-opt-fun2 hadamard-product (a b)
"Compute hadamard product (elementwise product) of VEC A and VEC B,
return result as a freshly allocated VEC.")
(declaim (ftype (sfunction (vec) single-float) vec-length))
(defun vec-length (a)
"Length of VEC A."
(%vec-length a))
(declaim (ftype (sfunction (vec) vec)))
(define-opt-fun normalize (a)
"Normalize VEC A, return result as a freshly allocated VEC."
:no-compiler-macro t)
(define-compiler-macro normalize (&whole form a)
(if (consp a)
(let ((op (car a))
(args (cdr a)))
(case op
(vec
`(%%normalized-vec (alloc-vec) ,@args))
(cross-product
`(%normalized-cross-product (alloc-vec) ,@args))
(otherwise
(optimize-vec-allocation form))))
form))
(declaim (ftype (sfunction (vec vec single-float) vec) vec-lerp))
(define-opt-fun2 vec-lerp (a b f)
"Linear interpolate VEC A and VEC B using single-float F as the
interpolation factor, return result as a freshly allocated VEC.")
;;; FIXME: It's a bit ugly that %VEC-MIN and %VEC-MAX have a fixed
;;; arity, whereas VEC-MIN and VEC-MAX have variable arity. Generalize
;;; and write a compiler-macro.
;;;
;;; Similarly it would be nice for most VEC operations to have
;;; a variable arity, but it has to be made efficient...
(declaim (ftype (sfunction (vec vec vec) vec) %vec-min)
(inline %vec-min))
(defun %vec-min (result a b)
"Elementwise minimum of VEC A and VEC B, store result in VEC RESULT."
(macrolet ((dim (n)
`(setf (aref result ,n) (min (aref a ,n) (aref b ,n)))))
(dim 0)
(dim 1)
(dim 2))
result)
(declaim (ftype (sfunction (vec vec vec) vec) %vec-max)
(inline %vec-max))
(defun %vec-max (result a b)
"Elementwise maximum of VEC A and VEC B, store result in VEC RESULT."
(macrolet ((dim (n)
`(setf (aref result ,n) (max (aref a ,n) (aref b ,n)))))
(dim 0)
(dim 1)
(dim 2))
result)
(declaim (ftype (sfunction (vec &rest vec) vec) vec-min))
(defun vec-min (vec &rest vecs)
"Elementwise minimum of VEC and VECS, return result as a freshly allocated
VEC."
(declare (dynamic-extent vecs))
(let ((result (copy-vec vec)))
(dolist (vec vecs)
(%vec-min result result vec))
result))
(declaim (ftype (sfunction (vec &rest vec) vec) vec-max))
(defun vec-max (vec &rest vecs)
"Elementwise maximum of VEC and VECS, return result as a freshly allocated
VEC."
(declare (dynamic-extent vecs))
(let ((result (copy-vec vec)))
(dolist (vec vecs)
(%vec-max result result vec))
result))
;;;; CROSS-PRODUCT
(defun %cross-product (result a b)
(let ((a1 (aref a 0))
(a2 (aref a 1))
(a3 (aref a 2))
(b1 (aref b 0))
(b2 (aref b 1))
(b3 (aref b 2)))
(setf (aref result 0) (- (* a2 b3) (* a3 b2))
(aref result 1) (- (* a3 b1) (* a1 b3))
(aref result 2) (- (* a1 b2) (* a2 b1)))
result))
(defun %normalized-cross-product (result a b)
(let ((a1 (aref a 0))
(a2 (aref a 1))
(a3 (aref a 2))
(b1 (aref b 0))
(b2 (aref b 1))
(b3 (aref b 2)))
(%%normalized-vec result
(- (* a2 b3) (* a3 b2))
(- (* a3 b1) (* a1 b3))
(- (* a1 b2) (* a2 b1)))
result))
(declaim (ftype (sfunction (vec vec) vec) cross-product))
(define-alloc-fun cross-product (a b)
"Cross product of 3D vector A and 3D vector B, return result as a freshly
allocated VEC."
(declare (optimize speed))
(%cross-product (alloc-vec) a b))
;;;; COMPARISON
(declaim (ftype (sfunction (vec vec) boolean) vec=))
(defun vec= (a b)
"Return true if VEC A and VEC B are elementwise identical."
(%vec= a b))
(declaim (ftype (sfunction (vec vec &optional single-float) boolean) vec~))
(defun vec~ (a b &optional (epsilon +default-epsilon+))
"Return true if VEC A and VEC B are elementwise within EPSILON of each other.
EPSILON defaults to +DEFAULT-EPSILON+."
(let ((-e (- epsilon))
(d (vec- a b)))
(declare (dynamic-extent d))
(macrolet ((dim (n)
`(<= -e (aref d ,n) epsilon)))
(and (dim 0) (dim 1) (dim 2)))))
;;;; ADJUSTING A VECTOR
(declaim (ftype (sfunction (vec vec single-float) vec) adjust-vec))
(define-opt-fun2 adjust-vec (point direction distance)
"Multiply VEC DIRECTION by single-float DISTANCE adding the result to VEC POINT.
Return result as a freshly allocated VEC.")
;;;; TRANSFORMATIONS
(declaim (ftype (sfunction (vec matrix) vec) transform-point))
(define-opt-fun transform-point (vec matrix)
"Apply transformation MATRIX to VEC, return result as a
freshly allocated VEC.")
(declaim (ftype (sfunction (vec matrix) vec) transform-direction))
(define-opt-fun transform-direction (vec matrix)
"Apply transformation MATRIX to VEC ignoring the translation component,
return result as a freshly allocated VEC.")
(declaim (ftype (function (vec vec matrix) (values vec vec &optional))
transform-bounds))
(defun transform-bounds (v1 v2 matrix)
"Transform the axis-aligned bounding box specified by its extreme corners
V1 and V2 using MATRIX. Return new extreme corners (minimum and maximum
coordinates) as freshly allocted VECs, as the primary and secondary value."
;; Naive method: transform all corners.
;; See http://www.ics.uci.edu/~arvo/code/TransformingBoxes.c
;; for a better way.
(let* ((min (transform-point v1 matrix))
(max (copy-vec min)))
(flet ((tran (i j k)
(let ((tmp (vec i j k)))
(declare (dynamic-extent tmp))
(%%transform-point/1 tmp matrix)
(%vec-min min min tmp)
(%vec-max max max tmp))))
(tran (aref v1 0) (aref v1 1) (aref v2 2))
(tran (aref v1 0) (aref v2 1) (aref v1 2))
(tran (aref v1 0) (aref v2 1) (aref v2 2))
(tran (aref v2 0) (aref v1 1) (aref v1 2))
(tran (aref v2 0) (aref v1 1) (aref v2 2))
(tran (aref v2 0) (aref v2 1) (aref v1 2))
(tran (aref v2 0) (aref v2 1) (aref v2 2)))
(values min max)))
;;;; HASHING
(defun sxhash-vec (vec)
(declare (type vec vec))
(logand most-positive-fixnum
(+ (sxhash (aref vec 0))
(sxhash (aref vec 1))
(sxhash (aref vec 2)))))