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