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::ObjectInstanceToIndex
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 <Eigen/Geometry>
26
27#include <SimoxUtility/math/pose/pose.h>
28#include <SimoxUtility/shapes/AxisAlignedBoundingBox.h>
29
31
37#include <RobotAPI/libraries/armem_index/aron/Spatial.aron.generated.h>
39#include <RobotAPI/libraries/armem_objects/aron/ObjectInstance.aron.generated.h>
41#include <RobotAPI/libraries/armem_robot_state/aron/RobotState.aron.generated.h>
45
46namespace armarx
47{
48
51 {
54
55 defs->optional(properties.object.maxFrequencyHz, "p.object.maxFrequency");
56
57 return defs;
58 }
59
61 {
62 addPlugin(objectClientPlugin);
63 }
64
65 std::string
67 {
68 return "ObjectInstanceToIndex";
69 }
70
71 void
73 {
74 using This = ObjectInstanceToIndex;
75 // This should not be necessary but seems to be. ToDo: Look into this.
77
78 memoryNameSystem().subscribe(armem::robot_state::memoryID, this, &This::processRobotState);
80 armem::objects::instaceSegmentID, this, &This::processObjectInstance);
81 }
82
83 void
85 {
86 try
87 {
88 indexSpatialMemoryWriter = memoryNameSystem().useWriter(armem::index::spatialSegmentID);
89 }
91 {
92 ARMARX_ERROR << e.what();
93 }
94
95 {
98 }
99 }
100
101 void
105
106 void
110
111 void
113 {
114 using namespace armarx::RemoteGui::Client;
115
116 VBoxLayout root = {VSpacer()};
117 RemoteGui_createTab(getName(), root, &tab);
118 }
119
120 void
124
125 void
126 ObjectInstanceToIndex::processRobotState(const armem::MemoryID& id,
127 const std::vector<armem::MemoryID>& snapshotIDs)
128 {
129#if 0
130 if (not robotMemoryReader)
131 {
132 try
133 {
134 robotMemoryReader = memoryNameSystem().getReader(objectInstanceSegmentID);
135 }
137 {
138 ARMARX_INFO << e.what();
139 return;
140 }
141 }
142 ARMARX_CHECK(robotMemoryReader);
143
144 // Get only the latest snapshot per entity.
145 std::map<armem::MemoryID, const armem::MemoryID*> entityToSnapshot;
146 for (const armem::MemoryID& snapshotID : snapshotIDs)
147 {
148 const armem::MemoryID entityID = snapshotID.getEntityID();
149 if (auto it = entityToSnapshot.find(entityID); it != entityToSnapshot.end())
150 {
151 if (it->second->timestamp < snapshotID.timestamp)
152 {
153 entityToSnapshot[entityID] = &snapshotID;
154 }
155 }
156 else
157 {
158 entityToSnapshot[entityID] = &snapshotID;
159 }
160 }
161
162 // Query them.
163 std::vector<armem::MemoryID> queryIDs;
164 for (const auto& [_, snapshotID] : entityToSnapshot)
165 {
166 queryIDs.push_back(*snapshotID);
167 }
168
169 armem::client::QueryResult result = robotMemoryReader.queryMemoryIDs(queryIDs);
170 if (result.success)
171 {
172 // Prepare the commit.
173
174 armem::MemoryID provSegID = indexSpatialSegmentID.withProviderSegmentName(getName());
175
176 armem::Commit commit;
177
178 result.memory.forEachInstance(
179 [&commit, &provSegID](const armem::wm::EntityInstance& instance)
180 {
181 const armem::arondto::ObjectInstance data = instance.dataAs<armem::arondto::ObjectInstance>();
182
183 armem::index::arondto::Spatial spatial;
184 armem::toAron(spatial.id, instance.id());
185 spatial.oobbGlobal;
186 spatial.aabbGlobal;
187
188 armem::EntityUpdate& update = commit.add();
189 update.entityID = provSegID.withEntityName(instance.id().str());
190 update.referencedTime = instance.id().timestamp;
191 update.instancesData = { spatial.toAron() };
192
193 return true;
194 });
195 }
196 else
197 {
198 ARMARX_INFO << result.errorMessage;
199 }
200#endif
201 }
202
203 void
204 ObjectInstanceToIndex::processObjectInstance(const armem::MemoryID& id,
205 const std::vector<armem::MemoryID>& snapshotIDs)
206 {
207 std::scoped_lock lock(objectMutex);
208 if (not object.has_value())
209 {
210 object = armem::objects::ObjectInstanceToIndex{
211 .objectPoseClient = objectClientPlugin->createClient(),
212 .indexSpatialMemoryWriter = indexSpatialMemoryWriter,
213 .indexSpatialProviderSegmentID =
215 .indexNamedProviderSegmentID =
217 .params =
218 armem::objects::ObjectInstanceToIndex::Parameters{
219 .maxFrequency = armarx::Frequency::Hertz(properties.object.maxFrequencyHz)},
220 .state = {}};
221 }
222 ARMARX_CHECK(object.has_value());
223
224 object->fetchAndCommitObjectInstances(snapshotIDs);
225 }
226
227} // namespace armarx
uint8_t data[1]
Default component property definition container.
Definition Component.h:70
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
static Frequency Hertz(std::int64_t hertz)
Definition Frequency.cpp:20
PluginT * addPlugin(const std::string prefix="", ParamsT &&... params)
std::string getName() const
Retrieve name of object.
void onInitComponent() override
Pure virtual hook for the subclass.
void onDisconnectComponent() override
Hook for subclass.
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void onConnectComponent() override
Pure virtual hook for the subclass.
void onExitComponent() override
Hook for subclass.
std::string getDefaultName() const override
MemoryID withProviderSegmentName(const std::string &name) const
Definition MemoryID.cpp:417
std::string str(bool escapeDelimiters=true) const
Get a string representation of this memory ID.
Definition MemoryID.cpp:102
MemoryID withEntityName(const std::string &name) const
Definition MemoryID.cpp:425
AronDtoT dataAs() const
Get the data converted to a generated Aron DTO class.
Reader getReader(const MemoryID &memoryID)
Get a reader to the given memory name.
Writer useWriter(const MemoryID &memoryID)
Use a memory server and get a writer for it.
void setComponent(ManagedIceObject *component)
SubscriptionHandle subscribe(const MemoryID &subscriptionID, Callback Callback)
Indicates that a query to the Memory Name System failed.
Definition mns.h:25
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
const MemoryID spatialSegmentID
const MemoryID namedSegmentID
const MemoryID instaceSegmentID
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition mongodb.cpp:68
void toAron(arondto::MemoryID &dto, const MemoryID &bo)
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
void RemoteGui_createTab(std::string const &name, RemoteGui::Client::Widget const &rootWidget, RemoteGui::Client::Tab *tab)
EntityUpdate & add()
Definition Commit.cpp:80
wm::Memory memory
The slice of the memory that matched the query.
Definition Query.h:58