]> git.mxchange.org Git - simgear.git/blob - Math/MAT3mat.c
Merged in make system changes from Bob Kuehne <rpk@sgi.com>
[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 <Math/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 (double (*result_mat)[4], register double (*mat1)[4], register double (*mat2)[4])
79 {
80    register int i, j;
81    MAT3mat      tmp_mat;
82
83    for (i = 0; i < 4; i++)
84       for (j = 0; j < 4; j++)
85          tmp_mat[i][j] = (mat1[i][0] * mat2[0][j] +
86                        mat1[i][1] * mat2[1][j] +
87                        mat1[i][2] * mat2[2][j] +
88                        mat1[i][3] * mat2[3][j]);
89    MAT3copy (result_mat, tmp_mat);
90 }
91
92 /*
93  * This returns the transpose of a matrix.  The result matrix may be
94  * the same as the one to transpose.
95  */
96
97 void
98 MAT3transpose (double (*result_mat)[4], register double (*mat)[4])
99 {
100    register int i, j;
101    MAT3mat      tmp_mat;
102
103    for (i = 0; i < 4; i++) 
104       for (j = 0; j < 4; j++) 
105          tmp_mat[i][j] = mat[j][i];
106
107    MAT3copy (result_mat, tmp_mat);
108 }
109
110
111 /*
112  * This prints the given matrix to the given file pointer.
113  */
114
115 void
116 MAT3print(double (*mat)[4], FILE *fp)
117 {
118    MAT3print_formatted(mat, fp, CNULL, CNULL, CNULL, CNULL);
119 }
120
121 /*
122  * This prints the given matrix to the given file pointer.
123  * use the format string to pass to fprintf.  head and tail
124  * are printed at the beginning and end of each line.
125  */
126
127 void
128 MAT3print_formatted(double (*mat)[4], FILE *fp, char *title, char *head, char *format, char *tail)
129 {
130    register int i, j;
131
132    /* This is to allow this to be called easily from a debugger */
133    if (fp == NULL) fp = stderr;
134
135    if (title  == NULL)  title  = "MAT3 matrix:\n";
136    if (head   == NULL)  head   = "  ";
137    if (format == NULL)  format = "%#8.4lf  ";
138    if (tail   == NULL)  tail   = "\n";
139
140    (void) fprintf(fp, title);
141
142    for (i = 0; i < 4; i++) {
143       (void) fprintf(fp, head);
144       for (j = 0; j < 4; j++) (void) fprintf(fp, format, mat[i][j]);
145       (void) fprintf(fp, tail);
146    }
147 }