4 #:vec2
#:make-vec2
#:vec2-x
#:vec2-y
6 #:vec
#:make-vec
#:v
#:vec-x
#:vec-y
#:vec-z
7 #:v.
#:v
+ #:v-
#:v
* #:norm
#:normalize
9 #:m
#:rotation-matrix
#:m
*
10 #:vec-i
#:make-vec-i
#:vec-i-x
#:vec-i-y
#:vec-i-z
11 #:v.-i
#:v
+-i
#:v--i
#:norm-i
))
15 ;;;; double-float 2 vector
17 `(simple-array double-float
(2)))
19 (defstruct (vec2 (:type
(vector double-float
)))
24 (values double-float
&optional
))
25 (let ((x (vec2-x vec2
))
27 (sqrt (+ (* x x
) (* y y
)))))
29 ;;;; double-float 3 vector
32 `(simple-array double-float
(3)))
34 (defstruct (vec (:type
(vector double-float
)))
35 (x 0d0
) (y 0d0
) (z 0d0
))
38 `(simple-array double-float
(3 3)))
40 (declaim (ftype (function (&optional double-float double-float double-float
)
41 (values vec
&optional
))
43 (defun v (&optional
(x 0d0
) (y 0d0
) (z 0d0
))
45 :element-type
'double-float
46 :initial-contents
(list x y z
)))
48 (declaim (ftype (function (vec vec
)
49 (values double-float
&optional
))
52 "Dot product between two vectors."
54 (declare (double-float sum
))
56 (incf sum
(* (aref a i
)
60 (declaim (ftype (function (vec vec
)
61 (values vec
&optional
))
64 (v (- (* (aref a
1) (aref b
2))
65 (* (aref a
2) (aref b
1)))
66 (- (* (aref a
2) (aref b
0))
67 (* (aref a
0) (aref b
2)))
68 (- (* (aref a
0) (aref b
1))
69 (* (aref a
1) (aref b
0)))))
74 (declaim (ftype (function (vec vec
)
75 (values vec
&optional
))
77 (defmacro v-op
(op a b
)
78 "Subtracting and adding vectors."
81 (setf (aref result i
) (,op
(aref ,a i
)
90 "Subtract two vectors."
94 (declaim (ftype (function (vec double-float
)
95 (values vec
&optional
))
98 "Multiply vector with scalar."
99 (declare (double-float scalar
)
103 (setf (aref result i
) (* scalar
(aref a i
))))
106 (declaim (ftype (function (vec)
107 (values double-float
&optional
))
110 "Length of a vector."
112 (declare (type (double-float 0d0
) l2
)) ;; Otherwise warning with complex-double
116 (declaim (ftype (function (vec)
117 (values vec
&optional
))
120 "Rescale vector to unit length."
121 (let ((len (norm a
)))
129 (declaim (ftype (function (double-float double-float double-float
130 double-float double-float double-float
131 double-float double-float double-float
)
132 (values mat
&optional
))
134 (defun m (a b c d e f g h i
)
136 :element-type
'double-float
137 :initial-contents
(list (list a b c
) (list d e f
) (list g h i
))))
139 (declaim (ftype (function (double-float vec
)
140 (values mat
&optional
))
142 (defun rotation-matrix (angle vect
)
143 "Create matrix that rotates by ANGLE radians around the direction
144 VECT. VECT must be normalized."
145 (let* ((u (aref vect
0))
166 (rotation-matrix (/ pi
2) (v 0d0
0d0
1d0
))
168 (declaim (ftype (function (mat vec
)
169 (values vec
&optional
))
171 (defun m* (matrix vect
)
172 "Multiply MATRIX with VECT. Copies 4th component w from VECT into
178 (* (aref matrix i j
) (aref vect j
)))))
181 (m* (rotation-matrix (/ pi
2) (v 0d0
0d0
1d0
)) (v 1d0
))
190 `(simple-array fixnum
(3)))
192 (defstruct (vec-i (:type
(vector fixnum
)))
195 (declaim (ftype (function (vec-i vec-i
)
196 (values fixnum
&optional
))
199 (+ (* (vec-i-x a
) (vec-i-x b
))
200 (* (vec-i-y a
) (vec-i-y b
))
201 (* (vec-i-z a
) (vec-i-z b
))))
203 (declaim (ftype (function (vec-i vec-i
)
204 (values vec-i
&optional
))
207 (make-vec-i :x
(- (vec-i-x a
) (vec-i-x b
))
208 :y
(- (vec-i-y a
) (vec-i-y b
))
209 :z
(- (vec-i-z a
) (vec-i-z b
))))
211 (make-vec-i :x
(+ (vec-i-x a
) (vec-i-x b
))
212 :y
(+ (vec-i-y a
) (vec-i-y b
))
213 :z
(+ (vec-i-z a
) (vec-i-z b
))))
215 (declaim (ftype (function (vec-i)
216 (values double-float
&optional
))
219 (sqrt (* 1d0
(v.-i a a
))))