15 #include <QStringList>
16 #include <QDataStream>
19 #include <vtkImageData.h>
30 mFileManagerService(fileManager)
37 if (calFilesPath.isEmpty())
39 QStringList list = fileName.split(
"/");
40 list[list.size()-1] =
"";
41 calFilesPath = list.join(
"/")+
"/";
47 if (QFileInfo(fileName).suffix().isEmpty())
60 retval.
mUsRaw = this->readUsDataFile(fileName);
62 std::pair<QString, ProbeDefinition> probeDefinitionFull = this->readProbeDefinitionBackwardsCompatible(
changeExtension(fileName,
"mhd"), calFilesPath);
70 "Mismatch in spacing values from calibration and recorded image.\n"
71 "This might be valid if the sound speed was changed prior to recording.\n"
77 retval.
mProbeUid = probeDefinitionFull.first;
80 retval.
mPositions = this->readPositions(fileName);
82 if (!this->valid(retval))
90 double msecs = (retval.
mFrames.rbegin()->mTime - retval.
mFrames.begin()->mTime);
91 report(QString(
"Read %1 seconds of us data from %2.").arg(msecs/1000, 0,
'g', 3).arg(fileName));
101 reportError(
"Mismatch between number of images and number of image positions.\n"
113 std::pair<QString, ProbeDefinition> UsReconstructionFileReader::readProbeDefinitionBackwardsCompatible(QString mhdFileName, QString calFilesPath)
119 report(QString(
"Invalid probe in %1, falling back to old format").arg(retval.first));
120 QString caliFilename;
121 QStringList probeConfigPath;
122 this->readCustomMhdTags(mhdFileName, &probeConfigPath, &caliFilename);
130 ImagePtr UsReconstructionFileReader::generateMask(USReconstructInputData data)
132 Eigen::Array3i dim(data.mUsRaw->getDimensions());
134 Vector3D spacing(data.mUsRaw->getSpacing());
142 void UsReconstructionFileReader::readCustomMhdTags(QString mhdFileName, QStringList* probeConfigPath, QString* calFileName)
146 QFile file(mhdFileName);
147 if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
149 reportWarning(
"Error in Reconstructer::readUsDataFile(): Can't open file: "
152 bool foundConfig =
false;
153 QStringList& configList = *probeConfigPath;
154 bool foundCalFile =
false;
155 while (!file.atEnd())
157 QString line = file.readLine();
158 if(line.startsWith(
"ConfigurationID", Qt::CaseInsensitive))
160 QStringList tempList = line.split(
"=", QString::SkipEmptyParts);
161 configList = tempList[1].trimmed().split(
":", QString::SkipEmptyParts);
162 if (configList.size()>=3)
163 configList[3] = configList[3].trimmed();
166 else if(line.startsWith(
"ProbeCalibration", Qt::CaseInsensitive))
168 QStringList list = line.split(
"=", QString::SkipEmptyParts);
169 *calFileName = list[1].trimmed();
176 reportWarning(QString(
"Error in Reconstructer::readUsDataFile(): ")
177 +
"Can't find ConfigurationID in file: "
182 reportWarning(QString(
"Error in Reconstructer::readUsDataFile(): ")
183 +
"Can't find ProbeCalibration in file: "
190 if (probeConfigPath.size()!=4)
193 QString xmlPath = calFilesPath+
"ProbeCalibConfigs.xml";
197 configuration = xmlConfigParser->getConfiguration(
203 return configuration;
208 std::pair<QString, ProbeDefinition> retval;
211 if (!QFileInfo(filename).exists())
213 reportWarning(
"File not found: " + filename +
", failed to load probe data.");
218 retval.second.parseXml(file.
getElement(
"configuration"));
220 retval.first = file.
getElement(
"tool").toElement().attribute(
"toolID");
225 USFrameDataPtr UsReconstructionFileReader::readUsDataFile(QString mhdFileName)
232 bool useOldFormat = !QFileInfo(
changeExtension(fileName,
"fts")).exists();
233 std::vector<TimedPosition> retval;
246 std::vector<TimedPosition> UsReconstructionFileReader::readPositions(QString fileName)
248 bool useOldFormat = !QFileInfo(
changeExtension(fileName,
"fts")).exists();
249 std::vector<TimedPosition> retval;
253 this->readPositionFile(
changeExtension(fileName,
"pos"),
true, &retval);
257 this->readPositionFile(
changeExtension(fileName,
"tp"),
false, &retval);
263 void UsReconstructionFileReader::readTimeStampsFile(QString fileName,
264 std::vector<TimedPosition>* timedPos)
266 QFile file(fileName);
267 if(!file.open(QIODevice::ReadOnly))
275 while (!file.atEnd())
277 if (i>=timedPos->size())
279 timedPos->push_back(TimedPosition());
282 QByteArray array = file.readLine();
283 double time = QString(array).toDouble(&ok);
289 timedPos->at(i).mTime = time;
295 void UsReconstructionFileReader::readPositionFile(QString posFile,
bool alsoReadTimestamps, std::vector<TimedPosition>* timedPos)
298 if(!file.open(QIODevice::ReadOnly))
307 while (!file.atEnd())
309 if (i>=timedPos->size())
311 timedPos->push_back(TimedPosition());
314 TimedPosition position;
316 if (alsoReadTimestamps)
319 QByteArray array = file.readLine();
320 position.mTime = QString(array).toDouble(&ok);
329 QString positionString = file.readLine();
330 positionString +=
" " + file.readLine();
331 positionString +=
" " + file.readLine();
332 positionString +=
" 0 0 0 1";
333 position.mPos = Transform3D::fromString(positionString, &ok);
344 timedPos->at(i) = position;
351 bool UsReconstructionFileReader::readMaskFile(QString mhdFileName,
ImagePtr mask)
355 if (!QFileInfo(fileName).exists())
361 QFile file(fileName);
362 file.open(QIODevice::ReadOnly);
363 QDataStream stream(&file);
365 unsigned char* dataPtr =
static_cast<unsigned char*
>(data->GetScalarPointer());
366 char *rawchars =
reinterpret_cast<char*
>(dataPtr);
368 stream.readRawData(rawchars, file.size());