31 namespace fs = std::filesystem;
46 memoryPrx = getProxy<WorkingMemoryInterfacePrx>(
"WorkingMemory");
47 objectInstancesMemoryPrx = memoryPrx->getObjectInstancesSegment();
49 longtermMemoryPrx = getProxy<LongtermMemoryInterfacePrx>(
"LongtermMemory");
51 const std::string sceneFile = getProperty<std::string>(
"SceneFile").getValue();
52 const std::string snapshotName = getProperty<std::string>(
"SnapshotName").getValue();
53 targetUnit = getProperty<LengthUnit>(
"TargetLengthUnit").getValue();
55 importXMLSnapshot(sceneFile);
57 if (longtermMemoryPrx->saveWorkingMemorySnapshot(snapshotName, memoryPrx))
59 ARMARX_INFO <<
"Snapshot " << snapshotName <<
" saved. Import complete!";
63 void XMLSceneImporter::importXMLSnapshot(
const std::string& fileName)
65 fs::path fpath(fileName);
67 if (!fs::exists(fpath))
69 ARMARX_ERROR <<
"File " << fileName <<
" does not exist! Aborting.";
73 std::ifstream infile(fpath.c_str());
74 std::string XMLString((std::istreambuf_iterator<char>(infile)), std::istreambuf_iterator<char>());
81 XMLDoc.
parse<0>(
const_cast<char*
>(XMLString.c_str()));
94 ARMARX_ERROR <<
"Malformed XML-file: <scene> Tag is missing";
103 ARMARX_ERROR <<
"Malformed XML-file: <scene> must be followed by a <nodes> tag.";
110 const ObjectInstanceBasePtr newObjectInstance = createObjectInstanceFromXML(entityNode);
112 if (newObjectInstance)
114 objectInstancesMemoryPrx->addEntity(newObjectInstance);
118 ARMARX_INFO <<
"File successfully imported: " << fileName;
122 ObjectInstanceBasePtr XMLSceneImporter::createObjectInstanceFromXML(
rapidxml::xml_node<>* xmlNode)
132 objectInstance->setExistenceCertainty(1.0f);
135 objectInstance->setClass(name, 1.0f);
138 armarx::FramedPositionBasePtr entityPosition = positionFromXML(xmlNode);
142 ARMARX_ERROR <<
"<position> tag is missing in node " << name;
146 objectInstance->setPosition(entityPosition);
149 armarx::FramedOrientationBasePtr entityOrientation = orientationFromXML(xmlNode);
151 if (entityOrientation)
153 objectInstance->setOrientation(entityOrientation);
156 return objectInstance;
168 Eigen::Vector3f position;
169 std::istringstream(positionNode->
first_attribute(
"x")->value()) >> position[0];
170 std::istringstream(positionNode->
first_attribute(
"y")->value()) >> position[1];
171 std::istringstream(positionNode->
first_attribute(
"z")->value()) >> position[2];
173 float scaleFactor = scaleFactorFromPositionXML(positionNode);
174 position *= scaleFactor;
179 armarx::FramedOrientationBasePtr XMLSceneImporter::orientationFromXML(
rapidxml::xml_node<>* xmlNode)
189 std::istringstream(quaternion->
first_attribute(
"qx")->value()) >> orientation.x();
190 std::istringstream(quaternion->
first_attribute(
"qy")->value()) >> orientation.y();
191 std::istringstream(quaternion->
first_attribute(
"qz")->value()) >> orientation.z();
192 std::istringstream(quaternion->
first_attribute(
"qw")->value()) >> orientation.w();
199 std::string unitstring =
"METER";
205 unitstring = std::string(unitAttribute->value());
210 if (unitstring ==
"METER")
215 if (unitstring ==
"CENTIMETER")
217 sourceFactor = 100.f;
219 else if (unitstring ==
"MILLIMETER")
221 sourceFactor = 1000.f;
225 std::stringstream strstr;
226 strstr << __FILE__ <<
" : " << __LINE__ <<
" (" << __FUNCTION__ <<
"): unknown unitstring '" << unitstring <<
"'";
227 throw std::invalid_argument {strstr.str()};
239 targetFactor = 100.f;
243 targetFactor = 1000.f;
246 std::stringstream strstr;
247 strstr << __FILE__ <<
" : " << __LINE__ <<
" (" << __FUNCTION__ <<
"): unknown targetUnit '" << targetUnit <<
"'";
248 throw std::invalid_argument {strstr.str()};
251 return targetFactor / sourceFactor;