Privacy
An open-source, flexible 3D physical simulation framework
primitives.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2011, 2012, 2014, 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 #include "primitives.h"
22 
23 #include <mars/data_broker/DataBrokerInterface.h>
24 
25 #include <mars/interfaces/sim/EntityManagerInterface.h>
26 #include <mars/interfaces/sim/SimulatorInterface.h>
27 #include <mars/interfaces/sim/NodeManagerInterface.h>
28 #include <mars/interfaces/sim/JointManagerInterface.h>
29 #include <mars/interfaces/graphics/GraphicsManagerInterface.h>
30 #include <mars/sim/SimEntity.h>
31 #include <mars/entity_generation/entity_factory/EntityFactoryManager.h>
32 #include <mars/interfaces/Logging.hpp>
33 
34 #include <mars/utils/misc.h>
35 #include <mars/utils/mathUtils.h>
36 
37 namespace mars {
38  namespace smurf {
39 
40  using namespace std;
41  using namespace interfaces;
42  using namespace utils;
43  using namespace configmaps;
44 
45  PRIMITIVES::PRIMITIVES(lib_manager::LibManager *theManager): MarsPluginTemplate(theManager, "PRIMITIVES"),
46  entity_generation::EntityFactoryInterface("primitive") {
49  "mars_entity_factory");
50  factoryManager->registerFactory("primitive", this);
51  theManager->releaseLibrary("mars_entity_factory");
52  }
53 
55  }
56 
59 
60  reset();
61  }
62 
64  primitivename = "";
65  entity = NULL;
66  }
67 
68  void PRIMITIVES::update(sReal time_ms) {
69  }
70 
71  void PRIMITIVES::handleURI(ConfigMap *map, std::string uri) {
73  handleURIs(&map2);
74  map->append(map2);
75  }
76 
78  if (map->find("URI") != map->end()) {
79  std::string file = (std::string) (*map)["URI"][0];
80  if (!file.empty() && file[0] != '/') {
81  file = tmpPath + file;
82  }
83  handleURI(map, file);
84  }
85  if (map->find("URIs") != map->end()) {
86  ConfigVector::iterator vIt = (*map)["URIs"].begin();
87  for (; vIt != (*map)["URIs"].end(); ++vIt) {
88  std::string file = (std::string) (*vIt);
89  if (!file.empty() && file[0] != '/') {
90  file = tmpPath + file;
91  }
92  handleURI(map, file);
93  }
94  }
95  }
96 
98  reset();
99  configmaps::ConfigMap entityconfig;
100  entityconfig = config;
101  std::string path = (std::string)entityconfig["path"];
102 
103  // node mapping and name checking
104  std::string primitivename = (std::string)entityconfig["name"];
105  if (primitivename == "")
106  primitivename = "unnamed_primitive";
107  entityconfig["name"] = primitivename;
108  entity = new sim::SimEntity(control, entityconfig);
109  // loadNode
111  if (entityconfig.hasKey("geometry") && entityconfig["geometry"].hasKey("type")) {
112  std::string type = entityconfig["geometry"]["type"];
113  map["filename"] = "PRIMITIVE";
114  if (type == "plane") {
115  map["name"] = primitivename;
116  map["physicmode"] = "plane";
117  map["origname"] = "plane";
118  map["extend"]["x"] = 10.;
119  map["extend"]["y"] = 10.;
120  } else
121  if (type == "box") {
122  map["name"] = primitivename;
123  map["physicmode"] = "box";
124  map["origname"] = "box";
125  map["extend"]["x"] = 1.;
126  map["extend"]["y"] = 1.;
127  map["extend"]["z"] = 1.;
128  map["position"]["z"] = 0.5;
129  map["movable"] = true;
130  } else
131  if (type == "sphere") {
132  map["name"] = primitivename;
133  map["physicmode"] = "sphere";
134  map["visualType"] = "sphere";
135  map["extend"]["x"] = 0.5;
136  map["extend"]["y"] = 1.;
137  map["extend"]["z"] = 1.;
138  map["position"]["z"] = 0.5;
139  map["movable"] = true;
140  } else
141  if (type == "cylinder") {
142 
143  } else
144  if (type == "capsule") {
145 
146  }
147  }
148  map.append(entityconfig);
149 
150  NodeData node;
151  int valid = node.fromConfigMap(&map, tmpPath, control->loadCenter);
152  if (!valid)
153  return NULL;
154 
155  // loadMaterial
156  configmaps::ConfigMap material;
157  material["name"] = "defaultGrey";
158  material["diffuseColor"]["a"] = 1.0;
159  material["diffuseColor"]["r"] = 0.33;
160  material["diffuseColor"]["g"] = 0.39;
161  material["diffuseColor"]["b"] = 0.5;
162  material["specularColor"]["a"] = 1.0;
163  material["specularColor"]["r"] = 0.5;
164  material["specularColor"]["g"] = 0.5;
165  material["specularColor"]["b"] = 0.5;
166  material["ambientColor"]["a"] = 1.0;
167  material["ambientColor"]["r"] = 0.53;
168  material["ambientColor"]["g"] = 0.59;
169  material["ambientColor"]["b"] = 0.7;
170  material["shininess"] = 80.0;
171  if (entityconfig.hasKey("material")) {
172  material.append(entityconfig["material"]);
173  }
174  node.material.fromConfigMap(&material, tmpPath);
175 
176  // check if meshes are stored as `.stl` file
177  string suffix = getFilenameSuffix(node.filename);
178  if (suffix == ".stl" || suffix == ".STL") {
179  // add an additional rotation of -90.0 degree due to wrong definition
180  // of which direction is up within .stl (for .stl -Y is up and in MARS
181  // Z is up)
182  node.visual_offset_rot *= eulerToQuaternion(Vector(-90.0, 0.0, 0.0));
183  }
184  std::string parentname;
185  if (entityconfig.hasKey("parent")) {
186  parentname = (std::string)entityconfig["parent"];
187  if (parentname == "world") {
188  node.movable = false;
189  node.groupID = 666;
190  }
191  }
192  NodeId oldId = node.index;
193  NodeId newId = control->nodes->addNode(&node);
194  if (!newId) {
195  LOG_ERROR("addNode returned 0");
196  return 0;
197  }
198  entity->addNode(node.index, node.name);
199 
200  /*if(control->loadCenter->getMappedSceneByName(robotname) == 0) {
201  control->loadCenter->setMappedSceneName(robotname);
202  }
203  mapIndex = control->loadCenter->getMappedSceneByName(robotname);*/
204  return entity;
205  }
206 
207  } // end of namespace smurf
208 } // end of namespace mars
209 
NodeManagerInterface * nodes
Definition: ControlCenter.h:98
std::string getFilenameSuffix(const std::string &file)
given a filename "foobar.baz" this will return ".baz"
Definition: misc.cpp:144
bool fromConfigMap(configmaps::ConfigMap *config, std::string filenamePrefix)
NodeData is a struct to exchange node information between the GUI and the simulation.
Definition: NodeData.h:52
std::string filename
The filename of the node to load the visual representation from.
Definition: NodeData.h:159
virtual void switchPluginUpdateMode(int mode, PluginInterface *pl)=0
static ConfigMap fromYamlFile(const std::string &filename, bool loadURI=false)
Definition: ConfigMap.cpp:54
virtual NodeId addNode(NodeData *nodeS, bool reload=false, bool loadGraphics=true)=0
Add a node to the node pool of the simulation.
virtual sim::SimEntity * createEntity(const configmaps::ConfigMap &config)
Definition: primitives.cpp:97
int groupID
Several nodes can be grouped by setting the same groupID.
Definition: NodeData.h:166
void update(mars::interfaces::sReal time_ms)
Definition: primitives.cpp:68
utils::Quaternion visual_offset_rot
In the same way as the visual_offset_pos, this quaternion can be used to differentiate the orientatio...
Definition: NodeData.h:313
Quaternion eulerToQuaternion(const Vector &euler_v)
Definition: mathUtils.cpp:133
sim::SimEntity * entity
Definition: primitives.h:61
virtual void registerFactory(const std::string type, EntityFactoryInterface *factory)
double sReal
Definition: MARSDefs.h:49
void handleURI(configmaps::ConfigMap *map, std::string uri)
Definition: primitives.cpp:71
bool fromConfigMap(configmaps::ConfigMap *config, std::string filenamePrefix, LoadCenter *loadCenter=0)
Definition: NodeData.cpp:98
T * acquireLibraryAs(const std::string &libName, bool load=false)
Definition: LibManager.hpp:113
bool hasKey(std::string key)
Definition: ConfigMap.cpp:131
Copyright 2012, DFKI GmbH Robotics Innovation Center.
Eigen::Matrix< double, 3, 1, Eigen::DontAlign > Vector
Definition: Vector.h:43
std::string primitivename
Definition: primitives.h:60
PRIMITIVES(lib_manager::LibManager *theManager)
Definition: primitives.cpp:45
CREATE_LIB(mars::smurf::PRIMITIVES)
DESTROY_LIB(mars::smurf::PRIMITIVES)
bool movable
This boolean variable defines if the physical representation is fixed in the world or if it&#39;s a movab...
Definition: NodeData.h:212
void addNode(unsigned long nodeId, const std::string &name)
Definition: SimEntity.cpp:74
std::string name
The name of the node.
Definition: NodeData.h:143
MaterialData material
The material struct defines the visual material of the node.
Definition: NodeData.h:262
unsigned long NodeId
Definition: MARSDefs.h:45
void handleURIs(configmaps::ConfigMap *map)
Definition: primitives.cpp:77
unsigned long index
The unique index of the node.
Definition: NodeData.h:172
#define LOG_ERROR(...)
Definition: Logging.hpp:21
ErrorNumber releaseLibrary(const std::string &libName)
Releases a previously acquired library.
Definition: LibManager.cpp:311