CollisionDetectorViewer.cpp
Go to the documentation of this file.
1 #include <fstream>
2 #include <iostream>
3 #include <boost/bind.hpp>
4 #include <hrpModel/ModelLoaderUtil.h>
5 #ifdef __APPLE__
6 #include <GLUT/glut.h>
7 #else
8 #include <GL/glut.h>
9 #endif
10 #include "hrpsys/util/ProjectUtil.h"
11 #include "hrpsys/util/GLbody.h"
12 #include "hrpsys/util/GLlink.h"
13 #include "hrpsys/util/GLutil.h"
14 #include "hrpsys/util/SDLUtil.h"
15 #include "hrpsys/util/LogManager.h"
16 #include "hrpsys/util/BVutil.h"
17 #include "TimedPosture.h"
18 #include "GLscene.h"
19 #include "hrpsys/idl/CollisionDetectorService.hh"
20 
21 using namespace hrp;
22 using namespace OpenHRP;
23 
24 hrp::BodyPtr createBody(const std::string& name, const ModelItem& mitem,
25  ModelLoader_ptr modelloader)
26 {
27  BodyInfo_var binfo;
28  try{
29  OpenHRP::ModelLoader::ModelLoadOption opt;
30  opt.readImage = true;
31  opt.AABBdata.length(0);
32  opt.AABBtype = OpenHRP::ModelLoader::AABB_NUM;
33  binfo = modelloader->getBodyInfoEx(mitem.url.c_str(), opt);
34  }catch(OpenHRP::ModelLoader::ModelLoaderException ex){
35  std::cerr << ex.description << std::endl;
36  return hrp::BodyPtr();
37  }
38  GLbody *glbody = new GLbody();
39  hrp::BodyPtr body(glbody);
40  hrp::loadBodyFromBodyInfo(body, binfo, true, GLlinkFactory);
41  loadShapeFromBodyInfo(glbody, binfo);
42 
43  convertToConvexHull(body);
44 
45  body->setName(name);
46  return body;
47 }
48 
49 int main(int argc, char* argv[])
50 {
51  if (argc < 2){
52  std::cerr << "Usage:" << argv[0] << " project.xml [-co CollisionDetector Component] [-size size] [-bg r g b]" << std::endl;
53  return 1;
54  }
55 
56  Project prj;
57  if (!prj.parse(argv[1])){
58  std::cerr << "failed to parse " << argv[1] << std::endl;
59  return 1;
60  }
61 
62  const char *coname = "co";
63  int wsize = 0;
64  float bgColor[] = {0,0,0};
65  for (int i = 2; i<argc; i++){
66  if (strcmp(argv[i], "-co")==0){
67  coname = argv[++i];
68  }else if(strcmp(argv[i], "-size")==0){
69  wsize = atoi(argv[++i]);
70  }else if(strcmp(argv[i], "-bg")==0){
71  bgColor[0] = atof(argv[++i]);
72  bgColor[1] = atof(argv[++i]);
73  bgColor[2] = atof(argv[++i]);
74  }
75  }
76 
77  //================= CORBA =========================
78  CORBA::ORB_var orb;
79  CosNaming::NamingContext_var namingContext;
80 
81  try {
82  orb = CORBA::ORB_init(argc, argv);
83 
84  CORBA::Object_var obj;
85  obj = orb->resolve_initial_references("RootPOA");
86  PortableServer::POA_var poa = PortableServer::POA::_narrow(obj);
87  if(CORBA::is_nil(poa)){
88  throw std::string("error: failed to narrow root POA.");
89  }
90 
91  PortableServer::POAManager_var poaManager = poa->the_POAManager();
92  if(CORBA::is_nil(poaManager)){
93  throw std::string("error: failed to narrow root POA manager.");
94  }
95 
96  obj = orb->resolve_initial_references("NameService");
97  namingContext = CosNaming::NamingContext::_narrow(obj);
98  if(CORBA::is_nil(namingContext)){
99  throw std::string("error: failed to narrow naming context.");
100  }
101 
102  poaManager->activate();
103  }catch (CORBA::SystemException& ex) {
104  std::cerr << ex._rep_id() << std::endl;
105  }catch (const std::string& error){
106  std::cerr << error << std::endl;
107  }
108 
109  //==================== viewer ===============
112  scene.setBackGroundColor(bgColor);
113 
114  SDLwindow window(&scene, &log);
115  window.init(wsize, wsize);
116 
117  std::vector<hrp::ColdetLinkPairPtr> pairs;
118  ModelLoader_var modelloader = getModelLoader(namingContext);
119  if (CORBA::is_nil(modelloader)){
120  std::cerr << "openhrp-model-loader is not running" << std::endl;
121  return 1;
122  }
123  BodyFactory factory = boost::bind(createBody, _1, _2, modelloader);
124  //hrp::BodyPtr m_robot = createBody("pa10", , modelloader);
125  //scene.addBody(m_robot);
126  initWorld(prj, factory, scene, pairs);
128  scene.showFloorGrid(false);
129 
130  log.enableRingBuffer(1);
131  OpenHRP::CollisionDetectorService_var coService;
132 
133  while(window.oneStep()) {
134  //==================== collision detecter ===============
135 
136  if (CORBA::is_nil(coService)){
137  try{
138  CosNaming::Name name;
139  name.length(1);
140  name[0].id = CORBA::string_dup(coname);
141  name[0].kind = CORBA::string_dup("rtc");
142  CORBA::Object_var obj = namingContext->resolve(name);
143  RTC::RTObject_var rtc = RTC::RTObject::_narrow(obj);
144  const char *ior = getServiceIOR(rtc, "CollisionDetectorService");
145  coService = OpenHRP::CollisionDetectorService::_narrow(orb->string_to_object(ior));
146  }catch(...){
147  std::cerr << "could not found collision detector component " << coname << std::endl;
148  return 1;
149  }
150  }
151  // get CollisionState
152  OpenHRP::CollisionDetectorService::CollisionState co;
153  bool stateUpdate = false;
154  if (!CORBA::is_nil(coService)){
155  try{
156  OpenHRP::CollisionDetectorService::CollisionState_var t_co;
157  coService->getCollisionStatus(t_co);
158  co = t_co;
159  stateUpdate = true;
160  }catch(...){
161  std::cerr << "exception in getCollisionStatus()" << std::endl;
162  coService = NULL;
163  }
164  }
165 
166  if (stateUpdate) {
167  try {
168  GLbody* glbody = dynamic_cast<GLbody *>(scene.body(0).get());
169  for (size_t i=0; i<co.collide.length(); i++) {
170  ((GLlink *)glbody->link(i))->highlight(co.collide[i]);
171  }
172 #if 0
173  TimedPosture tp;
174  tp.time = co.time;
175  tp.posture.resize(co.angle.length());
176  for (size_t i=0; i<tp.posture.size(); i++) {
177  tp.posture[i] = co.angle[i];
178  }
179  for (size_t i=0; i<co.lines.length(); i++) {
180  hrp::Vector3 p0, p1;
181  p0[0] = co.lines[i][0][0];
182  p0[1] = co.lines[i][0][1];
183  p0[2] = co.lines[i][0][2];
184  p1[0] = co.lines[i][1][0];
185  p1[1] = co.lines[i][1][1];
186  p1[2] = co.lines[i][1][2];
187  tp.lines.push_back(std::make_pair(p0,p1));
188  }
189 #endif
190  log.add(co);
191  } catch (...) {
192  std::cerr << "exceptoin in stateUpdate" << std::endl;
193  }
194  }
195 
196  // update Scene
197  }
198 
199  try {
200  orb->destroy();
201  }
202  catch(...){
203 
204  }
205 
206  return 0;
207 }
void enableRingBuffer(int len)
Definition: LogManager.h:157
ior
void showFloorGrid(bool flag)
HRPMODEL_API OpenHRP::ModelLoader_var getModelLoader(CosNaming::NamingContext_var cxt)
bool parse(const std::string &filename)
Definition: Project.cpp:32
hrp::BodyPtr createBody(const std::string &name, const ModelItem &mitem, ModelLoader_ptr modelloader)
void setBackGroundColor(float rgb[3])
png_infop png_charpp name
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)
void error(char *msg) const
void loadShapeFromBodyInfo(GLbody *body, BodyInfo_var i_binfo, GLshape *(*shapeFactory)())
Definition: GLutil.cpp:181
png_uint_32 i
const char * getServiceIOR(RTC::RTObject_var rtc, const char *sname)
Definition: OpenRTMUtil.cpp:65
boost::shared_ptr< Body > BodyPtr
Definition: GLbody.h:11
Eigen::Vector3d Vector3
bool oneStep()
Definition: SDLUtil.cpp:342
BodyPtr body(int index)
std::string url
Definition: Project.h:62
double time
Definition: TimedPosture.h:6
void convertToConvexHull(hrp::BodyPtr i_body)
Definition: BVutil.cpp:79
void add(const T &state)
Definition: LogManager.h:17
Link * link(int index) const
int main(int argc, char *argv[])
std::vector< std::pair< hrp::Vector3, hrp::Vector3 > > lines
Definition: TimedPosture.h:8
obj
boost::function2< hrp::BodyPtr, const std::string &, const ModelItem & > BodyFactory
Definition: ProjectUtil.h:9
void initWorld(Project &prj, BodyFactory &factory, hrp::World< hrp::ConstraintForceSolver > &world, std::vector< hrp::ColdetLinkPairPtr > &pairs)
Definition: ProjectUtil.cpp:9
std::vector< double > posture
Definition: TimedPosture.h:7


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