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 
12 
15 #include <RobotAPI/libraries/armem_objects/aron/ObjectClass.aron.generated.h>
20 
22 {
23 
25  SpecializedCoreSegment(memoryToIceAdapter,
26  objects::classSegmentID.coreSegmentName,
27  arondto::ObjectClass::ToAronType(),
28  -1)
29  {
30  }
31 
33  {
34  }
35 
36  void
38  {
39  SpecializedCoreSegment::defineProperties(defs, prefix);
40 
41  defs->optional(p.objectsPackage,
42  prefix + "ObjectsPackage",
43  "Name of the objects package to load from.");
44  defs->optional(p.loadFromObjectsPackage,
45  prefix + "LoadFromObjectsPackage",
46  "If true, load the objects from the objects package on startup.");
47 
48  floorVis.defineProperties(defs, prefix + "Floor.");
49  }
50 
51  void
53  {
54  SpecializedCoreSegment::init();
55 
56  if (p.loadFromObjectsPackage)
57  {
58  loadByObjectFinder(p.objectsPackage);
59  }
60  }
61 
62  void
64  {
65  this->arviz = arviz;
66 
67  floorVis.setArViz(arviz);
68  floorVis.updateFloorObject(*segmentPtr);
69  }
70 
71  void
72  Segment::loadByObjectFinder(const std::string& objectsPackage)
73  {
74  loadByObjectFinder(ObjectFinder(objectsPackage));
75  }
76 
77  void
79  {
80  this->objectFinder = finder;
82  }
83 
84  void
86  {
87  const Time now = Time::Now();
88 
89  const bool checkPaths = false;
90  std::vector<ObjectInfo> infos = objectFinder.findAllObjects(checkPaths);
91 
92  const MemoryID providerID =
94  ARMARX_INFO << "Loading up to " << infos.size() << " object classes from '"
95  << objectFinder.getPackageName() << "' ...";
96  Commit commit;
97  for (ObjectInfo& info : infos)
98  {
99  info.setLogError(false);
100 
101  EntityUpdate& update = commit.add();
102  update.entityID = providerID.withEntityName(info.id().str());
103  update.arrivedTime = update.referencedTime = update.sentTime = now;
104 
105  arondto::ObjectClass objectClass = objectClassFromInfo(info);
106  update.instancesData = {objectClass.toAron()};
107  }
108  ARMARX_INFO << "Loaded " << commit.updates.size() << " object classes from '"
109  << objectFinder.getPackageName() << "'.";
110  iceMemory.commitLocking(commit);
111  }
112 
113  void
114  Segment::visualizeClass(const MemoryID& entityID, bool showAABB, bool showOOBB)
115  {
117 
118  viz::Layer layerOrigin = arviz.layer("Origin");
119  layerOrigin.add(viz::Pose("Origin"));
120 
121  viz::Layer layerObject = arviz.layer("Class Model");
122  viz::Layer layerAABB = arviz.layer("Class AABB");
123  viz::Layer layerOOBB = arviz.layer("Class OOBB");
124 
125  if (segmentPtr)
126  {
127  try
128  {
129  std::optional<arondto::ObjectClass> aron = doLocked(
130  [this, &entityID]() {
131  return segmentPtr->findLatestInstanceDataAs<arondto::ObjectClass>(entityID,
132  0);
133  });
134  if (not aron.has_value())
135  {
136  return;
137  }
138 
139  if (not aron->simoxXmlPath.package.empty())
140  {
141  layerObject.add(viz::Object(entityID.str())
142  .file(aron->simoxXmlPath.package, aron->simoxXmlPath.path)
143  .pose(pose));
144  }
145 
146  if (showAABB)
147  {
148  layerAABB.add(viz::Box("AABB")
149  .pose(pose * simox::math::pose(aron->aabb.center))
150  .size(aron->aabb.extents)
151  .color(simox::Color::cyan(255, 64)));
152  }
153  if (showOOBB)
154  {
155  layerOOBB.add(viz::Box("OOBB")
156  .pose(pose * simox::math::pose(aron->oobb.center,
157  aron->oobb.orientation))
158  .size(aron->oobb.extents)
159  .color(simox::Color::lime(255, 64)));
160  }
161  }
162  catch (const armem::error::ArMemError& e)
163  {
164  ARMARX_INFO << "Failed to visualize object class " << entityID << "."
165  << "\nReason: " << e.what();
166  }
167  catch (const aron::error::AronException& e)
168  {
169  ARMARX_INFO << "Failed to visualize object class " << entityID << "."
170  << "\nReason: " << e.what();
171  }
172  }
173 
174  arviz.commit({layerObject, layerOrigin, layerAABB, layerOOBB});
175  }
176 
177  arondto::ObjectClass
179  {
180  namespace fs = std::filesystem;
181 
182  arondto::ObjectClass data;
183  toAron(data.id, info.id());
184 
185  auto setPathIfExists =
186  [](armarx::arondto::PackagePath& aron, const PackageFileLocation& location)
187  {
188  if (fs::is_regular_file(location.absolutePath))
189  {
190  toAron(aron, location);
191  }
192  else
193  {
194  toAron(aron, PackageFileLocation());
195  }
196  };
197  setPathIfExists(data.simoxXmlPath, info.simoxXML());
198  setPathIfExists(data.urdfPath, info.urdf());
199  setPathIfExists(data.sdfPath, info.sdf());
200  setPathIfExists(data.articulatedSimoxXmlPath, info.articulatedSimoxXML());
201  setPathIfExists(data.articulatedUrdfPath, info.articulatedUrdf());
202  setPathIfExists(data.articulatedSdfPath, info.articulatedSdf());
203  setPathIfExists(data.meshObjPath, info.wavefrontObj());
204  setPathIfExists(data.meshWrlPath, info.meshWrl());
205 
206  auto aabb = info.loadAABB();
207  toAron(data.aabb, aabb ? aabb.value() : simox::AxisAlignedBoundingBox());
208  auto oobb = info.loadOOBB();
209  toAron(data.oobb, oobb ? oobb.value() : simox::OrientedBoxf());
210 
211  if (auto recogNames = info.loadRecognizedNames())
212  {
213  data.names.recognized = recogNames.value();
214  }
215  if (auto spokenNames = info.loadSpokenNames())
216  {
217  data.names.spoken = spokenNames.value();
218  }
219 
220  return data;
221  }
222 
223  void
225  {
226  using namespace armarx::RemoteGui::Client;
227 
228  data.setup(segment);
229  visu.setup(segment);
230 
231  VBoxLayout layout;
232  layout.addChildren({data.group, visu.group});
233 
234  group = {};
235  group.setLabel("Class");
236  group.addChildren({layout, VSpacer()});
237  }
238 
239  void
241  {
242  data.update(segment);
243  visu.update(segment);
244  }
245 
246  void
248  {
249  using namespace armarx::RemoteGui::Client;
250 
251  reloadButton.setLabel("Reload");
252 
253  maxHistorySize.setValue(std::max(1, int(segment.properties.maxHistorySize)));
254  maxHistorySize.setRange(1, 1e6);
255  infiniteHistory.setValue(segment.properties.maxHistorySize == -1);
256 
257  GridLayout grid;
258  int row = 0;
259  grid.add(reloadButton, {row, 0}, {1, 2});
260  row++;
261  grid.add(Label("Max History Size"), {row, 0}).add(maxHistorySize, {row, 1});
262  row++;
263  grid.add(Label("Infinite History Size"), {row, 0}).add(infiniteHistory, {row, 1});
264  row++;
265 
266  group = {};
267  group.setLabel("Data");
268  group.addChild(grid);
269  }
270 
271  void
273  {
274  if (reloadButton.wasClicked())
275  {
276  // Core segment mutex will be locked on commit.
277  segment.loadByObjectFinder();
278  rebuild = true;
279  }
280  if (infiniteHistory.hasValueChanged() || maxHistorySize.hasValueChanged())
281  {
282  segment.doLocked(
283  [this, &segment]()
284  {
285  segment.properties.maxHistorySize =
286  infiniteHistory.getValue() ? -1 : maxHistorySize.getValue();
287  if (segment.segmentPtr)
288  {
289  segment.segmentPtr->setMaxHistorySize(
290  long(segment.properties.maxHistorySize));
291  }
292  });
293  }
294  }
295 
296  void
298  {
299  using namespace armarx::RemoteGui::Client;
300 
301  showComboBox = {};
302  showOptionsIndex.clear();
303  segment.segmentPtr->forEachEntity(
304  [this](const wm::Entity& entity)
305  {
306  std::stringstream option;
307  option << entity.id().entityName << " (" << entity.id().providerSegmentName << ")";
308  showComboBox.addOption(option.str());
309  showOptionsIndex.push_back(entity.id());
310  });
311  if (showOptionsIndex.empty())
312  {
313  showComboBox.addOption("<none>");
314  }
315  showButton.setLabel("Visualize Object Class");
316 
317  GridLayout grid;
318  int row = 0;
319  grid.add(showComboBox, {row, 0}, {1, 2});
320  row++;
321  grid.add(showButton, {row, 0}, {1, 2});
322  row++;
323 
324  group = {};
325  group.setLabel("Visualization");
326  group.addChild(grid);
327  }
328 
329  void
331  {
332  if (showButton.wasClicked())
333  {
334  const size_t index = static_cast<size_t>(showComboBox.getIndex());
335  if (/*index >= 0 &&*/ index < showOptionsIndex.size())
336  {
337  segment.visualizeClass(showOptionsIndex.at(index));
338  }
339  }
340  }
341 
342 } // 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::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:240
armarx::ObjectInfo::articulatedSdf
PackageFileLocation articulatedSdf() const
Definition: ObjectInfo.cpp:134
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:224
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:124
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:72
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:166
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:129
armarx::PackageFileLocation
Definition: ObjectInfo.h:22
armarx::ObjectInfo::urdf
PackageFileLocation urdf() const
Definition: ObjectInfo.cpp:94
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:247
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:261
armarx::armem::server::obj::clazz::Segment::init
void init() override
Definition: Segment.cpp:52
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:85
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::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:99
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:37
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:272
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:89
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:32
option
#define option(type, fn)
armarx::armem::server::obj::clazz::Segment::RemoteGui::Visu::update
void update(Segment &segment)
Definition: Segment.cpp:330
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:214
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:181
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:266
armarx::ObjectInfo::id
ObjectID id() const
Return "dataset/name".
Definition: ObjectInfo.cpp:52
armarx::ObjectInfo::meshWrl
PackageFileLocation meshWrl() const
Definition: ObjectInfo.cpp:161
armarx::armem::server::obj::clazz::Segment::connect
void connect(viz::Client arviz)
Definition: Segment.cpp:63
armarx::armem::server::obj::clazz::Segment::visualizeClass
void visualizeClass(const MemoryID &entityID, bool showAABB=true, bool showOOBB=true)
Definition: Segment.cpp:114
armarx::armem::server::obj::clazz::Segment::objectClassFromInfo
static arondto::ObjectClass objectClassFromInfo(const ObjectInfo &info)
Definition: Segment.cpp:178
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:297
armarx::armem::server::MemoryToIceAdapter::commitLocking
data::CommitResult commitLocking(const data::Commit &commitIce, Time timeArrived)
Definition: MemoryToIceAdapter.cpp:163
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:24