HumanPoseReader.cpp
Go to the documentation of this file.
1 #include "HumanPoseReader.h"
2 
3 #include <iterator>
4 
7 
9 
10 #include <VisionX/libraries/armem_human/aron/HumanPose.aron.generated.h>
12 
14 {
15  Reader::~Reader() = default;
16 
18  Reader::buildQuery(const Query& query) const
19  {
21 
23  qb.coreSegments().withName(properties().coreSegmentName);
24 
27  {
28  if (not query.providerName.empty())
29  {
30  return coreSegmentQuery.providerSegments().withName(query.providerName);
31  }
32  return coreSegmentQuery.providerSegments().all();
33  }();
34 
35  providerQuery.entities().all().snapshots().beforeOrAtTime(query.timestamp);
36 
37  return qb;
38  }
39 
40  std::string
42  {
43  return "mem.human.pose.";
44  }
45 
48  {
49  return {.memoryName = "Human", .coreSegmentName = "Pose"};
50  }
51 
52  std::vector<HumanPose>
53  asHumanPoses(const wm::ProviderSegment& providerSegment,
54  const DateTime& timestamp,
55  const Duration& maxAge)
56  {
58 
59  // ARMARX_CHECK(not providerSegment.empty()) << "No entities";
60  // ARMARX_CHECK(providerSegment.size() == 1) << "There should be only one entity!";
61 
62  std::vector<HumanPose> humanPoses;
63  providerSegment.forEachEntity(
64  [&](const wm::Entity& entity)
65  {
67 
68  const auto& entitySnapshot = entity.getLatestSnapshot();
69  // Not clear yet why, but it seems that this sometimes happens (not frequently).
70  // ARMARX_CHECK(not entitySnapshot.empty()) << "No entity snapshot instances";
71 
72  entitySnapshot.forEachInstance(
73  [&humanPoses, &timestamp, &maxAge](const auto& entityInstance)
74  {
76 
77  const core::time::Duration dtToNow =
78  timestamp - entityInstance.metadata().referencedTime;
79 
80  if (dtToNow < maxAge and dtToNow.isPositive())
81  {
82  const auto aronDto =
83  tryCast<armarx::human::arondto::HumanPose>(entityInstance);
84  ARMARX_CHECK(aronDto) << "Failed casting to HumanPose";
85 
86  HumanPose humanPose;
87  fromAron(*aronDto, humanPose);
88  humanPose.timestamp = entityInstance.metadata().referencedTime;
89 
90  humanPoses.push_back(humanPose);
91  };
92  });
93  });
94 
95  // when no humans are in the scene, this is empty
96  //ARMARX_CHECK_NOT_EMPTY(humanPoses);
97 
98  return humanPoses;
99  }
100 
101  Reader::Result
102  Reader::query(const Query& query) const
103  {
104  ARMARX_TRACE;
105  const auto qb = buildQuery(query);
106 
107  ARMARX_DEBUG << "[MappingDataReader] query ... ";
108 
109  const armem::client::QueryResult qResult = memoryReader().query(qb.buildQueryInput());
110 
111  ARMARX_DEBUG << "[MappingDataReader] result: " << qResult;
112 
113  if (not qResult.success)
114  {
115  ARMARX_WARNING << "Failed to query data from memory: " << qResult.errorMessage;
116  return {.humanPoses = {},
117  .status = Result::Status::Error,
118  .errorMessage = qResult.errorMessage};
119  }
120 
121  ARMARX_TRACE;
122  const auto coreSegment = qResult.memory.getCoreSegment(properties().coreSegmentName);
123 
124  if (query.providerName.empty())
125  {
126  ARMARX_TRACE;
127 
128  std::vector<HumanPose> allHumanPoses;
129 
130  coreSegment.forEachProviderSegment(
131  [&allHumanPoses, &query](const auto& providerSegment)
132  {
133  const std::vector<HumanPose> humanPoses =
134  asHumanPoses(providerSegment, query.timestamp, query.maxAge);
135  std::copy(
136  humanPoses.begin(), humanPoses.end(), std::back_inserter(allHumanPoses));
137  });
138 
139  if (allHumanPoses.empty())
140  {
141  // ARMARX_WARNING << "No entities.";
142  return {.humanPoses = {},
143  .status = Result::Status::NoData,
144  .errorMessage = "No entities"};
145  }
146 
147  return Result{.humanPoses = allHumanPoses, .status = Result::Status::Success};
148  }
149 
150  ARMARX_TRACE;
151 
152  // -> provider segment name is set
153  if (not coreSegment.hasProviderSegment(query.providerName))
154  {
155  ARMARX_INFO << deactivateSpam(5) << "Provider segment `" << query.providerName
156  << "` does not exist (yet).";
157  return {.humanPoses = {}, .status = Result::Status::NoData};
158  }
159 
160  ARMARX_TRACE;
161 
162  const wm::ProviderSegment& providerSegment =
163  coreSegment.getProviderSegment(query.providerName);
164 
165  if (providerSegment.empty())
166  {
167  ARMARX_WARNING << "No entities.";
168  return {
169  .humanPoses = {}, .status = Result::Status::NoData, .errorMessage = "No entities"};
170  }
171 
172  try
173  {
174  ARMARX_TRACE;
175 
176  const auto humanPoses = asHumanPoses(providerSegment, query.timestamp, query.maxAge);
177  return Result{.humanPoses = humanPoses, .status = Result::Status::Success};
178  }
179  catch (...)
180  {
182  .errorMessage = GetHandledExceptionString()};
183  }
184  }
185 
186 } // namespace armarx::armem::human::client
armarx::armem::client::query::EntitySelector::all
EntitySelector & all() override
Definition: selectors.cpp:104
armarx::armem::client::query::ProviderSegmentSelector
Definition: selectors.h:79
armarx::armem::base::detail::MemoryContainerBase::empty
bool empty() const
Definition: MemoryContainerBase.h:41
armarx::armem::client::util::SimpleReaderBase::properties
const Properties & properties() const
Definition: SimpleReaderBase.cpp:55
armarx::armem::client::query::ProviderSegmentSelector::entities
EntitySelector & entities()
Start specifying entities.
Definition: selectors.cpp:135
armarx::armem::human::client::Reader::defaultProperties
Properties defaultProperties() const override
Definition: HumanPoseReader.cpp:47
armarx::armem::human::client::Reader::Result::Status::Error
@ Error
armarx::armem::base::detail::GetLatestSnapshotMixin::getLatestSnapshot
auto & getLatestSnapshot(int snapshotIndex=0)
Retrieve the latest entity snapshot.
Definition: lookup_mixins.h:199
armarx::armem::human::client::Reader::Result
Definition: HumanPoseReader.h:48
armarx::armem::client::query::EntitySelector::snapshots
SnapshotSelector & snapshots()
Start specifying entity snapshots.
Definition: selectors.cpp:92
armarx::armem::human::client
Definition: HumanPoseReader.cpp:13
armarx::armem::client::QueryResult
Result of a QueryInput.
Definition: Query.h:50
armarx::armem::client::util::SimpleReaderBase::Properties::memoryName
std::string memoryName
Definition: SimpleReaderBase.h:45
armarx::armem::server::wm::Entity
Definition: memory_definitions.h:27
armarx::core::time::Duration::isPositive
bool isPositive() const
Tests whether the duration is positive (value in µs > 0).
Definition: Duration.cpp:168
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:75
armarx::GetHandledExceptionString
std::string GetHandledExceptionString()
Definition: Exception.cpp:165
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:77
copy
Use of this software is granted under one of the following two to be chosen freely by the user Boost Software License Version Marcin Kalicinski Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do all subject to the including the above license this restriction and the following must be included in all copies of the in whole or in and all derivative works of the unless such copies or derivative works are solely in the form of machine executable object code generated by a source language processor THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF FITNESS FOR A PARTICULAR TITLE AND NON INFRINGEMENT IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER WHETHER IN TORT OR ARISING OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE The MIT Marcin Kalicinski Permission is hereby free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to copy
Definition: license.txt:39
armarx::armem::client::query::Builder::coreSegments
CoreSegmentSelector & coreSegments()
Start specifying core segments.
Definition: Builder.cpp:42
armarx::armem::human::client::asHumanPoses
std::vector< HumanPose > asHumanPoses(const wm::ProviderSegment &providerSegment, const DateTime &timestamp, const Duration &maxAge)
Definition: HumanPoseReader.cpp:53
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:184
armarx::armem::client::query::CoreSegmentSelector::withName
CoreSegmentSelector & withName(const std::string &name) override
Definition: selectors.cpp:198
armarx::armem::human::client::Reader::Result::Status::Success
@ Success
timestamp
std::string timestamp()
Definition: CartographerAdapter.cpp:85
armarx::armem::client::util::SimpleReaderBase::memoryReader
const armem::client::Reader & memoryReader() const
Definition: SimpleReaderBase.cpp:49
armarx::armem::human::client::Reader::Result::humanPoses
std::vector< HumanPose > humanPoses
Definition: HumanPoseReader.h:50
armarx::armem::server::wm::ProviderSegment
Definition: memory_definitions.h:52
HumanPoseReader.h
ExpressionException.h
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::armem::client::util::SimpleReaderBase::Properties
Definition: SimpleReaderBase.h:43
armarx::armem::client::query::ProviderSegmentSelector::withName
ProviderSegmentSelector & withName(const std::string &name) override
Definition: selectors.cpp:155
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
aron_conversions.h
armarx::armem::base::ProviderSegmentBase::forEachEntity
bool forEachEntity(EntityFunctionT &&func)
Definition: ProviderSegmentBase.h:193
armarx::armem::client::query::SnapshotSelector::beforeOrAtTime
SnapshotSelector & beforeOrAtTime(Time timestamp)
Definition: selectors.cpp:73
armarx::armem::client::query::CoreSegmentSelector
Definition: selectors.h:118
armarx::armem::human::client::Reader::Result::Status::NoData
@ NoData
armarx::armem::human::client::Reader::Result::status
enum armarx::armem::human::client::Reader::Result::Status status
armarx::core::time::Duration
Represents a duration.
Definition: Duration.h:16
armarx::armem::client::query::Builder
The query::Builder class provides a fluent-style specification of hierarchical queries.
Definition: Builder.h:21
armarx::armem::client::query::CoreSegmentSelector::providerSegments
ProviderSegmentSelector & providerSegments()
Start specifying provider segments.
Definition: selectors.cpp:178
util.h
armarx::armem::human::client::Reader::buildQuery
::armarx::armem::client::query::Builder buildQuery(const Query &query) const
Definition: HumanPoseReader.cpp:18
Logging.h
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
armarx::armem::client::query::ProviderSegmentSelector::all
ProviderSegmentSelector & all() override
Definition: selectors.cpp:147
armarx::armem::human::client::Reader::Query
Definition: HumanPoseReader.h:39
armarx::armem::human::client::Reader::~Reader
~Reader() override
armarx::armem::client::Reader::query
QueryResult query(const QueryInput &input) const
Perform a query on the WM.
Definition: Reader.cpp:69
armarx::armem::human::client::Reader::query
Result query(const Query &query) const
Definition: HumanPoseReader.cpp:102
armarx::armem::human::client::Reader::propertyPrefix
std::string propertyPrefix() const override
Definition: HumanPoseReader.cpp:41