4 #:vec2
#:make-vec2
#:vec2-x
#:vec2-y
:v2.
:v2
*
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
#: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
)))))
30 "Dot product between two vectors."
32 (values double-float
&optional
))
34 (declare (double-float sum
))
36 (incf sum
(* (aref a i
)
41 "Multiply vector with scalar."
42 (declare (double-float scalar
)
44 (values vec2
&optional
))
45 (let* ((result (make-vec2)))
47 (setf (aref result i
) (* scalar
(aref a i
))))
50 ;;;; double-float 3 vector
53 `(simple-array double-float
(3)))
55 (defstruct (vec (:type
(vector double-float
)))
56 (x 0d0
) (y 0d0
) (z 0d0
))
59 `(simple-array double-float
(3 3)))
61 (defun v (&optional
(x 0d0
) (y 0d0
) (z 0d0
))
62 (declare (double-float x y z
)
63 (values vec
&optional
))
65 :element-type
'double-float
66 :initial-contents
(list x y z
)))
69 "Dot product between two vectors."
71 (values double-float
&optional
))
73 (declare (double-float sum
))
75 (incf sum
(* (aref a i
)
80 (values vec
&optional
))
81 (v (- (* (aref a
1) (aref b
2))
82 (* (aref a
2) (aref b
1)))
83 (- (* (aref a
2) (aref b
0))
84 (* (aref a
0) (aref b
2)))
85 (- (* (aref a
0) (aref b
1))
86 (* (aref a
1) (aref b
0)))))
91 (declaim (ftype (function (vec vec
)
92 (values vec
&optional
))
94 (defmacro v-op
(op a b
)
95 "Subtracting and adding vectors."
98 (setf (aref result i
) (,op
(aref ,a i
)
107 "Subtract two vectors."
110 #+nil
(defun v* (a scalar
)
111 "Multiply vector with scalar."
112 (declare (double-float scalar
)
114 (values vec
&optional
))
117 (setf (aref result i
) (* scalar
(aref a i
))))
120 (defun %v
* (a scalar
)
121 "Multiply vector with scalar."
122 (declare (double-float scalar
)
124 (values vec
&optional
))
127 (setf (aref result i
) (* scalar
(aref a i
))))
130 (defmacro v
* (a scalar
)
132 `(%v
* ,a
(load-time-value (* 1d0
,scalar
)))
139 (declaim (ftype (function (vec)
140 (values double-float
&optional
))
143 "Length of a vector."
145 (declare (type (double-float 0d0
) l2
)) ;; Otherwise warning with complex-double
149 (declaim (ftype (function (vec)
150 (values vec
&optional
))
153 "Rescale vector to unit length."
154 (let ((len (norm a
)))
162 (declaim (ftype (function (double-float double-float double-float
163 double-float double-float double-float
164 double-float double-float double-float
)
165 (values mat
&optional
))
167 (defun m (a b c d e f g h i
)
169 :element-type
'double-float
170 :initial-contents
(list (list a b c
) (list d e f
) (list g h i
))))
172 (defun rotation-matrix (angle vect
)
173 "Create matrix that rotates by ANGLE radians around the direction
174 VECT. VECT must be normalized."
175 (declare ((double-float 0d0
#.
(* 2d0 pi
)) angle
)
177 (values mat
&optional
))
178 (let* ((u (aref vect
0))
199 (rotation-matrix (/ pi
2) (v 0d0
0d0
1d0
))
201 (defun m* (matrix vect
)
202 "Multiply MATRIX with VECT. Copies 4th component w from VECT into
204 (declare (mat matrix
)
206 (values vec
&optional
))
211 (* (aref matrix i j
) (aref vect j
)))))
214 (m* (rotation-matrix (/ pi
2) (v 0d0
0d0
1d0
)) (v 1d0
))
223 `(simple-array fixnum
(3)))
225 (defstruct (vec-i (:type
(vector fixnum
)))
230 (values fixnum
&optional
))
231 (+ (* (vec-i-x a
) (vec-i-x b
))
232 (* (vec-i-y a
) (vec-i-y b
))
233 (* (vec-i-z a
) (vec-i-z b
))))
235 (declaim (ftype (function (vec-i vec-i
)
236 (values vec-i
&optional
))
239 (make-vec-i :x
(- (vec-i-x a
) (vec-i-x b
))
240 :y
(- (vec-i-y a
) (vec-i-y b
))
241 :z
(- (vec-i-z a
) (vec-i-z b
))))
243 (make-vec-i :x
(+ (vec-i-x a
) (vec-i-x b
))
244 :y
(+ (vec-i-y a
) (vec-i-y b
))
245 :z
(+ (vec-i-z a
) (vec-i-z b
))))
249 (values double-float
&optional
))
250 (sqrt (* 1d0
(v.-i a a
))))
252 (defun v*-i
(a scalar
)
253 "Multiply vector with scalar."
254 (declare (fixnum scalar
)
256 (values vec-i
&optional
))
257 (let* ((result (make-vec-i)))
259 (setf (aref result i
) (* scalar
(aref a i
))))