]> git.mxchange.org Git - simgear.git/blob - Math/MAT3mat.c
02518afb5d71ef2231f5d359ff308d1847bb8ffc
[simgear.git] / Math / MAT3mat.c
1 /* #include "HEADERS.h" */
2 /* Copyright 1988, Brown Computer Graphics Group.  All Rights Reserved. */
3
4 /* --------------------------------------------------------------------------
5  * This file contains routines that operate solely on matrices.
6  * -------------------------------------------------------------------------*/
7
8 #include <string.h>
9 #include "mat3defs.h"
10
11 #ifdef WIN32
12 #include <memory.h>      /* required for memset() and memcpy() */
13 #endif
14
15 /* #include "macros.h" */
16
17 /* --------------------------  Static Routines  ---------------------------- */
18
19 /* -------------------------  Internal Routines  --------------------------- */
20
21 /* --------------------------  Public Routines  ---------------------------- */
22
23
24 /*
25  * Sets a matrix to identity.
26  */
27
28 void
29 MAT3identity (register MAT3mat mat)
30 {
31    register int i;
32
33 #ifdef WIN32
34    memset(mat,0x00, sizeof(MAT3mat));
35 #else
36    bzero (mat, sizeof(MAT3mat));
37 #endif
38
39    for (i = 0; i < 4; i++)
40       mat[i][i] = 1.0;
41 }
42
43 /*
44  * Sets a matrix to zero.
45  */
46
47 void
48 MAT3zero (MAT3mat mat)
49 {
50 #ifdef WIN32
51    memset(mat,0x00, sizeof(MAT3mat));
52 #else
53    bzero (mat, sizeof(MAT3mat));
54 #endif
55 }
56
57
58 /*
59  * Copies one matrix to another.
60  */
61
62 void
63 MAT3copy(MAT3mat to, MAT3mat from)
64 {
65 #ifdef WIN32
66     memcpy(to, from, sizeof(MAT3mat));
67 #else
68     bcopy(from, to, sizeof(MAT3mat));
69 #endif
70 }
71
72 /*
73  * This multiplies two matrices, producing a third, which may the same as
74  * either of the first two.
75  */
76
77 void
78 MAT3mult (result_mat, mat1, mat2)
79 MAT3mat result_mat;
80 register MAT3mat mat1, mat2;
81 {
82    register int i, j;
83    MAT3mat      tmp_mat;
84
85    for (i = 0; i < 4; i++)
86       for (j = 0; j < 4; j++)
87          tmp_mat[i][j] = (mat1[i][0] * mat2[0][j] +
88                        mat1[i][1] * mat2[1][j] +
89                        mat1[i][2] * mat2[2][j] +
90                        mat1[i][3] * mat2[3][j]);
91    MAT3copy (result_mat, tmp_mat);
92 }
93
94 /*
95  * This returns the transpose of a matrix.  The result matrix may be
96  * the same as the one to transpose.
97  */
98
99 void
100 MAT3transpose (result_mat, mat)
101 MAT3mat result_mat;
102 register MAT3mat mat;
103 {
104    register int i, j;
105    MAT3mat      tmp_mat;
106
107    for (i = 0; i < 4; i++) 
108       for (j = 0; j < 4; j++) 
109          tmp_mat[i][j] = mat[j][i];
110
111    MAT3copy (result_mat, tmp_mat);
112 }
113
114
115 /*
116  * This prints the given matrix to the given file pointer.
117  */
118
119 void
120 MAT3print(mat, fp)
121 MAT3mat mat;
122 FILE    *fp;
123 {
124    MAT3print_formatted(mat, fp, CNULL, CNULL, CNULL, CNULL);
125 }
126
127 /*
128  * This prints the given matrix to the given file pointer.
129  * use the format string to pass to fprintf.  head and tail
130  * are printed at the beginning and end of each line.
131  */
132
133 void
134 MAT3print_formatted(mat, fp, title, head, format, tail)
135 MAT3mat mat;
136 FILE    *fp;
137 char    *title, *head, *format, *tail;
138 {
139    register int i, j;
140
141    /* This is to allow this to be called easily from a debugger */
142    if (fp == NULL) fp = stderr;
143
144    if (title  == NULL)  title  = "MAT3 matrix:\n";
145    if (head   == NULL)  head   = "  ";
146    if (format == NULL)  format = "%#8.4lf  ";
147    if (tail   == NULL)  tail   = "\n";
148
149    (void) fprintf(fp, title);
150
151    for (i = 0; i < 4; i++) {
152       (void) fprintf(fp, head);
153       for (j = 0; j < 4; j++) (void) fprintf(fp, format, mat[i][j]);
154       (void) fprintf(fp, tail);
155    }
156 }