00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef STATECONTROL_H
00020 #include "StateControl.h"
00021 #endif
00022
00023 #ifndef DISPATCHER_H
00024 #include "Dispatcher.h"
00025 #endif
00026
00027 #ifndef _GLIBCXX_SSTREAM
00028 #include <sstream>
00029 #endif
00030
00031 #ifndef _GLIBCXX_IOSTREAM
00032 #include <iostream>
00033 #endif
00034
00035 #ifndef OSGDB_READFILE
00036 #include <osgDB/ReadFile>
00037 #endif
00038
00039 #ifndef OSG_VEC3D
00040 #include <osg/Vec3d>
00041 #endif
00042
00043 #ifndef OSG_QUAT
00044 #include <osg/Quat>
00045 #endif
00046
00047 #ifndef OSG_POSITIONATTITUDETRANSFORM
00048 #include <osg/PositionAttitudeTransform>
00049 #endif
00050
00051 #ifndef OSG_AUTOTRANSFORM
00052 #include <osg/AutoTransform>
00053 #endif
00054
00055 #ifndef OSG_VEC4
00056 #include <osg/Vec4>
00057 #endif
00058
00059 #ifndef OSG_MATRIX
00060 #include <osg/Matrix>
00061 #endif
00062
00063 #ifndef OSG_MATRIXTRANSFORM
00064 #include <osg/MatrixTransform>
00065 #endif
00066
00067 #ifndef OSG_GEOMETRY
00068 #include <osg/Geometry>
00069 #endif
00070
00071 #ifndef OSG_STATESET
00072 #include <osg/StateSet>
00073 #endif
00074
00075 #ifndef OSGTEXT_TEXT
00076 #include <osgText/Text>
00077 #endif
00078
00079 #ifndef OSGGA_NODETRACKERMANIPULATOR
00080 #include <osgGA/NodeTrackerManipulator>
00081 #endif
00082
00083
00084
00085 #ifndef ANIMATIONPATHTIMEDCALLBACK_H
00086 #include "AnimationPathTimedCallback.h"
00087 #endif
00088
00089 #ifndef USERINTERFACE_H
00090 #include "UserInterface.h"
00091 #endif
00092
00093 #ifndef _GLIBCXX_LIST
00094 #include <list>
00095 #endif
00096
00097 #ifndef GLOBALS_H
00098 #include "Globals.h"
00099 #endif
00100
00101 #include <algorithm>
00102
00103
00104
00105
00106 void* go(void* none){
00107
00108 StateControl *st = static_cast<StateControl*>( none );
00109
00110 osg::Group* root = dynamic_cast<osg::Group*>( st->getOsgRoot() );
00111 assert( root != NULL );
00112 osgProducer::Viewer* viewer = new osgProducer::Viewer();
00113
00114 st->view = viewer;
00115
00116 viewer->setUpViewer(osgProducer::Viewer::STANDARD_SETTINGS);
00117 viewer->setSceneData(root);
00118
00119 root->addChild( st->ui->createHud() );
00120
00121 viewer->getEventHandlerList().push_front(new UserEventHandler(viewer, st->ui ));
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132 viewer->realize();
00133
00134 while( !viewer->done() ){
00135
00136 st->dis->dispatchMessage();
00137 viewer->sync();
00138 viewer->update();
00139 viewer->frame();
00140 }
00141 cout << "Viewer exit." << endl;
00142
00143 exit(0);
00144
00145 return NULL;
00146 }
00147
00148 StateControl::StateControl(Dispatcher* disp){
00149
00150 dis = disp;
00151 ui = new UserInterface(this);
00152 view = NULL;
00153 pausetime = 0.0;
00154
00155 readMapping(CARGO);
00156 readMapping(TRANSPORT);
00157 readMapping(LOCATION);
00158
00159 osgRoot = new osg::Group;
00160
00161
00162 pthread_create(&osg_update_loop, NULL, go, (void*)this);
00163 }
00164
00165 bool StateControl::readMapping(int type){
00166
00167 string line;
00168 vector<string> mapping;
00169 ifstream mappingfile;
00170 switch(type){
00171 case CARGO:
00172 mappingfile.open("../cargo");
00173 break;
00174 case TRANSPORT:
00175 mappingfile.open("../transport");
00176 break;
00177 case LOCATION:
00178 mappingfile.open("../locations");
00179 break;
00180 default:
00181 cout << "Warning: (Statecontrol::readMapping()) Error readMapping: Trying to read type " << type << ". Doesn't exist." << endl;
00182 break;
00183 }
00184
00185 if (mappingfile.is_open()){
00186 while (!mappingfile.eof()){
00187 getline (mappingfile, line);
00188 mapping.push_back(line);
00189 }
00190 } else {
00191 cout << "Error: (Statecontrol::readMapping()) Cannot open file for type " << type << "." << endl;
00192 exit(0);
00193 }
00194 mappingfile.close();
00195
00196 int spaceIndex;
00197 if(type == LOCATION){
00198 vector<string>::iterator it = mapping.begin();
00199
00200 tilescale = atof((*it).c_str());
00201 it++;
00202 for ( ; it != mapping.end() ; it++) {
00203 spaceIndex = (*it).find(" ", 0);
00204 string mapNrStr = it->substr(0, (spaceIndex));
00205 string mapValue = it->substr((spaceIndex+1));
00206 int mapNr= atoi(mapNrStr.c_str());
00207 tileMap[mapNr]= ("../"+mapValue);
00208 }
00209 } else if(type == CARGO) {
00210 spaceIndex = (mapping.at(0)).find(" ",0);
00211 cargomodel = ("../"+(mapping.at(0).substr(0,spaceIndex)));
00212 cargoscale = atof(mapping.at(0).substr(spaceIndex+1).c_str() );
00213 } else if(type==TRANSPORT) {
00214 spaceIndex = (mapping.at(0)).find(" ",0);
00215 transmodel = ("../"+(mapping.at(0).substr(0,spaceIndex)));
00216 transscale = atof(mapping.at(0).substr(spaceIndex+1).c_str() );
00217 }
00218
00219 return true;
00220 }
00221
00222 StateControl::~StateControl(){
00223 delete ui;
00224 }
00225
00226 bool StateControl::location(int loc_id, shared_ptr<string> desc, unsigned int capacity, double dist, double s, int tile, double width, double height, double x, double y, double rot){
00227
00228 osg::Node* locModel = osgDB::readNodeFile(tileMap[tile]);
00229 assert( locModel != NULL );
00230
00231 if ( world[loc_id] != NULL ) {
00232 cout << "Warning: (StateControl::location()) Something with id " << loc_id << " allready exists! No location created." << endl;
00233 #if STRICT
00234 exit(1);
00235 #endif
00236 return false;
00237 }
00238
00239
00240 if(width <=0 || height <=0 ){
00241 cout << "Warning: (StateControl::location()) Incorrect tile dimensions: width and height must be > 0" << endl;
00242 #if STRICT
00243 exit(1);
00244 #endif
00245
00246 width = 0;
00247 height = 0;
00248 }
00249
00250
00251 Infrastructure* inf = new Infrastructure(loc_id, LOCATION, s, tile, capacity, dist, locModel);
00252 world[loc_id]=inf;
00253
00254
00255 osg::AutoTransform* scaleTrans = new osg::AutoTransform();
00256 scaleTrans->setScale(osg::Vec3d(tilescale * (width/2.0), tilescale,tilescale * (height/2.0)));
00257
00258
00259 osg::PositionAttitudeTransform* locXform = new osg::PositionAttitudeTransform();
00260 locXform->setPosition(osg::Vec3d(x,0,y));
00261
00262
00263 double rrot = (rot/360.0)*(2.0*M_PI);
00264 locXform->setAttitude(osg::Quat(rrot, osg::Vec3d(0,1,0)));
00265
00266
00267 scaleTrans->addChild(locModel);
00268 locXform->addChild(scaleTrans);
00269 osgRoot->addChild(locXform);
00270
00271
00272 inf->initLabel();
00273 inf->setLabel(desc);
00274 inf->setDescription(*desc);
00275
00276 return true;
00277 }
00278
00279 bool StateControl::drv(int Rt, int Ri_a, int Ri_b, int Ri_d, double ts, double te){
00280
00281 Infrastructure *tmp_a = dynamic_cast<Infrastructure*>( world[Ri_a] );
00282 Infrastructure *tmp_b = dynamic_cast<Infrastructure*>( world[Ri_b] );
00283 Infrastructure *tmp_d = dynamic_cast<Infrastructure*>( world[Ri_d] );
00284 Transport *tmp_t = dynamic_cast<Transport*>( world[Rt] );
00285 assert ( !(tmp_a == NULL) && !(tmp_b == NULL) && !(tmp_d == NULL) && !(tmp_t == NULL) );
00286
00287 if( !((Infrastructure*)tmp_a)->isArc(Ri_b) ){
00288 cout << "Warning: (StateControl::drv()) No arc from " << Ri_a << " to " << Ri_b << ", drive action invalid." << endl;
00289 #if STRICT
00290 exit(1);
00291 #endif
00292 }
00293
00294 osg::AnimationPath* mypath = new osg::AnimationPath();
00295 mypath->setLoopMode(osg::AnimationPath::NO_LOOPING);
00296
00297 osg::Vec3d pos_a = tmp_a->getPosition();
00298 osg::Vec3d pos_b = tmp_b->getPosition();
00299
00300
00301 double angle = atan2( (pos_a.x() - pos_b.x()), (pos_b.z()-pos_a.z()) );
00302 osg::Quat myquat((-1*angle), osg::Vec3d(0,1,0));
00303
00304 mypath->insert(0.0, osg::AnimationPath::ControlPoint(pos_a, myquat));
00305
00306 mypath->insert((double)(te - ts), osg::AnimationPath::ControlPoint(pos_b, myquat));
00307
00308 osg::PositionAttitudeTransform* locXform = dynamic_cast<osg::PositionAttitudeTransform*>(tmp_t->getModel()->getParent(0)->getParent(0));
00309 assert( locXform != NULL);
00310
00311 AnimationPathTimedCallback* timePath = new AnimationPathTimedCallback();
00312 timePath->setAnimationPath(mypath);
00313 timePath->setTimeOffset(0.0);
00314 timePath->setTimeReference(dis);
00315 timePath->setTimeMultiplier(dis->getTimeFlow());
00316
00317 locXform->setUpdateCallback( ((osg::NodeCallback*)timePath) );
00318
00319 tmp_t->setDestination( tmp_d->getModel()->getName() );
00320
00321 return true;
00322 }
00323
00324 bool StateControl::load(int Rt, int Ri, int C, double ts, double tf){
00325 Cargo *cgl = dynamic_cast<Cargo*>( world[C] );
00326 Infrastructure *infl = dynamic_cast<Infrastructure*>( world[Ri] );
00327 Transport *tnl = dynamic_cast<Transport*>( world[Rt] );
00328 assert ( !(cgl == NULL) && !(infl == NULL) && !(tnl == NULL) );
00329
00330 int Cid = infl->removeCarg(C);
00331 if(Cid != C){
00332 cout << "Warning: (StateControl::load())Cargo" << cgl->getModel()->getName() <<" not at location " << infl->getModel()->getName() << endl;
00333 #if STRICT
00334 exit(1);
00335 #endif
00336 return false;
00337 }
00338 tnl->setCargo(C , cgl->getModel()->getName() );
00339
00340
00341
00342 osg::PositionAttitudeTransform *tr_pat = dynamic_cast<osg::PositionAttitudeTransform*>( tnl->getModel()->getParent(0)->getParent(0) );
00343 assert( tr_pat != NULL );
00344
00345 osg::AutoTransform *carg_at = dynamic_cast<osg::AutoTransform*>( cgl->getModel()->getParent(0) );
00346 assert( carg_at != NULL );
00347
00348 carg_at->setPosition( osg::Vec3d( 0, -2, 0) );
00349
00350 tr_pat->addChild( carg_at );
00351
00352
00353 osg::PositionAttitudeTransform *inf_pat = dynamic_cast<osg::PositionAttitudeTransform*>(infl->getModel()->getParent(0)->getParent(0));
00354 assert( inf_pat != NULL );
00355
00356 inf_pat->removeChild( carg_at );
00357
00358
00359 if (tnl->isSelected())updateHudText(tnl);
00360 return true;
00361
00362 }
00363
00364 bool StateControl::unload(int Rt, int Ri, int C, double ts, double te){
00365 Cargo *cgu = dynamic_cast<Cargo*>( world[C] );
00366 Infrastructure *infu = dynamic_cast<Infrastructure*>( world[Ri] );
00367 Transport *tnu = dynamic_cast<Transport*>( world[Rt] );
00368 assert ( !(cgu == NULL) && !(infu == NULL) && !(tnu == NULL) );
00369
00370 if( tnu->getCargo() != C){
00371 cout << "Warning: (StateControl::Cargo()) Cargo " << cgu->getModel()->getName() << " is not on transport " << tnu->getModel()->getName() << endl;
00372 #if STRICT
00373 exit(1);
00374 #endif
00375 return false;
00376 }
00377
00378
00379 if( !infu->addCarg( ( tnu->clearCargo() ) ) ){
00380 cout << "Warning: (StateControl::unload()) Failed to unload cargo " << cgu->getModel()->getName() << " from transport " << tnu->getModel()->getName() << " to location " << infu->getModel()->getName() << endl;
00381 #if STRICT
00382 exit(1);
00383 #endif
00384 return false;
00385 }
00386
00387
00388
00389 osg::PositionAttitudeTransform *loc_pat = dynamic_cast<osg::PositionAttitudeTransform*>( infu->getModel()->getParent(0)->getParent(0) );
00390 assert( loc_pat != NULL );
00391
00392 osg::AutoTransform *carg_at = dynamic_cast<osg::AutoTransform*>( cgu->getModel()->getParent(0) );
00393 assert( carg_at != NULL );
00394
00395 carg_at->setPosition( osg::Vec3d( -20, 0, 20) );
00396
00397 loc_pat->addChild( carg_at );
00398
00399
00400 osg::PositionAttitudeTransform *tr_pat = dynamic_cast<osg::PositionAttitudeTransform*>( tnu->getModel()->getParent(0)->getParent(0) );
00401 assert( tr_pat != NULL );
00402
00403 tr_pat->removeChild( carg_at );
00404
00405
00406 if ( tnu->isSelected() )
00407 updateHudText(tnu);
00408
00409 return true;
00410 }
00411
00412 bool StateControl::setspdinviz(int R, double s){
00413
00414 Infrastructure *tmpR = dynamic_cast<Infrastructure*>( world[R] );
00415 if (tmpR == NULL)
00416 Transport *tmpR = dynamic_cast<Transport*>( world[R] );
00417 assert( tmpR != NULL);
00418
00419 bool ret = tmpR->setSpeed(s);
00420 if( tmpR->isSelected() )
00421 updateHudText(tmpR);
00422
00423 return ret;
00424 }
00425
00426 bool StateControl::trnew(int Rt, shared_ptr<string> desc, int Ri, double s){
00427
00428 osg::Node* trModel = osgDB::readNodeFile(transmodel);
00429 assert( trModel != NULL );
00430
00431 if( world[Rt] != NULL ){
00432 cout << "Warning: (StateControl::trnew()) Something with id " << Rt << " allready exists. No transport created." << endl;
00433 #if STRICT
00434 exit(1);
00435 #endif
00436 return false;
00437 }
00438
00439
00440 Transport* trans = new Transport(Rt,TRANSPORT, s, trModel);
00441 world[Rt]=trans;
00442
00443 Infrastructure* loc = dynamic_cast<Infrastructure*>( world[Ri] );
00444 assert (loc != NULL);
00445
00446
00447 osg::Vec3d locpos = loc->getPosition();
00448
00449
00450 osg::PositionAttitudeTransform* trpos = new osg::PositionAttitudeTransform();
00451 trpos->setPosition(locpos);
00452
00453
00454 osg::AutoTransform* mytransform = new osg::AutoTransform();
00455 mytransform->setScale(osg::Vec3d(transscale,transscale,transscale));
00456
00457
00458 mytransform->addChild(trModel);
00459 trpos->addChild(mytransform);
00460 osgRoot->addChild(trpos);
00461
00462
00463 trans->initLabel();
00464 trans->setLabel(desc);
00465 trans->setDescription(*desc);
00466
00467 return true;
00468 }
00469
00470 bool StateControl::trdel(int Rt){
00471 Transport* trn = dynamic_cast<Transport*>( world[Rt] );
00472 assert( trn != NULL );
00473
00474 if( trn->isSelected() )
00475 deselectObject(trn);
00476
00477 if(trn->getCargo() != -1){
00478 cout << "Warning: (StateControl::trdel()) Transport " << (trn->getModel())->getName() << " was carrying cargo with id " << trn->getCargo() << endl;
00479 #if STRICT
00480 exit(1);
00481 #endif
00482 }
00483
00484 osg::PositionAttitudeTransform* trpos = dynamic_cast<osg::PositionAttitudeTransform*>( trn->getModel()->getParent(0)->getParent(0) );
00485 assert( trpos != NULL );
00486
00487 osgRoot->removeChild( trpos );
00488
00489 return true;
00490 }
00491
00492 bool StateControl::newcarg(int C, shared_ptr<string> desc, int Ri){
00493
00494 if( world[C] != NULL ){
00495 cout << "Warning: (StateControl::newcarg()) Something with" << C << "allready exists, no cargo made" << endl;
00496 #if STRICT
00497 exit(1);
00498 #endif
00499 return false;
00500 }
00501
00502 Infrastructure* loc = dynamic_cast<Infrastructure*>( world[Ri] );
00503 assert( loc != NULL );
00504
00505 osg::Node* cgModel = osgDB::readNodeFile(cargomodel);
00506 assert( cgModel != NULL );
00507
00508
00509 Cargo* carg = new Cargo(C, CARGO, cgModel);
00510 world[C]=carg;
00511 loc->addCarg(C);
00512
00513
00514 osg::PositionAttitudeTransform* cgpos = dynamic_cast<osg::PositionAttitudeTransform*>( loc->getModel()->getParent(0)->getParent(0) );
00515 assert( cgpos != NULL );
00516
00517
00518 osg::AutoTransform* mytransform = new osg::AutoTransform();
00519 mytransform->setScale( osg::Vec3d( cargoscale, cargoscale, cargoscale) );
00520 mytransform->setPosition( osg::Vec3d( 20, 0, 20) );
00521
00522
00523 mytransform->addChild(cgModel);
00524 cgpos->addChild(mytransform);
00525
00526
00527 carg->initLabel();
00528 carg->setLabel(desc);
00529 carg->setDescription(*desc);
00530
00531 return true;
00532 }
00533
00534 bool StateControl::rmcarg(int C){
00535
00536 Cargo* carg = dynamic_cast<Cargo*>( world[C] );
00537 assert( carg != NULL );
00538
00539 if( carg->isSelected() )
00540 deselectObject(carg);
00541
00542
00543 osg::PositionAttitudeTransform* cgpos = dynamic_cast<osg::PositionAttitudeTransform*>( carg->getModel()->getParent(0)->getParent(0) );
00544 assert( cgpos != NULL );
00545
00546 osg::AutoTransform* carg_at = dynamic_cast<osg::AutoTransform*>( carg->getModel()->getParent(0) );
00547 assert( carg_at != NULL );
00548
00549 cgpos->removeChild( carg_at );
00550
00551 return true;
00552 }
00553
00554 bool StateControl::arc(int Ri_a, int Ri_b){
00555 Infrastructure *a = dynamic_cast<Infrastructure*>( world[Ri_a] );
00556 Infrastructure *b = dynamic_cast<Infrastructure*>( world[Ri_b] );
00557 assert( !(a == NULL) && !(b == NULL) );
00558
00559
00560 if( a->addArc(Ri_b) )
00561 return true;
00562 else {
00563 cout<< "Warning: (StateControl::arc()) Arc between " << a->getModel()->getName() << " and " << b->getModel()->getName() << " allready exists." << endl;
00564 #if STRICT
00565 exit(1);
00566 #endif
00567 return false;
00568 }
00569 }
00570
00571 bool StateControl::adjustTimeFlow(double adj){
00572
00573 double oldTime = dis->getTimeFlow();
00574 if( oldTime <= 0 && adj <= 0 )
00575 return dis->setTimeFlow(0.0);
00576 else
00577 return dis->setTimeFlow( (dis->getTimeFlow() + adj));
00578 }
00579
00580 double StateControl::getTimeFlow(){
00581 return dis->getTimeFlow();
00582 }
00583
00584 double StateControl::getTime(){
00585 return dis->getTime();
00586 }
00587
00588 void StateControl::pause(){
00589 if(dis->getTimeFlow() != 0.0){
00590 pausetime = dis->getTimeFlow();
00591 dis->setTimeFlow(0.0);
00592 }
00593 else{
00594 dis->setTimeFlow(pausetime);
00595 pausetime = 0.0;
00596 }
00597
00598 return;
00599 }
00600
00601 int StateControl::selectObject(TraplasObject* obj){
00602
00603 obj->select();
00604
00605
00606
00607
00608 ui->setHUDText(obj->toString());
00609
00610 selected.push_back(obj);
00611 return selected.size();
00612 }
00613
00614 TraplasObject* StateControl::getLastSelected(){
00615 if(!selected.empty())
00616 return selected.at((selected.size()-1));
00617 else
00618 return NULL;
00619 }
00620
00621 int StateControl::deselectObject(TraplasObject* obj){
00622
00623
00624
00625 vector<TraplasObject*>::iterator findDeselect = find( selected.begin() , selected.end() , obj );
00626
00627 if( findDeselect != selected.end() ) {
00628 (*findDeselect)->select();
00629 ui->rmHUDText(obj->toString());
00630 selected.erase(findDeselect);
00631 }
00632
00633 return (selected.size());
00634 }
00635
00636 void StateControl::clearSelection(){
00637
00638
00639
00640
00641 for ( vector<TraplasObject*>::iterator rit = selected.begin() ; rit != selected.end() ; rit++ ) {
00642
00643 (*rit)->select();
00644
00645 ui->rmHUDText((*rit)->toString());
00646 }
00647 selected.clear();
00648 }
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675 void StateControl::updateHudText(TraplasObject* upd){
00676 ui->rmHUDText(upd->toString());
00677 ui->setHUDText(upd->toString());
00678 }