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