Fraxinus  18.10
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 "cxEnumConverter.h"
7 #include "cxFrame3D.h"
8 #include "cxTransformFile.h"
9 #include "cxDefinitionStrings.h"//Removes warning for string2enum
10 
11 namespace cx {
12 
14 {
15  //vtkMatrix4x4Ptr M = vtkMatrix4x4Ptr::New();
16  //mCalibration.ExportTransform(*(M.GetPointer()));
17  //Transform3D sMt = Transform3D::fromVtkMatrix(M);
18  //return sMt;
19  return mCalibration;
20 }
21 
23 {
24  mCalibration = cal;
25  //mCalibration.ImportTransform(*cal.getVtkMatrix());
26 }
27 
29 {
30  QString filename = mCalibrationFilename;
31 // QFile calibrationFile;
32  if (!filename.isEmpty() && QFile::exists(filename))
33  {
34  //Calibration file exists, overwrite
35 // calibrationFile.setFileName(mCalibrationFilename);
36  }
37  else
38  {
39  //Make a new file, use rom file name as base name
40  filename = mSROMFilename.remove(".rom", Qt::CaseInsensitive);
41  filename.append(".cal");
42 // calibrationFile.setFileName(calibrationFileName);
43  }
44 
45  TransformFile file(filename);
46  file.write(this->getCalibrationAsSSC());
47 //
52 // Transform3D sMt = this->getCalibrationAsSSC();
53 //
54 // if (!calibrationFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
55 // {
56 // reportError("Could not open " + mUid + "s calibrationfile: " + calibrationFile.fileName());
57 // return;
58 // }
59 //
60 // QTextStream streamer(&calibrationFile);
61 // streamer << qstring_cast(sMt);
62 // streamer << endl;
63 //
64 // calibrationFile.close();
65 
66  report("Replaced calibration in " + filename);
67 }
68 
70 {
71  bool retval = true;
72  QString verificationError("Internal verification of tool " + mUid + " failed! REASON: ");
73  if (!mIsPointer && !mIsReference && !mIsProbe)
74  {
75 // reportError(verificationError+" Tag <tool>::<type> is invalid ["+qstring_cast(mType)+"]. Valid types: [pointer, usprobe, reference]");
77  verificationError
78  + " Tag <tool>::<type> is invalid, must be one one of pointer/probe/reference ");
79  retval = false;
80  }
81  if (mUid.isEmpty())
82  {
83  reportError(verificationError + " Tag <tool>::<uid> is empty. Give tool a unique id.");
84  retval = false;
85  }
86  if (mTrackerType == tsNONE)
87  {
89  verificationError + " Tag <sensor>::<type> is invalid ["
91  + "]. Valid types: [polaris, spectra, vicra, aurora, micron (NOT SUPPORTED YET)]");
92  retval = false;
93  }
94  if ((mTrackerType == tsAURORA) && (mPortNumber >= 4))
95  {
97  verificationError + " Tag <sensor>::<portnumber> is invalid ["
99  + "]. Valid numbers: [0, 1, 2, 3]");
100  retval = false;
101  }
102  if ((mTrackerType == tsAURORA) && (mChannelNumber >= 1))
103  {
104  reportError(
105  verificationError + " Tag <sensor>::<channelnumber> is invalid ["
106  + qstring_cast(mChannelNumber) + "]. Valid numbers: [0, 1]");
107  retval = false;
108  }
109  QDir dir;
110  if (!mSROMFilename.isEmpty() && !dir.exists(mSROMFilename))
111  {
112  reportError(
113  verificationError + " Tag <sensor>::<rom_file> is invalid [" + mSROMFilename
114  + "]. Valid path: relative path to existing rom file.");
115  retval = false;
116  }
117  if (!mCalibrationFilename.isEmpty() && !dir.exists(mCalibrationFilename))
118  {
119  reportError(
120  verificationError + " Tag <calibration>::<cal_file> is invalid ["
122  + "]. Valid path: relative path to existing calibration file.");
123  retval = false;
124  }
125  if (!mTransformSaveFileName.isEmpty() && !dir.exists(mTransformSaveFileName))
126  {
127  reportError(verificationError + " Logging folder is invalid. Contact programmer! :)");
128  retval = false;
129  }
130  if (!mLoggingFolderName.isEmpty() && !dir.exists(mLoggingFolderName))
131  {
132  reportError(verificationError + " Logging folder is invalid. Contact programmer! :)");
133  retval = false;
134  }
135 
136  return retval;
137 }
138 
139 ToolFileParser::ToolFileParser(QString absoluteToolFilePath, QString loggingFolder) :
140  mToolFilePath(absoluteToolFilePath), mLoggingFolder(loggingFolder), mToolTag("tool"), mToolTypeTag(
141  "type"), mToolIdTag("id"), mToolNameTag("name"), mToolDescriptionTag("description"), mToolManufacturerTag(
142  "manufacturer"), mToolClinicalAppTag("clinical_app"), mToolGeoFileTag("geo_file"), mToolPicFileTag(
143  "pic_file"), mToolDocFileTag("doc_file"), mToolInstrumentTag("instrument"), mToolInstrumentTypeTag(
145  "manufacturer"), mToolInstrumentScannerIdTag("scannerid"), mToolInstrumentDescriptionTag(
146  "description"), mToolSensorTag("sensor"), mToolSensorTypeTag("type"), mToolSensorIdTag(
147  "id"), mToolSensorNameTag("name"), mToolSensorWirelessTag("wireless"), mToolSensorDOFTag(
149  "channelnumber"), mToolSensorReferencePointTag("reference_point"), mToolSensorManufacturerTag(
150  "manufacturer"), mToolSensorDescriptionTag("description"), mToolSensorRomFileTag(
151  "rom_file"), mToolCalibrationTag("calibration"), mToolCalibrationFileTag("cal_file")
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 toolInstrumentElement = toolNode.firstChildElement(mToolInstrumentTag);
229  if (toolInstrumentElement.isNull())
230  {
231  reportError(
232  "Could not find the <instrument> tag under the <tool> tag. Aborting this tool.");
233  return retval;
234  }
235  QDomElement toolInstrumentIdElement = toolInstrumentElement.firstChildElement(mToolInstrumentIdTag);
236  QString toolInstrumentIdText = toolInstrumentIdElement.text();
237  internalStructure->mInstrumentId = toolInstrumentIdText;
238 
239  QDomElement toolInstrumentScannerIdElement = toolInstrumentElement.firstChildElement(mToolInstrumentScannerIdTag);
240  QString toolInstrumentScannerIdText = toolInstrumentScannerIdElement.text();
241  internalStructure->mInstrumentScannerId = toolInstrumentScannerIdText;
242 
243  QDomElement toolSensorElement = toolNode.firstChildElement(mToolSensorTag);
244  if (toolSensorElement.isNull())
245  {
246  reportError("Could not find the <sensor> tag under the <tool> tag. Aborting this tool.");
247  return retval;
248  }
249  QDomElement toolSensorTypeElement = toolSensorElement.firstChildElement(mToolSensorTypeTag);
250  QString toolSensorTypeText = toolSensorTypeElement.text();
251  internalStructure->mTrackerType = string2enum<TRACKING_SYSTEM>(toolSensorTypeText);
252 
253  QDomElement toolSensorWirelessElement = toolSensorElement.firstChildElement(mToolSensorWirelessTag);
254  QString toolSensorWirelessText = toolSensorWirelessElement.text();
255  if (toolSensorWirelessText.contains("yes", Qt::CaseInsensitive))
256  internalStructure->mWireless = true;
257  else if (toolSensorWirelessText.contains("no", Qt::CaseInsensitive))
258  internalStructure->mWireless = false;
259 
260  QDomElement toolSensorDOFElement = toolSensorElement.firstChildElement(mToolSensorDOFTag);
261  QString toolSensorDOFText = toolSensorDOFElement.text();
262  if (toolSensorDOFText.contains("5", Qt::CaseInsensitive))
263  internalStructure->m5DOF = true;
264  else if (toolSensorDOFText.contains("6", Qt::CaseInsensitive))
265  internalStructure->m5DOF = false;
266 
267  QDomElement toolSensorPortnumberElement = toolSensorElement.firstChildElement(mToolSensorPortnumberTag);
268  QString toolSensorPortnumberText = toolSensorPortnumberElement.text();
269  internalStructure->mPortNumber = toolSensorPortnumberText.toUInt();
270 
271  QDomElement toolSensorChannelnumberElement = toolSensorElement.firstChildElement(mToolSensorChannelnumberTag);
272  QString toolSensorChannelnumberText = toolSensorChannelnumberElement.text();
273  internalStructure->mChannelNumber = toolSensorChannelnumberText.toUInt();
274 
275  QDomNodeList toolSensorReferencePointList = toolSensorElement.elementsByTagName(mToolSensorReferencePointTag);
276  for (int j = 0; j < toolSensorReferencePointList.count(); j++)
277  {
278  QDomNode node = toolSensorReferencePointList.item(j);
279  if (!node.hasAttributes())
280  {
281  reportWarning("Found reference point without id attribute. Skipping.");
282  continue;
283  }
284  bool ok;
285  int id = node.toElement().attribute("id").toInt(&ok);
286  if (!ok)
287  {
288  reportWarning("Attribute id of a reference point was not an int. Skipping.");
289  continue;
290  }
291  QString toolSensorReferencePointText = node.toElement().text();
292  Vector3D vector = Vector3D::fromString(toolSensorReferencePointText);
293  internalStructure->mReferencePoints[id] = vector;
294  }
295 
296  QDomElement toolSensorRomFileElement = toolSensorElement.firstChildElement(mToolSensorRomFileTag);
297  QString toolSensorRomFileText = toolSensorRomFileElement.text();
298  if (!toolSensorRomFileText.isEmpty())
299  toolSensorRomFileText = toolFolderAbsolutePath + toolSensorRomFileText;
300  internalStructure->mSROMFilename = toolSensorRomFileText;
301 
302  QDomElement toolCalibrationElement = toolNode.firstChildElement(mToolCalibrationTag);
303  if (toolCalibrationElement.isNull())
304  {
305  reportError(
306  "Could not find the <calibration> tag under the <tool> tag. Aborting this tool.");
307  return retval;
308  }
309  QDomElement toolCalibrationFileElement = toolCalibrationElement.firstChildElement(mToolCalibrationFileTag);
310  QString toolCalibrationFileText = toolCalibrationFileElement.text();
311  if (!toolCalibrationFileText.isEmpty())
312  toolCalibrationFileText = toolFolderAbsolutePath + toolCalibrationFileText;
313  internalStructure->mCalibrationFilename = toolCalibrationFileText;
314  internalStructure->mCalibration = this->readCalibrationFile(internalStructure->mCalibrationFilename);
315 
316  internalStructure->mTransformSaveFileName = mLoggingFolder;
317  internalStructure->mLoggingFolderName = mLoggingFolder;
318  retval = internalStructure;
319 
320  return retval;
321 }
322 
323 QDomNode ToolFileParser::getToolNode(QString toolAbsoluteFilePath)
324 {
325  QDomNode retval;
326  QFile toolFile(toolAbsoluteFilePath);
327  if (!mToolDoc.setContent(&toolFile))
328  {
329  reportError("Could not set the xml content of the tool file " + toolAbsoluteFilePath);
330  return retval;
331  }
332  //there can only be one tool defined in every tool.xml-file, that's why we say ...item(0)
333  retval = mToolDoc.elementsByTagName(mToolTag).item(0);
334  return retval;
335 }
336 
338 {
339  bool ok = true;
340  TransformFile file(absoluteFilePath);
341  Transform3D retval = file.read(&ok);
342 
343  if (ok)
344  {
345  retval = Frame3D::create(retval).transform(); // clean rotational parts, transform should now be pure rotation+translation
346  }
347 
348  return retval;
349 }
350 
351 /*
352 QString ToolFileParser::getTemplatesAbsoluteFilePath()
353 {
354  QString retval = DataLocations::getRootConfigPath() + "/tool/TEMPLATE_tool.xml";
355  return retval;
356 }
357 */
358 
359 }
QString qstring_cast(const T &val)
ToolFileParser(QString absoluteToolFilePath, QString loggingFolder="")
boost::shared_ptr< ToolInternalStructure > ToolInternalStructurePtr
void reportError(QString msg)
Definition: cxLogger.cpp:71
const QString mToolSensorRomFileTag
File format for storing a 4x4 matrix.The read/write methods emit error messages if you dont use the o...
const QString mToolSensorManufacturerTag
tsNONE
Not specified.
QString mLoggingFolder
absolutepath to the logging folder
const QString mToolCalibrationTag
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
Transform3D transform() const
Definition: cxFrame3D.cpp:140
QString mTransformSaveFileName
path to where transforms should be saved
const QString mToolGeoFileTag
const QString mToolSensorReferencePointTag
const QString mToolSensorDescriptionTag
const QString mToolSensorTag
const QString mToolSensorTypeTag
QString mSROMFilename
path to the tools SROM file
void write(const Transform3D &transform)
Transform3D read(bool *ok=0)
const QString mToolDocFileTag
unsigned int mPortNumber
the port number the tool is connected to
QDomDocument mToolDoc
the tool xml document
const QString mToolNameTag
const QString mToolSensorChannelnumberTag
const QString mToolInstrumentNameTag
const QString mToolInstrumentManufacturerTag
const QString mToolInstrumentDescriptionTag
unsigned int mChannelNumber
the channel the tool is connected to
virtual ToolInternalStructurePtr getTool()
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
const QString mToolInstrumentIdTag
QString mToolFilePath
absolutepath to the tool file
static Frame3D create(const Transform3D &transform)
Definition: cxFrame3D.cpp:128
Transform3D readCalibrationFile(QString absoluteFilePath)
const QString mToolSensorIdTag
const QString mToolPicFileTag
TRACKING_SYSTEM mTrackerType
what product the tool belongs to
const QString mToolInstrumentTag
const QString mToolInstrumentScannerIdTag
const QString mToolCalibrationFileTag
names of necessary tags in the tool file
QString mCalibrationFilename
path to the tools calibration file
const QString mToolDescriptionTag
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:42
const QString mToolIdTag
tsAURORA
NDIs Aurora tracker.
const QString mToolSensorPortnumberTag
void report(QString msg)
Definition: cxLogger.cpp:69
const QString mToolSensorWirelessTag
QString mLoggingFolderName
path to where log should be saved
const QString mToolManufacturerTag
const QString mToolSensorNameTag
const QString mToolSensorDOFTag
const QString mToolClinicalAppTag
Transform3D mCalibration
transform read from mCalibrationFilename
const QString mToolInstrumentTypeTag
const QString mToolTypeTag
QDomNode getToolNode(QString toolAbsoluteFilePath)
const QString mToolTag
Namespace for all CustusX production code.
void setCalibration(const Transform3D &cal)