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 <math.h>
25 
26 #include <algorithm>
27 #include <exception>
28 #include <map>
29 #include <string>
30 #include <vector>
31 
32 // Coin3D
33 #include <Inventor/nodes/SoNode.h>
34 
35 // MemoryXInterface
36 #include <MemoryX/interface/components/CommonStorageInterface.h>
37 #include <MemoryX/interface/components/PriorKnowledgeInterface.h>
38 
39 // MemoryX
41 
48 
49 // Simox-VirtualRobot
50 #include <VirtualRobot/ManipulationObject.h>
51 #include <VirtualRobot/Visualization/CoinVisualization/CoinVisualizationFactory.h>
52 
53 #include "MemoryXController.h"
55 
57  const memoryx::PriorKnowledgeInterfacePrx& priorKnowledgePrx)
58 {
59  this->priorKnowledgePrx = priorKnowledgePrx;
60  classesSegmentPrx = this->priorKnowledgePrx->getObjectClassesSegment();
61  databasePrx = this->priorKnowledgePrx->getCommonStorage();
62 
63  try
64  {
65  fileManager.reset(new memoryx::GridFileManager(databasePrx));
66  }
67  catch (const armarx::LocalException&)
68  {
69  fileManager.reset(new memoryx::GridFileManager(databasePrx, "./TempCacheForTesting/"));
70  }
71 }
72 
74  const PriorKnowledgeController& other)
75 {
76  std::unique_lock lock(mutexEntities);
77  this->classesSegmentPrx = other.classesSegmentPrx;
78  this->databasePrx = other.databasePrx;
79  this->memoryXController = other.memoryXController;
80  this->priorKnowledgePrx = other.priorKnowledgePrx;
81  this->fileManager = other.fileManager;
82 }
83 
85 {
86 }
87 
88 std::vector<std::string>
90 {
91  std::unique_lock lock(mutexEntities);
92  return classesSegmentPrx->getReadCollectionsNS();
93 }
94 
95 std::vector<memoryx::ObjectClassPtr>
97  const std::string& collection) const
98 {
99  std::vector<memoryx::ObjectClassPtr> classPtr;
100  std::unique_lock lock(mutexEntities);
101 
102  try
103  {
104  memoryx::CollectionInterfacePrx collectionInterface =
105  this->databasePrx->requestCollection(collection);
106  ARMARX_CHECK_EXPRESSION(collectionInterface);
107 
108  memoryx::EntityIdList ids = collectionInterface->findAllIds();
109 
110  for (memoryx::EntityIdList::const_iterator it = ids.begin(); it != ids.end(); ++it)
111  {
112  ARMARX_CHECK_EXPRESSION(classesSegmentPrx);
113 
114  const memoryx::EntityBasePtr entity = classesSegmentPrx->getEntityById(*it);
115  const memoryx::ObjectClassPtr objClass = memoryx::ObjectClassPtr::dynamicCast(entity);
116 
117  if (objClass)
118  {
119  classPtr.push_back(objClass);
120  }
121  }
122  }
123  catch (const memoryx::DBNotSpecifiedException&)
124  {
125  ARMARX_WARNING_S << "Tried to acces non-existing collection.";
126  return {};
127  }
128 
129  return classPtr;
130 }
131 
134  const std::string& collection) const
135 {
136  std::unique_lock lock(mutexEntities);
137  std::vector<memoryx::ObjectClassPtr> objectClasses = this->getAllObjectClassesPtr(collection);
138 
139  if (!objectClasses.empty())
140  {
141  for (memoryx::ObjectClassPtr ptr : objectClasses)
142  {
144 
145  if (ptr->getName() == className)
146  {
147  return ptr;
148  }
149  }
150  }
151 
152  return NULL;
153 }
154 
155 SoNode*
157  const memoryx::ObjectClassPtr& objectClass,
158  const bool& collisionModel) const
159 {
160  std::unique_lock lock(mutexEntities);
162  objectClass->addWrapper(new memoryx::EntityWrappers::SimoxObjectWrapper(fileManager));
163 
164  simoxWrapper->getManipulationObject()->setGlobalPose(Eigen::Matrix4f::Identity());
165 
166  VirtualRobot::VisualizationNodePtr visNode;
167  bool hasModel = true;
168 
169  if (collisionModel)
170  {
171  VirtualRobot::CollisionModelPtr collisionModel = simoxWrapper->getCollisionModel();
172 
173  if (!collisionModel)
174  {
175  hasModel = false;
176  }
177  else
178  {
179  visNode = collisionModel->getVisualization();
180  }
181  }
182  else
183  {
184  visNode = simoxWrapper->getVisualization();
185  }
186 
187  if (!hasModel)
188  {
189  return NULL;
190  }
191 
192  return VirtualRobot::CoinVisualizationFactory::getCoinVisualization(visNode);
193 }
194 
195 std::map<std::string, std::string>
197  const memoryx::ObjectClassPtr& objectClass) const
198 {
199  std::unique_lock lock(mutexEntities);
200  std::map<std::string, std::string> allAttributes;
201 
202  if (!objectClass)
203  {
204  return allAttributes;
205  }
206 
207  memoryx::NameList attributeNames = objectClass->getAttributeNames();
208 
210  objectClass->addWrapper(new memoryx::EntityWrappers::SimoxObjectWrapper(fileManager));
211 
212  for (memoryx::NameList::const_iterator it = attributeNames.begin(); it != attributeNames.end();
213  ++it)
214  {
215  memoryx::EntityAttributeBasePtr attrValue = objectClass->getAttribute(*it);
216  armarx::VariantPtr attributeValue = armarx::VariantPtr::dynamicCast(attrValue->getValue());
217 
218  if (attributeValue && (attributeValue->getOutputValueOnly() != ""))
219  {
220  if (*it == "ManipulationFile")
221  {
222  allAttributes.insert(std::pair<std::string, std::string>(
223  *it, simoxWrapper->getManipulationObjectFileName()));
224  }
225  else if (*it == "IvFile" || *it == "IvFileCollision" || *it == "texFiles")
226  {
227  // Since the class SimoxObjectWrapper does not provide a way to get the file path of the IvFile, we have to use this workaround
228  try
229  {
230  std::string maniPath = simoxWrapper->getManipulationObjectFileName();
231  memoryx::MongoDBRefPtr mongoPtr =
232  attributeValue->getClass<memoryx::MongoDBRef>();
233  const memoryx::GridFileInterfacePrx prx =
234  databasePrx->getFileProxyById(mongoPtr->dbName, mongoPtr->docId);
235  if (!prx)
236  {
237  ARMARX_WARNING_S << "Could not find IvFile of objectClass "
238  << objectClass->getName() << VAROUT(mongoPtr->dbName)
239  << VAROUT(mongoPtr->docId);
240  allAttributes.insert(std::pair<std::string, std::string>(*it, ""));
241  continue;
242  }
243 
244  std::string ivFilePath =
245  maniPath.substr(0, maniPath.rfind("/") + 1) + prx->getFilename();
246 
247  allAttributes.insert(std::pair<std::string, std::string>(*it, ivFilePath));
248  }
249  catch (const armarx::InvalidTypeException&)
250  {
251  ARMARX_WARNING_S << "Could not find IvFile of objectClass "
252  << objectClass->getName();
253  allAttributes.insert(std::pair<std::string, std::string>(*it, ""));
254  }
255  }
256  else if (*it == "featureFile")
257  {
258  memoryx::EntityWrappers::TexturedRecognitionWrapperPtr texturedRecognitionWrapper =
259  objectClass->addWrapper(
261  allAttributes.insert(std::pair<std::string, std::string>(
262  *it, texturedRecognitionWrapper->getFeatureFile()));
263  }
264  else
265  {
266  allAttributes.insert(
267  std::pair<std::string, std::string>(*it, attributeValue->getOutputValueOnly()));
268  }
269  }
270  else
271  {
272  allAttributes.insert(std::pair<std::string, std::string>(*it, ""));
273  }
274  }
275 
276  return allAttributes;
277 }
278 
279 std::string
281  const memoryx::ObjectInstancePtr& objectInstance) const
282 {
283  std::unique_lock lock(mutexEntities);
284 
285  memoryx::NameList collectionNames = this->getCollectionNames();
286  std::map<memoryx::ObjectClassPtr, std::string> possibleObjectClasses;
287  std::string className = objectInstance->getMostProbableClass();
288 
289  for (std::vector<std::string>::const_iterator it = collectionNames.begin();
290  it != collectionNames.end();
291  ++it)
292  {
293  memoryx::ObjectClassPtr ptr = this->getObjectClassPtr(className, *it);
294 
295  if (ptr)
296  {
297  possibleObjectClasses.insert(std::pair<memoryx::ObjectClassPtr, std::string>(ptr, *it));
298  }
299  }
300 
301  if (possibleObjectClasses.size() > 1)
302  {
303  // this should never happen, in all collections the names of the object-class shuold be unique.
304  // if there are for some reason two or more object-classes with the same name. one of these will be returned "at random"
305 
306  return possibleObjectClasses.begin()->second;
307  }
308  else if (possibleObjectClasses.size() == 1)
309  {
310  return possibleObjectClasses.begin()->second;
311  }
312  else
313  {
314  return "";
315  }
316 }
memoryxcontroller::PriorKnowledgeController::~PriorKnowledgeController
~PriorKnowledgeController()
Destructor.
Definition: PriorKnowledgeController.cpp:84
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:280
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:156
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:196
IceInternal::Handle< ObjectClass >
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:570
memoryx::EntityWrappers::TexturedRecognitionWrapper
TexturedRecognitionWrapper offers a simplified access to the data of an object class or instance rela...
Definition: ObjectRecognitionWrapper.h:63
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:96
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:133
ARMARX_WARNING_S
#define ARMARX_WARNING_S
Definition: Logging.h:213
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:54
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:198
PriorKnowledgeController.h
memoryx::GridFileManager
GridFileManager provides utility functions for working with files in Mongo GridFS and links to them s...
Definition: GridFileManager.h:41
memoryxcontroller::PriorKnowledgeController::getCollectionNames
std::vector< std::string > getCollectionNames() const
Returns a list of the names of all collections in the database.
Definition: PriorKnowledgeController.cpp:89
MemoryXController.h
memoryxcontroller::PriorKnowledgeController::PriorKnowledgeController
PriorKnowledgeController(const memoryx::PriorKnowledgeInterfacePrx &priorKnowledgePrx)
Constructor.
Definition: PriorKnowledgeController.cpp:56
Application.h