ARGoS  3
A parallel, multi-engine simulator for swarm robotics
plugins/simulator/visualizations/qt-opengl/qtopengl_render.cpp
Go to the documentation of this file.
00001 
00007 #include "qtopengl_render.h"
00008 #include "qtopengl_application.h"
00009 
00010 #include <argos3/core/utility/logging/argos_log.h>
00011 #include <argos3/core/utility/string_utilities.h>
00012 #include <cstring>
00013 
00014 #include <QPixmap>
00015 #include <QSplashScreen>
00016 
00017 namespace argos {
00018 
00019    /****************************************/
00020    /****************************************/
00021 
00022    void CQTOpenGLRender::Init(TConfigurationNode& t_tree) {
00023       /* Parse options from the XML */
00024 #ifdef ARGOS_WITH_LUA
00025       GetNodeAttributeOrDefault(t_tree, "lua_editor", m_bLuaEditor, m_bLuaEditor);
00026 #endif
00027       /* Save the configuration for later */
00028       m_tConfTree = t_tree;
00029       /* Set up dummy arguments for QApplication */
00030       m_nOptionNum = 1;
00031       m_ppcOptions = new char*[m_nOptionNum];
00032       m_ppcOptions[0] = new char[7];
00033       ::strcpy(m_ppcOptions[0], "argos3");
00034       /* Create the QT application */
00035       m_pcApplication = new CQTOpenGLApplication(m_nOptionNum, m_ppcOptions);
00036    }
00037 
00038    /****************************************/
00039    /****************************************/
00040 
00041    void CQTOpenGLRender::Execute() {
00042       try {
00043          /* Set some data about the application */
00044          m_pcApplication->setApplicationName("ARGoS");
00045          m_pcApplication->setApplicationVersion("3.0");
00046          m_pcApplication->setOrganizationName("Iridia-ULB");
00047          m_pcApplication->setOrganizationDomain("iridia.ulb.ac.be");
00048          /* Draw the main window */
00049          m_pcMainWindow = new CQTOpenGLMainWindow(m_tConfTree);
00050          m_pcMainWindow->show();
00051 #ifdef ARGOS_WITH_LUA
00052          /* Create Lua editor if required */
00053          if(m_bLuaEditor) {
00054             m_pcQTOpenGLLuaMainWindow = new CQTOpenGLLuaMainWindow(m_pcMainWindow);
00055             m_pcQTOpenGLLuaMainWindow->show();
00056          }
00057 #endif
00058          LOG.Flush();
00059          LOGERR.Flush();
00060          /* This effectively starts the experiment */
00061          m_pcApplication->exec();
00062       }
00063       catch(CARGoSException& ex) {
00064          THROW_ARGOSEXCEPTION_NESTED("Error while executing the experiment.", ex);
00065       }
00066    }
00067 
00068    /****************************************/
00069    /****************************************/
00070 
00071    void CQTOpenGLRender::Destroy() {
00072 #ifdef ARGOS_WITH_LUA
00073       /* Destroy the Lua editor */
00074       if(m_bLuaEditor) {
00075          delete m_pcQTOpenGLLuaMainWindow;
00076       }
00077 #endif
00078       /* Destroy the main window */
00079       delete m_pcMainWindow;
00080       /* Destroy the QT application */
00081       delete m_pcApplication;
00082       /* Get rid of the factory */
00083       CFactory<CQTOpenGLUserFunctions>::Destroy();
00084    }
00085 
00086    /****************************************/
00087    /****************************************/
00088 
00089    CQTOpenGLMainWindow& CQTOpenGLRender::GetMainWindow() {
00090       if(m_pcMainWindow == NULL) {
00091          THROW_ARGOSEXCEPTION("CQTOpenGLRender::GetMainWindow(): no main window created");
00092       }
00093       return *m_pcMainWindow;
00094    }
00095 
00096    /****************************************/
00097    /****************************************/
00098 
00099 #ifdef ARGOS_WITH_LUA
00100    CQTOpenGLLuaMainWindow& CQTOpenGLRender::GetLuaMainWindow() {
00101       if(m_pcQTOpenGLLuaMainWindow == NULL) {
00102          THROW_ARGOSEXCEPTION("CQTOpenGLRender::GetLuaMainWindow(): no Lua main window created");
00103       }
00104       return *m_pcQTOpenGLLuaMainWindow;
00105    }
00106 #endif
00107 
00108    /****************************************/
00109    /****************************************/
00110 
00111    REGISTER_VISUALIZATION(CQTOpenGLRender,
00112                           "qt-opengl",
00113                           "Carlo Pinciroli [ilpincy@gmail.com]",
00114                           "1.0",
00115                           "An interactive graphical renderer based on QT and OpenGL.",
00116                           "The QT-OpenGL renderer is a graphical renderer based on QT >= 4.5 and OpenGL.\n"
00117                           "It allows the user to watch and modify the simulation as it's running in an\n"
00118                           "intuitive way.\n\n"
00119                           "REQUIRED XML CONFIGURATION\n\n"
00120                           "  <visualization>\n"
00121                           "    <qtopengl_render />\n"
00122                           "  </visualization>\n\n"
00123                           "OPTIONAL XML CONFIGURATION\n\n"
00124                           "It is possible to set some camera parameters. There are 10 available\n"
00125                           "cameras to use. You can switch from one to the other by clicking on the\n"
00126                           "graphical view (to give it focus) and then pressing the keys 0-9.\n"
00127                           "To configure position and orientation of specific cameras, say cameras 0 to 3,\n"
00128                           "you have to include the following XML code:\n\n"
00129                           "  <visualization>\n"
00130                           "    <qtopengl_render>\n"
00131                           "      <camera>\n"
00132                           "        <placement idx=\"0\" position=\"2,2,2\" look_at=\"1,1,1\" />\n"
00133                           "        <placement idx=\"1\" position=\"1,0,7\" look_at=\"1,0,0\" />\n"
00134                           "        <placement idx=\"2\" position=\"3,3,4\" look_at=\"1,6,0\" />\n"
00135                           "        <placement idx=\"3\" position=\"2,3,2\" look_at=\"0,1,0\" />\n"
00136                           "      </camera>\n"
00137                           "    </qtopengl_render>\n"
00138                           "  </visualization>\n\n"
00139                           "The 'idx' attribute specifies the camera index (and the key to press to switch\n"
00140                           "to that camera).\n"
00141                           "The 'position' attribute contains the position of the camera in the arena.\n"
00142                           "The 'look_at' attribute sets the point the camera is looking at.\n"
00143                           "It also possible to set some optical parameters of real cameras, namely the\n"
00144                           "focal length and the length of the frame diagonal. For example:\n\n"
00145                           "  <visualization>\n"
00146                           "    <qtopengl_render>\n"
00147                           "      <camera>\n"
00148                           "        ...\n"
00149                           "        <placement idx=\"4\"\n"
00150                           "                   position=\"4,1,4\"\n"
00151                           "                   look_at=\"2,1,0\"\n"
00152                           "                   lens_focal_length=\"50\"\n"
00153                           "                   frame_diagonal=\"40\" />\n"
00154                           "        ...\n"
00155                           "      </camera>\n"
00156                           "    </qtopengl_render>\n"
00157                           "  </visualization>\n\n"
00158                           "The 'lens_focal_length' attribute controls the focal length of the lens of the\n"
00159                           "simulated camera. The value is in millimeters and it defaults, if not set in\n"
00160                           "XML, to 20mm.\n"
00161                           "The 'frame_diagonal' attribute specifies the length of the frame diagonal of\n"
00162                           "the image film. The value is in millimeters and it defaults, if not set in\n"
00163                           "XML, to 35mm.\n"
00164                           "This visualization also allows for user customization. In a similar fashion to\n"
00165                           "the loop functions, you can set a plug-in that derives from the\n"
00166                           "CQTOpenGLUserFunctions class. To load it in the system, follow this example:\n\n"
00167                           "  <visualization>\n"
00168                           "    <qtopengl_render>\n"
00169                           "      <user_functions library=\"/path/to/libmyuserfunctions.so\"\n"
00170                           "                      label=\"my_user_functions\" />\n"
00171                           "    </qtopengl_render>\n"
00172                           "  </visualization>\n\n"
00173                           "The 'library' attribute points to the library where the user functions are\n"
00174                           "stored. This library can be the same as the loop functions, or a new one.\n"
00175                           "There is no limitation to where the code is to be found.\n"
00176                           "The 'label' attribute identifies the user function class to use. In this way,\n"
00177                           "in a single library you can have multiple user function implementations, if\n"
00178                           "you wish.\n"
00179                           "You can also grab frames and store them into image files, for example to create\n"
00180                           "videos in a fast way. To do it, you just need to press the red capture button\n"
00181                           "and frame grabbing will be on. By default, the frames are named\n"
00182                           "'frame_NNNNN.png' and are stored in the current directory, i.e. the directory\n"
00183                           "where you run the 'argos' command. If you want to override this behavior, you\n"
00184                           "can add the optional 'frame_grabbing' section as follows:\n\n"
00185                           "  <visualization>\n"
00186                           "    <qtopengl_render>\n"
00187                           "      <frame_grabbing directory=\"frames\"\n"
00188                           "                      base_name=\"myframe_\"\n"
00189                           "                      format=\"png\"\n"
00190                           "                      quality=\"100\" />\n"
00191                           "    </qtopengl_render>\n"
00192                           "  </visualization>\n\n"
00193                           "All the attributes in this section are optional. If you don't specify one of\n"
00194                           "them, the default is taken.\n"
00195                           "The 'directory' attribute stores the directory where the frames are saved. If\n"
00196                           "the directory does not exist, a fatal error occurs. The directory must exist\n"
00197                           "and be writable. Both absolute and relative paths are allowed. The default\n"
00198                           "value is '.'\n"
00199                           "The 'base_name' attribute is the string to prepend to the file name. After this\n"
00200                           "string, the frame number (padded to 5 digits) is added. The default value is\n"
00201                           "'frame_', so a typical resulting name is 'frame_00165'.\n"
00202                           "The 'format' attribute specifies the format. The default value is 'png' but you\n"
00203                           "can put any format supported by Qt>=4.5. Refer to the Qt documentation for the\n"
00204                           "complete list of supported formats.\n"
00205                           "The 'quality' attribute dictates the quality of the image. Its value is in the\n"
00206                           "range [0:100] where 0 means maximum compression and minimum quality, and 100\n"
00207                           "means maximum quality and no compression at all. The default value is '-1',\n"
00208                           "which means to use Qt's default quality. For videos, it's best to use 100 to\n"
00209                           "avoid artifacts due to compression. For a normal screenshot, the default is the\n"
00210                           "safest choice.\n",
00211                           "Usable"
00212       );
00213 
00214 }