12 #include <hrpModel/ModelLoaderUtil.h> 19 #if (defined __APPLE__) 20 #include <pcl/surface/qhull.h> 22 #include <qhull/qhull_a.h> 26 #define deg2rad(x) ((x)*M_PI/180) 27 #define rad2deg(x) ((x)*180/M_PI) 29 std::vector<hrp::ColdetLinkPairPtr>
m_pair;
35 OpenHRP::ShapeInfoSequence_var sis = binfo->shapes();
36 OpenHRP::LinkInfoSequence_var lis = binfo->links();
37 for(
unsigned int i = 0;
i < m_robot->numLinks();
i++ ) {
38 const OpenHRP::LinkInfo& i_li = lis[
i];
39 const OpenHRP::TransformedShapeIndexSequence& tsis = i_li.shapeIndices;
42 for (
unsigned int l=0;
l<tsis.length();
l++) {
43 OpenHRP::ShapeInfo& si = sis[tsis[
l].shapeIndex];
44 const OpenHRP::LongSequence& triangles = si.triangles;
45 numTriangles += triangles.length();
47 double points[numTriangles*3];
52 Rs44 << Rs33(0,0),Rs33(0,1), Rs33(0,2), 0,
53 Rs33(1,0),Rs33(1,1), Rs33(1,2), 0,
54 Rs33(2,0),Rs33(2,1), Rs33(2,2), 0,
56 for (
unsigned int l=0;
l<tsis.length();
l++) {
57 const OpenHRP::DblArray12& M = tsis[
l].transformMatrix;
59 T0 << M[0], M[1], M[2], M[3],
60 M[4], M[5], M[6], M[7],
61 M[8], M[9], M[10], M[11],
64 const OpenHRP::ShapeInfo& si = sis[tsis[
l].shapeIndex];
65 const OpenHRP::LongSequence& triangles = si.triangles;
66 const float *vertices = si.vertices.get_buffer();
68 for(
unsigned int j=0;
j < triangles.length() / 3; ++
j){
69 for(
int k=0; k < 3; ++k){
70 long orgVertexIndex = si.triangles[
j * 3 + k];
71 int p = orgVertexIndex * 3;
74 points[points_i++] = v[0];
75 points[points_i++] = v[1];
76 points[points_i++] = v[2];
82 coldetModel->setName(std::string(i_li.name));
85 int triangleIndex = 0;
88 boolT ismalloc = False;
90 if (! qh_new_qhull (3,numTriangles,points,ismalloc,flags,NULL,stderr) ) {
95 vertexT *vertex,**vertexp;
96 coldetModel->setNumVertices(qh num_vertices);
97 coldetModel->setNumTriangles(qh num_facets);
98 int index[qh num_vertices];
100 int p = qh_pointid(vertex->point);
101 index[p] = vertexIndex;
102 coldetModel->setVertex(vertexIndex++, points[p*3+0], points[p*3+1], points[p*3+2]);
105 num = qh num_facets;;
109 setT *vertices = qh_facet3vertex (facet);
110 FOREACHvertexreverse12_ (vertices) {
112 p[j] = index[qh_pointid(vertex->point)];
114 fprintf(stderr,
"extra vertex %d\n",j);
118 coldetModel->setTriangle(triangleIndex++, p[0], p[1], p[2]);
123 coldetModel->build();
124 m_robot->link(
i)->coldetModel = coldetModel;
126 qh_freeqhull(!qh_ALL);
127 int curlong, totlong;
128 qh_memfreeshort (&curlong, &totlong);
129 if (curlong || totlong) {
130 fprintf(stderr,
"convhulln: did not free %d bytes of long memory (%d pieces)\n", totlong, curlong);
133 std::cerr << std::setw(16) << i_li.name <<
" reduce triangles from " << std::setw(5) << numTriangles <<
" to " << std::setw(4) << num << std::endl;
141 if ( i >= (
int)jointPath->numJoints() )
return false;
142 if ( collisionPairs.size() == 0 )
return true;
149 for(
float angle = l->
llimit; angle <= l->ulimit; angle += step)
152 m_robot->calcForwardKinematics(
false,
false);
153 m_robot->updateLinkColdetModelPositions();
158 for(std::vector<hrp::ColdetLinkPairPtr>::iterator it = collisionPairs.begin(); it != collisionPairs.end(); ) {
159 if ( (*it)->detectIntersection() ) {
160 std::cerr <<
"Find collision for " << (*it)->link(0)->name <<
" " << (*it)->link(1)->name << std::endl;
161 it = collisionPairs.erase(it);
173 for(std::vector<std::string>::iterator it =
blacklist.begin(); it !=
blacklist.end(); it++ ) {
174 if (l->
name == (*it))
return true;
180 for(std::vector<std::string>::iterator it =
blacklist.begin(); it !=
blacklist.end(); it++ ) {
181 if ((l1->
name +
":" + l2->
name) == (*it) ||
182 (l2->
name +
":" + l1->
name) == (*it))
return true;
188 return (m_robot->getJointPath(p1->link(0),p1->link(1))->numJoints()) > (m_robot->getJointPath(p2->link(0),p2->link(1))->numJoints());
193 std::vector<hrp::ColdetLinkPairPtr> tmp_pair;
195 std::cerr <<
"Setup Initial collision pair without " << std::endl;
196 for(std::vector<std::string>::iterator it =
blacklist.begin(); it !=
blacklist.end(); it++ ) {
197 std::cerr << *it <<
" ";
199 std::cerr << std::endl;
204 for (
unsigned int i=0;
i<m_robot->numLinks();
i++) {
206 for (
unsigned int j=
i+1;
j<m_robot->numLinks();
j++) {
216 std::cerr <<
"Initial collision pair size " << tmp_pair.size() << std::endl;
217 std::vector<hrp::ColdetLinkPairPtr>::iterator it;
219 std::cerr <<
"step 0: Remove collision pair if they are adjacent pair" << std::endl;
220 it = tmp_pair.begin();
221 while ( it != tmp_pair.end() ) {
222 hrp::JointPathPtr jointPath = m_robot->getJointPath((*it)->link(0),(*it)->link(1));
223 if ( jointPath->numJoints() == 1 ) {
225 bool always_collide =
true;
226 bool never_collide =
true;
228 std::cerr << l->
name <<
" ulimit - " << l->
llimit <<
" " << l->
ulimit << std::endl;
229 for(
float angle = l->
llimit; angle <= l->ulimit; angle +=
deg2rad(5)) {
231 m_robot->calcForwardKinematics(
false,
false);
232 m_robot->updateLinkColdetModelPositions();
233 if ( (*it)->detectIntersection() ) {
234 never_collide =
false;
236 always_collide =
false;
239 (*it)->clearCollisions();
241 std::cerr <<
" pair (" << jointPath->numJoints() <<
") " << (*it)->link(0)->name <<
"/" << (*it)->link(1)->name << (always_collide?
" always collide":
"") << (never_collide?
" never collide":
"") << std::endl;
242 it = tmp_pair.erase(it);
248 std::cerr <<
"step 1: Remove complex (2 or 3 distance) collision pair if they never/always collide" << std::endl;
249 it = tmp_pair.begin();
250 while ( it != tmp_pair.end() ) {
251 hrp::JointPathPtr jointPath = m_robot->getJointPath((*it)->link(0),(*it)->link(1));
252 if ( jointPath->numJoints() == 2 || jointPath->numJoints() == 3 ) {
254 bool always_collide =
true;
255 bool never_collide =
true;
256 for(
float angle = l->
llimit; angle <= l->ulimit; angle +=
deg2rad(5)) {
258 m_robot->calcForwardKinematics(
false,
false);
259 m_robot->updateLinkColdetModelPositions();
260 if ( (*it)->detectIntersection() ) {
261 never_collide =
false;
263 always_collide =
false;
266 (*it)->clearCollisions();
268 std::cerr <<
" pair (" << jointPath->numJoints() <<
") " << (*it)->link(0)->name <<
"/" << (*it)->link(1)->name <<
" always:" << always_collide <<
", never:" << never_collide <<
" " << (( always_collide || never_collide )?
"remove from list":
"") << std::endl;
269 if ( always_collide || never_collide ) {
270 it = tmp_pair.erase(it);
278 std::cerr <<
" Remove always/never collide pair for a length of 1,2,3 ... " << tmp_pair.size() << std::endl;
283 std::map<hrp::ColdetLinkPairPtr, std::vector<hrp::ColdetLinkPairPtr> > pair_tree;
286 it = tmp_pair.begin();
287 while ( it != tmp_pair.end() ) {
288 hrp::JointPathPtr jointPath1 = m_robot->getJointPath((*it)->link(0),(*it)->link(1));
291 bool is_new_key =
true;
293 hrp::JointPathPtr jointPath2 = m_robot->getJointPath(((*ii).first)->link(0),((*ii).first)->link(1));
295 bool find_key =
true;
296 for (
unsigned int j = 0;
j < jointPath1->numJoints() ;
j++ ) {
297 if ( jointPath1->joint(
j)->name != jointPath2->joint(
j)->name ) {
303 (*ii).second.push_back(*it);
308 pair_tree[*it] = std::vector<hrp::ColdetLinkPairPtr>(1,*it)
314 std::cerr <<
"step 2: Remove collision pair if they never collide" << std::endl;
318 std::vector<hrp::ColdetLinkPairPtr> sub_pairs = (*ii).second;
319 hrp::JointPathPtr jointPath = m_robot->getJointPath(key_pair->link(0),key_pair->link(1));
322 std::cerr <<
" " << i <<
"/" << tmp_pair.size() <<
" pair (" << jointPath->numJoints() <<
") " << (*ii).first->link(0)->name <<
" " << (*ii).first->link(1)->name <<std::endl;
323 i += (*ii).second.size();
324 for(std::vector<hrp::ColdetLinkPairPtr>::iterator it = sub_pairs.begin(); it != sub_pairs.end(); it++ ) {
325 hrp::JointPathPtr jointPath = m_robot->getJointPath((*it)->link(0),(*it)->link(1));
326 for (
unsigned int j = 0;
j < jointPath->numJoints();
j++ ) {
327 std::cerr << jointPath->joint(
j)->name <<
" ";
329 std::cerr << std::endl;
333 for(std::vector<hrp::ColdetLinkPairPtr>::iterator p = sub_pairs.begin(); p != sub_pairs.end(); p++ ) {
334 for(std::vector<hrp::ColdetLinkPairPtr>::iterator it = tmp_pair.begin(); it != tmp_pair.end(); ) {
335 if ( (*p)->link(0) == (*it)->link(0) && (*p)->link(1) == (*it)->link(1) ) {
344 it = tmp_pair.begin();
345 while ( it != tmp_pair.end() ) {
349 std::cerr <<
"Reduced collision pair size " <<
m_pair.size() << std::endl;
352 int main (
int argc,
char** argv)
356 for (
int i = 1;
i < argc; ++
i) {
357 std::string
arg(argv[
i]);
359 if ( arg ==
"--model" ) {
360 if (++i < argc) url = argv[i];
361 }
else if ( arg ==
"-o" ) {
363 }
else if ( arg[0] ==
'-' || arg[0] ==
'_' ) {
364 std::cerr << argv[0] <<
" : Unknwon arguments " << arg << std::endl;
370 std::cerr <<
"usage: " << argv[0] <<
" --model <model file> <blacklist : R_HAND_J0 L_HAND_J0 EYEBROW_P EYELID_P EYE_Y EYE_P MOUTH_P UPPERLIP_P LOWERLIP_P CHEEK_P>" << std::endl;
376 if (CORBA::is_nil(binfo)){
377 std::cerr <<
"failed to load model[" << url <<
"]" << std::endl;
381 std::cerr <<
"failed to load model[" << url <<
"]" << std::endl;
387 std::string fname =
"/tmp/"+m_robot->name()+
"collision_pair.conf";
388 std::ofstream ofs(fname.c_str());
389 ofs <<
"collision_pair:";
390 for (std::vector<hrp::ColdetLinkPairPtr>::iterator it =
m_pair.begin(); it !=
m_pair.end(); it++) {
391 ofs <<
" " << (*it)->link(0)->name <<
":" << (*it)->link(1)->name;
392 std::cerr << (*it)->link(0)->name <<
":" << (*it)->link(1)->name << std::endl;
396 std::cerr <<
"Write collision pair conf file to " << fname << std::endl;
bool compare_joint_path_length(const hrp::ColdetLinkPairPtr &p1, const hrp::ColdetLinkPairPtr &p2)
std::string normalize(std::string &str)
bool checkBlackListJointPair(hrp::Link *l1, hrp::Link *l2)
HRPMODEL_API OpenHRP::BodyInfo_var loadBodyInfo(const char *url, int &argc, char *argv[])
ColdetModelPtr coldetModel
boost::shared_ptr< Body > BodyPtr
RTC::ReturnCode_t setupCollisionModel(hrp::BodyPtr m_robot, const char *url, OpenHRP::BodyInfo_var binfo)
std::vector< hrp::ColdetLinkPairPtr > m_pair
def j(str, encoding="cp932")
bool checkBlackListJoint(hrp::Link *l)
void setupCollisionLinkPair()
std::string sprintf(char const *__restrict fmt,...)
bool checkCollisionForAllJointRange(int i, hrp::JointPathPtr jointPath, std::vector< hrp::ColdetLinkPairPtr > &collisionPairs)
int loadBodyFromBodyInfo(::World *world, const char *_name, BodyInfo_ptr bodyInfo)
int main(int argc, char **argv)
boost::intrusive_ptr< ColdetLinkPair > ColdetLinkPairPtr
boost::intrusive_ptr< ColdetModel > ColdetModelPtr
std::vector< std::string > blacklist