Viewer.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
10 #include <rtm/CorbaNaming.h>
11 
12 #include "hrpsys/util/Project.h"
13 #include <hrpModel/ModelLoaderUtil.h>
14 #include "hrpsys/util/GLbody.h"
15 #include "hrpsys/util/GLlink.h"
16 #include "hrpsys/util/GLutil.h"
17 #include "GLscene.h"
18 #include "hrpsys/idl/HRPDataTypes.hh"
19 #include "RTCGLbody.h"
20 #include "Viewer.h"
21 
22 // Module specification
23 // <rtc-template block="module_spec">
24 static const char* component_spec[] =
25 {
26  "implementation_id", "Viewer",
27  "type_name", "Viewer",
28  "description", "viewer component",
29  "version", HRPSYS_PACKAGE_VERSION,
30  "vendor", "AIST",
31  "category", "example",
32  "activity_type", "DataFlowComponent",
33  "max_instance", "10",
34  "language", "C++",
35  "lang_type", "compile",
36  // Configuration variables
37  "conf.default.project", "",
38  ""
39 };
40 // </rtc-template>
41 
43  : RTC::DataFlowComponentBase(manager),
44  // <rtc-template block="initializer">
45  m_sceneStateIn("state", m_sceneState),
46  // </rtc-template>
47  m_scene(&m_log),
48  m_window(&m_scene, &m_log),
49  dummy(0)
50 {
52 }
53 
55 {
56 }
57 
58 
59 
60 RTC::ReturnCode_t Viewer::onInitialize()
61 {
62  std::cout << m_profile.instance_name << ": onInitialize()" << std::endl;
63  // <rtc-template block="bind_config">
64  // Bind variables and configuration variable
65  bindParameter("project", m_project, "");
66 
67  // </rtc-template>
68 
69  // Registration: InPort/OutPort/Service
70  // <rtc-template block="registration">
71  // Set InPort buffers
72  addInPort("state", m_sceneStateIn);
73 
74  // Set OutPort buffer
75 
76  // Set service provider to Ports
77 
78  // Set service consumers to Ports
79 
80  // Set CORBA Service Ports
81 
82  // </rtc-template>
83 
84  //RTC::Properties& prop = getProperties();
85 
86  return RTC::RTC_OK;
87 }
88 
89 
90 
91 /*
92  RTC::ReturnCode_t Viewer::onFinalize()
93  {
94  return RTC::RTC_OK;
95  }
96 */
97 
98 /*
99  RTC::ReturnCode_t Viewer::onStartup(RTC::UniqueId ec_id)
100  {
101  return RTC::RTC_OK;
102  }
103 */
104 
105 /*
106  RTC::ReturnCode_t Viewer::onShutdown(RTC::UniqueId ec_id)
107  {
108  return RTC::RTC_OK;
109  }
110 */
111 
112 RTC::ReturnCode_t Viewer::onActivated(RTC::UniqueId ec_id)
113 {
114  std::cout << m_profile.instance_name<< ": onActivated(" << ec_id << ")" << std::endl;
115 
116  RTC::Manager& rtcManager = RTC::Manager::instance();
117  std::string nameServer = rtcManager.getConfig()["corba.nameservers"];
118  int comPos = nameServer.find(",");
119  if (comPos < 0){
120  comPos = nameServer.length();
121  }
122  nameServer = nameServer.substr(0, comPos);
123  RTC::CorbaNaming naming(rtcManager.getORB(), nameServer.c_str());
124 
125  if (m_project == ""){
126  std::cerr << "Project file is not specified." << std::endl;
127  return RTC::RTC_ERROR;
128  }
129 
130  Project prj;
131  if (!prj.parse(m_project)) return RTC::RTC_ERROR;
132 
133  m_window.init();
134 
135  for (std::map<std::string, ModelItem>::iterator it=prj.models().begin();
136  it != prj.models().end(); it++){
137  OpenHRP::BodyInfo_var binfo;
138  binfo = hrp::loadBodyInfo(it->second.url.c_str(),
139  CosNaming::NamingContext::_duplicate(naming.getRootContext()));
140  if (CORBA::is_nil(binfo)){
141  std::cerr << "failed to load model[" << it->second.url.c_str() << "]"
142  << std::endl;
143  }else{
144  GLbody *glbody = new GLbody();
145  hrp::BodyPtr body(glbody);
146  hrp::loadBodyFromBodyInfo(body, binfo, false, GLlinkFactory);
147  loadShapeFromBodyInfo(glbody, binfo);
148  body->setName(it->first);
149  m_scene.WorldBase::addBody(body);
150  m_bodies[it->first] = new RTCGLbody(glbody, this);
151  }
152  }
153 
154  return RTC::RTC_OK;
155 }
156 
157 RTC::ReturnCode_t Viewer::onDeactivated(RTC::UniqueId ec_id)
158 {
159  std::cout << m_profile.instance_name<< ": onDeactivated(" << ec_id << ")" << std::endl;
160 
161  for (std::map<std::string, RTCGLbody *>::iterator it = m_bodies.begin();
162  it != m_bodies.end(); it++){
163  delete it->second;
164  }
165  m_bodies.clear();
166 
167  return RTC::RTC_OK;
168 }
169 
170 RTC::ReturnCode_t Viewer::onExecute(RTC::UniqueId ec_id)
171 {
172  //std::cout << m_profile.instance_name<< ": onExecute(" << ec_id << ")" << std::endl;
173 
174  if (m_sceneStateIn.isNew()){
175  do{
177  }while(m_sceneStateIn.isNew());
179  }
180 
181  for (std::map<std::string, RTCGLbody *>::iterator it=m_bodies.begin();
182  it != m_bodies.end(); it++){
183  it->second->input();
184  }
185 
187  m_window.draw();
189 
190  return RTC::RTC_OK;
191 }
192 
193 /*
194  RTC::ReturnCode_t Viewer::onAborting(RTC::UniqueId ec_id)
195  {
196  return RTC::RTC_OK;
197  }
198 */
199 
200 /*
201  RTC::ReturnCode_t Viewer::onError(RTC::UniqueId ec_id)
202  {
203  return RTC::RTC_OK;
204  }
205 */
206 
207 /*
208  RTC::ReturnCode_t Viewer::onReset(RTC::UniqueId ec_id)
209  {
210  return RTC::RTC_OK;
211  }
212 */
213 
214 /*
215  RTC::ReturnCode_t Viewer::onStateUpdate(RTC::UniqueId ec_id)
216  {
217  return RTC::RTC_OK;
218  }
219 */
220 
221 /*
222  RTC::ReturnCode_t Viewer::onRateChanged(RTC::UniqueId ec_id)
223  {
224  return RTC::RTC_OK;
225  }
226 */
227 
228 
229 
230 extern "C"
231 {
232 
233  void ViewerInit(RTC::Manager* manager)
234  {
236  manager->registerFactory(profile,
237  RTC::Create<Viewer>,
238  RTC::Delete<Viewer>);
239  }
240 
241 };
242 
243 
ComponentProfile m_profile
png_infop png_charpp int png_charpp profile
void enableRingBuffer(int len)
Definition: LogManager.h:157
viewer component
void draw()
Definition: SDLUtil.cpp:308
SDLwindow m_window
Definition: Viewer.h:144
HRPMODEL_API OpenHRP::BodyInfo_var loadBodyInfo(const char *url, int &argc, char *argv[])
InPort< OpenHRP::SceneState > m_sceneStateIn
Definition: Viewer.h:114
bool parse(const std::string &filename)
Definition: Project.cpp:32
virtual ~Viewer()
Destructor.
Definition: Viewer.cpp:54
bool processEvents()
Definition: SDLUtil.cpp:97
bool init(int w=0, int h=0, bool resizable=true)
Definition: SDLUtil.cpp:54
HRPMODEL_API bool loadBodyFromBodyInfo(BodyPtr body, OpenHRP::BodyInfo_ptr bodyInfo, bool loadGeometryForCollisionDetection=false, Link *(*f)()=NULL)
CORBA::ORB_ptr getORB()
void loadShapeFromBodyInfo(GLbody *body, BodyInfo_var i_binfo, GLshape *(*shapeFactory)())
Definition: GLutil.cpp:181
GLscene m_scene
Definition: Viewer.h:140
virtual RTC::ReturnCode_t onExecute(RTC::UniqueId ec_id)
Definition: Viewer.cpp:170
static Manager & instance()
std::string m_project
Definition: Viewer.h:142
Definition: GLbody.h:11
void ViewerInit(RTC::Manager *manager)
Definition: Viewer.cpp:233
coil::Properties & getConfig()
ExecutionContextHandle_t UniqueId
bool bindParameter(const char *param_name, VarType &var, const char *def_val, bool(*trans)(VarType &, const char *)=coil::stringTo)
OpenHRP::SceneState m_sceneState
Definition: Viewer.h:113
std::map< std::string, RTCGLbody * > m_bodies
Definition: Viewer.h:141
virtual RTC::ReturnCode_t onActivated(RTC::UniqueId ec_id)
Definition: Viewer.cpp:112
void add(const T &state)
Definition: LogManager.h:17
std::map< std::string, ModelItem > & models()
Definition: Project.h:114
virtual RTC::ReturnCode_t onInitialize()
Definition: Viewer.cpp:60
Viewer(RTC::Manager *manager)
Constructor.
Definition: Viewer.cpp:42
naming
virtual RTC::ReturnCode_t onDeactivated(RTC::UniqueId ec_id)
Definition: Viewer.cpp:157
virtual bool isNew()
void swapBuffers()
Definition: SDLUtil.cpp:337
bool addInPort(const char *name, InPortBase &inport)
bool registerFactory(coil::Properties &profile, RtcNewFunc new_func, RtcDeleteFunc delete_func)
static const char * component_spec[]
Definition: Viewer.cpp:24
LogManager< OpenHRP::SceneState > m_log
Definition: Viewer.h:143


hrpsys
Author(s): AIST, Fumio Kanehiro
autogenerated on Thu May 6 2021 02:41:51