MC_trackball.cpp

Go to the documentation of this file.
00001 
00002 #include <MC_v3d.hpp>
00003 
00004 #include <MC_trackball.hpp>
00005 
00006 
00007 
00008 namespace mesh_conv
00009 {
00010 
00011     MC_trackball::MC_trackball()
00012     {disc_radius=0.8;}
00013 
00014     MC_trackball::~MC_trackball(){}
00015 
00016     double MC_trackball::project_to_disc(const double& x,const double& y) const
00017     {
00018         double n=sqrt(x*x+y*y);
00019         if(n<disc_radius*0.707107)
00020             return sqrt(disc_radius*disc_radius-n*n);
00021         else
00022             return disc_radius*disc_radius/(2.0*n);
00023     }
00024 
00025     MC_trackball& MC_trackball::set_2d_coords(const double& x0,const double& y0,const double& x1,const double& y1)
00026     {
00027         //axis of the rotation
00028         MC_v3d axis;
00029         // angle of the rotation
00030         double phi;
00031 
00032         //security to not move if start=end
00033         double epsilon=0.0001;
00034         if( std::sqrt((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1))<epsilon)
00035         {
00036             std::cout<<"MC_trackball::set_2d_coords problem"<<std::endl;
00037             d_q=MC_quaternion(1,0,0,0);
00038         }
00039         else
00040         {
00041             // temp position
00042             MC_v3d p1,p2;
00043 
00044 
00045             //printf("(%f,%f),(%f,%f)\n",x0,y0,x1,y1);
00046             p1=MC_v3d(x0,y0,project_to_disc(x0,y0));
00047             p2=MC_v3d(x1,y1,project_to_disc(x1,y1));
00048             //std::cout<<p1<<","<<p2<<"  "<<p1-p2<<std::endl;
00049 
00050             axis = (p1.cross(p2)).normalized();
00051 
00052             MC_v3d u=p1-p2;
00053             double t=u.norm()/(2*disc_radius);
00054             t=fmin(fmax(t,-1.0),1.0); //clamp
00055             phi = 2.0*asin(t);
00056 
00057             //compute quaternion to apply
00058             d_q=MC_quaternion(axis,phi);
00059         }
00060         return *this;
00061     }
00062 
00063     MC_trackball& MC_trackball::apply_rotation()
00064     {current_q=d_q.conjugated()*current_q;return *this;}
00065     MC_trackball& MC_trackball::no_motion()
00066     {d_q=MC_quaternion(1,0,0,0);return *this;}
00067 
00068 
00069     const MC_quaternion& MC_trackball::quaternion() const{return current_q;}
00070     MC_quaternion& MC_trackball::quaternion(){return current_q;}
00071 
00072     const MC_quaternion& MC_trackball::d_quaternion() const {return d_q;}
00073     MC_quaternion& MC_trackball::d_quaternion() {return d_q;}
00074 
00075 }

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