]> git.mxchange.org Git - simgear.git/blob - Lib/Math/mat3.h
Fixed an IRIX warning message where an inline function is referenced
[simgear.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 #if defined(i386)
41 #define USE_X86_ASM
42 #endif
43
44 #if defined(USE_X86_ASM)
45 static __inline__ int FloatToInt(float f)
46 {
47    int r;
48    __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
49    return r;
50 }
51 #elif  defined(__MSC__) && defined(__WIN32__)
52 static __inline int FloatToInt(float f)
53 {
54    int r;
55    _asm {
56      fld f
57      fistp r
58     }
59    return r;
60 }
61 #else
62 #define FloatToInt(F) ((int) ((F) < 0.0f ? (F)-0.5f : (F)+0.5f))
63 #endif
64
65 /* ------------------------------  Types  --------------------------------- */
66
67 typedef double MAT3mat[4][4];           /* 4x4 matrix                    */
68 typedef double MAT3vec[3];              /* Vector                        */
69 typedef double MAT3hvec[4];             /* Vector with homogeneous coord */
70
71 /* ------------------------------  Macros  -------------------------------- */
72
73 extern MAT3mat identityMatrix;
74
75 /* Tests if a number is within EPSILON of zero */
76 #define MAT3_IS_ZERO(N)         ((N) < MAT3_EPSILON && (N) > -MAT3_EPSILON)
77
78 /* Sets a vector to the three given values */
79 #define MAT3_SET_VEC(V,X,Y,Z)   ((V)[0]=(X), (V)[1]=(Y), (V)[2]=(Z))
80
81 /* Tests a vector for all components close to zero */
82 #define MAT3_IS_ZERO_VEC(V)     (MAT3_IS_ZERO((V)[0]) && \
83                                  MAT3_IS_ZERO((V)[1]) && \
84                                  MAT3_IS_ZERO((V)[2]))
85
86 /* Dot product of two vectors */
87 #define MAT3_DOT_PRODUCT(V1,V2) \
88                         ((V1)[0]*(V2)[0] + (V1)[1]*(V2)[1] + (V1)[2]*(V2)[2])
89
90 /* Copy one vector to other */
91 #define MAT3_COPY_VEC(TO,FROM)  ((TO)[0] = (FROM)[0], \
92                                  (TO)[1] = (FROM)[1], \
93                                  (TO)[2] = (FROM)[2])
94
95 /* Normalize vector to unit length, using TEMP as temporary variable.
96  * TEMP will be zero if vector has zero length */
97 #define MAT3_NORMALIZE_VEC(V,TEMP) \
98         if ((TEMP = sqrt(MAT3_DOT_PRODUCT(V,V))) > MAT3_EPSILON) { \
99            TEMP = 1.0 / TEMP; \
100            MAT3_SCALE_VEC(V,V,TEMP); \
101         } else TEMP = 0.0
102
103 /* Scale vector by given factor, storing result vector in RESULT_V */
104 #define MAT3_SCALE_VEC(RESULT_V,V,SCALE) \
105         MAT3_SET_VEC(RESULT_V, (V)[0]*(SCALE), (V)[1]*(SCALE), (V)[2]*(SCALE))
106
107 /* Adds vectors V1 and V2, storing result in RESULT_V */
108 #define MAT3_ADD_VEC(RESULT_V,V1,V2) \
109         MAT3_SET_VEC(RESULT_V, (V1)[0]+(V2)[0], (V1)[1]+(V2)[1], \
110                                (V1)[2]+(V2)[2])
111
112 /* Subtracts vector V2 from V1, storing result in RESULT_V */
113 #define MAT3_SUB_VEC(RESULT_V,V1,V2) \
114         MAT3_SET_VEC(RESULT_V, (V1)[0]-(V2)[0], (V1)[1]-(V2)[1], \
115                                (V1)[2]-(V2)[2])
116
117 /* Multiplies vectors V1 and V2, storing result in RESULT_V */
118 #define MAT3_MULT_VEC(RESULT_V,V1,V2) \
119         MAT3_SET_VEC(RESULT_V, (V1)[0]*(V2)[0], (V1)[1]*(V2)[1], \
120                                (V1)[2]*(V2)[2])
121
122 /* Sets RESULT_V to the linear combination of V1 and V2, scaled by
123  * SCALE1 and SCALE2, respectively */
124 #define MAT3_LINEAR_COMB(RESULT_V,SCALE1,V1,SCALE2,V2) \
125         MAT3_SET_VEC(RESULT_V,  (SCALE1)*(V1)[0] + (SCALE2)*(V2)[0], \
126                                 (SCALE1)*(V1)[1] + (SCALE2)*(V2)[1], \
127                                 (SCALE1)*(V1)[2] + (SCALE2)*(V2)[2])
128
129 /* Several of the vector macros are useful for homogeneous-coord vectors */
130 #define MAT3_SET_HVEC(V,X,Y,Z,W) ((V)[0]=(X), (V)[1]=(Y), \
131                                   (V)[2]=(Z), (V)[3]=(W))
132
133 #define MAT3_COPY_HVEC(TO,FROM) ((TO)[0] = (FROM)[0], \
134                                  (TO)[1] = (FROM)[1], \
135                                  (TO)[2] = (FROM)[2], \
136                                  (TO)[3] = (FROM)[3])
137
138 #define MAT3_SCALE_HVEC(RESULT_V,V,SCALE) \
139         MAT3_SET_HVEC(RESULT_V, (V)[0]*(SCALE), (V)[1]*(SCALE), \
140                                 (V)[2]*(SCALE), (V)[3]*(SCALE))
141
142 #define MAT3_ADD_HVEC(RESULT_V,V1,V2) \
143         MAT3_SET_HVEC(RESULT_V, (V1)[0]+(V2)[0], (V1)[1]+(V2)[1], \
144                                 (V1)[2]+(V2)[2], (V1)[3]+(V2)[3])
145
146 #define MAT3_SUB_HVEC(RESULT_V,V1,V2) \
147         MAT3_SET_HVEC(RESULT_V, (V1)[0]-(V2)[0], (V1)[1]-(V2)[1], \
148                                 (V1)[2]-(V2)[2], (V1)[3]-(V2)[3])
149
150 #define MAT3_MULT_HVEC(RESULT_V,V1,V2) \
151         MAT3_SET_HVEC(RESULT_V, (V1)[0]*(V2)[0], (V1)[1]*(V2)[1], \
152                                 (V1)[2]*(V2)[2], (V1)[3]*(V2)[3])
153
154 /* ------------------------------  Entries  ------------------------------- */
155
156
157 #define MAT3identity(mat)    fgmemcpy( mat, identityMatrix, sizeof(MAT3mat) )
158 #define MAT3zero(mat)        fgmemzero( mat, sizeof(MAT3mat) )
159 #define MAT3copy(to, from)   fgmemcpy( to, from, sizeof(MAT3mat) )
160
161 #if defined( USE_XTRA_MAT3_INLINES )
162
163 #  define MAT3mult_vec( result_vec, vec, mat) { \
164    MAT3vec tempvec; \
165    tempvec[0]=vec[0]*mat[0][0]+vec[1]*mat[1][0]+vec[2]*mat[2][0]+mat[3][0]; \
166    tempvec[1]=vec[0]*mat[0][1]+vec[1]*mat[1][1]+vec[2]*mat[2][1]+mat[3][1]; \
167    tempvec[2]=vec[0]*mat[0][2]+vec[1]*mat[1][2]+vec[2]*mat[2][2]+mat[3][2]; \
168    result_vec[0] = tempvec[0]; \
169    result_vec[1] = tempvec[1]; \
170    result_vec[2] = tempvec[2]; \
171 }
172
173 #  define MAT3cross_product(result_vec, vec1, vec2)  { \
174    MAT3vec tempvec; \
175    tempvec[0] = vec1[1] * vec2[2] - vec1[2] * vec2[1]; \
176    tempvec[1] = vec1[2] * vec2[0] - vec1[0] * vec2[2]; \
177    tempvec[2] = vec1[0] * vec2[1] - vec1[1] * vec2[0]; \
178    result_vec[0] = tempvec[0]; \
179    result_vec[1] = tempvec[1]; \
180    result_vec[2] = tempvec[2]; \
181
182
183 #  define MAT3mult( result_mat,  mat1,  mat2) { \
184    register int i, j; \
185    MAT3mat      tmp_mat; \
186    for (i = 0; i < 4; i++) \
187       for (j = 0; j < 4; j++) \
188          tmp_mat[i][j] = (mat1[i][0] * mat2[0][j] + \
189                           mat1[i][1] * mat2[1][j] + \
190                           mat1[i][2] * mat2[2][j] + \
191                           mat1[i][3] * mat2[3][j]); \
192    fgmemcpy(result_mat, tmp_mat, sizeof(MAT3mat)); \
193 }
194
195 #else // !defined( USE_XTRA_MAT3_INLINES )
196
197 /* In MAT3mat.c */
198 void    MAT3mult(MAT3mat result, MAT3mat, MAT3mat);
199 void    MAT3mult_vec(MAT3vec result_vec, MAT3vec vec, MAT3mat mat);
200 void    MAT3cross_product(MAT3vec result,MAT3vec,MAT3vec);
201
202 #endif // defined( USE_XTRA_MAT3_INLINES )
203
204 /* In MAT3geom.c */
205 void    MAT3direction_matrix (MAT3mat result_mat, MAT3mat mat);
206 int     MAT3normal_matrix (MAT3mat result_mat, MAT3mat mat);
207 void    MAT3rotate (MAT3mat result_mat, MAT3vec axis, double angle_in_radians);
208 void    MAT3translate (MAT3mat result_mat, MAT3vec trans);
209 void    MAT3scale (MAT3mat result_mat, MAT3vec scale);
210 void    MAT3shear(MAT3mat result_mat, double xshear, double yshear);
211
212 void    MAT3transpose (MAT3mat result, MAT3mat);
213 int     MAT3invert (MAT3mat result, MAT3mat);
214 void    MAT3print (MAT3mat, FILE *fp);
215 void    MAT3print_formatted (MAT3mat, FILE *fp, 
216                              char *title, char *head, char *format, char *tail);
217 int     MAT3equal( void );
218 double  MAT3trace( void );
219 int     MAT3power( void );
220 int     MAT3column_reduce( void );
221 int     MAT3kernel_basis( void );
222
223 /* In MAT3vec.c */
224 int     MAT3mult_hvec (MAT3hvec result_vec, MAT3hvec vec, MAT3mat mat, int normalize);
225 void    MAT3perp_vec(MAT3vec result_vec, MAT3vec vec, int is_unit);
226
227 #ifdef __cplusplus
228 }
229 #endif
230
231
232 #endif /* MAT3_HAS_BEEN_INCLUDED */
233