MC_navigator_tool.cpp

Go to the documentation of this file.
00001 
00002 #include <MC_navigator_tool.hpp>
00003 
00004 #include <MC_matrix.hpp>
00005 #include <MC_v3d_vector.hpp>
00006 
00007 #include <fstream>
00008 
00009 namespace mesh_conv
00010 {
00011     MC_navigator_tool::MC_navigator_tool()
00012     {
00013 #define PI 3.14159
00014 
00015         dist_cam1=-3.0;
00016         current_time=0;old_time=0;frame=0;current_fps=0.0;
00017         x_previous=0;y_previous=0;
00018         x_screen_size_1=100;y_screen_size_1=100;
00019         size_pointer=0.1;
00020 
00021         fov=45.0*PI/180.0;
00022         znear=0.01;
00023         zfar=150.0;
00024     }
00025     MC_matrix MC_navigator_tool::current_cam1() const
00026     {
00027         MC_matrix camera_1=MC_matrix::identity(4);
00028         camera_1.add_translation(MC_v3d(0,0,dist_cam1));
00029         camera_1=camera_1*trackball_cam1.quaternion().conjugated().matrix().to_matrix4();
00030 
00031         MC_v3d translate_after=camera_1.to_matrix3().first*tr_cam1;
00032         camera_1.add_translation(translate_after);
00033 
00034         return camera_1;
00035     }
00036     MC_matrix MC_navigator_tool::current_light1() const
00037     {
00038         MC_matrix light_1=MC_matrix::identity(4);
00039         light_1.add_translation(MC_v3d(0,0,dist_light1));
00040         light_1=light_1*trackball_light1.quaternion().conjugated().matrix().to_matrix4();
00041 
00042         MC_v3d translate_after=light_1.to_matrix3().first*tr_light1;
00043         light_1.add_translation(translate_after);
00044 
00045         return light_1;
00046     }
00047     MC_v3d_vector MC_navigator_tool::axis_cam1() const
00048     {
00049         MC_matrix camera_1=current_cam1();
00050 //        MC_v3d eye_n(camera_1(0,2),camera_1(1,2),camera_1(2,2));
00051 //        MC_v3d eye_up(camera_1(0,1),camera_1(1,1),camera_1(2,1));
00052 //        MC_v3d eye_right(camera_1(0,0),camera_1(1,0),camera_1(2,0));
00053 //
00054         MC_v3d eye_n(camera_1(2,0),camera_1(2,1),camera_1(2,2));
00055         MC_v3d eye_up(camera_1(1,0),camera_1(1,1),camera_1(1,2));
00056         MC_v3d eye_right(camera_1(0,0),camera_1(0,1),camera_1(0,2));
00057 
00058 
00059 
00060         MC_v3d local_p(camera_1(0,3),camera_1(1,3),camera_1(2,3));
00061 
00062         //local_p=camera_1.to_matrix3().first*(MC_v3d(0,0,-dist_cam1));
00063         //local_p=camera_1.to_matrix3().first.inverted()*(-local_p);
00064         //local_p=-(MC_v3d(camera_1.to_matrix3().first*MC_v3d(0,0,dist_cam1))+tr_cam1);
00065         local_p=-MC_v3d((camera_1.to_matrix3().first).inverted()*(camera_1.to_matrix3().second));
00066 
00067         MC_v3d_vector parameters;
00068         parameters=parameters<<local_p<<eye_n<<eye_right<<eye_up;
00069         return parameters;
00070     }
00071 
00072     MC_v3d_vector MC_navigator_tool::axis_light1() const
00073     {
00074         MC_matrix light_1=current_light1();
00075 
00076         MC_v3d eye_n(light_1(2,0),light_1(2,1),light_1(2,2));
00077         MC_v3d eye_up(light_1(1,0),light_1(1,1),light_1(1,2));
00078         MC_v3d eye_right(light_1(0,0),light_1(0,1),light_1(0,2));
00079 
00080         MC_v3d local_p(light_1(0,3),light_1(1,3),light_1(2,3));
00081         local_p=-MC_v3d((light_1.to_matrix3().first).inverted()*(light_1.to_matrix3().second));
00082 
00083         MC_v3d_vector parameters;
00084         parameters=parameters<<local_p<<eye_n<<eye_right<<eye_up;
00085         return parameters;
00086     }
00087 
00088     double MC_navigator_tool::update_fps()
00089     {
00090         current_fps=double(frame*1000)/double(current_time-old_time);
00091         old_time=current_time;
00092         frame=0;
00093         return current_fps;
00094     }
00095 
00096     void MC_navigator_tool::go_forward_trackball_cam1(const double& d_L)
00097     {
00098         MC_v3d z(0,0,1);
00099         tr_cam1+=d_L*(trackball_cam1.quaternion().conjugated()*z);
00100     }
00101     void MC_navigator_tool::go_right_trackball_cam1(const double& d_L)
00102     {
00103         MC_v3d z(-1,0,0);
00104         tr_cam1+=d_L*(trackball_cam1.quaternion().conjugated()*z);
00105     }
00106     void MC_navigator_tool::go_up_trackball_cam1(const double& d_L)
00107     {
00108         MC_v3d z(0,1,0);
00109         tr_cam1+=d_L*(trackball_cam1.quaternion().conjugated()*z);
00110     }
00111 
00112     std::ostream& MC_navigator_tool::write_cam1(std::ostream& stream) const
00113     {
00114         stream<<"quat "<<trackball_cam1.quaternion()<<"\ndist "<<dist_cam1<<"\ntr "<<tr_cam1<<std::endl;
00115         return stream;
00116     }
00117     const MC_navigator_tool& MC_navigator_tool::write_cam1_file(std::string& filename) const
00118     {
00119         std::ofstream fstream(filename.c_str());
00120         write_cam1(fstream);
00121         fstream.close();
00122         return *this;
00123     }
00124     MC_navigator_tool& MC_navigator_tool::read_cam1(std::istream& param)
00125     {
00126         std::string buffer;
00127 
00128         param>>buffer; //quat
00129         param>>trackball_cam1.quaternion();
00130 
00131         param>>buffer; //dist
00132         param>>dist_cam1;
00133 
00134         param>>buffer; //tr
00135         param>>tr_cam1;
00136 
00137         return *this;
00138     }
00139     MC_navigator_tool& MC_navigator_tool::read_cam1_file(const std::string& filename)
00140     {
00141         std::ifstream fstream(filename.c_str());
00142         if(!fstream.good())
00143         {std::cout<<"MC_navigator_tool::read_cam1_file("<<filename<<") cannot open the file"<<std::endl;}
00144         read_cam1(fstream);
00145         fstream.close();
00146         return *this;
00147     }
00148 
00149     std::ostream& MC_navigator_tool::write_light1(std::ostream& stream) const
00150     {
00151         stream<<"quat "<<trackball_light1.quaternion()<<"\ndist "<<dist_light1<<"\ntr "<<tr_light1<<std::endl;
00152         return stream;
00153     }
00154     const MC_navigator_tool& MC_navigator_tool::write_light1_file(std::string& filename) const
00155     {
00156         std::ofstream fstream(filename.c_str());
00157         write_light1(fstream);
00158         fstream.close();
00159         return *this;
00160     }
00161     MC_navigator_tool& MC_navigator_tool::read_light1(std::istream& param)
00162     {
00163         std::string buffer;
00164 
00165         param>>buffer; //quat
00166         param>>trackball_light1.quaternion();
00167 
00168         param>>buffer; //dist
00169         param>>dist_light1;
00170 
00171         param>>buffer; //tr
00172         param>>tr_light1;
00173 
00174         return *this;
00175     }
00176     MC_navigator_tool& MC_navigator_tool::read_light1_file(const std::string& filename)
00177     {
00178         std::ifstream fstream(filename.c_str());
00179         if(!fstream.good())
00180         {std::cout<<"MC_navigator_tool::read_light1_file("<<filename<<") cannot open the file"<<std::endl;}
00181 
00182         read_light1(fstream);
00183         fstream.close();
00184         return *this;
00185     }
00186 
00187 
00188 
00189     MC_double_vector MC_navigator_tool::projection_param() const
00190     {
00191         double aspect=static_cast<double>(x_screen_size_1)/static_cast<double>(y_screen_size_1);
00192         double top=tan(fov*0.5)*znear;
00193         double left=-aspect*top;
00194         return MC_double_vector(left,top,znear,zfar,aspect);
00195     }
00196     MC_matrix MC_navigator_tool::projection_matrix() const
00197     {
00198         MC_double_vector param=projection_param();
00199         double left=param[0];
00200         double right=-left;
00201         double top=param[1];
00202         double bottom=-top;
00203         double znear=param[2];
00204         double zfar=param[3];
00205         MC_matrix p(4,4);
00206 
00207 
00208         p(0,0)=2*znear/(right-left);
00209         p(1,1)=2*znear/(top-bottom);
00210 
00211         p(0,2)=(right+left)/(right-left);
00212         p(1,2)=(top+bottom)/(top-bottom);
00213         p(2,2)=-(zfar+znear)/(zfar-znear);
00214         p(3,2)=-1;
00215 
00216         p(2,3)=-2*zfar*znear/(zfar-znear);
00217 
00218 
00219 
00220         return p;
00221     }
00222 
00223     MC_v3d_vector MC_navigator_tool::ray_world_space_cam1(const int& x_screen,const int& y_screen) const
00224     {
00225         //normalized screen coordinates
00226         MC_v3d local(
00227                 (x_screen_size_1-2*x_screen)/double(x_screen_size_1)
00228                 ,
00229                 (y_screen_size_1-2*y_screen)/double(y_screen_size_1),0);
00230 
00231         //camera parameters
00232         MC_double_vector proj_param=projection_param();
00233         double Lh=-proj_param[1];
00234         double aspect=proj_param[4];
00235 
00236         //coordinate on the camera
00237         MC_v3d proj_ray(local[0]*Lh*aspect,local[1]*Lh,-znear);
00238 
00239         // modelview
00240         MC_matrix mod=current_cam1();
00241         MC_matrix mod_inv=mod.inverted();
00242 
00243         MC_v3d ray=mod_inv.to_matrix3().first*proj_ray;
00244         MC_v3d center=mod_inv.translation_part();
00245 
00246         return MC_v3d_vector(center,ray.normalized());
00247     }
00248 
00249 }

Generated on Sun Apr 18 20:24:47 2010 by  doxygen 1.6.1