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