HumanMemory.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package RobotAPI::ArmarXObjects::ExampleMemory
17  * @author Rainer Kartmann ( rainer dot kartmann at kit dot edu )
18  * @date 2020
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #include "HumanMemory.h"
24 
25 #include <qnamespace.h>
26 
27 #include <SimoxUtility/algorithm/apply.hpp>
28 #include <VirtualRobot/VirtualRobot.h>
29 
32 
34 #include <RobotAPI/interface/armem/commit.h>
39 
41 #include <VisionX/libraries/armem_human/aron/HumanPose.aron.generated.h>
42 
44 {
45 
48  {
51 
52  setMemoryName("Human");
53 
54  profileSegment.defineProperties(defs, "profile.");
55  personInstanceSegment.defineProperties(defs, "instance");
56  poseSegment.defineProperties(defs, "pose.");
57  faceRecognitionSegment.defineProperties(defs, "face.");
58  identificationSegment.defineProperties(defs, "ident.");
59 
60  return defs;
61  }
62 
64  profileSegment(iceAdapter()),
65  personInstanceSegment(iceAdapter()),
66  poseSegment(iceAdapter()),
67  faceRecognitionSegment(iceAdapter()),
68  identificationSegment(iceAdapter()),
69  visu(poseSegment, faceRecognitionSegment, personInstanceSegment)
70  {
71  addPlugin(virtualRobotReaderPlugin);
72  }
73 
74  std::string
76  {
77  return "HumanMemory";
78  }
79 
80  void
82  {
83  profileSegment.init();
84  personInstanceSegment.init();
85  poseSegment.init();
86  faceRecognitionSegment.init();
87  identificationSegment.init();
88  visu.init();
89 
90  workingMemory().addCoreSegment("RecognizedAction");
91  }
92 
93  void
95  {
97  }
98 
99  void
101  {
102  }
103 
104  void
106  {
107  }
108 
110  HumanMemory::getSynchronizedRobot(const std::string& robotName,
111  const armarx::DateTime& timestamp)
112  {
113  // seach and retrieve robot
114  auto& robot = [&]() -> VirtualRobot::RobotPtr&
115  {
116  // retrieve robot from memory if it is not in cache
117  if (robots.count(robotName) == 0)
118  {
119  auto newRobot = virtualRobotReaderPlugin->get().getRobot(robotName);
120  ARMARX_CHECK_NOT_NULL(newRobot)
121  << "Failed to obtain robot with name `" << robotName << "`.";
122 
123  robots[robotName] = newRobot;
124  }
125 
126  return robots.at(robotName);
127  }();
128 
129  // synchronize robot
130  ARMARX_CHECK(virtualRobotReaderPlugin->get().synchronizeRobot(*robot, timestamp));
131 
132  return robot;
133  }
134 
135  void
137  const ::armarx::aron::data::dto::AronDictSeq& dictSeq,
138  const ::std::string& providerName,
139  const ::armarx::core::time::dto::DateTime& iceTimestamp,
140  const ::Ice::Current& /*c*/)
141  {
142  if (dictSeq.empty())
143  {
144  return;
145  }
146 
147  // convert from ice
148  armarx::DateTime timestamp;
149  fromIce(iceTimestamp, timestamp);
150 
151  // convert to our aron type
152  const auto toHumanPose = [](const ::armarx::aron::data::dto::DictPtr& dict)
153  { return armarx::human::arondto::HumanPose::FromAron(dict); };
154 
155  std::vector<armarx::human::arondto::HumanPose> humanPoses =
156  simox::alg::apply(dictSeq, toHumanPose);
157 
158  // synchronize robot
159  const std::string robotName =
160  humanPoses.front().keypoints.begin()->second.positionCamera.header.agent;
161 
162  const auto robot = getSynchronizedRobot(robotName, timestamp);
163  ARMARX_CHECK_NOT_NULL(robot) << "Failed to obtain robot with name `" << robotName << "`.";
164 
165  // fill global pose info
166  {
167  for (armarx::human::arondto::HumanPose& humanPose : humanPoses)
168  {
169  for (auto& [_, keyPoint] : humanPose.keypoints)
170  {
171  const auto& cameraFrame = keyPoint.positionCamera.header.frame;
172  const auto& agent = keyPoint.positionCamera.header.agent;
173 
174  const FramedPosition keypoint(
175  keyPoint.positionCamera.position, cameraFrame, agent);
176  // ARMARX_IMPORTANT << VAROUT(keyPoint.positionCamera.position.transpose());
177 
178  // global pose
179  {
180  keyPoint.positionGlobal = armarx::arondto::FramedPosition();
181  armarx::toAron(keyPoint.positionGlobal.value(),
182  armarx::FramedPosition(keypoint.toGlobalEigen(robot),
184  agent));
185  }
186 
187  // pose in root frame
188  {
189  keyPoint.positionRobot = armarx::arondto::FramedPosition();
190  armarx::toAron(keyPoint.positionRobot.value(),
191  armarx::FramedPosition(keypoint.toRootEigen(robot),
192  robot->getRootNode()->getName(),
193  agent));
194  }
195 
196  // Orientation information available.
197  if (keyPoint.orientationCamera.has_value())
198  {
199  ARMARX_CHECK_EQUAL(keyPoint.orientationCamera->header.frame,
200  keyPoint.positionCamera.header.frame);
201 
202  const FramedPose keypointPose(
203  keyPoint.orientationCamera->orientation.toRotationMatrix(),
204  keyPoint.positionCamera.position,
205  cameraFrame,
206  agent);
207 
208  // global pose
209  {
210  const Eigen::Isometry3f global_T_human(
211  keypointPose.toGlobalEigen(robot));
212 
213  keyPoint.orientationGlobal = armarx::arondto::FramedOrientation();
214 
216  keyPoint.orientationGlobal.value(),
217  armarx::FramedOrientation(Eigen::Matrix3f{global_T_human.linear()},
219  agent));
220  }
221 
222  // pose in root frame
223  {
224  const Eigen::Isometry3f robot_root_T_human(
225  keypointPose.toRootEigen(robot));
226 
227 
228  keyPoint.orientationRobot = armarx::arondto::FramedOrientation();
229 
230  armarx::toAron(keyPoint.orientationRobot.value(),
232  Eigen::Matrix3f{robot_root_T_human.linear()},
233  robot->getRootNode()->getName(),
234  agent));
235  }
236  }
237  }
238  }
239  }
240 
241  // commit human poses
242  commitHumanPoses(humanPoses, providerName, timestamp);
243  }
244 
245  void
247  const std::vector<::armarx::human::arondto::HumanPose>& humanPoses,
248  const std::string& providerName,
249  const armarx::DateTime& timestamp)
250  {
252 
253  const auto providerId = armem::MemoryID(
254  workingMemory().name(), human::PoseSegment::CORE_SEGMENT_NAME, providerName);
255  const auto entityID = providerId.withEntityName("human_poses").withTimestamp(timestamp);
256 
258  update.entityID = entityID;
259  update.sentTime = timestamp; // TODO this is not fully correct
260  update.referencedTime = timestamp;
261 
263  humanPoses.begin(),
264  humanPoses.end(),
265  std::back_inserter(update.instancesData),
266  [](const ::armarx::human::arondto::HumanPose& humanPose) -> armarx::aron::data::DictPtr
267  { return humanPose.toAron(); });
268 
269  commit.add(update);
270 
271  ARMARX_DEBUG << "Committing " << update << " at time " << timestamp;
272 
274  }
275 } // namespace armarx::armem::server::human_memory
armarx::armem::server::human::PersonInstanceSegment::defineProperties
virtual void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string &prefix="") override
Definition: PersonInstanceSegment.cpp:45
armarx::armem::server::human::Visu::connect
void connect(const viz::Client &arviz, DebugObserverInterfacePrx debugObserver=nullptr)
Definition: Visualization.cpp:53
armarx::armem::Commit
A bundle of updates to be sent to the memory.
Definition: Commit.h:89
framed.h
armarx::FramedPose
The FramedPose class.
Definition: FramedPose.h:258
armarx::armem::server::human::PoseSegment::CORE_SEGMENT_NAME
static const std::string CORE_SEGMENT_NAME
Definition: PoseSegment.h:51
DateTime.h
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
armarx::GlobalFrame
const std::string GlobalFrame
Definition: FramedPose.h:62
armarx::armem::server::human_memory::HumanMemory::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: HumanMemory.cpp:75
armarx::armem::server::human::facerecog::Segment::init
void init() override
Definition: Segment.cpp:58
armarx::armem::server::plugins::ReadWritePluginUser::setMemoryName
void setMemoryName(const std::string &memoryName)
Definition: ReadWritePluginUser.cpp:25
ice_conversions.h
armarx::ManagedIceObject::addPlugin
PluginT * addPlugin(const std::string prefix="", ParamsT &&...params)
Definition: ManagedIceObject.h:182
HumanMemory.h
armarx::DebugObserverComponentPluginUser::getDebugObserver
const DebugObserverInterfacePrx & getDebugObserver() const
Definition: DebugObserverComponentPlugin.cpp:113
armarx::FramedPosition::toRootEigen
Eigen::Vector3f toRootEigen(const SharedRobotInterfacePrx &referenceRobot) const
Definition: FramedPose.cpp:786
armarx::armem::server::plugins::ReadWritePluginUser::workingMemory
server::wm::Memory & workingMemory()
Definition: ReadWritePluginUser.cpp:106
armarx::FramedPosition::toGlobalEigen
Eigen::Vector3f toGlobalEigen(const SharedRobotInterfacePrx &referenceRobot) const
Definition: FramedPose.cpp:752
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::armem::server::human_memory
Definition: ComponentInterface.ice:12
armarx::armem::server::human::profile::Segment::init
void init() override
Definition: Segment.cpp:68
armarx::armem::server::human::Visu::init
void init()
Definition: Visualization.cpp:47
memory_definitions.h
armarx::armem::server::human_memory::HumanMemory::onExitComponent
void onExitComponent() override
Hook for subclass.
Definition: HumanMemory.cpp:105
armarx::FramedPose::toGlobalEigen
Eigen::Matrix4f toGlobalEigen(const SharedRobotInterfacePrx &referenceRobot) const
Definition: FramedPose.cpp:517
PoseSegment.h
FramedPose.h
armarx::armem::server::human_memory::HumanMemory::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: HumanMemory.cpp:47
armarx::armem::server::segment::SpecializedCoreSegment::init
virtual void init() override
Definition: SpecializedCoreSegment.cpp:49
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
armarx::FramedPosition
The FramedPosition class.
Definition: FramedPose.h:142
armarx::armem::EntityUpdate
An update of an entity for a specific point in time.
Definition: Commit.h:27
ice_conversions.h
armarx::armem::base::MemoryBase::addCoreSegment
CoreSegmentT & addCoreSegment(const std::string &name, aron::type::ObjectPtr coreSegmentType=nullptr, const std::vector< PredictionEngine > &predictionEngines={})
Add an empty core segment with the given name, type and prediction engines.
Definition: MemoryBase.h:259
armarx::armem::server::plugins::ReadWritePluginUser::iceAdapter
MemoryToIceAdapter & iceAdapter()
Definition: ReadWritePluginUser.cpp:112
armarx::armem::server::human::profile::Segment::defineProperties
void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string &prefix="") override
Definition: Segment.cpp:56
armarx::FramedOrientation
The FramedOrientation class.
Definition: FramedPose.h:199
armarx::armem::server::human_memory::HumanMemory::onInitComponent
void onInitComponent() override
Pure virtual hook for the subclass.
Definition: HumanMemory.cpp:81
armarx::armem::fromIce
void fromIce(const data::MemoryID &ice, MemoryID &id)
Definition: ice_conversions.cpp:31
armarx::armem::server::human::PoseSegment::defineProperties
virtual void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string &prefix="") override
Definition: PoseSegment.cpp:45
armarx::armem::server::human_memory::HumanMemory::getSynchronizedRobot
const VirtualRobot::RobotPtr & getSynchronizedRobot(const std::string &robotName, const armarx::DateTime &timestamp)
Definition: HumanMemory.cpp:110
armarx::armem::server::ltm::util::mongodb::detail::update
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:67
MemoryToIceAdapter.h
armarx::FramedPose::toRootEigen
Eigen::Matrix4f toRootEigen(const SharedRobotInterfacePrx &referenceRobot) const
Definition: FramedPose.cpp:551
armarx::armem::server::plugins::ReadWritePluginUser::commit
virtual data::CommitResult commit(const data::Commit &commit, const Ice::Current &=Ice::emptyCurrent) override
Definition: ReadWritePluginUser.cpp:48
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::aron::data::DictPtr
std::shared_ptr< Dict > DictPtr
Definition: Dict.h:41
armarx::armem::server::human_memory::HumanMemory::onDisconnectComponent
void onDisconnectComponent() override
Hook for subclass.
Definition: HumanMemory.cpp:100
armarx::Component::getConfigIdentifier
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition: Component.cpp:74
armarx::armem::server::human::facerecog::Segment::defineProperties
void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string &prefix="") override
Definition: Segment.cpp:49
GfxTL::Matrix3f
MatrixXX< 3, 3, float > Matrix3f
Definition: MatrixXX.h:600
armarx::VariantType::FramedOrientation
const VariantTypeId FramedOrientation
Definition: FramedPose.h:40
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:70
armarx::ArVizComponentPluginUser::arviz
armarx::viz::Client arviz
Definition: ArVizComponentPlugin.h:43
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::transform
auto transform(const Container< InputT, Alloc > &in, OutputT(*func)(InputT const &)) -> Container< OutputT, typename std::allocator_traits< Alloc >::template rebind_alloc< OutputT > >
Convenience function (with less typing) to transform a container of type InputT into the same contain...
Definition: algorithm.h:315
armarx::armem::server::human::IdentificationSegment::defineProperties
virtual void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string &prefix="") override
Definition: IdentificationSegment.cpp:42
armarx::toAron
void toAron(arondto::PackagePath &dto, const PackageFileLocation &bo)
armarx::VariantType::FramedPosition
const VariantTypeId FramedPosition
Definition: FramedPose.h:39
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::armem::server::human_memory::HumanMemory::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: HumanMemory.cpp:94
armarx::armem::server::human_memory::HumanMemory::HumanMemory
HumanMemory()
Definition: HumanMemory.cpp:63
armarx::armem::server::MemoryToIceAdapter::commitLocking
data::CommitResult commitLocking(const data::Commit &commitIce, Time timeArrived)
Definition: MemoryToIceAdapter.cpp:166
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18
armarx::armem::server::human_memory::HumanMemory::commitHumanPoses
void commitHumanPoses(const std::vector<::armarx::human::arondto::HumanPose > &humanPoses, const std::string &providerName, const armarx::DateTime &timestamp)
Definition: HumanMemory.cpp:246
armarx::armem::server::human_memory::HumanMemory::commitHumanPosesInCameraFrame
void commitHumanPosesInCameraFrame(const ::armarx::aron::data::dto::AronDictSeq &dictSeq, const ::std::string &providerName, const ::armarx::core::time::dto::DateTime &timestamp, const ::Ice::Current &=::Ice::emptyCurrent) override
Definition: HumanMemory.cpp:136
armarx::human::MemoryID
const armem::MemoryID MemoryID
Definition: memory_ids.cpp:29