Segment.cpp
Go to the documentation of this file.
1 #include "Segment.h"
2 
3 #include <filesystem>
4 
5 #include <SimoxUtility/color/Color.h>
6 #include <SimoxUtility/math/pose/pose.h>
7 #include <SimoxUtility/shapes/AxisAlignedBoundingBox.h>
8 #include <SimoxUtility/shapes/OrientedBox.h>
9 
13 
16 #include <RobotAPI/libraries/armem_objects/aron/ObjectClass.aron.generated.h>
21 
23 {
24 
26  SpecializedCoreSegment(memoryToIceAdapter,
27  objects::classSegmentID.coreSegmentName,
28  arondto::ObjectClass::ToAronType(),
29  -1)
30  {
31  }
32 
34  {
35  }
36 
37  void
39  {
40  SpecializedCoreSegment::defineProperties(defs, prefix);
41 
42  defs->optional(p.objectsPackage,
43  prefix + "ObjectsPackage",
44  "Name of the objects package to load from.");
45  defs->optional(p.loadFromObjectsPackage,
46  prefix + "LoadFromObjectsPackage",
47  "If true, load the objects from the objects package on startup.");
48 
49  floorVis.defineProperties(defs, prefix + "Floor.");
50  }
51 
52  void
54  {
55  SpecializedCoreSegment::init();
56 
57  if (p.loadFromObjectsPackage)
58  {
59  ARMARX_VERBOSE << "Loading objects from package `" << p.objectsPackage << "`.";
60  loadByObjectFinder(p.objectsPackage);
61  }
62  }
63 
64  void
66  {
67  if (p.loadFromObjectsPackage)
68  {
69  ARMARX_VERBOSE << "Loading objects from package `" << p.objectsPackage << "`.";
70  loadByObjectFinder(p.objectsPackage);
71  }
72  else
73  {
74  ARMARX_INFO << "Loading object from package in `reloadObjectClassesByObjectFinder()` "
75  "is disabled via property. No object classes will be reloaded!";
76  }
77  }
78 
79  void
81  {
82  this->arviz = arviz;
83 
84  floorVis.setArViz(arviz);
85  floorVis.updateFloorObject(*segmentPtr);
86  }
87 
88  void
89  Segment::loadByObjectFinder(const std::string& objectsPackage)
90  {
92  loadByObjectFinder(ObjectFinder(objectsPackage));
93  }
94 
95  void
97  {
98  this->objectFinder = finder;
100  }
101 
102  void
104  {
105  ARMARX_TRACE;
106 
107  const Time now = Time::Now();
108 
109  const bool checkPaths = false;
110  std::vector<ObjectInfo> infos = objectFinder.findAllObjects(checkPaths);
111 
112  const MemoryID providerID =
114  ARMARX_INFO << "Loading up to " << infos.size() << " object classes from '"
115  << objectFinder.getPackageName() << "' ...";
116  Commit commit;
117  for (ObjectInfo& info : infos)
118  {
119  ARMARX_TRACE;
120 
121  ARMARX_VERBOSE << info.idStr();
122  info.setLogError(false);
123 
124  EntityUpdate& update = commit.add();
125  update.entityID = providerID.withEntityName(info.id().str());
126  update.arrivedTime = update.referencedTime = update.sentTime = now;
127 
128  ARMARX_TRACE;
129  ARMARX_VERBOSE << "object class from info";
130  arondto::ObjectClass objectClass = objectClassFromInfo(info);
131  update.instancesData = {objectClass.toAron()};
132  }
133 
134  ARMARX_TRACE;
135 
136  ARMARX_INFO << "Loaded " << commit.updates.size() << " object classes from '"
137  << objectFinder.getPackageName() << "'.";
138  iceMemory.commitLocking(commit);
139  }
140 
141  void
142  Segment::visualizeClass(const MemoryID& entityID, bool showAABB, bool showOOBB)
143  {
145 
146  viz::Layer layerOrigin = arviz.layer("Origin");
147  layerOrigin.add(viz::Pose("Origin"));
148 
149  viz::Layer layerObject = arviz.layer("Class Model");
150  viz::Layer layerAABB = arviz.layer("Class AABB");
151  viz::Layer layerOOBB = arviz.layer("Class OOBB");
152 
153  if (segmentPtr)
154  {
155  try
156  {
157  std::optional<arondto::ObjectClass> aron = doLocked(
158  [this, &entityID]() {
159  return segmentPtr->findLatestInstanceDataAs<arondto::ObjectClass>(entityID,
160  0);
161  });
162  if (not aron.has_value())
163  {
164  return;
165  }
166 
167  if (not aron->simoxXmlPath.package.empty())
168  {
169  layerObject.add(viz::Object(entityID.str())
170  .file(aron->simoxXmlPath.package, aron->simoxXmlPath.path)
171  .pose(pose));
172  }
173 
174  if (showAABB)
175  {
176  layerAABB.add(viz::Box("AABB")
177  .pose(pose * simox::math::pose(aron->aabb.center))
178  .size(aron->aabb.extents)
179  .color(simox::Color::cyan(255, 64)));
180  }
181  if (showOOBB)
182  {
183  layerOOBB.add(viz::Box("OOBB")
184  .pose(pose * simox::math::pose(aron->oobb.center,
185  aron->oobb.orientation))
186  .size(aron->oobb.extents)
187  .color(simox::Color::lime(255, 64)));
188  }
189  }
190  catch (const armem::error::ArMemError& e)
191  {
192  ARMARX_INFO << "Failed to visualize object class " << entityID << "."
193  << "\nReason: " << e.what();
194  }
195  catch (const aron::error::AronException& e)
196  {
197  ARMARX_INFO << "Failed to visualize object class " << entityID << "."
198  << "\nReason: " << e.what();
199  }
200  }
201 
202  arviz.commit({layerObject, layerOrigin, layerAABB, layerOOBB});
203  }
204 
205  arondto::ObjectClass
207  {
208  namespace fs = std::filesystem;
209 
210  // ARMARX_VERBOSE << info.idStr();
211 
212  arondto::ObjectClass data;
213  toAron(data.id, info.id());
214 
215  auto setPathIfExists =
216  [](armarx::arondto::PackagePath& aron, const PackageFileLocation& location)
217  {
218  if (fs::is_regular_file(location.absolutePath))
219  {
220  toAron(aron, location);
221  }
222  else
223  {
224  toAron(aron, PackageFileLocation());
225  }
226  };
227  setPathIfExists(data.simoxXmlPath, info.simoxXML());
228  setPathIfExists(data.urdfPath, info.urdf());
229  setPathIfExists(data.sdfPath, info.sdf());
230  setPathIfExists(data.articulatedSimoxXmlPath, info.articulatedSimoxXML());
231  setPathIfExists(data.articulatedUrdfPath, info.articulatedUrdf());
232  setPathIfExists(data.articulatedSdfPath, info.articulatedSdf());
233  setPathIfExists(data.meshObjPath, info.wavefrontObj());
234  setPathIfExists(data.meshWrlPath, info.meshWrl());
235 
236  ARMARX_TRACE;
237  ARMARX_VERBOSE << "AABB";
238  auto aabb = info.loadAABB();
239  toAron(data.aabb, aabb ? aabb.value() : simox::AxisAlignedBoundingBox());
240 
241  ARMARX_TRACE;
242  auto oobb = info.loadOOBB();
243  ARMARX_VERBOSE << "OOBB";
244  toAron(data.oobb, oobb ? oobb.value() : simox::OrientedBoxf());
245 
246  if (auto recogNames = info.loadRecognizedNames())
247  {
248  data.names.recognized = recogNames.value();
249  }
250  if (auto spokenNames = info.loadSpokenNames())
251  {
252  data.names.spoken = spokenNames.value();
253  }
254 
255  return data;
256  }
257 
258  void
260  {
261  using namespace armarx::RemoteGui::Client;
262 
263  data.setup(segment);
264  visu.setup(segment);
265 
266  VBoxLayout layout;
267  layout.addChildren({data.group, visu.group});
268 
269  group = {};
270  group.setLabel("Class");
271  group.addChildren({layout, VSpacer()});
272  }
273 
274  void
276  {
277  data.update(segment);
278  visu.update(segment);
279  }
280 
281  void
283  {
284  using namespace armarx::RemoteGui::Client;
285 
286  reloadButton.setLabel("Reload");
287 
288  maxHistorySize.setValue(std::max(1, int(segment.properties.maxHistorySize)));
289  maxHistorySize.setRange(1, 1e6);
290  infiniteHistory.setValue(segment.properties.maxHistorySize == -1);
291 
292  GridLayout grid;
293  int row = 0;
294  grid.add(reloadButton, {row, 0}, {1, 2});
295  row++;
296  grid.add(Label("Max History Size"), {row, 0}).add(maxHistorySize, {row, 1});
297  row++;
298  grid.add(Label("Infinite History Size"), {row, 0}).add(infiniteHistory, {row, 1});
299  row++;
300 
301  group = {};
302  group.setLabel("Data");
303  group.addChild(grid);
304  }
305 
306  void
308  {
309  if (reloadButton.wasClicked())
310  {
311  // Core segment mutex will be locked on commit.
312  segment.loadByObjectFinder();
313  rebuild = true;
314  }
315  if (infiniteHistory.hasValueChanged() || maxHistorySize.hasValueChanged())
316  {
317  segment.doLocked(
318  [this, &segment]()
319  {
320  segment.properties.maxHistorySize =
321  infiniteHistory.getValue() ? -1 : maxHistorySize.getValue();
322  if (segment.segmentPtr)
323  {
324  segment.segmentPtr->setMaxHistorySize(
325  long(segment.properties.maxHistorySize));
326  }
327  });
328  }
329  }
330 
331  void
333  {
334  using namespace armarx::RemoteGui::Client;
335 
336  showComboBox = {};
337  showOptionsIndex.clear();
338  segment.segmentPtr->forEachEntity(
339  [this](const wm::Entity& entity)
340  {
341  std::stringstream option;
342  option << entity.id().entityName << " (" << entity.id().providerSegmentName << ")";
343  showComboBox.addOption(option.str());
344  showOptionsIndex.push_back(entity.id());
345  });
346  if (showOptionsIndex.empty())
347  {
348  showComboBox.addOption("<none>");
349  }
350  showButton.setLabel("Visualize Object Class");
351 
352  GridLayout grid;
353  int row = 0;
354  grid.add(showComboBox, {row, 0}, {1, 2});
355  row++;
356  grid.add(showButton, {row, 0}, {1, 2});
357  row++;
358 
359  group = {};
360  group.setLabel("Visualization");
361  group.addChild(grid);
362  }
363 
364  void
366  {
367  if (showButton.wasClicked())
368  {
369  const size_t index = static_cast<size_t>(showComboBox.getIndex());
370  if (/*index >= 0 &&*/ index < showOptionsIndex.size())
371  {
372  segment.visualizeClass(showOptionsIndex.at(index));
373  }
374  }
375  }
376 
377 } // namespace armarx::armem::server::obj::clazz
memory_ids.h
armarx::armem::wm::detail::FindInstanceDataMixin::findLatestInstanceDataAs
std::optional< AronDtoT > findLatestInstanceDataAs(const MemoryID &entityID, int instanceIndex=0) const
Definition: data_lookup_mixins.h:83
armarx::armem::server::obj::clazz::Segment::reloadObjectClassesByObjectFinder
void reloadObjectClassesByObjectFinder()
Definition: Segment.cpp:65
armarx::aron::error::AronException
A base class for aron exceptions.
Definition: Exception.h:36
armarx::viz::Client::commit
CommitResult commit(StagedCommit const &commit)
Definition: Client.cpp:89
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:187
armarx::RemoteGui::Client::ContainerWidget::addChild
void addChild(Widget const &child)
Definition: Widgets.cpp:95
armarx::armem::server::obj::clazz::Segment::RemoteGui::visu
Visu visu
Definition: Segment.h:96
armarx::armem::server::obj::clazz::Segment::RemoteGui::update
void update(Segment &segment)
Definition: Segment.cpp:275
armarx::ObjectInfo::articulatedSdf
PackageFileLocation articulatedSdf() const
Definition: ObjectInfo.cpp:160
armarx::armem::MemoryID::providerSegmentName
std::string providerSegmentName
Definition: MemoryID.h:52
armarx::armem::server::obj::clazz::Segment::RemoteGui::setup
void setup(const Segment &segment)
Definition: Segment.cpp:259
armarx::armem::Commit
A bundle of updates to be sent to the memory.
Definition: Commit.h:84
armarx::armem::server::obj::clazz::FloorVis::defineProperties
void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string &prefix="")
Definition: FloorVis.cpp:17
armarx::ObjectFinder::findAllObjects
std::vector< ObjectInfo > findAllObjects(bool checkPaths=true) const
Definition: ObjectFinder.cpp:149
armarx::armem::server::obj::clazz
Definition: FloorVis.cpp:9
index
uint8_t index
Definition: EtherCATFrame.h:59
armarx::ObjectInfo::articulatedSimoxXML
PackageFileLocation articulatedSimoxXML() const
Definition: ObjectInfo.cpp:148
armarx::armem::server::obj::clazz::Segment::RemoteGui::Data::group
armarx::RemoteGui::Client::GroupBox group
Definition: Segment.h:70
armarx::RemoteGui::Client::VBoxLayout
Definition: Widgets.h:167
armarx::armem::server::MemoryToIceAdapter
Helps connecting a Memory server to the Ice interface.
Definition: MemoryToIceAdapter.h:19
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:650
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:51
armarx::RemoteGui::Client::GridLayout::add
GridLayout & add(Widget const &child, Pos pos, Span span=Span{1, 1})
Definition: Widgets.cpp:438
armarx::viz::Object::file
Object & file(std::string const &project, std::string const &filename)
Definition: Elements.h:340
armarx::armem::server::obj::clazz::Segment::loadByObjectFinder
void loadByObjectFinder(const std::string &objectsPackage)
Definition: Segment.cpp:89
armarx::armem::MemoryID::str
std::string str(bool escapeDelimiters=true) const
Get a string representation of this memory ID.
Definition: MemoryID.cpp:102
armarx::armem::toAron
void toAron(arondto::MemoryID &dto, const MemoryID &bo)
Definition: aron_conversions.cpp:19
armarx::armem::server::obj::clazz::Segment::RemoteGui::Visu::group
armarx::RemoteGui::Client::GroupBox group
Definition: Segment.h:86
armarx::armem::error::ArMemError
Base class for all exceptions thrown by the armem library.
Definition: ArMemError.h:18
armarx::ObjectInfo::wavefrontObj
PackageFileLocation wavefrontObj() const
Definition: ObjectInfo.cpp:193
armarx::armem::server::obj::clazz::FloorVis::setArViz
void setArViz(armarx::viz::Client arviz)
Definition: FloorVis.cpp:23
armarx::ObjectInfo::articulatedUrdf
PackageFileLocation articulatedUrdf() const
Definition: ObjectInfo.cpp:154
armarx::PackageFileLocation
Definition: ObjectInfo.h:22
armarx::ObjectInfo::urdf
PackageFileLocation urdf() const
Definition: ObjectInfo.cpp:115
armarx::armem::objects::classSegmentID
const MemoryID classSegmentID
Definition: memory_ids.cpp:31
armarx::viz::Layer::add
void add(ElementT const &element)
Definition: Layer.h:31
armarx::armem::server::obj::clazz::Segment::RemoteGui::group
armarx::RemoteGui::Client::GroupBox group
Definition: Segment.h:66
armarx::armem::server::obj::clazz::Segment::RemoteGui::Data::setup
void setup(const Segment &segment)
Definition: Segment.cpp:282
armarx::armem::server::wm::Entity
Definition: memory_definitions.h:27
armarx::RemoteGui::Client::VSpacer
Definition: Widgets.h:204
armarx::armem::base::detail::ForEachEntityMixin::forEachEntity
bool forEachEntity(FunctionT &&func)
Definition: iteration_mixins.h:259
armarx::ObjectInfo::loadRecognizedNames
std::optional< std::vector< std::string > > loadRecognizedNames() const
Load names to use when matched when recognizing an object by name.
Definition: ObjectInfo.cpp:326
armarx::armem::server::obj::clazz::Segment::init
void init() override
Definition: Segment.cpp:53
armarx::armem::Commit::updates
std::vector< EntityUpdate > updates
The entity updates.
Definition: Commit.h:92
armarx::aron::simox::arondto::AxisAlignedBoundingBox
::simox::arondto::AxisAlignedBoundingBox AxisAlignedBoundingBox
Definition: simox.h:14
visionx::voxelgrid::Label
uint32_t Label
Type of an object label.
Definition: types.h:6
armarx::armem::server::obj::clazz::Segment::loadByObjectFinder
void loadByObjectFinder()
Definition: Segment.cpp:103
armarx::RemoteGui::Client::GridLayout
Definition: Widgets.h:186
armarx::RemoteGui::Client::ContainerWidget::addChildren
void addChildren(std::initializer_list< Widget > children)
Definition: Widgets.cpp:101
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:77
armarx::ObjectFinder
Used to find objects in the ArmarX objects repository [1] (formerly [2]).
Definition: ObjectFinder.h:22
armarx::armem::server::segment::detail::SegmentBase< server::wm::CoreSegment >::segmentPtr
server::wm::CoreSegment * segmentPtr
Definition: SpecializedSegment.h:53
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:570
armarx::armem::MemoryID::withProviderSegmentName
MemoryID withProviderSegmentName(const std::string &name) const
Definition: MemoryID.cpp:417
armarx::armem::server::obj::clazz::Segment
Definition: Segment.h:23
armarx::viz::Object
Definition: Elements.h:332
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
if
if(!yyvaluep)
Definition: Grammar.cpp:645
armarx::ObjectInfo::sdf
PackageFileLocation sdf() const
Definition: ObjectInfo.cpp:121
data
uint8_t data[1]
Definition: EtherCATFrame.h:68
armarx::armem::server::segment::SpecializedCoreSegment::doLocked
auto doLocked(FunctionT &&function) const
Definition: SpecializedCoreSegment.h:38
armarx::armem::server::obj::clazz::Segment::RemoteGui::data
Data data
Definition: Segment.h:82
armarx::armem::EntityUpdate
An update of an entity for a specific point in time.
Definition: Commit.h:25
armarx::viz::Box
Definition: Elements.h:47
armarx::armem::server::obj::clazz::Segment::defineProperties
void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string &prefix="") override
Definition: Segment.cpp:38
armarx::viz::Pose
Definition: Elements.h:178
max
T max(T t1, T t2)
Definition: gdiam.h:51
armarx::armem::server::obj::clazz::Segment::RemoteGui::Data::update
void update(Segment &segment)
Definition: Segment.cpp:307
armarx::armem::base::detail::MemoryItem::id
MemoryID & id()
Definition: MemoryItem.h:25
armarx::armem::server::ltm::util::mongodb::detail::update
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:68
MemoryToIceAdapter.h
armarx::RemoteGui::Client::GroupBox::setLabel
void setLabel(std::string const &text)
Definition: Widgets.cpp:420
armarx::ObjectInfo::simoxXML
PackageFileLocation simoxXML() const
Definition: ObjectInfo.cpp:109
Segment.h
aron_conversions.h
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::armem::server::segment::SpecializedCoreSegment::Properties::maxHistorySize
int64_t maxHistorySize
Definition: SpecializedCoreSegment.h:59
armarx::armem::server::obj::clazz::Segment::~Segment
virtual ~Segment() override
Definition: Segment.cpp:33
option
#define option(type, fn)
armarx::armem::server::obj::clazz::Segment::RemoteGui::Visu::update
void update(Segment &segment)
Definition: Segment.cpp:365
armarx::ObjectInfo::loadOOBB
std::optional< simox::OrientedBox< float > > loadOOBB() const
Load the OOBB (object-oriented bounding box) from the bounding box JSON file.
Definition: ObjectInfo.cpp:268
armarx::armem::MemoryID::withEntityName
MemoryID withEntityName(const std::string &name) const
Definition: MemoryID.cpp:425
Exception.h
armarx::armem::Commit::add
EntityUpdate & add()
Definition: Commit.cpp:80
armarx::armem::server::segment::detail::SegmentBase< server::wm::CoreSegment >::iceMemory
MemoryToIceAdapter & iceMemory
Definition: SpecializedSegment.h:58
TimeUtil.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:181
PropertyDefinitionContainer.h
armarx::viz::ElementOps::pose
DerivedT & pose(Eigen::Matrix4f const &pose)
Definition: ElementOps.h:176
armarx::ObjectInfo::loadAABB
std::optional< simox::AxisAlignedBoundingBox > loadAABB() const
Load the AABB (axis-aligned bounding-box) from the bounding box JSON file.
Definition: ObjectInfo.cpp:211
IceUtil::Handle< class PropertyDefinitionContainer >
armarx::viz::Box::size
Box & size(Eigen::Vector3f const &s)
Definition: Elements.h:52
armarx::viz::ElementOps::color
DerivedT & color(Color color)
Definition: ElementOps.h:218
armarx::ObjectInfo::loadSpokenNames
std::optional< std::vector< std::string > > loadSpokenNames() const
Load names to use when verbalizing an object name.
Definition: ObjectInfo.cpp:332
armarx::ObjectInfo::id
ObjectID id() const
Return "dataset/name".
Definition: ObjectInfo.cpp:67
armarx::ObjectInfo::meshWrl
PackageFileLocation meshWrl() const
Definition: ObjectInfo.cpp:187
armarx::armem::server::obj::clazz::Segment::connect
void connect(viz::Client arviz)
Definition: Segment.cpp:80
armarx::armem::server::obj::clazz::Segment::visualizeClass
void visualizeClass(const MemoryID &entityID, bool showAABB=true, bool showOOBB=true)
Definition: Segment.cpp:142
armarx::armem::server::obj::clazz::Segment::objectClassFromInfo
arondto::ObjectClass objectClassFromInfo(const ObjectInfo &info)
Definition: Segment.cpp:206
Logging.h
armarx::armem::server::obj::clazz::FloorVis::updateFloorObject
void updateFloorObject(const wm::CoreSegment &classCoreSegment)
Draw a the floor as a simox object.
Definition: FloorVis.cpp:29
aron_conversions.h
armarx::viz::Client::layer
Layer layer(std::string const &name) const
Definition: Client.cpp:80
armarx::ObjectFinder::getPackageName
std::string getPackageName() const
Definition: ObjectFinder.cpp:30
armarx::viz::Client
Definition: Client.h:117
armarx::armem::server::segment::SpecializedCoreSegment::properties
Properties properties
Definition: SpecializedCoreSegment.h:62
armarx::RemoteGui::Client
Definition: EigenWidgets.cpp:8
armarx::viz::Layer
Definition: Layer.h:12
aron_conversions.h
armarx::armem::server::obj::clazz::Segment::RemoteGui::Visu::setup
void setup(const Segment &segment)
Definition: Segment.cpp:332
armarx::armem::server::MemoryToIceAdapter::commitLocking
data::CommitResult commitLocking(const data::Commit &commitIce, Time timeArrived)
Definition: MemoryToIceAdapter.cpp:166
armarx::ObjectInfo
Accessor for the object files.
Definition: ObjectInfo.h:36
armarx::armem::server::obj::clazz::Segment::Segment
Segment(armem::server::MemoryToIceAdapter &iceMemory)
Definition: Segment.cpp:25