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 
27 
28 #include <MemoryX/interface/components/WorkingMemoryInterface.h>
29 #include <MemoryX/interface/components/LongtermMemoryInterface.h>
30 #include <MemoryX/interface/components/PriorKnowledgeInterface.h>
31 #include <MemoryX/interface/components/CommonStorageInterface.h>
34 
35 #include <VirtualRobot/ManipulationObject.h>
36 #include <VirtualRobot/XML/SceneIO.h>
37 
38 #include <Inventor/nodes/SoNode.h>
39 #include <Inventor/nodes/SoGroup.h>
40 #include <Inventor/nodes/SoSeparator.h>
41 #include <Inventor/nodes/SoImage.h>
42 #include <Inventor/nodes/SoFile.h>
43 #include <Inventor/nodes/SoTexture2.h>
44 #include <Inventor/nodes/SoTexture3.h>
45 
46 
47 namespace memoryx
48 {
49  /**
50  * @brief The SimoxSceneImporterPropertyDefinitions class defines the command line properties of the SimoxSceneImporter
51  * @see ComponentPropertyDefinitions
52  */
55  {
56  public:
59  {
60  defineRequiredProperty<std::string>("SceneFile", "Name of Simox XML file")
61  .setCaseInsensitive(true);
62  defineRequiredProperty<std::string>("SnapshotName", "Name of snapshot (=MongoDB collection) to load objects to")
63  .setCaseInsensitive(true);
64  defineRequiredProperty<std::string>("FilesDbName", "Name of files database")
65  .setCaseInsensitive(true);
66  }
67  };
68 
69  /**
70  * @brief The SimoxSceneImporter class generates a LongtermMemory snapshot of a scene specified in the Simox Scene format
71  *
72  *
73  */
75  virtual public armarx::Component
76  {
77  public:
79 
80  // inherited from Component
81  std::string getDefaultName() const override
82  {
83  return "SimoxSceneImporter";
84  }
85  void onInitComponent() override;
86  void onConnectComponent() override;
87 
88  /**
89  * @see PropertyUser::createPropertyDefinitions()
90  */
92  {
95  getConfigIdentifier()));
96  }
97 
98  private:
99  /**
100  * @brief loadSceneFromXML parses \p fileName and creates a VirtualRobot::Scene object
101  * @param fileName Simox Scene description file (XML)
102  * @return VirtualRobot::Scene object if the contents of \p fileName was valid
103  */
104  VirtualRobot::ScenePtr loadSceneFromXML(const std::string& fileName);
105 
106  /**
107  * @brief importScene imports the given \p scene into the robot's working memory and creates a snapshot afterwards.
108  *
109  * Extracts all visualization files from \p scene and copies them into a temporary directory.
110  * Afterwards, the file references are updated to the new temporary location and the manipulation object files are written into the temporary directory.
111  * As a last step, those manipulation objects are turned into ObjectInstance instances and saved to the ObjectInstance segment of the WorkingMemory
112  * which is then used to create a snapshot of the scene.
113  *
114  * @param scene the scene to import into the memory
115  * @return true on success, false upon failure
116  */
117  bool importScene(VirtualRobot::ScenePtr scene);
118 
119  /**
120  * @brief getAbsoluteVisualizationFilenames collects all filenames required to visualize \p obstacle and stores them in \p allFilenames
121  * @param obstacle collect the visualization files for this object
122  * @param absoluteFilenames absolute paths to the visualization files are stored in this vector
123  */
124  void getAbsoluteVisualizationFilenames(VirtualRobot::ObstaclePtr obstacle, 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, const std::string& tmpPath, const std::string& origPath, std::map<std::string, std::string>& newFilenameMapping);
134 
135  /**
136  * @brief setNewVisualizationFilenames update visualization file references of \p obstacle. The mapping is provided by \p newFilenames
137  * @param obstacle the object to update the filenames on
138  * @param newFilenames mapping of old to new filenames
139  */
140  void setNewVisualizationFilenames(VirtualRobot::ObstaclePtr obstacle, std::map<std::string, std::string>& newFilenames);
141 
142  /**
143  * @brief saveObstacleAsManipulationObject serialize \p object into \p xmlfile storing all obstacles as ManipulationObject tags
144  * @param object the Obstacle instance to save to \p xmlFile
145  * @param xmlFile serialize \p object into this file
146  * @return true if writing was successful, false otherwise
147  */
148  bool saveObstacleAsManipulationObject(VirtualRobot::ObstaclePtr object, const std::string& xmlFile, const std::string& tmpPath);
149 
150  /**
151  * @brief createTempScene
152  *
153  * Loop over all Obstacle and ManipulationObject tags
154  * collect the visualization files (including itself)
155  * collect the collisionModel visualization files (including itself)
156  *
157  * copy all visualization files to the temp directory
158  *
159  * replace old visualization files with paths to the temp directory
160  *
161  * serialize all objects to files within the temp directory
162  *
163  * write out a scene.xml file and return its contents as ScenePtr
164  *
165  *
166  * @param tmpPath
167  * @param scene
168  * @return
169  */
170  VirtualRobot::ScenePtr createTempScene(const std::string& tmpPath, VirtualRobot::ScenePtr scene);
171 
172  /**
173  * @brief createObjectInstances creates object instances from \p obstacles, stores them to memory and adds a mapping to \p objectInstances.
174  * @param obstacles a vector of Obstacles to add to the working memory
175  * @param objectInstances stores a mapping of Obstacles and created ObjectInstance objects
176  */
177  template <typename T>
178  int createObjectInstances(std::vector<T> obstacles, std::map<VirtualRobot::ObstaclePtr, ObjectInstanceBasePtr>& objectInstances);
179 
180  /**
181  * @brief createInstance create an ObjectInstance instance from the SceneObject \p object
182  * @param sceneObject the object to convert
183  * @return NULL ObjectInstance if conversion failed, a valid ObjectInstance otherwise
184  */
185  ObjectInstanceBasePtr createInstance(VirtualRobot::SceneObjectPtr sceneObject);
186 
187  /**
188  * @brief importObjectsIntoPriorKnowledge adds all objects found in the imported scene as ObjectClass instances to PriorKnowledge
189  * @param objectInstances
190  */
191  void importObjectsIntoPriorKnowledge(std::map<VirtualRobot::ObstaclePtr, ObjectInstanceBasePtr>& objectInstances) ;
192 
193 
194  WorkingMemoryInterfacePrx memoryPrx;
195  ObjectInstanceMemorySegmentBasePrx objectInstancesMemoryPrx;
196  LongtermMemoryInterfacePrx longtermMemoryPrx;
197  PriorKnowledgeInterfacePrx priorKnowledgePrx;
198  PersistentObjectClassSegmentBasePrx classesSegmentPrx;
199  CommonStorageInterfacePrx dataBasePrx;
200 
201  MongoSerializerPtr dbSerializer;
202 
203  std::string filesDBName;
204  std::string sceneFile;
205  std::string snapshotName;
206 
207  const std::string TEMPDIR;
208  const std::string LONGTERM_SNAPSHOT_PREFIX;
209  };
210 
211 }
212 
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:333
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:74
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:57
Component.h
armarx::Component
Baseclass for all ArmarX ManagedIceObjects requiring properties.
Definition: Component.h:95
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:70
IceUtil::Handle< class PropertyDefinitionContainer >
memoryx::SimoxSceneImporter::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: SimoxSceneImporter.h:81
armarx::ComponentPropertyDefinitions::ComponentPropertyDefinitions
ComponentPropertyDefinitions(std::string prefix, bool hasObjectNameParameter=true)
Definition: Component.cpp:37
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:34
memoryx::SimoxSceneImporterPropertyDefinitions
The SimoxSceneImporterPropertyDefinitions class defines the command line properties of the SimoxScene...
Definition: SimoxSceneImporter.h:53
ImportExportComponent.h