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