SimoxSceneImporter.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::SimoxSceneImporter
17 * @author (vahrenkamp at kit dot edu)
18 * @date 2013
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #pragma once
24 
25 #include <VirtualRobot/ManipulationObject.h>
26 #include <VirtualRobot/XML/SceneIO.h>
27 
30 
31 #include <Inventor/nodes/SoFile.h>
32 #include <Inventor/nodes/SoGroup.h>
33 #include <Inventor/nodes/SoImage.h>
34 #include <Inventor/nodes/SoNode.h>
35 #include <Inventor/nodes/SoSeparator.h>
36 #include <Inventor/nodes/SoTexture2.h>
37 #include <Inventor/nodes/SoTexture3.h>
40 #include <MemoryX/interface/components/CommonStorageInterface.h>
41 #include <MemoryX/interface/components/LongtermMemoryInterface.h>
42 #include <MemoryX/interface/components/PriorKnowledgeInterface.h>
43 #include <MemoryX/interface/components/WorkingMemoryInterface.h>
44 
45 namespace memoryx
46 {
47  /**
48  * @brief The SimoxSceneImporterPropertyDefinitions class defines the command line properties of the SimoxSceneImporter
49  * @see ComponentPropertyDefinitions
50  */
52  {
53  public:
56  {
57  defineRequiredProperty<std::string>("SceneFile", "Name of Simox XML file")
58  .setCaseInsensitive(true);
59  defineRequiredProperty<std::string>(
60  "SnapshotName", "Name of snapshot (=MongoDB collection) to load objects to")
61  .setCaseInsensitive(true);
62  defineRequiredProperty<std::string>("FilesDbName", "Name of files database")
63  .setCaseInsensitive(true);
64  }
65  };
66 
67  /**
68  * @brief The SimoxSceneImporter class generates a LongtermMemory snapshot of a scene specified in the Simox Scene format
69  *
70  *
71  */
73  {
74  public:
76 
77  // inherited from Component
78  std::string
79  getDefaultName() const override
80  {
81  return "SimoxSceneImporter";
82  }
83 
84  void onInitComponent() override;
85  void onConnectComponent() override;
86 
87  /**
88  * @see PropertyUser::createPropertyDefinitions()
89  */
92  {
94  new SimoxSceneImporterPropertyDefinitions(getConfigIdentifier()));
95  }
96 
97  private:
98  /**
99  * @brief loadSceneFromXML parses \p fileName and creates a VirtualRobot::Scene object
100  * @param fileName Simox Scene description file (XML)
101  * @return VirtualRobot::Scene object if the contents of \p fileName was valid
102  */
103  VirtualRobot::ScenePtr loadSceneFromXML(const std::string& fileName);
104 
105  /**
106  * @brief importScene imports the given \p scene into the robot's working memory and creates a snapshot afterwards.
107  *
108  * Extracts all visualization files from \p scene and copies them into a temporary directory.
109  * Afterwards, the file references are updated to the new temporary location and the manipulation object files are written into the temporary directory.
110  * As a last step, those manipulation objects are turned into ObjectInstance instances and saved to the ObjectInstance segment of the WorkingMemory
111  * which is then used to create a snapshot of the scene.
112  *
113  * @param scene the scene to import into the memory
114  * @return true on success, false upon failure
115  */
116  bool importScene(VirtualRobot::ScenePtr scene);
117 
118  /**
119  * @brief getAbsoluteVisualizationFilenames collects all filenames required to visualize \p obstacle and stores them in \p allFilenames
120  * @param obstacle collect the visualization files for this object
121  * @param absoluteFilenames absolute paths to the visualization files are stored in this vector
122  */
123  void getAbsoluteVisualizationFilenames(VirtualRobot::ObstaclePtr obstacle,
124  std::vector<std::string>& absoluteFilenames);
125 
126  /**
127  * @brief createTempFiles copy all \p files to \p tmpPath. \p origPath is used to calculate relative paths. The mapping is stored in \p newFilenames
128  * @param files vector of files to copy to \p tmpPath
129  * @param tmpPath the directory where the files should be copied to
130  * @param origPath substract this path from the original filename so the relative paths are the same within \p tmpPath
131  * @param newFilenameMapping store the mapping of old/new filename in this map
132  */
133  void copyFilesToTempDir(const std::vector<std::string>& files,
134  const std::string& tmpPath,
135  const std::string& origPath,
136  std::map<std::string, std::string>& newFilenameMapping);
137 
138  /**
139  * @brief setNewVisualizationFilenames update visualization file references of \p obstacle. The mapping is provided by \p newFilenames
140  * @param obstacle the object to update the filenames on
141  * @param newFilenames mapping of old to new filenames
142  */
143  void setNewVisualizationFilenames(VirtualRobot::ObstaclePtr obstacle,
144  std::map<std::string, std::string>& newFilenames);
145 
146  /**
147  * @brief saveObstacleAsManipulationObject serialize \p object into \p xmlfile storing all obstacles as ManipulationObject tags
148  * @param object the Obstacle instance to save to \p xmlFile
149  * @param xmlFile serialize \p object into this file
150  * @return true if writing was successful, false otherwise
151  */
152  bool saveObstacleAsManipulationObject(VirtualRobot::ObstaclePtr object,
153  const std::string& xmlFile,
154  const std::string& tmpPath);
155 
156  /**
157  * @brief createTempScene
158  *
159  * Loop over all Obstacle and ManipulationObject tags
160  * collect the visualization files (including itself)
161  * collect the collisionModel visualization files (including itself)
162  *
163  * copy all visualization files to the temp directory
164  *
165  * replace old visualization files with paths to the temp directory
166  *
167  * serialize all objects to files within the temp directory
168  *
169  * write out a scene.xml file and return its contents as ScenePtr
170  *
171  *
172  * @param tmpPath
173  * @param scene
174  * @return
175  */
176  VirtualRobot::ScenePtr createTempScene(const std::string& tmpPath,
177  VirtualRobot::ScenePtr scene);
178 
179  /**
180  * @brief createObjectInstances creates object instances from \p obstacles, stores them to memory and adds a mapping to \p objectInstances.
181  * @param obstacles a vector of Obstacles to add to the working memory
182  * @param objectInstances stores a mapping of Obstacles and created ObjectInstance objects
183  */
184  template <typename T>
185  int createObjectInstances(
186  std::vector<T> obstacles,
187  std::map<VirtualRobot::ObstaclePtr, ObjectInstanceBasePtr>& objectInstances);
188 
189  /**
190  * @brief createInstance create an ObjectInstance instance from the SceneObject \p object
191  * @param sceneObject the object to convert
192  * @return NULL ObjectInstance if conversion failed, a valid ObjectInstance otherwise
193  */
194  ObjectInstanceBasePtr createInstance(VirtualRobot::SceneObjectPtr sceneObject);
195 
196  /**
197  * @brief importObjectsIntoPriorKnowledge adds all objects found in the imported scene as ObjectClass instances to PriorKnowledge
198  * @param objectInstances
199  */
200  void importObjectsIntoPriorKnowledge(
201  std::map<VirtualRobot::ObstaclePtr, ObjectInstanceBasePtr>& objectInstances);
202 
203 
204  WorkingMemoryInterfacePrx memoryPrx;
205  ObjectInstanceMemorySegmentBasePrx objectInstancesMemoryPrx;
206  LongtermMemoryInterfacePrx longtermMemoryPrx;
207  PriorKnowledgeInterfacePrx priorKnowledgePrx;
208  PersistentObjectClassSegmentBasePrx classesSegmentPrx;
209  CommonStorageInterfacePrx dataBasePrx;
210 
211  MongoSerializerPtr dbSerializer;
212 
213  std::string filesDBName;
214  std::string sceneFile;
215  std::string snapshotName;
216 
217  const std::string TEMPDIR;
218  const std::string LONGTERM_SNAPSHOT_PREFIX;
219  };
220 
221 } // namespace memoryx
files
Use of this software is granted under one of the following two to be chosen freely by the user Boost Software License Version Marcin Kalicinski Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do all subject to the including the above license this restriction and the following must be included in all copies of the in whole or in and all derivative works of the unless such copies or derivative works are solely in the form of machine executable object code generated by a source language processor THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF FITNESS FOR A PARTICULAR TITLE AND NON INFRINGEMENT IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER WHETHER IN TORT OR ARISING OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE The MIT Marcin Kalicinski Permission is hereby free of to any person obtaining a copy of this software and associated documentation files(the "Software")
memoryx::SimoxSceneImporter::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: SimoxSceneImporter.h:91
armarx::PropertyDefinitionContainer::prefix
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
Definition: PropertyDefinitionContainer.h:345
MongoSerializer.h
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
memoryx::SimoxSceneImporter
The SimoxSceneImporter class generates a LongtermMemory snapshot of a scene specified in the Simox Sc...
Definition: SimoxSceneImporter.h:72
scene3D::ScenePtr
std::shared_ptr< Scene > ScenePtr
Definition: PointerDefinitions.h:36
ARMARXCOMPONENT_IMPORT_EXPORT
#define ARMARXCOMPONENT_IMPORT_EXPORT
Definition: ImportExportComponent.h:38
EntityAttribute.h
memoryx::SimoxSceneImporterPropertyDefinitions::SimoxSceneImporterPropertyDefinitions
SimoxSceneImporterPropertyDefinitions(std::string prefix)
Definition: SimoxSceneImporter.h:54
Component.h
armarx::Component
Baseclass for all ArmarX ManagedIceObjects requiring properties.
Definition: Component.h:91
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:69
IceUtil::Handle< class PropertyDefinitionContainer >
memoryx::SimoxSceneImporter::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: SimoxSceneImporter.h:79
armarx::ComponentPropertyDefinitions::ComponentPropertyDefinitions
ComponentPropertyDefinitions(std::string prefix, bool hasObjectNameParameter=true)
Definition: Component.cpp:35
scene3D::SceneObjectPtr
boost::intrusive_ptr< SceneObject > SceneObjectPtr
Definition: PointerDefinitions.h:40
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:35
memoryx::SimoxSceneImporterPropertyDefinitions
The SimoxSceneImporterPropertyDefinitions class defines the command line properties of the SimoxScene...
Definition: SimoxSceneImporter.h:51
ImportExportComponent.h