Viewer.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00010 #include <rtm/CorbaNaming.h>
00011 
00012 #include "hrpsys/util/Project.h"
00013 #include <hrpModel/ModelLoaderUtil.h>
00014 #include "hrpsys/util/GLbody.h"
00015 #include "hrpsys/util/GLlink.h"
00016 #include "hrpsys/util/GLutil.h"
00017 #include "GLscene.h"
00018 #include "hrpsys/idl/HRPDataTypes.hh"
00019 #include "RTCGLbody.h"
00020 #include "Viewer.h"
00021 
00022 // Module specification
00023 // <rtc-template block="module_spec">
00024 static const char* component_spec[] =
00025 {
00026     "implementation_id", "Viewer",
00027     "type_name",         "Viewer",
00028     "description",       "viewer component",
00029     "version",           HRPSYS_PACKAGE_VERSION,
00030     "vendor",            "AIST",
00031     "category",          "example",
00032     "activity_type",     "DataFlowComponent",
00033     "max_instance",      "10",
00034     "language",          "C++",
00035     "lang_type",         "compile",
00036     // Configuration variables
00037     "conf.default.project", "",
00038     ""
00039 };
00040 // </rtc-template>
00041 
00042 Viewer::Viewer(RTC::Manager* manager)
00043     : RTC::DataFlowComponentBase(manager),
00044       // <rtc-template block="initializer">
00045       m_sceneStateIn("state", m_sceneState),
00046       // </rtc-template>
00047       m_scene(&m_log),
00048       m_window(&m_scene, &m_log),
00049       dummy(0)
00050 {
00051     m_log.enableRingBuffer(1);
00052 }
00053 
00054 Viewer::~Viewer()
00055 {
00056 }
00057 
00058 
00059 
00060 RTC::ReturnCode_t Viewer::onInitialize()
00061 {
00062     std::cout << m_profile.instance_name << ": onInitialize()" << std::endl;
00063     // <rtc-template block="bind_config">
00064     // Bind variables and configuration variable
00065     bindParameter("project", m_project, "");  
00066   
00067     // </rtc-template>
00068 
00069     // Registration: InPort/OutPort/Service
00070     // <rtc-template block="registration">
00071     // Set InPort buffers
00072     addInPort("state", m_sceneStateIn);
00073 
00074     // Set OutPort buffer
00075   
00076     // Set service provider to Ports
00077   
00078     // Set service consumers to Ports
00079   
00080     // Set CORBA Service Ports
00081   
00082     // </rtc-template>
00083 
00084     //RTC::Properties& prop = getProperties();
00085 
00086     return RTC::RTC_OK;
00087 }
00088 
00089 
00090 
00091 /*
00092   RTC::ReturnCode_t Viewer::onFinalize()
00093   {
00094   return RTC::RTC_OK;
00095   }
00096 */
00097 
00098 /*
00099   RTC::ReturnCode_t Viewer::onStartup(RTC::UniqueId ec_id)
00100   {
00101   return RTC::RTC_OK;
00102   }
00103 */
00104 
00105 /*
00106   RTC::ReturnCode_t Viewer::onShutdown(RTC::UniqueId ec_id)
00107   {
00108   return RTC::RTC_OK;
00109   }
00110 */
00111 
00112 RTC::ReturnCode_t Viewer::onActivated(RTC::UniqueId ec_id)
00113 {
00114     std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
00115 
00116     RTC::Manager& rtcManager = RTC::Manager::instance();
00117     std::string nameServer = rtcManager.getConfig()["corba.nameservers"];
00118     int comPos = nameServer.find(",");
00119     if (comPos < 0){
00120         comPos = nameServer.length();
00121     }
00122     nameServer = nameServer.substr(0, comPos);
00123     RTC::CorbaNaming naming(rtcManager.getORB(), nameServer.c_str());
00124 
00125     if (m_project == ""){
00126         std::cerr << "Project file is not specified." << std::endl;
00127         return RTC::RTC_ERROR;
00128     }
00129 
00130     Project prj;
00131     if (!prj.parse(m_project)) return RTC::RTC_ERROR;
00132 
00133     m_window.init();
00134 
00135     for (std::map<std::string, ModelItem>::iterator it=prj.models().begin();
00136          it != prj.models().end(); it++){
00137         OpenHRP::BodyInfo_var binfo;
00138         binfo = hrp::loadBodyInfo(it->second.url.c_str(),
00139                                   CosNaming::NamingContext::_duplicate(naming.getRootContext()));
00140         if (CORBA::is_nil(binfo)){
00141             std::cerr << "failed to load model[" << it->second.url.c_str() << "]" 
00142                       << std::endl;
00143         }else{
00144             GLbody *glbody = new GLbody();
00145             hrp::BodyPtr body(glbody);
00146             hrp::loadBodyFromBodyInfo(body, binfo, false, GLlinkFactory);
00147             loadShapeFromBodyInfo(glbody, binfo);
00148             body->setName(it->first);
00149             m_scene.WorldBase::addBody(body);
00150             m_bodies[it->first] = new RTCGLbody(glbody, this);
00151         }
00152     }
00153 
00154     return RTC::RTC_OK;
00155 }
00156 
00157 RTC::ReturnCode_t Viewer::onDeactivated(RTC::UniqueId ec_id)
00158 {
00159     std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
00160 
00161     for (std::map<std::string, RTCGLbody *>::iterator it = m_bodies.begin();
00162          it != m_bodies.end(); it++){
00163         delete it->second;
00164     }
00165     m_bodies.clear();
00166 
00167     return RTC::RTC_OK;
00168 }
00169 
00170 RTC::ReturnCode_t Viewer::onExecute(RTC::UniqueId ec_id)
00171 {
00172     //std::cout << m_profile.instance_name<< ": onExecute(" << ec_id << ")" << std::endl;
00173 
00174     if (m_sceneStateIn.isNew()){
00175         do{
00176             m_sceneStateIn.read();
00177         }while(m_sceneStateIn.isNew());
00178         m_log.add(m_sceneState);
00179     }
00180 
00181     for (std::map<std::string, RTCGLbody *>::iterator it=m_bodies.begin();
00182          it != m_bodies.end(); it++){
00183         it->second->input();
00184     }
00185 
00186     m_window.processEvents();
00187     m_window.draw();
00188     m_window.swapBuffers();
00189 
00190     return RTC::RTC_OK;
00191 }
00192 
00193 /*
00194   RTC::ReturnCode_t Viewer::onAborting(RTC::UniqueId ec_id)
00195   {
00196   return RTC::RTC_OK;
00197   }
00198 */
00199 
00200 /*
00201   RTC::ReturnCode_t Viewer::onError(RTC::UniqueId ec_id)
00202   {
00203   return RTC::RTC_OK;
00204   }
00205 */
00206 
00207 /*
00208   RTC::ReturnCode_t Viewer::onReset(RTC::UniqueId ec_id)
00209   {
00210   return RTC::RTC_OK;
00211   }
00212 */
00213 
00214 /*
00215   RTC::ReturnCode_t Viewer::onStateUpdate(RTC::UniqueId ec_id)
00216   {
00217   return RTC::RTC_OK;
00218   }
00219 */
00220 
00221 /*
00222   RTC::ReturnCode_t Viewer::onRateChanged(RTC::UniqueId ec_id)
00223   {
00224   return RTC::RTC_OK;
00225   }
00226 */
00227 
00228 
00229 
00230 extern "C"
00231 {
00232 
00233     void ViewerInit(RTC::Manager* manager)
00234     {
00235         RTC::Properties profile(component_spec);
00236         manager->registerFactory(profile,
00237                                  RTC::Create<Viewer>,
00238                                  RTC::Delete<Viewer>);
00239     }
00240 
00241 };
00242 
00243 


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Wed May 15 2019 05:02:19