Privacy
An open-source, flexible 3D physical simulation framework
utils.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011, 2012, DFKI GmbH Robotics Innovation Center
3  *
4  * This file is part of the MARS simulation framework.
5  *
6  * MARS is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation, either version 3
9  * of the License, or (at your option) any later version.
10  *
11  * MARS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with MARS. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 /*
22  * utils.cpp
23  * Simulator
24  *
25  * Created by Malte Roemmermann
26  *
27  */
28 
29 #include "utils.h"
30 
31 #include <cstdio>
32 #include <cmath>
33 #include <sstream>
34 #include <iostream>
35 #include <cassert>
36 
37 namespace mars {
38  namespace interfaces {
39 
40  using namespace std;
41  using namespace mars::utils;
42 
43  void getAbsFromRel(const NodeData &node1, NodeData* node2){
44  node2->pos = node1.pos + node1.rot * node2->pos;
45  node2->rot = node1.rot * node2->rot;
46  }
47 
48  void getRelFromAbs(const NodeData &node1, NodeData *node2){
49  node2->pos = node1.rot.inverse() * (node2->pos-node1.pos);
50  node2->rot = node1.rot.inverse()*node2->rot;
51  }
52 
53  /* keep this in sync with the JointTypes struct. */
54  static const char* sJointTypeStrings[NUMBER_OF_JOINT_TYPES] = {
55  "undefined",
56  "hinge",
57  "hinge2",
58  "slider",
59  "ball",
60  "universal",
61  "fixed",
62  "istruct-spine"
63  };
64 
65  const char* getJointTypeString(JointType type)
66  {
67  if (type >= NUMBER_OF_JOINT_TYPES)
68  {
69  std::cerr << "getJointTypeString(JointType id): invalid joint type id " << (int)type << std::endl;
70  //throw exception here?
71 
72  return NULL;
73  }
74  return sJointTypeStrings[type];
75  }
76 
77  JointType getJointType(const std::string& text)
78  {
79  assert(sizeof(sJointTypeStrings) / sizeof(char*) == NUMBER_OF_JOINT_TYPES);
80 
81  //keep this in sync with the correct ids (from mars_core/src/MARSDefs.h)
82  for (int i = 0; i < NUMBER_OF_JOINT_TYPES; ++i)
83  {
84  if (text == sJointTypeStrings[i])
85  {
86  return (JointType)i;
87  }
88  }
89 
90  //this type string might also be the type-id encoded in a decimal string.
91  std::istringstream iss(text);
92  int numberFromString;
93  if ( !(iss >> numberFromString).fail() )
94  {
95  //conversion to integer was ok but is this a valid type id?
96  if (numberFromString > 0 && numberFromString < NUMBER_OF_JOINT_TYPES)
97  {
98  //yes, it is.
99  return (JointType)numberFromString;
100  }
101  }
102 
103  //string not found and conversion to integer not successful.
104  std::cerr << __FILE__": Could not get joint type from string \""
105  << text << "\"." << std::endl;
106  return JOINT_TYPE_UNDEFINED;
107  }
108 
109  } // end of namespace interfaces
110 
111 } // end of namespace mars
const char * getJointTypeString(JointType type)
Return a string for the joint-type.
Definition: utils.cpp:65
utils::Vector pos
The position of the node.
Definition: NodeData.h:192
utils::Quaternion rot
This quaternion describes the orientation of the node.
Definition: NodeData.h:205
NodeData is a struct to exchange node information between the GUI and the simulation.
Definition: NodeData.h:52
void getAbsFromRel(const NodeData &node1, NodeData *node2)
get the absolute position of a new node by its relative position
Definition: utils.cpp:43
void getRelFromAbs(const NodeData &node1, NodeData *node2)
Definition: utils.cpp:48
Copyright 2012, DFKI GmbH Robotics Innovation Center.
JointType getJointType(const std::string &text)
Helper function to get type-id from string in scene file.
Definition: utils.cpp:77
static const char * sJointTypeStrings[NUMBER_OF_JOINT_TYPES]
Definition: utils.cpp:54