summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2022-10-24 17:17:50 -0400
committerDavid Thompson <dthompson2@worcester.edu>2022-10-24 17:18:29 -0400
commit4047c0d0a92eae8a7394b4caa479d0cb6dd9017c (patch)
tree2c7a4e6a6b22120dcd9d74a282bbc1c1d12de809
parente8674680c41ae856b5b83b3904caca9728b57fce (diff)
math: matrix: Add copy procedures.
-rw-r--r--chickadee/math/matrix.scm37
-rw-r--r--doc/api.texi27
-rw-r--r--tests/matrix.scm27
3 files changed, 82 insertions, 9 deletions
diff --git a/chickadee/math/matrix.scm b/chickadee/math/matrix.scm
index eab6cc6..e80eab6 100644
--- a/chickadee/math/matrix.scm
+++ b/chickadee/math/matrix.scm
@@ -31,6 +31,9 @@
make-null-matrix3
make-identity-matrix3
matrix3?
+ matrix3=
+ matrix3-copy!
+ matrix3-copy
matrix3-mult!
matrix3*
matrix3-identity!
@@ -49,6 +52,8 @@
make-identity-matrix4
matrix4?
matrix4=
+ matrix4-copy!
+ matrix4-copy
matrix4-mult!
matrix4*
matrix4-identity!
@@ -131,7 +136,7 @@
(matrix3-set! bv 2 2 cc)))
(define (make-null-matrix3)
- (let ((bv (make-f32vector 9)))
+ (let ((bv (make-bytevector 36)))
(%make-matrix3 bv)))
(define (make-matrix3 aa ab ac
@@ -146,6 +151,18 @@ column-major format."
ca cb cc)
matrix))
+(define (matrix3= m1 m2)
+ "Return #t if M1 is the same matrix as M2"
+ (equal? (matrix3-bv m1) (matrix3-bv m2)))
+
+(define (matrix3-copy! src dest)
+ "Copy the contents of matrix SRC to DEST."
+ (bytevector-copy! (matrix3-bv src) 0 (matrix3-bv dest) 0 36))
+
+(define (matrix3-copy matrix)
+ "Return a new 3x3 matrix that is a copy of MATRIX."
+ (%make-matrix3 (bytevector-copy (matrix3-bv matrix))))
+
(define (matrix3-mult! dest a b)
"Multiply matrices A and B, storing the result in DEST."
(let ((m1 (matrix3-bv a))
@@ -206,9 +223,6 @@ column-major format."
(* m1-2-1 m2-1-2)
(* m1-2-2 m2-2-2))))))
-(define (matrix3-copy matrix)
- (%make-matrix3 (bytevector-copy (matrix3-bv matrix))))
-
(define (matrix3* . matrices)
"Return the product of MATRICES."
(match matrices
@@ -407,7 +421,7 @@ column-major format."
(matrix4-set! bv 3 3 dd)))
(define (make-null-matrix4)
- (let ((bv (make-f32vector 16)))
+ (let ((bv (make-bytevector 64)))
(%make-matrix4 bv (bytevector->pointer bv))))
(define (make-matrix4 aa ab ac ad
@@ -428,6 +442,15 @@ column-major format."
"Return #t if M1 is the same matrix as M2"
(equal? (matrix4-bv m1) (matrix4-bv m2)))
+(define (matrix4-copy! src dest)
+ "Copy the contents of matrix SRC to DEST."
+ (bytevector-copy! (matrix4-bv src) 0 (matrix4-bv dest) 0 64))
+
+(define (matrix4-copy matrix)
+ "Return a new 4x4 matrix that is a copy of MATRIX."
+ (let ((bv (bytevector-copy (matrix4-bv matrix))))
+ (%make-matrix4 bv (bytevector->pointer bv))))
+
(define (matrix4-mult! dest a b)
"Multiply matrices A and B, storing the result in DEST."
(let ((m1 (matrix4-bv a))
@@ -546,10 +569,6 @@ column-major format."
(* m1-3-2 m2-2-3)
(* m1-3-3 m2-3-3))))))
-(define (matrix4-copy matrix)
- (let ((bv (bytevector-copy (matrix4-bv matrix))))
- (%make-matrix4 bv (bytevector->pointer bv))))
-
(define (matrix4* . matrices)
"Return the product of MATRICES."
(match matrices
diff --git a/doc/api.texi b/doc/api.texi
index e51a81d..9d743b4 100644
--- a/doc/api.texi
+++ b/doc/api.texi
@@ -1023,6 +1023,14 @@ equivalent to the following code:
Return @code{#t} if @var{obj} is a 3x3 matrix.
@end deffn
+@deffn {Procedure} matrix3= m1 m2
+Return @code{#t} if @var{m1} is the same matrix as @var{m2}.
+@end deffn
+
+@deffn {Procedure} matrix3-copy matrix
+Return a new 3x3 matrix that is a copy of @var{matrix}.
+@end deffn
+
@deffn {Procedure} matrix3* . matrices
Return a new 3x3 matrix containing the product of multiplying all of
the given @var{matrices}.
@@ -1057,6 +1065,10 @@ Return the inverse of @var{matrix}.
The following procedures perform in-place, destructive updates to 3x3
matrix objects:
+@deffn {Procedure} matrix3-copy! src dest
+Copy the contents of matrix @var{src} to @var{dest}.
+@end deffn
+
@deffn {Procedure} matrix3-identity! matrix
Modify @var{matrix} in-place to contain the identity matrix.
@end deffn
@@ -1124,6 +1136,14 @@ equivalent to the following code:
Return @code{#t} if @var{obj} is a 4x4 matrix.
@end deffn
+@deffn {Procedure} matrix4= m1 m2
+Return @code{#t} if @var{m1} is the same matrix as @var{m2}.
+@end deffn
+
+@deffn {Procedure} matrix4-copy matrix
+Return a new 4x4 matrix that is a copy of @var{matrix}.
+@end deffn
+
@deffn {Procedure} matrix4* . matrices
Return a new 4x4 matrix containing the product of multiplying all of
the given @var{matrices}.
@@ -1175,6 +1195,13 @@ Return a new 4x4 matrix that represents a rotation about the Z axis by
@var{theta} radians.
@end deffn
+The following procedures perform in-place, destructive updates to 4x4
+matrix objects:
+
+@deffn {Procedure} matrix4-copy! src dest
+Copy the contents of matrix @var{src} to @var{dest}.
+@end deffn
+
@deffn {Procedure} matrix4-identity! matrix
Modify @var{matrix} in-place to contain the identity matrix.
@end deffn
diff --git a/tests/matrix.scm b/tests/matrix.scm
index 7f36280..0cd24a9 100644
--- a/tests/matrix.scm
+++ b/tests/matrix.scm
@@ -24,7 +24,34 @@
#: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