Segment.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <filesystem>
4 #include <map>
5 #include <optional>
6 #include <string>
7 
8 #include <SimoxUtility/caching/CacheMap.h>
9 #include <SimoxUtility/shapes/OrientedBox.h>
10 
13 #include <RobotAPI/interface/core/RobotState.h>
14 #include <RobotAPI/interface/objectpose/ObjectPoseStorageInterface.h>
22 
23 #include "ArticulatedObjectVisu.h"
24 #include "Decay.h"
25 
26 namespace armarx::armem::arondto
27 {
28  class ObjectInstance;
29 }
30 
32 {
33 
35  {
36  public:
37  struct CommitStats
38  {
39  int numUpdated = 0;
40  };
41 
44  using ObjectPoseMap = std::map<ObjectID, ObjectPose>;
45 
46  struct Calibration
47  {
48  std::string robotName = "";
49  std::string robotNode = "Neck_2_Pitch";
50  float offset = 0.0f;
51 
53  const std::string& prefix = "calibration.");
54  };
55 
56 
57  public:
59  virtual ~Segment() override;
60 
61 
63  const std::string& prefix = "") override;
64  void init() override;
65  void connect(viz::Client arviz);
66 
67 
68  CommitStats commitObjectPoses(const std::string& providerName,
69  const objpose::data::ProvidedObjectPoseSeq& providedPoses,
70  const Calibration& calibration,
71  std::optional<Time> discardUpdatesUntil = std::nullopt);
72  void commitObjectPoses(const ObjectPoseSeq& objectPoses,
73  const std::string& providerName = "");
74 
75 
77  objpose::ObjectPoseMap getObjectPosesByProvider(const std::string& providerName,
78  const DateTime& now);
79 
80  wm::Entity* findObjectEntity(const ObjectID& objectID,
81  const std::string& providerName = "");
82  std::optional<simox::OrientedBoxf> getObjectOOBB(const ObjectID& id);
83 
84  objpose::ProviderInfo getProviderInfo(const std::string& providerName);
85 
86  objpose::AttachObjectToRobotNodeOutput
87  attachObjectToRobotNode(const objpose::AttachObjectToRobotNodeInput& input);
88  objpose::DetachObjectFromRobotNodeOutput
89  detachObjectFromRobotNode(const objpose::DetachObjectFromRobotNodeInput& input);
90  objpose::DetachAllObjectsFromRobotNodesOutput
91  detachAllObjectsFromRobotNodes(const objpose::DetachAllObjectsFromRobotNodesInput& input);
92 
93 
94  /**
95  * @brief If the object is attached to a robot node, update it according to the current robot state.
96  *
97  * If there is no attachement info in `objectPose` itself, the internal data
98  * structure `attachments` is queried. If an attachment is found there,
99  * it is written into the given `objectPose` (thus, it is "cached" in the
100  * info `objectPose`).
101  *
102  * @param synchronized Indicates whether the agent is already synchronized to the current time.
103  */
104  void updateAttachement(ObjectPose& objectPose,
106  bool& synchronized) const;
107 
108 
109  static ObjectPoseMap getLatestObjectPoses(const wm::CoreSegment& coreSeg);
111  static ObjectPose getLatestObjectPose(const wm::Entity& entity);
112 
113  static void getLatestObjectPoses(const wm::CoreSegment& coreSeg, ObjectPoseMap& out);
114  static void getLatestObjectPoses(const wm::ProviderSegment& provSeg, ObjectPoseMap& out);
115  static void getLatestObjectPose(const wm::Entity& entity, ObjectPose& out);
116 
117  static arondto::ObjectInstance getLatestInstanceData(const wm::Entity& entity);
118 
120 
121  static std::map<DateTime, ObjectPose>
122  getObjectPosesInRange(const wm::Entity& entity, const DateTime& start, const DateTime& end);
123 
124  private:
126 
127  void updateObjectPoses(ObjectPoseMap& objectPoses, const DateTime& now);
128  void updateObjectPoses(ObjectPoseMap& objectPoses,
129  const DateTime& now,
131  bool& agentSynchronized) const;
132  void updateObjectPose(ObjectPose& objectPose,
133  const DateTime& now,
135  bool& agentSynchronized) const;
136 
137 
138  ObjectPoseMap filterObjectPoses(const ObjectPoseMap& objectPoses) const;
139 
140 
141  void storeDetachedSnapshot(wm::Entity& entity,
142  const arondto::ObjectInstance& data,
143  Time now,
144  bool commitAttachedPose);
145 
146 
147  std::optional<wm::EntityInstance> findClassInstance(const ObjectID& objectID) const;
148 
149  friend struct DetachVisitor;
150 
151 
152  private:
153  void storeScene(const std::string& filename, const armarx::objects::Scene& scene);
154  std::optional<armarx::objects::Scene> loadScene(const std::string& filename);
155  std::optional<armarx::objects::Scene> loadScene(const std::filesystem::path& path);
156  std::optional<std::filesystem::path> resolveSceneFilepath(const std::string& filename);
157 
158  armarx::objects::Scene getSceneSnapshot() const;
159  void commitSceneSnapshot(const armarx::objects::Scene& scene, const std::string& sceneName);
160  void commitSceneSnapshotFromFilename(const std::string& filename, bool lockMemory);
161 
162 
163  public:
164  /// Loaded robot models identified by the robot name.
165  struct RobotsCache : public armarx::Logging
166  {
168  std::string fallbackName;
169 
170  std::map<std::string, VirtualRobot::RobotPtr> loaded;
171 
172  VirtualRobot::RobotPtr get(const std::string& robotName,
173  const std::string& providerName = "");
174  };
175 
177 
178 
179  objpose::ProviderInfoMap providers;
180 
181 
183 
184  /// Decay model.
186 
187 
188  private:
189  struct Properties
190  {
191  bool discardSnapshotsWhileAttached = true;
192 
193  /// Package containing the scene snapshots
194  std::string sceneSnapshotsPackage = armarx::ObjectFinder::DefaultObjectsPackageName;
195  std::string sceneSnapshotsDirectory = "scenes";
196  std::string sceneSnapshotsToLoad = "";
197 
198  std::vector<std::string> getSceneSnapshotsToLoad() const;
199  };
200 
201  Properties p;
202 
203 
204  /// Caches results of attempts to retrieve the OOBB from ArmarXObjects.
205  simox::caching::CacheMap<ObjectID, std::optional<simox::OrientedBoxf>> oobbCache;
206 
207  /// Class name -> dataset name.
208  simox::caching::CacheMap<std::string, std::string> classNameToDatasetCache;
209 
210  std::unique_ptr<CMakePackageFinder> finder;
211 
212  static const std::string timestampPlaceholder;
213 
214  public:
215  struct RemoteGui
216  {
218 
222 
226 
229 
230  void setup(const Segment& data);
231  void update(Segment& data);
232  };
233 
234 
235  private:
236  std::unique_ptr<ArticulatedObjectVisu> visu;
237  };
238 
239 } // namespace armarx::armem::server::obj::instance
armarx::armem::server::obj::instance::Segment::getProviderInfo
objpose::ProviderInfo getProviderInfo(const std::string &providerName)
Definition: Segment.cpp:711
Client.h
SpecializedSegment.h
armarx::armem::server::obj::instance::Segment::getObjectPoses
objpose::ObjectPoseMap getObjectPoses(const DateTime &now)
Definition: Segment.cpp:397
armarx::armem::robot_state::VirtualRobotReader
The VirtualRobotReader class.
Definition: VirtualRobotReader.h:40
armarx::ObjectID
A known object ID of the form "Dataset/ClassName" or "Dataset/ClassName/InstanceName".
Definition: ObjectID.h:11
armarx::ObjectFinder::DefaultObjectsPackageName
static const std::string DefaultObjectsPackageName
Definition: ObjectFinder.h:28
armarx::armem::server::obj::instance::Segment::getObjectOOBB
std::optional< simox::OrientedBoxf > getObjectOOBB(const ObjectID &id)
Definition: Segment.cpp:705
armarx::armem::server::obj::instance::Segment::providers
objpose::ProviderInfoMap providers
Definition: Segment.h:179
armarx::armem::server::segment::SpecializedCoreSegment
A base class for core segments.
Definition: SpecializedCoreSegment.h:20
armarx::armem::server::obj::instance::Segment::detachAllObjectsFromRobotNodes
objpose::DetachAllObjectsFromRobotNodesOutput detachAllObjectsFromRobotNodes(const objpose::DetachAllObjectsFromRobotNodesInput &input)
Definition: Segment.cpp:884
armarx::objpose::ObjectPoseSeq
std::vector< ObjectPose > ObjectPoseSeq
Definition: forward_declarations.h:20
armarx::armem::server::obj::instance::Segment::RemoteGui::loadButton
armarx::RemoteGui::Client::Button loadButton
Definition: Segment.h:221
armarx::armem::server::obj::instance::Segment::Calibration
Definition: Segment.h:46
armarx::armem::server::MemoryToIceAdapter
Helps connecting a Memory server to the Ice interface.
Definition: MemoryToIceAdapter.h:19
armarx::armem::server::obj::instance::Decay
Models decay of object localizations by decreasing the confidence the longer the object was not local...
Definition: Decay.h:19
armarx::armem::server::obj::instance::Segment::RobotsCache
Loaded robot models identified by the robot name.
Definition: Segment.h:165
Prediction.h
armarx::armem::server::obj::instance::Segment::RemoteGui::storeLoadLine
armarx::RemoteGui::Client::LineEdit storeLoadLine
Definition: Segment.h:219
Decay.h
armarx::armem::server::obj::instance::Segment::Calibration::offset
float offset
Definition: Segment.h:50
armarx::armem::server::obj::instance::Segment::objectFinder
ObjectFinder objectFinder
Definition: Segment.h:182
armarx::armem::server::obj::instance::Segment::init
void init() override
Definition: Segment.cpp:155
armarx::armem::articulated_object::ArticulatedObjects
armarx::armem::robot::Robots ArticulatedObjects
Definition: types.h:141
armarx::armem::server::obj::instance::Segment::RobotsCache::loaded
std::map< std::string, VirtualRobot::RobotPtr > loaded
Definition: Segment.h:170
armarx::armem::server::obj::instance::Segment::Calibration::robotName
std::string robotName
Definition: Segment.h:48
armarx::armem::server::wm::Entity
Definition: memory_definitions.h:30
armarx::armem::server::obj::instance::Segment::Calibration::robotNode
std::string robotNode
Definition: Segment.h:49
armarx::armem::server::obj::instance::Segment::attachObjectToRobotNode
objpose::AttachObjectToRobotNodeOutput attachObjectToRobotNode(const objpose::AttachObjectToRobotNodeInput &input)
Definition: Segment.cpp:731
armarx::armem::arondto
Definition: aron_conversions.h:17
armarx::armem::server::obj::instance
Definition: ArticulatedObjectVisu.cpp:16
memory_definitions.h
armarx::armem::server::obj::instance::Segment::connect
void connect(viz::Client arviz)
Definition: Segment.cpp:170
armarx::ObjectFinder
Used to find objects in the ArmarX objects repository [1] (formerly [2]).
Definition: ObjectFinder.h:23
armarx::armem::server::obj::instance::Segment::detachObjectFromRobotNode
objpose::DetachObjectFromRobotNodeOutput detachObjectFromRobotNode(const objpose::DetachObjectFromRobotNodeInput &input)
Definition: Segment.cpp:836
armarx::armem::server::obj::instance::Segment::updateAttachement
void updateAttachement(ObjectPose &objectPose, VirtualRobot::RobotPtr agent, bool &synchronized) const
If the object is attached to a robot node, update it according to the current robot state.
Definition: Segment.cpp:503
armarx::armem::server::obj::instance::Segment::RobotsCache::fallbackName
std::string fallbackName
Definition: Segment.h:168
armarx::armem::server::obj::instance::Segment::RobotsCache::get
VirtualRobot::RobotPtr get(const std::string &robotName, const std::string &providerName="")
Definition: Segment.cpp:1326
armarx::armem::server::obj::instance::Segment::ObjectPoseMap
std::map< ObjectID, ObjectPose > ObjectPoseMap
Definition: Segment.h:44
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::aron::input
ReaderT::InputType & input
Definition: rw.h:19
ObjectID.h
armarx::armem::server::obj::instance::Segment::RemoteGui::group
armarx::RemoteGui::Client::GroupBox group
Definition: Segment.h:217
armarx::RemoteGui::Client::GroupBox
Definition: Widgets.h:193
armarx::RemoteGui::Client::LineEdit
Definition: Widgets.h:40
armarx::armem::server::obj::instance::Segment::Calibration::defineProperties
void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string &prefix="calibration.")
Definition: Segment.cpp:47
filename
std::string filename
Definition: VisualizationRobot.cpp:84
armarx::armem::server::obj::instance::Segment::getArticulatedObjects
::armarx::armem::articulated_object::ArticulatedObjects getArticulatedObjects()
Definition: Segment.cpp:623
ObjectPose.h
armarx::armem::server::obj::instance::Segment::RemoteGui::discardSnapshotsWhileAttached
armarx::RemoteGui::Client::CheckBox discardSnapshotsWhileAttached
Definition: Segment.h:225
VirtualRobotReader.h
armarx::armem::server::obj::instance::Segment::Segment
Segment(server::MemoryToIceAdapter &iceMemory)
Definition: Segment.cpp:57
armarx::armem::server::obj::instance::Segment::decay
Decay decay
Decay model.
Definition: Segment.h:185
armarx::armem::server::obj::instance::Segment::CommitStats
Definition: Segment.h:37
armarx::armem::server::obj::instance::Segment::getLatestObjectPose
static ObjectPose getLatestObjectPose(const wm::Entity &entity)
Definition: Segment.cpp:557
armarx::armem::server::wm::ProviderSegment
Definition: memory_definitions.h:60
armarx::armem::server::obj::instance::Segment::RobotsCache::reader
robot_state::VirtualRobotReader * reader
Definition: Segment.h:167
armarx::armem::server::obj::instance::Segment::RemoteGui::update
void update(Segment &data)
Definition: Segment.cpp:1277
armarx::armem::server::obj::instance::Segment::RemoteGui::detachAllObjectsCommitAttachedPoseCheckBox
armarx::RemoteGui::Client::CheckBox detachAllObjectsCommitAttachedPoseCheckBox
Definition: Segment.h:228
armarx::objpose::ProvidedObjectPoseSeq
std::vector< ProvidedObjectPose > ProvidedObjectPoseSeq
Definition: forward_declarations.h:25
armarx::objects::Scene
Definition: Scene.h:56
armarx::armem::server::obj::instance::Segment::getObjectPosesByProvider
objpose::ObjectPoseMap getObjectPosesByProvider(const std::string &providerName, const DateTime &now)
Definition: Segment.cpp:405
armarx::armem::server::wm::CoreSegment
base::CoreSegmentBase
Definition: memory_definitions.h:86
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
forward_declarations.h
armarx::Logging
Base Class for all Logging classes.
Definition: Logging.h:232
armarx::armem::server::obj::instance::Segment::RemoteGui::infiniteHistory
armarx::RemoteGui::Client::CheckBox infiniteHistory
Definition: Segment.h:224
armarx::armem::server::obj::instance::Segment::getLatestObjectPoses
static ObjectPoseMap getLatestObjectPoses(const wm::CoreSegment &coreSeg)
Definition: Segment.cpp:541
armarx::armem::server::segment::detail::SegmentBase< server::wm::CoreSegment >::iceMemory
MemoryToIceAdapter & iceMemory
Definition: SpecializedSegment.h:60
armarx::armem::server::obj::instance::Segment::commitObjectPoses
CommitStats commitObjectPoses(const std::string &providerName, const objpose::data::ProvidedObjectPoseSeq &providedPoses, const Calibration &calibration, std::optional< Time > discardUpdatesUntil=std::nullopt)
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::armem::server::obj::instance::Segment::RemoteGui::setup
void setup(const Segment &data)
Definition: Segment.cpp:1217
armarx::armem::server::obj::instance::Segment::~Segment
virtual ~Segment() override
Definition: Segment.cpp:96
armarx::armem::server::obj::instance::Segment::RemoteGui
Definition: Segment.h:215
armarx::RemoteGui::Client::IntSpinBox
Definition: Widgets.h:69
armarx::armem::server::obj::instance::Segment::RemoteGui::detachAllObjectsButton
armarx::RemoteGui::Client::Button detachAllObjectsButton
Definition: Segment.h:227
armarx::RemoteGui::Client::CheckBox
Definition: Widgets.h:129
armarx::armem::server::obj::instance::Segment::ObjectPoseSeq
objpose::ObjectPoseSeq ObjectPoseSeq
Definition: Segment.h:43
armarx::RemoteGui::Client::Button
Definition: Widgets.h:120
armarx::armem::server::obj::instance::Segment
Definition: Segment.h:34
armarx::armem::server::obj::instance::Segment::RemoteGui::maxHistorySize
armarx::RemoteGui::Client::IntSpinBox maxHistorySize
Definition: Segment.h:223
armarx::armem::server::obj::instance::Segment::RemoteGui::storeButton
armarx::RemoteGui::Client::Button storeButton
Definition: Segment.h:220
armarx::viz::Client
Definition: Client.h:109
armarx::armem::server::obj::instance::Segment::getLatestInstanceData
static arondto::ObjectInstance getLatestInstanceData(const wm::Entity &entity)
Definition: Segment.cpp:608
armarx::armem::server::obj::instance::Segment::findObjectEntity
wm::Entity * findObjectEntity(const ObjectID &objectID, const std::string &providerName="")
Definition: Segment.cpp:415
armarx::armem::server::obj::instance::Segment::robots
RobotsCache robots
Definition: Segment.h:176
ArticulatedObjectVisu.h
armarx::armem::server::obj::instance::Segment::defineProperties
void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string &prefix="") override
Definition: Segment.cpp:101
armarx::armem::server::obj::instance::Segment::CommitStats::numUpdated
int numUpdated
Definition: Segment.h:39
armarx::objpose::ObjectPose
An object pose as stored by the ObjectPoseStorage.
Definition: ObjectPose.h:36
armarx::armem::server::obj::instance::Segment::DetachVisitor
friend struct DetachVisitor
Definition: Segment.h:149
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18
ObjectFinder.h
armarx::armem::server::obj::instance::Segment::getObjectPosesInRange
static std::map< DateTime, ObjectPose > getObjectPosesInRange(const wm::Entity &entity, const DateTime &start, const DateTime &end)
Definition: Segment.cpp:677
armarx::objpose::ObjectPoseMap
std::map< ObjectID, ObjectPose > ObjectPoseMap
Definition: forward_declarations.h:21