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>
11 
14 #include <RobotAPI/interface/core/RobotState.h>
15 #include <RobotAPI/interface/objectpose/ObjectPoseStorageInterface.h>
23 
24 #include "ArticulatedObjectVisu.h"
25 #include "Decay.h"
26 
27 namespace armarx::armem::arondto
28 {
29  class ObjectInstance;
30 }
31 
33 {
34 
36  {
37  public:
38  struct CommitStats
39  {
40  int numUpdated = 0;
41  };
42 
45  using ObjectPoseMap = std::map<ObjectID, ObjectPose>;
46 
47  struct Calibration
48  {
49  std::string robotName = "";
50  std::string robotNode = "Neck_2_Pitch";
51  float offset = 0.0f;
52 
54  const std::string& prefix = "calibration.");
55  };
56 
57 
58  public:
60  virtual ~Segment() override;
61 
62 
64  const std::string& prefix = "") override;
65  void init() override;
66  void connect(viz::Client arviz);
67 
68 
69  CommitStats commitObjectPoses(const std::string& providerName,
70  const objpose::data::ProvidedObjectPoseSeq& providedPoses,
71  const Calibration& calibration,
72  std::optional<Time> discardUpdatesUntil = std::nullopt);
73  void commitObjectPoses(const ObjectPoseSeq& objectPoses,
74  const std::string& providerName = "");
75 
76 
78  objpose::ObjectPoseMap getObjectPosesByProvider(const std::string& providerName,
79  const DateTime& now);
80 
81  wm::Entity* findObjectEntity(const ObjectID& objectID,
82  const std::string& providerName = "");
83  std::optional<simox::OrientedBoxf> getObjectOOBB(const ObjectID& id);
84 
85  objpose::ProviderInfo getProviderInfo(const std::string& providerName);
86 
87  objpose::AttachObjectToRobotNodeOutput
88  attachObjectToRobotNode(const objpose::AttachObjectToRobotNodeInput& input);
89  objpose::DetachObjectFromRobotNodeOutput
90  detachObjectFromRobotNode(const objpose::DetachObjectFromRobotNodeInput& input);
91  objpose::DetachAllObjectsFromRobotNodesOutput
92  detachAllObjectsFromRobotNodes(const objpose::DetachAllObjectsFromRobotNodesInput& input);
93 
94 
95  /**
96  * @brief If the object is attached to a robot node, update it according to the current robot state.
97  *
98  * If there is no attachement info in `objectPose` itself, the internal data
99  * structure `attachments` is queried. If an attachment is found there,
100  * it is written into the given `objectPose` (thus, it is "cached" in the
101  * info `objectPose`).
102  *
103  * @param synchronized Indicates whether the agent is already synchronized to the current time.
104  */
105  void updateAttachement(ObjectPose& objectPose,
107  bool& synchronized) const;
108 
109 
110  static ObjectPoseMap getLatestObjectPoses(const wm::CoreSegment& coreSeg);
112  static ObjectPose getLatestObjectPose(const wm::Entity& entity);
113 
114  static void getLatestObjectPoses(const wm::CoreSegment& coreSeg, ObjectPoseMap& out);
115  static void getLatestObjectPoses(const wm::ProviderSegment& provSeg, ObjectPoseMap& out);
116  static void getLatestObjectPose(const wm::Entity& entity, ObjectPose& out);
117 
118  static arondto::ObjectInstance getLatestInstanceData(const wm::Entity& entity);
119 
121 
122  static std::map<DateTime, ObjectPose>
123  getObjectPosesInRange(const wm::Entity& entity, const DateTime& start, const DateTime& end);
124 
125  private:
127 
128  void updateObjectPoses(ObjectPoseMap& objectPoses, const DateTime& now);
129  void updateObjectPoses(ObjectPoseMap& objectPoses,
130  const DateTime& now,
132  bool& agentSynchronized) const;
133  void updateObjectPose(ObjectPose& objectPose,
134  const DateTime& now,
136  bool& agentSynchronized) const;
137 
138 
139  ObjectPoseMap filterObjectPoses(const ObjectPoseMap& objectPoses) const;
140 
141 
142  void storeDetachedSnapshot(wm::Entity& entity,
143  const arondto::ObjectInstance& data,
144  Time now,
145  bool commitAttachedPose);
146 
147 
148  std::optional<wm::EntityInstance> findClassInstance(const ObjectID& objectID) const;
149 
150  friend struct DetachVisitor;
151 
152 
153  private:
154  void storeScene(const std::string& filename, const armarx::objects::Scene& scene);
155  std::optional<armarx::objects::Scene> loadScene(const std::string& filename);
156  std::optional<armarx::objects::Scene> loadScene(const std::filesystem::path& path);
157  std::optional<std::filesystem::path> resolveSceneFilepath(const std::string& filename);
158 
159  armarx::objects::Scene getSceneSnapshot() const;
160  void commitSceneSnapshot(const armarx::objects::Scene& scene, const std::string& sceneName);
161  void commitSceneSnapshotFromFilename(const std::string& filename, bool lockMemory);
162 
163 
164  public:
165  /// Loaded robot models identified by the robot name.
166  struct RobotsCache : public armarx::Logging
167  {
169  std::string fallbackName;
170 
171  std::map<std::string, VirtualRobot::RobotPtr> loaded;
172 
173  VirtualRobot::RobotPtr get(const std::string& robotName,
174  const std::string& providerName = "");
175  };
176 
178 
179 
180  objpose::ProviderInfoMap providers;
181 
182 
184 
185  /// Decay model.
187 
188 
189  private:
190  struct Properties
191  {
192  bool discardSnapshotsWhileAttached = true;
193 
194  /// Package containing the scene snapshots
195  std::string sceneSnapshotsPackage = armarx::ObjectFinder::DefaultObjectsPackageName;
196  std::string sceneSnapshotsDirectory = "scenes";
197  std::string sceneSnapshotsToLoad = "";
198 
199  bool autoReloadSceneSnapshotsOnFileChange = false;
200 
201  std::vector<std::string> getSceneSnapshotsToLoad() const;
202  };
203 
204  Properties p;
205 
206 
207  /// Caches results of attempts to retrieve the OOBB from ArmarXObjects.
208  simox::caching::CacheMap<ObjectID, std::optional<simox::OrientedBoxf>> oobbCache;
209 
210  /// Class name -> dataset name.
211  simox::caching::CacheMap<std::string, std::string> classNameToDatasetCache;
212 
213  std::unique_ptr<CMakePackageFinder> finder;
214 
215  static const std::string timestampPlaceholder;
216 
217  public:
218  struct RemoteGui
219  {
221 
225 
229 
232 
233  void setup(const Segment& data);
234  void update(Segment& data);
235  };
236 
237 
238  private:
239  std::unique_ptr<ArticulatedObjectVisu> visu;
240 
241  SimpleRunningTask<>::pointer_type fileWatcherTask;
242  };
243 
244 } // namespace armarx::armem::server::obj::instance
armarx::armem::server::obj::instance::Segment::getProviderInfo
objpose::ProviderInfo getProviderInfo(const std::string &providerName)
Definition: Segment.cpp:800
Client.h
SpecializedSegment.h
armarx::armem::server::obj::instance::Segment::getObjectPoses
objpose::ObjectPoseMap getObjectPoses(const DateTime &now)
Definition: Segment.cpp:486
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:794
armarx::armem::server::obj::instance::Segment::providers
objpose::ProviderInfoMap providers
Definition: Segment.h:180
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:973
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:224
armarx::armem::server::obj::instance::Segment::Calibration
Definition: Segment.h:47
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:166
Prediction.h
armarx::armem::server::obj::instance::Segment::RemoteGui::storeLoadLine
armarx::RemoteGui::Client::LineEdit storeLoadLine
Definition: Segment.h:222
Decay.h
armarx::armem::server::obj::instance::Segment::Calibration::offset
float offset
Definition: Segment.h:51
armarx::armem::server::obj::instance::Segment::objectFinder
ObjectFinder objectFinder
Definition: Segment.h:183
armarx::armem::server::obj::instance::Segment::init
void init() override
Definition: Segment.cpp:163
armarx::armem::server::obj::instance::Segment::RobotsCache::loaded
std::map< std::string, VirtualRobot::RobotPtr > loaded
Definition: Segment.h:171
armarx::armem::server::obj::instance::Segment::Calibration::robotName
std::string robotName
Definition: Segment.h:49
armarx::armem::server::wm::Entity
Definition: memory_definitions.h:30
armarx::armem::server::obj::instance::Segment::Calibration::robotNode
std::string robotNode
Definition: Segment.h:50
armarx::armem::server::obj::instance::Segment::attachObjectToRobotNode
objpose::AttachObjectToRobotNodeOutput attachObjectToRobotNode(const objpose::AttachObjectToRobotNodeInput &input)
Definition: Segment.cpp:820
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:259
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:925
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:592
armarx::armem::server::obj::instance::Segment::RobotsCache::fallbackName
std::string fallbackName
Definition: Segment.h:169
armarx::armem::server::obj::instance::Segment::RobotsCache::get
VirtualRobot::RobotPtr get(const std::string &robotName, const std::string &providerName="")
Definition: Segment.cpp:1416
armarx::armem::server::obj::instance::Segment::ObjectPoseMap
std::map< ObjectID, ObjectPose > ObjectPoseMap
Definition: Segment.h:45
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:220
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:52
filename
std::string filename
Definition: VisualizationRobot.cpp:83
armarx::armem::server::obj::instance::Segment::getArticulatedObjects
::armarx::armem::articulated_object::ArticulatedObjects getArticulatedObjects()
Definition: Segment.cpp:712
ObjectPose.h
armarx::armem::server::obj::instance::Segment::RemoteGui::discardSnapshotsWhileAttached
armarx::RemoteGui::Client::CheckBox discardSnapshotsWhileAttached
Definition: Segment.h:228
VirtualRobotReader.h
armarx::armem::server::obj::instance::Segment::Segment
Segment(server::MemoryToIceAdapter &iceMemory)
Definition: Segment.cpp:62
armarx::armem::server::obj::instance::Segment::decay
Decay decay
Decay model.
Definition: Segment.h:186
TaskUtil.h
armarx::armem::server::obj::instance::Segment::CommitStats
Definition: Segment.h:38
armarx::armem::server::obj::instance::Segment::getLatestObjectPose
static ObjectPose getLatestObjectPose(const wm::Entity &entity)
Definition: Segment.cpp:646
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:168
armarx::armem::server::obj::instance::Segment::RemoteGui::update
void update(Segment &data)
Definition: Segment.cpp:1367
armarx::armem::server::obj::instance::Segment::RemoteGui::detachAllObjectsCommitAttachedPoseCheckBox
armarx::RemoteGui::Client::CheckBox detachAllObjectsCommitAttachedPoseCheckBox
Definition: Segment.h:231
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:494
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::armem::articulated_object::ArticulatedObjects
armarx::armem::robot_state::Robots ArticulatedObjects
Definition: types.h:141
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:227
armarx::armem::server::obj::instance::Segment::getLatestObjectPoses
static ObjectPoseMap getLatestObjectPoses(const wm::CoreSegment &coreSeg)
Definition: Segment.cpp:630
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:1307
armarx::armem::server::obj::instance::Segment::~Segment
virtual ~Segment() override
Definition: Segment.cpp:101
armarx::armem::server::obj::instance::Segment::RemoteGui
Definition: Segment.h:218
armarx::RemoteGui::Client::IntSpinBox
Definition: Widgets.h:69
armarx::armem::server::obj::instance::Segment::RemoteGui::detachAllObjectsButton
armarx::RemoteGui::Client::Button detachAllObjectsButton
Definition: Segment.h:230
armarx::RemoteGui::Client::CheckBox
Definition: Widgets.h:129
armarx::armem::server::obj::instance::Segment::ObjectPoseSeq
objpose::ObjectPoseSeq ObjectPoseSeq
Definition: Segment.h:44
armarx::RemoteGui::Client::Button
Definition: Widgets.h:120
armarx::armem::server::obj::instance::Segment
Definition: Segment.h:35
armarx::armem::server::obj::instance::Segment::RemoteGui::maxHistorySize
armarx::RemoteGui::Client::IntSpinBox maxHistorySize
Definition: Segment.h:226
armarx::armem::server::obj::instance::Segment::RemoteGui::storeButton
armarx::RemoteGui::Client::Button storeButton
Definition: Segment.h:223
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:697
armarx::armem::server::obj::instance::Segment::findObjectEntity
wm::Entity * findObjectEntity(const ObjectID &objectID, const std::string &providerName="")
Definition: Segment.cpp:504
armarx::armem::server::obj::instance::Segment::robots
RobotsCache robots
Definition: Segment.h:177
ArticulatedObjectVisu.h
armarx::armem::server::obj::instance::Segment::defineProperties
void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string &prefix="") override
Definition: Segment.cpp:106
armarx::armem::server::obj::instance::Segment::CommitStats::numUpdated
int numUpdated
Definition: Segment.h:40
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:150
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:766
armarx::objpose::ObjectPoseMap
std::map< ObjectID, ObjectPose > ObjectPoseMap
Definition: forward_declarations.h:21