resolved problems about trackball and communicating views

This commit is contained in:
Michele Sottile sottile 2010-06-29 14:33:55 +00:00
parent 9caaa26108
commit 82e9ae3135
2 changed files with 184 additions and 61 deletions

View File

@ -625,10 +625,6 @@ void GLArea::sendShot()
double viewportYMm=shot.Intrinsics.PixelSizeMm[1]*shot.Intrinsics.ViewportPx[1];
shot.Intrinsics.FocalMm = viewportYMm/(2*tanf(vcg::math::ToRad(fov/2)));
//The new shot doesn't have to introduce a translation from the center
Point3f center = meshDoc->bbox().Center();
trackball.track.tra += center;
// This parameter is the one that controls:
// HOW LARGE IS THE TRACKBALL ICON ON THE SCREEN.
float viewRatio = 1.75f;
@ -643,23 +639,21 @@ void GLArea::sendShot()
Shot newShot = getShotFromTrack2(shot, &trackball);
trackball.track.tra -= center;
////Expressing scaling as a translation along z
////k is the ratio between default scale and new scale
//double oldScale= 3.0f/meshDoc->bbox().Diag();
//double k= oldScale/trackball.track.sca;
//Expressing scaling as a translation along z
//k is the ratio between default scale and new scale
double oldScale= 3.0f/meshDoc->bbox().Diag();
double k= oldScale/trackball.track.sca;
// //Apply this formula
//// R(t+p) = kR'(t'+p) forall p, R=R', k is a costant
//// R(t)= kR(t')
//// t = k*t'
//Point3d t1 = newShot.Extrinsics.Tra();
//
//Matrix44d rapM = Matrix44d().SetScale(k, k, k);
//Point3d t0 = rapM*t1;
//Apply this formula
// R(t+p) = kR'(t'+p) forall p, R=R', k is a costant
// R(t)= kR(t')
// t = k*t'
Point3d t1 = newShot.Extrinsics.Tra();
Matrix44d rapM = Matrix44d().SetScale(k, k, k);
Point3d t0 = rapM*t1;
newShot.Extrinsics.SetTra(t0);
//newShot.Extrinsics.SetTra(t0);
mvc->updateTrackballInViewers(newShot);
}
@ -1212,29 +1206,29 @@ void GLArea::loadShot(){
//Shot test
Shot shot;
initializeShot(shot);
//initializeShot(shot);
////oppure lo leggi da file-------------------------------------------------------
//readShotFromFile(shot);
////resize viewport
//int w = shot.Intrinsics.ViewportPx[0];
//int h = shot.Intrinsics.ViewportPx[1];
//oppure lo leggi da file-------------------------------------------------------
readShotFromFile(shot);
//resize viewport
int w = shot.Intrinsics.ViewportPx[0];
int h = shot.Intrinsics.ViewportPx[1];
//if(w > width()) {
// h = h*width()/w;
// w = width();
//}
//if(h > height()) {
// w = w*height()/h;
// h = height();
//}
//shot.Intrinsics.DistorCenterPx[0]=w/2;
// shot.Intrinsics.DistorCenterPx[1]=h/2;
// shot.Intrinsics.CenterPx[0]=w/2;
// shot.Intrinsics.CenterPx[1]=h/2;
// shot.Intrinsics.ViewportPx[0]=w;
// shot.Intrinsics.ViewportPx[1]=h;
////-----------------------------------------------------------------------------------------
/*if(w > width()) {
h = h*width()/w;
w = width();
}
if(h > height()) {
w = w*height()/h;
h = height();
}*/
shot.Intrinsics.DistorCenterPx[0]=w/2;
shot.Intrinsics.DistorCenterPx[1]=h/2;
shot.Intrinsics.CenterPx[0]=w/2;
shot.Intrinsics.CenterPx[1]=h/2;
shot.Intrinsics.ViewportPx[0]=w;
shot.Intrinsics.ViewportPx[1]=h;
//-----------------------------------------------------------------------------------------
// This parameter is the one that controls:
// HOW LARGE IS THE TRACKBALL ICON ON THE SCREEN.
@ -1242,7 +1236,7 @@ void GLArea::loadShot(){
float cameraDist = viewRatio / tanf(vcg::math::ToRad(fov*.5f));
//Esempi di shot di ingresso
shot.Extrinsics.SetTra(Point3d(0, 0, cameraDist*2));
//shot.Extrinsics.SetTra(Point3d(0, 0, cameraDist*2));
/*vcg::Matrix44d rot;
rot.Identity();
rot.SetRotateDeg(90,Point3<double>(0,1,0));
@ -1265,23 +1259,20 @@ void GLArea::loadShot(const Shot &shot_){
// HOW LARGE IS THE TRACKBALL ICON ON THE SCREEN.
float viewRatio = 1.75f;
float cameraDist = viewRatio / tanf(vcg::math::ToRad(fov*.5f));
//correct the translation introduced by gluLookAt() (0,0,cameraDist)---------------------------------------
//T(gl)*S*R*T(t) => SR(gl+t) => S R (S^(-1)R^(-1)gl + t)
//To compensate S^(-1)R^(-1)gl we add to t S^(-1) R^(-1)(-gl)
//Shot doesn't introduce scaling
shot.Extrinsics.SetTra(shot.Extrinsics.Tra() + (Inverse(shot.Extrinsics.Rot())*Point3d(0, 0, -cameraDist)));
//reset trackball. The point of view must be set only by the shot
trackball.Reset();
float newScale= 3.0f/meshDoc->bbox().Diag();
trackball.track.sca = newScale;
trackball.track.tra = -meshDoc->bbox().Center();
//trackball.track.tra = -meshDoc->bbox().Center();
Point3f point = meshDoc->bbox().Center();
Point3f p1 = ((trackball.track.Matrix()*(point-trackball.center))- Point3f(0,0,cameraDist));
Shot2Track(shot, cameraDist,trackball);
//Shot test;
//getShotFromTrack2(shot, &trackball);
//Expressing the translation along Z with a scale factor k
Point3f p2 = ((trackball.track.Matrix()*(point-trackball.center))- Point3f(0,0,cameraDist));
@ -1305,7 +1296,7 @@ void GLArea::loadShot(const Shot &shot_){
Point3f t1 = rapM*Inverse(s1)*s0*tra + rapM2*Inverse(s1)*Inverse(r)*Point3f(0,0,cameraDist);
trackball.track.sca =sca;
trackball.track.tra =t1;
trackball.track.tra =t1 /*+ tb.track.rot.Inverse().Rotate(glLookAt)*/ ;
//Test on trackball
/*Matrix44f s_inv = Matrix44f().SetScale(1/trackball.track.sca, 1/trackball.track.sca, 1/trackball.track.sca);
@ -1320,3 +1311,72 @@ void GLArea::loadShot(const Shot &shot_){
updateGL();
}
//void GLArea::loadShot(const Shot &shot_){
//
// Shot shot = shot_;
//
// double viewportYMm=shot.Intrinsics.PixelSizeMm[1]*shot.Intrinsics.ViewportPx[1];
// fov = 2*(vcg::math::ToDeg(atanf(viewportYMm/(2*shot.Intrinsics.FocalMm))));
// /*fov=60;
// float focal = viewportYMm/(2*tanf(vcg::math::ToRad(fov/2)));*/
//
// // This parameter is the one that controls:
// // HOW LARGE IS THE TRACKBALL ICON ON THE SCREEN.
// float viewRatio = 1.75f;
// float cameraDist = viewRatio / tanf(vcg::math::ToRad(fov*.5f));
//
// //correct the translation introduced by gluLookAt() (0,0,cameraDist)---------------------------------------
// //T(gl)*S*R*T(t) => SR(t)+ gl => S R (S^(-1)R^(-1)gl + t)
// //To compensate S^(-1)R^(-1)gl we add to t S^(-1) R^(-1)(-gl)
// //Shot doesn't introduce scaling
// shot.Extrinsics.SetTra(shot.Extrinsics.Tra() + (Inverse(shot.Extrinsics.Rot())*Point3d(0, 0, -cameraDist)));
//
// //reset trackball. The point of view must be set only by the shot
// trackball.Reset();
// float newScale= 3.0f/meshDoc->bbox().Diag();
// trackball.track.sca = newScale;
// //trackball.track.tra = -meshDoc->bbox().Center();
//
// Point3f point = meshDoc->bbox().Center();
// Point3f p1 = ((trackball.track.Matrix()*(point-trackball.center))- Point3f(0,0,cameraDist));
// Shot2Track(shot, cameraDist,trackball);
//
// //Expressing the translation along Z with a scale factor k
// Point3f p2 = ((trackball.track.Matrix()*(point-trackball.center))- Point3f(0,0,cameraDist));
//
// //k is the ratio between the distances along z of two correspondent points (before and after the traslazion)
// //from the point of view
// float k= abs(p2.Z()/p1.Z());
//
// float sca= trackball.track.sca/k;
// Point3f tra = trackball.track.tra;
//
// // Apply this formula:
// // SR(t+p) -v = k[S'R'(t'+p) -v] forall p, R=R', k is a costant
// // SR(t) -v = k[S'R(t') -v]
// // t' = 1/k* S'^-1St + (k-1)/k S'^-1*R^-1v
// Matrix44f s0 = Matrix44f().SetScale(trackball.track.sca,trackball.track.sca, trackball.track.sca);
// Matrix44f s1 = Matrix44f().SetScale(sca, sca, sca);
// Matrix44f r;
// trackball.track.rot.ToMatrix(r);
// Matrix44f rapM = Matrix44f().SetScale(1/k, 1/k, 1/k);
// Matrix44f rapM2 = Matrix44f().SetScale(1-1/k, 1-1/k, 1-1/k);
// Point3f t1 = rapM*Inverse(s1)*s0*tra + rapM2*Inverse(s1)*Inverse(r)*Point3f(0,0,cameraDist);
//
// trackball.track.sca =sca;
// trackball.track.tra =t1;
//
// //Test on trackball
// /*Matrix44f s_inv = Matrix44f().SetScale(1/trackball.track.sca, 1/trackball.track.sca, 1/trackball.track.sca);
// vcg::Matrix44f rot_inv;
// Inverse(trackball.track.rot).ToMatrix(rot_inv);
//
// Shot2Track(shot, cameraDist,trackball);
//
// trackball.track.tra += s_inv*rot_inv*Point3f(0, 0, cameraDist);*/
//
// //Shot2Track(shot, cameraDist,trackball);
//
// updateGL();
//}

View File

@ -429,7 +429,7 @@ private:
vcg::Matrix44d imodel = model;
vcg::Transpose(imodel);
tra = -(imodel*tra);
//tra *= idet; //SOTTILE
tra *= idet;
view.Extrinsics.SetTra(vcg::Point3<T>::Construct(tra));
//use same current intrinsics
@ -439,27 +439,90 @@ private:
}
/*
Given a Shot "from", and a trackball "tb" replaces "tb" with a trackball "tb'" such that:
"from" o "tb" = "tb'"
TBD
*/
template <class T>
void Shot2Track(const vcg::Shot<T> &from, const float cameraDist, vcg::Trackball &tb){
Shot id;
vcg::Quaterniond qto; qto.FromMatrix(id.Extrinsics.Rot());
vcg::Quaterniond qfrom; qfrom.FromMatrix(from.Extrinsics.Rot());
/*float sca=tb.track.sca;
tb.track.sca=1;*/
tb.track.tra += ( tb.track.rot.Inverse().Rotate( vcg::Point3f::Construct(-from.Extrinsics.Tra()) + tb.center ) ) / tb.track.sca;
tb.track.rot = vcg::Quaternionf::Construct(qto.Inverse() * qfrom) * tb.track.rot;
tb.track.tra -= ( tb.track.rot.Inverse().Rotate( vcg::Point3f::Construct(- id.Extrinsics.Tra()) + tb.center ) ) / tb.track.sca;
Matrix44f rotFrom;
from.Extrinsics.Rot().ToMatrix(rotFrom);
Point3f oldC= meshDoc->bbox().Center();
Point3f p1 = rotFrom*(vcg::Point3f::Construct(from.Extrinsics.Tra())/*+(oldC)*/);
//Expressing the translation along Z with a scale factor k
Point3f p2 = (/*(tb.track.Matrix()*(oldC))- */Point3f(0,0,cameraDist));
//k is the ratio between the distances along z of two correspondent points (before and after the traslation)
//from the point of view
float k= abs(p2.Z()/p1.Z());
trackball.track.sca =k;
Point3f glLookAt = Point3f(0,0,-cameraDist);
tb.track.rot = vcg::Quaternionf::Construct(qfrom);
Point3d fromTra = from.Extrinsics.Tra();
Point3d fromTraSca = from.Extrinsics.Tra()* tb.track.sca;
Point3f fromTraScaRot = tb.track.rot.Rotate(vcg::Point3f::Construct(from.Extrinsics.Tra())* tb.track.sca);
Point3f glLookAtRot = tb.track.rot.Rotate(glLookAt);
tb.track.tra = (vcg::Point3f::Construct(-from.Extrinsics.Tra()) /*- tb.track.rot.Inverse().Rotate(glLookAt)*/);
tb.track.tra += vcg::Point3f::Construct(tb.track.rot.Inverse().Rotate(Point3f(0,0,cameraDist)))*(1/tb.track.sca /*-tb.track.sca*/);
//tb.track.tra += (vcg::Point3f::Construct(from.Extrinsics.Tra())* tb.track.sca - tb.track.rot.Inverse().Rotate(glLookAt));
//tb.track.tra = Point3f(0,0,0);
//aggiustare sca e tra per mettere il centro della trackball al punto giusto
// per ora fatto fuori
//tb.track.tra *= tb.track.sca;
}
};
//
///*
// Given a Shot "from", and a trackball "tb" replaces "tb" with a trackball "tb'" such that:
// "from" o "tb" = "id" o "tb'" where "id" is a shot with identic rot e no traslation
// */
// template <class T>
// void Shot2Track(const vcg::Shot<T> &from, const float cameraDist, vcg::Trackball &tb){
// Shot id;
//
// vcg::Quaterniond qto; qto.FromMatrix(id.Extrinsics.Rot());
// vcg::Quaterniond qfrom; qfrom.FromMatrix(from.Extrinsics.Rot());
//
// Point3f oldC= meshDoc->bbox().Center();
//
// Matrix44f rotFrom;
// from.Extrinsics.Rot().ToMatrix(rotFrom);
//
// Point3f p1 = rotFrom*(vcg::Point3f::Construct(from.Extrinsics.Tra())+(oldC));
//
// //Expressing the translation along Z with a scale factor k
// Point3f p2 = ((tb.track.Matrix()*(oldC))- Point3f(0,0,cameraDist));
//
// //k is the ratio between the distances along z of two correspondent points (before and after the traslation)
// //from the point of view
// float k= abs(p2.Z()/p1.Z());
//
// trackball.track.sca /=k;
//
// /*float sca=tb.track.sca;
// tb.track.sca=1;*/
// tb.track.tra = (-oldC)/*/trackball.track.sca*/;
// tb.track.tra += ( tb.track.rot.Inverse().Rotate( vcg::Point3f::Construct(-from.Extrinsics.Tra()) + oldC ) )/*/ tb.track.sca*/ ;
// tb.track.rot = vcg::Quaternionf::Construct(qto.Inverse() * qfrom) * tb.track.rot;
// tb.track.tra -= ( tb.track.rot.Inverse().Rotate( vcg::Point3f::Construct(- id.Extrinsics.Tra()) + tb.center ) )/ tb.track.sca;
//
// //aggiustare sca e tra per mettere il centro della trackball al punto giusto
// // per ora fatto fuori
// //tb.track.tra *= tb.track.sca;
// }
//};
#endif