From b64e503ffd72bbbce4d9e299f4d96c7448172875 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Wed, 22 Mar 2023 18:46:01 -0400 Subject: Add generic math module. --- .gitignore | 3 ++ Makefile.am | 2 + catbird/math.scm | 127 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/math.scm | 99 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 231 insertions(+) create mode 100644 catbird/math.scm create mode 100644 tests/math.scm 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 +;;; +;;; 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 . + +;;; Commentary: +;; +;; Generic math methods. +;; +;;; Code: +(define-module (catbird math) + #:use-module (chickadee math matrix) + #:use-module (chickadee math vector) + #:use-module (oop goops) + #:export ( + + + + vec)) + +(define (class-of (vec2 0.0 0.0))) +(define (class-of (vec3 0.0 0.0 0.0))) +(define (class-of (make-identity-matrix3))) +(define (class-of (make-identity-matrix4))) + +(define-method (vec (x ) (y )) + (vec2 x y)) + +(define-method (vec (x ) (y ) (z )) + (vec3 x y z)) + +(define-method (vec (v ) (z )) + (vec3 (vec2-x v) (vec2-y v) z)) + +(define-method (vec (x ) (v )) + (vec3 x (vec2-x v) (vec2-y v))) + +(define-method (+ (a ) (b )) + (vec2+ a b)) + +(define-method (+ (a ) (b )) + (vec2+ a b)) + +(define-method (+ (a ) (b )) + (vec2+ b a)) + +(define-method (+ (a ) (b )) + (vec3+ a b)) + +(define-method (+ (a ) (b )) + (vec3+ a b)) + +(define-method (+ (a ) (b )) + (vec3+ b a)) + +(define-method (- (a ) (b )) + (vec2- a b)) + +(define-method (- (a ) (b )) + (vec2- a b)) + +(define-method (- (a ) (b )) + (vec3- a b)) + +(define-method (- (a ) (b )) + (vec3- a b)) + +(define-method (* (a ) (b )) + (vec2* a b)) + +(define-method (* (a ) (b )) + (vec2* a b)) + +(define-method (* (a ) (b )) + (vec2* b a)) + +(define-method (* (a ) (b )) + (vec3* a b)) + +(define-method (* (a ) (b )) + (vec3* a b)) + +(define-method (* (a ) (b )) + (vec3* b a)) + +(define-method (* (a ) (b )) + (matrix3* a b)) + +(define-method (* (a ) (b )) + (matrix4* a b)) + +(define-method (/ (a ) (b )) + (vec2* a (vec2 (/ 1.0 (vec2-x b)) + (/ 1.0 (vec2-y b))))) + +(define-method (/ (a ) (b )) + (vec2* a (/ 1.0 b))) + +(define-method (/ (a ) (b )) + (vec2* (vec2 (/ 1.0 (vec2-x b)) + (/ 1.0 (vec2-y b))) + b)) + +(define-method (/ (a ) (b )) + (vec3* a (vec3 (/ 1.0 (vec3-x b)) + (/ 1.0 (vec3-y b)) + (/ 1.0 (vec3-z b))))) + +(define-method (/ (a ) (b )) + (vec3* a (/ 1.0 b))) + +(define-method (/ (a ) (b )) + (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 +;;; +;;; 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 . +(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)))) -- cgit v1.2.3