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";
60  loadByObjectFinder(p.objectsPackage);
61  }
62  }
63 
64  void
66  {
67  this->arviz = arviz;
68 
69  floorVis.setArViz(arviz);
70  floorVis.updateFloorObject(*segmentPtr);
71  }
72 
73  void
74  Segment::loadByObjectFinder(const std::string& objectsPackage)
75  {
77  loadByObjectFinder(ObjectFinder(objectsPackage));
78  }
79 
80  void
82  {
83  this->objectFinder = finder;
85  }
86 
87  void
89  {
91 
92  const Time now = Time::Now();
93 
94  const bool checkPaths = false;
95  std::vector<ObjectInfo> infos = objectFinder.findAllObjects(checkPaths);
96 
97  const MemoryID providerID =
99  ARMARX_INFO << "Loading up to " << infos.size() << " object classes from '"
100  << objectFinder.getPackageName() << "' ...";
101  Commit commit;
102  for (ObjectInfo& info : infos)
103  {
104  ARMARX_TRACE;
105 
106  ARMARX_VERBOSE << info.idStr();
107  info.setLogError(false);
108 
109  EntityUpdate& update = commit.add();
110  update.entityID = providerID.withEntityName(info.id().str());
111  update.arrivedTime = update.referencedTime = update.sentTime = now;
112 
113  ARMARX_TRACE;
114  ARMARX_VERBOSE << "object class from info";
115  arondto::ObjectClass objectClass = objectClassFromInfo(info);
116  update.instancesData = {objectClass.toAron()};
117  }
118 
119  ARMARX_TRACE;
120 
121  ARMARX_INFO << "Loaded " << commit.updates.size() << " object classes from '"
122  << objectFinder.getPackageName() << "'.";
123  iceMemory.commitLocking(commit);
124  }
125 
126  void
127  Segment::visualizeClass(const MemoryID& entityID, bool showAABB, bool showOOBB)
128  {
130 
131  viz::Layer layerOrigin = arviz.layer("Origin");
132  layerOrigin.add(viz::Pose("Origin"));
133 
134  viz::Layer layerObject = arviz.layer("Class Model");
135  viz::Layer layerAABB = arviz.layer("Class AABB");
136  viz::Layer layerOOBB = arviz.layer("Class OOBB");
137 
138  if (segmentPtr)
139  {
140  try
141  {
142  std::optional<arondto::ObjectClass> aron = doLocked(
143  [this, &entityID]() {
144  return segmentPtr->findLatestInstanceDataAs<arondto::ObjectClass>(entityID,
145  0);
146  });
147  if (not aron.has_value())
148  {
149  return;
150  }
151 
152  if (not aron->simoxXmlPath.package.empty())
153  {
154  layerObject.add(viz::Object(entityID.str())
155  .file(aron->simoxXmlPath.package, aron->simoxXmlPath.path)
156  .pose(pose));
157  }
158 
159  if (showAABB)
160  {
161  layerAABB.add(viz::Box("AABB")
162  .pose(pose * simox::math::pose(aron->aabb.center))
163  .size(aron->aabb.extents)
164  .color(simox::Color::cyan(255, 64)));
165  }
166  if (showOOBB)
167  {
168  layerOOBB.add(viz::Box("OOBB")
169  .pose(pose * simox::math::pose(aron->oobb.center,
170  aron->oobb.orientation))
171  .size(aron->oobb.extents)
172  .color(simox::Color::lime(255, 64)));
173  }
174  }
175  catch (const armem::error::ArMemError& e)
176  {
177  ARMARX_INFO << "Failed to visualize object class " << entityID << "."
178  << "\nReason: " << e.what();
179  }
180  catch (const aron::error::AronException& e)
181  {
182  ARMARX_INFO << "Failed to visualize object class " << entityID << "."
183  << "\nReason: " << e.what();
184  }
185  }
186 
187  arviz.commit({layerObject, layerOrigin, layerAABB, layerOOBB});
188  }
189 
190  arondto::ObjectClass
192  {
193  namespace fs = std::filesystem;
194 
195  // ARMARX_VERBOSE << info.idStr();
196 
197  arondto::ObjectClass data;
198  toAron(data.id, info.id());
199 
200  auto setPathIfExists =
201  [](armarx::arondto::PackagePath& aron, const PackageFileLocation& location)
202  {
203  if (fs::is_regular_file(location.absolutePath))
204  {
205  toAron(aron, location);
206  }
207  else
208  {
209  toAron(aron, PackageFileLocation());
210  }
211  };
212  setPathIfExists(data.simoxXmlPath, info.simoxXML());
213  setPathIfExists(data.urdfPath, info.urdf());
214  setPathIfExists(data.sdfPath, info.sdf());
215  setPathIfExists(data.articulatedSimoxXmlPath, info.articulatedSimoxXML());
216  setPathIfExists(data.articulatedUrdfPath, info.articulatedUrdf());
217  setPathIfExists(data.articulatedSdfPath, info.articulatedSdf());
218  setPathIfExists(data.meshObjPath, info.wavefrontObj());
219  setPathIfExists(data.meshWrlPath, info.meshWrl());
220 
221  ARMARX_TRACE;
222  ARMARX_VERBOSE << "AABB";
223  auto aabb = info.loadAABB();
224  toAron(data.aabb, aabb ? aabb.value() : simox::AxisAlignedBoundingBox());
225 
226  ARMARX_TRACE;
227  auto oobb = info.loadOOBB();
228  ARMARX_VERBOSE << "OOBB";
229  toAron(data.oobb, oobb ? oobb.value() : simox::OrientedBoxf());
230 
231  if (auto recogNames = info.loadRecognizedNames())
232  {
233  data.names.recognized = recogNames.value();
234  }
235  if (auto spokenNames = info.loadSpokenNames())
236  {
237  data.names.spoken = spokenNames.value();
238  }
239 
240  return data;
241  }
242 
243  void
245  {
246  using namespace armarx::RemoteGui::Client;
247 
248  data.setup(segment);
249  visu.setup(segment);
250 
251  VBoxLayout layout;
252  layout.addChildren({data.group, visu.group});
253 
254  group = {};
255  group.setLabel("Class");
256  group.addChildren({layout, VSpacer()});
257  }
258 
259  void
261  {
262  data.update(segment);
263  visu.update(segment);
264  }
265 
266  void
268  {
269  using namespace armarx::RemoteGui::Client;
270 
271  reloadButton.setLabel("Reload");
272 
273  maxHistorySize.setValue(std::max(1, int(segment.properties.maxHistorySize)));
274  maxHistorySize.setRange(1, 1e6);
275  infiniteHistory.setValue(segment.properties.maxHistorySize == -1);
276 
277  GridLayout grid;
278  int row = 0;
279  grid.add(reloadButton, {row, 0}, {1, 2});
280  row++;
281  grid.add(Label("Max History Size"), {row, 0}).add(maxHistorySize, {row, 1});
282  row++;
283  grid.add(Label("Infinite History Size"), {row, 0}).add(infiniteHistory, {row, 1});
284  row++;
285 
286  group = {};
287  group.setLabel("Data");
288  group.addChild(grid);
289  }
290 
291  void
293  {
294  if (reloadButton.wasClicked())
295  {
296  // Core segment mutex will be locked on commit.
297  segment.loadByObjectFinder();
298  rebuild = true;
299  }
300  if (infiniteHistory.hasValueChanged() || maxHistorySize.hasValueChanged())
301  {
302  segment.doLocked(
303  [this, &segment]()
304  {
305  segment.properties.maxHistorySize =
306  infiniteHistory.getValue() ? -1 : maxHistorySize.getValue();
307  if (segment.segmentPtr)
308  {
309  segment.segmentPtr->setMaxHistorySize(
310  long(segment.properties.maxHistorySize));
311  }
312  });
313  }
314  }
315 
316  void
318  {
319  using namespace armarx::RemoteGui::Client;
320 
321  showComboBox = {};
322  showOptionsIndex.clear();
323  segment.segmentPtr->forEachEntity(
324  [this](const wm::Entity& entity)
325  {
326  std::stringstream option;
327  option << entity.id().entityName << " (" << entity.id().providerSegmentName << ")";
328  showComboBox.addOption(option.str());
329  showOptionsIndex.push_back(entity.id());
330  });
331  if (showOptionsIndex.empty())
332  {
333  showComboBox.addOption("<none>");
334  }
335  showButton.setLabel("Visualize Object Class");
336 
337  GridLayout grid;
338  int row = 0;
339  grid.add(showComboBox, {row, 0}, {1, 2});
340  row++;
341  grid.add(showButton, {row, 0}, {1, 2});
342  row++;
343 
344  group = {};
345  group.setLabel("Visualization");
346  group.addChild(grid);
347  }
348 
349  void
351  {
352  if (showButton.wasClicked())
353  {
354  const size_t index = static_cast<size_t>(showComboBox.getIndex());
355  if (/*index >= 0 &&*/ index < showOptionsIndex.size())
356  {
357  segment.visualizeClass(showOptionsIndex.at(index));
358  }
359  }
360  }
361 
362 } // 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:93
armarx::aron::error::AronException
A base class for aron exceptions.
Definition: Exception.h:42
armarx::viz::Client::commit
CommitResult commit(StagedCommit const &commit)
Definition: Client.cpp:80
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:180
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:95
armarx::armem::server::obj::clazz::Segment::RemoteGui::update
void update(Segment &segment)
Definition: Segment.cpp:260
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:244
armarx::armem::Commit
A bundle of updates to be sent to the memory.
Definition: Commit.h:89
armarx::armem::server::obj::clazz::FloorVis::defineProperties
void defineProperties(armarx::PropertyDefinitionsPtr defs, const std::string &prefix="")
Definition: FloorVis.cpp:18
armarx::ObjectFinder::findAllObjects
std::vector< ObjectInfo > findAllObjects(bool checkPaths=true) const
Definition: ObjectFinder.cpp:144
armarx::armem::server::obj::clazz
Definition: FloorVis.cpp:10
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:71
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
armarx::core::time::DateTime::Now
static DateTime Now()
Definition: DateTime.cpp:55
armarx::RemoteGui::Client::GridLayout::add
GridLayout & add(Widget const &child, Pos pos, Span span=Span{1, 1})
Definition: Widgets.cpp:412
armarx::viz::Object::file
Object & file(std::string const &project, std::string const &filename)
Definition: Elements.h:328
armarx::armem::server::obj::clazz::Segment::loadByObjectFinder
void loadByObjectFinder(const std::string &objectsPackage)
Definition: Segment.cpp:74
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:24
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:32
armarx::viz::Layer::add
void add(ElementT const &element)
Definition: Layer.h:29
armarx::armem::server::obj::clazz::Segment::RemoteGui::group
armarx::RemoteGui::Client::GroupBox group
Definition: Segment.h:67
armarx::armem::server::obj::clazz::Segment::RemoteGui::Data::setup
void setup(const Segment &segment)
Definition: Segment.cpp:267
armarx::armem::server::wm::Entity
Definition: memory_definitions.h:30
armarx::RemoteGui::Client::VSpacer
Definition: Widgets.h:204
armarx::armem::base::detail::ForEachEntityMixin::forEachEntity
bool forEachEntity(FunctionT &&func)
Definition: iteration_mixins.h:258
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:97
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:7
armarx::armem::server::obj::clazz::Segment::loadByObjectFinder
void loadByObjectFinder()
Definition: Segment.cpp:88
armarx::RemoteGui::Client::GridLayout
Definition: Widgets.h:186
armarx::RemoteGui::Client::ContainerWidget::addChildren
void addChildren(std::initializer_list< Widget > children)
Definition: Widgets.cpp:100
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
armarx::ObjectFinder
Used to find objects in the ArmarX objects repository [1] (formerly [2]).
Definition: ObjectFinder.h:23
armarx::armem::server::segment::detail::SegmentBase< server::wm::CoreSegment >::segmentPtr
server::wm::CoreSegment * segmentPtr
Definition: SpecializedSegment.h:54
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
armarx::armem::MemoryID::withProviderSegmentName
MemoryID withProviderSegmentName(const std::string &name) const
Definition: MemoryID.cpp:412
armarx::armem::server::obj::clazz::Segment
Definition: Segment.h:24
armarx::viz::Object
Definition: Elements.h:321
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
if
if(!yyvaluep)
Definition: Grammar.cpp:724
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:39
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:27
armarx::viz::Box
Definition: Elements.h:51
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:179
max
T max(T t1, T t2)
Definition: gdiam.h:48
armarx::armem::server::obj::clazz::Segment::RemoteGui::Data::update
void update(Segment &segment)
Definition: Segment.cpp:292
armarx::armem::base::detail::MemoryItem::id
MemoryID & id()
Definition: MemoryItem.h:27
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::RemoteGui::Client::GroupBox::setLabel
void setLabel(std::string const &text)
Definition: Widgets.cpp:395
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:62
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:350
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
GfxTL::Matrix4f
MatrixXX< 4, 4, float > Matrix4f
Definition: MatrixXX.h:601
armarx::armem::MemoryID::withEntityName
MemoryID withEntityName(const std::string &name) const
Definition: MemoryID.cpp:420
Exception.h
armarx::armem::Commit::add
EntityUpdate & add()
Definition: Commit.cpp:81
armarx::armem::server::segment::detail::SegmentBase< server::wm::CoreSegment >::iceMemory
MemoryToIceAdapter & iceMemory
Definition: SpecializedSegment.h:60
TimeUtil.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
PropertyDefinitionContainer.h
armarx::viz::ElementOps::pose
DerivedT & pose(Eigen::Matrix4f const &pose)
Definition: ElementOps.h:159
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:55
armarx::viz::ElementOps::color
DerivedT & color(Color color)
Definition: ElementOps.h:195
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:65
armarx::armem::server::obj::clazz::Segment::visualizeClass
void visualizeClass(const MemoryID &entityID, bool showAABB=true, bool showOOBB=true)
Definition: Segment.cpp:127
armarx::armem::server::obj::clazz::Segment::objectClassFromInfo
arondto::ObjectClass objectClassFromInfo(const ObjectInfo &info)
Definition: Segment.cpp:191
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:30
aron_conversions.h
armarx::viz::Client::layer
Layer layer(std::string const &name) const
Definition: Client.cpp:73
armarx::ObjectFinder::getPackageName
std::string getPackageName() const
Definition: ObjectFinder.cpp:32
armarx::viz::Client
Definition: Client.h:109
armarx::armem::server::segment::SpecializedCoreSegment::properties
Properties properties
Definition: SpecializedCoreSegment.h:64
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:317
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:37
armarx::armem::server::obj::clazz::Segment::Segment
Segment(armem::server::MemoryToIceAdapter &iceMemory)
Definition: Segment.cpp:25