25 #include <Math/Math3d.h>
33 #include <boost/filesystem.hpp>
58 settings_priorMemory = getProperty<std::string>(
"PriorKnowledgeMemoryProxyName").getValue();
59 usingProxy(settings_priorMemory);
61 featureCalculator.addFeature<
CHArea>();
62 featureCalculator.addFeature<
CHVolume>();
64 featureCalculator.addFeature<
BBVolume>();
65 featureCalculator.addFeature<
BBArea>();
78 memoryPrx = getProxy<PriorKnowledgeInterfacePrx>(settings_priorMemory);
79 classesSegmentPrx = memoryPrx->getObjectClassesSegment();
80 databasePrx = memoryPrx->getCommonStorage();
84 connected = databasePrx->isConnected();
87 void ObjectShapeClassification::refetchObjects()
90 boost::recursive_mutex::scoped_lock lock(mutexEntities);
91 EntityIdList ids = classesSegmentPrx->getAllEntityIds();
94 for (EntityIdList::const_iterator it = ids.begin(); it != ids.end(); ++it)
96 const EntityBasePtr entity = classesSegmentPrx->getEntityById(*it);
97 const ObjectClassPtr objClass = ObjectClassPtr::dynamicCast(entity);
102 dbObjects.push_back(objClass);
107 void ObjectShapeClassification::checkFeatures()
109 for (
auto optr : dbObjects)
124 std::string ivFile =
"";
129 fileManager->ensureFileInCache(obj->getAttribute(
ATTR_IV_FILE), ivFile,
false);
134 ARMARX_WARNING <<
"No IV file found for object" << obj->getName();
142 template <
typename Pair>
145 return a.second < b.second;
151 std::vector<std::pair<std::string, Points>> objectsPoints;
154 for (
auto& obj : dbObjects)
156 auto points = getPointsFromIV(obj);
158 if (points.size() != 0)
160 objectsPoints.push_back(
TaggedPoints(obj->getName(), points));
166 auto newObjectFeatures = featureCalculator.getFeatures(points);
185 for (
const auto& feat : newObjectFeatures)
192 std::map<std::string, std::map<std::string, double>> objectDiffs;
193 std::map<std::string, double> maxDiffs;
198 for (
const auto& obj : objectsPoints)
201 auto features = featureCalculator.getFeatures(obj.second);
202 std::map<std::string, double> diffs;
205 for (
const auto& p : newObjectFeatures)
207 auto& feature = p.second;
208 double diff = feature->compare(*features[feature->name()]);
209 diffs[feature->name()] = diff;
212 double oldValue = maxDiffs[feature->name()];
213 maxDiffs[feature->name()] = (diff > oldValue) ? diff : oldValue;
216 objectDiffs[obj.first] = diffs;
219 std::map<std::string, double> objectDiffsTotal;
222 for (
const auto& obj : objectDiffs)
226 double totalDiff = std::accumulate(obj.second.begin(), obj.second.end(), 0.0, [&](
double prev,
const std::pair<std::string, double> p)
228 double diff = (p.second / maxDiffs[p.first]);
229 ARMARX_VERBOSE_S <<
"-- " << p.first <<
": " << diff;
230 return prev + (p.second / maxDiffs[p.first]);
232 objectDiffsTotal[obj.first] = totalDiff;
233 ARMARX_VERBOSE <<
"--+ Total score(" << obj.first <<
"): " << totalDiff <<
"\n";
237 auto bestMatch = *std::min_element(objectDiffsTotal.begin(), objectDiffsTotal.end(),
by_second());
239 ARMARX_INFO <<
"Best match found: " << bestMatch.first <<
" with score of " << bestMatch.second;
244 void ObjectShapeClassification::matchToFoundPointCloud(
const Points& newPoints,
const Points& foundPointCloud)
261 for (
size_t i = 0; i < segmentedObjectPoints.size(); i++)
263 points.push_back(armarx::Vector3Ptr::dynamicCast(segmentedObjectPoints.at(i))->toEigen());
272 ARMARX_INFO <<
"Connected to DB, starting classification";
279 matchToFoundPointCloud(points, foundPointCloud.second);
285 return foundPointCloud.first;
289 ARMARX_WARNING <<
"No connection to DB. Unable to start classification";
292 return "ERROR: No similar object found";