Fraxinus  17.12-rc1
An IGT application
cxUSAcquisition.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 
33 #include "cxUSAcquisition.h"
34 
35 #include "cxBoolProperty.h"
36 
37 #include "cxSettings.h"
38 #include "cxVideoService.h"
39 #include "cxTrackingService.h"
40 #include "cxUSSavingRecorder.h"
41 #include "cxAcquisitionData.h"
44 #include "cxPatientModelService.h"
45 #include "cxVideoSource.h"
46 #include "cxAcquisitionService.h"
48 #include "cxVisServices.h"
49 
50 
51 namespace cx
52 {
53 
55  QObject(parent),
56  mBase(base),
57  mReady(true),
58  mInfoText("")
59 {
60  mCore.reset(new USSavingRecorder());
61  connect(mCore.get(), SIGNAL(saveDataCompleted(QString)), this, SLOT(checkIfReadySlot()));
62  connect(mCore.get(), SIGNAL(saveDataCompleted(QString)), this, SIGNAL(saveDataCompleted(QString)));
63 
64 
65  connect(this->getServices()->tracking().get(), &TrackingService::stateChanged, this, &USAcquisition::checkIfReadySlot);
66  connect(this->getServices()->tracking().get(), SIGNAL(activeToolChanged(const QString&)), this, SLOT(checkIfReadySlot()));
67  connect(this->getServices()->video().get(), SIGNAL(activeVideoSourceChanged()), this, SLOT(checkIfReadySlot()));
68  connect(this->getServices()->video().get(), &VideoService::connected, this, &USAcquisition::checkIfReadySlot);
69 
70  connect(mBase.get(), SIGNAL(started()), this, SLOT(recordStarted()));
71  connect(mBase.get(), SIGNAL(acquisitionStopped()), this, SLOT(recordStopped()), Qt::QueuedConnection);
72  connect(mBase.get(), SIGNAL(cancelled()), this, SLOT(recordCancelled()));
73 
74  this->checkIfReadySlot();
75 }
76 
78 {
79 
80 }
81 
82 VisServicesPtr USAcquisition::getServices()
83 {
84  return mBase->getPluginData()->getServices();
85 }
86 
87 UsReconstructionServicePtr USAcquisition::getReconstructer()
88 {
89  return mBase->getPluginData()->getReconstructer();
90 }
91 
93 {
94  if (!context.testFlag(AcquisitionService::tUS))
95  return true;
96  return mReady;
97 }
98 
100 {
101  if (!context.testFlag(AcquisitionService::tUS))
102  return "";
103  return mInfoText;
104 }
105 
106 void USAcquisition::checkIfReadySlot()
107 {
108  bool tracking = this->getServices()->tracking()->getState()>=Tool::tsTRACKING;
109  bool streaming = this->getServices()->video()->isConnected();
110  ToolPtr tool = this->getServices()->tracking()->getFirstProbe();
111  ToolPtr reference_frame = this->getServices()->tracking()->getReferenceTool();
112  if(tracking && reference_frame)
113  {
114  QObject::connect(reference_frame.get(), &Tool::toolVisible, this, &USAcquisition::checkIfReadySlot, Qt::UniqueConnection);
115  }
116 
117  QString mWhatsMissing;
118  mWhatsMissing.clear();
119 
120  if(tracking && streaming)
121  {
122  mWhatsMissing = "<font color=green>Ready to record!</font><br>";
123  if (!tool || !tool->getVisible())
124  {
125  mWhatsMissing += "<font color=orange>Probe not visible.</font><br>";
126  }
127  if(!reference_frame || !reference_frame->getVisible())
128  {
129  mWhatsMissing += "<font color=orange>Reference frame not visible.</font><br>";
130  }
131  }
132  else
133  {
134  if(!tracking)
135  mWhatsMissing.append("<font color=red>Need to start tracking.</font><br>");
136  if(!streaming)
137  mWhatsMissing.append("<font color=red>Need to start streaming.</font><br>");
138  }
139 
140  int saving = mCore->getNumberOfSavingThreads();
141 
142  if (saving!=0)
143  mWhatsMissing.append(QString("<font color=orange>Saving %1 acquisition data.</font><br>").arg(saving));
144 
145  // remove redundant line breaks
146  QStringList list = mWhatsMissing.split("<br>", QString::SkipEmptyParts);
147  mWhatsMissing = list.join("<br>");
148 
149  //Make sure we have at least 2 lines to avoid "bouncing buttons"
150  if (list.size() < 2)
151  mWhatsMissing.append("<br>");
152 
153  // do not require tracking to be present in order to perform an acquisition.
154  this->setReady(streaming, mWhatsMissing);
155 }
156 
157 void USAcquisition::setReady(bool val, QString text)
158 {
159  mReady = val;
160  mInfoText = text;
161 
162  emit readinessChanged();
163 }
164 
166 {
167  return mCore->getNumberOfSavingThreads();
168 }
169 
170 void USAcquisition::recordStarted()
171 {
172  if (!mBase->getCurrentContext().testFlag(AcquisitionService::tUS))
173  return;
174 
175  this->getReconstructer()->selectData(USReconstructInputData()); // clear old data in reconstructeer
176 
177  ToolPtr tool = this->getServices()->tracking()->getFirstProbe();
178  mCore->setWriteColor(this->getWriteColor());
179  mCore->startRecord(mBase->getLatestSession(),
180  tool,
181  this->getServices()->tracking()->getReferenceTool(),
182  this->getRecordingVideoSources(tool));
183 }
184 
185 void USAcquisition::recordStopped()
186 {
187  if (!mBase->getCurrentContext().testFlag(AcquisitionService::tUS))
188  return;
189 
190  mCore->stopRecord();
191 
192  this->sendAcquisitionDataToReconstructer();
193 
194  mCore->set_rMpr(this->getServices()->patient()->get_rMpr());
195  bool compress = settings()->value("Ultrasound/CompressAcquisition", true).toBool();
196  QString baseFolder = this->getServices()->patient()->getActivePatientFolder();
197  mCore->startSaveData(baseFolder, compress);
198 
199  mCore->clearRecording();
200 
201  this->checkIfReadySlot();
202 }
203 
204 void USAcquisition::recordCancelled()
205 {
206  mCore->cancelRecord();
207 }
208 
209 void USAcquisition::sendAcquisitionDataToReconstructer()
210 {
211  mCore->set_rMpr(this->getServices()->patient()->get_rMpr());
212 
213  VideoSourcePtr activeVideoSource = this->getServices()->video()->getActiveVideoSource();
214  if (activeVideoSource)
215  {
216  USReconstructInputData data = mCore->getDataForStream(activeVideoSource->getUid());
217  this->getReconstructer()->selectData(data);
218  emit acquisitionDataReady();
219  }
220 }
221 
222 std::vector<VideoSourcePtr> USAcquisition::getRecordingVideoSources(ToolPtr tool)
223 {
224  std::vector<VideoSourcePtr> retval;
225 
226  if (tool && tool->getProbe())
227  {
228  ProbePtr probe = tool->getProbe();
229  QStringList sources = probe->getAvailableVideoSources();
230  for (unsigned i=0; i<sources.size(); ++i)
231  retval.push_back(probe->getRTSource(sources[i]));
232  }
233  else
234  {
235  retval = this->getServices()->video()->getVideoSources();
236  }
237 
238  return retval;
239 }
240 
241 bool USAcquisition::getWriteColor()
242 {
243  PropertyPtr angio = this->getReconstructer()->getParam("Angio data");
244  bool writeColor = angio->getValueAsVariant().toBool()
245  || !settings()->value("Ultrasound/8bitAcquisitionData").toBool();
246  return writeColor;
247 }
248 
249 }
boost::shared_ptr< class UsReconstructionService > UsReconstructionServicePtr
void saveDataCompleted(QString mhdFilename)
emitted when data has been saved to file
void connected(bool on)
boost::shared_ptr< class VisServices > VisServicesPtr
Definition: cxMainWindow.h:61
Record and save ultrasound data.Use the start/stop pair to record video from the input streams during...
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Definition: cxSettings.cpp:87
USAcquisition(AcquisitionPtr base, QObject *parent=0)
boost::shared_ptr< class Acquisition > AcquisitionPtr
bool isReady(AcquisitionService::TYPES context) const
boost::shared_ptr< Probe > ProbePtr
Definition: cxProbe.h:93
void toolVisible(bool visible)
boost::shared_ptr< class Property > PropertyPtr
boost::shared_ptr< class VideoSource > VideoSourcePtr
Settings * settings()
Shortcut for accessing the settings instance.
Definition: cxSettings.cpp:42
void acquisitionDataReady()
emitted when data is acquired and sent to the reconstruction module
emitting tracking data
Definition: cxTool.h:98
int getNumberOfSavingThreads() const
QString getInfoText(AcquisitionService::TYPES context) const
Namespace for all CustusX production code.
boost::shared_ptr< class Tool > ToolPtr