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