# HG changeset patch # User Steve Losh # Date 1462828964 0 # Node ID 45d7df1f48f332b466de4e1271a57032d6956738 # Parent d4f4d410ba4c568f5a7b8d2a70c4dc17981cb630 Create vec3, I'm sure we'll need it diff -r d4f4d410ba4c -r 45d7df1f48f3 .lispwords --- a/.lispwords Mon May 09 21:09:06 2016 +0000 +++ b/.lispwords Mon May 09 21:22:44 2016 +0000 @@ -1,6 +1,6 @@ (1 scancode-case) (1 make-sketch) (2 with-vals) -(2 with-vec) -(1 with-vecs) +(2 with-vec with-vec3) +(1 with-vecs with-vec3s) (1 with-setup) diff -r d4f4d410ba4c -r 45d7df1f48f3 coding-math.asd --- a/coding-math.asd Mon May 09 21:09:06 2016 +0000 +++ b/coding-math.asd Mon May 09 21:22:44 2016 +0000 @@ -34,5 +34,6 @@ (:file "ballistics"))) (:module "3d" :serial t - :components ((:file "demo"))))))) + :components ((:file "vectors") + (:file "demo"))))))) diff -r d4f4d410ba4c -r 45d7df1f48f3 package.lisp --- a/package.lisp Mon May 09 21:09:06 2016 +0000 +++ b/package.lisp Mon May 09 21:22:44 2016 +0000 @@ -180,6 +180,27 @@ ;;;; 3D stuff +(defpackage #:coding-math.3d.vectors + (:use + #:cl + #:coding-math.math + #:coding-math.utils + #:coding-math.quickutils) + (:export + #:vec3 + #:vec3-x + #:vec3-y + #:make-vec3 + #:make-random-vec3 + #:vec3-magnitude + #:vec3-add #:vec3-add! + #:vec3-sub #:vec3-sub! + #:vec3-mul #:vec3-mul! + #:vec3-div #:vec3-div! + #:vec3-lerp + #:with-vec3 + #:with-vec3s)) + (defpackage #:coding-math.3d.demo (:use #:cl @@ -188,5 +209,6 @@ #:coding-math.utils #:coding-math.fps #:coding-math.math + #:coding-math.3d.vectors )) diff -r d4f4d410ba4c -r 45d7df1f48f3 src/3d/vectors.lisp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/3d/vectors.lisp Mon May 09 21:22:44 2016 +0000 @@ -0,0 +1,92 @@ +(in-package #:coding-math.3d.vectors) + + +(declaim (inline vec3-x vec3-y vec3-z make-vec3 + vec3-magnitude + vec3-add vec3-sub vec3-mul vec3-div + vec3-add! vec3-sub! vec3-mul! vec3-div! + vec3-lerp)) + +(defstruct (vec3 + (:constructor make-vec3 + (&optional (x 0) (y 0) (z 0))) + (:type vector)) + (x 0 :type real) + (y 0 :type real) + (z 0 :type real)) + +(defun make-random-vec3 (max-x max-y max-z) + (make-vec3 (random max-x) (random max-y) (random max-z))) + + +(defmacro with-vec3 (bindings vec &body body) + (once-only (vec) + `(let ((,(first bindings) (vec3-x ,vec)) + (,(second bindings) (vec3-y ,vec)) + (,(third bindings) (vec3-z ,vec))) + ,@body))) + +(defmacro with-vec3s (bindings &body body) + (if (null bindings) + `(progn ,@body) + (destructuring-bind (vars vec-form . remaining) bindings + `(with-vec3 ,vars ,vec-form (with-vec3s ,remaining ,@body))))) + + +(defun vec3-magnitude (vec) + (with-vec3 (x y z) vec + (sqrt (+ (square x) + (square y) + (square z))))) + + +(defun vec3-add (v1 v2) + (make-vec3 (+ (vec3-x v1) (vec3-x v2)) + (+ (vec3-y v1) (vec3-y v2)) + (+ (vec3-z v1) (vec3-z v2)))) + +(defun vec3-sub (v1 v2) + (make-vec3 (- (vec3-x v1) (vec3-x v2)) + (- (vec3-y v1) (vec3-y v2)) + (- (vec3-z v1) (vec3-z v2)))) + +(defun vec3-mul (v s) + (make-vec3 (* (vec3-x v) s) + (* (vec3-y v) s) + (* (vec3-z v) s))) + +(defun vec3-div (v s) + (make-vec3 (/ (vec3-x v) s) + (/ (vec3-y v) s) + (/ (vec3-z v) s))) + + +(defun vec3-add! (v1 v2) + (incf (vec3-x v1) (vec3-x v2)) + (incf (vec3-y v1) (vec3-y v2)) + (incf (vec3-z v1) (vec3-z v2))) + +(defun vec3-sub! (v1 v2) + (decf (vec3-x v1) (vec3-x v2)) + (decf (vec3-y v1) (vec3-y v2)) + (decf (vec3-z v1) (vec3-z v2))) + +(defun vec3-mul! (v s) + (setf (vec3-x v) (* (vec3-x v) s) + (vec3-y v) (* (vec3-y v) s) + (vec3-z v) (* (vec3-z v) s))) + +(defun vec3-div! (v s) + (setf (vec3-x v) (/ (vec3-x v) s) + (vec3-y v) (/ (vec3-y v) s) + (vec3-z v) (/ (vec3-z v) s))) + + +(defun vec3-lerp (v1 v2 n) + (with-vec3s ((x1 y1 z1) v1 + (x2 y2 z2) v2) + (make-vec3 (lerp x1 x2 n) + (lerp y1 y2 n) + (lerp z1 z2 n)))) + +