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