00001
00008 #include "mat.h"
00009
00011
00018 TVector::TVector(const TVector &start, const TVector &end)
00019 {
00020 x = end.x - start.x;
00021 y = end.y - start.y;
00022 z = end.z - start.z;
00023 w = end.w - start.w;
00024 }
00025
00032 void TVector::Create(const TVector &start, const TVector &end)
00033 {
00034 x = end.x - start.x;
00035 y = end.y - start.y;
00036 z = end.z - start.z;
00037 w = end.w - start.w;
00038 }
00039
00045 float TVector::Lenght()
00046 {
00047 return (float)(sqrt(x*x + y*y + z*z + w*w));
00048 }
00049
00054 void TVector::Normalize()
00055 {
00056 float lenght = Lenght();
00057 if(lenght==0) lenght=1;
00058 x /= lenght;
00059 y /= lenght;
00060 z /= lenght;
00061 w /= lenght;
00062 }
00063
00071 float DotProduct(const TVector &v1, const TVector &v2)
00072 {
00073 return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
00074 }
00075
00083 TVector CrossProduct(const TVector &v1, const TVector &v2)
00084 {
00085 TVector v;
00086 v.x = (v1.y * v2.z) - (v1.z * v2.y);
00087 v.y = (v1.z * v2.x) - (v1.x * v2.z);
00088 v.z = (v1.x * v2.y) - (v1.y * v2.x);
00089 return v;
00090 }
00091
00099 float VecAngle(TVector &v1, TVector &v2)
00100 {
00101 float angle_cos = DotProduct(v1,v2)/( v1.Lenght() * v2.Lenght() );
00102 return acos(angle_cos);
00104
00105 }
00106
00107
00109
00114 TMatrix::TMatrix()
00115 {
00116 for(int i=0; i<16; i++) values[i] = 0.0;
00117 }
00118
00124 TMatrix::TMatrix( float v0, float v1, float v2, float v3,
00125 float v4, float v5, float v6, float v7,
00126 float v8, float v9, float v10, float v11,
00127 float v12, float v13, float v14, float v15)
00128 {
00129 values[0]=v0; values[1]=v1; values[2]=v2; values[3]=v3;
00130 values[4]=v4; values[5]=v5; values[6]=v6; values[7]=v7;
00131 values[8]=v8; values[9]=v9; values[10]=v10; values[11]=v11;
00132 values[12]=v12; values[13]=v13; values[14]=v14; values[15]=v15;
00133 }
00134
00140 TMatrix::TMatrix(const TMatrix &ref)
00141 {
00142 memcpy(values, ref.values, 16*sizeof(float));
00143 }
00144
00151 void TMatrix::GetRow(int position, float *ref) const
00152 {
00153 ref[0] = values[position];
00154 ref[1] = values[4 + position];
00155 ref[2] = values[8 + position];
00156 ref[3] = values[12 + position];
00157 }
00158
00159
00160
00161 void TMatrix::LoadIdentity()
00162 {
00163 values[0]= 1.0; values[1]= 0.0; values[2]= 0.0; values[3]= 0.0;
00164 values[4]= 0.0; values[5]= 1.0; values[6]= 0.0; values[7]= 0.0;
00165 values[8]= 0.0; values[9]= 0.0; values[10]=1.0; values[11]=0.0;
00166 values[12]=0.0; values[13]=0.0; values[14]=0.0; values[15]=1.0;
00167 }
00168
00169
00176 TMatrix TMatrix::operator*(const TMatrix &ref) const
00177 {
00178 TMatrix result;
00179 for(int i = 0; i < 4; i++)
00180 for(int j = 0; j < 4;j++)
00181 {
00182 result.values[4*i+j] = 0;
00183 for (int k = 0; k < 4;k++)
00184 result.values[4*i+j] += ref.values[4*i+k]*values[4*k+j];
00185 }
00186 return result;
00187 }
00188
00196 ostream& operator<<(ostream &os, const TMatrix &m)
00197 {
00198 os <<"| "<<m.values[0]<<", "<<m.values[1]<<", "<<m.values[2]<<", "<<m.values[3]<<"| "<<endl
00199 <<"| "<<m.values[4]<<", "<<m.values[5]<<", "<<m.values[6]<<", "<<m.values[7]<<"| "<<endl
00200 <<"| "<<m.values[8]<<", "<<m.values[9]<<", "<<m.values[10]<<", "<<m.values[11]<<"| "<<endl
00201 <<"| "<<m.values[12]<<", "<<m.values[13]<<", "<<m.values[14]<<", "<<m.values[15]<<"| "<<endl;
00202 return os;
00203 }
00204
00210 TMatrix TMatrix::Inverse()
00211 {
00212 TMatrix m(*this);
00213
00214
00215 for(int i=0;i<3;i++)
00216 for(int j=0;j<i;j++)
00217 std::swap(m.values[4 * i + j], m.values[4 * j + i]);
00218
00219
00220 for(int i=0;i<3;i++)
00221 {
00222 m.values[4 * i + 3] = -m.values[4 * i + 3];
00223 m.values[12 + i] = -m.values[12 + i];
00224 }
00225
00226
00227 for(int i=0;i<3;i++)
00228 {
00229 m.values[4 * i + 3] = 0;
00230 m.values[12 + i] = 0;
00231 }
00232 return m;
00233 }
00234
00235 TMatrix TMatrix::Transpose()
00236 {
00237 TMatrix tmp;
00238
00239
00240
00241
00242
00243
00244 tmp[0][0] = values[0], tmp[0][1] = values[4], tmp[0][2] = values[8], tmp[0][3] = values[12];
00245 tmp[1][0] = values[1], tmp[1][1] = values[5], tmp[1][2] = values[9], tmp[1][3] = values[13];
00246 tmp[2][0] = values[2], tmp[2][1] = values[6], tmp[2][2] = values[10], tmp[2][3] = values[14];
00247 tmp[3][0] = values[3], tmp[3][1] = values[7], tmp[3][2] = values[11], tmp[3][3] = values[15];
00248 return tmp;
00249 }
00250