summaryrefslogtreecommitdiff
path: root/tests/matrix.scm
blob: ec6209a493946f992a0398f0f42e82678cc5fea0 (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
;;; Chickadee Game Toolkit
;;; Copyright © 2022 David Thompson <dthompson2@worcester.edu>
;;;
;;; Licensed under the Apache License, Version 2.0 (the "License");
;;; you may not use this file except in compliance with the License.
;;; You may obtain a copy of the License at
;;;
;;;    http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing, software
;;; distributed under the License is distributed on an "AS IS" BASIS,
;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;;; See the License for the specific language governing permissions and
;;; limitations under the License.

(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)))))))