23 void createGeometry(
ODE_Link* link,
const LinkInfo& linkInfo);
24 void addLinkVerticesAndTriangles(
ODE_Link* link,
const LinkInfo& linkInfo);
30 return (limitseq.length() == 0) ? defaultValue : limitseq[0];
36 bool ModelLoaderHelper::createBody(
BodyPtr body,
ODE_World* world, BodyInfo_ptr bodyInfo)
41 const char*
name = bodyInfo->name();
42 body->setModelName(name);
45 int n = bodyInfo->links()->length();
46 linkInfoSeq = bodyInfo->links();
47 shapeInfoSeq = bodyInfo->shapes();
51 for(
int i=0;
i <
n; ++
i){
52 if(linkInfoSeq[
i].parentIndex < 0){
66 body->setRootLink(rootLink);
68 body->setDefaultRootPosition(rootLink->
b, rootLink->
Rs);
70 body->initializeConfiguration();
80 const LinkInfo& linkInfo = linkInfoSeq[
index];
81 int jointId = linkInfo.jointId;
84 dBodyID bodyId = dBodyCreate(worldId);
87 CORBA::String_var name0 = linkInfo.name;
88 link->
name = string( name0 );
92 parentRs << parentT(0,0), parentT(0,1), parentT(0,2),
93 parentT(1,0), parentT(1,1), parentT(1,2),
94 parentT(2,0), parentT(2,1), parentT(2,2);
95 Vector3 relPos(linkInfo.translation[0], linkInfo.translation[1], linkInfo.translation[2]);
96 link->
b = parentRs * relPos;
98 Vector3 rotAxis(linkInfo.rotation[0], linkInfo.rotation[1], linkInfo.rotation[2]);
101 localT(0,3) = linkInfo.translation[0];
102 localT(1,3) = linkInfo.translation[1];
103 localT(2,3) = linkInfo.translation[2];
106 link->
Rs << T(0,0), T(0,1), T(0,2),
107 T(1,0), T(1,1), T(1,2),
108 T(2,0), T(2,1), T(2,2);
109 Vector3 p(T(0,3), T(1,3), T(2,3));
111 link->
C =
Vector3(linkInfo.centerOfMass[0], linkInfo.centerOfMass[1], linkInfo.centerOfMass[2]);
115 CORBA::String_var jointType = linkInfo.jointType;
116 const string jt( jointType );
119 dJointID djointId = dJointCreateFixed(worldId, 0);
120 dJointAttach(djointId, bodyId, 0);
122 }
else if(jt ==
"free" ){
124 }
else if(jt ==
"rotate" ){
126 dJointID djointId = dJointCreateHinge(worldId, 0);
127 dJointAttach(djointId, bodyId, parentBodyId);
128 dJointSetHingeAnchor(djointId, T(0,3), T(1,3), T(2,3));
129 Vector4 axis( T *
Vector4(linkInfo.jointAxis[0], linkInfo.jointAxis[1], linkInfo.jointAxis[2], 0));
130 dJointSetHingeAxis(djointId, axis(0), axis(1), axis(2));
132 }
else if(jt ==
"slide" ){
134 dJointID djointId = dJointCreateSlider(worldId, 0);
135 dJointAttach(djointId, bodyId, parentBodyId);
136 Vector4 axis( T *
Vector4(linkInfo.jointAxis[0], linkInfo.jointAxis[1], linkInfo.jointAxis[2], 0));
137 dJointSetSliderAxis(djointId, axis(0), axis(1), axis(2));
145 Vector3 axis( Rs *
Vector3(linkInfo.jointAxis[0], linkInfo.jointAxis[1], linkInfo.jointAxis[2]));
155 cerr <<
"Warning: Joint ID is not given to joint " << link->
name 156 <<
" of model " << body->modelName() <<
"." << endl;
160 link->
m = linkInfo.mass;
161 link->
Ir = linkInfo.rotorInertia;
166 dMassSetParameters(&mass, linkInfo.mass,
168 linkInfo.inertia[0], linkInfo.inertia[4], linkInfo.inertia[8],
169 linkInfo.inertia[1], linkInfo.inertia[2], linkInfo.inertia[5] );
170 dBodySetMass(bodyId, &mass);
180 DblSequence ulimit = linkInfo.ulimit;
181 DblSequence llimit = linkInfo.llimit;
182 DblSequence uvlimit = linkInfo.uvlimit;
183 DblSequence lvlimit = linkInfo.lvlimit;
192 link->
c = Rs *
Vector3(linkInfo.centerOfMass[0], linkInfo.centerOfMass[1], linkInfo.centerOfMass[2]);
196 link->
I = Rs * Io * Rs.transpose();
199 stack<Link*> children;
202 int childNum = linkInfo.childIndices.length();
203 for(
int i = 0 ;
i < childNum ;
i++) {
204 int childIndex = linkInfo.childIndices[
i];
207 children.push(childLink);
210 while(!children.empty()){
217 createGeometry(link, linkInfo);
222 void ModelLoaderHelper::createGeometry(
ODE_Link* link,
const LinkInfo& linkInfo)
224 int totalNumTriangles = 0;
225 const TransformedShapeIndexSequence& shapeIndices = linkInfo.shapeIndices;
227 for(
unsigned int i=0;
i < shapeIndices.length();
i++){
228 const TransformedShapeIndex& tsi = shapeIndices[
i];
229 const DblArray12& M = tsi.transformMatrix;
230 dMatrix3 R = { M[0], M[1], M[2], 0,
232 M[8], M[9], M[10], 0 };
233 short shapeIndex = shapeIndices[
i].shapeIndex;
234 const ShapeInfo& shapeInfo = shapeInfoSeq[shapeIndex];
236 R0 << M[0],M[1],M[2],
240 switch(shapeInfo.primitiveType){
241 case OpenHRP::SP_BOX :{
242 dReal x = shapeInfo.primitiveParameters[0];
243 dReal y = shapeInfo.primitiveParameters[1];
244 dReal z = shapeInfo.primitiveParameters[2];
245 link->
geomIds.push_back(dCreateBox( spaceId, x, y, z));
247 dGeomSetOffsetRotation(link->
geomIds.at(numofGeom), R);
248 dGeomSetOffsetPosition(link->
geomIds.at(numofGeom), M[3]-link->
C(0), M[7]-link->
C(1), M[11]-link->
C(2));
252 case OpenHRP::SP_CYLINDER :{
253 dReal radius = shapeInfo.primitiveParameters[0];
255 link->
geomIds.push_back(dCreateCylinder( spaceId, radius, height));
257 dGeomSetOffsetRotation(link->
geomIds.at(numofGeom), R);
258 dGeomSetOffsetPosition(link->
geomIds.at(numofGeom), M[3]-link->
C(0), M[7]-link->
C(1), M[11]-link->
C(2));
262 case OpenHRP::SP_SPHERE :{
263 dReal radius = shapeInfo.primitiveParameters[0];
264 link->
geomIds.push_back(dCreateSphere( spaceId, radius ));
266 dGeomSetOffsetRotation(link->
geomIds.at(numofGeom), R);
267 dGeomSetOffsetPosition(link->
geomIds.at(numofGeom), M[3]-link->
C(0), M[7]-link->
C(1), M[11]-link->
C(2));
272 totalNumTriangles += shapeInfo.triangles.length() / 3;
275 totalNumTriangles += shapeInfo.triangles.length() / 3;
277 int totalNumVertices = totalNumTriangles * 3;
279 link->
vertices.resize(totalNumVertices*3);
280 link->
indices.resize(totalNumTriangles*3);
281 addLinkVerticesAndTriangles( link, linkInfo );
282 if(totalNumTriangles){
285 totalNumVertices, &link->
indices[0], totalNumTriangles*3, 3*
sizeof(
int));
288 dGeomSetOffsetPosition (link->
geomIds.at(numofGeom), -link->
C(0), -link->
C(1), -link->
C(2));
294 void ModelLoaderHelper::addLinkVerticesAndTriangles(
ODE_Link* link,
const LinkInfo& linkInfo)
297 int triangleIndex = 0;
299 const TransformedShapeIndexSequence& shapeIndices = linkInfo.shapeIndices;
301 for(
unsigned int i=0;
i < shapeIndices.length();
i++){
302 const TransformedShapeIndex& tsi = shapeIndices[
i];
303 short shapeIndex = tsi.shapeIndex;
304 const ShapeInfo& shapeInfo = shapeInfoSeq[shapeIndex];
305 const DblArray12& M = tsi.transformMatrix;
307 R0 << M[0],M[1],M[2],
311 (shapeInfo.primitiveType == OpenHRP::SP_BOX ||
312 shapeInfo.primitiveType == OpenHRP::SP_CYLINDER ||
313 shapeInfo.primitiveType == OpenHRP::SP_SPHERE ) )
317 T << M[0], M[1], M[2], M[3],
318 M[4], M[5], M[6], M[7],
319 M[8], M[9], M[10], M[11],
321 const FloatSequence& vertices = shapeInfo.vertices;
322 const LongSequence& triangles = shapeInfo.triangles;
323 const int numTriangles = triangles.length() / 3;
325 for(
int j=0; j < numTriangles; ++j){
326 int vertexIndexTop = vertexIndex;
327 for(
int k=0; k < 3; ++k){
328 long orgVertexIndex = shapeInfo.triangles[j * 3 + k];
329 int p = orgVertexIndex * 3;
330 Vector4 v(T *
Vector4(vertices[p+0], vertices[p+1], vertices[p+2], 1.0));
331 link->
vertices[vertexIndex*3] = v[0];
332 link->
vertices[vertexIndex*3+1] = v[1];
333 link->
vertices[vertexIndex*3+2] = v[2];
336 link->
indices[triangleIndex++] = vertexIndexTop;
337 link->
indices[triangleIndex++] = vertexIndexTop+1;
338 link->
indices[triangleIndex++] = vertexIndexTop+2;
345 int numSensors = sensorInfoSeq.length();
347 for(
int i=0 ;
i < numSensors ; ++
i ) {
348 const SensorInfo& sensorInfo = sensorInfoSeq[
i];
350 int id = sensorInfo.id;
352 cerr <<
"Warning: sensor ID is not given to sensor " << sensorInfo.name
353 <<
"of model " << body->modelName() <<
"." << endl;
357 CORBA::String_var type0 = sensorInfo.type;
366 CORBA::String_var name0 = sensorInfo.name;
378 body->addSensor(sensor, sensorType,
id);
381 const DblArray3& p = sensorInfo.translation;
384 const Vector3 axis(sensorInfo.rotation[0], sensorInfo.rotation[1], sensorInfo.rotation[2]);
395 range->
scanAngle = sensorInfo.specValues[0];
396 range->
scanStep = sensorInfo.specValues[1];
397 range->
scanRate = sensorInfo.specValues[2];
406 if(!CORBA::is_nil(bodyInfo)){
408 return helper.
createBody(body, world, bodyInfo);
double Ir
rotor inertia [kg.m^2]
png_infop png_charp png_int_32 png_int_32 int * type
void addChild(Link *link)
void setTransform(const hrp::Vector3 &pos, const hrp::Matrix33 &R)
png_infop png_charpp name
translational joint (1 dof)
HRP_UTIL_EXPORT bool isOrthogonalMatrix(Matrix33 &m)
void getMatrix33FromRowMajorArray(Matrix33 &m33, const Array &a, size_t top=0)
boost::shared_ptr< Body > BodyPtr
png_infop png_uint_32 png_uint_32 * height
double lvlimit
the lower limit of joint velocities
static Sensor * create(int type)
Matrix33 rodrigues(const Vector3 &axis, double q)
Vector3 b
relative position (parent local)
Matrix33 Rs
relative attitude of the link segment (self local)
std::vector< dReal > vertices
std::vector< dGeomID > geomIds
double uvlimit
the upper limit of joint velocities
Matrix33 I
inertia tensor (self local, around c)
int jointId
jointId value written in a model file
LinkInfoSequence_var linkInfoSeq
bool ODE_loadBodyFromBodyInfo(BodyPtr body, ODE_World *world, OpenHRP::BodyInfo_ptr bodyInfo)
static Joint * createLink(::World *world, const char *charname, int index, LinkInfoSequence_var iLinks, Joint *pjoint)
double getLimitValue(DblSequence limitseq, double defaultValue)
HRP_UTIL_EXPORT void calcRodrigues(Matrix33 &out_R, const Vector3 &axis, double q)
double llimit
the lower limit of joint values
dTriMeshDataID triMeshDataId
bool createBody(BodyPtr body, ODE_World *worldId, BodyInfo_ptr bodyInfo)
std::vector< int > indices
std::vector< Sensor * > sensors
sensors attached to this link
ShapeInfoSequence_var shapeInfoSeq
double ulimit
the upper limit of joint values
Vector3 a
rotational joint axis (self local)
Vector3 d
translation joint axis (self local)
static int max(int a, int b)
Vector3 c
center of mass (self local)
static void createSensors(::World *world, Joint *jnt, SensorInfoSequence iSensors)
double Jm2
Equivalent rotor inertia: n^2*Jm [kg.m^2].