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 ///
46 {
47  // check, if the RobotNodeSet-name already exists as key
48  //if (designerTrajectories.find(rnsName) == designerTrajectories.end())
49  if (!rnsExists(rnsName) && !rnsIsPartOfExistingRns(rnsName))
50  {
51  // RobotNodeSet-name does not exist as key
52  designerTrajectories.insert(
53  std::make_pair(
54  rnsName,
56  new DesignerTrajectoryManager(rnsName, environment))));
57  // environment->getRobot()->getRobotNodeSet(rnsName)))));
58  return true;
59  }
60  else
61  {
62  return false;
63  }
64 }
65 
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 
91 {
92  if (rnsExists(rnsName))
93  {
94  return designerTrajectories.at(rnsName);
95  }
96  else
97  {
98  throw InvalidArgumentException("RNS name does not exist in map");
99  }
100 }
101 
102 bool DesignerTrajectoryHolder::import(DesignerTrajectoryPtr designerTrajectory, bool force = true)
103 {
104  VirtualRobot::RobotNodeSetPtr tmpRns = designerTrajectory->getRns();
105 
106  DesignerTrajectoryManagerPtr tmpDTM(new DesignerTrajectoryManager(tmpRns->getName(),
107  environment));
108  tmpDTM.get()->import(designerTrajectory);
109 
110  // check, if the the RobotNodeSet-name already exists as key
111  if (!rnsExists(tmpRns->getName()))
112  {
113  // RobotNodeSet-name does not exist as key
114  designerTrajectories.insert(std::make_pair(tmpRns->getName(), tmpDTM));
115 
116  return true; // RobotNodeSet-name inserted
117  }
118  else if (force)
119  {
120  designerTrajectories[tmpRns->getName()] = tmpDTM;
121  return true; // RobotNodeSet-name inserted because forced
122  }
123  else
124  {
125  return false; // RobotNodeSet-name not inserted because it already exists
126  }
127 }
128 
129 bool DesignerTrajectoryHolder::rnsExists(std::string rnsName)
130 {
131  // check, if the RobotNodeSet-name already exists as key
132  if (designerTrajectories.find(rnsName) != designerTrajectories.end())
133  {
134  // rns found
135  return true;
136  }
137  else
138  {
139  // rns not found
140  return false;
141  }
142 }
143 
145 {
146  // save all existing RobotNodeSets as vector<vector<string>>
147  // one RobotNodeSet is representet as vector of its RobotNodes-name
148  std::vector<std::vector<std::string>> rnssNodeNames;
149 
150  for (std::map<std::string, DesignerTrajectoryManagerPtr>::iterator it
151  = designerTrajectories.begin();
152  it != designerTrajectories.end();
153  ++it)
154  {
155  VirtualRobot::RobotNodeSetPtr rnsptr = environment->getRobot()->getRobotNodeSet(it->first);
156  std::vector<std::string> tmpRnsNodesNames = rnsptr->getNodeNames();
157  rnssNodeNames.push_back(tmpRnsNodesNames);
158 
159  }
160 
161  // get nodes of given rns
162  std::vector<std::string> givenRnsNodeNames
163  = environment->getRobot()->getRobotNodeSet(rnsName)->getNodeNames();
164 
165  // check if givenRnsNodeNames is a subset of any rnsNodesNames
166  for (std::vector<std::string> rnsNodeNames : rnssNodeNames)
167  {
168  std::vector<std::string> intersection;
169 
170  sort(rnsNodeNames.begin(), rnsNodeNames.end());
171  sort(givenRnsNodeNames.begin(), givenRnsNodeNames.end());
172 
173  std::set_intersection(rnsNodeNames.begin(), rnsNodeNames.end(),
174  givenRnsNodeNames.begin(), givenRnsNodeNames.end(),
175  std::back_inserter(intersection));
176 
177  if (intersection.size() != 0)
178  {
179  return true;
180  }
181  }
182 
183  return false;
184 }
185 
186 bool DesignerTrajectoryHolder::isInCollision(std::string activeColModelName,
187  std::vector<std::string> bodyColModelsNames,
188  unsigned int fps)
189 {
190  TrajectoryPtr t = mergeTrajectories(fps);
191  VirtualRobot::RobotPtr cdRob = environment->getCDRobot();
192 
193  ////////////////////////////////////////////////////////////////////////////////////////////////
194  // Setup CDManager
195  // create new CDManager
196  VirtualRobot::CDManagerPtr cdm(new VirtualRobot::CDManager());
197 
198  // create sosActiveColModel from activeColModelName
199  if (cdRob->getRobotNodeSet(activeColModelName) == nullptr)
200  {
201  return false; // cannot find collision model
202  }
203 
204  VirtualRobot::SceneObjectSetPtr sosActiveColModel(new VirtualRobot::SceneObjectSet());
205  sosActiveColModel->addSceneObjects(
206  cdRob->getRobotNodeSet(activeColModelName));
207 
208  // add sosActiveColModel to CDManager
209  cdm->addCollisionModel(sosActiveColModel);
210 
211  // create bodyColModels
212  for (std::string bodyColModelName : bodyColModelsNames)
213  {
214  // create sosBodyColModel from bodyColModelName
215  if (cdRob->getRobotNodeSet(bodyColModelName) == nullptr)
216  {
217  continue; // cannot find collision model
218  }
219 
220  VirtualRobot::SceneObjectSetPtr sosBodyColModel(new VirtualRobot::SceneObjectSet());
221  sosBodyColModel->addSceneObjects(
222  cdRob->getRobotNodeSet(bodyColModelName));
223 
224  // add sosBodyColModel to CDManager
225  cdm->addCollisionModel(sosBodyColModel);
226  }
227 
228  ////////////////////////////////////////////////////////////////////////////////////////////////
229  // Collision checks
230 
231  // increment for timesteps
232  double increment = 1.0 / fps;
233  double time = 0.0;
234 
235  while (t->dataExists(time))
236  {
237  ////////////////////////////////////////////////////////////////////////////////////////////
238  // Create RobotConfig
239  std::vector<std::string> dimNames = t->getDimensionNames();
240  std::vector<double> jointValuesDouble = t->getStates(time, 0);
241  std::vector<float> jointValues(jointValuesDouble.begin(), jointValuesDouble.end());
242 
243  VirtualRobot::RobotConfigPtr robConf(new VirtualRobot::RobotConfig(
244  cdRob,
245  "config at t=" + std::to_string(time),
246  dimNames,
247  jointValues));
248 
249  ////////////////////////////////////////////////////////////////////////////////////////////
250  // Set RobotConfig to CDRobot
251  cdRob->setConfig(robConf);
252 
253  ////////////////////////////////////////////////////////////////////////////////////////////
254  // Check for collision
255  if (cdm->isInCollision())
256  {
257  return true;
258  }
259 
260  ////////////////////////////////////////////////////////////////////////////////////////////
261  // increment loop variable
262  time = time + increment;
263  }
264 
265  return false;
266 }
267 
268 TrajectoryPtr DesignerTrajectoryHolder::mergeTrajectories(unsigned int fps)
269 {
270  std::vector<TrajectoryPtr> trajectories;
271  std::vector<double> endTimes;
272  double maxEndTime = 0;
273  std::vector<double> timestamps;
274  TrajectoryPtr finalTrajectory(new Trajectory());
275 
276  for (std::map<std::string, DesignerTrajectoryManagerPtr>::iterator it
277  = designerTrajectories.begin();
278  it != designerTrajectories.end();
279  ++it)
280  {
281  if (!it->second->getDesignerTrajectory())
282  {
283  continue;
284  }
285  TrajectoryPtr traj = it->second->getDesignerTrajectory()->getFinalTrajectory();
286 
287  trajectories.push_back(traj);
288  double t = traj->getTimeLength();
289  if (t > maxEndTime)
290  {
291  maxEndTime = t;
292  }
293  endTimes.push_back(t);
294  }
295 
296  double inc = 1.0 / fps;
297  for (double d = 0.0; d < maxEndTime; d = d + inc)
298  {
299  timestamps.push_back(d);
300  }
301 
302  timestamps.push_back(maxEndTime);
303 
304  //get dimensionData of every single dimension of every Trajectory
305  for (unsigned int t = 0; t < trajectories.size(); t++)
306  {
307  for (unsigned int dim = 0; dim < trajectories[t]->dim(); dim++)
308  {
309  std::vector<double> dimData;
310  for (double time : timestamps)
311  {
312  if (time < endTimes[t])
313  {
314  dimData.push_back(trajectories[t]->getState(time, dim));
315  }
316  else
317  {
318  dimData.push_back(trajectories[t]->getState(endTimes[t], dim));
319  }
320  }
321 
322  finalTrajectory->addDimension(dimData, timestamps, trajectories[t]->getDimensionName(dim));
323  }
324  }
325  return finalTrajectory;
326 }
armarx::DesignerTrajectoryHolder::rnsExists
bool rnsExists(std::string rnsName)
Checks if there is already a DesignerTrajectoryManager assigned to the given RobotNodeSet.
Definition: DesignerTrajectoryHolder.cpp:129
simox::intersection
std::vector< Eigen::Vector2f > intersection(const Circlef &circle, const Segment2D &segment)
Definition: Trajectory.cpp:87
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:102
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:186
armarx::DesignerTrajectoryHolder::createDesignerTrajectoryManager
bool createDesignerTrajectoryManager(std::string rnsName)
Public functions.
Definition: DesignerTrajectoryHolder.cpp:45
armarx::DesignerTrajectoryManager
The DesignerTrajectoryManager class enables easy interaction with the model.
Definition: DesignerTrajectoryManager.h:54
armarx::DesignerTrajectoryHolder::getTrajectoryManager
DesignerTrajectoryManagerPtr getTrajectoryManager(std::string rnsName)
Returns the DesignerTrajectoryManager assigned to the given RobotNodeSet.
Definition: DesignerTrajectoryHolder.cpp:90
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:40
armarx::VariantType::Trajectory
const VariantTypeId Trajectory
Definition: Trajectory.h:44
armarx::DesignerTrajectoryManagerPtr
std::shared_ptr< armarx::DesignerTrajectoryManager > DesignerTrajectoryManagerPtr
Typedefinitions.
Definition: DesignerTrajectoryManager.h:399
armarx::DesignerTrajectoryPtr
std::shared_ptr< DesignerTrajectory > DesignerTrajectoryPtr
Definition: DesignerTrajectory.h:165
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:144
DesignerTrajectoryHolder.h
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18