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