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