]> git.mxchange.org Git - flightgear.git/blob - Lib/Math/MAT3mat.c
Updated for 0.6.2
[flightgear.git] / Lib / 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 #    ifdef __BORLANDC__
16 #      include <mem.h>
17 #    else
18 #      include <memory.h>      /* required for memset() and memcpy() */
19 #    endif
20 #  endif
21 #endif
22
23 #include <string.h>
24 #include <Math/mat3defs.h>
25
26 MAT3mat identityMatrix = {
27     { 1.0, 0.0, 0.0, 0.0 },
28     { 0.0, 1.0, 0.0, 0.0 },
29     { 0.0, 0.0, 1.0, 0.0 },
30     { 0.0, 0.0, 0.0, 1.0 }
31 };
32
33 /* #include "macros.h" */
34
35 /* --------------------------  Static Routines  ---------------------------- */
36
37 /* -------------------------  Internal Routines  --------------------------- */
38
39 /* --------------------------  Public Routines  ---------------------------- */
40
41
42 #if !defined( USE_XTRA_MAT3_INLINES )
43
44 /*
45  * This multiplies two matrices, producing a third, which may the same as
46  * either of the first two.
47  */
48
49 void
50 MAT3mult (double (*result_mat)[4], register double (*mat1)[4], register double (*mat2)[4])
51 {
52    register int i, j;
53    MAT3mat      tmp_mat;
54
55    for (i = 0; i < 4; i++)
56       for (j = 0; j < 4; j++)
57          tmp_mat[i][j] = (mat1[i][0] * mat2[0][j] +
58                        mat1[i][1] * mat2[1][j] +
59                        mat1[i][2] * mat2[2][j] +
60                        mat1[i][3] * mat2[3][j]);
61    MAT3copy (result_mat, tmp_mat);
62 }
63 #endif // !defined( USE_XTRA_MAT3_INLINES )
64
65 /*
66  * This returns the transpose of a matrix.  The result matrix may be
67  * the same as the one to transpose.
68  */
69
70 void
71 MAT3transpose (double (*result_mat)[4], register double (*mat)[4])
72 {
73    register int i, j;
74    MAT3mat      tmp_mat;
75
76    for (i = 0; i < 4; i++) 
77       for (j = 0; j < 4; j++) 
78          tmp_mat[i][j] = mat[j][i];
79
80    MAT3copy (result_mat, tmp_mat);
81 }
82
83
84 /*
85  * This prints the given matrix to the given file pointer.
86  */
87
88 void
89 MAT3print(double (*mat)[4], FILE *fp)
90 {
91    MAT3print_formatted(mat, fp, CNULL, CNULL, CNULL, CNULL);
92 }
93
94 /*
95  * This prints the given matrix to the given file pointer.
96  * use the format string to pass to fprintf.  head and tail
97  * are printed at the beginning and end of each line.
98  */
99
100 void
101 MAT3print_formatted(double (*mat)[4], FILE *fp, char *title, char *head, char *format, char *tail)
102 {
103    register int i, j;
104
105    /* This is to allow this to be called easily from a debugger */
106    if (fp == NULL) fp = stderr;
107
108    if (title  == NULL)  title  = "MAT3 matrix:\n";
109    if (head   == NULL)  head   = "  ";
110    if (format == NULL)  format = "%#8.4lf  ";
111    if (tail   == NULL)  tail   = "\n";
112
113    (void) fprintf(fp, title);
114
115    for (i = 0; i < 4; i++) {
116       (void) fprintf(fp, head);
117       for (j = 0; j < 4; j++) (void) fprintf(fp, format, mat[i][j]);
118       (void) fprintf(fp, tail);
119    }
120 }