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