SimoxObjectWrapper.h
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 
23 #pragma once
24 
25 // boost
26 #include <filesystem>
27 #include <memory>
28 
29 // Simox-VirtualRobot
30 #include <VirtualRobot/ManipulationObject.h>
31 #include <VirtualRobot/Visualization/CoinVisualization/CoinVisualizationFactory.h>
32 #include <VirtualRobot/XML/RobotIO.h>
33 
34 // MemoryX
37 
39 {
40 
41  /**
42  * SimoxObjectWrapper offers a simplified access to the Simox ManipulationObject
43  * (i.e visualization, collision model, grasps) stored in the attributes of MemoryX entity.
44  *
45  */
47  {
48  public:
50  {
54  };
55 
56  /**
57  * Constructs new SimoxObjectWrapper.
58  *
59  * @param gfm GridFileManager to store/load files
60  * @param loadMode eFull or eCollisionModel. If eFull, then both, visualization and collision model is loaded.
61  * If eCollisionModel then only the collision model will be loaded.
62  *
63  */
65  const GridFileManagerPtr& gfm,
66  VirtualRobot::RobotIO::RobotDescription loadMode = VirtualRobot::RobotIO::eFull);
67  ~SimoxObjectWrapper() override;
68 
69  /**
70  * Retrieve visualization model.
71  *
72  * Corresponding files will be fetched and cached locally, if needed.
73  */
74  VirtualRobot::VisualizationNodePtr getVisualization() const;
75 
76  /**
77  * Retrieve collision model
78  *
79  * Corresponding files will be fetched and cached locally, if needed.
80  */
81  VirtualRobot::CollisionModelPtr getCollisionModel() const;
82 
83  /**
84  * Retrieve the complete Simox ManipulationObject
85  *
86  * Corresponding files will be fetched and cached locally, if needed.
87  */
88  VirtualRobot::ManipulationObjectPtr getManipulationObject() const;
89 
90  /**
91  * Retrieve the Simox MainpulationObjects file name in local cache. File is cached
92  * when this method is called.
93  *
94  */
95  std::string getManipulationObjectFileName() const;
96 
97  /**
98  * Store Simox ManipulationObject in the wrapped entity.
99  *
100  * Namely, this function does the following:
101  * \li 1. Clears previous manipulation object, if any. Corresponding files are deleted from GridFS as well.
102  * \li 2. Saves all files in the GridFS
103  * \li 2.1. Manipulation object XML file
104  * \li 2.2. Visualization model file (normally, *.iv)
105  * \li 2.3. Collision model file (normally, *.iv)
106  * \li 2.4. Any textures used by 2.2. and 2.3.
107  * \li 3. Stores references to the files from 2. in corresponding attributes of the wrapped enity
108  *
109  * @param mo manipulation object
110  * @param filesDBName Mongo database name to store additional files into
111  *
112  * @return MongoDB Id of the stored object
113  */
114  std::string setAndStoreManipulationObject(const VirtualRobot::ManipulationObjectPtr mo,
115  const std::string& filesDBName);
116 
117  /**
118  * Set Simox ManipulationObject in the wrapped entity. The object is not stored to GridFS.
119  *
120  * @param mo manipulation object
121  */
122  void setManipulationObject(const VirtualRobot::ManipulationObjectPtr mo);
123 
124  /**
125  * Create Simox ManipulationObject from an XML file and store it in the wrapped entity.
126  * See setManipulationObject for details.
127  *
128  * @param xmlFName name of XML file with manipulation object definition
129  * @param filesDBName Mongo database name to store additional files into
130  */
131  void setAndStoreManipulationFile(const std::string& xmlFName,
132  const std::string& filesDBName);
133 
134  /**
135  * Create Simox ManipulationObject from an XML file, but do not store it in the wrapped entity.
136  * See setManipulationObject for details.
137  *
138  * @param xmlFName name of XML file with manipulation object definition
139  */
140  void setManipulationFile(const std::string& xmlFName);
141 
142  /**
143  * Create Simox ManipulationObject from separate .iv models and store it in the wrapped entity.
144  * See setManipulationObject for details.
145  *
146  * @param ivFNameVis name of .iv file for visualization model
147  * @param ivFNameCollision name of .iv file for collision model
148  * @param filesDBName Mongo database name to store additional files into
149  */
150  void setAndStoreModelIVFiles(const std::string& ivFNameVis,
151  const std::string& ivFNameCollision,
152  const std::string& filesDBName);
153 
154  /**
155  * Create Simox ManipulationObject from separate .iv models, but do not store it in the wrapped entity.
156  * See setManipulationObject for details.
157  *
158  * @param ivFNameVis name of .iv file for visualization model
159  * @param ivFNameCollision name of .iv file for collision model
160  */
161  void setModelIVFiles(const std::string& ivFNameVis, const std::string& ivFNameCollision);
162 
163  /**
164  * Update the Simox object using attribute values (position, orientation etc) from the entity,
165  * but reusing the existing visualization. This method meant to be used with subsequent observations of
166  * the same entity. For new entity instances, create new SimoxObjectWrapper object instead.
167  *
168  * @param entity entity to get attribute values from
169  */
170  void updateFromEntity(const EntityBasePtr& entity);
171 
172 
173  /**
174  * Update object visualization using the current values of pose and position uncertainty from the wrapped entity
175  */
176  void refreshVisuPose();
177 
178  /**
179  * Set visualization type for position uncertainty.
180  *
181  * @param visuType one of: eEllipse - ellipsoids, full 3d uncertainty shown
182  * eHeatMap - flat colored map, XY uncertainty only
183  * eHeatSurface - 3d surface,
184  */
186 
187  /**
188  * Set parameters for uncertainty visualization
189  *
190  * @param ellipseRadiusInSigmas
191  * @param heatmapMinVariance
192  * @param heatmapDensityThreshold
193  *
194  */
195  void setUncertaintyVisuParams(float ellipseRadiusInSigmas,
196  float heatmapMinVariance,
197  float heatmapDensityThreshold,
198  int heatmapGridSize);
199 
200  /**
201  * Clears the Simox ManipulationObject and optionally removes the corresponding files from GridFS.
202  *
203  * @param removeFiles whether GridFS files (.iv models, textures, MO XML) should be deleted as well
204  */
205  void clear(bool removeFiles = true);
206 
207  /**
208  * @brief GetAllFilenames walks \p node and appends absolute paths of all found SoFile, SoImage, and SoTexture2 node filenames to \p storeFilenames.
209  * @param node SoNode which is analyzed
210  * @param storeFilenames vector to which the found filenames are appended
211  * @param origFile name of the original file used to load \p node (can be used to retrieve absolute paths like texture filenames which are stored as relative paths in Coin3D).
212  */
213  static void GetAllFilenames(SoNode* node,
214  std::vector<std::string>& storeFilenames,
215  const std::string& origFile = "");
216 
217  /**
218  * @brief GetAbsolutePath prepend \p filename with \p origFile and append it to \p storeFilenames (does nothing if \p filename is NULL)
219  * @param filename SbString of the filename to check
220  * @param origFile string containing the original filename
221  * @param storeFilenames vector filename strings to which the result should be appended
222  */
223  static std::string GetAbsolutePath(SbString filename, const std::string& origFile);
224 
225  /**
226  * @brief FindIvTextures scans the given \p ivFName for texture files and returns them in the \p textures string list
227  * @param ivFName path to Inventor file to scan for textures
228  * @param textures vector of strings containing the paths to texture files
229  */
230  static void FindIvTextures(const std::string& ivFName, NameList& textures);
231 
232  /**
233  * Returns the orientation of the object that it should have when it is being put down e.g. onto the table by the robot.
234  */
235  Eigen::Vector3f getPutdownOrientationRPY();
236  /**
237  * Sets the orientation of the object that it should have when it is being put down e.g. onto the table by the robot.
238  */
239  void setPutdownOrientationRPY(Eigen::Vector3f& rpy);
240  Ice::ObjectPtr ice_clone() const override;
241 
242  private:
243  VirtualRobot::CoinVisualizationFactoryPtr coinVisFactory;
244  mutable VirtualRobot::ManipulationObjectPtr simoxObject;
245  VirtualRobot::RobotIO::RobotDescription loadMode;
246  std::filesystem::path cachePath;
247  std::string moTempFile;
248 
249  UncertaintyVisuType uncertaintyVisuType;
250  float ellipseRadiusInSigmas;
251  float heatmapMinVariance;
252  float heatmapDensityThreshold;
253  int heatmapGridSize;
254 
255  void loadSimoxObject() const;
256 
257  std::string cacheAttributeFile(const std::string& attrName,
258  bool preserveOriginalFName = false) const;
259  bool cacheAttributeFiles(const std::string& attrName,
260  bool preserveOriginalFName = false) const;
261 
262  VirtualRobot::ManipulationObjectPtr
263  loadManipulationObjectFile(const std::string& xmlFName) const;
264  VirtualRobot::ManipulationObjectPtr
265  createManipulationObjectFromIvFiles(const std::string& objName,
266  const std::string& ivFNameVis,
267  const std::string& ivFNameCollision) const;
268 
269  std::string storeManipulationObject(const VirtualRobot::ManipulationObjectPtr mo,
270  const std::string& filesDBName);
271  void storeEntityIVFiles(const std::string& visuFName,
272  const std::string& collisionFName,
273  const std::string& filesDBName,
274  bool processTextures = true);
275 
276  VirtualRobot::VisualizationNodePtr
277  getUncertaintyEllipsesVisu(GaussianMixtureDistributionBasePtr posGM,
278  const Eigen::Vector3f& objPos,
279  const VirtualRobot::ColorMap& cmap,
280  float transparency);
281  VirtualRobot::VisualizationNodePtr
282  getUncertaintyHeatVisu(GaussianMixtureDistributionBasePtr posGM,
283  const Eigen::Vector3f& objPos,
284  bool heatSurface,
285  const VirtualRobot::ColorMap& cmap);
286 
287  float evaluate2DGaussian(const Eigen::Vector2f& point,
288  const Eigen::Vector2f& mean,
289  const Eigen::Matrix2f& covInv,
290  float covDetSqrtInv);
291  float evaluate3DGaussian(const Eigen::Vector3f& point,
292  const Eigen::Vector3f& mean,
293  const Eigen::Matrix3f& covInv,
294  float covDet);
295 
296  VirtualRobot::ColorMap getUncertaintyColorMap();
297  float getUncertaintyEllipseTransparency();
298  };
299 
301 
302 } // namespace memoryx::EntityWrappers
armarx::eFull
@ eFull
Definition: MorphingItem.h:35
memoryx::EntityWrappers::SimoxObjectWrapper::eHeatSurface
@ eHeatSurface
Definition: SimoxObjectWrapper.h:53
memoryx::EntityWrappers::SimoxObjectWrapper::refreshVisuPose
void refreshVisuPose()
Update object visualization using the current values of pose and position uncertainty from the wrappe...
Definition: SimoxObjectWrapper.cpp:325
memoryx::EntityWrappers::SimoxObjectWrapper::setModelIVFiles
void setModelIVFiles(const std::string &ivFNameVis, const std::string &ivFNameCollision)
Create Simox ManipulationObject from separate .iv models, but do not store it in the wrapped entity.
Definition: SimoxObjectWrapper.cpp:222
memoryx::EntityWrappers::SimoxObjectWrapper::getPutdownOrientationRPY
Eigen::Vector3f getPutdownOrientationRPY()
Returns the orientation of the object that it should have when it is being put down e....
Definition: SimoxObjectWrapper.cpp:977
memoryx::EntityWrappers::SimoxObjectWrapper::setUncertaintyVisuParams
void setUncertaintyVisuParams(float ellipseRadiusInSigmas, float heatmapMinVariance, float heatmapDensityThreshold, int heatmapGridSize)
Set parameters for uncertainty visualization.
Definition: SimoxObjectWrapper.cpp:237
GridFileManager.h
memoryx::EntityWrappers::SimoxObjectWrapper::FindIvTextures
static void FindIvTextures(const std::string &ivFName, NameList &textures)
FindIvTextures scans the given ivFName for texture files and returns them in the textures string list...
Definition: SimoxObjectWrapper.cpp:959
memoryx::EntityWrappers::SimoxObjectWrapper::setManipulationObject
void setManipulationObject(const VirtualRobot::ManipulationObjectPtr mo)
Set Simox ManipulationObject in the wrapped entity.
Definition: SimoxObjectWrapper.cpp:176
memoryx::EntityWrappers::SimoxObjectWrapper::ice_clone
Ice::ObjectPtr ice_clone() const override
Definition: SimoxObjectWrapper.cpp:106
memoryx::EntityWrappers::SimoxObjectWrapper::clear
void clear(bool removeFiles=true)
Clears the Simox ManipulationObject and optionally removes the corresponding files from GridFS.
Definition: SimoxObjectWrapper.cpp:674
memoryx::EntityWrappers::SimoxObjectWrapper::eEllipse
@ eEllipse
Definition: SimoxObjectWrapper.h:51
memoryx::EntityWrappers::SimoxObjectWrapper::getManipulationObject
VirtualRobot::ManipulationObjectPtr getManipulationObject() const
Retrieve the complete Simox ManipulationObject.
Definition: SimoxObjectWrapper.cpp:143
memoryx::EntityWrappers::SimoxObjectWrapper::setAndStoreManipulationObject
std::string setAndStoreManipulationObject(const VirtualRobot::ManipulationObjectPtr mo, const std::string &filesDBName)
Store Simox ManipulationObject in the wrapped entity.
Definition: SimoxObjectWrapper.cpp:168
memoryx::EntityWrappers::SimoxObjectWrapper::SimoxObjectWrapper
SimoxObjectWrapper(const GridFileManagerPtr &gfm, VirtualRobot::RobotIO::RobotDescription loadMode=VirtualRobot::RobotIO::eFull)
Constructs new SimoxObjectWrapper.
Definition: SimoxObjectWrapper.cpp:80
memoryx::EntityWrappers::SimoxObjectWrapper::getCollisionModel
VirtualRobot::CollisionModelPtr getCollisionModel() const
Retrieve collision model.
Definition: SimoxObjectWrapper.cpp:127
IceInternal::Handle< SimoxObjectWrapper >
memoryx::EntityWrappers::SimoxObjectWrapper::getVisualization
VirtualRobot::VisualizationNodePtr getVisualization() const
Retrieve visualization model.
Definition: SimoxObjectWrapper.cpp:112
armarx::mean
std::optional< float > mean(const boost::circular_buffer< NameValueMap > &buffer, const std::string &key)
Definition: KinematicUnitGuiPlugin.cpp:1620
GfxTL::Matrix2f
MatrixXX< 2, 2, float > Matrix2f
Definition: MatrixXX.h:648
memoryx::EntityWrappers::SimoxObjectWrapper::setPutdownOrientationRPY
void setPutdownOrientationRPY(Eigen::Vector3f &rpy)
Sets the orientation of the object that it should have when it is being put down e....
Definition: SimoxObjectWrapper.cpp:1001
memoryx::EntityWrappers::AbstractFileEntityWrapper
Definition: AbstractEntityWrapper.h:81
AbstractEntityWrapper.h
filename
std::string filename
Definition: VisualizationRobot.cpp:86
memoryx::EntityWrappers::SimoxObjectWrapper::UncertaintyVisuType
UncertaintyVisuType
Definition: SimoxObjectWrapper.h:49
memoryx::EntityWrappers::SimoxObjectWrapper
SimoxObjectWrapper offers a simplified access to the Simox ManipulationObject (i.e visualization,...
Definition: SimoxObjectWrapper.h:46
memoryx::EntityWrappers::SimoxObjectWrapper::setUncertaintyVisuType
void setUncertaintyVisuType(UncertaintyVisuType visuType)
Set visualization type for position uncertainty.
Definition: SimoxObjectWrapper.cpp:231
memoryx::EntityWrappers::SimoxObjectWrapper::setAndStoreManipulationFile
void setAndStoreManipulationFile(const std::string &xmlFName, const std::string &filesDBName)
Create Simox ManipulationObject from an XML file and store it in the wrapped entity.
Definition: SimoxObjectWrapper.cpp:183
memoryx::EntityWrappers::SimoxObjectWrapper::~SimoxObjectWrapper
~SimoxObjectWrapper() override
Definition: SimoxObjectWrapper.cpp:101
memoryx::EntityWrappers
Definition: AbstractEntityWrapper.cpp:30
memoryx::GridFileManagerPtr
std::shared_ptr< GridFileManager > GridFileManagerPtr
Definition: AbstractEntityWrapper.h:33
memoryx::EntityWrappers::SimoxObjectWrapper::eHeatMap
@ eHeatMap
Definition: SimoxObjectWrapper.h:52
memoryx::EntityWrappers::SimoxObjectWrapper::setAndStoreModelIVFiles
void setAndStoreModelIVFiles(const std::string &ivFNameVis, const std::string &ivFNameCollision, const std::string &filesDBName)
Create Simox ManipulationObject from separate .iv models and store it in the wrapped entity.
Definition: SimoxObjectWrapper.cpp:211
memoryx::EntityWrappers::SimoxObjectWrapper::updateFromEntity
void updateFromEntity(const EntityBasePtr &entity)
Update the Simox object using attribute values (position, orientation etc) from the entity,...
Definition: SimoxObjectWrapper.cpp:317
armarx::aron::type::ObjectPtr
std::shared_ptr< Object > ObjectPtr
Definition: Object.h:36
GfxTL::Matrix3f
MatrixXX< 3, 3, float > Matrix3f
Definition: MatrixXX.h:649
memoryx::EntityWrappers::SimoxObjectWrapper::GetAllFilenames
static void GetAllFilenames(SoNode *node, std::vector< std::string > &storeFilenames, const std::string &origFile="")
GetAllFilenames walks node and appends absolute paths of all found SoFile, SoImage,...
Definition: SimoxObjectWrapper.cpp:1010
memoryx::EntityWrappers::SimoxObjectWrapper::getManipulationObjectFileName
std::string getManipulationObjectFileName() const
Retrieve the Simox MainpulationObjects file name in local cache.
Definition: SimoxObjectWrapper.cpp:156
memoryx::EntityWrappers::SimoxObjectWrapper::GetAbsolutePath
static std::string GetAbsolutePath(SbString filename, const std::string &origFile)
GetAbsolutePath prepend filename with origFile and append it to storeFilenames (does nothing if filen...
Definition: SimoxObjectWrapper.cpp:1125
memoryx::EntityWrappers::SimoxObjectWrapper::setManipulationFile
void setManipulationFile(const std::string &xmlFName)
Create Simox ManipulationObject from an XML file, but do not store it in the wrapped entity.
Definition: SimoxObjectWrapper.cpp:200