35 #include <QTextStream> 39 #include <vtkImageChangeInformation.h> 40 #include <vtkImageData.h> 41 #include "vtkImageAppend.h" 42 #include "vtkMetaImageWriter.h" 63 mSessionDescription(sessionDescription)
73 return mReconstructData;
81 std::vector<TimeInfo> imageTimestamps,
83 std::map<double, ToolPositionMetadata> trackerRecordedMetadata,
84 std::map<double, ToolPositionMetadata> referenceRecordedMetadata,
85 ToolPtr tool, QString streamUid,
88 if(trackerRecordedData.empty())
89 reportWarning(
"No tracking data for writing to reconstruction file.");
99 for (TimedTransformMap::iterator it = trackerRecordedData.begin(); it != trackerRecordedData.end(); ++it)
102 current.
mTime = it->first;
104 current.
mPos = it->second;
108 std::vector<TimeInfo> fts = imageTimestamps;
109 for (
unsigned i=0; i<fts.size(); ++i)
114 current.
mPos = Transform3D::Identity();
116 retval.
mFrames.push_back(current);
119 if (tool && tool->getProbe())
127 this->fillFramePositions(&retval);
142 bool UsReconstructionFileMaker::writeTrackerTimestamps(QString reconstructionFolder, QString session, std::vector<TimedPosition> ts)
144 return this->writeTimestamps(reconstructionFolder+
"/"+session+
".tts",
145 ts,
"tracking timestamps");
148 bool UsReconstructionFileMaker::writeUSTimestamps(QString reconstructionFolder, QString session, std::vector<TimedPosition> ts)
152 retval = this->writeTimestamps(reconstructionFolder+
"/"+session+
".fts",
153 ts,
"frame timestamps", Modified);
155 retval &= this->writeTimestamps(reconstructionFolder+
"/"+session+
".scanner.frameTimestamps",
156 ts,
"frame scanner timestamps", Scanner);
158 retval &= this->writeTimestamps(reconstructionFolder+
"/"+session+
".softwareArrive.frameTimestamps",
159 ts,
"frame arrive in software timestamps", SoftwareArrive);
163 bool UsReconstructionFileMaker::writeTimestamps(QString filename, std::vector<TimedPosition> ts, QString type, TimeStampType timeStampType)
165 bool success =
false;
167 QFile file(filename);
168 if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
173 QTextStream stream(&file);
175 for (
unsigned i=0; i<ts.size(); ++i)
177 switch(timeStampType)
180 stream <<
qstring_cast(ts[i].mTimeInfo.getAcquisitionTime());
183 stream <<
qstring_cast(ts[i].mTimeInfo.getSoftwareAcquisitionTime());
186 stream <<
qstring_cast(ts[i].mTimeInfo.getScannerAcquisitionTime());
197 QFileInfo info(file);
198 mReport << QString(
"%1, %2 bytes, %3 %4.")
199 .arg(info.fileName())
208 bool UsReconstructionFileMaker::writeUSTransforms(QString reconstructionFolder, QString session, std::vector<TimedPosition> ts)
210 return this->writeTransforms(reconstructionFolder+
"/"+session+
".fp", ts,
"frame transforms rMu");
213 bool UsReconstructionFileMaker::writeTrackerMetadata(QString reconstructionFolder, QString session,
const std::map<double, ToolPositionMetadata>& ts)
215 QString filename = reconstructionFolder+
"/"+session+
".probe.toolmeta";
216 return this->writeMetadata(filename, ts,
"tracker metadata");
219 bool UsReconstructionFileMaker::writeReferenceMetadata(QString reconstructionFolder, QString session,
const std::map<double, ToolPositionMetadata>& ts)
221 QString filename = reconstructionFolder+
"/"+session+
".ref.toolmeta";
222 return this->writeMetadata(filename, ts,
"tracker metadata");
225 bool UsReconstructionFileMaker::writeMetadata(QString filename,
const std::map<double, ToolPositionMetadata>& ts, QString type)
227 bool success =
false;
228 QFile file(filename);
229 if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
234 QTextStream stream(&file);
236 for (std::map<double, ToolPositionMetadata>::const_iterator i=ts.begin(); i!=ts.end(); ++i)
238 stream << i->second.toString() << endl;
243 QFileInfo info(file);
249 bool UsReconstructionFileMaker::writeTrackerTransforms(QString reconstructionFolder, QString session, std::vector<TimedPosition> ts)
251 return this->writeTransforms(reconstructionFolder+
"/"+session+
".tp", ts,
"tracking transforms prMt");
254 bool UsReconstructionFileMaker::writeTransforms(QString filename, std::vector<TimedPosition> ts, QString type)
256 bool success =
false;
257 QFile file(filename);
258 if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
263 QTextStream stream(&file);
265 for (
unsigned i=0; i<ts.size(); ++i)
287 QFileInfo info(file);
296 void UsReconstructionFileMaker::writeProbeConfiguration(QString reconstructionFolder, QString session,
ProbeDefinition data, QString uid)
300 file.
getElement(
"tool").toElement().setAttribute(
"toolID", uid);
307 QDir patientDir(patientFolder +
"/US_Acq");
309 QString subfolder = sessionDescription;
310 QString subfolderAbsolutePath = patientDir.absolutePath()+
"/"+subfolder;
311 QString newPathName = subfolderAbsolutePath;
313 while(!findNewSubfolder(newPathName))
315 newPathName = subfolderAbsolutePath+
"_"+QString::number(i++);
317 patientDir.mkpath(newPathName);
318 patientDir.cd(newPathName);
320 retval = patientDir.absolutePath();
327 QDir patientDir(patientFolder +
"/US_Acq");
329 QString subfolder = sessionDescription;
330 QString subfolderAbsolutePath = patientDir.absolutePath()+
"/"+subfolder;
331 QString newPathName = subfolderAbsolutePath;
332 patientDir.mkpath(newPathName);
333 patientDir.cd(newPathName);
335 retval = patientDir.absolutePath();
339 bool UsReconstructionFileMaker::findNewSubfolder(QString subfolderAbsolutePath)
342 if(dir.exists(subfolderAbsolutePath))
348 void UsReconstructionFileMaker::report()
350 foreach(QString
string, mReport)
356 void UsReconstructionFileMaker::writeUSImages(QString path,
ImageDataContainerPtr images,
bool compression, std::vector<TimedPosition> pos)
361 for (
unsigned i=0; i<images->size(); ++i)
364 QString filename = QString(
"%1/%2_%3.mhd").arg(path).arg(mSessionDescription).arg(i);
366 writer->SetInputData(currentImage);
368 writer->SetCompression(compression);
375 customReader->setTransform(pos[i].mPos);
376 customReader->setModality(
"US");
377 customReader->setImageType(mSessionDescription);
381 void UsReconstructionFileMaker::writeMask(QString path, QString session,
vtkImageDataPtr mask)
383 QString filename = QString(
"%1/%2.mask.mhd").arg(path).arg(session);
386 reportWarning(QString(
"No mask found, ignoring write to %1").arg(filename));
391 writer->SetInputData(mask);
393 writer->SetCompression(
false);
398 void UsReconstructionFileMaker::writeREADMEFile(QString reconstructionFolder, QString session)
401 "* ==== Format description \n" 403 "* http://custusx.org/uploads/developer_doc/nightly/org_custusx_acquisition.html \n" 404 "* for a description of the files. \n" 407 QFile file(reconstructionFolder+
"/"+session+
".README.txt");
408 if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
413 QTextStream stream(&file);
421 mReconstructData.
mFilename = path+
"/"+mSessionDescription+
".fts";
424 mReport <<
"Made reconstruction folder: " + path;
425 QString session = mSessionDescription;
429 this->writeTrackerTimestamps(path, session, mReconstructData.
mPositions);
430 this->writeTrackerTransforms(path, session, mReconstructData.
mPositions);
431 this->writeUSTimestamps(path, session, mReconstructData.
mFrames);
432 this->writeUSTransforms(path, session, mReconstructData.
mFrames);
434 this->writeMask(path, session, mReconstructData.
getMask());
435 this->writeREADMEFile(path, session);
439 this->writeUSImages(path, imageData, compression, mReconstructData.
mFrames);
441 mReport <<
"failed to find frame data, save failed.";
444 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