ObjectInstanceToIndex.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package RobotAPI::ArmarXObjects::RobotAndObjectToIndex
17  * @author Rainer Kartmann ( rainer dot kartmann at kit dot edu )
18  * @date 2022
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #include "ObjectInstanceToIndex.h"
24 
25 #include <SimoxUtility/shapes/AxisAlignedBoundingBox.h>
26 #include <SimoxUtility/shapes/OrientedBox.h>
27 
29 #include <RobotAPI/libraries/armem_index/aron/Named.aron.generated.h>
30 #include <RobotAPI/libraries/armem_index/aron/Spatial.aron.generated.h>
35 
37 {
38 
39  void
41  const std::vector<armem::MemoryID>& updatedObjectInstanceSnapshotIDs)
42  {
44 
45  // Fetch the latest poses.
47 
48  const auto filtered = filterObjectPoses(objectPoses, updatedObjectInstanceSnapshotIDs);
49 
50 
51  // Prepare the commit.
52 
53  armem::Commit commit;
54 
55  for (const objpose::ObjectPose* objectPosePtr : filtered)
56  {
57  const objpose::ObjectPose& objectPose = *objectPosePtr;
58 
59  armem::MemoryID objectInstanceID =
61 
62  // Spatial
63 
64  std::optional<simox::OrientedBoxf> oobb = objectPose.oobbGlobal();
65  if (oobb.has_value())
66  {
68  simox::AxisAlignedBoundingBox::from_points(oobb->corners());
69 
70  armem::index::arondto::Spatial spatial;
71  armem::toAron(spatial.id, objectInstanceID);
72  aron::toAron(spatial.oobbGlobal, oobb);
73  toAron(spatial.aabbGlobal, aabb);
74 
75  armem::EntityUpdate& update = commit.add();
77  objectInstanceID.getEntityID().str());
78  update.referencedTime = objectPose.timestamp;
79  update.instancesData = {spatial.toAron()};
80  }
81 
82  // Named
83 
84  // Load object class information.
85  std::optional<ObjectInfo> info =
87  if (info.has_value())
88  {
89  std::optional<std::vector<std::string>> recognized, spoken;
90  recognized = info->loadRecognizedNames();
91  spoken = info->loadSpokenNames();
92 
93  armem::index::arondto::Named named;
94  armem::toAron(named.id, objectInstanceID);
95 
96  if (recognized.has_value())
97  {
98  named.names.recognized = recognized.value();
99  }
100  else
101  {
102  named.names.recognized = {info->className()};
103  }
104 
105  if (spoken.has_value())
106  {
107  named.names.spoken = spoken.value();
108  }
109  else
110  {
111  named.names.spoken = {info->className()};
112  }
113 
114  armem::EntityUpdate& update = commit.add();
116  objectInstanceID.getEntityID().str());
117  update.referencedTime = objectPose.timestamp;
118  update.instancesData = {named.toAron()};
119  }
120  }
121 
122  // Commit.
124  }
125 
126 
127  std::vector<const objpose::ObjectPose*>
128  ObjectInstanceToIndex::filterObjectPoses(const objpose::ObjectPoseSeq& objectPoses,
129  const std::vector<MemoryID>& updatedSnapshotIDs)
130  {
131  // Returns true to keep the item, false to skip it.
132  auto filter = [this, &updatedSnapshotIDs](const objpose::ObjectPose& objectPose)
133  {
134  auto it = state.latestUpdateDateTimes.find(objectPose.objectID);
135  if (it == state.latestUpdateDateTimes.end())
136  {
137  // Never encountered that before, commit it.
138  return true;
139  }
140 
141  const armarx::DateTime& latestTime = it->second;
142  armarx::DateTime nextDueTime = latestTime + params.maxFrequency.toCycleDuration();
143  if (objectPose.timestamp < nextDueTime)
144  {
145  // Skip.
146  return false;
147  }
148 
149  armem::MemoryID objectInstanceID =
151  bool found = false;
152  for (const MemoryID& updatedSnapshotID : updatedSnapshotIDs)
153  {
154  if (armem::contains(updatedSnapshotID, objectInstanceID))
155  {
156  found = true;
157  break;
158  }
159  }
160  return found;
161  };
162 
163 
164  std::vector<const objpose::ObjectPose*> filtered;
165  for (const objpose::ObjectPose& objectPose : objectPoses)
166  {
167  if (filter(objectPose))
168  {
169  filtered.push_back(&objectPose);
170  state.latestUpdateDateTimes[objectPose.objectID] = objectPose.timestamp;
171  }
172  }
173 
174  return filtered;
175  }
176 
177 } // namespace armarx::armem::objects
memory_ids.h
armarx::objpose::ObjectPoseClient::fetchObjectPoses
ObjectPoseSeq fetchObjectPoses() const
Fetch all known object poses.
Definition: ObjectPoseClient.cpp:39
armarx::armem::objects::ObjectInstanceToIndex::indexSpatialProviderSegmentID
armem::MemoryID indexSpatialProviderSegmentID
Definition: ObjectInstanceToIndex.h:52
armarx::objpose::ObjectPoseSeq
std::vector< ObjectPose > ObjectPoseSeq
Definition: forward_declarations.h:20
armarx::armem::Commit
A bundle of updates to be sent to the memory.
Definition: Commit.h:89
armarx::armem::objects::ObjectInstanceToIndex::Parameters::maxFrequency
armarx::Frequency maxFrequency
Definition: ObjectInstanceToIndex.h:58
armarx::armem::objects::ObjectInstanceToIndex::params
Parameters params
Definition: ObjectInstanceToIndex.h:60
stl.h
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::contains
bool contains(const MemoryID &general, const MemoryID &specific)
Indicates whether general is "less specific" than, or equal to, specific, i.e.
Definition: MemoryID.cpp:558
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::objpose::ObjectPoseClient::getObjectFinder
const ObjectFinder & getObjectFinder() const
Get the internal object finder.
Definition: ObjectPoseClient.cpp:111
armarx::aron::simox::arondto::AxisAlignedBoundingBox
::simox::arondto::AxisAlignedBoundingBox AxisAlignedBoundingBox
Definition: simox.h:14
armarx::armem::objects::ObjectInstanceToIndex::fetchAndCommitObjectInstances
void fetchAndCommitObjectInstances(const std::vector< armem::MemoryID > &updatedObjectInstanceSnapshotIDs)
Definition: ObjectInstanceToIndex.cpp:40
armarx::objpose::ObjectPose::oobbGlobal
std::optional< simox::OrientedBoxf > oobbGlobal() const
Get the OOBB in the global frame (according to objectPoseGlobal).
Definition: ObjectPose.cpp:230
armarx::core::time::Frequency::toCycleDuration
Duration toCycleDuration() const
Definition: Frequency.cpp:67
armarx::armem::MemoryID
A memory ID.
Definition: MemoryID.h:47
armarx::armem::objects::ObjectInstanceToIndex::state
State state
Definition: ObjectInstanceToIndex.h:66
armarx::armem::EntityUpdate
An update of an entity for a specific point in time.
Definition: Commit.h:27
armarx::armem::client::Writer::commit
CommitResult commit(const Commit &commit) const
Writes a Commit to the memory.
Definition: Writer.cpp:59
armarx::armem::objects::ObjectInstanceToIndex::indexNamedProviderSegmentID
armem::MemoryID indexNamedProviderSegmentID
Definition: ObjectInstanceToIndex.h:53
simox.h
armarx::armem::objects
Definition: ObjectInstanceToIndex.cpp:36
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
aron_conversions.h
armarx::core::time::DateTime
Represents a point in time.
Definition: DateTime.h:24
armarx::armem::MemoryID::getEntityID
MemoryID getEntityID() const
Definition: MemoryID.cpp:305
armarx::objpose::ObjectPose::objectID
armarx::ObjectID objectID
The object ID, i.e. dataset, class name and instance name.
Definition: ObjectPose.h:58
armarx::armem::MemoryID::withEntityName
MemoryID withEntityName(const std::string &name) const
Definition: MemoryID.cpp:420
armarx::armem::Commit::add
EntityUpdate & add()
Definition: Commit.cpp:81
armarx::aron::toAron
void toAron(T &dto, const T &bo)
Framework for converting ARON DTOs (Data Transfer Objects) to C++ BOs (Business Objects) and back.
Definition: aron_conversions.h:74
armarx::ObjectFinder::findObject
std::optional< ObjectInfo > findObject(const std::string &dataset, const std::string &name) const
Definition: ObjectFinder.cpp:65
armarx::armem::objects::ObjectInstanceToIndex::indexSpatialMemoryWriter
armem::client::Writer indexSpatialMemoryWriter
Definition: ObjectInstanceToIndex.h:50
armarx::armem::objects::ObjectInstanceToIndex::objectPoseClient
objpose::ObjectPoseClient objectPoseClient
Definition: ObjectInstanceToIndex.h:49
armarx::armem::objects::reconstructObjectInstanceID
armem::MemoryID reconstructObjectInstanceID(const objpose::ObjectPose &objectPose)
Definition: utils.cpp:34
armarx::armem::objects::ObjectInstanceToIndex::State::latestUpdateDateTimes
std::map< armarx::ObjectID, armarx::DateTime > latestUpdateDateTimes
Definition: ObjectInstanceToIndex.h:64
ObjectInstanceToIndex.h
armarx::objpose::ObjectPoseClient::isConnected
bool isConnected() const
Indicate whether this client is connected to an object pose storage.
Definition: ObjectPoseClient.cpp:32
utils.h
armarx::objpose::ObjectPose
An object pose as stored by the ObjectPoseStorage.
Definition: ObjectPose.h:36
armarx::objpose::ObjectPose::timestamp
DateTime timestamp
Source timestamp.
Definition: ObjectPose.h:100
armarx::human::MemoryID
const armem::MemoryID MemoryID
Definition: memory_ids.cpp:29