14 #include <QTextStream> 18 #include <vtkImageChangeInformation.h> 19 #include <vtkImageData.h> 20 #include "vtkImageAppend.h" 21 #include "vtkMetaImageWriter.h" 42 mSessionDescription(sessionDescription)
52 return mReconstructData;
60 std::vector<TimeInfo> imageTimestamps,
62 std::map<double, ToolPositionMetadata> trackerRecordedMetadata,
63 std::map<double, ToolPositionMetadata> referenceRecordedMetadata,
64 ToolPtr tool, QString streamUid,
67 if(trackerRecordedData.empty())
68 reportWarning(
"No tracking data for writing to reconstruction file.");
78 for (TimedTransformMap::iterator it = trackerRecordedData.begin(); it != trackerRecordedData.end(); ++it)
81 current.
mTime = it->first;
83 current.
mPos = it->second;
87 std::vector<TimeInfo> fts = imageTimestamps;
88 for (
unsigned i=0; i<fts.size(); ++i)
93 current.
mPos = Transform3D::Identity();
95 retval.
mFrames.push_back(current);
98 if (tool && tool->getProbe())
106 this->fillFramePositions(&retval);
121 bool UsReconstructionFileMaker::writeTrackerTimestamps(QString reconstructionFolder, QString session, std::vector<TimedPosition> ts)
123 return this->writeTimestamps(reconstructionFolder+
"/"+session+
".tts",
124 ts,
"tracking timestamps");
127 bool UsReconstructionFileMaker::writeUSTimestamps(QString reconstructionFolder, QString session, std::vector<TimedPosition> ts)
131 retval = this->writeTimestamps(reconstructionFolder+
"/"+session+
".fts",
132 ts,
"frame timestamps", Modified);
134 retval &= this->writeTimestamps(reconstructionFolder+
"/"+session+
".scanner.frameTimestamps",
135 ts,
"frame scanner timestamps", Scanner);
137 retval &= this->writeTimestamps(reconstructionFolder+
"/"+session+
".softwareArrive.frameTimestamps",
138 ts,
"frame arrive in software timestamps", SoftwareArrive);
142 bool UsReconstructionFileMaker::writeTimestamps(QString filename, std::vector<TimedPosition> ts, QString type, TimeStampType timeStampType)
144 bool success =
false;
146 QFile file(filename);
147 if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
152 QTextStream stream(&file);
154 for (
unsigned i=0; i<ts.size(); ++i)
156 switch(timeStampType)
159 stream <<
qstring_cast(ts[i].mTimeInfo.getAcquisitionTime());
162 stream <<
qstring_cast(ts[i].mTimeInfo.getSoftwareAcquisitionTime());
165 stream <<
qstring_cast(ts[i].mTimeInfo.getScannerAcquisitionTime());
176 QFileInfo info(file);
177 mReport << QString(
"%1, %2 bytes, %3 %4.")
178 .arg(info.fileName())
187 bool UsReconstructionFileMaker::writeUSTransforms(QString reconstructionFolder, QString session, std::vector<TimedPosition> ts)
189 return this->writeTransforms(reconstructionFolder+
"/"+session+
".fp", ts,
"frame transforms rMu");
192 bool UsReconstructionFileMaker::writeTrackerMetadata(QString reconstructionFolder, QString session,
const std::map<double, ToolPositionMetadata>& ts)
194 QString filename = reconstructionFolder+
"/"+session+
".probe.toolmeta";
195 return this->writeMetadata(filename, ts,
"tracker metadata");
198 bool UsReconstructionFileMaker::writeReferenceMetadata(QString reconstructionFolder, QString session,
const std::map<double, ToolPositionMetadata>& ts)
200 QString filename = reconstructionFolder+
"/"+session+
".ref.toolmeta";
201 return this->writeMetadata(filename, ts,
"tracker metadata");
204 bool UsReconstructionFileMaker::writeMetadata(QString filename,
const std::map<double, ToolPositionMetadata>& ts, QString type)
206 bool success =
false;
207 QFile file(filename);
208 if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
213 QTextStream stream(&file);
215 for (std::map<double, ToolPositionMetadata>::const_iterator i=ts.begin(); i!=ts.end(); ++i)
217 stream << i->second.toString() << endl;
222 QFileInfo info(file);
228 bool UsReconstructionFileMaker::writeTrackerTransforms(QString reconstructionFolder, QString session, std::vector<TimedPosition> ts)
230 return this->writeTransforms(reconstructionFolder+
"/"+session+
".tp", ts,
"tracking transforms prMt");
233 bool UsReconstructionFileMaker::writeTransforms(QString filename, std::vector<TimedPosition> ts, QString type)
235 bool success =
false;
236 QFile file(filename);
237 if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
242 QTextStream stream(&file);
244 for (
unsigned i=0; i<ts.size(); ++i)
266 QFileInfo info(file);
275 void UsReconstructionFileMaker::writeProbeConfiguration(QString reconstructionFolder, QString session,
ProbeDefinition data, QString uid)
279 file.
getElement(
"tool").toElement().setAttribute(
"toolID", uid);
286 QDir patientDir(patientFolder +
"/US_Acq");
288 QString subfolder = sessionDescription;
289 QString subfolderAbsolutePath = patientDir.absolutePath()+
"/"+subfolder;
290 QString newPathName = subfolderAbsolutePath;
292 while(!findNewSubfolder(newPathName))
294 newPathName = subfolderAbsolutePath+
"_"+QString::number(i++);
296 patientDir.mkpath(newPathName);
297 patientDir.cd(newPathName);
299 retval = patientDir.absolutePath();
306 QDir patientDir(patientFolder +
"/US_Acq");
308 QString subfolder = sessionDescription;
309 QString subfolderAbsolutePath = patientDir.absolutePath()+
"/"+subfolder;
310 QString newPathName = subfolderAbsolutePath;
311 patientDir.mkpath(newPathName);
312 patientDir.cd(newPathName);
314 retval = patientDir.absolutePath();
318 bool UsReconstructionFileMaker::findNewSubfolder(QString subfolderAbsolutePath)
321 if(dir.exists(subfolderAbsolutePath))
327 void UsReconstructionFileMaker::report()
329 foreach(QString
string, mReport)
335 void UsReconstructionFileMaker::writeUSImages(QString path,
ImageDataContainerPtr images,
bool compression, std::vector<TimedPosition> pos)
340 for (
unsigned i=0; i<images->size(); ++i)
343 QString filename = QString(
"%1/%2_%3.mhd").arg(path).arg(mSessionDescription).arg(i);
345 writer->SetInputData(currentImage);
347 writer->SetCompression(compression);
354 customReader->setTransform(pos[i].mPos);
355 customReader->setModality(
"US");
356 customReader->setImageType(mSessionDescription);
360 void UsReconstructionFileMaker::writeMask(QString path, QString session,
vtkImageDataPtr mask)
362 QString filename = QString(
"%1/%2.mask.mhd").arg(path).arg(session);
365 reportWarning(QString(
"No mask found, ignoring write to %1").arg(filename));
370 writer->SetInputData(mask);
372 writer->SetCompression(
false);
377 void UsReconstructionFileMaker::writeREADMEFile(QString reconstructionFolder, QString session)
380 "* ==== Format description \n" 382 "* http://custusx.org/uploads/developer_doc/nightly/org_custusx_acquisition.html \n" 383 "* for a description of the files. \n" 386 QFile file(reconstructionFolder+
"/"+session+
".README.txt");
387 if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
392 QTextStream stream(&file);
400 mReconstructData.
mFilename = path+
"/"+mSessionDescription+
".fts";
403 mReport <<
"Made reconstruction folder: " + path;
404 QString session = mSessionDescription;
408 this->writeTrackerTimestamps(path, session, mReconstructData.
mPositions);
409 this->writeTrackerTransforms(path, session, mReconstructData.
mPositions);
410 this->writeUSTimestamps(path, session, mReconstructData.
mFrames);
411 this->writeUSTransforms(path, session, mReconstructData.
mFrames);
413 this->writeMask(path, session, mReconstructData.
getMask());
414 this->writeREADMEFile(path, session);
418 this->writeUSImages(path, imageData, compression, mReconstructData.
mFrames);
420 mReport <<
"failed to find frame data, save failed.";
423 mReport << QString(
"Completed save to %1. Spent %2s, %3fps").arg(mSessionDescription).arg(time/1000).arg(imageData->size()*1000/time);
QString qstring_cast(const T &val)
DoubleBoundingBox3D transform(const Transform3D &m, const DoubleBoundingBox3D &bb)
void reportError(QString msg)
~UsReconstructionFileMaker()
void addXml(QDomNode dataNode) const
UsReconstructionFileMaker(QString sessionDescription)
#define CX_ASSERT(statement)
One position with timestamp.
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
vtkSmartPointer< vtkImageAppend > vtkImageAppendPtr
cstring_cast_Placeholder cstring_cast(const T &val)
QString writeToNewFolder(QString path, bool compression)
QDomElement getElement()
return the current element
boost::shared_ptr< class CustomMetaImage > CustomMetaImagePtr
void setAcquisitionTime(double mSecsSinceEpoch)
void reportWarning(QString msg)
double getAcquisitionTime() const
vtkSmartPointer< class vtkMetaImageWriter > vtkMetaImageWriterPtr
static QString createFolder(QString patientFolder, QString sessionDescription)
void reportSuccess(QString msg)
Definition of characteristics for an Ultrasound Probe Sector.
USReconstructInputData getReconstructData()
void save()
save entire document.
static USFrameDataPtr create(ImagePtr inputFrameData)
static QString createUniqueFolder(QString patientFolder, QString sessionDescription)
Helper class for xml files used to store ssc/cx data.
std::map< double, Transform3D > TimedTransformMap
void setData(ProbeDefinition data)
boost::shared_ptr< class ImageDataContainer > ImageDataContainerPtr
Namespace for all CustusX production code.
boost::shared_ptr< class Tool > ToolPtr