Advertisement
Guest User

Untitled

a guest
Apr 25th, 2013
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 6.17 KB | None | 0 0
  1. ;(defvar pi 3.14159265)
  2.  
  3. ;(declaim (optimize (safety 0) (speed 3) (debug 0)))
  4.  
  5. (defun @ (a i) (elt a i))
  6.  
  7. (defun I (x) x)
  8.  
  9. (defmacro def (&rest args) `(defparameter ,@args))
  10.  
  11. (defun build-list-helper (size proc)
  12.   (if (eq size -1)
  13.       nil
  14.       (cons (funcall proc size) (build-list-helper (- size 1) proc))))
  15.  
  16. (defun build-list (size proc)
  17.   (let ((result (make-list size)))
  18.     (loop for i from 0 to (- size 1) do
  19.       (setf (elt result i) (funcall proc i)))
  20.       result))
  21.  
  22. (defun .x (v) (elt v 0))
  23. (defun .y (v) (elt v 1))
  24. (defun .z (v) (elt v 2))
  25.  
  26. (defun make-zero-vec () (list 0.0 0.0 0.0))
  27.  
  28. (defun make-unity-matrix ()
  29.     (list
  30.         (list 1.0 0.0 0.0)
  31.         (list 0.0 1.0 0.0)
  32.         (list 0.0 0.0 1.0)))
  33.  
  34. ;Vectors
  35. (defun l2-norm (lst)
  36.   (sqrt (reduce #'+ (mapcar (lambda (x) (* x x)) lst))))
  37.  
  38. (defun m*v (mat lst)
  39.   (mapcar (lambda (row) (reduce #'+ (mapcar #'* row lst)))
  40.        mat))
  41.        
  42. (defun s*v (s v)
  43.   (mapcar (lambda (x) (* s x)) v))
  44.  
  45. (defun normalize (v)
  46.   (let ((norm (l2-norm v)))
  47.     (if (eq norm 0.0)
  48.         '(0.0 0.0 0.0)
  49.          (s*v (/ 1.0 norm) v))))
  50.  
  51. (defun v+v (A B)
  52.   (mapcar #'+ A B))
  53.  
  54. (defun v+ (&rest args)
  55.   (reduce #'v+v args))
  56.  
  57. (defun v- (A B)
  58.       (mapcar #'- A B))
  59.  
  60. (defun v.v (A B)
  61.   (reduce #'+ (mapcar #'* A B)))
  62.  
  63. (defun cross* (A B)
  64.   (let ((a1 (elt A 0)) (a2 (elt A 1)) (a3 (elt A 2))
  65.         (b1 (elt B 0)) (b2 (elt B 1)) (b3 (elt B 2)))
  66.     (list (- (* a2 b3) (* a3 b2))
  67.           (- (* a3 b1) (* a1 b3))
  68.           (- (* a1 b2) (* a2 b1)))))
  69.          
  70. (defun project (target vect)
  71.   (s*v (v.v target vect) (normalize target)))
  72.  
  73. (defun normal-part (normal vect) (project normal vect))
  74.  
  75. (defun tangential-part (normal vect) (v- vect (project normal vect)))
  76.  
  77. ;Matrices
  78. (defun take-row (A i)
  79.   (@ A i))
  80.  
  81. (defun take-col (A j)
  82.   (mapcar (lambda (row) (elt row j)) A))
  83.  
  84. (defun row*col (A i B j)
  85.   (v.v (take-row A i) (take-col B j)))
  86.  
  87. (defun build-mat (M N proc)
  88.   (build-list M (lambda (i) (build-list N (lambda (j) (funcall proc i j))))))
  89.  
  90. (defun n-rows (mat)
  91.   (length mat))
  92.  
  93. (defun n-cols (mat)
  94.   (length (car mat)))
  95.  
  96. (defun s*m (alpha mat)
  97.   (mapcar (lambda (v) (s*v alpha v)) mat))
  98.  
  99. (defun m*m (A B)
  100.   (build-mat (n-rows A)
  101.              (n-cols B)
  102.              (lambda (i j) (row*col A i B j))))
  103.              
  104. (defun star-matrix (vec)
  105.     (list (list 0.0 (- (.z vec)) (.y vec))
  106.           (list (.z vec) 0.0 (- (.x vec)))
  107.           (list (- (.y vec)) (.x vec) 0.0)))
  108.  
  109. (defun matrix-transpose (mat)
  110.   (let ((m (length (car mat)))
  111.         (res nil))
  112.     (loop for i from 0 to (- m 1) do
  113.        (push (take-col mat i) res))
  114.     (reverse res)))
  115.  
  116. (defun diff-mul (a b c d) (- (* a b) (* c d)))
  117.    
  118. (defun matrix-inverse3x3 (mat)
  119.     (destructuring-bind
  120.         ((a b c) (d e f) (g h k))
  121.         mat
  122.         (let ((det (+ (- (* a (- (* e k) (* f h)))
  123.                          (* b (- (* k d) (* f g))))
  124.                       (* c (- (* d h) (* e g)))))
  125.               (a-inv (diff-mul e k f h))
  126.               (b-inv (- (diff-mul d k f g)))
  127.               (c-inv (diff-mul d h e g))
  128.               (d-inv (- (diff-mul b k c h)))
  129.               (e-inv (diff-mul a k c g))
  130.               (f-inv (- (diff-mul a h b g)))
  131.               (g-inv (diff-mul b f c e))
  132.               (h-inv (- (diff-mul a f c d)))
  133.               (k-inv (diff-mul a e b d)))
  134.           (if (not (eq det 0.0))
  135.               (s*m (/ 1.0 det)
  136.                    (list (list a-inv d-inv g-inv)
  137.                          (list b-inv e-inv h-inv)
  138.                          (list c-inv f-inv k-inv)))
  139.                    (progn (print "[Numeric error: trying to inverse DET=0 matrix]")
  140.                           nil)))))
  141.    
  142.          
  143. ;(defun (print-v3 lst)
  144. ;  (printf "[X:~s Y:~s Z:~s]\n" (@ lst 0) (@ lst 1) (@ lst 2)))
  145.  
  146. (defun ang->rad (ang)
  147.   (* 2 pi (/ ang 360)))
  148.  
  149. ;Rotation matrices
  150. (defun rot-x-m3 (phi)
  151.   (list
  152.    (list 1.0 0.0 0.0)
  153.    (list 0.0 (cos phi) (- (sin phi)))
  154.    (list 0.0 (sin phi)    (cos phi))))
  155.  
  156. (defun rot-y-m3 (phi)
  157.   (list
  158.    (list (cos phi) 0.0 (sin phi))
  159.    (list 0.0 1.0 0.0)
  160.    (list (- (sin phi)) 0.0 (cos phi))))
  161.  
  162. (defun rot-z-m3 (phi)
  163.   (list
  164.    (list (cos phi) (- (sin phi)) 0.0)
  165.    (list (sin phi)    (cos phi) 0.0)
  166.    (list 0.0 0.0 1.0)))
  167.  
  168. (defun rot-matrix (rot-v)
  169.   (m*m (rot-z-m3 (@ rot-v 2))
  170.        (m*m (rot-y-m3 (@ rot-v 1)) (rot-x-m3 (@ rot-v 0)))))
  171.  
  172. (defun rotate-angles (angles v)
  173.   (m*v (rot-matrix angles) v))
  174.  
  175. ;Rodriguez formula
  176. (defun rotate-axis-angle (unit-axis phi v)
  177.   (let ((k unit-axis))
  178.     (v+ (s*v (cos phi) v)
  179.         (s*v (sin phi) (cross* k v))
  180.         (s*v (* (v.v k v) (- 1.0 (cos phi))) k))))
  181.  
  182. ;Quaternions
  183. (defun quaternion+ (A B)
  184.   (mapcar #'+ A B))
  185.  
  186. (defun quaternion* (A B)
  187.   (let ((s1 (car A)) (s2 (car B))
  188.         (v1 (cdr A)) (v2 (cdr B)))
  189.     (cons (- (* s1 s2) (v.v v1 v2))
  190.           (v+ (s*v s1 v2)
  191.               (s*v s2 v1)
  192.               (cross* v1 v2)))))
  193.  
  194. (defun quaternion-conjugate (A)
  195.   (cons (car A) (mapcar #'- (cdr A))))
  196.  
  197. (defun quaternion-inverse (A)
  198.   (s*v (/ 1.0 (l2-norm A)) (quaternion-conjugate A)))
  199.  
  200. (defun make-quaternion-rotation (unit-axis phi)
  201.   (cons (cos (/ phi 2)) (s*v (sin (/ phi 2)) unit-axis)))
  202.  
  203. (defun apply-quaternion-rotation (q v)
  204.   (cdr (quaternion* (quaternion* q (cons 0.0 v))
  205.                     (quaternion-conjugate q))))
  206.  
  207. (defun S (x) (* x x))
  208.                    
  209. (defun quaternion->rot-matrix (q)
  210.   (let* ((s (car q)) (v (cdr q))
  211.                      (vx (.x v))
  212.                      (vy (.y v))
  213.                      (vz (.z v)))
  214.     (list (list (- 1.0 (* 2.0 (+ (S vy) (S vz))))
  215.                 (* 2.0 (- (* vx vy) (* s vz)))
  216.                 (* 2.0 (+ (* vx vz) (* s vy))))
  217.           (list (* 2.0 (+ (* vx vy) (* s vz)))
  218.                 (- 1.0 (* 2.0 (+ (S vx) (S vz))))
  219.                 (* 2.0 (- (* vy vz) (* s vx))))
  220.           (list (* 2.0 (- (* vx vz) (* s vy)))
  221.                 (* 2.0 (+ (* vy vz) (* s vx)))
  222.                 (- 1.0 (* 2.0 (+ (S vx) (S vy))))))))
  223.  
  224. (defun quaternion->axis/angle (q)
  225.   (let ((phi (* 2.0 (acos (first q)))))
  226.   (cons (normalize (cdr q))
  227.         phi)))
  228.        
  229. ;(def N (expt 10 4))
  230.  
  231. ;(def rot (make-quaternion-rotation (normalize '(-1.0 10.0 1.0)) 0.6))
  232.  
  233. (defun random-float () (float (/ (random (expt 10 9))
  234.                                       (expt 10 9))))
  235.  
  236. ; (def v (build-list N (lambda (j)
  237.                                 ; (normalize
  238.                                   ; (build-list 3 (lambda (i) (random-float)))))))
  239.  
  240. ;(time (mapcar (lambda (vec) (apply-quaternion-rotation rot vec)) v))
  241.  
  242. ;(print "nya")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement