32namespace fs = std::filesystem;
49 objectInstancesMemoryPrx = memoryPrx->getObjectInstancesSegment();
57 importXMLSnapshot(sceneFile);
59 if (longtermMemoryPrx->saveWorkingMemorySnapshot(snapshotName, memoryPrx))
61 ARMARX_INFO <<
"Snapshot " << snapshotName <<
" saved. Import complete!";
66 XMLSceneImporter::importXMLSnapshot(
const std::string& fileName)
68 fs::path fpath(fileName);
70 if (!fs::exists(fpath))
72 ARMARX_ERROR <<
"File " << fileName <<
" does not exist! Aborting.";
76 std::ifstream infile(fpath.c_str());
77 std::string XMLString((std::istreambuf_iterator<char>(infile)),
78 std::istreambuf_iterator<char>());
85 XMLDoc.
parse<0>(
const_cast<char*
>(XMLString.c_str()));
87 catch (rapidxml::parse_error& e)
94 rapidxml::xml_node<>* rootNode = XMLDoc.
first_node(
"scene");
98 ARMARX_ERROR <<
"Malformed XML-file: <scene> Tag is missing";
103 rapidxml::xml_node<>* sceneNodes = rootNode->
first_node(
"nodes");
107 ARMARX_ERROR <<
"Malformed XML-file: <scene> must be followed by a <nodes> tag.";
112 for (rapidxml::xml_node<>* entityNode = sceneNodes->
first_node(
"node"); entityNode;
115 const ObjectInstanceBasePtr newObjectInstance = createObjectInstanceFromXML(entityNode);
117 if (newObjectInstance)
119 objectInstancesMemoryPrx->addEntity(newObjectInstance);
123 ARMARX_INFO <<
"File successfully imported: " << fileName;
126 ObjectInstanceBasePtr
127 XMLSceneImporter::createObjectInstanceFromXML(rapidxml::xml_node<>* xmlNode)
137 objectInstance->setExistenceCertainty(1.0f);
140 objectInstance->setClass(name, 1.0f);
143 armarx::FramedPositionBasePtr entityPosition = positionFromXML(xmlNode);
147 ARMARX_ERROR <<
"<position> tag is missing in node " << name;
151 objectInstance->setPosition(entityPosition);
154 armarx::FramedOrientationBasePtr entityOrientation = orientationFromXML(xmlNode);
156 if (entityOrientation)
158 objectInstance->setOrientation(entityOrientation);
161 return objectInstance;
164 armarx::FramedPositionBasePtr
165 XMLSceneImporter::positionFromXML(rapidxml::xml_node<>* xmlNode)
167 rapidxml::xml_node<>* positionNode = xmlNode->
first_node(
"position");
174 Eigen::Vector3f position;
179 float scaleFactor = scaleFactorFromPositionXML(positionNode);
180 position *= scaleFactor;
185 armarx::FramedOrientationBasePtr
186 XMLSceneImporter::orientationFromXML(rapidxml::xml_node<>* xmlNode)
188 rapidxml::xml_node<>* quaternion = xmlNode->
first_node(
"rotation");
201 return new armarx::FramedOrientation(
206 XMLSceneImporter::scaleFactorFromPositionXML(rapidxml::xml_node<>* positionNode)
208 std::string unitstring =
"METER";
210 rapidxml::xml_attribute<char>* unitAttribute = positionNode->
first_attribute(
"unit");
214 unitstring = std::string(unitAttribute->
value());
219 if (unitstring ==
"METER")
224 if (unitstring ==
"CENTIMETER")
226 sourceFactor = 100.f;
228 else if (unitstring ==
"MILLIMETER")
230 sourceFactor = 1000.f;
234 std::stringstream strstr;
235 strstr << __FILE__ <<
" : " << __LINE__ <<
" (" << __FUNCTION__
236 <<
"): unknown unitstring '" << unitstring <<
"'";
237 throw std::invalid_argument{strstr.str()};
249 targetFactor = 100.f;
253 targetFactor = 1000.f;
256 std::stringstream strstr;
257 strstr << __FILE__ <<
" : " << __LINE__ <<
" (" << __FUNCTION__
258 <<
"): unknown targetUnit '" << targetUnit <<
"'";
259 throw std::invalid_argument{strstr.str()};
262 return targetFactor / sourceFactor;
Property< PropertyType > getProperty(const std::string &name)
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
void onInitComponent() override
Pure virtual hook for the subclass.
void onConnectComponent() override
Pure virtual hook for the subclass.
const char * what() const noexcept override
Gets human readable description of error.
Ch * value() const
Gets value of node.
This class represents root of the DOM hierarchy.
void parse(Ch *text)
Parses zero-terminated XML string according to given flags.
xml_node< Ch > * next_sibling(const Ch *name=nullptr, std::size_t name_size=0, bool case_sensitive=true) const
Gets next sibling node, optionally matching node name.
xml_node< Ch > * first_node(const Ch *name=nullptr, std::size_t name_size=0, bool case_sensitive=true) const
Gets first child node, optionally matching node name.
xml_attribute< Ch > * first_attribute(const Ch *name=nullptr, std::size_t name_size=0, bool case_sensitive=true) const
Gets first attribute of node, optionally matching attribute name.
#define ARMARX_INFO
The normal logging level.
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
std::string const GlobalFrame
Variable of the global coordinate system.
Quaternion< float, 0 > Quaternionf
IceInternal::Handle< FramedPosition > FramedPositionPtr
IceInternal::Handle< FramedOrientation > FramedOrientationPtr
IceInternal::Handle< ObjectInstance > ObjectInstancePtr