]> git.mxchange.org Git - flightgear.git/blob - Lib/Math/mat3.h
Merge FG_Lib as subdirectory
[flightgear.git] / Lib / Math / mat3.h
1 /* Copyright 1988, Brown Computer Graphics Group.  All Rights Reserved. */
2
3 /* -------------------------------------------------------------------------
4                        Public MAT3 include file
5    ------------------------------------------------------------------------- */
6
7 #ifndef MAT3_HAS_BEEN_INCLUDED
8 #define MAT3_HAS_BEEN_INCLUDED
9
10 /* -----------------------------  Constants  ------------------------------ */
11
12 /*
13  * Make sure the math library .h file is included, in case it wasn't.
14  */
15
16 #ifndef HUGE
17 #include <math.h>
18 #endif
19 #include <stdio.h>
20
21 #include <string.h>
22 #include "Include/fg_memory.h"
23
24 #ifdef __cplusplus                                                          
25 extern "C" {                            
26 #endif                                   
27
28
29 #define MAT3_DET0       -1                      /* Indicates singular mat */
30 #define MAT3_EPSILON    1e-12                   /* Close enough to zero   */
31
32 #ifdef M_PI
33 #  define  MAT3_PI    M_PI
34 #else
35 #  define  MAT3_PI    3.14159265358979323846
36 #endif
37
38 #define USE_XTRA_MAT3_INLINES
39
40 /* ------------------------------  Types  --------------------------------- */
41
42 typedef double MAT3mat[4][4];           /* 4x4 matrix                    */
43 typedef double MAT3vec[3];              /* Vector                        */
44 typedef double MAT3hvec[4];             /* Vector with homogeneous coord */
45
46 /* ------------------------------  Macros  -------------------------------- */
47
48 extern MAT3mat identityMatrix;
49
50 /* Tests if a number is within EPSILON of zero */
51 #define MAT3_IS_ZERO(N)         ((N) < MAT3_EPSILON && (N) > -MAT3_EPSILON)
52
53 /* Sets a vector to the three given values */
54 #define MAT3_SET_VEC(V,X,Y,Z)   ((V)[0]=(X), (V)[1]=(Y), (V)[2]=(Z))
55
56 /* Tests a vector for all components close to zero */
57 #define MAT3_IS_ZERO_VEC(V)     (MAT3_IS_ZERO((V)[0]) && \
58                                  MAT3_IS_ZERO((V)[1]) && \
59                                  MAT3_IS_ZERO((V)[2]))
60
61 /* Dot product of two vectors */
62 #define MAT3_DOT_PRODUCT(V1,V2) \
63                         ((V1)[0]*(V2)[0] + (V1)[1]*(V2)[1] + (V1)[2]*(V2)[2])
64
65 /* Copy one vector to other */
66 #define MAT3_COPY_VEC(TO,FROM)  ((TO)[0] = (FROM)[0], \
67                                  (TO)[1] = (FROM)[1], \
68                                  (TO)[2] = (FROM)[2])
69
70 /* Normalize vector to unit length, using TEMP as temporary variable.
71  * TEMP will be zero if vector has zero length */
72 #define MAT3_NORMALIZE_VEC(V,TEMP) \
73         if ((TEMP = sqrt(MAT3_DOT_PRODUCT(V,V))) > MAT3_EPSILON) { \
74            TEMP = 1.0 / TEMP; \
75            MAT3_SCALE_VEC(V,V,TEMP); \
76         } else TEMP = 0.0
77
78 /* Scale vector by given factor, storing result vector in RESULT_V */
79 #define MAT3_SCALE_VEC(RESULT_V,V,SCALE) \
80         MAT3_SET_VEC(RESULT_V, (V)[0]*(SCALE), (V)[1]*(SCALE), (V)[2]*(SCALE))
81
82 /* Adds vectors V1 and V2, storing result in RESULT_V */
83 #define MAT3_ADD_VEC(RESULT_V,V1,V2) \
84         MAT3_SET_VEC(RESULT_V, (V1)[0]+(V2)[0], (V1)[1]+(V2)[1], \
85                                (V1)[2]+(V2)[2])
86
87 /* Subtracts vector V2 from V1, storing result in RESULT_V */
88 #define MAT3_SUB_VEC(RESULT_V,V1,V2) \
89         MAT3_SET_VEC(RESULT_V, (V1)[0]-(V2)[0], (V1)[1]-(V2)[1], \
90                                (V1)[2]-(V2)[2])
91
92 /* Multiplies vectors V1 and V2, storing result in RESULT_V */
93 #define MAT3_MULT_VEC(RESULT_V,V1,V2) \
94         MAT3_SET_VEC(RESULT_V, (V1)[0]*(V2)[0], (V1)[1]*(V2)[1], \
95                                (V1)[2]*(V2)[2])
96
97 /* Sets RESULT_V to the linear combination of V1 and V2, scaled by
98  * SCALE1 and SCALE2, respectively */
99 #define MAT3_LINEAR_COMB(RESULT_V,SCALE1,V1,SCALE2,V2) \
100         MAT3_SET_VEC(RESULT_V,  (SCALE1)*(V1)[0] + (SCALE2)*(V2)[0], \
101                                 (SCALE1)*(V1)[1] + (SCALE2)*(V2)[1], \
102                                 (SCALE1)*(V1)[2] + (SCALE2)*(V2)[2])
103
104 /* Several of the vector macros are useful for homogeneous-coord vectors */
105 #define MAT3_SET_HVEC(V,X,Y,Z,W) ((V)[0]=(X), (V)[1]=(Y), \
106                                   (V)[2]=(Z), (V)[3]=(W))
107
108 #define MAT3_COPY_HVEC(TO,FROM) ((TO)[0] = (FROM)[0], \
109                                  (TO)[1] = (FROM)[1], \
110                                  (TO)[2] = (FROM)[2], \
111                                  (TO)[3] = (FROM)[3])
112
113 #define MAT3_SCALE_HVEC(RESULT_V,V,SCALE) \
114         MAT3_SET_HVEC(RESULT_V, (V)[0]*(SCALE), (V)[1]*(SCALE), \
115                                 (V)[2]*(SCALE), (V)[3]*(SCALE))
116
117 #define MAT3_ADD_HVEC(RESULT_V,V1,V2) \
118         MAT3_SET_HVEC(RESULT_V, (V1)[0]+(V2)[0], (V1)[1]+(V2)[1], \
119                                 (V1)[2]+(V2)[2], (V1)[3]+(V2)[3])
120
121 #define MAT3_SUB_HVEC(RESULT_V,V1,V2) \
122         MAT3_SET_HVEC(RESULT_V, (V1)[0]-(V2)[0], (V1)[1]-(V2)[1], \
123                                 (V1)[2]-(V2)[2], (V1)[3]-(V2)[3])
124
125 #define MAT3_MULT_HVEC(RESULT_V,V1,V2) \
126         MAT3_SET_HVEC(RESULT_V, (V1)[0]*(V2)[0], (V1)[1]*(V2)[1], \
127                                 (V1)[2]*(V2)[2], (V1)[3]*(V2)[3])
128
129 /* ------------------------------  Entries  ------------------------------- */
130
131
132 #define MAT3identity(mat)    fgmemcpy( mat, identityMatrix, sizeof(MAT3mat) )
133 #define MAT3zero(mat)        fgmemzero( mat, sizeof(MAT3mat) )
134 #define MAT3copy(to, from)   fgmemcpy( to, from, sizeof(MAT3mat) )
135
136 #if defined( USE_XTRA_MAT3_INLINES )
137
138 #  define MAT3mult_vec( result_vec, vec, mat) { \
139    MAT3vec tempvec; \
140    tempvec[0]=vec[0]*mat[0][0]+vec[1]*mat[1][0]+vec[2]*mat[2][0]+mat[3][0]; \
141    tempvec[1]=vec[0]*mat[0][1]+vec[1]*mat[1][1]+vec[2]*mat[2][1]+mat[3][1]; \
142    tempvec[2]=vec[0]*mat[0][2]+vec[1]*mat[1][2]+vec[2]*mat[2][2]+mat[3][2]; \
143    result_vec[0] = tempvec[0]; \
144    result_vec[1] = tempvec[1]; \
145    result_vec[2] = tempvec[2]; \
146 }
147
148 #  define MAT3cross_product(result_vec, vec1, vec2)  { \
149    MAT3vec tempvec; \
150    tempvec[0] = vec1[1] * vec2[2] - vec1[2] * vec2[1]; \
151    tempvec[1] = vec1[2] * vec2[0] - vec1[0] * vec2[2]; \
152    tempvec[2] = vec1[0] * vec2[1] - vec1[1] * vec2[0]; \
153    result_vec[0] = tempvec[0]; \
154    result_vec[1] = tempvec[1]; \
155    result_vec[2] = tempvec[2]; \
156
157
158 #  define MAT3mult( result_mat,  mat1,  mat2) { \
159    register int i, j; \
160    MAT3mat      tmp_mat; \
161    for (i = 0; i < 4; i++) \
162       for (j = 0; j < 4; j++) \
163          tmp_mat[i][j] = (mat1[i][0] * mat2[0][j] + \
164                           mat1[i][1] * mat2[1][j] + \
165                           mat1[i][2] * mat2[2][j] + \
166                           mat1[i][3] * mat2[3][j]); \
167    fgmemcpy(result_mat, tmp_mat, sizeof(MAT3mat)); \
168 }
169
170 #else // !defined( USE_XTRA_MAT3_INLINES )
171
172 /* In MAT3mat.c */
173 void    MAT3mult(MAT3mat result, MAT3mat, MAT3mat);
174 void    MAT3mult_vec(MAT3vec result_vec, MAT3vec vec, MAT3mat mat);
175 void    MAT3cross_product(MAT3vec result,MAT3vec,MAT3vec);
176
177 #endif // defined( USE_XTRA_MAT3_INLINES )
178
179 /* In MAT3geom.c */
180 void    MAT3direction_matrix (MAT3mat result_mat, MAT3mat mat);
181 int     MAT3normal_matrix (MAT3mat result_mat, MAT3mat mat);
182 void    MAT3rotate (MAT3mat result_mat, MAT3vec axis, double angle_in_radians);
183 void    MAT3translate (MAT3mat result_mat, MAT3vec trans);
184 void    MAT3scale (MAT3mat result_mat, MAT3vec scale);
185 void    MAT3shear(MAT3mat result_mat, double xshear, double yshear);
186
187 void    MAT3transpose (MAT3mat result, MAT3mat);
188 int     MAT3invert (MAT3mat result, MAT3mat);
189 void    MAT3print (MAT3mat, FILE *fp);
190 void    MAT3print_formatted (MAT3mat, FILE *fp, 
191                              char *title, char *head, char *format, char *tail);
192 int     MAT3equal( void );
193 double  MAT3trace( void );
194 int     MAT3power( void );
195 int     MAT3column_reduce( void );
196 int     MAT3kernel_basis( void );
197
198 /* In MAT3vec.c */
199 int     MAT3mult_hvec (MAT3hvec result_vec, MAT3hvec vec, MAT3mat mat, int normalize);
200 void    MAT3perp_vec(MAT3vec result_vec, MAT3vec vec, int is_unit);
201
202 #ifdef __cplusplus
203 }
204 #endif
205
206
207 #endif /* MAT3_HAS_BEEN_INCLUDED */
208