summaryrefslogtreecommitdiff
path: root/tests/matrix.scm
blob: 0cd24a90771d780e05a58759c6b3432354fd519d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
;;; Chickadee Game Toolkit
;;; Copyright © 2022 David Thompson <dthompson2@worcester.edu>
;;;
;;; Chickadee is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published
;;; by the Free Software Foundation, either version 3 of the License,
;;; or (at your option) any later version.
;;;
;;; Chickadee is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

(define-module (tests matrix)
  #:use-module (tests utils)
  #:use-module (srfi srfi-64)
  #:use-module (chickadee math)
  #:use-module (chickadee math matrix)
  #:use-module (chickadee math quaternion)
  #:use-module (chickadee math vector))

(with-tests "matrix"
  (test-group "3x3 matrices"
    (test-assert "matrix3-copy"
      (let ((m (make-matrix3 0 1 2
                             3 4 5
                             6 7 8)))
        (matrix3= m (matrix3-copy m))))
    (test-assert "matrix3-copy!"
      (let ((src (make-matrix3 0 1 2
                               3 4 5
                               6 7 8))
            (dest (make-null-matrix3)))
        (matrix3-copy! src dest)
        (matrix3= src dest))))
  (test-group "4x4 matrices"
    (test-assert "matrix4-copy"
      (let ((m (make-matrix4 0 1 2 3
                             4 5 6 7
                             8 9 0 1
                             2 3 4 5)))
        (matrix4= m (matrix4-copy m))))
    (test-assert "matrix4-copy!"
      (let ((src (make-matrix4 0 1 2 3
                               4 5 6 7
                               8 9 0 1
                               2 3 4 5))
            (dest (make-null-matrix4)))
        (matrix4-copy! src dest)
        (matrix4= src dest)))
    (test-group "multiplication"
      (test-assert "identity"
        (let ((m (make-matrix4 0 1 2 3
                               4 5 6 7
                               8 9 0 1
                               2 3 4 5)))
          (matrix4= m (matrix4* (make-identity-matrix4) m))))
      (test-assert "inverse"
        ;; Using whole numbers to avoid floating point precision
        ;; issues where the resulting identity matrix has values very
        ;; close to 1.0 (like 0.9999999403953552) but not exactly,
        ;; causing the test to fail.
        (let ((m (matrix4-scale (vec3 2 3 4))))
          (matrix4= (make-identity-matrix4)
                    (matrix4* m (matrix4-inverse m))))))
    (test-group "transformation"
      (test-assert "identity (vec2)"
        (let ((v (vec2 1 2)))
          (vec2= v (matrix4-transform-vec2 (make-identity-matrix4) v))))
      (test-assert "identity (vec3)"
        (let ((v (vec3 1 2 3)))
          (vec3= v (matrix4-transform-vec3 (make-identity-matrix4) v))))
      (test-assert "translate"
        (vec3= (vec3 5 7 9)
               (matrix4-transform-vec3 (matrix4-translate (vec3 4 5 6))
                                       (vec3 1 2 3))))
      (test-assert "scale"
        (vec3= (vec3 6 12 20)
               (matrix4-transform-vec3 (matrix4-scale (vec3 3 4 5))
                                       (vec3 2 3 4))))
      (test-assert "rotate"
        (vec3= (vec3 0 1 0)
               (matrix4-transform-vec3 (matrix4-rotate
                                        (rotation->quaternion (vec3 0 0 -1)
                                                              (/ pi 2)))
                                       (vec3 1 0 0)))))))