38 #include <QTextStream>
51 mConfigurationFilePath(absoluteConfigFilePath), mLoggingFolder(loggingFolder), mConfigTag(
52 "configuration"), mConfigTrackerTag(
"tracker"), mConfigTrackerToolFile(
"toolfile"), mTypeAttribute(
53 "type"), mClinicalAppAttribute(
"clinical_app"), mReferenceAttribute(
"reference")
55 this->setConfigDocument(mConfigurationFilePath);
64 if (!this->isConfigFileValid())
67 QDomNode configNode = mConfigureDoc.elementsByTagName(mConfigTag).at(0);
68 QString retval = configNode.toElement().attribute(mClinicalAppAttribute);
74 std::vector<IgstkTracker::InternalStructure> retval;
76 if (!this->isConfigFileValid())
79 QDomNodeList trackerNodes = mConfigureDoc.elementsByTagName(mConfigTrackerTag);
80 for (
int i = 0; i < trackerNodes.count(); ++i)
83 QString trackerType = trackerNodes.at(i).toElement().attribute(mTypeAttribute);
84 internalStructure.
mType = string2enum<TRACKING_SYSTEM>(trackerType);
87 retval.push_back(internalStructure);
90 if (retval.size() > 1)
92 "Config file " + mConfigurationFilePath
93 +
" has a invalid number of tracking systems, we only support 1 tracking system atm!");
100 std::vector<QString> retval;
102 if (!this->isConfigFileValid())
105 QDomNodeList toolFileNodes = mConfigureDoc.elementsByTagName(mConfigTrackerToolFile);
106 for (
int i = 0; i < toolFileNodes.count(); ++i)
108 QString absoluteToolFilePath = this->getAbsoluteToolFilePath(toolFileNodes.at(i).toElement());
109 if (absoluteToolFilePath.isEmpty())
112 retval.push_back(absoluteToolFilePath);
122 if (!this->isConfigFileValid())
129 QDomNodeList toolFileNodes = mConfigureDoc.elementsByTagName(mConfigTrackerToolFile);
130 for (
int i = 0; i < toolFileNodes.count(); ++i)
132 QString reference = toolFileNodes.at(i).toElement().attribute(mReferenceAttribute);
133 if (reference.contains(
"yes", Qt::CaseInsensitive))
136 retval = this->getAbsoluteToolFilePath(toolFileNodes.at(i).toElement());
148 QString ConfigurationFileParser::convertToRelativeToolFilePath(QString configFilename, QString absoluteToolFilePath)
150 foreach (QString root,
profile()->getAllRootConfigPaths())
152 QString configPath = getToolPathFromRoot(root);
153 if (!absoluteToolFilePath.contains(configPath))
155 absoluteToolFilePath.replace(configPath,
"");
156 if (absoluteToolFilePath.startsWith(
"/"))
157 absoluteToolFilePath.remove(0, 1);
158 return absoluteToolFilePath;
162 return absoluteToolFilePath;
165 QString ConfigurationFileParser::getToolPathFromRoot(QString root)
167 return root +
"/tool/Tools/";
173 doc.appendChild(doc.createProcessingInstruction(
"xml version =",
"\"1.0\""));
175 QDomElement configNode = doc.createElement(
"configuration");
176 configNode.setAttribute(
"clinical_app", config.
mClinical_app);
182 QDomElement trackerTagNode = doc.createElement(
"tracker");
183 trackerTagNode.setAttribute(
"type", trackerType);
185 ToolFilesAndReferenceVector::iterator it2 = it1->second.begin();
186 for (; it2 != it1->second.end(); ++it2)
188 QString absoluteToolFilePath = it2->first;
189 QString relativeToolFilePath = convertToRelativeToolFilePath(config.
mFileName, absoluteToolFilePath);
195 reportWarning(
"When saving configuration, skipping tool " + relativeToolFilePath +
" of type "
196 + toolTrackerType +
" because tracker is set to " + trackerType);
200 QDomElement toolFileNode = doc.createElement(
"toolfile");
201 toolFileNode.appendChild(doc.createTextNode(relativeToolFilePath));
202 toolFileNode.setAttribute(
"reference", (it2->second ?
"yes" :
"no"));
203 trackerTagNode.appendChild(toolFileNode);
205 configNode.appendChild(trackerTagNode);
208 doc.appendChild(configNode);
212 QDir().mkpath(QFileInfo(config.
mFileName).absolutePath());
214 if (!file.open(QIODevice::WriteOnly))
216 reportWarning(
"Could not open file " + file.fileName() +
", aborting writing of config.");
219 QTextStream stream(&file);
221 reportSuccess(
"Configuration file " + file.fileName() +
" is written.");
224 void ConfigurationFileParser::setConfigDocument(QString configAbsoluteFilePath)
226 QFile configFile(configAbsoluteFilePath);
227 if (!configFile.exists())
233 if (!mConfigureDoc.setContent(&configFile))
235 reportError(
"Could not set the xml content of the config file " + configAbsoluteFilePath);
240 bool ConfigurationFileParser::isConfigFileValid()
243 QDomNode configNode = mConfigureDoc.elementsByTagName(mConfigTag).item(0);
244 if (configNode.isNull())
252 QString ConfigurationFileParser::findXmlFileWithDirNameInPath(QString path)
256 filter << dir.dirName() +
".xml";
257 QStringList filepaths = dir.entryList(filter);
258 if (!filepaths.isEmpty())
259 return dir.absoluteFilePath(filter[0]);
263 QString ConfigurationFileParser::searchForExistingToolFilePath(QString relativeToolFilePath)
266 relativeToolFilePath.replace(
"../Tools/",
"");
268 foreach (QString root,
profile()->getAllRootConfigPaths())
270 QString configPath = this->getToolPathFromRoot(root);
271 QFileInfo guess(configPath +
"/" + relativeToolFilePath);
273 return guess.canonicalFilePath();
278 QString ConfigurationFileParser::getAbsoluteToolFilePath(QDomElement toolfileelement)
280 QString relativeToolFilePath = toolfileelement.text();
281 if (relativeToolFilePath.isEmpty())
284 QString absoluteToolFilePath = this->searchForExistingToolFilePath(relativeToolFilePath);
286 QFileInfo info(absoluteToolFilePath);
288 reportError(QString(
"Tool file %1 in configuration %2 not found. Skipping.")
289 .arg(relativeToolFilePath)
290 .arg(mConfigurationFilePath));
293 absoluteToolFilePath = this->findXmlFileWithDirNameInPath(absoluteToolFilePath);
294 return absoluteToolFilePath;
299 mToolFilePath(absoluteToolFilePath), mLoggingFolder(loggingFolder), mToolTag(
"tool"), mToolTypeTag(
300 "type"), mToolIdTag(
"id"), mToolNameTag(
"name"), mToolDescriptionTag(
"description"), mToolManufacturerTag(
301 "manufacturer"), mToolClinicalAppTag(
"clinical_app"), mToolGeoFileTag(
"geo_file"), mToolPicFileTag(
302 "pic_file"), mToolDocFileTag(
"doc_file"), mToolInstrumentTag(
"instrument"), mToolInstrumentTypeTag(
303 "type"), mToolInstrumentIdTag(
"id"), mToolInstrumentNameTag(
"name"), mToolInstrumentManufacturerTag(
304 "manufacturer"), mToolInstrumentScannerIdTag(
"scannerid"), mToolInstrumentDescriptionTag(
305 "description"), mToolSensorTag(
"sensor"), mToolSensorTypeTag(
"type"), mToolSensorIdTag(
306 "id"), mToolSensorNameTag(
"name"), mToolSensorWirelessTag(
"wireless"), mToolSensorDOFTag(
307 "DOF"), mToolSensorPortnumberTag(
"portnumber"), mToolSensorChannelnumberTag(
308 "channelnumber"), mToolSensorReferencePointTag(
"reference_point"), mToolSensorManufacturerTag(
309 "manufacturer"), mToolSensorDescriptionTag(
"description"), mToolSensorRomFileTag(
310 "rom_file"), mToolCalibrationTag(
"calibration"), mToolCalibrationFileTag(
"cal_file")
322 QFile toolFile(mToolFilePath);
323 QString toolFolderAbsolutePath = QFileInfo(toolFile).dir().absolutePath() +
"/";
324 QDomNode toolNode = this->getToolNode(mToolFilePath);
326 if (toolNode.isNull())
329 "Could not read the <tool> tag of file: " + mToolFilePath
330 +
", this is not a tool file, skipping.");
334 QDomElement toolTypeElement = toolNode.firstChildElement(mToolTypeTag);
335 QString toolTypeText = toolTypeElement.text();
337 internalStructure.
mIsReference = toolTypeText.contains(
"reference", Qt::CaseInsensitive);
338 internalStructure.
mIsPointer = toolTypeText.contains(
"pointer", Qt::CaseInsensitive);
339 internalStructure.
mIsProbe = toolTypeText.contains(
"usprobe", Qt::CaseInsensitive);
355 QDomElement toolIdElement = toolNode.firstChildElement(mToolIdTag);
356 QString toolIdText = toolIdElement.text();
357 internalStructure.
mUid = toolIdText;
359 QDomElement toolNameElement = toolNode.firstChildElement(mToolNameTag);
360 QString toolNameText = toolNameElement.text();
361 internalStructure.
mName = toolNameText;
363 QDomElement toolClinicalAppElement = toolNode.firstChildElement(mToolClinicalAppTag);
364 QString toolClinicalAppText = toolClinicalAppElement.text();
365 QStringList applicationList = toolClinicalAppText.split(
" ");
366 foreach(QString
string, applicationList)
368 if (
string.isEmpty())
370 string =
string.toLower();
374 QDomElement toolGeofileElement = toolNode.firstChildElement(mToolGeoFileTag);
375 QString toolGeofileText = toolGeofileElement.text();
376 if (!toolGeofileText.isEmpty())
377 toolGeofileText = toolFolderAbsolutePath + toolGeofileText;
380 QDomElement toolPicfileElement = toolNode.firstChildElement(mToolPicFileTag);
381 QString toolPicfileText = toolPicfileElement.text();
382 if (!toolPicfileText.isEmpty())
383 toolPicfileText = toolFolderAbsolutePath + toolPicfileText;
386 QDomElement toolInstrumentElement = toolNode.firstChildElement(mToolInstrumentTag);
387 if (toolInstrumentElement.isNull())
390 "Could not find the <instrument> tag under the <tool> tag. Aborting this tool.");
393 QDomElement toolInstrumentIdElement = toolInstrumentElement.firstChildElement(mToolInstrumentIdTag);
394 QString toolInstrumentIdText = toolInstrumentIdElement.text();
397 QDomElement toolInstrumentScannerIdElement = toolInstrumentElement.firstChildElement(mToolInstrumentScannerIdTag);
398 QString toolInstrumentScannerIdText = toolInstrumentScannerIdElement.text();
401 QDomElement toolSensorElement = toolNode.firstChildElement(mToolSensorTag);
402 if (toolSensorElement.isNull())
404 reportError(
"Could not find the <sensor> tag under the <tool> tag. Aborting this tool.");
407 QDomElement toolSensorTypeElement = toolSensorElement.firstChildElement(mToolSensorTypeTag);
408 QString toolSensorTypeText = toolSensorTypeElement.text();
409 internalStructure.
mTrackerType = string2enum<TRACKING_SYSTEM>(toolSensorTypeText);
411 QDomElement toolSensorWirelessElement = toolSensorElement.firstChildElement(mToolSensorWirelessTag);
412 QString toolSensorWirelessText = toolSensorWirelessElement.text();
413 if (toolSensorWirelessText.contains(
"yes", Qt::CaseInsensitive))
415 else if (toolSensorWirelessText.contains(
"no", Qt::CaseInsensitive))
418 QDomElement toolSensorDOFElement = toolSensorElement.firstChildElement(mToolSensorDOFTag);
419 QString toolSensorDOFText = toolSensorDOFElement.text();
420 if (toolSensorDOFText.contains(
"5", Qt::CaseInsensitive))
421 internalStructure.
m5DOF =
true;
422 else if (toolSensorDOFText.contains(
"6", Qt::CaseInsensitive))
423 internalStructure.
m5DOF =
false;
425 QDomElement toolSensorPortnumberElement = toolSensorElement.firstChildElement(mToolSensorPortnumberTag);
426 QString toolSensorPortnumberText = toolSensorPortnumberElement.text();
427 internalStructure.
mPortNumber = toolSensorPortnumberText.toInt();
429 QDomElement toolSensorChannelnumberElement = toolSensorElement.firstChildElement(mToolSensorChannelnumberTag);
430 QString toolSensorChannelnumberText = toolSensorChannelnumberElement.text();
431 internalStructure.
mChannelNumber = toolSensorChannelnumberText.toInt();
433 QDomNodeList toolSensorReferencePointList = toolSensorElement.elementsByTagName(mToolSensorReferencePointTag);
434 for (
int j = 0; j < toolSensorReferencePointList.count(); j++)
436 QDomNode node = toolSensorReferencePointList.item(j);
437 if (!node.hasAttributes())
439 reportWarning(
"Found reference point without id attribute. Skipping.");
443 int id = node.toElement().attribute(
"id").toInt(&ok);
446 reportWarning(
"Attribute id of a reference point was not an int. Skipping.");
449 QString toolSensorReferencePointText = node.toElement().text();
450 Vector3D vector = Vector3D::fromString(toolSensorReferencePointText);
454 QDomElement toolSensorRomFileElement = toolSensorElement.firstChildElement(mToolSensorRomFileTag);
455 QString toolSensorRomFileText = toolSensorRomFileElement.text();
456 if (!toolSensorRomFileText.isEmpty())
457 toolSensorRomFileText = toolFolderAbsolutePath + toolSensorRomFileText;
460 QDomElement toolCalibrationElement = toolNode.firstChildElement(mToolCalibrationTag);
461 if (toolCalibrationElement.isNull())
464 "Could not find the <calibration> tag under the <tool> tag. Aborting this tool.");
467 QDomElement toolCalibrationFileElement = toolCalibrationElement.firstChildElement(mToolCalibrationFileTag);
468 QString toolCalibrationFileText = toolCalibrationFileElement.text();
469 if (!toolCalibrationFileText.isEmpty())
470 toolCalibrationFileText = toolFolderAbsolutePath + toolCalibrationFileText;
476 retval = internalStructure;
481 QDomNode ToolFileParser::getToolNode(QString toolAbsoluteFilePath)
484 QFile toolFile(toolAbsoluteFilePath);
485 if (!mToolDoc.setContent(&toolFile))
487 reportError(
"Could not set the xml content of the tool file " + toolAbsoluteFilePath);
491 retval = mToolDoc.elementsByTagName(mToolTag).item(0);
501 igstk::Transform ToolFileParser::readCalibrationFile(QString absoluteFilePath)
512 retval.ImportTransform(*M.getVtkMatrix());
TrackersAndToolsMap mTrackersAndTools
the trackers and tools (relative path) that should be used in the config
cxResource_EXPORT ProfilePtr profile()
void reportError(QString msg)
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
Transform3D transform() const
~ConfigurationFileParser()
QString getApplicationapplication()
static QString getTemplatesAbsoluteFilePath()
QString mFileName
absolute path and filename for the new config file
ConfigurationFileParser(QString absoluteConfigFilePath, QString loggingFolder="")
QString mLoggingFolderName
path to where log should be saved
QString getAbsoluteReferenceFilePath()
static void saveConfiguration(Configuration &config)
void reportWarning(QString msg)
static Frame3D create(const Transform3D &transform)
std::vector< IgstkTracker::InternalStructure > getTrackers()
void reportSuccess(QString msg)
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
static QString getRootConfigPath()
return path to root config folder. May be replaced with getExistingConfigPath()
std::vector< QString > getAbsoluteToolFilePaths()
TRACKING_SYSTEM mType
the trackers type
QString mClinical_app
the clinical application this config is made for
QString enum2string(const ENUM &val)