iCub-main
bvh.cpp
Go to the documentation of this file.
1 /*
2  * bvh.cpp
3  */
4 
5 /*
6  * Copyright (C) 2009 RobotCub Consortium
7  * Author: Alessandro Scalzo alessandro.scalzo@iit.it
8  * CopyPolicy: Released under the terms of the GNU GPL v2.0.
9  *
10  * Based on:
11  *
12  * Qavimator
13  * Copyright (C) 2006 by Zi Ree *
14  * Zi Ree @ SecondLife *
15  * Released under the terms of the GNU GPL v2.0.
16  */
17 
18 #include <iostream>
19 #include <errno.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string>
23 
24 #include <qmessagebox.h>
25 
26 #include "bvh.h"
27 
28 extern std::string GUI_NAME;
29 
31 {
32  pRoot=NULL;
33  mObjectsManager=objManager;
34 
35  mAB=new BVHNode**[8];
36 
37  for (int p=0; p<8; ++p)
38  {
39  mAB[p]=new BVHNode*[8];
40 
41  for (int l=0; l<8; ++l)
42  {
43  mAB[p][l]=NULL;
44  }
45  }
46 }
47 
48 bool BVH::Create(yarp::os::ResourceFinder& config)
49 {
50  robot=QString(config.find("robot").asString().c_str());
51 
52  bvhChannelName.append("Xposition");
53  bvhChannelName.append("Yposition");
54  bvhChannelName.append("Zposition");
55  bvhChannelName.append("Xrotation");
56  bvhChannelName.append("Yrotation");
57  bvhChannelName.append("Zrotation");
58 
59  // default avatar scale for BVH and AVM files
60  dAvatarScale=1.0;
61 
62  pRoot=bvhRead(config);
64 
65  return pRoot!=NULL;
66 }
67 
68 BVH::~ BVH()
69 {
70  portEncBase.interrupt();
71  portEncHead.interrupt();
72  portEncTorso.interrupt();
73  portEncLeftArm.interrupt();
74  portEncRightArm.interrupt();
75  portEncLeftLeg.interrupt();
76  portEncRightLeg.interrupt();
77 
78  portEncBase.close();
79  portEncHead.close();
80  portEncTorso.close();
81  portEncLeftArm.close();
82  portEncRightArm.close();
83  portEncLeftLeg.close();
84  portEncRightLeg.close();
85 
86  if (pRoot)
87  {
88  delete pRoot;
89  pRoot=NULL;
90  }
91 
92  Network::fini();
93 
94  if (mAB)
95  {
96  for (int p=0; p<8; ++p)
97  {
98  if (mAB[p])
99  {
100  delete [] mAB[p];
101  mAB[p]=NULL;
102  }
103  }
104 
105  delete [] mAB;
106 
107  mAB=NULL;
108  }
109 }
110 
111 QString BVH::token()
112 {
113  if (tokenPos>=(int)tokens.size())
114  {
115  qDebug("BVH::token(): no more tokens at index %d",tokenPos);
116  return QString();
117  }
118  return tokens[tokenPos++];
119 }
120 
121 bool BVH::expect_token(const QString& name)
122 {
123  if(name!=token())
124  {
125  qDebug("BVH::expect_token(): Bad file: %s missing\n", name.toLatin1().data());
126  return false;
127  }
128  return true;
129 }
130 
131 BVHNode* BVH::bvhRead(yarp::os::ResourceFinder& config)
132 {
133  QString fileName(config.findPath("geometry").c_str());
134  QFile geometryFile(fileName);
135  if(!geometryFile.open(QIODevice::ReadOnly))
136  {
137  QMessageBox::critical(0,QObject::tr("File not found"),QObject::tr("BVH File not found: %1").arg(fileName.toLatin1().data()));
138  return NULL;
139  }
140 
141  inputFile=QString(geometryFile.readAll());
142  geometryFile.close();
143 
144  tokens=tokenize(inputFile.simplified(),' ');
145 
146  tokenPos=0;
147 
148  Network::init();
149 
150  portEncBase.open((GUI_NAME+"/base:i").c_str());
151  portEncTorso.open((GUI_NAME+"/torso:i").c_str());
152  portEncHead.open((GUI_NAME+"/head:i").c_str());
153  portEncLeftArm.open((GUI_NAME+"/left_arm:i").c_str());
154  portEncRightArm.open((GUI_NAME+"/right_arm:i").c_str());
155  portEncLeftLeg.open((GUI_NAME+"/left_leg:i").c_str());
156  portEncRightLeg.open((GUI_NAME+"/right_leg:i").c_str());
157 
159 
160  memset(dEncBuffer,0,sizeof(dEncBuffer));
161  dEncBuffer[10]=90.0;
162  dEncBuffer[26]=90.0;
163 
164  nJTorso=3;
165  nJHead=6;
166  nJLeftArm=16;
167  nJRightArm=16;
168  nJLeftLeg=6;
169  nJRightLeg=6;
170  nJBase=6;
171 
179 
180  return bvhReadNode(config);
181 }
182 
183 BVHNode* BVH::bvhReadNode(yarp::os::ResourceFinder& config)
184 {
185  QString sType=token();
186  if (sType=="}") return NULL;
187 
188  // check for node type first
189 
190  static const int BVH_ROOT=1,BVH_JOINT=2,BVH_END=3;
191  int nType;
192  if (sType=="ROOT") nType=BVH_ROOT;
193  else if (sType=="JOINT") nType=BVH_JOINT;
194  else if (sType=="END") nType=BVH_END;
195  else
196  {
197  qDebug("BVH::bvhReadNode(): Bad animation file: unknown node type: '%s'\n",sType.toLatin1().data());
198  return NULL;
199  }
200 
201  BVHNode *node=NULL;
202 
203  QString sName=token();
204  partNames << sName;
205 
206  expect_token("{");
207  iCubMesh *pMesh=0;
208  QString tag=token();
209  QString ftPortName="";
210 
211  int skinPart=0;
212  int skinLink=0;
213 
214  if (tag=="SKIN")
215  {
216  QString partName=token();
217  if (partName=="torso")
218  skinPart=1;
219  else if (partName=="head")
220  skinPart=2;
221  else if (partName=="left_arm")
222  skinPart=3;
223  else if (partName=="right_arm")
224  skinPart=4;
225  else if (partName=="left_leg")
226  skinPart=5;
227  else if (partName=="right_leg")
228  skinPart=6;
229  skinLink=token().toInt();
230  tag=token();
231  }
232 
233  if (tag=="MESH")
234  {
235  QString name=token();
236  double a=token().toDouble();
237  double b=token().toDouble();
238  double c=token().toDouble();
239  double d=token().toDouble();
240  double e=token().toDouble();
241  double f=token().toDouble();
242  QString aux = QString("covers/%1").arg(name);
243  QString file = QString("%1").arg(config.findPath((const char *)(aux.toLatin1().data())).c_str());
244  if (file.isEmpty()) file=name;
245  printf("\n%s\n\n",file.toLatin1().data());
246  pMesh=new iCubMesh(file,a,b,c,d,e,f);
247  tag=token();
248  }
249 
250  if (tag=="FORCE_TORQUE")
251  {
252  ftPortName=token();
253  tag=token();
254  }
255 
256  switch (nType)
257  {
258  case BVH_ROOT:
259  {
260  int id=token().toInt();
261 
262  double Px=token().toDouble();
263  double Py=token().toDouble();
264  double Pz=token().toDouble();
265 
266  node=new BVHNodeROOT(sName,id,Px,Py,Pz,pMesh,mObjectsManager);
267  }
268  break;
269  case BVH_JOINT:
270  if (tag=="RPY_XYZ")
271  {
272  double Rz=token().toDouble();
273  double Ry=token().toDouble();
274  double Rx=token().toDouble();
275  double Px=token().toDouble();
276  double Py=token().toDouble();
277  double Pz=token().toDouble();
278 
279  node=new BVHNodeXYZ_RPY(sName,Px,Py,Pz,Rz,Ry,Rx);
280  }
281  else if (tag=="DH")
282  {
283  int a=token().toInt();
284  double b=token().toDouble();
285  double c=token().toDouble();
286  double d=token().toDouble();
287  double e=token().toDouble();
288  if (ftPortName=="")
289  {
290  node=new BVHNodeDH(sName,a,b,c,d,e,pMesh);
291  }
292  else
293  {
294  node=new BVHNodeForceTorque(sName,ftPortName,a,b,c,d,e,pMesh);
295  }
296  }
297  break;
298  case BVH_END:
299  if (tag=="EYE")
300  {
301  int n=token().toInt();
302  double a=token().toDouble();
303  double b=token().toDouble();
304  double c=token().toDouble();
305  double d=token().toDouble();
306  node=new BVHNodeEYE(sName,n,a,b,c,d,pMesh);
307  }
308  else if (tag=="DH")
309  {
310  int n=token().toInt();
311  double a=token().toDouble();
312  double b=token().toDouble();
313  double c=token().toDouble();
314  double d=token().toDouble();
315  node=new BVHNodeEND(sName,n,a,b,c,d,pMesh);
316  }
317  else if (tag=="LEFTHAND")
318  {
319  int a=token().toInt();
320  double b=token().toDouble();
321  double c=token().toDouble();
322  double d=token().toDouble();
323  double e=token().toDouble();
324  node=new BVHNodeLEFTHAND(sName,a,b,c,d,e,pMesh);
325  }
326  else if (tag=="RIGHTHAND")
327  {
328  int a=token().toInt();
329  double b=token().toDouble();
330  double c=token().toDouble();
331  double d=token().toDouble();
332  double e=token().toDouble();
333  node=new BVHNodeRIGHTHAND(sName,a,b,c,d,e,pMesh);
334  }
335  else if (tag=="INERTIAL")
336  {
337  double a=token().toDouble();
338  double b=token().toDouble();
339  double c=token().toDouble();
340  double d=token().toDouble();
341  node=new BVHNodeINERTIAL(sName,a,b,c,d,robot+"/head/inertials",pMesh);
342  }
343  break;
344  }
345 
346  if (skinPart)
347  {
348  mAB[skinPart][skinLink]=node;
349  }
350 
351  BVHNode* child=0;
352 
353  do
354  {
355  if((child=bvhReadNode(config)))
356  {
357  node->addChild(child);
358  }
359  } while (child);
360 
361  return node;
362 }
std::string GUI_NAME
void addChild(BVHNode *pChild)
Definition: bvhnode.h:231
BVHNode * bvhRead(yarp::os::ResourceFinder &config)
Definition: bvh.cpp:131
BVH(ObjectsManager *objManager=NULL)
Definition: bvh.cpp:30
QStringList tokens
Definition: bvh.h:179
ObjectsManager * mObjectsManager
Definition: bvh.h:189
yarp::os::BufferedPort< yarp::sig::Vector > portEncRightLeg
Definition: bvh.h:210
BVHNode *** mAB
Definition: bvh.h:191
QStringList partNames
Definition: bvh.h:65
int nJHead
Definition: bvh.h:202
yarp::os::BufferedPort< yarp::sig::Vector > portEncTorso
Definition: bvh.h:205
int tokenPos
Definition: bvh.h:180
int nJLeftLeg
Definition: bvh.h:202
yarp::os::BufferedPort< yarp::sig::Vector > portEncHead
Definition: bvh.h:206
QString inputFile
Definition: bvh.h:178
double * dEncBase
Definition: bvh.h:213
double * dEncLeftArm
Definition: bvh.h:213
int nJRightArm
Definition: bvh.h:202
int nJRightLeg
Definition: bvh.h:202
yarp::os::BufferedPort< yarp::sig::Vector > portEncRightArm
Definition: bvh.h:208
bool expect_token(const QString &expect)
Definition: bvh.cpp:121
double * dEncRightLeg
Definition: bvh.h:213
yarp::os::BufferedPort< yarp::sig::Vector > portEncBase
Definition: bvh.h:204
int nJLeftArm
Definition: bvh.h:202
bool Create(yarp::os::ResourceFinder &config)
Definition: bvh.cpp:48
yarp::os::BufferedPort< yarp::sig::Vector > portEncLeftLeg
Definition: bvh.h:209
yarp::os::BufferedPort< yarp::sig::Vector > portEncLeftArm
Definition: bvh.h:207
QStringList bvhChannelName
Definition: bvh.h:65
QString token()
Definition: bvh.cpp:111
BVHNode * pRoot
Definition: bvh.h:187
double * dEncLeftLeg
Definition: bvh.h:213
QString robot
Definition: bvh.h:195
double * dEncRightArm
Definition: bvh.h:213
double * dEncHead
Definition: bvh.h:213
double * dEncTorso
Definition: bvh.h:213
double dEncBuffer[59]
Definition: bvh.h:212
BVHNode * bvhReadNode(yarp::os::ResourceFinder &config)
Definition: bvh.cpp:183
int nJBase
Definition: bvh.h:202
int nJTorso
Definition: bvh.h:202
double dAvatarScale
Definition: bvh.h:184
void setAddressBook(BVHNode ***ab)
Definition: objectsthread.h:94
Definition: mesh.h:54
int n
FILE * file
Definition: main.cpp:81
QStringList tokenize(QString in, char sep)
Definition: mesh.h:36