00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112 #include <GL/glew.h>
00113 #include "trackball.h"
00114 #include<set>
00115
00116 #include <wrap/gl/math.h>
00117 #include <wrap/gl/space.h>
00118
00119 using namespace vcg;
00120
00121 Transform::Transform() {
00122 track.SetIdentity();
00123 radius=1.0f;
00124 center=Point3f(0,0,0);
00125 }
00126
00127 Trackball::Trackball(): current_button(0), current_mode(NULL), inactive_mode(NULL),
00128 dragging(false), last_time(0), spinnable(true), spinning(false),
00129 history_size(10), fixedTimestepMode(false) {
00130 setDefaultMapping ();
00131 }
00132
00133 Trackball::~Trackball()
00134 {
00135 ClearModes();
00136 delete inactive_mode;
00137 }
00138
00139 void Trackball::ClearModes()
00140 {
00141
00142
00143 std::set<TrackMode *> goodModes;
00144 std::map<int, TrackMode *>::iterator it;
00145 for(it = modes.begin(); it != modes.end(); it++)
00146 if ((*it).second) goodModes.insert( (*it).second);
00147
00148 std::set<TrackMode *>::iterator its;
00149 for(its = goodModes.begin(); its != goodModes.end(); its++)
00150 delete *its;
00151
00152 modes.clear();
00153 }
00154
00155 void Trackball::setDefaultMapping () {
00156 idle_and_keys_mode = NULL;
00157
00158 inactive_mode = new InactiveMode ();
00159 ClearModes();
00160 modes[0] = NULL;
00161
00162 modes[BUTTON_MIDDLE | KEY_ALT] =
00163 modes[BUTTON_LEFT] = new SphereMode ();
00164
00165 modes[BUTTON_LEFT | KEY_CTRL] = new PanMode ();
00166
00167 modes[BUTTON_MIDDLE] = new PanMode ();
00168
00169 modes[WHEEL] =
00170 modes[BUTTON_LEFT | KEY_SHIFT] = new ScaleMode ();
00171
00172 modes[BUTTON_LEFT | KEY_ALT] = new ZMode ();
00173
00174 }
00175
00176 void Trackball::SetIdentity() {
00177 track.SetIdentity();
00178 Reset();
00179 }
00180 void Trackball::SetPosition(const Point3f &c, int ) {
00181 center = c;
00182 }
00183
00184 void Trackball::GetView() {
00185 camera.GetView();
00186 }
00187
00188
00189 void Trackball::DrawPostApply() {
00190 if(current_mode !=NULL){
00191 current_mode->Draw(this);
00192 }else{
00193 if (inactive_mode != NULL) inactive_mode->Draw(this);
00194 }
00195 }
00196
00197 void Trackball::Apply () {
00198 glTranslate (center);
00199 glMultMatrix (track.Matrix());
00200 glTranslate (-center);
00201 }
00202
00203 void Trackball::Apply(bool ToDraw) {
00204 Apply();
00205 if(ToDraw){
00206 DrawPostApply();
00207 }
00208 }
00209
00210
00211 void Trackball::ApplyInverse() {
00212 glTranslate(center);
00213 glMultMatrix(track.InverseMatrix());
00214 glTranslate(-center);
00215 }
00216
00217
00218 Matrix44f Trackball::Matrix() const{
00219 #ifndef VCG_USE_EIGEN
00220 Matrix44f r; track.rot.ToMatrix(r);
00221 Matrix44f sr = Matrix44f().SetScale(track.sca, track.sca, track.sca) * r;
00222 Matrix44f s_inv = Matrix44f().SetScale(1/track.sca, 1/track.sca, 1/track.sca);
00223 Matrix44f t = Matrix44f().SetTranslate(s_inv*r.transpose()*center + track.tra - center);
00224
00225 return Matrix44f(sr*t);
00226 #else
00227 Eigen::Quaternionf rot(track.rot);
00228 Eigen::Translation3f tr( (1/track.sca) * (rot.inverse() * center) + track.tra - center );
00229 return ( Eigen::Scaling3f(track.sca) * (rot * tr) ).matrix();
00230 #endif
00231 }
00232
00233 Matrix44f Trackball::InverseMatrix() const{
00234 return Inverse(Matrix());
00235 }
00236
00237 void Trackball::Scale(const float s)
00238 {
00239 track.sca*=s;
00240 }
00241
00242 void Trackball::Translate(Point3f tr)
00243 {
00244 Quaternionf irot = track.rot;
00245 irot.Invert();
00246 track.tra = last_track.tra + irot.Rotate(tr)/track.sca;
00247 }
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281 void Trackball::ToAscii(char* result){
00282 float * f = (float*) &track;
00283 sprintf(result, "trackball(%f,%f,%f,%f,%f,%f,%f,%f)",
00284 f[0],f[1],f[2],f[3],f[4],f[5],f[6],f[7] );
00285 }
00286
00287 bool Trackball::SetFromAscii(const char * st){
00288 float * f = (float*) &track;
00289 int res= sscanf(st, "trackball(%f,%f,%f,%f,%f,%f,%f,%f)",
00290 f+0,f+1,f+2,f+3,f+4,f+5,f+6,f+7 );
00291
00292 return (res==8);
00293 }
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360 void Trackball::Reset() {
00361 track.SetIdentity();
00362 undo_track = track;
00363 std::map<int, TrackMode *>::iterator i;
00364 for(i = modes.begin(); i != modes.end(); i++){
00365 TrackMode * mode=(*i).second;
00366 if(mode!=NULL)
00367 mode->Reset();
00368 }
00369 if (inactive_mode != NULL) inactive_mode->Reset();
00370 }
00371
00372
00373 void Trackball::MouseDown(int button) {
00374 undo_track = track;
00375 current_button |= button;
00376 SetCurrentAction();
00377 Hits.clear();
00378 }
00379 void Trackball::MouseDown(int x, int y, int button) {
00380 undo_track = track;
00381 current_button |= button;
00382 SetCurrentAction();
00383 last_point = Point3f((float)x, (float)y, 0);
00384 Hits.clear();
00385 }
00386
00387 void Trackball::MouseMove(int x, int y) {
00388 if(current_mode == NULL) return;
00389 if(last_point[2] == -1) {
00390 last_point = Point3f((float)x, (float)y, 0);
00391 return;
00392 }
00393 undo_track = track;
00394 current_mode->Apply(this, Point3f(float(x), float(y), 0));
00395 }
00396
00397 bool Trackball::IsAnimating(unsigned int msec){
00398 bool res;
00399 if(idle_and_keys_mode == NULL) res=false; else res=idle_and_keys_mode->IsAnimating(this);
00400
00401 if (!fixedTimestepMode) {
00402 if (msec==0) msec = clock()*1000/CLOCKS_PER_SEC;
00403 if (!res) {
00404 last_time = msec;
00405 }
00406 }
00407 return res;
00408 }
00409
00410 void Trackball::Sync(unsigned int msec) {
00411 if (!fixedTimestepMode) Animate(msec);
00412 }
00413
00414 void Trackball::Animate(unsigned int msec){
00415 unsigned int delta;
00416 if (fixedTimestepMode) delta=msec;
00417 else {
00418 if (msec==0) msec = clock()*1000/CLOCKS_PER_SEC;
00419 delta = msec -last_time;
00420 last_time = msec;
00421 }
00422 if(idle_and_keys_mode == NULL) return;
00423 idle_and_keys_mode->Animate(delta,this);
00424 }
00425
00426 void Trackball::MouseUp(int , int , int button) {
00427 undo_track = track;
00428 ButtonUp(vcg::Trackball::Button(button));
00429
00430
00431 }
00432
00433
00434 void Trackball::MouseWheel(float notch)
00435 {
00436 undo_track = track;
00437 int buttons = current_button;
00438 current_button = WHEEL | (buttons&(KEY_SHIFT|KEY_CTRL|KEY_ALT));
00439 SetCurrentAction();
00440 if (current_mode == NULL)
00441 {
00442
00443
00444 }
00445 else
00446 {
00447 current_mode->Apply(this, notch);
00448 }
00449 current_button = buttons;
00450 SetCurrentAction();
00451 }
00452
00453 void Trackball::MouseWheel(float notch, int button)
00454 {
00455 undo_track = track;
00456 current_button |= button;
00457 SetCurrentAction();
00458 if (current_mode == NULL) {
00459 ScaleMode scalemode;
00460 scalemode.Apply (this, notch);
00461 } else {
00462 current_mode->Apply (this, notch);
00463 }
00464 current_button &= (~button);
00465 SetCurrentAction ();
00466 }
00467
00468 void Trackball::ButtonDown(Trackball::Button button, unsigned int msec) {
00469 Sync(msec);
00470 bool old_sticky=false, new_sticky=false;
00471 assert (modes.count (0));
00472
00473 Button b=Button(current_button & MODIFIER_MASK);
00474 if ( ( modes.count (b) ) && ( modes[b] != NULL ) ) old_sticky = modes[b]->isSticky();
00475
00476 current_button |= button;
00477 b=Button(current_button & MODIFIER_MASK);
00478 if ( ( modes.count (b) ) && ( modes[b] != NULL ) ) new_sticky = modes[b]->isSticky();
00479
00480 if ( !old_sticky && !new_sticky) SetCurrentAction();
00481
00482 }
00483
00484 void Trackball::ButtonUp(Trackball::Button button) {
00485 bool old_sticky=false, new_sticky=false;
00486 assert (modes.count (0));
00487
00488 Button b=Button(current_button & MODIFIER_MASK);
00489 if ( ( modes.count (b) ) && ( modes[b] != NULL ) ) old_sticky = modes[b]->isSticky();
00490
00491 current_button &= (~button);
00492 b=Button(current_button & MODIFIER_MASK);
00493 if ( ( modes.count (b) ) && ( modes[b] != NULL ) ) new_sticky = modes[b]->isSticky();
00494
00495 if ( !old_sticky && !new_sticky) SetCurrentAction();
00496 }
00497
00498 void Trackball::Undo(){
00499 track = undo_track;
00500 if(current_mode != NULL)
00501 current_mode->Undo();
00502 }
00503
00504
00505
00506 void Trackball::SetSpinnable(bool ){}
00507 bool Trackball::IsSpinnable() {
00508 return spinnable;
00509 }
00510 void Trackball::SetSpinning(Quaternionf &){}
00511 void Trackball::StopSpinning(){}
00512 bool Trackball::IsSpinning() {
00513 return spinning;
00514 }
00515
00516
00517 void Trackball::Back(){}
00518 void Trackball::Forward(){}
00519 void Trackball::Home(){}
00520 void Trackball::HistorySize(int ){}
00521
00522 void Trackball::SetCurrentAction ()
00523 {
00524
00525 assert (modes.count (0));
00526 if (!modes.count (current_button & MODIFIER_MASK)) {
00527 current_mode = NULL;
00528 } else {
00529 current_mode = modes[current_button & MODIFIER_MASK];
00530 if(current_mode != NULL)
00531 current_mode->SetAction();
00532 }
00533 last_point = Point3f (0, 0, -1);
00534 last_track = track;
00535 }
00536
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556