sofaVerification.cpp
Go to the documentation of this file.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 #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
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 }