inlib  1.2.0
/Users/barrand/private/dev/softinex/old/inexlib-1.2/inlib/inlib/mat4f
Go to the documentation of this file.
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
00002 // See the file inlib.license for terms.
00003 
00004 #ifndef inlib_mat4f
00005 #define inlib_mat4f
00006 
00007 #include "a4"
00008 #include "vdata"
00009 //#include "vec3f"
00010 #include "fmath"
00011 
00012 namespace inlib {
00013 
00014 class mat4f : public inlib::a4::sqm<float> {
00015 public:
00016   mat4f(): inlib::a4::sqm<float>() {}
00017   virtual ~mat4f() {}
00018 public:
00019   mat4f(const mat4f& a_from): inlib::a4::sqm<float>(a_from){}
00020   mat4f& operator=(const mat4f& a_from){
00021     inlib::a4::sqm<float>::operator=(a_from);
00022     return *this;
00023   }
00024 public: //operators
00025   //mat4f& operator*=(const mat4f& a_from) {
00026   //  inlib::a4::sqm<float>::operator*=(a_from);
00027   //  return *this;
00028   //}
00029   //bool operator==(const mat4f& a_v) const {return equal(a_v);}
00030   //bool operator!=(const mat4f& a_v) const {return !operator==(a_v);}
00031 /*
00032   vec3f operator*(const vec3f& a_in) const {
00033     float in0 = a_in[0];
00034     float in1 = a_in[1];
00035     float in2 = a_in[2];
00036     //m_vector[R + C * 4];
00037     const std::vector<float>& vec = inlib::array<float>::m_vector;
00038     //vec[0] = 00;vec[4] = 01;vec[ 8] = 02;vec[12] = 03;
00039     //vec[1] = 10;vec[5] = 11;vec[ 9] = 12;vec[13] = 13;
00040     //vec[2] = 20;vec[6] = 21;vec[10] = 22;vec[14] = 23;
00041     //vec[3] = 30;vec[7] = 31;vec[11] = 32;vec[15] = 33;
00042     vec3f out;
00043     float v0 = vec[0]*in0+vec[4]*in1+vec[ 8]*in2+vec[12];
00044     float v1 = vec[1]*in0+vec[5]*in1+vec[ 9]*in2+vec[13];
00045     float v2 = vec[2]*in0+vec[6]*in1+vec[10]*in2+vec[14];
00046     //float v3 = vec[3]*in0+vec[7]*in1+vec[11]*in2+vec[15];
00047     return vec3f(v0,v1,v2);
00048   }
00049 */
00050 public:
00051   mat4f(float a_00,float a_01,float a_02,float a_03, //first  row
00052                float a_10,float a_11,float a_12,float a_13, //second row
00053                float a_20,float a_21,float a_22,float a_23, //third  row
00054                float a_30,float a_31,float a_32,float a_33) //fourth row
00055   {
00056     //a_<R><C>
00057     //m_vector[R + C * 4];
00058     std::vector<float>& vec = inlib::array<float>::m_vector;
00059     vec[0] = a_00;vec[4] = a_01;vec[ 8] = a_02;vec[12] = a_03;
00060     vec[1] = a_10;vec[5] = a_11;vec[ 9] = a_12;vec[13] = a_13;
00061     vec[2] = a_20;vec[6] = a_21;vec[10] = a_22;vec[14] = a_23;
00062     vec[3] = a_30;vec[7] = a_31;vec[11] = a_32;vec[15] = a_33;
00063   }
00064 
00065   void set_matrix(float a_00,float a_01,float a_02,float a_03, //1 row
00066                          float a_10,float a_11,float a_12,float a_13, //2 row
00067                          float a_20,float a_21,float a_22,float a_23, //3 row
00068                          float a_30,float a_31,float a_32,float a_33) //4 row
00069   {
00070     //a_<R><C>
00071     //m_vector[R + C * 4];
00072     std::vector<float>& vec = inlib::array<float>::m_vector;
00073     vec[0] = a_00;vec[4] = a_01;vec[ 8] = a_02;vec[12] = a_03;
00074     vec[1] = a_10;vec[5] = a_11;vec[ 9] = a_12;vec[13] = a_13;
00075     vec[2] = a_20;vec[6] = a_21;vec[10] = a_22;vec[14] = a_23;
00076     vec[3] = a_30;vec[7] = a_31;vec[11] = a_32;vec[15] = a_33;
00077   }
00078 
00079   void set_matrix(const mat4f& a_from){ //optimization.
00080     const std::vector<float>& fv = a_from.m_vector;
00081     std::vector<float>::const_iterator fvi = fv.begin();
00082     std::vector<float>& tv = inlib::array<float>::m_vector; //this vector.
00083     std::vector<float>::iterator tvi = tv.begin();
00084     for(;tvi!=tv.end();++tvi,++fvi) *tvi = *fvi;
00085   }
00086 /*
00087   void set_scale(float a_1,float a_2,float a_3) {
00088     std::vector<float>& vec = inlib::array<float>::m_vector;
00089     vec[0] = a_1;vec[4] =   0;vec[ 8] =   0;vec[12] = 0;
00090     vec[1] =   0;vec[5] = a_2;vec[ 9] =   0;vec[13] = 0;
00091     vec[2] =   0;vec[6] =   0;vec[10] = a_3;vec[14] = 0;
00092     vec[3] =   0;vec[7] =   0;vec[11] =   0;vec[15] = 1;
00093   }
00094 
00095   void set_translate(const vec3f& a_t) {
00096     std::vector<float>& vec = inlib::array<float>::m_vector;
00097     vec[0] = 1;vec[4] = 0;vec[ 8] = 0;vec[12] = a_t.v0();
00098     vec[1] = 0;vec[5] = 1;vec[ 9] = 0;vec[13] = a_t.v1();
00099     vec[2] = 0;vec[6] = 0;vec[10] = 1;vec[14] = a_t.v2();
00100     vec[3] = 0;vec[7] = 0;vec[11] = 0;vec[15] = 1;
00101   }
00102 */
00103   void set_translate(float a_x,float a_y,float a_z) {
00104     std::vector<float>& vec = inlib::array<float>::m_vector;
00105     vec[0] = 1;vec[4] = 0;vec[ 8] = 0;vec[12] = a_x;
00106     vec[1] = 0;vec[5] = 1;vec[ 9] = 0;vec[13] = a_y;
00107     vec[2] = 0;vec[6] = 0;vec[10] = 1;vec[14] = a_z;
00108     vec[3] = 0;vec[7] = 0;vec[11] = 0;vec[15] = 1;
00109   }
00110   void set_rotate(float a_x,float a_y,float a_z,float a_angle) {
00111     //WARNING : (a_x,a_y,a_z) must be a normalized vector.
00112     float c = fcos(a_angle);    
00113     float s = fsin(a_angle);    
00114     float x = a_x;
00115     float y = a_y;
00116     float z = a_z;
00117     float x2 = x*x;
00118     float y2 = y*y;
00119     float z2 = z*z;
00120     float xy = x*y;
00121     float xz = x*z;
00122     float yz = y*z;
00123     std::vector<float>& v = inlib::array<float>::m_vector;
00124     v[0] =  x2+(1-x2)*c;v[4] = xy*(1-c)-z*s;v[ 8] = xz*(1-c)+y*s;v[12] = 0;
00125     v[1] = xy*(1-c)+z*s;v[5] =  y2+(1-y2)*c;v[ 9] = yz*(1-c)-x*s;v[13] = 0;
00126     v[2] = xz*(1-c)-y*s;v[6] = yz*(1-c)+x*s;v[10] =  z2+(1-z2)*c;v[14] = 0;
00127     v[3] =            0;v[7] =            0;v[11] =            0;v[15] = 1;
00128   }
00129 /*
00130   void set_rotate(const vec3f& a_u,float a_angle) {
00131     //WARNING : a_u must be normalized.
00132     set_rotate(a_u.v0(),a_u.v1(),a_u.v2(),a_angle);
00133   }
00134 */
00135   void set_ortho(float a_l,float a_r,   //left,right
00136                         float a_b,float a_t,   //bottom,top
00137                         float a_n,float a_f) { //znear,zfar
00138     // from man glOrtho.
00139     float tx = -(a_r+a_l)/(a_r-a_l);
00140     float ty = -(a_t+a_b)/(a_t-a_b);
00141     float tz = -(a_f+a_n)/(a_f-a_n);
00142 
00143     float a_00,a_01,a_02,a_03;
00144     float a_10,a_11,a_12,a_13;
00145     float a_20,a_21,a_22,a_23;
00146     float a_30,a_31,a_32,a_33;
00147     a_00 = 2/(a_r-a_l);a_01 =           0;a_02 =            0;a_03 = tx;
00148     a_10 =           0;a_11 = 2/(a_t-a_b);a_12 =            0;a_13 = ty;
00149     a_20 =           0;a_21 =           0;a_22 = -2/(a_f-a_n);a_23 = tz;
00150     a_30 =           0;a_31 =           0;a_32 =            0;a_33 =  1;
00151     set_matrix(a_00,a_01,a_02,a_03,  //1 row
00152                a_10,a_11,a_12,a_13,  //2 row
00153                a_20,a_21,a_22,a_23,  //3 row
00154                a_30,a_31,a_32,a_33); //4 row
00155   }
00156   void set_frustum(float a_l,float a_r,   //left,right
00157                           float a_b,float a_t,   //bottom,top
00158                           float a_n,float a_f) { //znear,zfar
00159     // from man glFrustum.
00160     float A = (a_r+a_l)/(a_r-a_l);
00161     float B = (a_t+a_b)/(a_t-a_b);
00162     float C = -(a_f+a_n)/(a_f-a_n);
00163     float D = -(2*a_f*a_n)/(a_f-a_n);
00164 
00165     float a_00,a_01,a_02,a_03;
00166     float a_10,a_11,a_12,a_13;
00167     float a_20,a_21,a_22,a_23;
00168     float a_30,a_31,a_32,a_33;
00169     a_00 = (2*a_n)/(a_r-a_l);a_01 =                 0;a_02 =  A;a_03 = 0;
00170     a_10 =                 0;a_11 = (2*a_n)/(a_t-a_b);a_12 =  B;a_13 = 0;
00171     a_20 =                 0;a_21 =                 0;a_22 =  C;a_23 = D;
00172     a_30 =                 0;a_31 =                 0;a_32 = -1;a_33 = 0;
00173     set_matrix(a_00,a_01,a_02,a_03,  //1 row
00174                a_10,a_11,a_12,a_13,  //2 row
00175                a_20,a_21,a_22,a_23,  //3 row
00176                a_30,a_31,a_32,a_33); //4 row
00177   }
00178 public:
00179   void mul_3f(float& a_x,float& a_y,float& a_z) const {
00180     // a_[x,y,z] = this * a_[x,y,z]
00181     //m_vector[R + C * 4];
00182     const std::vector<float>& vec = inlib::array<float>::m_vector;
00183     //vec[0] = 00;vec[4] = 01;vec[ 8] = 02;vec[12] = 03;
00184     //vec[1] = 10;vec[5] = 11;vec[ 9] = 12;vec[13] = 13;
00185     //vec[2] = 20;vec[6] = 21;vec[10] = 22;vec[14] = 23;
00186     //vec[3] = 30;vec[7] = 31;vec[11] = 32;vec[15] = 33;
00187     float x = vec[0]*a_x+vec[4]*a_y+vec[ 8]*a_z+vec[12];
00188     float y = vec[1]*a_x+vec[5]*a_y+vec[ 9]*a_z+vec[13];
00189     float z = vec[2]*a_x+vec[6]*a_y+vec[10]*a_z+vec[14];
00190     a_x = x;
00191     a_y = y;
00192     a_z = z;
00193   }
00194   void mul_2f(float& a_x,float& a_y) const {
00195     // a_[x,y] = this * a_[x,y]
00196     //m_vector[R + C * 4];
00197     const std::vector<float>& vec = inlib::array<float>::m_vector;
00198     //vec[0] = 00;vec[4] = 01;vec[ 8] = 02;vec[12] = 03;
00199     //vec[1] = 10;vec[5] = 11;vec[ 9] = 12;vec[13] = 13;
00200     //vec[2] = 20;vec[6] = 21;vec[10] = 22;vec[14] = 23;
00201     //vec[3] = 30;vec[7] = 31;vec[11] = 32;vec[15] = 33;
00202     float x = vec[0]*a_x+vec[4]*a_y+vec[12];
00203     float y = vec[1]*a_x+vec[5]*a_y+vec[13];
00204     a_x = x;
00205     a_y = y;
00206   }
00207   void mul_scale(float a_sx,float a_sy,float a_sz) {
00208     // this = this * mat4f_scale(a_s[x,y,z]
00209     //m_vector[R + C * 4];
00210     std::vector<float>& vec = inlib::array<float>::m_vector;
00211     //vec[0] = 00;vec[4] = 01;vec[ 8] = 02;vec[12] = 03;
00212     //vec[1] = 10;vec[5] = 11;vec[ 9] = 12;vec[13] = 13;
00213     //vec[2] = 20;vec[6] = 21;vec[10] = 22;vec[14] = 23;
00214     //vec[3] = 30;vec[7] = 31;vec[11] = 32;vec[15] = 33;
00215     vec[0] *= a_sx;
00216     vec[1] *= a_sx;
00217     vec[2] *= a_sx;
00218     vec[3] *= a_sx;
00219 
00220     vec[4] *= a_sy;
00221     vec[5] *= a_sy;
00222     vec[6] *= a_sy;
00223     vec[7] *= a_sy;
00224 
00225     vec[ 8] *= a_sz;
00226     vec[ 9] *= a_sz;
00227     vec[10] *= a_sz;
00228     vec[11] *= a_sz;
00229   }
00230   void mul_trans(float a_x,float a_y,float a_z) {
00231     // this = this * mat4f_trans(a_[x,y,z]
00232     //m_vector[R + C * 4];
00233     std::vector<float>& vec = inlib::array<float>::m_vector;
00234     //vec[0] = 00;vec[4] = 01;vec[ 8] = 02;vec[12] = 03;
00235     //vec[1] = 10;vec[5] = 11;vec[ 9] = 12;vec[13] = 13;
00236     //vec[2] = 20;vec[6] = 21;vec[10] = 22;vec[14] = 23;
00237     //vec[3] = 30;vec[7] = 31;vec[11] = 32;vec[15] = 33;
00238     
00239     float v12 = vec[0]*a_x+vec[4]*a_y+vec[ 8]*a_z+vec[12];
00240     float v13 = vec[1]*a_x+vec[5]*a_y+vec[ 9]*a_z+vec[13];
00241     float v14 = vec[2]*a_x+vec[6]*a_y+vec[10]*a_z+vec[14];
00242     float v15 = vec[3]*a_x+vec[7]*a_y+vec[11]*a_z+vec[15];
00243 
00244     vec[12] = v12;
00245     vec[13] = v13;
00246     vec[14] = v14;
00247     vec[15] = v15;
00248   }
00249 
00250   void mul_mtx(const mat4f& a_from) {
00251     // this = this * a_from
00252     //m_vector[R + C * 4];
00253     const std::vector<float>& fv = a_from.m_vector;
00254     std::vector<float>& tv = inlib::array<float>::m_vector;
00255     //vec[0] = 00;vec[4] = 01;vec[ 8] = 02;vec[12] = 03;
00256     //vec[1] = 10;vec[5] = 11;vec[ 9] = 12;vec[13] = 13;
00257     //vec[2] = 20;vec[6] = 21;vec[10] = 22;vec[14] = 23;
00258     //vec[3] = 30;vec[7] = 31;vec[11] = 32;vec[15] = 33;
00259     
00260     float v00 = tv[0]*fv[0]+tv[4]*fv[1]+tv[ 8]*fv[2]+tv[12]*fv[3];
00261     float v10 = tv[1]*fv[0]+tv[5]*fv[1]+tv[ 9]*fv[2]+tv[13]*fv[3];
00262     float v20 = tv[2]*fv[0]+tv[6]*fv[1]+tv[10]*fv[2]+tv[14]*fv[3];
00263     float v30 = tv[3]*fv[0]+tv[7]*fv[1]+tv[11]*fv[2]+tv[15]*fv[3];
00264 
00265     float v01 = tv[0]*fv[4]+tv[4]*fv[5]+tv[ 8]*fv[6]+tv[12]*fv[7];
00266     float v11 = tv[1]*fv[4]+tv[5]*fv[5]+tv[ 9]*fv[6]+tv[13]*fv[7];
00267     float v21 = tv[2]*fv[4]+tv[6]*fv[5]+tv[10]*fv[6]+tv[14]*fv[7];
00268     float v31 = tv[3]*fv[4]+tv[7]*fv[5]+tv[11]*fv[6]+tv[15]*fv[7];
00269 
00270     float v02 = tv[0]*fv[8]+tv[4]*fv[9]+tv[ 8]*fv[10]+tv[12]*fv[11];
00271     float v12 = tv[1]*fv[8]+tv[5]*fv[9]+tv[ 9]*fv[10]+tv[13]*fv[11];
00272     float v22 = tv[2]*fv[8]+tv[6]*fv[9]+tv[10]*fv[10]+tv[14]*fv[11];
00273     float v32 = tv[3]*fv[8]+tv[7]*fv[9]+tv[11]*fv[10]+tv[15]*fv[11];
00274 
00275     float v03 = tv[0]*fv[12]+tv[4]*fv[13]+tv[ 8]*fv[14]+tv[12]*fv[15];
00276     float v13 = tv[1]*fv[12]+tv[5]*fv[13]+tv[ 9]*fv[14]+tv[13]*fv[15];
00277     float v23 = tv[2]*fv[12]+tv[6]*fv[13]+tv[10]*fv[14]+tv[14]*fv[15];
00278     float v33 = tv[3]*fv[12]+tv[7]*fv[13]+tv[11]*fv[14]+tv[15]*fv[15];
00279 
00280     tv[0] = v00;tv[4] = v01;tv[ 8] = v02;tv[12] = v03;
00281     tv[1] = v10;tv[5] = v11;tv[ 9] = v12;tv[13] = v13;
00282     tv[2] = v20;tv[6] = v21;tv[10] = v22;tv[14] = v23;
00283     tv[3] = v30;tv[7] = v31;tv[11] = v32;tv[15] = v33;
00284   }
00285 public:
00286   const float* data() const { //for OpenGL.
00287     return inlib::vec_data<float>(m_vector);
00288   }
00289 public:
00290   //NOTE : don't handle a static object because of mem balance.
00291   //static const mat4f& identity() {
00292   //  static const mat4f s_v(1,0,0,0,
00293   //                         0,1,0,0,
00294   //                         0,0,1,0,
00295   //                         0,0,0,1);
00296   //  return s_v;
00297   //}
00298 private:
00299   static void check_instantiation() {
00300     mat4f dummy;
00301     //dummy.set_rotate(vec3f(1,0,0),0);
00302   }
00303 };
00304 
00305 inline std::ostream& operator<<(std::ostream& a_out,const mat4f&){
00306   //a_out << "x = " << a_this[0]
00307   //      << ",y = " << a_this[1]
00308   //      << ",z = " << a_this[2]
00309   //      << std::endl;
00310   return a_out;
00311 }
00312 
00313 }
00314 
00315 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines