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 
13 
15 {
16  Reader::~Reader() = default;
17 
19  Reader::buildQuery(const Query& query) const
20  {
22 
24  qb.coreSegments().withName(properties().coreSegmentName);
25 
28  {
29  if (not query.providerName.empty())
30  {
31  return coreSegmentQuery.providerSegments().withName(query.providerName);
32  }
33  return coreSegmentQuery.providerSegments().all();
34  }();
35 
36  providerQuery.entities()
37  .all()
38  .snapshots()
39  .beforeOrAtTime(query.timestamp);
40 
41  return qb;
42  }
43 
44  std::string
46  {
47  return "mem.human.pose.";
48  }
49 
52  {
53  return {.memoryName = "Human", .coreSegmentName = "Pose"};
54  }
55 
56  std::vector<HumanPose>
57  asHumanPoses(const wm::ProviderSegment& providerSegment,
58  const DateTime& timestamp,
59  const Duration& maxAge)
60  {
62 
63  // ARMARX_CHECK(not providerSegment.empty()) << "No entities";
64  // ARMARX_CHECK(providerSegment.size() == 1) << "There should be only one entity!";
65 
66  std::vector<HumanPose> humanPoses;
67  providerSegment.forEachEntity(
68  [&](const wm::Entity& entity)
69  {
71 
72  const auto& entitySnapshot = entity.getLatestSnapshot();
73  // Not clear yet why, but it seems that this sometimes happens (not frequently).
74  // ARMARX_CHECK(not entitySnapshot.empty()) << "No entity snapshot instances";
75 
76  entitySnapshot.forEachInstance(
77  [&humanPoses, &timestamp, &maxAge](const auto& entityInstance)
78  {
80 
81  const core::time::Duration dtToNow =
82  timestamp - entityInstance.metadata().referencedTime;
83 
84  if (dtToNow < maxAge and dtToNow.isPositive())
85  {
86  const auto aronDto =
87  tryCast<armarx::human::arondto::HumanPose>(entityInstance);
88  ARMARX_CHECK(aronDto) << "Failed casting to HumanPose";
89 
90  HumanPose humanPose;
91  fromAron(*aronDto, humanPose);
92  humanPose.timestamp = entityInstance.metadata().referencedTime;
93 
94  humanPoses.push_back(humanPose);
95  };
96  });
97  });
98 
99  // when no humans are in the scene, this is empty
100  //ARMARX_CHECK_NOT_EMPTY(humanPoses);
101 
102  return humanPoses;
103  }
104 
105  Reader::Result
106  Reader::query(const Query& query) const
107  {
108  ARMARX_TRACE;
109  const auto qb = buildQuery(query);
110 
111  ARMARX_DEBUG << "[MappingDataReader] query ... ";
112 
113  const armem::client::QueryResult qResult = memoryReader().query(qb.buildQueryInput());
114 
115  ARMARX_DEBUG << "[MappingDataReader] result: " << qResult;
116 
117  if (not qResult.success)
118  {
119  ARMARX_WARNING << "Failed to query data from memory: " << qResult.errorMessage;
120  return {.humanPoses = {},
121  .status = Result::Status::Error,
122  .errorMessage = qResult.errorMessage};
123  }
124 
125  ARMARX_TRACE;
126  const auto coreSegment = qResult.memory.getCoreSegment(properties().coreSegmentName);
127 
128  if (query.providerName.empty())
129  {
130  ARMARX_TRACE;
131 
132  std::vector<HumanPose> allHumanPoses;
133 
134  coreSegment.forEachProviderSegment(
135  [&allHumanPoses, &query](const auto& providerSegment)
136  {
137  const std::vector<HumanPose> humanPoses =
138  asHumanPoses(providerSegment, query.timestamp, query.maxAge);
139  std::copy(
140  humanPoses.begin(), humanPoses.end(), std::back_inserter(allHumanPoses));
141  });
142 
143  if (allHumanPoses.empty())
144  {
145  // ARMARX_WARNING << "No entities.";
146  return {.humanPoses = {},
147  .status = Result::Status::NoData,
148  .errorMessage = "No entities"};
149  }
150 
151  return Result{.humanPoses = allHumanPoses, .status = Result::Status::Success};
152  }
153 
154  ARMARX_TRACE;
155 
156  // -> provider segment name is set
157  if (not coreSegment.hasProviderSegment(query.providerName))
158  {
159  ARMARX_INFO << deactivateSpam(5) << "Provider segment `" << query.providerName
160  << "` does not exist (yet).";
161  return {.humanPoses = {}, .status = Result::Status::NoData};
162  }
163 
164  ARMARX_TRACE;
165 
166  const wm::ProviderSegment& providerSegment =
167  coreSegment.getProviderSegment(query.providerName);
168 
169  if (providerSegment.empty())
170  {
171  ARMARX_WARNING << "No entities.";
172  return {
173  .humanPoses = {}, .status = Result::Status::NoData, .errorMessage = "No entities"};
174  }
175 
176  try
177  {
178  ARMARX_TRACE;
179 
180  const auto humanPoses = asHumanPoses(providerSegment, query.timestamp, query.maxAge);
181  return Result{.humanPoses = humanPoses, .status = Result::Status::Success};
182  }
183  catch (...)
184  {
186  .errorMessage = GetHandledExceptionString()};
187  }
188  }
189 
190 } // namespace armarx::armem::human::client
armarx::armem::client::query::EntitySelector::all
EntitySelector & all() override
Definition: selectors.cpp:96
armarx::armem::client::query::ProviderSegmentSelector
Definition: selectors.h:80
armarx::armem::base::detail::MemoryContainerBase::empty
bool empty() const
Definition: MemoryContainerBase.h:44
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:123
armarx::armem::human::client::Reader::defaultProperties
Properties defaultProperties() const override
Definition: HumanPoseReader.cpp:51
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:86
armarx::armem::human::client
Definition: HumanPoseReader.cpp:14
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:30
armarx::core::time::Duration::isPositive
bool isPositive() const
Tests whether the duration is positive (value in µs > 0).
Definition: Duration.cpp:195
deactivateSpam
SpamFilterDataPtr deactivateSpam(SpamFilterDataPtr const &spamFilter, float deactivationDurationSec, const std::string &identifier, bool deactivate)
Definition: Logging.cpp:72
armarx::GetHandledExceptionString
std::string GetHandledExceptionString()
Definition: Exception.cpp:147
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
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:38
armarx::armem::human::client::asHumanPoses
std::vector< HumanPose > asHumanPoses(const wm::ProviderSegment &providerSegment, const DateTime &timestamp, const Duration &maxAge)
Definition: HumanPoseReader.cpp:57
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::armem::client::query::CoreSegmentSelector::withName
CoreSegmentSelector & withName(const std::string &name) override
Definition: selectors.cpp:177
armarx::armem::human::client::Reader::Result::Status::Success
@ Success
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:60
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:140
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
aron_conversions.h
armarx::armem::base::ProviderSegmentBase::forEachEntity
bool forEachEntity(EntityFunctionT &&func)
Definition: ProviderSegmentBase.h:189
armarx::armem::client::query::SnapshotSelector::beforeOrAtTime
SnapshotSelector & beforeOrAtTime(Time timestamp)
Definition: selectors.cpp:68
armarx::armem::client::query::CoreSegmentSelector
Definition: selectors.h:117
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:17
armarx::armem::client::query::Builder
The query::Builder class provides a fluent-style specification of hierarchical queries.
Definition: Builder.h:22
armarx::armem::client::query::CoreSegmentSelector::providerSegments
ProviderSegmentSelector & providerSegments()
Start specifying provider segments.
Definition: selectors.cpp:160
util.h
armarx::armem::human::client::Reader::buildQuery
::armarx::armem::client::query::Builder buildQuery(const Query &query) const
Definition: HumanPoseReader.cpp:19
Logging.h
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:186
armarx::armem::client::query::ProviderSegmentSelector::all
ProviderSegmentSelector & all() override
Definition: selectors.cpp:133
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.
Definition: Reader.cpp:33
armarx::armem::human::client::Reader::query
Result query(const Query &query) const
Definition: HumanPoseReader.cpp:106
armarx::armem::human::client::Reader::propertyPrefix
std::string propertyPrefix() const override
Definition: HumanPoseReader.cpp:45