LocationUtils.cpp
Go to the documentation of this file.
1 #include "LocationUtils.h"
2 
3 #include <optional>
4 #include <string>
5 #include <variant>
6 #include <vector>
7 
8 #include <VirtualRobot/XML/BaseIO.h>
9 #include <VirtualRobot/XML/RobotIO.h>
10 
13 
20 
22 
24 {
25 
26  template <typename... Ts>
27  struct overload : Ts...
28  {
29  using Ts::operator()...;
30  };
31 
32  template <typename... Ts>
33  overload(Ts...) -> overload<Ts...>; // deduction guide
34 
36  resolveArticulated(const FramedPose& framedPose,
37  const objpose::ObjectPose& pose,
38  const ObjectInfo info)
39  {
40  std::optional<PackageFileLocation> articulatedModel = info.articulatedSimoxXML();
41  if (!articulatedModel.has_value())
42  {
43  return "object '" + info.idStr() +
44  "' does not have an articulated xml model but the specified frame isn't "
45  "root";
46  }
47 
48  const auto modelPath = armarx::PackagePath(articulatedModel.value().package,
49  articulatedModel.value().relativePath);
50  const auto robot = VirtualRobot::RobotIO::loadRobot(modelPath.toSystemPath(),
51  VirtualRobot::BaseIO::eStructure);
52  if (robot == nullptr)
53  {
54  return "articulated model of '" + info.idStr() + "couldn't be loaded";
55  }
56  robot->setGlobalPose(pose.objectPoseGlobal);
57  robot->setJointValues(pose.objectJointValues);
58 
59  const auto node = robot->getRobotNode(framedPose.frame);
60  if (node == nullptr)
61  {
62  return "unknown node '" + framedPose.frame + "' of object '" + info.idStr() + "'";
63  }
64 
65  const auto& global_T_node = node->getGlobalPose();
66  const auto& node_T_location = framedPose.toEigen();
67 
68  return Pose(global_T_node * node_T_location);
69  }
70 
71  /*!
72  * \brief Resolve the location to it's global pose if it is defined globally or return the
73  * ObjectID it is defined relatively to
74  */
75  std::variant<Pose, ObjectID>
76  resolveGlobal(const FramedPose& framedPose, const std::string& instanceID)
77  {
78  if (framedPose.agent.empty())
79  {
80  ARMARX_CHECK_EQUAL(framedPose.frame, armarx::GlobalFrame)
81  << "frame is not set as global frame even though agent is empty";
82  return core::Pose(framedPose.toEigen());
83  }
84 
85  ObjectID id(framedPose.agent);
86  if (id.instanceName().empty())
87  {
88  id.setInstanceName(instanceID);
89  }
90 
91  return id;
92  }
93 
94  ResolvedLocation
96  const FramedPose& framedPose,
97  const std::string& instanceID)
98  {
99  const auto res = resolveGlobal(framedPose, instanceID);
100 
101  return std::visit(
102  overload{[&](const Pose& pose) -> ResolvedLocation { return pose; },
103  [&](const ObjectID& id) -> ResolvedLocation
104  {
105  const std::optional<objpose::ObjectPose> pose = client.fetchObjectPose(id);
106  if (!pose.has_value())
107  {
108  return "unknown object '" + id.str() + "'";
109  }
110 
111  if (framedPose.frame == "root")
112  {
113  const auto& global_T_obj = pose.value().objectPoseGlobal;
114  const auto& obj_T_location = framedPose.toEigen();
115 
116  return Pose(global_T_obj * obj_T_location);
117  }
118  else
119  {
120  const auto& finder = client.getObjectFinder();
121  std::optional<ObjectInfo> info = finder.findObject(id);
122  if (!info.has_value())
123  {
124  return "can't find ObjectInfo for '" + id.str() + "'";
125  }
126 
127  return resolveArticulated(framedPose, pose.value(), info.value());
128  }
129  }},
130  res);
131  }
132 
133  ResolvedLocation
135  const std::vector<ObjectInfo>& info,
136  const FramedPose& framedPose,
137  const std::string& instanceID)
138  {
139  const auto res = resolveGlobal(framedPose, instanceID);
140 
141  return std::visit(
142 
143  overload{[&](const Pose& pose) -> ResolvedLocation { return pose; },
144  [&](const ObjectID& id) -> ResolvedLocation
145  {
146  const auto pose = objects.find(id);
147  if (pose == objects.end())
148  {
149  return "unknown object '" + id.str() + "'";
150  }
151 
152  if (framedPose.frame == "root")
153  {
154  const auto& global_T_obj = pose->second.objectPoseGlobal;
155  const auto& obj_T_location = framedPose.toEigen();
156 
157  return Pose(global_T_obj * obj_T_location);
158  }
159  else
160  {
161  for (const ObjectInfo& i : info)
162  {
163  if (i.id().equalClass(id))
164  {
165  return resolveArticulated(framedPose, pose->second, i);
166  }
167  }
168 
169  return "Can't find ObjectInfo for '" + id.str() + "'";
170  }
171  }},
172  res);
173  }
174 
175 
176 } // namespace armarx::navigation::core
armarx::ObjectID
A known object ID of the form "Dataset/ClassName" or "Dataset/ClassName/InstanceName".
Definition: ObjectID.h:10
armarx::navigation::core::resolveLocation
void resolveLocation(Graph::Vertex &vertex, const aron::data::DictPtr &locationData)
Definition: Graph.cpp:250
armarx::navigation::core::Pose
Eigen::Isometry3f Pose
Definition: basic_types.h:31
basic_types.h
armarx::ObjectInfo::articulatedSimoxXML
PackageFileLocation articulatedSimoxXML() const
Definition: ObjectInfo.cpp:148
armarx::FramedPose
The FramedPose class.
Definition: FramedPose.h:280
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:65
armarx::navigation::core
This file is part of ArmarX.
Definition: aron_conversions.cpp:19
armarx::navigation::core::overload
overload(Ts...) -> overload< Ts... >
armarx::objpose::ObjectPoseClient::fetchObjectPose
std::optional< ObjectPose > fetchObjectPose(const ObjectID &objectID) const
Fetch the pose of a single object.
Definition: ObjectPoseClient.cpp:56
armarx::objpose::ObjectPoseClient
Provides access to the armarx::objpose::ObjectPoseStorageInterface (aka the object memory).
Definition: ObjectPoseClient.h:17
cxxopts::empty
bool empty(const std::string &s)
Definition: cxxopts.hpp:234
armarx::objpose::ObjectPoseClient::getObjectFinder
const ObjectFinder & getObjectFinder() const
Get the internal object finder.
Definition: ObjectPoseClient.cpp:102
armarx::navigation::core::ResolvedLocation
Definition: LocationUtils.h:38
LocationUtils.h
FramedPose.h
armarx::navigation::core::resolveGlobal
std::variant< Pose, ObjectID > resolveGlobal(const FramedPose &framedPose, const std::string &instanceID)
Resolve the location to it's global pose if it is defined globally or return the ObjectID it is defin...
Definition: LocationUtils.cpp:76
ObjectID.h
armarx::Pose::toEigen
virtual Eigen::Matrix4f toEigen() const
Definition: Pose.cpp:334
ObjectPoseClient.h
ObjectPose.h
ObjectInfo.h
armarx::ObjectInfo::idStr
std::string idStr() const
Definition: ObjectInfo.cpp:73
ExpressionException.h
forward_declarations.h
armarx::objpose::ObjectPose::objectPoseGlobal
Eigen::Matrix4f objectPoseGlobal
The object pose in the global frame.
Definition: ObjectPose.h:71
armarx::ObjectFinder::findObject
std::optional< ObjectInfo > findObject(const std::string &dataset, const std::string &name) const
Definition: ObjectFinder.cpp:64
armarx::aron::data::visit
requires isVisitor< VisitorImplementation, typename VisitorImplementation::Input > void visit(VisitorImplementation &v, typename VisitorImplementation::Input &o)
Definition: Visitor.h:136
armarx::navigation::core::overload
Definition: LocationUtils.cpp:27
ARMARX_CHECK_EQUAL
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
Definition: ExpressionException.h:130
armarx::navigation::core::resolveArticulated
ResolvedLocation resolveArticulated(const FramedPose &framedPose, const objpose::ObjectPose &pose, const ObjectInfo info)
Definition: LocationUtils.cpp:36
armarx::PackagePath
Definition: PackagePath.h:52
armarx::objpose::ObjectPose::objectJointValues
std::map< std::string, float > objectJointValues
The object's joint values if it is articulated.
Definition: ObjectPose.h:83
armarx::objpose::ObjectPose
An object pose as stored by the ObjectPoseStorage.
Definition: ObjectPose.h:33
armarx::ObjectInfo
Accessor for the object files.
Definition: ObjectInfo.h:36
PackagePath.h
armarx::objpose::ObjectPoseMap
std::map< ObjectID, ObjectPose > ObjectPoseMap
Definition: forward_declarations.h:21