Fraxinus  17.12-rc4
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 
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 ["
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 ["
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 ["
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(
140  "type"), mToolIdTag("id"), mToolNameTag("name"), mToolDescriptionTag("description"), mToolManufacturerTag(
141  "manufacturer"), mToolClinicalAppTag("clinical_app"), mToolGeoFileTag("geo_file"), mToolPicFileTag(
142  "pic_file"), mToolDocFileTag("doc_file"), mToolInstrumentTag("instrument"), mToolInstrumentTypeTag(
144  "manufacturer"), mToolInstrumentScannerIdTag("scannerid"), mToolInstrumentDescriptionTag(
145  "description"), mToolSensorTag("sensor"), mToolSensorTypeTag("type"), mToolSensorIdTag(
146  "id"), mToolSensorNameTag("name"), mToolSensorWirelessTag("wireless"), mToolSensorDOFTag(
148  "channelnumber"), mToolSensorReferencePointTag("reference_point"), mToolSensorManufacturerTag(
149  "manufacturer"), mToolSensorDescriptionTag("description"), mToolSensorRomFileTag(
150  "rom_file"), mToolCalibrationTag("calibration"), mToolCalibrationFileTag("cal_file")
151 {
152 }
153 
155 {
156 }
157 
159 {
160  ToolInternalStructure retval;
161 
162  QFile toolFile(mToolFilePath);
163  QString toolFolderAbsolutePath = QFileInfo(toolFile).dir().absolutePath() + "/";
164  QDomNode toolNode = this->getToolNode(mToolFilePath);
165  ToolFileParser::ToolInternalStructure internalStructure;
166  if (toolNode.isNull())
167  {
168  report(
169  "Could not read the <tool> tag of file: " + mToolFilePath
170  + ", this is not a tool file, skipping.");
171  return retval;
172  }
173 
174  QDomElement toolTypeElement = toolNode.firstChildElement(mToolTypeTag);
175  QString toolTypeText = toolTypeElement.text();
176 
177  internalStructure.mIsReference = toolTypeText.contains("reference", Qt::CaseInsensitive);
178  internalStructure.mIsPointer = toolTypeText.contains("pointer", Qt::CaseInsensitive);
179  internalStructure.mIsProbe = toolTypeText.contains("usprobe", Qt::CaseInsensitive);
180 
181 // if (toolTypeText.contains("reference", Qt::CaseInsensitive))
182 // {
183 // internalStructure.mType = Tool::TOOL_REFERENCE;
184 // } else if (toolTypeText.contains("pointer", Qt::CaseInsensitive))
185 // {
186 // internalStructure.mType = Tool::TOOL_POINTER;
187 // } else if (toolTypeText.contains("usprobe", Qt::CaseInsensitive))
188 // {
189 // internalStructure.mType = Tool::TOOL_US_PROBE;
190 // } else
191 // {
192 // internalStructure.mType = Tool::TOOL_NONE;
193 // }
194 
195  QDomElement toolIdElement = toolNode.firstChildElement(mToolIdTag);
196  QString toolIdText = toolIdElement.text();
197  internalStructure.mUid = toolIdText;
198 
199  QDomElement toolNameElement = toolNode.firstChildElement(mToolNameTag);
200  QString toolNameText = toolNameElement.text();
201  internalStructure.mName = toolNameText;
202 
203  QDomElement toolClinicalAppElement = toolNode.firstChildElement(mToolClinicalAppTag);
204  QString toolClinicalAppText = toolClinicalAppElement.text();
205  QStringList applicationList = toolClinicalAppText.split(" ");
206  foreach(QString string, applicationList)
207  {
208  if (string.isEmpty())
209  continue;
210  string = string.toLower();
211  internalStructure.mClinicalApplications.push_back(string);
212  }
213 
214  QDomElement toolGeofileElement = toolNode.firstChildElement(mToolGeoFileTag);
215  QString toolGeofileText = toolGeofileElement.text();
216  if (!toolGeofileText.isEmpty())
217  toolGeofileText = toolFolderAbsolutePath + toolGeofileText;
218  internalStructure.mGraphicsFileName = toolGeofileText;
219 
220  QDomElement toolPicfileElement = toolNode.firstChildElement(mToolPicFileTag);
221  QString toolPicfileText = toolPicfileElement.text();
222  if (!toolPicfileText.isEmpty())
223  toolPicfileText = toolFolderAbsolutePath + toolPicfileText;
224  internalStructure.mPictureFileName = toolPicfileText;
225 
226  QDomElement toolInstrumentElement = toolNode.firstChildElement(mToolInstrumentTag);
227  if (toolInstrumentElement.isNull())
228  {
229  reportError(
230  "Could not find the <instrument> tag under the <tool> tag. Aborting this tool.");
231  return retval;
232  }
233  QDomElement toolInstrumentIdElement = toolInstrumentElement.firstChildElement(mToolInstrumentIdTag);
234  QString toolInstrumentIdText = toolInstrumentIdElement.text();
235  internalStructure.mInstrumentId = toolInstrumentIdText;
236 
237  QDomElement toolInstrumentScannerIdElement = toolInstrumentElement.firstChildElement(mToolInstrumentScannerIdTag);
238  QString toolInstrumentScannerIdText = toolInstrumentScannerIdElement.text();
239  internalStructure.mInstrumentScannerId = toolInstrumentScannerIdText;
240 
241  QDomElement toolSensorElement = toolNode.firstChildElement(mToolSensorTag);
242  if (toolSensorElement.isNull())
243  {
244  reportError("Could not find the <sensor> tag under the <tool> tag. Aborting this tool.");
245  return retval;
246  }
247  QDomElement toolSensorTypeElement = toolSensorElement.firstChildElement(mToolSensorTypeTag);
248  QString toolSensorTypeText = toolSensorTypeElement.text();
249  internalStructure.mTrackerType = string2enum<TRACKING_SYSTEM>(toolSensorTypeText);
250 
251  QDomElement toolSensorWirelessElement = toolSensorElement.firstChildElement(mToolSensorWirelessTag);
252  QString toolSensorWirelessText = toolSensorWirelessElement.text();
253  if (toolSensorWirelessText.contains("yes", Qt::CaseInsensitive))
254  internalStructure.mWireless = true;
255  else if (toolSensorWirelessText.contains("no", Qt::CaseInsensitive))
256  internalStructure.mWireless = false;
257 
258  QDomElement toolSensorDOFElement = toolSensorElement.firstChildElement(mToolSensorDOFTag);
259  QString toolSensorDOFText = toolSensorDOFElement.text();
260  if (toolSensorDOFText.contains("5", Qt::CaseInsensitive))
261  internalStructure.m5DOF = true;
262  else if (toolSensorDOFText.contains("6", Qt::CaseInsensitive))
263  internalStructure.m5DOF = false;
264 
265  QDomElement toolSensorPortnumberElement = toolSensorElement.firstChildElement(mToolSensorPortnumberTag);
266  QString toolSensorPortnumberText = toolSensorPortnumberElement.text();
267  internalStructure.mPortNumber = toolSensorPortnumberText.toInt();
268 
269  QDomElement toolSensorChannelnumberElement = toolSensorElement.firstChildElement(mToolSensorChannelnumberTag);
270  QString toolSensorChannelnumberText = toolSensorChannelnumberElement.text();
271  internalStructure.mChannelNumber = toolSensorChannelnumberText.toInt();
272 
273  QDomNodeList toolSensorReferencePointList = toolSensorElement.elementsByTagName(mToolSensorReferencePointTag);
274  for (int j = 0; j < toolSensorReferencePointList.count(); j++)
275  {
276  QDomNode node = toolSensorReferencePointList.item(j);
277  if (!node.hasAttributes())
278  {
279  reportWarning("Found reference point without id attribute. Skipping.");
280  continue;
281  }
282  bool ok;
283  int id = node.toElement().attribute("id").toInt(&ok);
284  if (!ok)
285  {
286  reportWarning("Attribute id of a reference point was not an int. Skipping.");
287  continue;
288  }
289  QString toolSensorReferencePointText = node.toElement().text();
290  Vector3D vector = Vector3D::fromString(toolSensorReferencePointText);
291  internalStructure.mReferencePoints[id] = vector;
292  }
293 
294  QDomElement toolSensorRomFileElement = toolSensorElement.firstChildElement(mToolSensorRomFileTag);
295  QString toolSensorRomFileText = toolSensorRomFileElement.text();
296  if (!toolSensorRomFileText.isEmpty())
297  toolSensorRomFileText = toolFolderAbsolutePath + toolSensorRomFileText;
298  internalStructure.mSROMFilename = toolSensorRomFileText;
299 
300  QDomElement toolCalibrationElement = toolNode.firstChildElement(mToolCalibrationTag);
301  if (toolCalibrationElement.isNull())
302  {
303  reportError(
304  "Could not find the <calibration> tag under the <tool> tag. Aborting this tool.");
305  return retval;
306  }
307  QDomElement toolCalibrationFileElement = toolCalibrationElement.firstChildElement(mToolCalibrationFileTag);
308  QString toolCalibrationFileText = toolCalibrationFileElement.text();
309  if (!toolCalibrationFileText.isEmpty())
310  toolCalibrationFileText = toolFolderAbsolutePath + toolCalibrationFileText;
311  internalStructure.mCalibrationFilename = toolCalibrationFileText;
312  internalStructure.mCalibration = this->readCalibrationFile(internalStructure.mCalibrationFilename);
313 
314  internalStructure.mTransformSaveFileName = mLoggingFolder;
315  internalStructure.mLoggingFolderName = mLoggingFolder;
316  retval = internalStructure;
317 
318  return retval;
319 }
320 
321 QDomNode ToolFileParser::getToolNode(QString toolAbsoluteFilePath)
322 {
323  QDomNode retval;
324  QFile toolFile(toolAbsoluteFilePath);
325  if (!mToolDoc.setContent(&toolFile))
326  {
327  reportError("Could not set the xml content of the tool file " + toolAbsoluteFilePath);
328  return retval;
329  }
330  //there can only be one tool defined in every tool.xml-file, that's why we say ...item(0)
331  retval = mToolDoc.elementsByTagName(mToolTag).item(0);
332  return retval;
333 }
334 
336 {
337  bool ok = true;
338  TransformFile file(absoluteFilePath);
339  Transform3D retval = file.read(&ok);
340 
341  if (ok)
342  {
343  retval = Frame3D::create(retval).transform(); // clean rotational parts, transform should now be pure rotation+translation
344  }
345 
346  return retval;
347 }
348 
349 /*
350 QString ToolFileParser::getTemplatesAbsoluteFilePath()
351 {
352  QString retval = DataLocations::getRootConfigPath() + "/tool/TEMPLATE_tool.xml";
353  return retval;
354 }
355 */
356 
357 }
QString qstring_cast(const T &val)
ToolFileParser(QString absoluteToolFilePath, QString loggingFolder="")
QString mInstrumentScannerId
The id of the ultrasound scanner if the instrument is a probe.
void reportError(QString msg)
Definition: cxLogger.cpp:92
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
bool m5DOF
whether or not the tool have 5 DOF
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
std::vector< QString > mClinicalApplications
the tools clinical application applications
Transform3D transform() const
Definition: cxFrame3D.cpp:161
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
void reportWarning(QString msg)
Definition: cxLogger.cpp:91
const QString mToolInstrumentIdTag
QString mToolFilePath
absolutepath to the tool file
static Frame3D create(const Transform3D &transform)
Definition: cxFrame3D.cpp:149
bool mWireless
whether or not the tool is wireless
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
QString mPictureFileName
path to picture of the tool
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
const QString mToolIdTag
QString mGraphicsFileName
path to this tools graphics file
tsAURORA
NDIs Aurora tracker.
const QString mToolSensorPortnumberTag
void report(QString msg)
Definition: cxLogger.cpp:90
const QString mToolSensorWirelessTag
QString mLoggingFolderName
path to where log should be saved
const QString mToolManufacturerTag
const QString mToolSensorNameTag
const QString mToolSensorDOFTag
const QString mToolClinicalAppTag
virtual ToolInternalStructure getTool()
Transform3D mCalibration
transform read from mCalibrationFilename
const QString mToolInstrumentTypeTag
const QString mToolTypeTag
std::map< int, Vector3D > mReferencePoints
optional point on the frame, specifying a known reference point, 0,0,0 is default, in sensor space
QString mInstrumentId
The instruments id.
QDomNode getToolNode(QString toolAbsoluteFilePath)
const QString mToolTag
Namespace for all CustusX production code.
void setCalibration(const Transform3D &cal)