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 
46 {
47 
50  {
53 
54  setMemoryName("Human");
55 
56  profileSegment.defineProperties(defs, "profile.");
57  personInstanceSegment.defineProperties(defs, "instance");
58  poseSegment.defineProperties(defs, "pose.");
59  faceRecognitionSegment.defineProperties(defs, "face.");
60  identificationSegment.defineProperties(defs, "ident.");
61 
62  return defs;
63  }
64 
66  profileSegment(iceAdapter()),
67  personInstanceSegment(iceAdapter()),
68  poseSegment(iceAdapter()),
69  faceRecognitionSegment(iceAdapter()),
70  identificationSegment(iceAdapter()),
71  visu(poseSegment)
72  {
73  addPlugin(virtualRobotReaderPlugin);
74  }
75 
76  std::string
78  {
79  return "HumanMemory";
80  }
81 
82  void
84  {
85  profileSegment.init();
86  personInstanceSegment.init();
87  poseSegment.init();
88  faceRecognitionSegment.init();
89  identificationSegment.init();
90  visu.init();
91 
92  workingMemory().addCoreSegment("RecognizedAction");
93  }
94 
95  void
97  {
99  }
100 
101  void
103  {
104  }
105 
106  void
108  {
109  }
110 
112  HumanMemory::getSynchronizedRobot(const std::string& robotName,
113  const armarx::DateTime& timestamp)
114  {
115  // seach and retrieve robot
116  auto& robot = [&]() -> VirtualRobot::RobotPtr&
117  {
118  // retrieve robot from memory if it is not in cache
119  if (robots.count(robotName) == 0)
120  {
121  auto newRobot = virtualRobotReaderPlugin->get().getRobot(robotName);
122  ARMARX_CHECK_NOT_NULL(newRobot)
123  << "Failed to obtain robot with name `" << robotName << "`.";
124 
125  robots[robotName] = newRobot;
126  }
127 
128  return robots.at(robotName);
129  }();
130 
131  // synchronize robot
132  ARMARX_CHECK(virtualRobotReaderPlugin->get().synchronizeRobot(*robot, timestamp));
133 
134  return robot;
135  }
136 
137  void
139  const ::armarx::aron::data::dto::AronDictSeq& dictSeq,
140  const ::std::string& providerName,
141  const ::armarx::core::time::dto::DateTime& iceTimestamp,
142  const ::Ice::Current& /*c*/)
143  {
144  if (dictSeq.empty())
145  {
146  return;
147  }
148 
149  // convert from ice
150  armarx::DateTime timestamp;
151  fromIce(iceTimestamp, timestamp);
152 
153  // convert to our aron type
154  const auto toHumanPose = [](const ::armarx::aron::data::dto::DictPtr& dict)
155  { return armarx::human::arondto::HumanPose::FromAron(dict); };
156 
157  std::vector<armarx::human::arondto::HumanPose> humanPoses =
158  simox::alg::apply(dictSeq, toHumanPose);
159 
160  // synchronize robot
161  const std::string robotName =
162  humanPoses.front().keypoints.begin()->second.positionCamera.header.agent;
163 
164  const auto robot = getSynchronizedRobot(robotName, timestamp);
165  ARMARX_CHECK_NOT_NULL(robot) << "Failed to obtain robot with name `" << robotName << "`.";
166 
167  // fill global pose info
168  {
169  for (armarx::human::arondto::HumanPose& humanPose : humanPoses)
170  {
171  for (auto& [_, keyPoint] : humanPose.keypoints)
172  {
173  const auto& cameraFrame = keyPoint.positionCamera.header.frame;
174  const auto& agent = keyPoint.positionCamera.header.agent;
175 
176  const FramedPosition keypoint(
177  keyPoint.positionCamera.position, cameraFrame, agent);
178  // ARMARX_IMPORTANT << VAROUT(keyPoint.positionCamera.position.transpose());
179 
180  // global pose
181  {
182  keyPoint.positionGlobal = armarx::arondto::FramedPosition();
183  armarx::toAron(keyPoint.positionGlobal.value(),
184  armarx::FramedPosition(keypoint.toGlobalEigen(robot),
186  agent));
187  }
188 
189  // pose in root frame
190  {
191  keyPoint.positionRobot = armarx::arondto::FramedPosition();
192  armarx::toAron(keyPoint.positionRobot.value(),
193  armarx::FramedPosition(keypoint.toRootEigen(robot),
194  robot->getRootNode()->getName(),
195  agent));
196  }
197 
198  // Orientation information available.
199  if (keyPoint.orientationCamera.has_value())
200  {
201  ARMARX_CHECK_EQUAL(keyPoint.orientationCamera->header.frame,
202  keyPoint.positionCamera.header.frame);
203 
204  const FramedPose keypointPose(
205  keyPoint.orientationCamera->orientation.toRotationMatrix(),
206  keyPoint.positionCamera.position,
207  cameraFrame,
208  agent);
209 
210  // global pose
211  {
212  const Eigen::Isometry3f global_T_human(
213  keypointPose.toGlobalEigen(robot));
214 
215  keyPoint.orientationGlobal = armarx::arondto::FramedOrientation();
216 
218  keyPoint.orientationGlobal.value(),
219  armarx::FramedOrientation(Eigen::Matrix3f{global_T_human.linear()},
221  agent));
222  }
223 
224  // pose in root frame
225  {
226  const Eigen::Isometry3f robot_root_T_human(
227  keypointPose.toRootEigen(robot));
228 
229 
230  keyPoint.orientationRobot = armarx::arondto::FramedOrientation();
231 
232  armarx::toAron(keyPoint.orientationRobot.value(),
234  Eigen::Matrix3f{robot_root_T_human.linear()},
235  robot->getRootNode()->getName(),
236  agent));
237  }
238  }
239  }
240  }
241  }
242 
243  // commit human poses
244  commitHumanPoses(humanPoses, providerName, timestamp);
245  }
246 
247  void
249  const std::vector<::armarx::human::arondto::HumanPose>& humanPoses,
250  const std::string& providerName,
251  const armarx::DateTime& timestamp)
252  {
254 
255  const auto providerId = armem::MemoryID(
256  workingMemory().name(), human::PoseSegment::CORE_SEGMENT_NAME, providerName);
257  const auto entityID = providerId.withEntityName("human_poses").withTimestamp(timestamp);
258 
260  update.entityID = entityID;
261  update.sentTime = timestamp; // TODO this is not fully correct
262  update.referencedTime = timestamp;
263 
265  humanPoses.begin(),
266  humanPoses.end(),
267  std::back_inserter(update.instancesData),
268  [](const ::armarx::human::arondto::HumanPose& humanPose) -> armarx::aron::data::DictPtr
269  { return humanPose.toAron(); });
270 
271  commit.add(update);
272 
273  ARMARX_DEBUG << "Committing " << update << " at time " << timestamp;
274 
276  }
277 } // 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:43
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:77
armarx::armem::server::human::facerecog::Segment::init
void init() override
Definition: Segment.cpp:59
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:46
memory_definitions.h
armarx::armem::server::human_memory::HumanMemory::onExitComponent
void onExitComponent() override
Hook for subclass.
Definition: HumanMemory.cpp:107
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:49
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:83
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:112
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:102
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:96
armarx::armem::server::human_memory::HumanMemory::HumanMemory
HumanMemory()
Definition: HumanMemory.cpp:65
armarx::armem::server::MemoryToIceAdapter::commitLocking
data::CommitResult commitLocking(const data::Commit &commitIce, Time timeArrived)
Definition: MemoryToIceAdapter.cpp:163
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:248
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:138
armarx::human::MemoryID
const armem::MemoryID MemoryID
Definition: memory_ids.cpp:29