NorMIT-nav  2023.01.05-dev+develop.0da12
An IGT application
cxToolFileParser.cpp
Go to the documentation of this file.
1 #include "cxToolFileParser.h"
2 
3 #include <QFile>
4 #include <QDir>
5 #include "cxLogger.h"
6 #include "cxFrame3D.h"
7 #include "cxTransformFile.h"
8 #include "cxEnumConversion.h"
9 
10 namespace cx {
11 
13 {
14  //vtkMatrix4x4Ptr M = vtkMatrix4x4Ptr::New();
15  //mCalibration.ExportTransform(*(M.GetPointer()));
16  //Transform3D sMt = Transform3D::fromVtkMatrix(M);
17  //return sMt;
18  return mCalibration;
19 }
20 
22 {
23  mCalibration = cal;
24  //mCalibration.ImportTransform(*cal.getVtkMatrix());
25 }
26 
28 {
29  QString filename = mCalibrationFilename;
30  // QFile calibrationFile;
31  if (!filename.isEmpty() && QFile::exists(filename))
32  {
33  //Calibration file exists, overwrite
34  // calibrationFile.setFileName(mCalibrationFilename);
35  }
36  else
37  {
38  //Make a new file, use rom file name as base name
39  filename = mSROMFilename.remove(".rom", Qt::CaseInsensitive);
40  filename.append(".cal");
41  // calibrationFile.setFileName(calibrationFileName);
42  }
43 
44  TransformFile file(filename);
45  file.write(this->getCalibrationAsSSC());
46  //
51  // Transform3D sMt = this->getCalibrationAsSSC();
52  //
53  // if (!calibrationFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
54  // {
55  // reportError("Could not open " + mUid + "s calibrationfile: " + calibrationFile.fileName());
56  // return;
57  // }
58  //
59  // QTextStream streamer(&calibrationFile);
60  // streamer << qstring_cast(sMt);
61  // streamer << endl;
62  //
63  // calibrationFile.close();
64 
65  report("Replaced calibration in " + filename);
66 }
67 
69 {
70  bool retval = true;
71  QString verificationError("Internal verification of tool " + mUid + " failed! REASON: ");
72  if (!mIsPointer && !mIsReference && !mIsProbe)
73  {
74  // reportError(verificationError+" Tag <tool>::<type> is invalid ["+qstring_cast(mType)+"]. Valid types: [pointer, usprobe, reference]");
76  verificationError
77  + " Tag <tool>::<type> is invalid, must be one one of pointer/probe/reference ");
78  retval = false;
79  }
80  if (mUid.isEmpty())
81  {
82  reportError(verificationError + " Tag <tool>::<uid> is empty. Give tool a unique id.");
83  retval = false;
84  }
85  if (mTrackerType == tsNONE)
86  {
88  verificationError + " Tag <sensor>::<type> is invalid ["
89  + qstring_cast(mTrackerType)
90  + "]. Valid types: [polaris, spectra, vicra, aurora, micron (NOT SUPPORTED YET)]");
91  retval = false;
92  }
93  if ((mTrackerType == tsAURORA) && (mPortNumber >= 4))
94  {
96  verificationError + " Tag <sensor>::<portnumber> is invalid ["
97  + qstring_cast(mPortNumber)
98  + "]. Valid numbers: [0, 1, 2, 3]");
99  retval = false;
100  }
101  if ((mTrackerType == tsAURORA) && (mChannelNumber >= 1))
102  {
103  reportError(
104  verificationError + " Tag <sensor>::<channelnumber> is invalid ["
105  + qstring_cast(mChannelNumber) + "]. Valid numbers: [0, 1]");
106  retval = false;
107  }
108  QDir dir;
109  if (!mSROMFilename.isEmpty() && !dir.exists(mSROMFilename))
110  {
111  reportError(
112  verificationError + " Tag <sensor>::<rom_file> is invalid [" + mSROMFilename
113  + "]. Valid path: relative path to existing rom file.");
114  retval = false;
115  }
116  if (!mCalibrationFilename.isEmpty() && !dir.exists(mCalibrationFilename))
117  {
118  reportError(
119  verificationError + " Tag <calibration>::<cal_file> is invalid ["
120  + mCalibrationFilename
121  + "]. Valid path: relative path to existing calibration file.");
122  retval = false;
123  }
124  if (!mTransformSaveFileName.isEmpty() && !dir.exists(mTransformSaveFileName))
125  {
126  reportError(verificationError + " Logging folder is invalid. Contact programmer! :)");
127  retval = false;
128  }
129  if (!mLoggingFolderName.isEmpty() && !dir.exists(mLoggingFolderName))
130  {
131  reportError(verificationError + " Logging folder is invalid. Contact programmer! :)");
132  retval = false;
133  }
134 
135  return retval;
136 }
137 
138 ToolFileParser::ToolFileParser(QString absoluteToolFilePath, QString loggingFolder) :
139  mToolFilePath(absoluteToolFilePath), mLoggingFolder(loggingFolder), mToolTag("tool"), mToolTypeTag("type"),
140  mToolIdTag("id"), mToolNameTag("name"), mToolDescriptionTag("description"), mToolManufacturerTag("manufacturer"),
141  mToolClinicalAppTag("clinical_app"), mToolGeoFileTag("geo_file"), mToolPicFileTag("pic_file"),
142  mToolDocFileTag("doc_file"), mToolInstrumentTag("instrument"), mToolInstrumentTypeTag("type"),
144  mToolInstrumentScannerIdTag("scannerid"), mToolInstrumentDescriptionTag("description"),
145  mToolSensorTag("sensor"), mToolSensorTypeTag("type"), mToolSensorIdTag("id"),
146  mToolSensorNameTag("name"), mToolSensorWirelessTag("wireless"), mToolSensorDOFTag("DOF"),
147  mToolSensorPortnumberTag("portnumber"), mToolSensorChannelnumberTag("channelnumber"),
148  mToolSensorReferencePointTag("reference_point"), mToolSensorManufacturerTag("manufacturer"),
149  mToolSensorDescriptionTag("description"), mToolSensorRomFileTag("rom_file"), mToolCalibrationTag("calibration"),
150  mToolCalibrationFileTag("cal_file"),
151  mToolOpenigtlinkImageIdTag("openigtlinkimageid"), mToolOpenigtlinkTransformIdTag("openigtlinktransformid")
152 {
153 }
154 
156 {
157 }
158 
160 {
162 
163  QFile toolFile(mToolFilePath);
164  QString toolFolderAbsolutePath = QFileInfo(toolFile).dir().absolutePath() + "/";
165  QDomNode toolNode = this->getToolNode(mToolFilePath);
167  // ToolFileParser::ToolInternalStructure internalStructure;
168  if (toolNode.isNull())
169  {
170  report(
171  "Could not read the <tool> tag of file: " + mToolFilePath
172  + ", this is not a tool file, skipping.");
173  return retval;
174  }
175 
176  QDomElement toolTypeElement = toolNode.firstChildElement(mToolTypeTag);
177  QString toolTypeText = toolTypeElement.text();
178 
179  internalStructure->mIsReference = toolTypeText.contains("reference", Qt::CaseInsensitive);
180  internalStructure->mIsPointer = toolTypeText.contains("pointer", Qt::CaseInsensitive);
181  internalStructure->mIsProbe = toolTypeText.contains("usprobe", Qt::CaseInsensitive);
182 
183  // if (toolTypeText.contains("reference", Qt::CaseInsensitive))
184  // {
185  // internalStructure.mType = Tool::TOOL_REFERENCE;
186  // } else if (toolTypeText.contains("pointer", Qt::CaseInsensitive))
187  // {
188  // internalStructure.mType = Tool::TOOL_POINTER;
189  // } else if (toolTypeText.contains("usprobe", Qt::CaseInsensitive))
190  // {
191  // internalStructure.mType = Tool::TOOL_US_PROBE;
192  // } else
193  // {
194  // internalStructure.mType = Tool::TOOL_NONE;
195  // }
196 
197  QDomElement toolIdElement = toolNode.firstChildElement(mToolIdTag);
198  QString toolIdText = toolIdElement.text();
199  internalStructure->mUid = toolIdText;
200 
201  QDomElement toolNameElement = toolNode.firstChildElement(mToolNameTag);
202  QString toolNameText = toolNameElement.text();
203  internalStructure->mName = toolNameText;
204 
205  QDomElement toolClinicalAppElement = toolNode.firstChildElement(mToolClinicalAppTag);
206  QString toolClinicalAppText = toolClinicalAppElement.text();
207  QStringList applicationList = toolClinicalAppText.split(" ");
208  foreach(QString string, applicationList)
209  {
210  if (string.isEmpty())
211  continue;
212  string = string.toLower();
213  internalStructure->mClinicalApplications.push_back(string);
214  }
215 
216  QDomElement toolGeofileElement = toolNode.firstChildElement(mToolGeoFileTag);
217  QString toolGeofileText = toolGeofileElement.text();
218  if (!toolGeofileText.isEmpty())
219  toolGeofileText = toolFolderAbsolutePath + toolGeofileText;
220  internalStructure->mGraphicsFileName = toolGeofileText;
221 
222  QDomElement toolPicfileElement = toolNode.firstChildElement(mToolPicFileTag);
223  QString toolPicfileText = toolPicfileElement.text();
224  if (!toolPicfileText.isEmpty())
225  toolPicfileText = toolFolderAbsolutePath + toolPicfileText;
226  internalStructure->mPictureFileName = toolPicfileText;
227 
228  QDomElement toolImageIdfileElement = toolNode.firstChildElement(mToolOpenigtlinkImageIdTag);
229  QString toolImageIdfileText = toolImageIdfileElement.text();
230  internalStructure->mOpenigtlinkImageId = toolImageIdfileText;
231 
232  QDomElement toolTransformIdfileElement = toolNode.firstChildElement(mToolOpenigtlinkTransformIdTag);
233  QString toolTransformIdfileText = toolTransformIdfileElement.text();
234  internalStructure->mOpenigtlinkTransformId = toolTransformIdfileText;
235 
236  QDomElement toolInstrumentElement = toolNode.firstChildElement(mToolInstrumentTag);
237  if (toolInstrumentElement.isNull())
238  {
239  reportError(
240  "Could not find the <instrument> tag under the <tool> tag. Aborting this tool.");
241  return retval;
242  }
243  QDomElement toolInstrumentIdElement = toolInstrumentElement.firstChildElement(mToolInstrumentIdTag);
244  QString toolInstrumentIdText = toolInstrumentIdElement.text();
245  internalStructure->mInstrumentId = toolInstrumentIdText;
246 
247  QDomElement toolInstrumentScannerIdElement = toolInstrumentElement.firstChildElement(mToolInstrumentScannerIdTag);
248  QString toolInstrumentScannerIdText = toolInstrumentScannerIdElement.text();
249  internalStructure->mInstrumentScannerId = toolInstrumentScannerIdText;
250 
251  QDomElement toolSensorElement = toolNode.firstChildElement(mToolSensorTag);
252  if (toolSensorElement.isNull())
253  {
254  reportError("Could not find the <sensor> tag under the <tool> tag. Aborting this tool.");
255  return retval;
256  }
257  QDomElement toolSensorTypeElement = toolSensorElement.firstChildElement(mToolSensorTypeTag);
258  QString toolSensorTypeText = toolSensorTypeElement.text();
259  internalStructure->mTrackerType = string2enum<TRACKING_SYSTEM>(toolSensorTypeText);
260 
261  QDomElement toolSensorWirelessElement = toolSensorElement.firstChildElement(mToolSensorWirelessTag);
262  QString toolSensorWirelessText = toolSensorWirelessElement.text();
263  if (toolSensorWirelessText.contains("yes", Qt::CaseInsensitive))
264  internalStructure->mWireless = true;
265  else if (toolSensorWirelessText.contains("no", Qt::CaseInsensitive))
266  internalStructure->mWireless = false;
267 
268  QDomElement toolSensorDOFElement = toolSensorElement.firstChildElement(mToolSensorDOFTag);
269  QString toolSensorDOFText = toolSensorDOFElement.text();
270  if (toolSensorDOFText.contains("5", Qt::CaseInsensitive))
271  internalStructure->m5DOF = true;
272  else if (toolSensorDOFText.contains("6", Qt::CaseInsensitive))
273  internalStructure->m5DOF = false;
274 
275  QDomElement toolSensorPortnumberElement = toolSensorElement.firstChildElement(mToolSensorPortnumberTag);
276  QString toolSensorPortnumberText = toolSensorPortnumberElement.text();
277  internalStructure->mPortNumber = toolSensorPortnumberText.toUInt();
278 
279  QDomElement toolSensorChannelnumberElement = toolSensorElement.firstChildElement(mToolSensorChannelnumberTag);
280  QString toolSensorChannelnumberText = toolSensorChannelnumberElement.text();
281  internalStructure->mChannelNumber = toolSensorChannelnumberText.toUInt();
282 
283  QDomNodeList toolSensorReferencePointList = toolSensorElement.elementsByTagName(mToolSensorReferencePointTag);
284  for (int j = 0; j < toolSensorReferencePointList.count(); j++)
285  {
286  QDomNode node = toolSensorReferencePointList.item(j);
287  if (!node.hasAttributes())
288  {
289  reportWarning("Found reference point without id attribute. Skipping.");
290  continue;
291  }
292  QString id = node.toElement().attribute("id");
293  QString toolSensorReferencePointText = node.toElement().text();
294  Vector3D vector = Vector3D::fromString(toolSensorReferencePointText);
295  internalStructure->mReferencePoints[id] = vector;
296  }
297 
298  QDomElement toolSensorRomFileElement = toolSensorElement.firstChildElement(mToolSensorRomFileTag);
299  QString toolSensorRomFileText = toolSensorRomFileElement.text();
300  if (!toolSensorRomFileText.isEmpty())
301  toolSensorRomFileText = toolFolderAbsolutePath + toolSensorRomFileText;
302  internalStructure->mSROMFilename = toolSensorRomFileText;
303 
304  QDomElement toolCalibrationElement = toolNode.firstChildElement(mToolCalibrationTag);
305  if (toolCalibrationElement.isNull())
306  {
307  reportError(
308  "Could not find the <calibration> tag under the <tool> tag. Aborting this tool.");
309  return retval;
310  }
311  QDomElement toolCalibrationFileElement = toolCalibrationElement.firstChildElement(mToolCalibrationFileTag);
312  QString toolCalibrationFileText = toolCalibrationFileElement.text();
313  if (!toolCalibrationFileText.isEmpty())
314  toolCalibrationFileText = toolFolderAbsolutePath + toolCalibrationFileText;
315  internalStructure->mCalibrationFilename = toolCalibrationFileText;
316  internalStructure->mCalibration = this->readCalibrationFile(internalStructure->mCalibrationFilename);
317 
318  internalStructure->mTransformSaveFileName = mLoggingFolder;
319  internalStructure->mLoggingFolderName = mLoggingFolder;
320  retval = internalStructure;
321 
322  return retval;
323 }
324 
325 QDomNode ToolFileParser::getToolNode(QString toolAbsoluteFilePath)
326 {
327  QDomNode retval;
328  QFile toolFile(toolAbsoluteFilePath);
329  if (!mToolDoc.setContent(&toolFile))
330  {
331  reportError("Could not set the xml content of the tool file " + toolAbsoluteFilePath);
332  return retval;
333  }
334  //there can only be one tool defined in every tool.xml-file, that's why we say ...item(0)
335  retval = mToolDoc.elementsByTagName(mToolTag).item(0);
336  return retval;
337 }
338 
340 {
341  bool ok = true;
342  TransformFile file(absoluteFilePath);
343  Transform3D retval = file.read(&ok);
344 
345  if (ok)
346  {
347  retval = Frame3D::create(retval).transform(); // clean rotational parts, transform should now be pure rotation+translation
348  }
349 
350  return retval;
351 }
352 
353 /*
354 QString ToolFileParser::getTemplatesAbsoluteFilePath()
355 {
356  QString retval = DataLocations::getRootConfigPath() + "/tool/TEMPLATE_tool.xml";
357  return retval;
358 }
359 */
360 
361 }
cx::ToolFileParser::mToolInstrumentIdTag
const QString mToolInstrumentIdTag
Definition: cxToolFileParser.h:119
cx::ToolFileParser::mToolSensorDescriptionTag
const QString mToolSensorDescriptionTag
Definition: cxToolFileParser.h:123
cxLogger.h
cx::ToolFileParser::mToolSensorWirelessTag
const QString mToolSensorWirelessTag
Definition: cxToolFileParser.h:121
cx::ToolFileParser::mLoggingFolder
QString mLoggingFolder
absolutepath to the logging folder
Definition: cxToolFileParser.h:114
qstring_cast
QString qstring_cast(const T &val)
Definition: cxTypeConversions.h:46
cx::ToolFileParser::mToolNameTag
const QString mToolNameTag
Definition: cxToolFileParser.h:117
cx::Frame3D::transform
Transform3D transform() const
Definition: cxFrame3D.cpp:140
cx::ToolFileParser::mToolInstrumentNameTag
const QString mToolInstrumentNameTag
Definition: cxToolFileParser.h:119
cx::ToolFileParser::mToolTag
const QString mToolTag
Definition: cxToolFileParser.h:117
cx::ToolFileParser::ToolInternalStructure::verify
bool verify()
Definition: cxToolFileParser.cpp:68
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::ToolFileParser::mToolSensorManufacturerTag
const QString mToolSensorManufacturerTag
Definition: cxToolFileParser.h:123
cx::ToolFileParser::mToolOpenigtlinkTransformIdTag
const QString mToolOpenigtlinkTransformIdTag
names of necessary tags in the tool file
Definition: cxToolFileParser.h:125
cx::ToolFileParser::ToolInternalStructure::saveCalibrationToFile
void saveCalibrationToFile()
Definition: cxToolFileParser.cpp:27
cx::report
void report(QString msg)
Definition: cxLogger.cpp:69
cx::TransformFile::read
Transform3D read(bool *ok=0)
Definition: cxTransformFile.cpp:33
cx::TransformFile
File format for storing a 4x4 matrix.
Definition: cxTransformFile.h:37
cxFrame3D.h
cx::ToolFileParser::ToolInternalStructure
A tools internal structure.
Definition: cxToolFileParser.h:50
cx::ToolFileParser::mToolCalibrationTag
const QString mToolCalibrationTag
Definition: cxToolFileParser.h:124
cx::ToolFileParser::ToolFileParser
ToolFileParser(QString absoluteToolFilePath, QString loggingFolder="")
Definition: cxToolFileParser.cpp:138
cx::ToolFileParser::getTool
virtual ToolInternalStructurePtr getTool()
Definition: cxToolFileParser.cpp:159
cx::ToolFileParser::~ToolFileParser
virtual ~ToolFileParser()
Definition: cxToolFileParser.cpp:155
cx::ToolFileParser::getToolNode
QDomNode getToolNode(QString toolAbsoluteFilePath)
Definition: cxToolFileParser.cpp:325
cx::ToolFileParser::mToolSensorNameTag
const QString mToolSensorNameTag
Definition: cxToolFileParser.h:121
cx::ToolFileParser::mToolDoc
QDomDocument mToolDoc
the tool xml document
Definition: cxToolFileParser.h:116
cx::ToolFileParser::mToolGeoFileTag
const QString mToolGeoFileTag
Definition: cxToolFileParser.h:118
cx::ToolFileParser::ToolInternalStructure::mCalibration
Transform3D mCalibration
transform read from mCalibrationFilename
Definition: cxToolFileParser.h:72
cx::ToolFileParser::mToolInstrumentTypeTag
const QString mToolInstrumentTypeTag
Definition: cxToolFileParser.h:119
cx::ToolFileParser::mToolFilePath
QString mToolFilePath
absolutepath to the tool file
Definition: cxToolFileParser.h:113
cx::ToolFileParser::mToolDocFileTag
const QString mToolDocFileTag
Definition: cxToolFileParser.h:118
cx::Frame3D::create
static Frame3D create(const Transform3D &transform)
Definition: cxFrame3D.cpp:128
cxToolFileParser.h
cx::Transform3D
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
Definition: cxLandmarkPatientRegistrationWidget.h:33
cx::TransformFile::write
void write(const Transform3D &transform)
Definition: cxTransformFile.cpp:65
cx::ToolFileParser::mToolPicFileTag
const QString mToolPicFileTag
Definition: cxToolFileParser.h:118
cx::ToolFileParser::mToolSensorTypeTag
const QString mToolSensorTypeTag
Definition: cxToolFileParser.h:121
cx::ToolFileParser::mToolCalibrationFileTag
const QString mToolCalibrationFileTag
Definition: cxToolFileParser.h:124
cx::ToolFileParser::mToolSensorRomFileTag
const QString mToolSensorRomFileTag
Definition: cxToolFileParser.h:124
cx::ToolFileParser::mToolClinicalAppTag
const QString mToolClinicalAppTag
Definition: cxToolFileParser.h:118
cx::ToolFileParser::mToolInstrumentManufacturerTag
const QString mToolInstrumentManufacturerTag
Definition: cxToolFileParser.h:120
cx::ToolFileParser::mToolInstrumentTag
const QString mToolInstrumentTag
Definition: cxToolFileParser.h:118
cx::ToolFileParser::mToolSensorReferencePointTag
const QString mToolSensorReferencePointTag
Definition: cxToolFileParser.h:123
cx::ToolFileParser::mToolDescriptionTag
const QString mToolDescriptionTag
Definition: cxToolFileParser.h:117
cx::ToolFileParser::mToolSensorChannelnumberTag
const QString mToolSensorChannelnumberTag
Definition: cxToolFileParser.h:122
cx::ToolFileParser::mToolInstrumentScannerIdTag
const QString mToolInstrumentScannerIdTag
Definition: cxToolFileParser.h:120
cx::ToolFileParser::mToolTypeTag
const QString mToolTypeTag
Definition: cxToolFileParser.h:117
cx::ToolFileParser::readCalibrationFile
Transform3D readCalibrationFile(QString absoluteFilePath)
Definition: cxToolFileParser.cpp:339
cx::ToolFileParser::mToolSensorPortnumberTag
const QString mToolSensorPortnumberTag
Definition: cxToolFileParser.h:122
cx::ToolFileParser::mToolOpenigtlinkImageIdTag
const QString mToolOpenigtlinkImageIdTag
Definition: cxToolFileParser.h:125
tsAURORA
tsAURORA
NDIs Aurora tracker.
Definition: cxDefinitions.h:117
cx::ToolFileParser::mToolSensorIdTag
const QString mToolSensorIdTag
Definition: cxToolFileParser.h:121
cx::ToolFileParser::ToolInternalStructure::setCalibration
void setCalibration(const Transform3D &cal)
Definition: cxToolFileParser.cpp:21
cx::ToolFileParser::mToolIdTag
const QString mToolIdTag
Definition: cxToolFileParser.h:117
cxTransformFile.h
cxEnumConversion.h
cx::ToolFileParser::mToolManufacturerTag
const QString mToolManufacturerTag
Definition: cxToolFileParser.h:117
cx::ToolFileParser::mToolInstrumentDescriptionTag
const QString mToolInstrumentDescriptionTag
Definition: cxToolFileParser.h:120
cx::ToolFileParser::ToolInternalStructure::getCalibrationAsSSC
Transform3D getCalibrationAsSSC() const
Definition: cxToolFileParser.cpp:12
cx::reportError
void reportError(QString msg)
Definition: cxLogger.cpp:71
tsNONE
tsNONE
Not specified.
Definition: cxDefinitions.h:112
cx::Vector3D
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:42
cx::ToolFileParser::ToolInternalStructurePtr
boost::shared_ptr< ToolInternalStructure > ToolInternalStructurePtr
Definition: cxToolFileParser.h:99
cx::reportWarning
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
cx::ToolFileParser::mToolSensorTag
const QString mToolSensorTag
Definition: cxToolFileParser.h:121
cx::ToolFileParser::mToolSensorDOFTag
const QString mToolSensorDOFTag
Definition: cxToolFileParser.h:122