PriorKnowledgeController.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package MemoryX::gui-plugins::SceneEditor
19  * @date 2015
20  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
21  * GNU General Public License
22  */
23 
24 #include <exception>
25 #include <string>
26 #include <map>
27 #include <vector>
28 #include <math.h>
29 #include <algorithm>
30 
31 // Coin3D
32 #include <Inventor/nodes/SoNode.h>
33 
34 // MemoryXInterface
35 #include <MemoryX/interface/components/PriorKnowledgeInterface.h>
36 #include <MemoryX/interface/components/CommonStorageInterface.h>
37 
38 // MemoryX
46 
47 // Simox-VirtualRobot
48 #include <VirtualRobot/Visualization/CoinVisualization/CoinVisualizationFactory.h>
49 #include <VirtualRobot/ManipulationObject.h>
50 
52 #include "MemoryXController.h"
53 
54 memoryxcontroller::PriorKnowledgeController::PriorKnowledgeController(const memoryx::PriorKnowledgeInterfacePrx& priorKnowledgePrx)
55 {
56  this->priorKnowledgePrx = priorKnowledgePrx;
57  classesSegmentPrx = this->priorKnowledgePrx->getObjectClassesSegment();
58  databasePrx = this->priorKnowledgePrx->getCommonStorage();
59 
60  try
61  {
62  fileManager.reset(new memoryx::GridFileManager(databasePrx));
63  }
64  catch (const armarx::LocalException&)
65  {
66  fileManager.reset(new memoryx::GridFileManager(databasePrx, "./TempCacheForTesting/"));
67  }
68 }
69 
71 {
72  std::unique_lock lock(mutexEntities);
73  this->classesSegmentPrx = other.classesSegmentPrx;
74  this->databasePrx = other.databasePrx;
75  this->memoryXController = other.memoryXController;
76  this->priorKnowledgePrx = other.priorKnowledgePrx;
77  this->fileManager = other.fileManager;
78 }
79 
81 {
82 }
83 
85 {
86  std::unique_lock lock(mutexEntities);
87  return classesSegmentPrx->getReadCollectionsNS();
88 }
89 
90 std::vector<memoryx::ObjectClassPtr> memoryxcontroller::PriorKnowledgeController::getAllObjectClassesPtr(const std::string& collection) const
91 {
92  std::vector<memoryx::ObjectClassPtr> classPtr;
93  std::unique_lock lock(mutexEntities);
94 
95  try
96  {
97  memoryx::CollectionInterfacePrx collectionInterface = this->databasePrx->requestCollection(collection);
98  ARMARX_CHECK_EXPRESSION(collectionInterface);
99 
100  memoryx::EntityIdList ids = collectionInterface->findAllIds();
101 
102  for (memoryx::EntityIdList::const_iterator it = ids.begin(); it != ids.end(); ++it)
103  {
104  ARMARX_CHECK_EXPRESSION(classesSegmentPrx);
105 
106  const memoryx::EntityBasePtr entity = classesSegmentPrx->getEntityById(*it);
107  const memoryx::ObjectClassPtr objClass = memoryx::ObjectClassPtr::dynamicCast(entity);
108 
109  if (objClass)
110  {
111  classPtr.push_back(objClass);
112  }
113  }
114  }
115  catch (const memoryx::DBNotSpecifiedException&)
116  {
117  ARMARX_WARNING_S << "Tried to acces non-existing collection.";
118  return {};
119  }
120 
121  return classPtr;
122 }
123 
124 memoryx::ObjectClassPtr memoryxcontroller::PriorKnowledgeController::getObjectClassPtr(const std::string& className, const std::string& collection) const
125 {
126  std::unique_lock lock(mutexEntities);
127  std::vector<memoryx::ObjectClassPtr> objectClasses = this->getAllObjectClassesPtr(collection);
128 
129  if (!objectClasses.empty())
130  {
131  for (memoryx::ObjectClassPtr ptr : objectClasses)
132  {
134 
135  if (ptr->getName() == className)
136  {
137  return ptr;
138  }
139  }
140  }
141 
142  return NULL;
143 }
144 
145 SoNode* memoryxcontroller::PriorKnowledgeController::getCoinVisualisation(const memoryx::ObjectClassPtr& objectClass, const bool& collisionModel) const
146 {
147  std::unique_lock lock(mutexEntities);
148  memoryx::EntityWrappers::SimoxObjectWrapperPtr simoxWrapper = objectClass->addWrapper(new memoryx::EntityWrappers::SimoxObjectWrapper(fileManager));
149 
150  simoxWrapper->getManipulationObject()->setGlobalPose(Eigen::Matrix4f::Identity());
151 
152  VirtualRobot::VisualizationNodePtr visNode;
153  bool hasModel = true;
154 
155  if (collisionModel)
156  {
157  VirtualRobot::CollisionModelPtr collisionModel = simoxWrapper->getCollisionModel();
158 
159  if (!collisionModel)
160  {
161  hasModel = false;
162  }
163  else
164  {
165  visNode = collisionModel->getVisualization();
166  }
167  }
168  else
169  {
170  visNode = simoxWrapper->getVisualization();
171  }
172 
173  if (!hasModel)
174  {
175  return NULL;
176  }
177 
178  return VirtualRobot::CoinVisualizationFactory::getCoinVisualization(visNode);
179 }
180 
181 std::map<std::string, std::string> memoryxcontroller::PriorKnowledgeController::getAllAttributes(const memoryx::ObjectClassPtr& objectClass) const
182 {
183  std::unique_lock lock(mutexEntities);
184  std::map<std::string, std::string> allAttributes;
185 
186  if (!objectClass)
187  {
188  return allAttributes;
189  }
190 
191  memoryx::NameList attributeNames = objectClass->getAttributeNames();
192 
193  memoryx::EntityWrappers::SimoxObjectWrapperPtr simoxWrapper = objectClass->addWrapper(new memoryx::EntityWrappers::SimoxObjectWrapper(fileManager));
194 
195  for (memoryx::NameList::const_iterator it = attributeNames.begin(); it != attributeNames.end(); ++it)
196  {
197  memoryx::EntityAttributeBasePtr attrValue = objectClass->getAttribute(*it);
198  armarx::VariantPtr attributeValue = armarx::VariantPtr::dynamicCast(attrValue->getValue());
199 
200  if (attributeValue && (attributeValue->getOutputValueOnly() != ""))
201  {
202  if (*it == "ManipulationFile")
203  {
204  allAttributes.insert(std::pair<std::string, std::string>(*it, simoxWrapper->getManipulationObjectFileName()));
205  }
206  else if (*it == "IvFile" || *it == "IvFileCollision" || *it == "texFiles")
207  {
208  // Since the class SimoxObjectWrapper does not provide a way to get the file path of the IvFile, we have to use this workaround
209  try
210  {
211  std::string maniPath = simoxWrapper->getManipulationObjectFileName();
212  memoryx::MongoDBRefPtr mongoPtr = attributeValue->getClass<memoryx::MongoDBRef>();
213  const memoryx::GridFileInterfacePrx prx = databasePrx->getFileProxyById(mongoPtr->dbName, mongoPtr->docId);
214  if (!prx)
215  {
216  ARMARX_WARNING_S << "Could not find IvFile of objectClass " << objectClass->getName() << VAROUT(mongoPtr->dbName) << VAROUT(mongoPtr->docId);
217  allAttributes.insert(std::pair<std::string, std::string>(*it, ""));
218  continue;
219  }
220 
221  std::string ivFilePath = maniPath.substr(0, maniPath.rfind("/") + 1) + prx->getFilename();
222 
223  allAttributes.insert(std::pair<std::string, std::string>(*it, ivFilePath));
224  }
225  catch (const armarx::InvalidTypeException&)
226  {
227  ARMARX_WARNING_S << "Could not find IvFile of objectClass " << objectClass->getName();
228  allAttributes.insert(std::pair<std::string, std::string>(*it, ""));
229  }
230  }
231  else if (*it == "featureFile")
232  {
233  memoryx::EntityWrappers::TexturedRecognitionWrapperPtr texturedRecognitionWrapper = objectClass->addWrapper(new memoryx::EntityWrappers::TexturedRecognitionWrapper(fileManager));
234  allAttributes.insert(std::pair<std::string, std::string>(*it, texturedRecognitionWrapper->getFeatureFile()));
235  }
236  else
237  {
238  allAttributes.insert(std::pair<std::string, std::string>(*it, attributeValue->getOutputValueOnly()));
239  }
240  }
241  else
242  {
243  allAttributes.insert(std::pair<std::string, std::string>(*it, ""));
244  }
245  }
246 
247  return allAttributes;
248 }
249 
251 {
252  std::unique_lock lock(mutexEntities);
253 
254  memoryx::NameList collectionNames = this->getCollectionNames();
255  std::map<memoryx::ObjectClassPtr, std::string> possibleObjectClasses;
256  std::string className = objectInstance->getMostProbableClass();
257 
258  for (std::vector<std::string>::const_iterator it = collectionNames.begin(); it != collectionNames.end(); ++it)
259  {
260  memoryx::ObjectClassPtr ptr = this->getObjectClassPtr(className, *it);
261 
262  if (ptr)
263  {
264  possibleObjectClasses.insert(std::pair<memoryx::ObjectClassPtr, std::string>(ptr, *it));
265  }
266  }
267 
268  if (possibleObjectClasses.size() > 1)
269  {
270  // this should never happen, in all collections the names of the object-class shuold be unique.
271  // if there are for some reason two or more object-classes with the same name. one of these will be returned "at random"
272 
273  return possibleObjectClasses.begin()->second;
274  }
275  else if (possibleObjectClasses.size() == 1)
276  {
277  return possibleObjectClasses.begin()->second;
278  }
279  else
280  {
281  return "";
282  }
283 }
memoryxcontroller::PriorKnowledgeController::~PriorKnowledgeController
~PriorKnowledgeController()
Destructor.
Definition: PriorKnowledgeController.cpp:80
memoryxcontroller::PriorKnowledgeController::getCollection
std::string getCollection(const memoryx::ObjectInstancePtr &objectInstance) const
Returns the name of the collection the object class of the given object instance belongs to.
Definition: PriorKnowledgeController.cpp:250
GridFileManager.h
memoryxcontroller::PriorKnowledgeController::getCoinVisualisation
SoNode * getCoinVisualisation(const memoryx::ObjectClassPtr &objectClass, const bool &collisionModel) const
Returns a coin node that contains a visual model of the given object class.
Definition: PriorKnowledgeController.cpp:145
ObjectRecognitionWrapper.h
ObjectClass.h
memoryx::MongoDBRef
Represents a cross-database reference to a document in MongoDB.
Definition: MongoDBRef.h:45
memoryxcontroller::PriorKnowledgeController::getAllAttributes
std::map< std::string, std::string > getAllAttributes(const memoryx::ObjectClassPtr &objectClass) const
Returns a map with every attribute and its value of the given object class.
Definition: PriorKnowledgeController.cpp:181
IceInternal::Handle< ObjectClass >
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
memoryx::EntityWrappers::TexturedRecognitionWrapper
TexturedRecognitionWrapper offers a simplified access to the data of an object class or instance rela...
Definition: ObjectRecognitionWrapper.h:66
memoryxcontroller::PriorKnowledgeController::getAllObjectClassesPtr
std::vector< memoryx::ObjectClassPtr > getAllObjectClassesPtr(const std::string &collection) const
Returns a list of pointers to all object classes in the given collection.
Definition: PriorKnowledgeController.cpp:90
MemoryXCoreObjectFactories.h
memoryxcontroller::PriorKnowledgeController::getObjectClassPtr
memoryx::ObjectClassPtr getObjectClassPtr(const std::string &className, const std::string &collection) const
Returns a pointer to the object class with the given name in the give collection.
Definition: PriorKnowledgeController.cpp:124
ARMARX_WARNING_S
#define ARMARX_WARNING_S
Definition: Logging.h:206
memoryx::EntityWrappers::SimoxObjectWrapper
SimoxObjectWrapper offers a simplified access to the Simox ManipulationObject (i.e visualization,...
Definition: SimoxObjectWrapper.h:46
memoryxcontroller::PriorKnowledgeController
The controller for the communication with the priorknowledge.
Definition: PriorKnowledgeController.h:55
SimoxObjectWrapper.h
ObjectInstance.h
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
VAROUT
#define VAROUT(x)
Definition: StringHelpers.h:182
PriorKnowledgeController.h
memoryx::GridFileManager
GridFileManager provides utility functions for working with files in Mongo GridFS and links to them s...
Definition: GridFileManager.h:42
memoryxcontroller::PriorKnowledgeController::getCollectionNames
std::vector< std::string > getCollectionNames() const
Returns a list of the names of all collections in the database.
Definition: PriorKnowledgeController.cpp:84
MemoryXController.h
memoryxcontroller::PriorKnowledgeController::PriorKnowledgeController
PriorKnowledgeController(const memoryx::PriorKnowledgeInterfacePrx &priorKnowledgePrx)
Constructor.
Definition: PriorKnowledgeController.cpp:54
Application.h