00001 #include <iostream>
00002 extern "C" {
00003 #include <qhull/qhull_a.h>
00004 }
00005 #include <hrpModel/Link.h>
00006 #include "BVutil.h"
00007
00008 using namespace hrp;
00009
00010 void convertToAABB(hrp::BodyPtr i_body)
00011 {
00012 for (unsigned int i=0; i<i_body->numLinks(); i++) convertToAABB(i_body->link(i));
00013 }
00014
00015 void convertToAABB(hrp::Link *i_link)
00016 {
00017 if (!i_link->coldetModel || !i_link->coldetModel->getNumVertices()) return;
00018
00019 int ptype = i_link->coldetModel->getPrimitiveType();
00020 if (ptype == ColdetModel::SP_PLANE || ptype == ColdetModel::SP_SPHERE){
00021 return;
00022 }
00023
00024 std::vector<Vector3> boundingBoxData;
00025 i_link->coldetModel->getBoundingBoxData(0, boundingBoxData);
00026 if (boundingBoxData.size() != 2){
00027 std::cerr << "unexpected bounding box data size(" << i_link->name
00028 << ", " << boundingBoxData.size() << ")" << std::endl;
00029 return;
00030 }
00031 const Vector3 &c = boundingBoxData[0];
00032 const Vector3 &s = boundingBoxData[1];
00033 ColdetModelPtr coldetModel(new ColdetModel());
00034 coldetModel->setName(std::string(i_link->name.c_str()));
00035 coldetModel->setPrimitiveType(ColdetModel::SP_BOX);
00036 coldetModel->setNumPrimitiveParams(3);
00037 for (int i=0; i<3; i++){
00038 coldetModel->setPrimitiveParam(i, s[i]);
00039 }
00040 double R[] = {1,0,0,0,1,0,0,0,1};
00041 coldetModel->setPrimitivePosition(R, c.data());
00042 coldetModel->setNumVertices(8);
00043 coldetModel->setVertex(0, c[0]+s[0], c[1]+s[1], c[2]+s[2]);
00044 coldetModel->setVertex(1, c[0]-s[0], c[1]+s[1], c[2]+s[2]);
00045 coldetModel->setVertex(2, c[0]-s[0], c[1]-s[1], c[2]+s[2]);
00046 coldetModel->setVertex(3, c[0]+s[0], c[1]-s[1], c[2]+s[2]);
00047 coldetModel->setVertex(4, c[0]+s[0], c[1]+s[1], c[2]-s[2]);
00048 coldetModel->setVertex(5, c[0]-s[0], c[1]+s[1], c[2]-s[2]);
00049 coldetModel->setVertex(6, c[0]-s[0], c[1]-s[1], c[2]-s[2]);
00050 coldetModel->setVertex(7, c[0]+s[0], c[1]-s[1], c[2]-s[2]);
00051 int numTriangles = 12;
00052 coldetModel->setNumTriangles(numTriangles);
00053 int triangles[] = {0,2,3,
00054 0,1,2,
00055 4,3,7,
00056 4,0,3,
00057 0,4,5,
00058 5,1,0,
00059 1,5,6,
00060 1,6,2,
00061 2,6,7,
00062 2,7,3,
00063 7,6,4,
00064 5,4,6};
00065 for(int j=0; j < numTriangles; ++j){
00066 int t0 = triangles[j*3];
00067 int t1 = triangles[j*3+1];
00068 int t2 = triangles[j*3+2];
00069 coldetModel->setTriangle(j, t0, t1, t2);
00070 }
00071 coldetModel->build();
00072 i_link->coldetModel = coldetModel;
00073 }
00074
00075 void convertToConvexHull(hrp::BodyPtr i_body)
00076 {
00077 for (unsigned int i=0; i<i_body->numLinks(); i++){
00078 convertToConvexHull(i_body->link(i));
00079 }
00080 }
00081
00082 void convertToConvexHull(hrp::Link *i_link)
00083 {
00084 if (!i_link->coldetModel || !i_link->coldetModel->getNumVertices()) return;
00085
00086 int ptype = i_link->coldetModel->getPrimitiveType();
00087 if (ptype == ColdetModel::SP_PLANE || ptype == ColdetModel::SP_SPHERE){
00088 return;
00089 }
00090
00091 ColdetModelPtr coldetModel(new ColdetModel());
00092 coldetModel->setName(i_link->name.c_str());
00093 coldetModel->setPrimitiveType(ColdetModel::SP_MESH);
00094
00095 int numVertices = i_link->coldetModel->getNumVertices();
00096 double points[numVertices*3];
00097 float v[3];
00098 for (int i=0; i<numVertices; i++){
00099 i_link->coldetModel->getVertex(i, v[0],v[1],v[2]);
00100 points[i*3+0] = v[0];
00101 points[i*3+1] = v[1];
00102 points[i*3+2] = v[2];
00103 }
00104 char flags[250];
00105 boolT ismalloc = False;
00106 sprintf(flags,"qhull Qt Tc");
00107 if (qh_new_qhull (3,numVertices,points,ismalloc,flags,NULL,stderr)) return;
00108
00109 qh_triangulate();
00110 qh_vertexneighbors();
00111
00112 coldetModel->setNumVertices(qh num_vertices);
00113 coldetModel->setNumTriangles(qh num_facets);
00114 int index[numVertices];
00115 int vertexIndex = 0;
00116 vertexT *vertex;
00117 FORALLvertices {
00118 int p = qh_pointid(vertex->point);
00119 index[p] = vertexIndex;
00120 coldetModel->setVertex(vertexIndex++, points[p*3+0], points[p*3+1], points[p*3+2]);
00121 }
00122 facetT *facet;
00123 int num = qh num_facets;
00124 int triangleIndex = 0;
00125 FORALLfacets {
00126 int j = 0, p[3];
00127 setT *vertices = qh_facet3vertex (facet);
00128 vertexT **vertexp;
00129 FOREACHvertexreverse12_ (vertices) {
00130 if (j<3) {
00131 p[j] = index[qh_pointid(vertex->point)];
00132 } else {
00133 fprintf(stderr, "extra vertex %d\n",j);
00134 }
00135 j++;
00136 }
00137 coldetModel->setTriangle(triangleIndex++, p[0], p[1], p[2]);
00138 }
00139
00140 coldetModel->build();
00141 i_link->coldetModel = coldetModel;
00142
00143 qh_freeqhull(!qh_ALL);
00144 int curlong, totlong;
00145 qh_memfreeshort (&curlong, &totlong);
00146 if (curlong || totlong) {
00147 fprintf(stderr, "convhulln: did not free %d bytes of long memory (%d pieces)\n", totlong, curlong);
00148 }
00149
00150
00151 }