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 {
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
The FramedPose class.
Definition FramedPose.h:281
A known object ID of the form "Dataset/ClassName" or "Dataset/ClassName/InstanceName".
Definition ObjectID.h:11
Accessor for the object files.
Definition ObjectInfo.h:37
std::string idStr() const
PackageFileLocation articulatedSimoxXML() const
virtual Eigen::Matrix4f toEigen() const
Definition Pose.cpp:334
Provides access to the armarx::objpose::ObjectPoseStorageInterface (aka the object memory).
#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...
std::string const GlobalFrame
Variable of the global coordinate system.
Definition FramedPose.h:65
This file is part of ArmarX.
This file is part of ArmarX.
ResolvedLocation resolveArticulated(const FramedPose &framedPose, const objpose::ObjectPose &pose, const ObjectInfo info)
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...
void resolveLocation(Graph::Vertex &vertex, const aron::data::DictPtr &locationData)
Definition Graph.cpp:267
Eigen::Isometry3f Pose
Definition basic_types.h:31
overload(Ts...) -> overload< Ts... >
std::map< ObjectID, ObjectPose > ObjectPoseMap
An object pose as stored by the ObjectPoseStorage.
Definition ObjectPose.h:34
std::map< std::string, float > objectJointValues
The object's joint values if it is articulated.
Definition ObjectPose.h:83
Eigen::Matrix4f objectPoseGlobal
The object pose in the global frame.
Definition ObjectPose.h:71