Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

recorder.C

Go to the documentation of this file.
00001 #include "disp/recorder.H"
00002 
00003 int 
00004 CameraPath::write_stream( iostream& os ) { 
00005 
00006    STDdstream out(&os);
00007    out << state_list.num();
00008    for ( int i = 0 ; i < state_list.num() ; i++ ) { 
00009       out << state_list[i]->t();
00010       out << state_list[i]->cam()->data();
00011    }
00012    return 1;
00013 }
00014 
00015 int 
00016 CameraPath::read_stream ( iostream& is ) { 
00017 
00018    STDdstream in(&is);
00019    int fnum;
00020    double ftime;
00021    CAMptr cptr = new CAM ( "state" );  
00022    in >> fnum;
00023    for ( int i=0; i < fnum; i++ ) { 
00024       cerr << "frame " << i << endl;
00025       in >> ftime;
00026       CAMdataptr cd = cptr->data(); // XXX - temp variable to avoid warnings
00027       in >> cd;                 //          here
00028       state_list += new CamState ( ftime, cptr );
00029    }
00030    return 1;
00031 }
00032 
00033 Recorder::Recorder(VIEWptr view) : 
00034    _view(view),
00035    _swatch(new stop_watch()),
00036    _cur_path(NULL),
00037    _cur_path_num(-1),
00038    _fps(24),
00039    _on(false),
00040    _record_on(false), 
00041    _play_on(false), 
00042    _render_on(false), 
00043    _paused(false),
00044    _path_pos(0), 
00045    _start_time(0),
00046    _path_time(0),
00047    _next_time(0),
00048    _sync(false),
00049    _ui(NULL)
00050 {  
00051 }
00052 
00053 
00054 Recorder::~Recorder() 
00055 { 
00056    delete _ui; 
00057    for ( int i=0; i < _campaths.num() ; i++ )
00058       delete _campaths[i];
00059    _view = 0;
00060 }
00061 
00062 
00063 void
00064 Recorder::activate() 
00065 {
00066    assert (_ui);
00067   
00068    _ui->show();
00069   
00070    _on = true;
00071    _swatch->set();
00072   
00073 }       
00074 
00075 void
00076 Recorder::deactivate() 
00077 {
00078   
00079    if ( _ui) _ui->hide();
00080    _on = false;
00081   
00082 }
00083 
00084 
00085 
00086 // power button
00087 
00088 bool
00089 Recorder::on() { return _on; }
00090 
00091  
00092 void 
00093 Recorder::rec_record() 
00094 { 
00095    _swatch->set();
00096    _record_on = true;
00097    _render_on = false;
00098 
00099 
00100    if ( _paused && _play_on ) {    //stop during playback to overwrite
00101       _swatch->set(_start_time);
00102       // ARRAY < CamState *> tmp = _cur_path->state_list;
00103       _paused = false;
00104    } 
00105 
00106    _play_on = false;
00107    _next_time = 0;
00108    _start_time =0;
00109 }
00110 
00111 // play button
00112  
00113 void 
00114 Recorder::rec_play()
00115 {
00116 
00117    _play_on     = true;
00118    _record_on   = false;
00119    _target_frame = _cur_path->state_list.num()-1;
00120    _swatch->set();
00121   
00122    _paused      = false;
00123   
00124    cerr << "\nsetplay\n";
00125 }
00126 void
00127 Recorder::replay() { 
00128 
00129    //
00130    // set proper environment to
00131    // replay things exactly
00132    //
00133    //   
00134    //
00135 
00136    _path_pos = 0;
00137    _ui->set_frame_num( _path_pos );
00138 
00139    _play_all_frames = true;
00140    _ui->set_play_all_frames();
00141 
00142    //start playing again
00143       
00144    _play_on = true;
00145    _swatch->set();
00146    _paused  = false;
00147    _ui->update_checks();
00148 }
00149 
00150 void 
00151 Recorder::set_sync(bool s) {  //accessor for the ui  
00152    if ( s && !_sync ) { 
00153       _target_frame = _path_pos;
00154       replay();
00155    }
00156    else if ( !s && _sync ) { 
00157       _path_time = _cur_path->state_list[_path_pos]->t();
00158    }
00159    _sync = s;
00160 }
00161 //step forward / back
00162 void 
00163 Recorder::step_fwd()
00164 {
00165    //using the step functions shifts you into play all frames mode
00166    _play_all_frames=true;
00167   
00168    _play_on     = true;
00169    _paused      = false;
00170    _target_frame = min ( _path_pos+1 , _cur_path->state_list.num()-1 );
00171    _path_pos = _target_frame;
00172    _ui->set_frame_num ( _path_pos );
00173    _path_time   = _cur_path->state_list[_path_pos]->t();
00174    _start_time  = _path_time;
00175 
00176    cerr << "step fwd to: " << _path_pos << "\n";
00177 }
00178 
00179 void 
00180 Recorder::step_rev()
00181 {
00182    //using the step functions shifts you into play all frames mode
00183    _play_all_frames=true;
00184   
00185    _play_on     = true;
00186    _paused      = false;
00187    _target_frame = max (_path_pos-1 , 0 );
00188    _path_pos = _target_frame;
00189    _ui->set_frame_num ( _path_pos );
00190 
00191    _path_time   = _cur_path->state_list[_path_pos]->t();
00192    _start_time  = _path_time;
00193 
00194    //can't go 'backward' in sync mode 
00195    //start from the beginning and 
00196    //work forward 
00197    if ( _sync )  replay();
00198   
00199 
00200    cerr << "step rev to: " << _path_pos << "\n";
00201 }
00202 
00203 
00204 // stop everything, time to zero, 
00205 void 
00206 Recorder::rec_stop()
00207 {
00208    _play_on           = false;
00209    _record_on      = false;
00210    _render_on      = false;
00211    _paused         = false;
00212    _path_pos       = 0;
00213    _path_time      = 0;
00214    _target_frame   = _cur_path->state_list.num()-1 ;
00215    cerr << "\nstop\n";
00216 } 
00217 
00218 // pause record or playback -- hit pause again to resume.
00219 //  REC OR PLAY WILL RESET, NOT UNPAUSE
00220 
00221 void 
00222 Recorder::rec_pause()
00223 {
00224    if (!_paused) { // when you pause, preserve the time you paused at 
00225       _start_time = _swatch->elapsed_time();
00226    }
00227    else {       // when you unpause, restore to that time.
00228       _swatch->set( _start_time );
00229    }
00230    _paused = !_paused;
00231 }
00232 
00233 //render out to images?
00234 void 
00235 Recorder::render_on()
00236 {
00237    _render_on   =       true;
00238 }
00239 
00240 void Recorder::render_off()
00241 {
00242    _render_on   =       false;
00243 }
00244 
00245 
00246 int 
00247 Recorder::new_path() 
00248 { 
00249 
00250    str_ptr filename = ( **_name_buf != NULL ) ? _name_buf : str_ptr ( "path");
00251    _campaths.add ( new CameraPath( filename ));
00252    int id       = _campaths.num()-1;
00253    _ui->add_path_entry ( id, (char*) **filename );
00254    _cur_path_num = id;
00255    _cur_path    = _campaths[_cur_path_num];
00256    _path_pos    = 0;
00257    _path_time   = 0;
00258    _ui->sync_live();
00259    return 1;
00260 }
00261 
00262 
00263 int 
00264 Recorder::del_path(int k) 
00265 { 
00266    delete _campaths[k];
00267    _campaths[k] = NULL;
00268    _cur_path_num = 1;
00269    _cur_path = NULL;
00270    _ui->sync_live();
00271    set_path(-1);
00272    return _ui->del_path_entry(k);
00273   
00274 }
00275 
00276 
00277 int 
00278 Recorder::save_path ( int pathnum ) { 
00279   
00280    if ( _campaths.num() == 0 || pathnum < 0 || pathnum >= _campaths.num() ) return 0;
00281  
00282    CameraPath * cpath = _campaths[pathnum];
00283    fstream fout;
00284    fout.open ( (char*) **(cpath->get_name()),ios::out );
00285    if ( !fout )
00286       {
00287          err_ret("Recorder::save_path: error writing file");
00288       }
00289    int tmp = cpath->write_stream ( fout );
00290    fout.close();
00291    err_msg("wrote %s", (char*) **(cpath->get_name()) );
00292    return tmp;
00293 }
00294 
00295 int 
00296 Recorder::open_path ( char * filename ) { 
00297    cerr << "Recorder::open_path  - - Nothin yet" << endl;
00298    cerr << "filename is " << filename << endl; 
00299    fstream fin;
00300 #if (defined (WIN32) && defined(_MSC_VER) && (_MSC_VER <=1300)) /*VS 6.0*/
00301    fin.open ( filename, ios::in | ios::nocreate);
00302 #else
00303    fin.open ( filename, ios::in );
00304 #endif
00305    if ( !fin )
00306       {
00307          err_ret("Recorder::open_path: error opening file");
00308       }
00309    CameraPath * tmp = new CameraPath ( filename );
00310    if ( !tmp->read_stream (fin )) return 0;
00311    fin.close();
00312    _campaths.add ( tmp );
00313    _ui->add_path_entry ( _campaths.num()-1, filename );
00314    //set the opened path to be the current path
00315    cerr << "num is " << _campaths.num() << endl;
00316    set_path ( _campaths.num()-1 ) ;
00317    cerr << "end open " << endl;
00318 
00319    return 1;
00320 }
00321 
00322 
00323 //set current path
00324 int 
00325 Recorder::num_paths() 
00326 { 
00327    return _campaths.num(); 
00328 }
00329   
00330 int 
00331 Recorder::set_path( int pathnum ) 
00332 { 
00333    if ( pathnum < 0 || pathnum >= _campaths.num() ) {
00334       cerr << "illegal path value:" << pathnum  ;
00335       _cur_path = NULL;
00336       _cur_path_num = -1;
00337       _ui->sync_live();
00338       return -1;
00339    }
00340    _cur_path    = _campaths[pathnum];
00341    _cur_path_num = pathnum;
00342    _path_pos    = 0;
00343    _path_time   = 0;
00344    _ui->sync_live();
00345    return _cur_path_num;
00346 
00347 }
00348 
00349 
00350 int *
00351 Recorder::get_path () { 
00352    return &_cur_path_num;
00353 }
00354  
00355 //frame rate;
00356   
00357 int* Recorder::get_fps() { return &_fps; }
00358 
00359 int Recorder::set_fps(int fps) { return (_fps = fps); }  
00360 
00361 //save/load paths to file 
00362 
00363 /*
00364  * draw loop concerns;
00365  */
00366 
00367 
00368 //callbacks before/after frame is drawn;
00369 
00370 void
00371 Recorder::set_pos(int pos) 
00372 { 
00373    if ( pos > 0 &&  pos <  _cur_path->state_list.num() ) { 
00374       //setting pos enables play all frames
00375       _play_all_frames     = true;
00376          
00377       if ( _sync ) { 
00378          if ( pos > _path_pos) { 
00379             target_frame()      = pos ;  
00380             _play_on                    = true;
00381             _paused                     = false;
00382          } else if ( pos < _path_pos ) { 
00383             target_frame() = pos ;
00384             replay();
00385             _paused                     = false;
00386          }
00387       } else { 
00388          _target_frame     = pos;
00389          _path_pos         = pos; 
00390          _play_on          = true;
00391          _paused           = false;
00392       }
00393    }
00394 } 
00395 
00396 void 
00397 Recorder::pre_draw_CB()
00398 {
00399    //set/get time, cam_pos, etc...
00400    CAM tmp("temp");
00401    
00402    if ( !_cur_path )
00403       return;
00404 
00405    if ( !_paused ) { 
00406 
00407       if ( _sync && ( _path_pos > _target_frame ) )
00408          replay();
00409       
00410       double ttime = _swatch->elapsed_time();
00411       
00412       if ( _play_on )  { 
00413          
00414          //forward to closest path after current time
00415          if ( _path_pos >= _cur_path->state_list.num() ||
00416               _cur_path->state_list.num() == 0 ) { 
00417             cerr << "end of state list reached::camera stop" << endl;
00418             rec_stop() ; 
00419             return; 
00420          } 
00421          
00422          
00423          if ( _render_on || _play_all_frames ) {
00424 
00425             CAMptr mine = (_cur_path->state_list[_path_pos]->cam());          
00426             _view->cam()->data()->set_from ( mine->data()->from() );
00427             _view->cam()->data()->set_at ( mine->data()->at() );
00428             _view->cam()->data()->set_up ( mine->data()->up() );
00429             _view->cam()->data()->set_center ( mine->data()->center() );
00430             _view->cam()->data()->set_focal ( mine->data()->focal() );
00431             _view->cam()->data()->set_persp ( mine->data()->persp() );
00432             _view->cam()->data()->set_iod ( mine->data()->iod() );
00433             _view->set_frame_time( _cur_path->state_list[_path_pos]->t());
00434             
00435             //update UI to reflect which frame is being shown
00436             _ui->set_frame_num ( _path_pos );
00437             
00438             if ( _sync ) { 
00439                if ( _path_pos == _target_frame )   { 
00440                   cerr << "at target frame: pausing:" << endl ; 
00441                   rec_pause(); 
00442                } else if ( _path_pos > _target_frame ) { 
00443                   cerr << "ack, we need to set back"<< endl; 
00444                   replay(); 
00445                } else _path_pos++;
00446             } else { 
00447                if ( _path_pos >= _cur_path->state_list.num() -1 ) rec_stop();
00448                else if ( _path_pos >= _target_frame ) rec_pause();
00449                else _path_pos++;
00450             }   
00451          } else {                
00452             while ( _cur_path->state_list[_path_pos]->t() < ttime ) { 
00453                _path_pos++;
00454                if ( _path_pos >= _cur_path->state_list.num()) { 
00455                   rec_stop(); 
00456                   return;
00457                }
00458             }
00459             
00460             CAMptr mine = (_cur_path->state_list[_path_pos]->cam());          
00461             _view->cam()->data()->set_from ( mine->data()->from() );
00462             _view->cam()->data()->set_at ( mine->data()->at() );
00463             _view->cam()->data()->set_up ( mine->data()->up() );
00464             _view->cam()->data()->set_center ( mine->data()->center() );
00465             _view->cam()->data()->set_focal ( mine->data()->focal() );
00466             _view->cam()->data()->set_persp ( mine->data()->persp() );
00467             _view->cam()->data()->set_iod ( mine->data()->iod() );
00468             
00469             _view->set_frame_time( _cur_path->state_list[_path_pos]->t());
00470             _ui->set_frame_num ( _path_pos );
00471          }
00472          //set view camera to stored camera
00473       } else if ( _record_on ) { 
00474          while ( ttime >= _next_time ) {
00475             _cur_path->add( _next_time, _view->cam()); //always adds to end of list
00476             _path_pos = _cur_path->state_list.num()-1; 
00477             _next_time += 1.0/_fps; //set next time to get data
00478             
00479          }       
00480       }
00481    } else if ( _paused && _play_on) {
00482       if ( _sync && ( _path_pos > _target_frame )  ) {
00483          replay();
00484          _paused = true; 
00485       }
00486       
00487       if ( _path_pos < 0 ||
00488            _path_pos >= _cur_path->state_list.num() ||
00489            _cur_path->state_list.num() == 0 ) {
00490          rec_stop();
00491          return;
00492       }
00493    }
00494 
00495    _ui->update_checks();
00496 }
00497   
00498 void 
00499 Recorder::post_draw_CB()
00500 {
00501    if ( !_paused && _render_on && _play_on) { 
00502 
00503       /*make directory */
00504 
00505       char num[32]; //yes, this is absurd
00506       sprintf (num, "%06d", _path_pos); //1 million frames
00507 
00508       //mkdir("imagedir");
00509       str_ptr base_dir = "imagedir/";
00510       str_ptr filename = base_dir + _cur_path->get_name() +
00511          str_ptr("_") + str_ptr( num ) + str_ptr (".png");
00512       cerr << "writing " << filename << "\n";
00513       int w,h; VIEW_SIZE (w,h);
00514       Image output (w,h,3);
00515       //sketchy/
00516 
00517       VIEWimpl* impl = _view->impl();
00518 
00519       if (impl) { 
00520          _view->set_grabbing_screen(1);
00521          impl->prepare_buf_read();
00522          impl->read_pixels(output.data());
00523          _view->set_grabbing_screen(0);
00524          impl->end_buf_read();
00525       }
00526 
00527       if ( !output.write_png(**filename)) {cerr << "error writing file!"; } 
00528   
00529    }
00530 }
00531 

Generated on Mon Sep 18 11:39:33 2006 for jot by  doxygen 1.4.4