Biomechanical Joint Model
 Author: Anderson Maciel

comesimulator.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2003 by Anderson Maciel                                 *
00003  *   andi.maciel@gmail.com                                                 *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  **************************************************************************/
00020 
00031 
00032 #include        <general/comescenario.h>
00033 #include        <general/comesimulator.h>
00034 #include        <general/comexml.h>
00035 #include        <time.h>
00036 #include        <general/cometimemanager.h>
00037 #include        <physics/comecollide.h>
00038 //#include      <viscoelasticity/openglview.h>
00039 #include        <zthread/Mutex.h>
00040 
00041 
00042 
00046 
00047 COME_Simulator::COME_Simulator(){
00048 /* not used constructor. verify!!
00049         printf("Type XML file name: ");
00050         char sFname[64];
00051         scanf("%s", sFname );
00052 
00053         // Set mode for recalculations of springs
00054         howRecalculate = 0;
00055         whenRecalculate = 0;
00056 
00057         // Create a scene from file
00058         scene = new COME_Scenario();
00059         scene->loadFile( sFname, this );
00060 */
00061         duration = 10.0;
00062         setFPS(400);
00063 
00064         //simulation dialog
00065         bStop = false;
00066         bPause = false;
00067         running = false;
00068 
00069         //howRecalculate = NUM_CONNECTIONS;
00070         
00071         mutex = new ZThread::Mutex();
00072         whenRecalculate = NEVER;
00073         howRecalculate = NONE;
00074 }
00075 
00076 
00077 COME_Simulator::~COME_Simulator(){
00078 
00079         //delete scene;
00080         //delete view;
00081 }
00082 
00086 void
00087 COME_Simulator::setFPS( double fpsN ){
00088 
00089         FPS = fpsN;
00090         timestep = 1.0/FPS;
00091         COME::timestepGlobal = timestep;
00092 }
00093 
00094 void
00095 COME_Simulator::setTimestep( double timestepN ){
00096 
00097         timestep = timestepN;
00098         COME::timestepGlobal = timestep;
00099         FPS = 1.0/timestep;
00100 }
00101 
00102 void
00103 COME_Simulator::setClock( double clockN ){
00104 
00105         sim_clock = clockN;
00106 }
00107 
00108 void
00109 COME_Simulator::setDuration( double durationN ){
00110 
00111         duration = durationN;
00112 }
00113 
00114 void
00115 COME_Simulator::setScene( COME_Scenario *sceneN ){
00116 
00117         scene = sceneN;
00118 }
00119 
00120 void    
00121 COME_Simulator:: setStop( bool bStopN ){
00122 
00123         bStop = bStopN;
00124 }
00125 
00126 void    
00127 COME_Simulator:: setPause( bool bPauseN ){
00128 
00129         bPause = bPauseN;
00130 }
00131 
00132 void
00133 COME_Simulator::setType( int ty ){
00134 
00135         type = ty;
00136 
00137 }
00138 
00142 double
00143 COME_Simulator::getFPS() const {
00144 
00145         return FPS;
00146 }
00147 
00148 double
00149 COME_Simulator::getTimestep() const {
00150 
00151         return timestep;
00152 }
00153 
00154 double
00155 COME_Simulator::getClock() const {
00156 
00157         return sim_clock;
00158 }
00159 
00160 double
00161 COME_Simulator::getDuration() const {
00162 
00163         return duration;
00164 }
00165 
00166 bool 
00167 COME_Simulator::getPause() const{
00168 
00169         return bPause;
00170 }
00171 
00172 bool 
00173 COME_Simulator::getStop() const{
00174 
00175         return bStop;
00176 }
00177 
00178 int
00179 COME_Simulator::getType(){
00180 
00181         return type;
00182 }
00183 
00184 int
00185 COME_Simulator::getHowRecalculate(){
00186 
00187         return howRecalculate;
00188 }
00189 
00190 COME_Scenario*
00191 COME_Simulator::getScene() const{
00192 
00193         return scene;
00194 }
00195 
00196 bool
00197 COME_Simulator::isRunning() const{
00198 
00199         return running;
00200 }
00201 
00202 void
00203 COME_Simulator::run() {
00204 
00205         running = true;
00206         // Setup simulation parameters
00207         //timestep = 1.0/FPS;
00208         sim_clock = 0.0;
00209         difKinematics = 0.0;
00210         difDeformation = 0.0;
00211         difCollisions = 0.0;
00212         
00213         printf( "duration: %f\n", duration );
00214         printf( "TIME STEP: %f \n", timestep );
00215         for( sim_clock = 0-timestep; (duration < 0) || (sim_clock < duration);  ){
00216         
00217                 // Go one step in the deformation/motion engine
00218                 // If convergency is not reached, divide timestep and go again.
00219                 if( !bPause ){
00220                          scene->lock();
00221                          scene->simulate( timestep, sim_clock += timestep, FUTURE );
00222                          scene->unlock();
00223                 }
00224         }
00225         printf( "End of simulator::run thread.\n" );
00226         printf ("Total kinematics time was %.7f seconds. \n", difKinematics / 1000000.0 );
00227         printf ("Total deformation time was %.7f seconds. \n", difDeformation / 1000000.0 );
00228         printf ("Total collision time was %.7f seconds. \n", difCollisions / 1000000.0 );
00229         printf ("Signed distance called %d times. \n", scene->getCollisionDetector()->CONTADIST ); 
00230         running = false;
00231         bStop = true;
00232         
00233         // Save animation description file
00234         if( COME::flagExportToPrecalculatedFile ){
00235                 COME_Xml docS;
00236                 string strFName = "animation.xml";
00237                 docS.saveAnimationFile( strFName, *(scene->getPatientList()), this );
00238         }
00239 }

Generated on Thu Dec 1 10:13:36 2005 for COME - Biomechanical Joint Model by  doxygen 1.4.5