LocationLoader.cpp
Go to the documentation of this file.
1 #include "LocationLoader.h"
2 
3 #include <SimoxUtility/algorithm/string.h>
4 
8 
10 
12 {
14  LocationLoader::LoadFramedLocation(const std::string& source,
15  const std::string& locationName,
16  const nlohmann::json& j)
17  {
18  ARMARX_CHECK(j.find("framedPose") != j.end());
19 
20  const auto framedPose = j.at("framedPose").get<std::map<std::string, nlohmann::json>>();
21 
22  std::string frame = framedPose.at("frame");
23  std::string agent = framedPose.at("agent");
24  Eigen::Matrix4f pose;
25 
26  // sanitize frame if not set
27  if (frame.empty())
28  {
29  if (agent.empty())
30  {
31  ARMARX_WARNING << "Got empty frame for location '" + locationName +
32  "'. Sanitizing it to '" + GlobalFrame + "'.";
33  frame = GlobalFrame;
34  }
35  else
36  {
37  ARMARX_WARNING << "Got empty frame for location '" + locationName +
38  "'. Sanitizing it to 'root' because "
39  "an agent name '" +
40  agent + "' was set.";
41  frame = "root";
42  }
43  }
44 
45  if (frame != GlobalFrame && agent.empty())
46  {
47  ARMARX_WARNING << "Got an empty agent name but a set frame '" + frame +
48  "' for location '" + locationName +
49  "'. This may lead to problems...";
50  // TODO: What to do in that case?
51  }
52 
53  // Utilize ice structure of eigen
55  pose,
56  framedPose.at("pose").get<std::vector<std::vector<float>>>()); // load the 4x4 matrix
57 
58  std::optional<Names> names;
59  if (auto it = j.find("names"); it != j.end())
60  {
61  it->get_to(names.emplace());
62  }
63 
64  FramedLocationPtr loc(new FramedLocation(LocationId(source, locationName),
66  frame,
67  agent,
68  pose,
69  names));
70  return loc;
71  }
72 
74  LocationLoader::LoadBoxedLocation(const std::string& source,
75  const std::string& locationName,
76  const nlohmann::json& j)
77  {
78  ARMARX_CHECK(j.find("framedOrientedBox") != j.end());
79 
80  const auto framedOrientedBox =
81  j.at("framedOrientedBox").get<std::map<std::string, nlohmann::json>>();
82 
83  std::string frame = framedOrientedBox.at("frame");
84  std::string agent = framedOrientedBox.at("agent");
85  Eigen::Matrix4f pose;
86  Eigen::Vector3f extents;
87 
88  // sanitize frame if not set
89  if (frame.empty())
90  {
91  if (agent.empty())
92  {
93  ARMARX_WARNING << "Got empty frame for location '" + locationName +
94  "'. Sanitizing it to '" + GlobalFrame + "'.";
95  frame = GlobalFrame;
96  }
97  else
98  {
99  ARMARX_WARNING << "Got empty frame for location '" + locationName +
100  "'. Sanitizing it to 'root' because "
101  "an agent name '" +
102  agent + "' was set.";
103  frame = "root";
104  }
105  }
106 
107  if (frame != GlobalFrame && agent.empty())
108  {
109  ARMARX_WARNING << "Got an empty agent name but a set frame '" + frame +
110  "' for location '" + locationName +
111  "'. This may lead to problems...";
112  // TODO: What to do in that case?
113  }
114 
115  // Utilize ice structure of eigen
117  pose,
118  framedOrientedBox.at("pose")
119  .get<std::vector<std::vector<float>>>()); // load the 4x4 matrix
120 
121  // Utilize ice structure of eigen
123  extents,
124  framedOrientedBox.at("extents").get<std::vector<float>>()); // load the 4x4 matrix
125 
126  std::optional<Names> names;
127  if (auto it = j.find("names"); it != j.end())
128  {
129  it->get_to(names.emplace());
130  }
131 
132  FramedBoxedLocationPtr loc(new FramedBoxedLocation(LocationId(source, locationName),
134  frame,
135  agent,
136  pose,
137  extents,
138  names));
139  return loc;
140  }
141 
142  std::vector<LocationPtr>
143  LocationLoader::LoadLocations(const std::string& source /* e.g. dataset/class or graph */,
144  const nlohmann::json& js)
145  {
146  std::vector<LocationPtr> ret;
147  if (not js.contains("locations"))
148  {
150  << "The locations file has the wrong structure. Missing key 'locations'.";
151  return ret;
152  }
153 
154  for (const auto& [locationName, j] :
155  js.at("locations").get<std::map<std::string, nlohmann::json>>())
156  {
157  if (j.find("framedPose") != j.end())
158  {
159  ret.push_back(LocationLoader::LoadFramedLocation(source, locationName, j));
160  }
161  else if (j.find("framedOrientedBox") != j.end())
162  {
163  ret.push_back(LocationLoader::LoadBoxedLocation(source, locationName, j));
164  }
165  else
166  {
168  << "The element '" << locationName
169  << "' has no 'framedPose' member or no 'framedOrientedBox' member. Skipping "
170  "this entity.";
171  continue;
172  }
173  }
174  return ret;
175  }
176 } // namespace armarx::priorknowledge::util
armarx::aron::ret
ReaderT::InputType T & ret
Definition: rw.h:21
armarx::priorknowledge::util::FramedBoxedLocationPtr
std::unique_ptr< FramedBoxedLocation > FramedBoxedLocationPtr
Definition: Location.h:89
ice_conversions.h
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:62
armarx::priorknowledge::util::FramedLocationPtr
std::unique_ptr< FramedLocation > FramedLocationPtr
Definition: Location.h:90
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
LocationLoader.h
armarx::priorknowledge::util::LocationType::FRAMED_BOXED_LOCATION
@ FRAMED_BOXED_LOCATION
armarx::core::eigen::fromIce
void fromIce(Eigen::Vector2f &e, const Ice::FloatSeq &ice)
Definition: ice_conversions.cpp:10
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:681
ExpressionException.h
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
armarx::priorknowledge::util::LocationLoader::LoadLocations
static std::vector< LocationPtr > LoadLocations(const std::string &dataset, const nlohmann::json &)
Definition: LocationLoader.cpp:143
armarx::viz::data::ElementFlags::names
const simox::meta::IntEnumNames names
Definition: json_elements.cpp:14
Logging.h
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::priorknowledge::util::LocationType::FRAMED_LOCATION
@ FRAMED_LOCATION
armarx::priorknowledge::util
Definition: AffordanceLoader.cpp:6
json_conversions.h