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 #define MAT3_DET0 -1 /* Indicates singular mat */
23 #define MAT3_EPSILON 1e-12 /* Close enough to zero */
24 #define MAT3_PI 3.141592653589793 /* Pi */
26 /* ------------------------------ Types --------------------------------- */
28 typedef double MAT3mat[4][4]; /* 4x4 matrix */
29 typedef double MAT3vec[3]; /* Vector */
30 typedef double MAT3hvec[4]; /* Vector with homogeneous coord */
32 /* ------------------------------ Macros -------------------------------- */
34 /* Tests if a number is within EPSILON of zero */
35 #define MAT3_IS_ZERO(N) ((N) < MAT3_EPSILON && (N) > -MAT3_EPSILON)
37 /* Sets a vector to the three given values */
38 #define MAT3_SET_VEC(V,X,Y,Z) ((V)[0]=(X), (V)[1]=(Y), (V)[2]=(Z))
40 /* Tests a vector for all components close to zero */
41 #define MAT3_IS_ZERO_VEC(V) (MAT3_IS_ZERO((V)[0]) && \
42 MAT3_IS_ZERO((V)[1]) && \
45 /* Dot product of two vectors */
46 #define MAT3_DOT_PRODUCT(V1,V2) \
47 ((V1)[0]*(V2)[0] + (V1)[1]*(V2)[1] + (V1)[2]*(V2)[2])
49 /* Copy one vector to other */
50 #define MAT3_COPY_VEC(TO,FROM) ((TO)[0] = (FROM)[0], \
51 (TO)[1] = (FROM)[1], \
54 /* Normalize vector to unit length, using TEMP as temporary variable.
55 * TEMP will be zero if vector has zero length */
56 #define MAT3_NORMALIZE_VEC(V,TEMP) \
57 if ((TEMP = sqrt(MAT3_DOT_PRODUCT(V,V))) > MAT3_EPSILON) { \
59 MAT3_SCALE_VEC(V,V,TEMP); \
62 /* Scale vector by given factor, storing result vector in RESULT_V */
63 #define MAT3_SCALE_VEC(RESULT_V,V,SCALE) \
64 MAT3_SET_VEC(RESULT_V, (V)[0]*(SCALE), (V)[1]*(SCALE), (V)[2]*(SCALE))
66 /* Adds vectors V1 and V2, storing result in RESULT_V */
67 #define MAT3_ADD_VEC(RESULT_V,V1,V2) \
68 MAT3_SET_VEC(RESULT_V, (V1)[0]+(V2)[0], (V1)[1]+(V2)[1], \
71 /* Subtracts vector V2 from V1, storing result in RESULT_V */
72 #define MAT3_SUB_VEC(RESULT_V,V1,V2) \
73 MAT3_SET_VEC(RESULT_V, (V1)[0]-(V2)[0], (V1)[1]-(V2)[1], \
76 /* Multiplies vectors V1 and V2, storing result in RESULT_V */
77 #define MAT3_MULT_VEC(RESULT_V,V1,V2) \
78 MAT3_SET_VEC(RESULT_V, (V1)[0]*(V2)[0], (V1)[1]*(V2)[1], \
81 /* Sets RESULT_V to the linear combination of V1 and V2, scaled by
82 * SCALE1 and SCALE2, respectively */
83 #define MAT3_LINEAR_COMB(RESULT_V,SCALE1,V1,SCALE2,V2) \
84 MAT3_SET_VEC(RESULT_V, (SCALE1)*(V1)[0] + (SCALE2)*(V2)[0], \
85 (SCALE1)*(V1)[1] + (SCALE2)*(V2)[1], \
86 (SCALE1)*(V1)[2] + (SCALE2)*(V2)[2])
88 /* Several of the vector macros are useful for homogeneous-coord vectors */
89 #define MAT3_SET_HVEC(V,X,Y,Z,W) ((V)[0]=(X), (V)[1]=(Y), \
90 (V)[2]=(Z), (V)[3]=(W))
92 #define MAT3_COPY_HVEC(TO,FROM) ((TO)[0] = (FROM)[0], \
93 (TO)[1] = (FROM)[1], \
94 (TO)[2] = (FROM)[2], \
97 #define MAT3_SCALE_HVEC(RESULT_V,V,SCALE) \
98 MAT3_SET_HVEC(RESULT_V, (V)[0]*(SCALE), (V)[1]*(SCALE), \
99 (V)[2]*(SCALE), (V)[3]*(SCALE))
101 #define MAT3_ADD_HVEC(RESULT_V,V1,V2) \
102 MAT3_SET_HVEC(RESULT_V, (V1)[0]+(V2)[0], (V1)[1]+(V2)[1], \
103 (V1)[2]+(V2)[2], (V1)[3]+(V2)[3])
105 #define MAT3_SUB_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])
109 #define MAT3_MULT_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])
113 /* ------------------------------ Entries ------------------------------- */
117 void MAT3direction_matrix (MAT3mat result_mat, MAT3mat mat);
118 int MAT3normal_matrix (MAT3mat result_mat, MAT3mat mat);
119 void MAT3rotate (MAT3mat result_mat, MAT3vec axis, double angle_in_radians);
120 void MAT3translate (MAT3mat result_mat, MAT3vec trans);
121 void MAT3scale (MAT3mat result_mat, MAT3vec scale);
122 void MAT3shear(MAT3mat result_mat, double xshear, double yshear);
125 void MAT3identity(MAT3mat);
126 void MAT3zero(MAT3mat);
127 void MAT3copy (MAT3mat to, MAT3mat from);
128 void MAT3mult (MAT3mat result, MAT3mat, MAT3mat);
129 void MAT3transpose (MAT3mat result, MAT3mat);
130 int MAT3invert (MAT3mat result, MAT3mat);
131 void MAT3print (MAT3mat, FILE *fp);
132 void MAT3print_formatted (MAT3mat, FILE *fp,
133 char *title, char *head, char *format, char *tail);
134 extern int MAT3equal( void );
135 extern double MAT3trace( void );
136 extern int MAT3power( void );
137 extern int MAT3column_reduce( void );
138 extern int MAT3kernel_basis( void );
141 void MAT3mult_vec(MAT3vec result_vec, MAT3vec vec, MAT3mat mat);
142 int MAT3mult_hvec (MAT3hvec result_vec, MAT3hvec vec, MAT3mat mat, int normalize);
143 void MAT3cross_product(MAT3vec result,MAT3vec,MAT3vec);
144 void MAT3perp_vec(MAT3vec result_vec, MAT3vec vec, int is_unit);
146 #endif /* MAT3_HAS_BEEN_INCLUDED */