Main.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/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 //  .option(&fileName,'f',"file","scene file")
00085     .option(&startAnim,'s',"start","start the animation loop")
00086     .option(&printFactory,'p',"factory","print factory logs")
00087     //.option(&nogui,'g',"nogui","use no gui, run a number of iterations and exit")
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         //return 1; 
00125     }
00126 
00127     if (startAnim)
00128         groot->setAnimate(true);
00129 
00130     
00131 
00132     //=======================================
00133     // Run the main loop
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 }

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