00001
00002 #include "StdAfx.h"
00003 #include "matrix.h"
00004
00005 #define EPSILON 0.000001
00006
00007
00008
00009
00010
00011
00012 Matrix4x4::Matrix4x4() {
00013
00014 for(int i = 0; i < 4; i++) {
00015 for(int j=0;j < 4; j++) {
00016 mat[i][j] = 0.0;
00017 }
00018 }
00019 }
00020
00021 Matrix4x4::Matrix4x4(Matrix4x4 *matrix) {
00022 for(int i =0;i <4;i++)
00023 for(int j=0;j <4; j++)
00024 mat[i][j] = matrix->mat[i][j];
00025 }
00026
00027 Matrix4x4::Matrix4x4(float m11, float m12, float m13, float m14,
00028 float m21, float m22, float m23, float m24,
00029 float m31, float m32, float m33, float m34,
00030 float m41, float m42, float m43, float m44) {
00031
00032 mat[0][0] = m11;
00033 mat[0][1] = m21;
00034 mat[0][2] = m31;
00035 mat[0][3] = m41;
00036 mat[1][0] = m12;
00037 mat[1][1] = m22;
00038 mat[1][2] = m32;
00039 mat[1][3] = m42;
00040 mat[2][0] = m13;
00041 mat[2][1] = m23;
00042 mat[2][2] = m33;
00043 mat[2][3] = m43;
00044 mat[3][0] = m14;
00045 mat[3][1] = m24;
00046 mat[3][2] = m34;
00047 mat[3][3] = m44;
00048
00049 }
00050
00051 void
00052 Matrix4x4::identity() {
00053 for(int i =0; i < 4; i++)
00054 for(int j= 0;j < 4; j++)
00055 mat[i][j] = 0.0;
00056 mat[0][0] = 1.0;
00057 mat[1][1] = 1.0;
00058 mat[2][2] = 1.0;
00059 mat[3][3] = 1.0;
00060 }
00061
00062 const VECTOR
00063 Matrix4x4::operator * (const VECTOR& vec) const {
00064 VECTOR result;
00065 result.x = mat[0][0]*vec.x + mat[1][0]*vec.y + mat[2][0] * vec.z + mat[3][0];
00066 result.y = mat[0][1]*vec.x + mat[1][1]*vec.y + mat[2][1] * vec.z + mat[3][1];
00067 result.z = mat[0][2]*vec.x + mat[1][2]*vec.y + mat[2][2] * vec.z + mat[3][2];
00068 return result;
00069 }
00070
00071 void
00072 Matrix4x4::transpose() {
00073 float tmp;
00074 for(int i = 0; i < 4; i++) {
00075 for(int j = 0; j < 4; j++) {
00076 tmp = mat[i][j];
00077 mat[i][j] = mat[j][i];
00078 mat[j][i] = tmp;
00079 }
00080 }
00081 }
00082
00083
00084
00085
00086
00087
00088
00089 void
00090 Matrix4x4::rotation(VECTOR from, VECTOR to) {
00091 VECTOR v;
00092 float e, h, f;
00093
00094 v = from.cross(to);
00095 e = from.dot(to);
00096 f = (e < 0)? -e:e;
00097 if (f > 1.0 - EPSILON) {
00098 VECTOR x;
00099
00100 x.x = (from.x > 0.0) ? from.x : -from.x;
00101 x.y = (from.y > 0.0) ? from.y : -from.y;
00102 x.z = (from.z > 0.0) ? from.z : -from.z;
00103
00104 if(x.x < x.y) {
00105 if(x.x < x.z) {
00106 x.x = 1.0;
00107 x.y = x.z = 0.0;
00108 } else {
00109 x.z = 1.0; x.x = x.y = 0.0;
00110 }
00111 }
00112 else {
00113 if(x.y < x.z) {
00114 x.y = 1.0; x.x = x.z = 0.0;
00115 } else {
00116 x.z = 1.0; x.x = x.y = 0.0;
00117 }
00118 }
00119
00120 VECTOR u = x - from;
00121 VECTOR w = x - to;
00122
00123 float c1 = 2.0 / u.dot(u);
00124 float c2 = 2.0 / w.dot(w);
00125 float c3 = c1 * c2 * u.dot(w);
00126
00127 float u_h[3];
00128 float w_h[3];
00129
00130 u_h[0] = u.x;
00131 u_h[1] = u.y;
00132 u_h[2] = u.z;
00133
00134 w_h[0] = w.x;
00135 w_h[1] = w.y;
00136 w_h[2] = w.z;
00137
00138
00139 for(int i = 0; i < 3; i++) {
00140 for(int j = 0; j < 3; j++) {
00141 mat[i][j] = -c1 * u_h[i] * u_h[j] - c2 * w_h[i]*w_h[j] + c3 * w_h[i] * u_h[j];
00142 }
00143 mat[i][i] += 1.0;
00144 }
00145 }
00146 else
00147 {
00148 h = (1.0 - e)/(v.dot(v));
00149
00150 mat[0][0] = e + h*v.x * v.x;
00151 mat[0][1] = h*v.x*v.y - v.z;
00152 mat[0][2] = h*v.x*v.z + v.y;
00153 mat[0][3] = 0.0f;
00154
00155 mat[1][0] = h*v.x*v.y + v.z;
00156 mat[1][1] = e + h * v.y * v.y;
00157 mat[1][2] = h*v.y*v.z - v.x;
00158 mat[1][3] = 0.0f;
00159
00160 mat[2][0] = h*v.x*v.z - v.y;
00161 mat[2][1] = h*v.y*v.z + v.x;
00162 mat[2][2] = e + h*v.z * v.z;
00163 mat[2][3] = 0.0f;
00164
00165 mat[3][0] = 0.0f;
00166 mat[3][1] = 0.0f;
00167 mat[3][2] = 0.0f;
00168 mat[3][3] = 1.0f;
00169 }
00170 }
00171
00172 void
00173 Matrix4x4::rotateX(float alpha) {
00174 float cosa = cos(alpha);
00175 float sina = sin(alpha);
00176
00177 mat[0][0] = 1.0f;
00178 mat[0][1] = 0.0f;
00179 mat[0][2] = 0.0f;
00180 mat[0][3] = 0.0f;
00181
00182 mat[1][0] = 0.0f;
00183 mat[1][1] = cosa;
00184 mat[1][2] = sina;
00185 mat[1][3] = 0.0f;
00186
00187 mat[2][0] = 1.0f;
00188 mat[2][1] = -sina;
00189 mat[2][2] = cosa;
00190 mat[2][3] = 0.0f;
00191
00192 mat[3][0] = 0.0f;
00193 mat[3][1] = 0.0f;
00194 mat[3][2] = 0.0f;
00195 mat[3][3] = 1.0f;
00196 }
00197
00198 void
00199 Matrix4x4::rotateY(float alpha) {
00200 float cosa = cos(alpha);
00201 float sina = sin(alpha);
00202
00203 mat[0][0] = cosa;
00204 mat[0][1] = 0.0f;
00205 mat[0][2] = -sina;
00206 mat[0][3] = 0.0f;
00207
00208 mat[1][0] = 0.0f;
00209 mat[1][1] = 1.0f;
00210 mat[1][2] = 0.0f;
00211 mat[1][3] = 0.0f;
00212
00213 mat[2][0] = sina;
00214 mat[2][1] = 0.0f;
00215 mat[2][2] = cosa;
00216 mat[2][3] = 0.0f;
00217
00218 mat[3][0] = 0.0f;
00219 mat[3][1] = 0.0f;
00220 mat[3][2] = 0.0f;
00221 mat[3][3] = 1.0f;
00222 }
00223
00224 void
00225 Matrix4x4::rotateZ(float alpha) {
00226 float cosa = cos(alpha);
00227 float sina = sin(alpha);
00228
00229 mat[0][0] = cosa;
00230 mat[0][1] = sina;
00231 mat[0][2] = 0.0f;
00232 mat[0][3] = 0.0f;
00233
00234 mat[1][0] = -sina;
00235 mat[1][1] = cosa;
00236 mat[1][2] = 0.0f;
00237 mat[1][3] = 0.0f;
00238
00239 mat[2][0] = 0.0f;
00240 mat[2][1] = 0.0f;
00241 mat[2][2] = 1.0f;
00242 mat[2][3] = 0.0f;
00243
00244 mat[3][0] = 0.0f;
00245 mat[3][1] = 0.0f;
00246 mat[3][2] = 0.0f;
00247 mat[3][3] = 1.0f;
00248 }