<commit_before>;;; matrix.lisp --- Simple vector and matrix math for 3d graphics
;;;                  _        _
;;;  _ __ ___   __ _| |_ _ __(_)_  __
;;; | '_ ` _ \ / _` | __| '__| \ \/ /
;;; | | | | | | (_| | |_| |  | |>  <
;;; |_| |_| |_|\__,_|\__|_|  |_/_/\_\
;;;
;;; Copyright (C) 2007 <NAME> <<EMAIL>>
;;;

(in-package :vecmath)

;;;; ----------------------------------------------------------------------------
;;;; * Matrix types
;;;;

(defvector mat2
    ((ux 1.0) uy
     vx (vy 1.0)))

(defvector mat3
    ((ux 1.0) uy      uz
     vx     (vy 1.0) vz
     wx      wy      (wz 1.0)))

(defvector mat4
    ((ux 1.0) uy uz uw
     vx (vy 1.0) vz vw
     wx wy (wz 1.0) ww
     tx ty tz (tw 1.0)))

(declaim (inline mat-ensure-store))
(defun mat-ensure-store (template &optional store)
  (declare (type mat template) (type (or null mat) store))
  (or store (vec-clone-empty template)))

(declaim (inline mat-ensure-copy))
(defun mat-ensure-copy (template &optional store)
  (declare (type mat template) (type (or null mat) store))
  (the mat (or store (copy-seq template))))

;;;; ----------------------------------------------------------------------------
;;;; * Constructors and Converters

(defvfun mat2<-columns ((u vec2) (v vec2)) mat2
  "Construct a 2x2 matrix from two column vectors."
  (values u.x u.y
          v.x v.y))

(defvfun mat2<-rows ((a vec2) (b vec2)) mat2
  "Construct a 2x2 matrix from two column vectors."
  (values a.x b.x
          a.y b.y))

(defvfun mat3<-columns ((u vec3) (v vec3) (w vec3)) mat3
  "Construct a 3x3 matrix from three column vectors."
  (values u.x u.y u.z
          v.x v.y v.z
          w.x w.y w.z))

(defvfun mat3<-rows ((a vec3) (b vec3) (c vec3)) mat3
  "Construct a 3x3 matrix from three column vectors."
  (values a.x b.x c.x
          a.y b.y c.y
          a.z b.z c.z))

(defvfun mat4<-columns ((u vec4) (v vec4) (w vec4) (trans vec4)) mat4
  "Construct a 4x4 matrix from four column vectors."
  (values u.x u.y u.z u.w
          v.x v.y v.z v.w
          w.x w.y w.z w.w
          trans.x trans.y trans.z trans.w))

(defvfun mat4<-rows ((a vec4) (b vec4) (c vec4) (d vec4)) mat4
  "Construct a 4x4 matrix from four column vectors."
  (values a.x b.x c.x d.x
          a.y b.y c.y d.y
          a.z b.z c.z d.z
          a.w b.w c.w d.w))

(defvfun mat4<-columns3 ((u vec3) (v vec3) (w vec3) (trans vec3)) mat4
  "Construct a 4x4 matrix from four column vectors in three space.
The last row will be set to #(0.0 0.0 0.0 1.0)."
  (values u.x u.y u.z +scalar-zero+
          v.x v.y v.z +scalar-zero+
          w.x w.y w.z +scalar-zero+
          trans.x trans.y trans.z +scalar-one+))

(defvfun mat4<-rows3 ((a vec3) (b vec3) (c vec3) (d vec3)) mat4
  "Construct a 4x4 matrix from four column vectors."
  (values a.x b.x c.x d.x
          a.y b.y c.y d.y
          a.z b.z c.z d.z
          +scalar-zero+ +scalar-zero+ +scalar-zero+ +scalar-one+))

;;;; ----------------------------------------------------------------------------
;;;; ** Convert matrices from and to the euler-angles type.

(defvfun mat3<-euler-angles ((e euler-angles)) mat3
  "Construct a rotation matrix from three angles, describing the rotation
about the Y, Z and X axis and applied in this order."
  (let ((cy (cos e.yaw))
        (sy (sin e.yaw))
        (cp (cos e.pitch))
        (sp (sin e.pitch))
        (cr (cos e.roll))
        (sr (sin e.roll)))
    (values (* cy cp) sp (- (* sy cp))
            (+ (* sy sr) (* (- cy) sp cr)) (* cp cr) (+ (* sy sp cr) (* cy sr))
            (+ (* cy sp sr) (* sy cr)) (* (- cp) sr) (+ (* (- sy) sp sr) (* cy cr)))))

(defvfun euler-angles<-mat3 ((m mat3)) euler-angles
  (declare (ignore m.vx m.vz))
  (cond ((> m.uy (- +scalar-one+ +delta+))
         (values (atan m.wx m.wz) +scalar-pi-half+ +scalar-zero+))
        ((< m.uy (+ +scalar-minus-one+ +delta+))
         (values (atan m.wx m.wz) (- +scalar-pi-half+) +scalar-zero+))
        (t
         (values (atan (- m.uz m.ux)) (atan (- m.wy) m.vy) (asin m.uy)))))

(defvfun mat4<-euler-angles ((e euler-angles) (trans vec3)) mat4
  "Construct a rotation matrix from three angles, describing the rotation
about the Y, Z and X axis and applied in this order."
  (let ((cy (cos e.yaw))
        (sy (sin e.yaw))
        (cp (cos e.pitch))
        (sp (sin e.pitch))
        (cr (cos e.roll))
        (sr (sin e.roll)))
    (values (* cy cp) sp (- (* sy cp))
            (+ (* sy sr) (* (- cy) sp cr)) (* cp cr) (+ (* sy sp cr) (* cy sr))
            (+ (* cy sp sr) (* sy cr)) (* (- cp) sr) (+ (* (- sy) sp sr) (* cy cr))
            trans.x trans.y trans.z +scalar-one+)))

(defvfun euler-angles<-mat4 ((m mat4)) euler-angles
  (declare (ignore m.uw m.vx m.vz m.vw m.ww m.tx m.ty m.tz m.tw))
  (cond ((> m.uy (- +scalar-one+ +delta+))
         (values (atan m.wx m.wz) +scalar-pi-half+ +scalar-zero+))
        ((< m.uy (+ +scalar-minus-one+ +delta+))
         (values (atan m.wx m.wz) (- +scalar-pi-half+) +scalar-zero+))
        (t<commit_msg>Added missing zeros<commit_after>;;; matrix.lisp --- Simple vector and matrix math for 3d graphics
;;;                  _        _
;;;  _ __ ___   __ _| |_ _ __(_)_  __
;;; | '_ ` _ \ / _` | __| '__| \ \/ /
;;; | | | | | | (_| | |_| |  | |>  <
;;; |_| |_| |_|\__,_|\__|_|  |_/_/\_\
;;;
;;; Copyright (C) 2007 <NAME> <<EMAIL>>
;;;

(in-package :vecmath)

;;;; ----------------------------------------------------------------------------
;;;; * Matrix types
;;;;

(defvector mat2
    ((ux 1.0) uy
     vx (vy 1.0)))

(defvector mat3
    ((ux 1.0) uy      uz
     vx     (vy 1.0) vz
     wx      wy      (wz 1.0)))

(defvector mat4
    ((ux 1.0) uy uz uw
     vx (vy 1.0) vz vw
     wx wy (wz 1.0) ww
     tx ty tz (tw 1.0)))

(declaim (inline mat-ensure-store))
(defun mat-ensure-store (template &optional store)
  (declare (type mat template) (type (or null mat) store))
  (or store (vec-clone-empty template)))

(declaim (inline mat-ensure-copy))
(defun mat-ensure-copy (template &optional store)
  (declare (type mat template) (type (or null mat) store))
  (the mat (or store (copy-seq template))))

;;;; ----------------------------------------------------------------------------
;;;; * Constructors and Converters

(defvfun mat2<-columns ((u vec2) (v vec2)) mat2
  "Construct a 2x2 matrix from two column vectors."
  (values u.x u.y
          v.x v.y))

(defvfun mat2<-rows ((a vec2) (b vec2)) mat2
  "Construct a 2x2 matrix from two column vectors."
  (values a.x b.x
          a.y b.y))

(defvfun mat3<-columns ((u vec3) (v vec3) (w vec3)) mat3
  "Construct a 3x3 matrix from three column vectors."
  (values u.x u.y u.z
          v.x v.y v.z
          w.x w.y w.z))

(defvfun mat3<-rows ((a vec3) (b vec3) (c vec3)) mat3
  "Construct a 3x3 matrix from three column vectors."
  (values a.x b.x c.x
          a.y b.y c.y
          a.z b.z c.z))

(defvfun mat4<-columns ((u vec4) (v vec4) (w vec4) (trans vec4)) mat4
  "Construct a 4x4 matrix from four column vectors."
  (values u.x u.y u.z u.w
          v.x v.y v.z v.w
          w.x w.y w.z w.w
          trans.x trans.y trans.z trans.w))

(defvfun mat4<-rows ((a vec4) (b vec4) (c vec4) (d vec4)) mat4
  "Construct a 4x4 matrix from four column vectors."
  (values a.x b.x c.x d.x
          a.y b.y c.y d.y
          a.z b.z c.z d.z
          a.w b.w c.w d.w))

(defvfun mat4<-columns3 ((u vec3) (v vec3) (w vec3) (trans vec3)) mat4
  "Construct a 4x4 matrix from four column vectors in three space.
The last row will be set to #(0.0 0.0 0.0 1.0)."
  (values u.x u.y u.z +scalar-zero+
          v.x v.y v.z +scalar-zero+
          w.x w.y w.z +scalar-zero+
          trans.x trans.y trans.z +scalar-one+))

(defvfun mat4<-rows3 ((a vec3) (b vec3) (c vec3) (d vec3)) mat4
  "Construct a 4x4 matrix from four column vectors."
  (values a.x b.x c.x d.x
          a.y b.y c.y d.y
          a.z b.z c.z d.z
          +scalar-zero+ +scalar-zero+ +scalar-zero+ +scalar-one+))

;;;; ----------------------------------------------------------------------------
;;;; ** Convert matrices from and to the euler-angles type.

(defvfun mat3<-euler-angles ((e euler-angles)) mat3
  "Construct a rotation matrix from three angles, describing the rotation
about the Y, Z and X axis and applied in this order."
  (let ((cy (cos e.yaw))
        (sy (sin e.yaw))
        (cp (cos e.pitch))
        (sp (sin e.pitch))
        (cr (cos e.roll))
        (sr (sin e.roll)))
    (values (* cy cp) sp (- (* sy cp))
            (+ (* sy sr) (* (- cy) sp cr)) (* cp cr) (+ (* sy sp cr) (* cy sr))
            (+ (* cy sp sr) (* sy cr)) (* (- cp) sr) (+ (* (- sy) sp sr) (* cy cr)))))

(defvfun euler-angles<-mat3 ((m mat3)) euler-angles
  (declare (ignore m.vx m.vz))
  (cond ((> m.uy (- +scalar-one+ +delta+))
         (values (atan m.wx m.wz) +scalar-pi-half+ +scalar-zero+))
        ((< m.uy (+ +scalar-minus-one+ +delta+))
         (values (atan m.wx m.wz) (- +scalar-pi-half+) +scalar-zero+))
        (t
         (values (atan (- m.uz m.ux)) (atan (- m.wy) m.vy) (asin m.uy)))))

(defvfun mat4<-euler-angles ((e euler-angles) (trans vec3)) mat4
  "Construct a rotation matrix from three angles, describing the rotation
about the Y, Z and X axis and applied in this order."
  (let ((cy (cos e.yaw))
        (sy (sin e.yaw))
        (cp (cos e.pitch))
        (sp (sin e.pitch))
        (cr (cos e.roll))
        (sr (sin e.roll)))
    (values (* cy cp) sp (- (* sy cp)) +scalar-zero+
            (+ (* sy sr) (* (- cy) sp cr)) (* cp cr) (+ (* sy sp cr) (* cy sr))  +scalar-zero+
            (+ (* cy sp sr) (* sy cr)) (* (- cp) sr) (+ (* (- sy) sp sr) (* cy cr))  +scalar-zero+
            trans.x trans.y trans.z +scalar-one+)))

(defvfun euler-angles<-mat4 ((m mat4)) euler-angles
  (declare (ignore m.uw m.vx m.vz m.vw m.ww m.tx m.ty m.tz m.tw))
  (cond ((> m.uy (- +scalar-one+ +delta+))
         (values (atan m.wx m.wz) +scalar-pi-half+ +scalar-zero+))
        ((< m.uy (+ +scalar-minus-one+ +delta+))
         (values (atan m.wx m.wz) (- +scalar-pi-half+) +scalar-zero+))
        (t
{'fraction_non_alphanumeric': 0.22418967587034813, 'fraction_numerical': 0.020608243297318928, 'mean_word_length': 1.8344201871278707, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3167592', 'n_tokens_mistral': 3929, 'n_tokens_neox': 3783, 'n_words': 1208}