ObjectRecognitionWrapper.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 MemoryX::Helpers
17* @author Alexey Kozlov ( kozlov at kit dot edu)
18* @date 2012
19* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20* GNU General Public License
21*/
22
24
25// MemoryX
27
28// ArmarXCore
30
32
34{
35 // ObjectRecognitionWrapper attributes
36 const std::string ATTR_RECOGNITION_METHOD = "recognitionMethod";
37 const std::string ATTR_MOTION_MODEL = "defaultMotionModel";
38 const std::string ATTR_RECOGNITION_QUERIES = "recognitionQueries";
39
40 // TexturedRecognitionWrapper attributes
41 const std::string ATTR_FEATURE_FILE = "featureFile";
42
43 // SegmentableRecognitionWrapper attributes
44 const std::string ATTR_OBJECT_COLOR = "objectColor";
45 const std::string ATTR_SEGMENTABLE_DATA = "segmentableObjectData";
46
47 // ArMarkerWrapper attributes
48 const std::string ATTR_AR_MARKER_ID = "arMarkerID";
49 const std::string ATTR_AR_MARKER_SIZE = "arMarkerSize";
50 const std::string ATTR_AR_MARKER_TRANSLATION = "arMarkerTranslation";
51 const std::string ATTR_AR_MARKER_ROTATION = "arMarkerRotation";
52
53 // PointCloudLocalizerWrapper attributes
54 const std::string POINTCLOUD_FILE = "pointcloudFile";
55 const std::string POINTCLOUD_EXP_MATCH_DIST = "expectedMatchDistance";
56 const std::string POINTCLOUD_MISMATCH_DIST = "mismatchDistance";
57
58 std::string
60 {
61 if (getEntity()->hasAttribute(ATTR_RECOGNITION_METHOD))
62 {
63 return EntityPtr::dynamicCast(getEntity())
64 ->getAttributeValue(ATTR_RECOGNITION_METHOD)
65 ->getString();
66 }
67
68 return "";
69 }
70
71 std::string
73 {
74 if (getEntity()->hasAttribute(ATTR_MOTION_MODEL))
75 {
76 return EntityPtr::dynamicCast(getEntity())
77 ->getAttributeValue(ATTR_MOTION_MODEL)
78 ->getString();
79 }
80
81 return "";
82 }
83
84 void
85 ObjectRecognitionWrapper::setRecognitionMethod(const std::string& recognitionMethod)
86 {
87 if (recognitionMethod != "")
88 {
89 EntityAttributeBasePtr recognitionMethodAttr =
91 recognitionMethodAttr->setValue(new armarx::Variant(recognitionMethod));
92 getEntity()->putAttribute(recognitionMethodAttr);
93 }
94 }
95
96 void
97 ObjectRecognitionWrapper::setDefaultMotionModel(const std::string& defaultMotionModel)
98 {
99 if (defaultMotionModel != "")
100 {
101 EntityAttributeBasePtr motionModelAttr = new EntityAttribute(ATTR_MOTION_MODEL);
102 motionModelAttr->setValue(new armarx::Variant(defaultMotionModel));
103 getEntity()->putAttribute(motionModelAttr);
104 }
105 }
106
107 void
109 {
110 if (getEntity()->hasAttribute(ATTR_RECOGNITION_QUERIES))
111 {
112 getEntity()
113 ->getAttribute(ATTR_RECOGNITION_QUERIES)
114 ->addValue(new armarx::Variant(query));
115 }
116 else
117 {
118 EntityAttributeBasePtr recognitionQueryAttr =
120 recognitionQueryAttr->addValue(new armarx::Variant(query));
121 getEntity()->putAttribute(recognitionQueryAttr);
122 }
123 }
124
125 LocalizationQueryList
127 {
128 LocalizationQueryList resultList;
129
130 if (!getEntity()->hasAttribute(ATTR_RECOGNITION_QUERIES))
131 {
132 return resultList;
133 }
134
135 int numberElements = getEntity()->getAttribute(ATTR_RECOGNITION_QUERIES)->size();
136
137 for (int e = 0; e < numberElements; e++)
138 {
139 armarx::VariantPtr var = armarx::VariantPtr::dynamicCast(
140 getEntity()->getAttribute(ATTR_RECOGNITION_QUERIES)->getValueAt(e));
141 resultList.push_back(var->getClass<LocalizationQuery>());
142 }
143
144 return resultList;
145 }
146
147 void
149 {
150 if (!getEntity()->hasAttribute(ATTR_RECOGNITION_QUERIES))
151 {
152 return;
153 }
154
155 int numberElements = getEntity()->getAttribute(ATTR_RECOGNITION_QUERIES)->size();
156
157 for (int e = 0; e < numberElements; e++)
158 {
159 armarx::VariantPtr var = armarx::VariantPtr::dynamicCast(
160 getEntity()->getAttribute(ATTR_RECOGNITION_QUERIES)->getValueAt(e));
161 std::string currentQueryName = var->getClass<LocalizationQuery>()->queryName;
162
163 if (currentQueryName == queryName)
164 {
165 getEntity()->getAttribute(ATTR_RECOGNITION_QUERIES)->removeValueAt(e);
166 e--;
167 numberElements--;
168 }
169 }
170 }
171
172 void
174 const LocalizationQueryPtr& query)
175 {
176 if (!getEntity()->hasAttribute(ATTR_RECOGNITION_QUERIES))
177 {
178 ARMARX_ERROR_S << "Entity " << getEntity()->getName() << " does not have Attribute "
180 return;
181 }
182
183 int numberElements = getEntity()->getAttribute(ATTR_RECOGNITION_QUERIES)->size();
185
186 for (int e = 0; e < numberElements; e++)
187 {
188 armarx::VariantPtr var = armarx::VariantPtr::dynamicCast(
189 getEntity()->getAttribute(ATTR_RECOGNITION_QUERIES)->getValueAt(e));
190 LocalizationQueryPtr currentQuery = var->getClass<LocalizationQuery>();
191
192 armarx::VariantPtr newVar = new armarx::Variant();
194
195 if (currentQuery->queryName != queryName) // copy
196 {
197 newVar->setClass(LocalizationQueryPtr::dynamicCast(currentQuery->ice_clone()));
198 }
199 else // insert new query
200 {
201 newVar->setClass(LocalizationQueryPtr::dynamicCast(query->ice_clone()));
202 }
203
204 updatedQueries->addValue(newVar);
205 }
206
207 getEntity()->removeAttribute(ATTR_RECOGNITION_QUERIES);
208 getEntity()->putAttribute(updatedQueries);
209 }
210
211 Ice::ObjectPtr
213 {
214 return new ObjectRecognitionWrapper(*this);
215 }
216
221
222 std::string
224 {
225 if (getEntity()->hasAttribute(ATTR_FEATURE_FILE))
226 {
227 const std::string featureFileName = cacheAttributeFile(ATTR_FEATURE_FILE, true);
228 return featureFileName;
229 }
230
231 return "";
232 }
233
234 void
235 TexturedRecognitionWrapper::setFeatureFile(const std::string& featureFileName,
236 const std::string& filesDBName)
237 {
238 if (featureFileName != "")
239 {
240 EntityAttributeBasePtr featureFileAttr = new EntityAttribute(ATTR_FEATURE_FILE);
241 fileManager->storeFileToAttr(filesDBName, featureFileName, featureFileAttr);
242 cleanUpAttributeFiles(getEntity()->getAttribute(ATTR_FEATURE_FILE), featureFileAttr);
243 getEntity()->putAttribute(featureFileAttr);
244 }
245 }
246
247 Ice::ObjectPtr
249 {
250 return new TexturedRecognitionWrapper(*this);
251 }
252
257
258 std::string
260 {
261 std::vector<std::string> fileNames;
262
263 if (getEntity()->hasAttribute(ATTR_SEGMENTABLE_DATA))
264 {
265 fileManager->ensureFilesInCache(
266 getEntity()->getAttribute(ATTR_SEGMENTABLE_DATA), fileNames, true);
267 }
268
269 if (fileNames.size() == 0)
270 {
271 return "";
272 }
273
274 // retrieve path
275 std::filesystem::path path;
276 path = fileNames.at(0);
277
278 return path.parent_path().c_str();
279 }
280
281 void
283 const std::string& filesDBName)
284 {
285 if (dataPath != "")
286 {
287 EntityAttributeBasePtr featureFileAttr = new EntityAttribute(ATTR_SEGMENTABLE_DATA);
288 // if(getEntity()–>getAttribute(ATTR_SEGMENTABLE_DATA))
289 // fileManager->removeAttrFiles(getEntity()–>getAttribute(ATTR_SEGMENTABLE_DATA));
290 ARMARX_VERBOSE_S << "STORING datapath " << dataPath;
291 fileManager->storeDirectoryToAttr(filesDBName, dataPath, featureFileAttr);
293 featureFileAttr);
294
295 getEntity()->putAttribute(featureFileAttr);
296 }
297 }
298
299 ObjectColor
301 {
302 if (getEntity()->hasAttribute(ATTR_OBJECT_COLOR))
303 {
304 return (ObjectColor)EntityPtr::dynamicCast(getEntity())
305 ->getAttributeValue(ATTR_OBJECT_COLOR)
306 ->getInt();
307 }
308
309 return eNone;
310 }
311
312 void
314 {
315 EntityAttributeBasePtr objectColorAttr = new EntityAttribute(ATTR_OBJECT_COLOR);
316 objectColorAttr->setValue(new armarx::Variant(int(color)));
317 getEntity()->putAttribute(objectColorAttr);
318 }
319
320 Ice::ObjectPtr
325
330
331 ObjectColor
333 {
334 if (getEntity()->hasAttribute(ATTR_OBJECT_COLOR))
335 {
336 return (ObjectColor)EntityPtr::dynamicCast(getEntity())
337 ->getAttributeValue(ATTR_OBJECT_COLOR)
338 ->getInt();
339 }
340 else
341 {
342 return eNone;
343 }
344 }
345
346 void
348 {
349 EntityAttributeBasePtr objectColorAttr = new EntityAttribute(ATTR_OBJECT_COLOR);
350 objectColorAttr->setValue(new armarx::Variant(int(color)));
351 getEntity()->putAttribute(objectColorAttr);
352 }
353
354 Ice::ObjectPtr
356 {
357 return new HandMarkerBallWrapper(*this);
358 }
359
363
364 std::vector<int>
366 {
367 std::vector<int> result;
368
369 if (getEntity()->hasAttribute(ATTR_AR_MARKER_ID))
370 {
371 EntityPtr p = EntityPtr::dynamicCast(getEntity());
372 EntityAttributeBasePtr attr = p->getAttribute(ATTR_AR_MARKER_ID);
373
374 for (int i = 0; i < attr->size(); i++)
375 {
376 result.push_back(attr->getValueAt(i)->getInt());
377 }
378 }
379 else
380 {
381 ARMARX_WARNING_S << "Entity doesn't have the requested attribute " << ATTR_AR_MARKER_ID;
382 }
383
384 return result;
385 }
386
387 void
388 ArMarkerWrapper::setArMarkerIDs(const std::vector<int>& newMarkerIDs)
389 {
390 EntityAttributeBasePtr attr = new EntityAttribute(ATTR_AR_MARKER_ID);
391
392 for (size_t i = 0; i < newMarkerIDs.size(); i++)
393 {
394 attr->addValue(new armarx::Variant(newMarkerIDs.at(i)));
395 }
396
397 getEntity()->putAttribute(attr);
398 }
399
400 std::vector<float>
402 {
403 std::vector<float> result;
404
405 if (getEntity()->hasAttribute(ATTR_AR_MARKER_SIZE))
406 {
407 EntityPtr p = EntityPtr::dynamicCast(getEntity());
408 EntityAttributeBasePtr attr = p->getAttribute(ATTR_AR_MARKER_SIZE);
409
410 for (int i = 0; i < attr->size(); i++)
411 {
412 result.push_back(attr->getValueAt(i)->getFloat());
413 }
414 }
415 else
416 {
417 ARMARX_WARNING_S << "Entity doesn't have the requested attribute "
419 }
420
421 return result;
422 }
423
424 void
425 ArMarkerWrapper::setArMarkerSizes(const std::vector<float>& newMarkerSizes)
426 {
427 EntityAttributeBasePtr attr = new EntityAttribute(ATTR_AR_MARKER_SIZE);
428
429 for (size_t i = 0; i < newMarkerSizes.size(); i++)
430 {
431 attr->addValue(new armarx::Variant(newMarkerSizes.at(i)));
432 }
433
434 getEntity()->putAttribute(attr);
435 }
436
437 std::vector<Eigen::Vector3f>
439 {
440 std::vector<Eigen::Vector3f> result;
441
442 if (getEntity()->hasAttribute(ATTR_AR_MARKER_TRANSLATION))
443 {
444 EntityPtr p = EntityPtr::dynamicCast(getEntity());
445 EntityAttributeBasePtr attr = p->getAttribute(ATTR_AR_MARKER_TRANSLATION);
446
447 for (int i = 0; i < attr->size(); i++)
448 {
449 armarx::Vector3BasePtr vecBase =
450 armarx::VariantPtr::dynamicCast(attr->getValueAt(i))
451 ->getClass<armarx::Vector3Base>();
452 Eigen::Vector3f vec;
453 vec(0) = vecBase->x;
454 vec(1) = vecBase->y;
455 vec(2) = vecBase->z;
456 result.push_back(vec);
457 }
458 }
459 else
460 {
461 ARMARX_WARNING_S << "Entity doesn't have the requested attribute "
463 }
464
465 return result;
466 }
467
468 void
470 const std::vector<Eigen::Vector3f>& newTranslations)
471 {
472 EntityAttributeBasePtr attr = new EntityAttribute(ATTR_AR_MARKER_TRANSLATION);
473
474 for (size_t i = 0; i < newTranslations.size(); i++)
475 {
476 armarx::Vector3 vec(newTranslations.at(i));
477 attr->addValue(new armarx::Variant(vec));
478 }
479
480 getEntity()->putAttribute(attr);
481 }
482
483 std::vector<Eigen::Vector3f>
485 {
486 std::vector<Eigen::Vector3f> result;
487
488 if (getEntity()->hasAttribute(ATTR_AR_MARKER_ROTATION))
489 {
490 EntityPtr p = EntityPtr::dynamicCast(getEntity());
491 EntityAttributeBasePtr attr = p->getAttribute(ATTR_AR_MARKER_ROTATION);
492
493 for (int i = 0; i < attr->size(); i++)
494 {
495 armarx::Vector3BasePtr vecBase =
496 armarx::VariantPtr::dynamicCast(attr->getValueAt(i))
497 ->getClass<armarx::Vector3Base>();
498 Eigen::Vector3f vec;
499 vec(0) = vecBase->x;
500 vec(1) = vecBase->y;
501 vec(2) = vecBase->z;
502 result.push_back(vec);
503 }
504 }
505 else
506 {
507 ARMARX_WARNING_S << "Entity doesn't have the requested attribute "
509 }
510
511 return result;
512 }
513
514 void
516 const std::vector<Eigen::Vector3f>& newRotations)
517 {
518 EntityAttributeBasePtr attr = new EntityAttribute(ATTR_AR_MARKER_ROTATION);
519
520 for (size_t i = 0; i < newRotations.size(); i++)
521 {
522 armarx::Vector3 vec(newRotations.at(i));
523 attr->addValue(new armarx::Variant(vec));
524 }
525
526 getEntity()->putAttribute(attr);
527 }
528
529 std::vector<Eigen::Matrix4f>
531 {
532 std::vector<Eigen::Matrix4f> result;
533
534 if (getEntity()->hasAttribute(ATTR_AR_MARKER_TRANSLATION) &&
535 getEntity()->hasAttribute(ATTR_AR_MARKER_ROTATION))
536 {
537 EntityPtr p = EntityPtr::dynamicCast(getEntity());
538 EntityAttributeBasePtr attrTransl = p->getAttribute(ATTR_AR_MARKER_TRANSLATION);
539 EntityAttributeBasePtr attrRot = p->getAttribute(ATTR_AR_MARKER_ROTATION);
540
541 for (int i = 0; i < attrTransl->size() && i < attrRot->size(); i++)
542 {
543 Eigen::Matrix4f newTrafo = Eigen::Matrix4f::Identity();
544 armarx::Vector3BasePtr vecBase =
545 armarx::VariantPtr::dynamicCast(attrTransl->getValueAt(i))
546 ->getClass<armarx::Vector3Base>();
547 newTrafo(0, 3) = vecBase->x;
548 newTrafo(1, 3) = vecBase->y;
549 newTrafo(2, 3) = vecBase->z;
550 vecBase = armarx::VariantPtr::dynamicCast(attrRot->getValueAt(i))
551 ->getClass<armarx::Vector3Base>();
552 newTrafo.block<3, 3>(0, 0) =
553 (Eigen::AngleAxisf(vecBase->x, Eigen::Vector3f::UnitX()) *
554 Eigen::AngleAxisf(vecBase->y, Eigen::Vector3f::UnitY()) *
555 Eigen::AngleAxisf(vecBase->z, Eigen::Vector3f::UnitZ()))
556 .matrix();
557 result.push_back(newTrafo);
558 }
559 }
560 else
561 {
562 ARMARX_WARNING_S << "Entity doesn't have both requested attributes "
564 }
565
566 return result;
567 }
568
569 Ice::ObjectPtr
571 {
572 return new ArMarkerWrapper(*this);
573 }
574
579
580 std::string
582 {
583 if (getEntity()->hasAttribute(POINTCLOUD_FILE))
584 {
585 const std::string fileName = cacheAttributeFile(POINTCLOUD_FILE, true);
586 return fileName;
587 }
588
589 return "";
590 }
591
592 void
594 const std::string& filesDBName)
595 {
596 if (fileName != "")
597 {
598 EntityAttributeBasePtr fileAttr = new EntityAttribute(POINTCLOUD_FILE);
599 fileManager->storeFileToAttr(filesDBName, fileName, fileAttr);
600 cleanUpAttributeFiles(getEntity()->getAttribute(POINTCLOUD_FILE), fileAttr);
601 getEntity()->putAttribute(fileAttr);
602 }
603 }
604
605 void
607 float& mismatchDistance)
608 {
609 if (getEntity()->hasAttribute(POINTCLOUD_EXP_MATCH_DIST) &&
610 getEntity()->hasAttribute(POINTCLOUD_MISMATCH_DIST))
611 {
612 EntityPtr p = EntityPtr::dynamicCast(getEntity());
613 expectedMatchDistance = p->getAttributeValue(POINTCLOUD_EXP_MATCH_DIST)->getFloat();
614 mismatchDistance = p->getAttributeValue(POINTCLOUD_MISMATCH_DIST)->getFloat();
615 }
616 else
617 {
618 ARMARX_IMPORTANT_S << "Attribute " << POINTCLOUD_EXP_MATCH_DIST << " or "
619 << POINTCLOUD_MISMATCH_DIST << " not set for object "
620 << getEntity()->getName();
621 expectedMatchDistance = 0;
622 mismatchDistance = 0;
623 }
624 }
625
626 void
628 const float mismatchDistance)
629 {
630 EntityAttributePtr expectedMatchDistanceAttr =
632 expectedMatchDistanceAttr->setValue(new armarx::Variant(expectedMatchDistance));
633 getEntity()->putAttribute(expectedMatchDistanceAttr);
635 mismatchDistanceAttr->setValue(new armarx::Variant(mismatchDistance));
636 getEntity()->putAttribute(mismatchDistanceAttr);
637 }
638
639 Ice::ObjectPtr
641 {
642 return new PointCloudLocalizerWrapper(*this);
643 }
644} // namespace memoryx::EntityWrappers
The Variant class is described here: Variants.
Definition Variant.h:224
The Vector3 class.
Definition Pose.h:113
Attribute of MemoryX entities.
EntityBasePtr getEntity() const
Get the stored name of the stored entity.
std::string cacheAttributeFile(const std::string &attrName, bool preserveOriginalFName=false) const
void cleanUpAttributeFiles(EntityAttributeBasePtr oldAttr, EntityAttributeBasePtr newAttr)
cleanUpAttributeFiles compares the files attached to the two given attributes and removes the files o...
std::vector< Eigen::Matrix4f > getTransformationsToCenter() const
ArMarkerWrapper(const GridFileManagerPtr &gfm)
Constructs new ArMarkerWrapper.
std::vector< Eigen::Vector3f > getTransformationToCenterTranslations() const
void setTransformationToCenterRotationsRPY(const std::vector< Eigen::Vector3f > &newRotations)
void setArMarkerIDs(const std::vector< int > &newMarkerIDs)
std::vector< Eigen::Vector3f > getTransformationToCenterRotationsRPY() const
void setTransformationToCenterTranslations(const std::vector< Eigen::Vector3f > &newTranslations)
void setArMarkerSizes(const std::vector< float > &newMarkerSizes)
HandMarkerBallWrapper(const GridFileManagerPtr &gfm)
Constructs new HandMarkerBallWrapper.
void setDefaultMotionModel(const std::string &defaultMotionModel)
void addLocalizationQuery(const LocalizationQueryPtr &query)
void updateLocalizationQuery(const std::string &queryName, const LocalizationQueryPtr &query)
void setRecognitionMethod(const std::string &recognitionMethod)
PointCloudLocalizerWrapper(const GridFileManagerPtr &gfm)
Constructs new PointCloudLocalizerWrapper.
void setPointCloudFileName(const std::string &fileName, const std::string &filesDBName)
void getExpectedMatchingDistance(float &expectedMatchDistance, float &mismatchDistance)
void setExpectedMatchingDistance(const float expectedMatchDistance, const float mismatchDistance)
SegmentableRecognitionWrapper(const GridFileManagerPtr &gfm)
Constructs new SegmentableRecognitionWrapper.
void setDataFiles(const std::string &dataPath, const std::string &filesDBName)
void setFeatureFile(const std::string &featureFileName, const std::string &filesDBName)
TexturedRecognitionWrapper(const GridFileManagerPtr &gfm)
Constructs new TexturedRecognitionWrapper.
The LocalizationQuery class is used to create LocalizationJob instances and provide an interface to q...
#define ARMARX_ERROR_S
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:216
#define ARMARX_VERBOSE_S
Definition Logging.h:207
#define ARMARX_IMPORTANT_S
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:210
#define ARMARX_WARNING_S
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:213
const armarx::VariantTypeId LocalizationQuery
IceInternal::Handle< Variant > VariantPtr
Definition Variant.h:41
const std::string ATTR_AR_MARKER_TRANSLATION
const std::string POINTCLOUD_EXP_MATCH_DIST
IceInternal::Handle< LocalizationQuery > LocalizationQueryPtr
IceInternal::Handle< EntityAttribute > EntityAttributePtr
Typedef of EntityAttributePtr as IceInternal::Handle<EntityAttribute> for convenience.
IceInternal::Handle< Entity > EntityPtr
Typedef of EntityPtr as IceInternal::Handle<Entity> for convenience.
Definition Entity.h:45
std::shared_ptr< GridFileManager > GridFileManagerPtr