21 #include <hrpCorba/OpenHRPCommon.hh> 22 #include <hrpCorba/ViewSimulator.hh> 37 int size = data->length();
38 for(
int i=0;
i < size-1; ++
i){
39 cout << data[
i] <<
", ";
47 ostream&
operator<<(ostream& os, DblArray3_var& data)
49 cout << data[CORBA::ULong(0)] <<
", " << data[CORBA::ULong(1)] <<
", " << data[CORBA::ULong(2)];
54 ostream&
operator<<(ostream& os, DblArray9_var& data)
56 for(CORBA::ULong
i=0;
i < 8; ++
i){
57 cout << data[
i] <<
", ";
59 cout << data[CORBA::ULong(9)];
64 void dumpBodyInfo(BodyInfo_ptr bodyInfo)
66 cout <<
"<<< BodyInfo >>>\n";
68 CORBA::String_var charaName = bodyInfo->name();
70 cout <<
"name: " << charaName <<
"\n";
72 LinkInfoSequence_var linkInfoSeq = bodyInfo->links();
74 int numLinks = linkInfoSeq->length();
75 cout <<
"num links: " << numLinks <<
"\n";
77 for(
int i=0;
i < numLinks; ++
i){
79 const LinkInfo& linkInfo = linkInfoSeq[
i];
80 CORBA::String_var linkName = linkInfo.name;
82 cout <<
"<<< LinkInfo: " << linkName <<
" (index " <<
i <<
") >>>\n";
83 cout <<
"parentIndex: " << linkInfo.parentIndex <<
"\n";
85 const ShortSequence& childIndices = linkInfo.childIndices;
86 if(childIndices.length() > 0){
87 cout <<
"childIndices: ";
88 for(CORBA::ULong i=0; i < childIndices.length(); ++
i){
89 cout << childIndices[
i] <<
" ";
94 const SensorInfoSequence& sensorInfoSeq = linkInfo.sensors;
96 int numSensors = sensorInfoSeq.length();
97 cout <<
"num sensors: " << numSensors <<
"\n";
99 for(
int j=0; j < numSensors; ++j){
100 cout <<
"<<< SensorInfo >>>\n";
101 const SensorInfo& sensorInfo = sensorInfoSeq[j];
102 cout <<
"id: " << sensorInfo.id <<
"\n";
103 cout <<
"type: " << sensorInfo.type <<
"\n";
105 CORBA::String_var sensorName = sensorInfo.name;
106 cout <<
"name: \"" << sensorName <<
"\"\n";
108 const DblArray3& p = sensorInfo.translation;
109 cout <<
"translation: " << p[0] <<
", " << p[1] <<
", " << p[2] <<
"\n";
111 const DblArray4& r = sensorInfo.rotation;
112 cout <<
"rotation: " << r[0] <<
", " << r[1] <<
", " << r[2] <<
", " << r[3] <<
"\n";
121 inline double getLimitValue(DblSequence limitseq,
double defaultValue)
123 return (limitseq.length() == 0) ? defaultValue : limitseq[0];
126 static Link *createNewLink() {
return new Link(); }
127 class ModelLoaderHelper
130 ModelLoaderHelper() {
131 collisionDetectionModelLoading =
false;
132 createLinkFunc = createNewLink;
135 void enableCollisionDetectionModelLoading(
bool isEnabled) {
136 collisionDetectionModelLoading = isEnabled;
138 void setLinkFactory(
Link *(*
f)()) { createLinkFunc =
f; }
140 bool createBody(
BodyPtr& body, BodyInfo_ptr bodyInfo);
144 LinkInfoSequence_var linkInfoSeq;
145 ShapeInfoSequence_var shapeInfoSeq;
146 ExtraJointInfoSequence_var extraJointInfoSeq;
147 bool collisionDetectionModelLoading;
148 Link *(*createLinkFunc)();
152 void createLights(
Link* link,
const LightInfoSequence& lightInfoSeq,
const Matrix33& Rs);
153 void createColdetModel(
Link* link,
const LinkInfo& linkInfo);
155 const double *R,
const double *p,
156 const ShapeInfo& shapeInfo);
157 void addLinkVerticesAndTriangles(
ColdetModelPtr& coldetModel,
const LinkInfo& linkInfo);
158 void addLinkVerticesAndTriangles(
ColdetModelPtr& coldetModel,
const TransformedShapeIndex& tsi,
const Matrix44& Tparent, ShapeInfoSequence_var& shapes,
int& vertexIndex,
int& triangleIndex);
159 void setExtraJoints();
164 bool ModelLoaderHelper::createBody(
BodyPtr& body, BodyInfo_ptr bodyInfo)
168 const char*
name = bodyInfo->name();
169 body->setModelName(name);
172 int n = bodyInfo->links()->length();
173 linkInfoSeq = bodyInfo->links();
174 shapeInfoSeq = bodyInfo->shapes();
175 extraJointInfoSeq = bodyInfo->extraJoints();
179 for(
int i=0; i <
n; ++
i){
180 if(linkInfoSeq[i].parentIndex < 0){
195 body->setRootLink(rootLink);
196 body->setDefaultRootPosition(rootLink->
b, rootLink->
Rs);
198 body->installCustomizer();
199 body->initializeConfiguration();
212 const LinkInfo& linkInfo = linkInfoSeq[index];
213 int jointId = linkInfo.jointId;
215 Link* link = (*createLinkFunc)();
217 CORBA::String_var name0 = linkInfo.
name;
218 link->
name = string( name0 );
221 Vector3 relPos(linkInfo.translation[0], linkInfo.translation[1], linkInfo.translation[2]);
222 link->
b = parentRs * relPos;
224 Vector3 rotAxis(linkInfo.rotation[0], linkInfo.rotation[1], linkInfo.rotation[2]);
226 link->
Rs = (parentRs * R);
229 CORBA::String_var jointType = linkInfo.jointType;
230 const std::string jt( jointType );
234 }
else if(jt ==
"free" ){
236 }
else if(jt ==
"rotate" ){
237 link->
jointType = Link::ROTATIONAL_JOINT;
238 }
else if(jt ==
"slide" ){
240 }
else if(jt ==
"crawler"){
248 if(link->
jointType == Link::ROTATIONAL_JOINT || link->
jointType == Link::SLIDE_JOINT){
249 std::cerr <<
"Warning: Joint ID is not given to joint " << link->
name 250 <<
" of model " << body->modelName() <<
"." << std::endl;
257 Vector3 axis( Rs *
Vector3(linkInfo.jointAxis[0], linkInfo.jointAxis[1], linkInfo.jointAxis[2]));
259 if(link->
jointType == Link::ROTATIONAL_JOINT || jt ==
"crawler"){
261 }
else if(link->
jointType == Link::SLIDE_JOINT){
265 link->
m = linkInfo.mass;
266 link->
Ir = linkInfo.rotorInertia;
275 DblSequence ulimit = linkInfo.ulimit;
276 DblSequence llimit = linkInfo.llimit;
277 DblSequence uvlimit = linkInfo.uvlimit;
278 DblSequence lvlimit = linkInfo.lvlimit;
279 DblSequence climit = linkInfo.climit;
289 link->
c = Rs *
Vector3(linkInfo.centerOfMass[0], linkInfo.centerOfMass[1], linkInfo.centerOfMass[2]);
293 link->
I = Rs * Io * Rs.transpose();
296 std::stack<Link*> children;
299 int childNum = linkInfo.childIndices.length();
300 for(
int i = 0 ; i < childNum ; i++) {
301 int childIndex = linkInfo.childIndices[
i];
304 children.push(childLink);
307 while(!children.empty()){
313 createLights(link, linkInfo.
lights, Rs);
315 if(collisionDetectionModelLoading){
316 createColdetModel(link, linkInfo);
323 void ModelLoaderHelper::createLights(
Link* link,
const LightInfoSequence& lightInfoSeq,
const Matrix33& Rs)
325 int numLights = lightInfoSeq.length();
327 for(
int i=0 ; i < numLights ; ++
i ) {
328 const LightInfo& lightInfo = lightInfoSeq[
i];
329 std::string
name(lightInfo.name);
330 Light *light = body->createLight(link, lightInfo.type, name);
331 const double *T = lightInfo.transformMatrix;
332 light->
localPos << T[3], T[7], T[11];
333 light->
localR << T[0], T[1], T[2], T[4], T[5], T[6], T[8], T[9], T[10];
334 switch (lightInfo.type){
341 light->
on = lightInfo.on;
342 light->
radius = lightInfo.radius;
344 case Light::DIRECTIONAL:
348 light->
on = lightInfo.on;
357 light->
on = lightInfo.on;
358 light->
radius = lightInfo.radius;
364 std::cerr <<
"unknown light type" << std::endl;
371 int numSensors = sensorInfoSeq.length();
373 for(
int i=0 ; i < numSensors ; ++
i ) {
374 const SensorInfo& sensorInfo = sensorInfoSeq[
i];
376 int id = sensorInfo.id;
378 std::cerr <<
"Warning: sensor ID is not given to sensor " << sensorInfo.name
379 <<
"of model " << body->modelName() <<
"." << std::endl;
381 int sensorType = Sensor::COMMON;
383 CORBA::String_var type0 = sensorInfo.type;
386 if(type ==
"Force") { sensorType = Sensor::FORCE; }
387 else if(type ==
"RateGyro") { sensorType = Sensor::RATE_GYRO; }
388 else if(type ==
"Acceleration") { sensorType = Sensor::ACCELERATION; }
389 else if(type ==
"Vision") { sensorType = Sensor::VISION; }
390 else if(type ==
"Range") { sensorType = Sensor::RANGE; }
392 CORBA::String_var name0 = sensorInfo.name;
395 Sensor* sensor = body->createSensor(link, sensorType,
id, name);
398 const DblArray3& p = sensorInfo.translation;
401 const Vector3 axis(sensorInfo.rotation[0], sensorInfo.rotation[1], sensorInfo.rotation[2]);
406 if ( sensorType == Sensor::RANGE ) {
408 range->
scanAngle = sensorInfo.specValues[0];
409 range->
scanStep = sensorInfo.specValues[1];
410 range->
scanRate = sensorInfo.specValues[2];
412 }
else if (sensorType == Sensor::VISION) {
414 vision->
near = sensorInfo.specValues[0];
415 vision->
far = sensorInfo.specValues[1];
416 vision->
fovy = sensorInfo.specValues[2];
417 vision->
width = sensorInfo.specValues[4];
418 vision->
height = sensorInfo.specValues[5];
420 switch((
int)sensorInfo.specValues[3]){
426 vision->
image.resize(npixel*3);
430 vision->
image.resize(npixel);
435 case Camera::COLOR_DEPTH:
436 vision->
imageType = VisionSensor::COLOR_DEPTH;
437 vision->
image.resize(npixel*3);
439 case Camera::MONO_DEPTH:
440 vision->
imageType = VisionSensor::MONO_DEPTH;
441 vision->
image.resize(npixel);
444 vision->
frameRate = sensorInfo.specValues[6];
451 void ModelLoaderHelper::createColdetModel(
Link* link,
const LinkInfo& linkInfo)
453 int totalNumVertices = 0;
454 int totalNumTriangles = 0;
455 const TransformedShapeIndexSequence& shapeIndices = linkInfo.shapeIndices;
456 unsigned int nshape = shapeIndices.length();
459 for(
unsigned int i=0; i < shapeIndices.length(); i++){
460 shapeIndex = shapeIndices[
i].shapeIndex;
461 const DblArray12 &tform = shapeIndices[
i].transformMatrix;
462 R[0] = tform[0]; R[1] = tform[1]; R[2] = tform[2]; p[0] = tform[3];
463 R[3] = tform[4]; R[4] = tform[5]; R[5] = tform[6]; p[1] = tform[7];
464 R[6] = tform[8]; R[7] = tform[9]; R[8] = tform[10]; p[2] = tform[11];
465 const ShapeInfo& shapeInfo = shapeInfoSeq[shapeIndex];
466 totalNumVertices += shapeInfo.vertices.length() / 3;
467 totalNumTriangles += shapeInfo.triangles.length() / 3;
470 const SensorInfoSequence& sensors = linkInfo.sensors;
471 for (
unsigned int i=0; i<sensors.length(); i++){
472 const SensorInfo &sinfo = sensors[
i];
473 const TransformedShapeIndexSequence tsis = sinfo.shapeIndices;
474 nshape += tsis.length();
475 for (
unsigned int j=0; j<tsis.length(); j++){
476 short shapeIndex = tsis[j].shapeIndex;
477 const ShapeInfo& shapeInfo = shapeInfoSeq[shapeIndex];
478 totalNumTriangles += shapeInfo.triangles.length() / 3;
479 totalNumVertices += shapeInfo.vertices.length() / 3 ;
484 coldetModel->setName(std::string(linkInfo.name));
485 if(totalNumTriangles > 0){
486 coldetModel->setNumVertices(totalNumVertices);
487 coldetModel->setNumTriangles(totalNumTriangles);
489 addLinkPrimitiveInfo(coldetModel, R, p, shapeInfoSeq[shapeIndex]);
491 addLinkVerticesAndTriangles(coldetModel, linkInfo);
492 coldetModel->build();
497 void ModelLoaderHelper::addLinkVerticesAndTriangles
498 (
ColdetModelPtr& coldetModel,
const TransformedShapeIndex& tsi,
const Matrix44& Tparent, ShapeInfoSequence_var& shapes,
int& vertexIndex,
int& triangleIndex)
500 short shapeIndex = tsi.shapeIndex;
501 const DblArray12& M = tsi.transformMatrix;;
503 Tlocal << M[0], M[1], M[2], M[3],
504 M[4], M[5], M[6], M[7],
505 M[8], M[9], M[10], M[11],
507 T = Tparent * Tlocal;
509 const ShapeInfo& shapeInfo = shapes[shapeIndex];
510 int vertexIndexBase = vertexIndex;
511 const FloatSequence& vertices = shapeInfo.vertices;
512 const int numVertices = vertices.length() / 3;
513 for(
int j=0; j < numVertices; ++j){
514 Vector4 v(T *
Vector4(vertices[j*3], vertices[j*3+1], vertices[j*3+2], 1.0));
515 coldetModel->setVertex(vertexIndex++, v[0], v[1], v[2]);
518 const LongSequence& triangles = shapeInfo.triangles;
519 const int numTriangles = triangles.length() / 3;
520 for(
int j=0; j < numTriangles; ++j){
521 int t0 = triangles[j*3] + vertexIndexBase;
522 int t1 = triangles[j*3+1] + vertexIndexBase;
523 int t2 = triangles[j*3+2] + vertexIndexBase;
524 coldetModel->setTriangle( triangleIndex++, t0, t1, t2);
529 void ModelLoaderHelper::addLinkVerticesAndTriangles(
ColdetModelPtr& coldetModel,
const LinkInfo& linkInfo)
532 int triangleIndex = 0;
534 const TransformedShapeIndexSequence& shapeIndices = linkInfo.shapeIndices;
537 for(
unsigned int i=0; i < shapeIndices.length(); i++){
538 addLinkVerticesAndTriangles(coldetModel, shapeIndices[i], E, shapeInfoSeq,
539 vertexIndex, triangleIndex);
543 const SensorInfoSequence& sensors = linkInfo.sensors;
544 for (
unsigned int i=0; i<sensors.length(); i++){
545 const SensorInfo& sensor = sensors[
i];
547 sensor.rotation[2]), sensor.rotation[3]);
548 T(0,3) = sensor.translation[0];
549 T(1,3) = sensor.translation[1];
550 T(2,3) = sensor.translation[2];
551 const TransformedShapeIndexSequence& shapeIndices = sensor.shapeIndices;
552 for (
unsigned int j=0; j<shapeIndices.length(); j++){
553 addLinkVerticesAndTriangles(coldetModel, shapeIndices[j], T,
555 vertexIndex, triangleIndex);
560 void ModelLoaderHelper::addLinkPrimitiveInfo(
ColdetModelPtr& coldetModel,
561 const double *R,
const double *p,
562 const ShapeInfo& shapeInfo)
564 switch(shapeInfo.primitiveType){
566 coldetModel->setPrimitiveType(ColdetModel::SP_BOX);
569 coldetModel->setPrimitiveType(ColdetModel::SP_CYLINDER);
572 coldetModel->setPrimitiveType(ColdetModel::SP_CONE);
575 coldetModel->setPrimitiveType(ColdetModel::SP_SPHERE);
578 coldetModel->setPrimitiveType(ColdetModel::SP_PLANE);
583 coldetModel->setNumPrimitiveParams(shapeInfo.primitiveParameters.length());
584 for (
unsigned int i=0; i<shapeInfo.primitiveParameters.length(); i++){
585 coldetModel->setPrimitiveParam(i, shapeInfo.primitiveParameters[i]);
587 coldetModel->setPrimitivePosition(R, p);
590 void ModelLoaderHelper::setExtraJoints()
592 body->extraJoints.clear();
593 int n = extraJointInfoSeq->length();
595 for(
int i=0; i <
n; ++
i){
596 const ExtraJointInfo& extraJointInfo = extraJointInfoSeq[
i];
598 joint.
name = extraJointInfo.name;
599 joint.
link[0] = body->link(
string(extraJointInfo.link[0]));
600 joint.
link[1] = body->link(
string(extraJointInfo.link[1]));
602 ExtraJointType jointType = extraJointInfo.
jointType;
603 if(jointType == OpenHRP::EJ_XY){
604 joint.
type = Body::EJ_XY;
605 }
else if(jointType == OpenHRP::EJ_XYZ){
606 joint.
type = Body::EJ_XYZ;
607 }
else if(jointType == OpenHRP::EJ_Z){
608 joint.
type = Body::EJ_Z;
611 joint.
axis =
Vector3(extraJointInfo.axis[0], extraJointInfo.axis[1], extraJointInfo.axis[2] );
612 joint.
point[0] =
Vector3(extraJointInfo.point[0][0], extraJointInfo.point[0][1], extraJointInfo.point[0][2] );
613 joint.
point[1] =
Vector3(extraJointInfo.point[1][0], extraJointInfo.point[1][1], extraJointInfo.point[1][2] );
615 body->extraJoints.push_back(joint);
621 if(!CORBA::is_nil(bodyInfo)){
622 ModelLoaderHelper helper;
623 if (
f) helper.setLinkFactory(
f);
624 if(loadGeometryForCollisionDetection){
625 helper.enableCollisionDetectionModelLoading(
true);
627 return helper.createBody(body, bodyInfo);
634 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
640 CosNaming::NamingContext_var cxt;
642 CORBA::Object_var nS = orb->resolve_initial_references(
"NameService");
643 cxt = CosNaming::NamingContext::_narrow(nS);
644 }
catch(CORBA::SystemException& ex) {
645 std::cerr <<
"NameService doesn't exist" << std::endl;
646 return BodyInfo::_nil();
653 CosNaming::NamingContext_var cxt;
655 CORBA::Object_var nS = orb->resolve_initial_references(
"NameService");
656 cxt = CosNaming::NamingContext::_narrow(nS);
657 }
catch(CORBA::SystemException& ex) {
658 std::cerr <<
"NameService doesn't exist" << std::endl;
666 CosNaming::Name ncName;
668 ncName[0].id = CORBA::string_dup(
"ModelLoader");
669 ncName[0].kind = CORBA::string_dup(
"");
670 ModelLoader_var modelLoader = NULL;
672 modelLoader = ModelLoader::_narrow(cxt->resolve(ncName));
673 modelLoader->_non_existent();
675 std::cerr <<
"ModelLoader not found: ";
677 case CosNaming::NamingContext::missing_node:
678 std::cerr <<
"Missing Node" << std::endl;
679 case CosNaming::NamingContext::not_context:
680 std::cerr <<
"Not Context" << std::endl;
682 case CosNaming::NamingContext::not_object:
683 std::cerr <<
"Not Object" << std::endl;
686 modelLoader = ModelLoader::_nil();
687 }
catch(CosNaming::NamingContext::CannotProceed &exc) {
688 std::cerr <<
"Resolve ModelLoader CannotProceed" << std::endl;
689 modelLoader = ModelLoader::_nil();
690 }
catch(CosNaming::NamingContext::AlreadyBound &exc) {
691 std::cerr <<
"Resolve ModelLoader InvalidName" << std::endl;
692 modelLoader = ModelLoader::_nil();
694 modelLoader = ModelLoader::_nil();
703 BodyInfo_var bodyInfo;
705 bodyInfo = modelLoader->getBodyInfo(url);
706 }
catch(CORBA::SystemException& ex) {
707 std::cerr <<
"CORBA::SystemException raised by ModelLoader: " << ex._rep_id() << std::endl;
708 }
catch(ModelLoader::ModelLoaderException& ex){
709 std::cerr <<
"ModelLoaderException : " << ex.description << std::endl;
718 if(!CORBA::is_nil(bodyInfo)){
719 ModelLoaderHelper helper;
720 if(loadGeometryForCollisionDetection){
721 helper.enableCollisionDetectionModelLoading(
true);
723 return helper.createBody(body, bodyInfo);
732 CosNaming::NamingContext_var cxt;
734 CORBA::Object_var nS = orb->resolve_initial_references(
"NameService");
735 cxt = CosNaming::NamingContext::_narrow(nS);
736 }
catch(CORBA::SystemException& ex) {
737 std::cerr <<
"NameService doesn't exist" << std::endl;
747 CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);
double Ir
rotor inertia [kg.m^2]
png_infop png_charp png_int_32 png_int_32 int * type
HRPMODEL_API OpenHRP::BodyInfo_var loadBodyInfo(const char *url, int &argc, char *argv[])
void addChild(Link *link)
HRPMODEL_API OpenHRP::ModelLoader_var getModelLoader(CosNaming::NamingContext_var cxt)
std::vector< unsigned char > image
static const bool debugMode
ColdetModelPtr coldetModel
png_infop png_charpp name
HRPMODEL_API bool loadBodyFromBodyInfo(BodyPtr body, OpenHRP::BodyInfo_ptr bodyInfo, bool loadGeometryForCollisionDetection=false, Link *(*f)()=NULL)
void getMatrix33FromRowMajorArray(Matrix33 &m33, const Array &a, size_t top=0)
boost::shared_ptr< Body > BodyPtr
std::vector< Light * > lights
lights attached to this link
double lvlimit
the lower limit of joint velocities
Matrix33 rodrigues(const Vector3 &axis, double q)
Vector3 b
relative position (parent local)
Matrix33 Rs
relative attitude of the link segment (self local)
std::ostream & operator<<(std::ostream &ost, const Point &p)
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
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)
void getVector3(Vector3 &v3, const V &v, size_t top=0)
double llimit
the lower limit of joint values
double climit
the upper limit of joint current (tlimit = climit x grarRatio x torqueConst)
std::vector< Sensor * > sensors
sensors attached to this link
boost::intrusive_ptr< ColdetModel > ColdetModelPtr
double ulimit
the upper limit of joint values
HRPMODEL_API bool loadBodyFromModelLoader(BodyPtr body, const char *url, CORBA_ORB_var orb, bool loadGeometryForCollisionDetection=false)
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].