DesignerTrajectoryHolder.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2015-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarXGuiPlugins::RobotTrajectoryDesigner::Manager
19  * @author Luca Quaer
20  * @date 2018
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
26 
27 #include <VirtualRobot/Robot.h>
28 
29 using namespace armarx;
30 
31 ////////////////////////////////////////////////////////////////////////////////////////////////////
32 /// Constructor(s)
33 ////////////////////////////////////////////////////////////////////////////////////////////////////
34 ///
35 
37  environment(environment)
38 {
39 }
40 
41 ////////////////////////////////////////////////////////////////////////////////////////////////////
42 /// Public functions
43 ////////////////////////////////////////////////////////////////////////////////////////////////////
44 ///
45 bool
47 {
48  // check, if the RobotNodeSet-name already exists as key
49  //if (designerTrajectories.find(rnsName) == designerTrajectories.end())
50  if (!rnsExists(rnsName) && !rnsIsPartOfExistingRns(rnsName))
51  {
52  // RobotNodeSet-name does not exist as key
53  designerTrajectories.insert(std::make_pair(
54  rnsName,
55  DesignerTrajectoryManagerPtr(new DesignerTrajectoryManager(rnsName, environment))));
56  // environment->getRobot()->getRobotNodeSet(rnsName)))));
57  return true;
58  }
59  else
60  {
61  return false;
62  }
63 }
64 
65 bool
67 {
68  // check, if the RobotNodeSet-name already exists as key
69  //if (designerTrajectories.find(rnsName) != designerTrajectories.end())
70  if (rnsExists(rnsName))
71  {
72  // RobotNodeSetPtr does exist as key
73  int numberOfErasedElements = designerTrajectories.erase(rnsName);
74 
75  if (numberOfErasedElements > 0)
76  {
77  return true;
78  }
79  else
80  {
81  return false;
82  }
83  }
84  else
85  {
86  return false;
87  }
88 }
89 
92 {
93  if (rnsExists(rnsName))
94  {
95  return designerTrajectories.at(rnsName);
96  }
97  else
98  {
99  throw InvalidArgumentException("RNS name does not exist in map");
100  }
101 }
102 
103 bool
104 DesignerTrajectoryHolder::import(DesignerTrajectoryPtr designerTrajectory, bool force = true)
105 {
106  VirtualRobot::RobotNodeSetPtr tmpRns = designerTrajectory->getRns();
107 
109  new DesignerTrajectoryManager(tmpRns->getName(), environment));
110  tmpDTM.get()->import(designerTrajectory);
111 
112  // check, if the the RobotNodeSet-name already exists as key
113  if (!rnsExists(tmpRns->getName()))
114  {
115  // RobotNodeSet-name does not exist as key
116  designerTrajectories.insert(std::make_pair(tmpRns->getName(), tmpDTM));
117 
118  return true; // RobotNodeSet-name inserted
119  }
120  else if (force)
121  {
122  designerTrajectories[tmpRns->getName()] = tmpDTM;
123  return true; // RobotNodeSet-name inserted because forced
124  }
125  else
126  {
127  return false; // RobotNodeSet-name not inserted because it already exists
128  }
129 }
130 
131 bool
133 {
134  // check, if the RobotNodeSet-name already exists as key
135  if (designerTrajectories.find(rnsName) != designerTrajectories.end())
136  {
137  // rns found
138  return true;
139  }
140  else
141  {
142  // rns not found
143  return false;
144  }
145 }
146 
147 bool
149 {
150  // save all existing RobotNodeSets as vector<vector<string>>
151  // one RobotNodeSet is representet as vector of its RobotNodes-name
152  std::vector<std::vector<std::string>> rnssNodeNames;
153 
154  for (std::map<std::string, DesignerTrajectoryManagerPtr>::iterator it =
155  designerTrajectories.begin();
156  it != designerTrajectories.end();
157  ++it)
158  {
159  VirtualRobot::RobotNodeSetPtr rnsptr = environment->getRobot()->getRobotNodeSet(it->first);
160  std::vector<std::string> tmpRnsNodesNames = rnsptr->getNodeNames();
161  rnssNodeNames.push_back(tmpRnsNodesNames);
162  }
163 
164  // get nodes of given rns
165  std::vector<std::string> givenRnsNodeNames =
166  environment->getRobot()->getRobotNodeSet(rnsName)->getNodeNames();
167 
168  // check if givenRnsNodeNames is a subset of any rnsNodesNames
169  for (std::vector<std::string> rnsNodeNames : rnssNodeNames)
170  {
171  std::vector<std::string> intersection;
172 
173  sort(rnsNodeNames.begin(), rnsNodeNames.end());
174  sort(givenRnsNodeNames.begin(), givenRnsNodeNames.end());
175 
176  std::set_intersection(rnsNodeNames.begin(),
177  rnsNodeNames.end(),
178  givenRnsNodeNames.begin(),
179  givenRnsNodeNames.end(),
180  std::back_inserter(intersection));
181 
182  if (intersection.size() != 0)
183  {
184  return true;
185  }
186  }
187 
188  return false;
189 }
190 
191 bool
192 DesignerTrajectoryHolder::isInCollision(std::string activeColModelName,
193  std::vector<std::string> bodyColModelsNames,
194  unsigned int fps)
195 {
196  TrajectoryPtr t = mergeTrajectories(fps);
197  VirtualRobot::RobotPtr cdRob = environment->getCDRobot();
198 
199  ////////////////////////////////////////////////////////////////////////////////////////////////
200  // Setup CDManager
201  // create new CDManager
202  VirtualRobot::CDManagerPtr cdm(new VirtualRobot::CDManager());
203 
204  // create sosActiveColModel from activeColModelName
205  if (cdRob->getRobotNodeSet(activeColModelName) == nullptr)
206  {
207  return false; // cannot find collision model
208  }
209 
210  VirtualRobot::SceneObjectSetPtr sosActiveColModel(new VirtualRobot::SceneObjectSet());
211  sosActiveColModel->addSceneObjects(cdRob->getRobotNodeSet(activeColModelName));
212 
213  // add sosActiveColModel to CDManager
214  cdm->addCollisionModel(sosActiveColModel);
215 
216  // create bodyColModels
217  for (std::string bodyColModelName : bodyColModelsNames)
218  {
219  // create sosBodyColModel from bodyColModelName
220  if (cdRob->getRobotNodeSet(bodyColModelName) == nullptr)
221  {
222  continue; // cannot find collision model
223  }
224 
225  VirtualRobot::SceneObjectSetPtr sosBodyColModel(new VirtualRobot::SceneObjectSet());
226  sosBodyColModel->addSceneObjects(cdRob->getRobotNodeSet(bodyColModelName));
227 
228  // add sosBodyColModel to CDManager
229  cdm->addCollisionModel(sosBodyColModel);
230  }
231 
232  ////////////////////////////////////////////////////////////////////////////////////////////////
233  // Collision checks
234 
235  // increment for timesteps
236  double increment = 1.0 / fps;
237  double time = 0.0;
238 
239  while (t->dataExists(time))
240  {
241  ////////////////////////////////////////////////////////////////////////////////////////////
242  // Create RobotConfig
243  std::vector<std::string> dimNames = t->getDimensionNames();
244  std::vector<double> jointValuesDouble = t->getStates(time, 0);
245  std::vector<float> jointValues(jointValuesDouble.begin(), jointValuesDouble.end());
246 
247  VirtualRobot::RobotConfigPtr robConf(new VirtualRobot::RobotConfig(
248  cdRob, "config at t=" + std::to_string(time), dimNames, jointValues));
249 
250  ////////////////////////////////////////////////////////////////////////////////////////////
251  // Set RobotConfig to CDRobot
252  cdRob->setConfig(robConf);
253 
254  ////////////////////////////////////////////////////////////////////////////////////////////
255  // Check for collision
256  if (cdm->isInCollision())
257  {
258  return true;
259  }
260 
261  ////////////////////////////////////////////////////////////////////////////////////////////
262  // increment loop variable
263  time = time + increment;
264  }
265 
266  return false;
267 }
268 
270 DesignerTrajectoryHolder::mergeTrajectories(unsigned int fps)
271 {
272  std::vector<TrajectoryPtr> trajectories;
273  std::vector<double> endTimes;
274  double maxEndTime = 0;
275  std::vector<double> timestamps;
276  TrajectoryPtr finalTrajectory(new Trajectory());
277 
278  for (std::map<std::string, DesignerTrajectoryManagerPtr>::iterator it =
279  designerTrajectories.begin();
280  it != designerTrajectories.end();
281  ++it)
282  {
283  if (!it->second->getDesignerTrajectory())
284  {
285  continue;
286  }
287  TrajectoryPtr traj = it->second->getDesignerTrajectory()->getFinalTrajectory();
288 
289  trajectories.push_back(traj);
290  double t = traj->getTimeLength();
291  if (t > maxEndTime)
292  {
293  maxEndTime = t;
294  }
295  endTimes.push_back(t);
296  }
297 
298  double inc = 1.0 / fps;
299  for (double d = 0.0; d < maxEndTime; d = d + inc)
300  {
301  timestamps.push_back(d);
302  }
303 
304  timestamps.push_back(maxEndTime);
305 
306  //get dimensionData of every single dimension of every Trajectory
307  for (unsigned int t = 0; t < trajectories.size(); t++)
308  {
309  for (unsigned int dim = 0; dim < trajectories[t]->dim(); dim++)
310  {
311  std::vector<double> dimData;
312  for (double time : timestamps)
313  {
314  if (time < endTimes[t])
315  {
316  dimData.push_back(trajectories[t]->getState(time, dim));
317  }
318  else
319  {
320  dimData.push_back(trajectories[t]->getState(endTimes[t], dim));
321  }
322  }
323 
324  finalTrajectory->addDimension(
325  dimData, timestamps, trajectories[t]->getDimensionName(dim));
326  }
327  }
328  return finalTrajectory;
329 }
armarx::DesignerTrajectoryHolder::rnsExists
bool rnsExists(std::string rnsName)
Checks if there is already a DesignerTrajectoryManager assigned to the given RobotNodeSet.
Definition: DesignerTrajectoryHolder.cpp:132
simox::intersection
std::vector< Eigen::Vector2f > intersection(const Circlef &circle, const Segment2D &segment)
Definition: Trajectory.cpp:89
armarx::DesignerTrajectoryHolder::deleteDesignerTrajectoryManager
bool deleteDesignerTrajectoryManager(std::string rnsName)
Deletes the DesignerTrajectoryManager assigned to the given RobotNodeSet.
Definition: DesignerTrajectoryHolder.cpp:66
IceInternal::Handle< Trajectory >
armarx::EnvironmentPtr
std::shared_ptr< Environment > EnvironmentPtr
Definition: Environment.h:29
armarx::DesignerTrajectoryHolder::DesignerTrajectoryHolder
DesignerTrajectoryHolder(EnvironmentPtr environment)
Constructor(s)
Definition: DesignerTrajectoryHolder.cpp:36
armarx::DesignerTrajectoryHolder::import
bool import(DesignerTrajectoryPtr designerTrajectory, bool force)
Imports the given DesignerTrajectory.
Definition: DesignerTrajectoryHolder.cpp:104
armarx::DesignerTrajectoryHolder::isInCollision
bool isInCollision(std::string activeColModelName, std::vector< std::string > bodyColModelsNames, unsigned int fps)
isInCollision Checks if the given collision models (active and body) collide for the jointangle confi...
Definition: DesignerTrajectoryHolder.cpp:192
armarx::DesignerTrajectoryHolder::createDesignerTrajectoryManager
bool createDesignerTrajectoryManager(std::string rnsName)
Public functions.
Definition: DesignerTrajectoryHolder.cpp:46
armarx::DesignerTrajectoryManager
The DesignerTrajectoryManager class enables easy interaction with the model.
Definition: DesignerTrajectoryManager.h:57
armarx::DesignerTrajectoryHolder::getTrajectoryManager
DesignerTrajectoryManagerPtr getTrajectoryManager(std::string rnsName)
Returns the DesignerTrajectoryManager assigned to the given RobotNodeSet.
Definition: DesignerTrajectoryHolder.cpp:91
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:41
armarx::VariantType::Trajectory
const VariantTypeId Trajectory
Definition: Trajectory.h:44
armarx::DesignerTrajectoryManagerPtr
std::shared_ptr< armarx::DesignerTrajectoryManager > DesignerTrajectoryManagerPtr
Typedefinitions.
Definition: DesignerTrajectoryManager.h:402
armarx::DesignerTrajectoryPtr
std::shared_ptr< DesignerTrajectory > DesignerTrajectoryPtr
Definition: DesignerTrajectory.h:163
armarx::DesignerTrajectoryHolder::rnsIsPartOfExistingRns
bool rnsIsPartOfExistingRns(std::string rnsName)
Checks if the given RobotNodeSet is part of a RobotNodeSet to which a DesignerTrajectoryManager is as...
Definition: DesignerTrajectoryHolder.cpp:148
DesignerTrajectoryHolder.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:19