Main.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/component/init.h>
00032 #include <sofa/helper/Factory.h>
00033 #include <sofa/helper/BackTrace.h>
00034 #include <sofa/helper/system/FileRepository.h>
00035 #include <sofa/gui/SofaGUI.h>
00036 #include <sofa/helper/system/gl.h>
00037 #include <sofa/helper/system/glut.h>
00038
00039 #ifndef WIN32
00040 #include <dlfcn.h>
00041 bool loadPlugin(const char* filename)
00042 {
00043 void *handle;
00044 handle=dlopen(filename, RTLD_LAZY);
00045 if (!handle)
00046 {
00047 std::cerr<<"Error loading plugin "<<filename<<": "<<dlerror()<<std::endl;
00048 return false;
00049 }
00050 std::cerr<<"Plugin "<<filename<<" loaded."<<std::endl;
00051 return true;
00052 }
00053 #else
00054 bool loadPlugin(const char* filename)
00055 {
00056 std::cerr << "Plugin loading not supported on this platform.\n";
00057 return false;
00058 }
00059 #endif
00060
00061
00062
00063
00064 int main(int argc, char** argv)
00065 {
00066 sofa::helper::BackTrace::autodump();
00067
00068
00069 glutInit(&argc,argv);
00070
00071 sofa::gui::SofaGUI::SetProgramName(argv[0]);
00072 std::string fileName ;
00073 bool startAnim = false;
00074 bool printFactory = false;
00075 std::string gui = sofa::gui::SofaGUI::GetGUIName();
00076 std::vector<std::string> plugins;
00077 std::vector<std::string> files;
00078
00079 std::string gui_help = "choose the UI (";
00080 gui_help += sofa::gui::SofaGUI::ListSupportedGUI('|');
00081 gui_help += ")";
00082
00083 sofa::helper::parse(&files, "This is a SOFA application. Here are the command line arguments")
00084
00085 .option(&startAnim,'s',"start","start the animation loop")
00086 .option(&printFactory,'p',"factory","print factory logs")
00087
00088 .option(&gui,'g',"gui",gui_help.c_str())
00089 .option(&plugins,'l',"load","load given plugins")
00090 (argc,argv);
00091
00092 sofa::component::init();
00093
00094 if (!files.empty()) fileName = files[0];
00095
00096 for (unsigned int i=0;i<plugins.size();i++)
00097 loadPlugin(plugins[i].c_str());
00098
00099 if (printFactory)
00100 {
00101 std::cout << "////////// FACTORY //////////" << std::endl;
00102 sofa::helper::printFactoryLog();
00103 std::cout << "//////// END FACTORY ////////" << std::endl;
00104 }
00105
00106 if (int err=sofa::gui::SofaGUI::Init(argv[0],gui.c_str()))
00107 return err;
00108
00109 sofa::simulation::tree::GNode* groot = NULL;
00110
00111 if (fileName.empty())
00112 {
00113 fileName = "Demos/liver.scn";
00114 sofa::helper::system::DataRepository.findFile(fileName);
00115 }
00116
00117 std::string in_filename(fileName);
00118 if (in_filename.rfind(".simu") == std::string::npos)
00119 groot = dynamic_cast<sofa::simulation::tree::GNode*>( sofa::simulation::tree::getSimulation()->load(fileName.c_str()));
00120
00121 if (groot==NULL)
00122 {
00123 groot = new sofa::simulation::tree::GNode;
00124
00125 }
00126
00127 if (startAnim)
00128 groot->setAnimate(true);
00129
00130
00131
00132
00133
00134
00135 if (gui=="none")
00136 {
00137 if (groot==NULL)
00138 {
00139 std::cerr<<"Could not load file "<<fileName<<std::endl;
00140 return 1;
00141 }
00142 std::cout << "Computing 1000 iterations." << std::endl;
00143 for (int i=0;i<1000;i++)
00144 {
00145 sofa::simulation::tree::getSimulation()->animate(groot);
00146 }
00147 std::cout << "1000 iterations done." << std::endl;
00148 }
00149 else
00150 {
00151 if (int err=sofa::gui::SofaGUI::MainLoop(groot,fileName.c_str()))
00152 return err;
00153 groot = dynamic_cast<sofa::simulation::tree::GNode*>( sofa::gui::SofaGUI::CurrentSimulation() );
00154 }
00155
00156 if (groot!=NULL)
00157 sofa::simulation::tree::getSimulation()->unload(groot);
00158 return 0;
00159 }