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
47
49
50namespace armarx
51{
52
55 {
58
59 defs->optional(properties.object.maxFrequencyHz, "p.object.maxFrequency");
60
61 return defs;
62 }
63
65 {
66 addPlugin(objectClientPlugin);
67 }
68
69 std::string
71 {
72 return "ObjectInstanceToIndex";
73 }
74
75 void
77 {
78 using This = ObjectInstanceToIndex;
79 // This should not be necessary but seems to be. ToDo: Look into this.
81
82 memoryNameSystem().subscribe(armem::robot_state::memoryID, this, &This::processRobotState);
84 armem::objects::instaceSegmentID, this, &This::processObjectInstance);
85 }
86
87 void
89 {
90 try
91 {
92 indexSpatialMemoryWriter = memoryNameSystem().useWriter(armem::index::spatialSegmentID);
93 }
95 {
96 ARMARX_ERROR << e.what();
97 }
98
99 {
102 }
103 }
104
105 void
109
110 void
114
115 void
117 {
118 using namespace armarx::RemoteGui::Client;
119
120 VBoxLayout root = {VSpacer()};
121 RemoteGui_createTab(getName(), root, &tab);
122 }
123
124 void
128
129 void
130 ObjectInstanceToIndex::processRobotState(const armem::MemoryID& id,
131 const std::vector<armem::MemoryID>& snapshotIDs)
132 {
133#if 0
134 if (not robotMemoryReader)
135 {
136 try
137 {
138 robotMemoryReader = memoryNameSystem().getReader(objectInstanceSegmentID);
139 }
141 {
142 ARMARX_INFO << e.what();
143 return;
144 }
145 }
146 ARMARX_CHECK(robotMemoryReader);
147
148 // Get only the latest snapshot per entity.
149 std::map<armem::MemoryID, const armem::MemoryID*> entityToSnapshot;
150 for (const armem::MemoryID& snapshotID : snapshotIDs)
151 {
152 const armem::MemoryID entityID = snapshotID.getEntityID();
153 if (auto it = entityToSnapshot.find(entityID); it != entityToSnapshot.end())
154 {
155 if (it->second->timestamp < snapshotID.timestamp)
156 {
157 entityToSnapshot[entityID] = &snapshotID;
158 }
159 }
160 else
161 {
162 entityToSnapshot[entityID] = &snapshotID;
163 }
164 }
165
166 // Query them.
167 std::vector<armem::MemoryID> queryIDs;
168 for (const auto& [_, snapshotID] : entityToSnapshot)
169 {
170 queryIDs.push_back(*snapshotID);
171 }
172
173 armem::client::QueryResult result = robotMemoryReader.queryMemoryIDs(queryIDs);
174 if (result.success)
175 {
176 // Prepare the commit.
177
178 armem::MemoryID provSegID = indexSpatialSegmentID.withProviderSegmentName(getName());
179
180 armem::Commit commit;
181
182 result.memory.forEachInstance(
183 [&commit, &provSegID](const armem::wm::EntityInstance& instance)
184 {
185 const armem::arondto::ObjectInstance data = instance.dataAs<armem::arondto::ObjectInstance>();
186
187 armem::index::arondto::Spatial spatial;
188 armem::toAron(spatial.id, instance.id());
189 spatial.oobbGlobal;
190 spatial.aabbGlobal;
191
192 armem::EntityUpdate& update = commit.add();
193 update.entityID = provSegID.withEntityName(instance.id().str());
194 update.referencedTime = instance.id().timestamp;
195 update.instancesData = { spatial.toAron() };
196
197 return true;
198 });
199 }
200 else
201 {
202 ARMARX_INFO << result.errorMessage;
203 }
204#endif
205 }
206
207 void
208 ObjectInstanceToIndex::processObjectInstance(const armem::MemoryID& id,
209 const std::vector<armem::MemoryID>& snapshotIDs)
210 {
211 std::scoped_lock lock(objectMutex);
212 if (not object.has_value())
213 {
214 object = armem::objects::ObjectInstanceToIndex{
215 .objectPoseClient = objectClientPlugin->createClient(),
216 .indexSpatialMemoryWriter = indexSpatialMemoryWriter,
217 .indexSpatialProviderSegmentID =
219 .indexNamedProviderSegmentID =
221 .params =
222 armem::objects::ObjectInstanceToIndex::Parameters{
223 .maxFrequency = armarx::Frequency::Hertz(properties.object.maxFrequencyHz)},
224 .state = {}};
225 }
226 ARMARX_CHECK(object.has_value());
227
228 object->fetchAndCommitObjectInstances(snapshotIDs);
229 }
230
231} // namespace armarx
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
Definition Decoupled.h:29
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.
Brief description of class ObjectInstanceToIndex.
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