sofaVerification.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This program is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU General Public License as published by the Free  *
00007 * Software Foundation; either version 2 of the License, or (at your option)   *
00008 * any later version.                                                          *
00009 *                                                                             *
00010 * This program is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for    *
00013 * more details.                                                               *
00014 *                                                                             *
00015 * You should have received a copy of the GNU General Public License along     *
00016 * with this program; if not, write to the Free Software Foundation, Inc., 51  *
00017 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.                   *
00018 *******************************************************************************
00019 *                            SOFA :: Applications                             *
00020 *                                                                             *
00021 * Authors: M. Adam, J. Allard, B. Andre, P-J. Bensoussan, S. Cotin, C. Duriez,*
00022 * H. Delingette, F. Falipou, F. Faure, S. Fonteneau, L. Heigeas, C. Mendoza,  *
00023 * M. Nesme, P. Neumann, J-P. de la Plata Alcade, F. Poyer and F. Roy          *
00024 *                                                                             *
00025 * Contact information: contact@sofa-framework.org                             *
00026 ******************************************************************************/
00027 #include <iostream>
00028 #include <fstream>
00029 #include <sofa/helper/ArgumentParser.h>
00030 #include <sofa/simulation/tree/Simulation.h>
00031 #include <sofa/helper/Factory.h>
00032 #include <sofa/helper/BackTrace.h>
00033 #include <sofa/helper/system/FileRepository.h>
00034 #include <sofa/helper/system/SetDirectory.h>
00035 #include <sofa/simulation/common/WriteStateVisitor.h>
00036 #include <sofa/component/misc/ReadState.h>
00037 #include <sofa/component/misc/WriteState.h>
00038 #include <sofa/component/misc/CompareState.h>
00039 
00040 #include <ctime>
00041 
00042 // ---------------------------------------------------------------------
00043 // ---
00044 // ---------------------------------------------------------------------
00045 
00046 void apply(std::vector< std::string> &files, unsigned int iterations, bool reinit)
00047 {
00048   using namespace sofa::helper::system;
00049     sofa::simulation::tree::GNode* groot = NULL; 
00050     for (unsigned int i=0;i<files.size();++i)
00051       {
00052         groot = dynamic_cast< sofa::simulation::tree::GNode* >( sofa::simulation::tree::getSimulation()->load(files[i].c_str()));
00053         if (groot == NULL)
00054           {
00055         std::cerr << "CANNOT open " << files[i] << " !\n";
00056         continue;
00057           }
00058 
00059 
00060 
00061         std::string file = SetDirectory::GetParentDir(sofa::helper::system::DataRepository.getFirstPath().c_str());
00062         file = file + std::string("/applications/projects/sofaVerification/simulation/") + SetDirectory::GetFileName(files[i].c_str());
00063 
00064         if (reinit)
00065          {
00066            sofa::component::misc::WriteStateCreator compareVisitor;
00067            compareVisitor.setCreateInMapping(true);
00068            compareVisitor.setSceneName(file);
00069            compareVisitor.execute(groot);
00070 
00071            sofa::component::misc::WriteStateActivator v_write(true);
00072            v_write.execute(groot);  
00073          }
00074            else
00075          {
00076            sofa::component::misc::CompareStateCreator compareVisitor;
00077            compareVisitor.setCreateInMapping(true);
00078            compareVisitor.setSceneName(file);
00079            compareVisitor.execute(groot);
00080 
00081            sofa::component::misc::ReadStateActivator v_read(true);
00082            v_read.execute(groot);     
00083          }
00084            clock_t curtime = clock();
00085          std::cout << "Computing " <<  iterations << " for " << files[i] <<  std::endl;
00086         for (unsigned int i=0;i<iterations;i++)
00087           {
00088         sofa::simulation::tree::getSimulation()->animate(groot);
00089           }
00090            double t = (clock() - curtime)/((double)CLOCKS_PER_SEC);
00091          
00092          std::cout << iterations << " iterations done in " << t  << " seconds" <<std::endl;
00093 
00094          if (!reinit)
00095            {
00096          sofa::component::misc::CompareStateResult result;
00097          result.execute(groot);
00098          std::cout << "ERROR : " << result.getError() << "\n";
00099          
00100            }
00101          sofa::simulation::tree::getSimulation()->unload(groot);
00102       }
00103 }
00104 
00105 int main(int argc, char** argv)
00106 {
00107     sofa::helper::BackTrace::autodump();
00108  
00109     std::string fileName ;
00110     std::vector<std::string> files; 
00111     unsigned int iterations=100;
00112     bool reinit=false;
00113 
00114 
00115 
00116 
00117     sofa::helper::parse(&files, "This is a SOFA verification. Here are the command line arguments")
00118     .option(&reinit,'r',"reinit","Recreate the references state files")
00119     .option(&iterations, 'i',"iteration", "Number of iterations for testing")
00120     (argc,argv);
00121 
00122 
00123     if (!files.empty()) fileName = files[0];
00124 
00125         sofa::helper::system::DataRepository.findFile(fileName);
00126 
00127 
00128     //Get the list of scenes to test
00129     files.clear();
00130     std::ifstream end(fileName.c_str());
00131     std::string s;
00132     while( end >> s ) {
00133       sofa::helper::system::DataRepository.findFile(s);
00134       files.push_back(s);
00135     }
00136     end.close();
00137 
00138 
00139     if (reinit) apply(files, iterations, true);
00140     else
00141       apply(files, iterations, false);
00142 
00143 
00144     return 0;
00145 }

Generated on Sun Nov 23 02:28:35 2008 for Sofa by  doxygen 1.5.7.1