CustusX  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxPlaybackUSAcquisitionVideo.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 
34 #include <QStringList>
35 #include <QDir>
36 #include <QtCore>
37 #include <boost/bind.hpp>
38 #include "vtkImageImport.h"
39 #include "vtkImageData.h"
40 #include "cxTypeConversions.h"
42 #include "cxTestVideoSource.h"
43 #include "cxTrackingService.h"
44 #include "cxReporter.h"
45 //#include "cxProbeImpl.h"
46 #include "cxUSFrameData.h"
47 #include "cxPlaybackTime.h"
48 
49 #include "cxBasicVideoSource.h"
50 #include "cxImage.h"
51 #include "cxImageDataContainer.h"
52 #include "cxVideoServiceBackend.h"
53 #include "cxFileHelpers.h"
54 #include <QtConcurrent>
55 #include "cxTool.h"
56 
57 namespace cx
58 {
59 
61  QObject(NULL),
62  mVideoSourceUid("playback " + type)
63 {
64  mType=type;
65  mBackend = backend;
66  mVideoSource.reset(new BasicVideoSource(mVideoSourceUid));
67  mVideoSource->setStatusString(QString("No US Acquisition"));
68 
69  connect(&mUSImageDataFutureWatcher, SIGNAL(finished()), this, SLOT(usDataLoadFinishedSlot()));
70 }
71 
73 {
74 }
75 
76 
78 {
79  return mVideoSource;
80 }
81 
83 {
84  return mTimer ? true : false;
85 }
86 
88 {
89  if (mTimer)
90  disconnect(mTimer.get(), SIGNAL(changed()), this, SLOT(timerChangedSlot()));
91  mTimer = controller;
92  if (mTimer)
93  connect(mTimer.get(), SIGNAL(changed()), this, SLOT(timerChangedSlot()));
94 
95  if (controller)
96  {
97  mVideoSource->start();
98  }
99  else
100  {
101  mVideoSource->deconfigure();
102  }
103 }
104 
105 void USAcquisitionVideoPlayback::setRoot(const QString path)
106 {
107  mRoot = path;
108  mEvents = this->getEvents();
109 }
110 
111 std::vector<TimelineEvent> USAcquisitionVideoPlayback::getEvents()
112 {
113  std::vector<TimelineEvent> events;
114 
115  QStringList allFiles = this->getAbsolutePathToFtsFiles(mRoot);
116  for (int i=0; i<allFiles.size(); ++i)
117  {
119  std::vector<TimedPosition> timestamps = reader.readFrameTimestamps(allFiles[i]);
120 
121  if (timestamps.empty())
122  continue;
123 
124  TimelineEvent current(
125  QString("Acquisition %1").arg(QFileInfo(allFiles[i]).fileName()),
126  timestamps.front().mTime,
127  timestamps.back().mTime);
128  current.mUid = allFiles[i];
129  current.mGroup = "acquisition";
130  current.mColor = QColor::fromHsv(36, 255, 222);
131 
132  events.push_back(current);
133 
134  }
135 
136  return events;
137 }
138 
141 QStringList USAcquisitionVideoPlayback::getAbsolutePathToFtsFiles(QString folder)
142 {
143  return getAbsolutePathToFiles(folder,QStringList(QString("*_").append(mType)), true);
144 }
145 
147 {
148  return mType;
149 }
150 
151 void USAcquisitionVideoPlayback::timerChangedSlot()
152 {
153  TimelineEvent event;
154  for (unsigned i=0; i<mEvents.size(); ++i)
155  {
156  if (mEvents[i].isInside(mTimer->getTime().toMSecsSinceEpoch()))
157  {
158  event = mEvents[i];
159  break;
160  }
161  }
162 
163  this->loadFullData(event.mUid);
164  this->updateFrame(event.mUid);
165 }
166 
167 void USAcquisitionVideoPlayback::loadFullData(QString filename)
168 {
169  // if same filename, ok and return
170  if (filename == mCurrentData.mFilename)
171  return;
172 
173  mVideoSource->setInfoString(QString(""));
174  mVideoSource->setStatusString(QString("No US Acquisition"));
175  mVideoSource->deconfigure();
176 
177  // if no data: ignore but keep the already loaded data
178  if (filename.isEmpty())
179  return;
180 
181  // clear data
182  mCurrentData = USReconstructInputData();
183 
184  // if no new data, return
185  if (filename.isEmpty())
186  return;
187 
188  // load new data
189  // start an asynchronous read of data
190  if (!mUSImageDataReader)
191  {
192  mUSImageDataReader.reset(new UsReconstructionFileReader());
193  mUSImageDataFutureResult = QtConcurrent::run(boost::bind(&UsReconstructionFileReader::readAllFiles, mUSImageDataReader, filename, ""));
194  mUSImageDataFutureWatcher.setFuture(mUSImageDataFutureResult);
195  }
196 }
197 
198 void USAcquisitionVideoPlayback::usDataLoadFinishedSlot()
199 {
200  // file read operation has completed: read and clear
201  mCurrentData = mUSImageDataFutureResult.result();
202  mCurrentData.mProbeDefinition.mData.setUid(mVideoSourceUid);
203  // clear result so we can check for it next run
204  mUSImageDataReader.reset();
205 
206  mVideoSource->start();
207 
208  // set the probe sector from file data:
209  ToolPtr tool = mBackend->tracking()->getFirstProbe();
210  if (tool)
211  {
212  ProbePtr probe = tool->getProbe();
213  if (probe)
214  probe->setProbeDefinition(mCurrentData.mProbeDefinition.mData);
215  }
216 
217  // create a vector to allow for quick search
218  mCurrentTimestamps.clear();
219  for (unsigned i=0; i<mCurrentData.mFrames.size(); ++i)
220  mCurrentTimestamps.push_back(mCurrentData.mFrames[i].mTime);
221 
222  this->updateFrame(mCurrentData.mFilename);
223 }
224 
225 void USAcquisitionVideoPlayback::updateFrame(QString filename)
226 {
227  if (mUSImageDataReader)
228  {
229  mVideoSource->setInfoString(QString("Loading US Data..."));
230  mVideoSource->setStatusString(QString("Loading US Data..."));
231  mVideoSource->setInput(ImagePtr());
232  return;
233  }
234 
235  if (mCurrentData.mFilename.isEmpty() || !mCurrentData.mUsRaw || filename!=mCurrentData.mFilename)
236  {
237  mVideoSource->setInfoString(QString(""));
238  mVideoSource->setStatusString(QString("No US Acquisition"));
239  mVideoSource->setInput(ImagePtr());
240  return;
241  }
242 
243  // if not already started:
244  mVideoSource->start();
245 
246  double timestamp = mTimer->getTime().toMSecsSinceEpoch();
247 
248  // find index of current frame: Use the last frame _before_ the current timestamp.
249  std::vector<double>::iterator iter = std::lower_bound(mCurrentTimestamps.begin(), mCurrentTimestamps.end(), timestamp);
250  if (iter==mCurrentTimestamps.begin())
251  return;
252  --iter; // use the frame before, not after.
253  int index = std::distance(mCurrentTimestamps.begin(), iter);
254 
255  int timeout = 1000; // invalidate data if timestamp differ from time too much
256  mVideoSource->overrideTimeout(fabs(timestamp-*iter)>timeout);
257 
258  ImagePtr image(new Image(mVideoSourceUid, mCurrentData.mUsRaw->getImageContainer()->get(index)));
259  image->setAcquisitionTime(QDateTime::fromMSecsSinceEpoch(timestamp));
260 
261  mVideoSource->setInfoString(QString("%1 - Frame %2").arg(mCurrentData.mUsRaw->getName()).arg(index));
262  if (mVideoSource->validData())
263  mVideoSource->setStatusString(QString(""));
264  else
265  mVideoSource->setStatusString(QString("Timeout"));
266 
267  mVideoSource->setInput(image);
268 }
269 
270 
271 
272 } // cx
273 
Reader class for the US Acquisition files.
void setTime(PlaybackTimePtr controller)
Description of one event in time.
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
std::vector< TimedPosition > readFrameTimestamps(QString fileName)
std::vector< TimedPosition > mFrames
boost::shared_ptr< class VideoServiceBackend > VideoServiceBackendPtr
boost::shared_ptr< class PlaybackTime > PlaybackTimePtr
boost::shared_ptr< Probe > ProbePtr
Definition: cxProbe.h:93
USReconstructInputData readAllFiles(QString fileName, QString calFilesPath="")
void setUid(QString uid)
boost::shared_ptr< class VideoSource > VideoSourcePtr
USAcquisitionVideoPlayback(VideoServiceBackendPtr backend, QString type)
VideoSource controlled by a vtkImageData.
ProbeDefinition mData
Definition: cxProbeSector.h:75
std::vector< TimelineEvent > getEvents()
QStringList getAbsolutePathToFiles(QString path, QStringList nameFilters, bool includeSubDirs)
QString mFilename
filename used for current data read
USFrameDataPtr mUsRaw
All imported US data frames with pointers to each frame.
boost::shared_ptr< class Tool > ToolPtr