]> git.mxchange.org Git - simgear.git/blob - Math/mat3.h
Prepairing for C++ integration.
[simgear.git] / 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 #ifdef __cplusplus                                                          
11 extern "C" {                            
12 #endif                                   
13
14 /* -----------------------------  Constants  ------------------------------ */
15
16 /*
17  * Make sure the math library .h file is included, in case it wasn't.
18  */
19
20 #ifndef HUGE
21 #include <math.h>
22 #endif
23 #include <stdio.h>
24
25
26 #define MAT3_DET0       -1                      /* Indicates singular mat */
27 #define MAT3_EPSILON    1e-12                   /* Close enough to zero   */
28 #define MAT3_PI         3.141592653589793       /* Pi                     */
29
30 /* ------------------------------  Types  --------------------------------- */
31
32 typedef double MAT3mat[4][4];           /* 4x4 matrix                    */
33 typedef double MAT3vec[3];              /* Vector                        */
34 typedef double MAT3hvec[4];             /* Vector with homogeneous coord */
35
36 /* ------------------------------  Macros  -------------------------------- */
37
38 /* Tests if a number is within EPSILON of zero */
39 #define MAT3_IS_ZERO(N)         ((N) < MAT3_EPSILON && (N) > -MAT3_EPSILON)
40
41 /* Sets a vector to the three given values */
42 #define MAT3_SET_VEC(V,X,Y,Z)   ((V)[0]=(X), (V)[1]=(Y), (V)[2]=(Z))
43
44 /* Tests a vector for all components close to zero */
45 #define MAT3_IS_ZERO_VEC(V)     (MAT3_IS_ZERO((V)[0]) && \
46                                  MAT3_IS_ZERO((V)[1]) && \
47                                  MAT3_IS_ZERO((V)[2]))
48
49 /* Dot product of two vectors */
50 #define MAT3_DOT_PRODUCT(V1,V2) \
51                         ((V1)[0]*(V2)[0] + (V1)[1]*(V2)[1] + (V1)[2]*(V2)[2])
52
53 /* Copy one vector to other */
54 #define MAT3_COPY_VEC(TO,FROM)  ((TO)[0] = (FROM)[0], \
55                                  (TO)[1] = (FROM)[1], \
56                                  (TO)[2] = (FROM)[2])
57
58 /* Normalize vector to unit length, using TEMP as temporary variable.
59  * TEMP will be zero if vector has zero length */
60 #define MAT3_NORMALIZE_VEC(V,TEMP) \
61         if ((TEMP = sqrt(MAT3_DOT_PRODUCT(V,V))) > MAT3_EPSILON) { \
62            TEMP = 1.0 / TEMP; \
63            MAT3_SCALE_VEC(V,V,TEMP); \
64         } else TEMP = 0.0
65
66 /* Scale vector by given factor, storing result vector in RESULT_V */
67 #define MAT3_SCALE_VEC(RESULT_V,V,SCALE) \
68         MAT3_SET_VEC(RESULT_V, (V)[0]*(SCALE), (V)[1]*(SCALE), (V)[2]*(SCALE))
69
70 /* Adds vectors V1 and V2, storing result in RESULT_V */
71 #define MAT3_ADD_VEC(RESULT_V,V1,V2) \
72         MAT3_SET_VEC(RESULT_V, (V1)[0]+(V2)[0], (V1)[1]+(V2)[1], \
73                                (V1)[2]+(V2)[2])
74
75 /* Subtracts vector V2 from V1, storing result in RESULT_V */
76 #define MAT3_SUB_VEC(RESULT_V,V1,V2) \
77         MAT3_SET_VEC(RESULT_V, (V1)[0]-(V2)[0], (V1)[1]-(V2)[1], \
78                                (V1)[2]-(V2)[2])
79
80 /* Multiplies vectors V1 and V2, storing result in RESULT_V */
81 #define MAT3_MULT_VEC(RESULT_V,V1,V2) \
82         MAT3_SET_VEC(RESULT_V, (V1)[0]*(V2)[0], (V1)[1]*(V2)[1], \
83                                (V1)[2]*(V2)[2])
84
85 /* Sets RESULT_V to the linear combination of V1 and V2, scaled by
86  * SCALE1 and SCALE2, respectively */
87 #define MAT3_LINEAR_COMB(RESULT_V,SCALE1,V1,SCALE2,V2) \
88         MAT3_SET_VEC(RESULT_V,  (SCALE1)*(V1)[0] + (SCALE2)*(V2)[0], \
89                                 (SCALE1)*(V1)[1] + (SCALE2)*(V2)[1], \
90                                 (SCALE1)*(V1)[2] + (SCALE2)*(V2)[2])
91
92 /* Several of the vector macros are useful for homogeneous-coord vectors */
93 #define MAT3_SET_HVEC(V,X,Y,Z,W) ((V)[0]=(X), (V)[1]=(Y), \
94                                   (V)[2]=(Z), (V)[3]=(W))
95
96 #define MAT3_COPY_HVEC(TO,FROM) ((TO)[0] = (FROM)[0], \
97                                  (TO)[1] = (FROM)[1], \
98                                  (TO)[2] = (FROM)[2], \
99                                  (TO)[3] = (FROM)[3])
100
101 #define MAT3_SCALE_HVEC(RESULT_V,V,SCALE) \
102         MAT3_SET_HVEC(RESULT_V, (V)[0]*(SCALE), (V)[1]*(SCALE), \
103                                 (V)[2]*(SCALE), (V)[3]*(SCALE))
104
105 #define MAT3_ADD_HVEC(RESULT_V,V1,V2) \
106         MAT3_SET_HVEC(RESULT_V, (V1)[0]+(V2)[0], (V1)[1]+(V2)[1], \
107                                 (V1)[2]+(V2)[2], (V1)[3]+(V2)[3])
108
109 #define MAT3_SUB_HVEC(RESULT_V,V1,V2) \
110         MAT3_SET_HVEC(RESULT_V, (V1)[0]-(V2)[0], (V1)[1]-(V2)[1], \
111                                 (V1)[2]-(V2)[2], (V1)[3]-(V2)[3])
112
113 #define MAT3_MULT_HVEC(RESULT_V,V1,V2) \
114         MAT3_SET_HVEC(RESULT_V, (V1)[0]*(V2)[0], (V1)[1]*(V2)[1], \
115                                 (V1)[2]*(V2)[2], (V1)[3]*(V2)[3])
116
117 /* ------------------------------  Entries  ------------------------------- */
118
119
120 /* In MAT3geom.c */
121 void            MAT3direction_matrix (MAT3mat result_mat, MAT3mat mat);
122 int             MAT3normal_matrix (MAT3mat result_mat, MAT3mat mat);
123 void            MAT3rotate (MAT3mat result_mat, MAT3vec axis, double angle_in_radians);
124 void            MAT3translate (MAT3mat result_mat, MAT3vec trans);
125 void            MAT3scale (MAT3mat result_mat, MAT3vec scale);
126 void            MAT3shear(MAT3mat result_mat, double xshear, double yshear);
127
128 /* In MAT3mat.c */
129 void            MAT3identity(MAT3mat);
130 void            MAT3zero(MAT3mat);
131 void            MAT3copy (MAT3mat to, MAT3mat from);
132 void            MAT3mult (MAT3mat result, MAT3mat, MAT3mat);
133 void            MAT3transpose (MAT3mat result, MAT3mat);
134 int                     MAT3invert (MAT3mat result, MAT3mat);
135 void            MAT3print (MAT3mat, FILE *fp);
136 void            MAT3print_formatted (MAT3mat, FILE *fp, 
137                                      char *title, char *head, char *format, char *tail);
138 extern int              MAT3equal( void );
139 extern double           MAT3trace( void );
140 extern int              MAT3power( void );
141 extern int              MAT3column_reduce( void );
142 extern int              MAT3kernel_basis( void );
143
144 /* In MAT3vec.c */
145 void            MAT3mult_vec(MAT3vec result_vec, MAT3vec vec, MAT3mat mat);
146 int             MAT3mult_hvec (MAT3hvec result_vec, MAT3hvec vec, MAT3mat mat, int normalize);
147 void            MAT3cross_product(MAT3vec result,MAT3vec,MAT3vec);
148 void            MAT3perp_vec(MAT3vec result_vec, MAT3vec vec, int is_unit);
149
150 #ifdef __cplusplus
151 }
152 #endif
153
154 #endif /* MAT3_HAS_BEEN_INCLUDED */
155