summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2023-03-22 18:46:01 -0400
committerDavid Thompson <dthompson2@worcester.edu>2023-03-22 18:46:01 -0400
commitb64e503ffd72bbbce4d9e299f4d96c7448172875 (patch)
tree231b3feff313564d0646daf89fbd452f38829f0d
parent97040d5339002b0e1e5235ed4cc887755c7d91bd (diff)
Add generic math module.
-rw-r--r--.gitignore3
-rw-r--r--Makefile.am2
-rw-r--r--catbird/math.scm127
-rw-r--r--tests/math.scm99
4 files changed, 231 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
index 2891512..32dd253 100644
--- a/.gitignore
+++ b/.gitignore
@@ -10,3 +10,6 @@
/build-aux/
/Makefile
/test-env
+/tests/*.log
+/tests/*.trs
+/test-suite.log
diff --git a/Makefile.am b/Makefile.am
index 5351101..baf499c 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -39,6 +39,7 @@ godir=$(libdir)/guile/$(GUILE_EFFECTIVE_VERSION)/site-ccache
SOURCES = \
catbird/config.scm \
+ catbird/math.scm \
catbird/inotify.scm \
catbird/ring-buffer.scm \
catbird/mixins.scm \
@@ -61,6 +62,7 @@ SOURCES = \
catbird.scm
TESTS = \
+ tests/math.scm \
tests/scene.scm
TEST_EXTENSIONS = .scm
diff --git a/catbird/math.scm b/catbird/math.scm
new file mode 100644
index 0000000..cca718d
--- /dev/null
+++ b/catbird/math.scm
@@ -0,0 +1,127 @@
+;;; Catbird Game Engine
+;;; Copyright © 2023 David Thompson <davet@gnu.org>
+;;;
+;;; Catbird 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.
+;;;
+;;; Catbird 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 Catbird. If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+;;
+;; Generic math methods.
+;;
+;;; Code:
+(define-module (catbird math)
+ #:use-module (chickadee math matrix)
+ #:use-module (chickadee math vector)
+ #:use-module (oop goops)
+ #:export (<vec2>
+ <vec3>
+ <matrix3>
+ <matrix4>
+ vec))
+
+(define <vec2> (class-of (vec2 0.0 0.0)))
+(define <vec3> (class-of (vec3 0.0 0.0 0.0)))
+(define <matrix3> (class-of (make-identity-matrix3)))
+(define <matrix4> (class-of (make-identity-matrix4)))
+
+(define-method (vec (x <number>) (y <number>))
+ (vec2 x y))
+
+(define-method (vec (x <number>) (y <number>) (z <number>))
+ (vec3 x y z))
+
+(define-method (vec (v <vec2>) (z <number>))
+ (vec3 (vec2-x v) (vec2-y v) z))
+
+(define-method (vec (x <number>) (v <vec2>))
+ (vec3 x (vec2-x v) (vec2-y v)))
+
+(define-method (+ (a <vec2>) (b <vec2>))
+ (vec2+ a b))
+
+(define-method (+ (a <vec2>) (b <number>))
+ (vec2+ a b))
+
+(define-method (+ (a <number>) (b <vec2>))
+ (vec2+ b a))
+
+(define-method (+ (a <vec3>) (b <vec3>))
+ (vec3+ a b))
+
+(define-method (+ (a <vec3>) (b <number>))
+ (vec3+ a b))
+
+(define-method (+ (a <number>) (b <vec3>))
+ (vec3+ b a))
+
+(define-method (- (a <vec2>) (b <vec2>))
+ (vec2- a b))
+
+(define-method (- (a <vec2>) (b <number>))
+ (vec2- a b))
+
+(define-method (- (a <vec3>) (b <vec3>))
+ (vec3- a b))
+
+(define-method (- (a <vec3>) (b <number>))
+ (vec3- a b))
+
+(define-method (* (a <vec2>) (b <vec2>))
+ (vec2* a b))
+
+(define-method (* (a <vec2>) (b <number>))
+ (vec2* a b))
+
+(define-method (* (a <number>) (b <vec2>))
+ (vec2* b a))
+
+(define-method (* (a <vec3>) (b <vec3>))
+ (vec3* a b))
+
+(define-method (* (a <vec3>) (b <number>))
+ (vec3* a b))
+
+(define-method (* (a <number>) (b <vec3>))
+ (vec3* b a))
+
+(define-method (* (a <matrix3>) (b <matrix3>))
+ (matrix3* a b))
+
+(define-method (* (a <matrix4>) (b <matrix4>))
+ (matrix4* a b))
+
+(define-method (/ (a <vec2>) (b <vec2>))
+ (vec2* a (vec2 (/ 1.0 (vec2-x b))
+ (/ 1.0 (vec2-y b)))))
+
+(define-method (/ (a <vec2>) (b <number>))
+ (vec2* a (/ 1.0 b)))
+
+(define-method (/ (a <number>) (b <vec2>))
+ (vec2* (vec2 (/ 1.0 (vec2-x b))
+ (/ 1.0 (vec2-y b)))
+ b))
+
+(define-method (/ (a <vec3>) (b <vec3>))
+ (vec3* a (vec3 (/ 1.0 (vec3-x b))
+ (/ 1.0 (vec3-y b))
+ (/ 1.0 (vec3-z b)))))
+
+(define-method (/ (a <vec3>) (b <number>))
+ (vec3* a (/ 1.0 b)))
+
+(define-method (/ (a <number>) (b <vec3>))
+ (vec3* (vec3 (/ 1.0 (vec3-x b))
+ (/ 1.0 (vec3-y b))
+ (/ 1.0 (vec3-z b)))
+ b))
diff --git a/tests/math.scm b/tests/math.scm
new file mode 100644
index 0000000..916bca3
--- /dev/null
+++ b/tests/math.scm
@@ -0,0 +1,99 @@
+;;; Catbird Game Engine
+;;; Copyright © 2023 David Thompson <davet@gnu.org>
+;;;
+;;; Catbird 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.
+;;;
+;;; Catbird 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 Catbird. If not, see <http://www.gnu.org/licenses/>.
+(define-module (tests math)
+ #:use-module (catbird math)
+ #:use-module (chickadee math vector)
+ #:use-module (srfi srfi-64)
+ #:use-module (tests utils))
+
+(with-tests "math"
+ (test-group "vec"
+ (test-equal "with 2 numbers"
+ (vec2 1.0 2.0)
+ (vec 1.0 2.0))
+ (test-equal "with 3 numbers"
+ (vec3 1.0 2.0 3.0)
+ (vec 1.0 2.0 3.0))
+ (test-equal "with a number and a vec2"
+ (vec3 1.0 2.0 3.0)
+ (vec 1.0 (vec 2.0 3.0)))
+ (test-equal "with a vec2 and a number"
+ (vec3 1.0 2.0 3.0)
+ (vec (vec 1.0 2.0) 3.0)))
+ (test-group "+"
+ (test-equal "with 2 vec2s"
+ (vec2 4.0 6.0)
+ (+ (vec 1.0 2.0) (vec 3.0 4.0)))
+ (test-equal "with a vec2 and a number"
+ (vec2 4.0 5.0)
+ (+ (vec 1.0 2.0) 3.0))
+ (test-equal "with a number and a vec2"
+ (vec2 4.0 5.0)
+ (+ 3.0 (vec 1.0 2.0)))
+ (test-equal "with 2 vec3s"
+ (vec3 5.0 7.0 9.0)
+ (+ (vec 1.0 2.0 3.0) (vec 4.0 5.0 6.0)))
+ (test-equal "with a vec3 and a number"
+ (vec3 5.0 6.0 7.0)
+ (+ (vec 1.0 2.0 3.0) 4.0))
+ (test-equal "with a number and a vec3"
+ (vec3 5.0 6.0 7.0)
+ (+ 4.0 (vec 1.0 2.0 3.0))))
+ (test-group "-"
+ (test-equal "with 2 vec2s"
+ (vec2 2.0 1.0)
+ (- (vec 3.0 2.0) (vec 1.0 1.0)))
+ (test-equal "with a vec2 and a number"
+ (vec2 2.0 1.0)
+ (- (vec 3.0 2.0) 1.0))
+ (test-equal "with 2 vec3s"
+ (vec3 3.0 2.0 1.0)
+ (- (vec 4.0 3.0 2.0) (vec 1.0 1.0 1.0)))
+ (test-equal "with a vec3 and a number"
+ (vec3 3.0 2.0 1.0)
+ (- (vec 4.0 3.0 2.0) 1.0)))
+ (test-group "*"
+ (test-equal "with 2 vec2s"
+ (vec2 3.0 8.0)
+ (* (vec 1.0 2.0) (vec 3.0 4.0)))
+ (test-equal "with a vec2 and a number"
+ (vec2 3.0 6.0)
+ (* (vec 1.0 2.0) 3.0))
+ (test-equal "with a number and a vec2"
+ (vec2 3.0 6.0)
+ (* 3.0 (vec 1.0 2.0)))
+ (test-equal "with 2 vec3s"
+ (vec3 4.0 10.0 18.0)
+ (* (vec 1.0 2.0 3.0) (vec 4.0 5.0 6.0)))
+ (test-equal "with a vec3 and a number"
+ (vec3 4.0 8.0 12.0)
+ (* (vec 1.0 2.0 3.0) 4.0))
+ (test-equal "with a number and a vec3"
+ (vec3 4.0 8.0 12.0)
+ (* 4.0 (vec 1.0 2.0 3.0))))
+ (test-group "/"
+ (test-equal "with 2 vec2s"
+ (vec2 2.0 3.0)
+ (/ (vec 4.0 9.0) (vec 2.0 3.0)))
+ (test-equal "with a vec2 and a number"
+ (vec2 2.0 4.0)
+ (/ (vec 4.0 8.0) 2.0))
+ (test-equal "with 2 vec3s"
+ (vec3 2.0 3.0 4.0)
+ (/ (vec 4.0 9.0 16.0) (vec 2.0 3.0 4.0)))
+ (test-equal "with a vec3 and a number"
+ (vec3 2.0 4.0 6.0)
+ (/ (vec 4.0 8.0 12.0) 2.0))))