3 #include <boost/algorithm/string.hpp> 5 #include <libxml/parser.h> 6 #include <libxml/xmlreader.h> 7 #include <libxml/xpath.h> 8 #include <libxml/xpathInternals.h> 9 #include <hrpModel/Config.h> 13 showScale(true), showCoM(false), showCoMonFloor(false), showCollision(true){
14 double r = 5.0, pan =
M_PI/4, tilt =
M_PI/16;
15 double cp = cos(pan), sp = sin(pan);
16 double ct = cos(tilt), st = sin(tilt);
21 T[ 0] =
R(0,0);
T[ 1] =
R(0,1);
T[ 2] =
R(0,2);
T[ 3] = r*cp*ct;
22 T[ 4] =
R(1,0);
T[ 5] =
R(1,1);
T[ 6] =
R(1,2);
T[ 7] = r*sp*ct;
23 T[ 8] =
R(2,0);
T[ 9] =
R(2,1);
T[10] =
R(2,2);
T[11] = r*st + 0.8;
24 T[12] = 0;
T[13] = 0;
T[14] = 0;
T[15] = 1.0;
28 m_timeStep(0.001), m_logTimeStep(0.01), m_totalTime(1.0), m_gravity(9.8), m_isEuler(true), m_kinematicsOnly(false), m_realTime(false)
35 xmlDocPtr doc = xmlParseFile(filename.c_str());
37 std::cerr <<
"unable to parse file(" << filename <<
")" << std::endl;
42 xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
43 if ( xpathCtx == NULL ) {
44 std::cerr <<
"unable to create new XPath context" << std::endl;
50 xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression(BAD_CAST
"/grxui/mode/item", xpathCtx);
51 if ( xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) ) {
52 std::cerr <<
"unable to find <mode>" << std::endl;
56 size = xpathObj->nodesetval->nodeNr;
58 for (
int i = 0;
i < size;
i++ ) {
59 xmlNodePtr node = xpathObj->nodesetval->nodeTab[
i];
61 if ( xmlStrEqual( xmlGetProp(node, (xmlChar *)
"class"), (xmlChar *)
"com.generalrobotix.ui.item.GrxSimulationItem") ) {
62 xmlNodePtr
cur_node = node->children;
64 if ( cur_node->type == XML_ELEMENT_NODE ) {
65 if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"integrate") ) {
66 m_kinematicsOnly = std::string((
char *)(xmlGetProp(cur_node, (xmlChar *)
"value"))) ==
"false";
68 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"viewsimulate") ) {
69 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"totalTime") ) {
70 m_totalTime = atof((
char *)(xmlGetProp(cur_node, (xmlChar *)
"value")));
71 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"timeStep") ) {
72 m_timeStep = atof((
char *)(xmlGetProp(cur_node, (xmlChar *)
"value")));
73 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"realTime") ) {
74 m_realTime = std::string((
char *)(xmlGetProp(cur_node, (xmlChar *)
"value"))) ==
"true";
75 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"gravity") ) {
76 m_gravity = atof((
char *)(xmlGetProp(cur_node, (xmlChar *)
"value")));
77 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"method") ) {
78 m_isEuler = std::string((
char *)(xmlGetProp(cur_node, (xmlChar *)
"value"))) == std::string(
"EULER");
81 std::cerr <<
"Unknown tag : " << cur_node->name <<
" " 82 <<
"name=" << xmlGetProp(cur_node, (xmlChar *)
"name")
83 <<
"value=" << xmlGetProp(cur_node, (xmlChar *)
"value") << std::endl;
87 cur_node = cur_node->next;
89 }
else if ( xmlStrEqual( xmlGetProp(node, (xmlChar *)
"class"), (xmlChar *)
"com.generalrobotix.ui.item.GrxRTSItem") ) {
90 xmlNodePtr
cur_node = node->children;
92 if ( cur_node->type == XML_ELEMENT_NODE ) {
93 std::string
name = (
char *)xmlGetProp(cur_node, (xmlChar *)
"name");
94 std::string
value = (
char *)xmlGetProp(cur_node, (xmlChar *)
"value");
97 if (name ==
"connection"){
98 int pos = value.find(
':');
100 std::cerr <<
"can't find a separator(:) in " 101 << value << std::endl;
103 std::string p1 = value.substr(0,pos);
104 std::string p2 = value.substr(pos+1);
108 int pos = name.find(
'.');
110 std::cerr <<
"unknown property name:" << name
113 std::string comp = name.substr(0,pos);
114 std::string cat = name.substr(pos+1);
116 if (cat ==
"factory"){
118 int pos = value.find_last_of(
"/");
119 rtc.
name = value.substr(pos+1);
120 }
else if (cat ==
"period") {
121 rtc.
period = atof(value.c_str());
128 cur_node = cur_node->next;
130 }
else if ( xmlStrEqual( xmlGetProp(node, (xmlChar *)
"class"), (xmlChar *)
"com.generalrobotix.ui.item.GrxModelItem") ) {
132 std::string
path = (
char *)xmlGetProp(node, (xmlChar *)
"url");
133 if ( path.find(
"$(CURRENT_DIR)") != std::string::npos ) {
134 if (filename.find_last_of(
"/") != std::string::npos){
135 path.replace(path.find(
"$(CURRENT_DIR)"),14,
136 filename.substr(0, filename.find_last_of(
"/")));
138 path.replace(path.find(
"$(CURRENT_DIR)"),15,
"");
141 char buf[MAXPATHLEN];
142 path = std::string(getcwd(buf, MAXPATHLEN))+
"/"+path;
145 if ( path.find(
"$(PROJECT_DIR)") != std::string::npos ) {
146 std::string shdir = OPENHRP_SHARE_DIR;
147 std::string pjdir = shdir +
"/sample/project";
148 path.replace(path.find(
"$(PROJECT_DIR)"),14, pjdir);
151 m.
url = std::string(
"file://")+path;
152 xmlNodePtr
cur_node = node->children;
154 if ( cur_node->type == XML_ELEMENT_NODE ) {
155 if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"isRobot") ) {
156 if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"value"),(xmlChar *)
"true")) {
159 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"controlTime") ) {
161 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"rtcName") ) {
162 m.
rtcName = (
char *)xmlGetProp(cur_node, (xmlChar *)
"value");
163 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"inport") ) {
164 m.
inports.push_back((
char *)xmlGetProp(cur_node, (xmlChar *)
"value"));
165 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"outport") ) {
166 m.
outports.push_back((
char *)xmlGetProp(cur_node, (xmlChar *)
"value"));
167 }
else if ( std::string((
char *)xmlGetProp(cur_node, (xmlChar *)
"name")).rfind(
".angle") != std::string::npos ) {
168 std::string
name = std::string((
char *)xmlGetProp(cur_node, (xmlChar *)
"name"));
169 name.erase(name.rfind(
".angle"));
170 m.
joint[name].angle = atof((
char *)xmlGetProp(cur_node, (xmlChar *)
"value"));
171 }
else if ( std::string((
char *)xmlGetProp(cur_node, (xmlChar *)
"name")).rfind(
".mode") != std::string::npos ) {
172 std::string
name = std::string((
char *)xmlGetProp(cur_node, (xmlChar *)
"name"));
173 name.erase(name.rfind(
".mode"));
174 m.
joint[name].isHighGain = xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"value"), (xmlChar *)
"HighGain");
175 }
else if ( std::string((
char *)xmlGetProp(cur_node, (xmlChar *)
"name")).rfind(
".NumOfAABB") != std::string::npos ) {
176 std::string
name = std::string((
char *)xmlGetProp(cur_node, (xmlChar *)
"name"));
177 name.erase(name.rfind(
".NumOfAABB"));
178 m.
joint[name].NumOfAABB = atof((
char *)(xmlGetProp(cur_node, (xmlChar *)
"value")));
179 }
else if ( std::string((
char *)xmlGetProp(cur_node, (xmlChar *)
"name")).rfind(
".collisionShape") != std::string::npos ) {
180 std::string
name = std::string((
char *)xmlGetProp(cur_node, (xmlChar *)
"name"));
181 name.erase(name.rfind(
".collisionShape"));
182 m.
joint[name].collisionShape = (
char *)(xmlGetProp(cur_node, (xmlChar *)
"value"));
183 boost::trim(m.
joint[name].collisionShape);
184 }
else if ( std::string((
char *)xmlGetProp(cur_node, (xmlChar *)
"name")).rfind(
".translation") != std::string::npos ) {
185 std::string
name = std::string((
char *)xmlGetProp(cur_node, (xmlChar *)
"name"));
186 name.erase(name.rfind(
".translation"));
188 sscanf(((
char *)xmlGetProp(cur_node, (xmlChar *)
"value")),
"%f %f %f", &x, &y, &z);
189 m.
joint[name].translation[0] =
x; m.
joint[name].translation[1] =
y; m.
joint[name].translation[2] =
z;
190 }
else if ( std::string((
char *)xmlGetProp(cur_node, (xmlChar *)
"name")).rfind(
".rotation") != std::string::npos ) {
191 std::string
name = std::string((
char *)xmlGetProp(cur_node, (xmlChar *)
"name"));
192 name.erase(name.rfind(
".rotation"));
194 sscanf(((
char *)xmlGetProp(cur_node, (xmlChar *)
"value")),
"%f %f %f %f", &x, &y, &z, &w);
196 }
else if ( std::string((
char *)xmlGetProp(cur_node, (xmlChar *)
"name")).rfind(
".velocity") != std::string::npos ) {
197 std::string
name = std::string((
char *)xmlGetProp(cur_node, (xmlChar *)
"name"));
198 name.erase(name.rfind(
".velocity"));
200 sscanf(((
char *)xmlGetProp(cur_node, (xmlChar *)
"value")),
"%f %f %f", &x, &y, &z);
201 m.
joint[name].linearVelocity <<
x,
y,
z;
202 }
else if ( std::string((
char *)xmlGetProp(cur_node, (xmlChar *)
"name")).rfind(
".angularVelocity") != std::string::npos ) {
203 std::string
name = std::string((
char *)xmlGetProp(cur_node, (xmlChar *)
"name"));
204 name.erase(name.rfind(
".angularVelocity"));
206 sscanf(((
char *)xmlGetProp(cur_node, (xmlChar *)
"value")),
"%f %f %f", &x, &y, &z);
207 m.
joint[name].angularVelocity <<
x,
y,
z;
210 std::cerr <<
"Unknown tag : " << cur_node->name <<
" " 211 <<
"name=" << xmlGetProp(cur_node, (xmlChar *)
"name") <<
" " 212 <<
"value=" << xmlGetProp(cur_node, (xmlChar *)
"value") << std::endl;
216 cur_node = cur_node->next;
218 std::string
n = std::string((
char *)xmlGetProp(node, (xmlChar *)
"name"));
220 }
else if ( xmlStrEqual( xmlGetProp(node, (xmlChar *)
"class"), (xmlChar *)
"com.generalrobotix.ui.item.GrxWorldStateItem") ) {
221 xmlNodePtr
cur_node = node->children;
223 if ( cur_node->type == XML_ELEMENT_NODE ) {
224 if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"logTimeStep") ) {
225 m_logTimeStep = atof((
char *)(xmlGetProp(cur_node, (xmlChar *)
"value")));
226 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"timeStep") ) {
227 m_timeStep = atof((
char *)(xmlGetProp(cur_node, (xmlChar *)
"value")));
228 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"totalTime") ) {
229 m_totalTime = atof((
char *)(xmlGetProp(cur_node, (xmlChar *)
"value")));
230 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"method") ) {
231 m_isEuler = std::string((
char *)(xmlGetProp(cur_node, (xmlChar *)
"value"))) == std::string(
"EULER");
234 cur_node = cur_node->next;
236 }
else if ( xmlStrEqual ( xmlGetProp(node, (xmlChar *)
"class"), (xmlChar *)
"com.generalrobotix.ui.item.GrxCollisionPairItem") ) {
238 xmlNodePtr
cur_node = node->children;
240 if ( cur_node->type == XML_ELEMENT_NODE ) {
241 if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"objectName1") ) {
242 c.
objectName1 = (
char *)(xmlGetProp(cur_node, (xmlChar *)
"value"));
243 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"objectName2") ) {
244 c.
objectName2 = (
char *)(xmlGetProp(cur_node, (xmlChar *)
"value"));
245 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"jointName1") ) {
246 c.
jointName1 = (
char *)(xmlGetProp(cur_node, (xmlChar *)
"value"));
247 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"jointName2") ) {
248 c.
jointName2 = (
char *)(xmlGetProp(cur_node, (xmlChar *)
"value"));
249 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"slidingFriction") ) {
250 c.
slidingFriction = atof((
char *)(xmlGetProp(cur_node, (xmlChar *)
"value")));
251 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"staticFriction") ) {
252 c.
staticFriction = atof((
char *)(xmlGetProp(cur_node, (xmlChar *)
"value")));
253 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"cullingThresh") ) {
254 c.
cullingThresh = atof((
char *)(xmlGetProp(cur_node, (xmlChar *)
"value")));
255 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"Restitution") ) {
256 c.
restitution = atof((
char *)(xmlGetProp(cur_node, (xmlChar *)
"value")));
257 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"sprintDamperModel") ) {
261 std::cerr <<
"Unknown tag : " << cur_node->name <<
" " 262 <<
", name=" << xmlGetProp(cur_node, (xmlChar *)
"name")
263 <<
", value=" << xmlGetProp(cur_node, (xmlChar *)
"value") << std::endl;
267 cur_node = cur_node->next;
270 }
else if ( xmlStrEqual ( xmlGetProp(node, (xmlChar *)
"class"), (xmlChar *)
"com.generalrobotix.ui.item.GrxExtraJointItem") ) {
272 xmlNodePtr
cur_node = node->children;
274 if ( cur_node->type == XML_ELEMENT_NODE ) {
275 if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"object1Name") ) {
276 c.
object1Name = (
char *)(xmlGetProp(cur_node, (xmlChar *)
"value"));
277 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"object2Name") ) {
278 c.
object2Name = (
char *)(xmlGetProp(cur_node, (xmlChar *)
"value"));
279 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"link1Name") ) {
280 c.
link1Name = (
char *)(xmlGetProp(cur_node, (xmlChar *)
"value"));
281 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"link2Name") ) {
282 c.
link2Name = (
char *)(xmlGetProp(cur_node, (xmlChar *)
"value"));
283 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"jointType") ) {
284 c.
jointType = (
char *)(xmlGetProp(cur_node, (xmlChar *)
"value"));
285 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"jointAxis") ) {
287 sscanf(((
char *)xmlGetProp(cur_node, (xmlChar *)
"value")),
"%f %f %f", &x, &y, &z);
289 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"link1LocalPos") ) {
291 sscanf(((
char *)xmlGetProp(cur_node, (xmlChar *)
"value")),
"%f %f %f", &x, &y, &z);
293 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"link2LocalPos") ) {
295 sscanf(((
char *)xmlGetProp(cur_node, (xmlChar *)
"value")),
"%f %f %f", &x, &y, &z);
299 std::cerr <<
"Unknown tag : " << cur_node->name <<
" " 300 <<
", name=" << xmlGetProp(cur_node, (xmlChar *)
"name")
301 <<
", value=" << xmlGetProp(cur_node, (xmlChar *)
"value") << std::endl;
305 cur_node = cur_node->next;
312 xmlXPathFreeObject(xpathObj);
313 xmlXPathFreeContext(xpathCtx);
317 xmlXPathContextPtr xpathCtx = xmlXPathNewContext(doc);
318 if ( xpathCtx == NULL ) {
319 std::cerr <<
"unable to create new XPath context" << std::endl;
324 xmlXPathObjectPtr xpathObj = xmlXPathEvalExpression(BAD_CAST
"/grxui/mode/view", xpathCtx);
325 if ( xmlXPathNodeSetIsEmpty(xpathObj->nodesetval) ) {
326 std::cerr <<
"unable to find <mode>" << std::endl;
330 size = xpathObj->nodesetval->nodeNr;
332 for (
int i = 0;
i < size;
i++ ) {
333 xmlNodePtr node = xpathObj->nodesetval->nodeTab[
i];
335 if ( xmlStrEqual( xmlGetProp(node, (xmlChar *)
"class"), (xmlChar *)
"com.generalrobotix.ui.view.GrxRobotHardwareClientView") ) {
336 xmlNodePtr
cur_node = node->children;
338 if ( cur_node->type == XML_ELEMENT_NODE ) {
339 if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"robotHost") ) {
341 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"robotPort") ) {
342 m_rhview.
port = atoi((
char *)(xmlGetProp(cur_node, (xmlChar *)
"value")));
343 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"interval") ) {
344 m_rhview.
interval = atoi((
char *)(xmlGetProp(cur_node, (xmlChar *)
"value")));
345 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"stateProvider") ) {
347 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"StateHolderRTC") ) {
351 cur_node = cur_node->next;
353 }
else if ( xmlStrEqual( xmlGetProp(node, (xmlChar *)
"class"), (xmlChar *)
"com.generalrobotix.ui.view.Grx3DView") ) {
354 xmlNodePtr
cur_node = node->children;
356 if ( cur_node->type == XML_ELEMENT_NODE ) {
357 if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"showScale") ) {
358 m_3dview.
showScale = std::string((
char *)(xmlGetProp(cur_node, (xmlChar *)
"value"))) ==
"true";
359 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"showCoM") ) {
360 m_3dview.
showCoM = std::string((
char *)(xmlGetProp(cur_node, (xmlChar *)
"value"))) ==
"true";
361 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"showCoMonFloor") ) {
363 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"showCollision") ) {
365 }
else if ( xmlStrEqual(xmlGetProp(cur_node, (xmlChar *)
"name"),(xmlChar *)
"eyeHomePosition") ) {
366 sscanf(((
char *)xmlGetProp(cur_node, (xmlChar *)
"value")),
"%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf ",
372 cur_node = cur_node->next;
376 xmlXPathFreeObject(xpathObj);
377 xmlXPathFreeContext(xpathCtx);
std::map< std::string, rtc > components
bool parse(const std::string &filename)
png_infop png_charpp name
std::vector< CollisionPairItem > m_collisionPairs
std::vector< std::string > outports
Matrix33 rotFromRpy(const Vector3 &rpy)
std::string RobotHardwareName
HRP_UTIL_EXPORT void calcRodrigues(Matrix44 &out_R, const Vector3 &axis, double q)
std::map< std::string, ModelItem > m_models
std::string sprintDamperModel
RobotHardwareClientView m_rhview
std::vector< ExtraJointItem > m_extraJoints
std::vector< std::string > inports
std::vector< std::pair< std::string, std::string > > configuration
std::vector< std::pair< std::string, std::string > > connections
std::map< std::string, JointItem > joint
std::string StateHolderName