1 /* Copyright 1988, Brown Computer Graphics Group. All Rights Reserved. */
3 /* -------------------------------------------------------------------------
4 Public MAT3 include file
5 ------------------------------------------------------------------------- */
7 #ifndef MAT3_HAS_BEEN_INCLUDED
8 #define MAT3_HAS_BEEN_INCLUDED
10 /* ----------------------------- Constants ------------------------------ */
13 * Make sure the math library .h file is included, in case it wasn't.
22 #include <simgear/fg_memory.h>
29 #define MAT3_DET0 -1 /* Indicates singular mat */
30 #define MAT3_EPSILON 1e-12 /* Close enough to zero */
35 # define MAT3_PI 3.14159265358979323846
38 #define USE_XTRA_MAT3_INLINES
44 #if defined(USE_X86_ASM)
45 static __inline__ int FloatToInt(float f)
48 __asm__ ("fistpl %0" : "=m" (r) : "t" (f) : "st");
51 #elif defined(__MSC__) && defined(__WIN32__)
52 static __inline int FloatToInt(float f)
62 #define FloatToInt(F) ((int) ((F) < 0.0f ? (F)-0.5f : (F)+0.5f))
65 /* ------------------------------ Types --------------------------------- */
67 typedef double MAT3mat[4][4]; /* 4x4 matrix */
68 typedef double MAT3vec[3]; /* Vector */
69 typedef double MAT3hvec[4]; /* Vector with homogeneous coord */
71 /* ------------------------------ Macros -------------------------------- */
73 extern MAT3mat identityMatrix;
75 /* Tests if a number is within EPSILON of zero */
76 #define MAT3_IS_ZERO(N) ((N) < MAT3_EPSILON && (N) > -MAT3_EPSILON)
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))
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]) && \
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])
90 /* Copy one vector to other */
91 #define MAT3_COPY_VEC(TO,FROM) ((TO)[0] = (FROM)[0], \
92 (TO)[1] = (FROM)[1], \
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) { \
100 MAT3_SCALE_VEC(V,V,TEMP); \
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))
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], \
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], \
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], \
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])
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))
133 #define MAT3_COPY_HVEC(TO,FROM) ((TO)[0] = (FROM)[0], \
134 (TO)[1] = (FROM)[1], \
135 (TO)[2] = (FROM)[2], \
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))
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])
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])
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])
154 /* ------------------------------ Entries ------------------------------- */
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) )
161 #if defined( USE_XTRA_MAT3_INLINES )
163 # define MAT3mult_vec( result_vec, vec, mat) { \
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]; \
173 # define MAT3cross_product(result_vec, vec1, vec2) { \
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]; \
183 # define MAT3mult( result_mat, mat1, mat2) { \
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)); \
195 #else // !defined( USE_XTRA_MAT3_INLINES )
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);
202 #endif // defined( USE_XTRA_MAT3_INLINES )
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);
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 );
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);
232 #endif /* MAT3_HAS_BEEN_INCLUDED */