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
45namespace 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);
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 */
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
#define ARMARXCOMPONENT_IMPORT_EXPORT
Default component property definition container.
Definition Component.h:70
ComponentPropertyDefinitions(std::string prefix, bool hasObjectNameParameter=true)
Definition Component.cpp:46
Baseclass for all ArmarX ManagedIceObjects requiring properties.
Definition Component.h:94
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
PropertyDefinition< PropertyType > & defineRequiredProperty(const std::string &name, const std::string &description="", PropertyDefinitionBase::PropertyConstness constness=PropertyDefinitionBase::eConstant)
The SimoxSceneImporterPropertyDefinitions class defines the command line properties of the SimoxScene...
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
std::string getDefaultName() const override
Retrieve default name of component.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
VirtualRobot headers.
IceUtil::Handle< MongoSerializer > MongoSerializerPtr
Definition EntityRef.h:41