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 <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 //  .option(&fileName,'f',"file","scene file")
00097     .option(&startAnim,'s',"start","start the animation loop")
00098     .option(&printFactory,'p',"factory","print factory logs")
00099     //.option(&nogui,'g',"nogui","use no gui, run a number of iterations and exit")
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         //return 1; 
00135     }
00136 
00137     if (startAnim)
00138         groot->setAnimate(true);
00139 
00140     //=======================================
00141     // Add the haptic device
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     //phantom->sphereModel->init();
00161 
00162 
00163     //=======================================
00164     // Run the main loop
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 }

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