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
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 {
43 ARMARX_CHECK(objectPoseClient.isConnected());
44
45 // Fetch the latest poses.
46 const objpose::ObjectPoseSeq objectPoses = objectPoseClient.fetchObjectPoses();
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 {
67 simox::AxisAlignedBoundingBox aabb =
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();
76 update.entityID = indexSpatialProviderSegmentID.withEntityName(
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 =
86 objectPoseClient.getObjectFinder().findObject(objectPose.objectID);
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();
115 update.entityID = indexNamedProviderSegmentID.withEntityName(
116 objectInstanceID.getEntityID().str());
117 update.referencedTime = objectPose.timestamp;
118 update.instancesData = {named.toAron()};
119 }
120 }
121
122 // Commit.
123 indexSpatialMemoryWriter.commit(commit);
124 }
125
126 std::vector<const objpose::ObjectPose*>
127 ObjectInstanceToIndex::filterObjectPoses(const objpose::ObjectPoseSeq& objectPoses,
128 const std::vector<MemoryID>& updatedSnapshotIDs)
129 {
130 // Returns true to keep the item, false to skip it.
131 auto filter = [this, &updatedSnapshotIDs](const objpose::ObjectPose& objectPose)
132 {
133 auto it = state.latestUpdateDateTimes.find(objectPose.objectID);
134 if (it == state.latestUpdateDateTimes.end())
135 {
136 // Never encountered that before, commit it.
137 return true;
138 }
139
140 const armarx::DateTime& latestTime = it->second;
141 armarx::DateTime nextDueTime = latestTime + params.maxFrequency.toCycleDuration();
142 if (objectPose.timestamp < nextDueTime)
143 {
144 // Skip.
145 return false;
146 }
147
148 armem::MemoryID objectInstanceID =
150 bool found = false;
151 for (const MemoryID& updatedSnapshotID : updatedSnapshotIDs)
152 {
153 if (armem::contains(updatedSnapshotID, objectInstanceID))
154 {
155 found = true;
156 break;
157 }
158 }
159 return found;
160 };
161
162
163 std::vector<const objpose::ObjectPose*> filtered;
164 for (const objpose::ObjectPose& objectPose : objectPoses)
165 {
166 if (filter(objectPose))
167 {
168 filtered.push_back(&objectPose);
169 state.latestUpdateDateTimes[objectPose.objectID] = objectPose.timestamp;
170 }
171 }
172
173 return filtered;
174 }
175
176} // namespace armarx::armem::objects
std::string str(bool escapeDelimiters=true) const
Get a string representation of this memory ID.
Definition MemoryID.cpp:102
MemoryID getEntityID() const
Definition MemoryID.cpp:310
void fetchAndCommitObjectInstances(const std::vector< armem::MemoryID > &updatedObjectInstanceSnapshotIDs)
Represents a point in time.
Definition DateTime.h:25
Duration toCycleDuration() const
Definition Frequency.cpp:60
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
armem::MemoryID reconstructObjectInstanceID(const objpose::ObjectPose &objectPose)
Definition utils.cpp:104
bool contains(const MemoryID &general, const MemoryID &specific)
Indicates whether general is "less specific" than, or equal to, specific, i.e.
Definition MemoryID.cpp:563
void toAron(arondto::MemoryID &dto, const MemoryID &bo)
void toAron(T &dto, const T &bo)
Framework for converting ARON DTOs (Data Transfer Objects) to C++ BOs (Business Objects) and back.
std::vector< ObjectPose > ObjectPoseSeq
A bundle of updates to be sent to the memory.
Definition Commit.h:90
EntityUpdate & add()
Definition Commit.cpp:80
An update of an entity for a specific point in time.
Definition Commit.h:26
std::map< armarx::ObjectID, armarx::DateTime > latestUpdateDateTimes
An object pose as stored by the ObjectPoseStorage.
Definition ObjectPose.h:34
armarx::ObjectID objectID
The object ID, i.e. dataset, class name and instance name.
Definition ObjectPose.h:56
DateTime timestamp
Source timestamp.
Definition ObjectPose.h:98
std::optional< simox::OrientedBoxf > oobbGlobal() const
Get the OOBB in the global frame (according to objectPoseGlobal).