Fraxinus  18.10
An IGT application
cxAngleCorrectionWidget.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 "cxDoubleProperty.h"
35 #include "cxHelperWidgets.h"
36 #include "cxLogger.h"
37 #include "cxMesh.h"
38 #include "cxProfile.h"
41 #include "cxSettings.h"
42 #include "cxTime.h"
43 #include "cxViewGroupData.h"
44 #include "cxViewService.h"
45 #include "cxVisServices.h"
46 #include "Exceptions.hpp"
47 #include <QDir>
48 #include <QLabel>
49 #include <QVBoxLayout>
50 
51 
52 
53 
55 
56 namespace cx
57 {
58 
60  BaseWidget(parent, "AngleCorrectionWidget", "Angle Correction"),
61  mVerticalLayout(new QVBoxLayout(this)),
62  mVelFileSelectWidget( new FileSelectWidget(this)),
63  mVisServices(visServices)
64 {
65  mSettings = profile()->getXmlSettings().descend("angleCorr");
66  connect(mVisServices->patient().get(), SIGNAL(patientChanged()), this, SLOT(patientChangedSlot()));
67  this->setWhatsThis(this->defaultWhatsThis());
68 
69  mClDataSelectWidget = StringPropertySelectMesh::New(mVisServices->patient());
70  mClDataSelectWidget->setUidRegexp("tsf_cl(?!.*angleCorr).*");
71  mClDataSelectWidget->setValueName("Centerline: ");
72  mVerticalLayout->addWidget(new DataSelectWidget(mVisServices->view(), mVisServices->patient(), this, mClDataSelectWidget));
73  connect(mClDataSelectWidget.get(), SIGNAL(changed()), this, SLOT(cLDataChangedSlot()));
74  connect(mClDataSelectWidget.get(), SIGNAL(changed()), this, SLOT(step1ParamChangedSlot()));
75 
76 
77  mOptionsWidget = this->createOptionsWidget();
78  mOptionsWidget->setVisible(settings()->value("AngleCorr/AngleCorrShowDetails").toBool());
79  mVerticalLayout->addWidget(mOptionsWidget);
80 
81  this->createAction(this,
82  QIcon(":/icons/open_icon_library/system-run-5.png"),
83  "Details", "Show angle correction settings",
84  SLOT(toggleDetailsSlot()),
85  mVerticalLayout);
86 
87  mRunAngleCorrButton = new QPushButton("Angle correct", this);
88  connect(mRunAngleCorrButton, &QPushButton::clicked, this, &AngleCorrectionWidget::runAngleCorection);
89  mVerticalLayout->addWidget(mRunAngleCorrButton);
90 
91  mOutDataSelectWidget = StringPropertySelectMesh::New(mVisServices->patient());
92  mOutDataSelectWidget->setUidRegexp("angleCorr");
93  mOutDataSelectWidget->setValueName("Output: ");
94  mVerticalLayout->addWidget(new DataSelectWidget(mVisServices->view(), mVisServices->patient(), this, mOutDataSelectWidget));
95 
96  mExecuter.reset(new AngleCorrectionExecuter());
97  connect(mExecuter.get(), SIGNAL(finished()), this, SLOT(executionFinished()));
98  connect(mExecuter.get(), SIGNAL(aboutToStart()), this, SLOT(preprocessExecuter()));
99  mTimedAlgorithmProgressBar = new cx::TimedAlgorithmProgressBar;
100  mTimedAlgorithmProgressBar->attach(mExecuter);
101  mVerticalLayout->addWidget(mTimedAlgorithmProgressBar);
102 
103  mVerticalLayout->addStretch();
104 
105  this->patientChangedSlot();
106  this->cLDataChangedSlot();
107  mOptionsWidget->setVisible(false);
108 
109  mUid="";
110  mName="";
111  mStep1ParamChanged=true;
112 
113  mVNyq = 0.0;
114 
115 }
116 
118 {
119 
120 }
121 
122 QString AngleCorrectionWidget::defaultWhatsThis() const
123 {
124  return "<html>"
125  "<h3>AngleCorrection plugin.</h3>"
126  "<p>Angle correct velocities from ultrasound acquisitons</p>"
127  "</html>";
128 }
129 
131 {
132  QString dataFilename = mVisServices->patient()->getActivePatientFolder() + "/US_Acq/";
133  mVelFileSelectWidget->setPath(dataFilename);
134 }
135 
137 {
138  if(!mClDataSelectWidget->getMesh()){
139  return;
140  }
141  QString clUid = mClDataSelectWidget->getMesh()->getUid().section("_",1,2 );
142  QStringList files=mVelFileSelectWidget->getAllFiles();
143  for (int i = 0; i < files.size(); ++i)
144  {
145  if(files.at(i).contains(clUid))
146  {
147  mVelFileSelectWidget->setFilename(files.at(i));
148  return;
149  }
150  }
151 
152  // No velocity data found => open advanced settings
153  mOptionsWidget->setVisible(true);
154 }
155 
156 
157 void AngleCorrectionWidget::step1ParamChangedSlot()
158 {
159  mStep1ParamChanged=true;
160 }
161 
162 void AngleCorrectionWidget::step2ParamChangedSlot()
163 {
164  if(mStep1ParamChanged) return;
165  setInput();
166  if(!mExecuter->calculate(false)) return;
167  vtkSmartPointer<vtkPolyData> output = mExecuter->getOutput();
168  mOutData->setVtkPolyData(output);
169  report(QString("Angle correction updated"));
170 }
171 
173 {
174  if (filename.isEmpty())
175  {
176  reportWarning("No velocity file selected");
177  return;
178  }
179  mVelFileSelectWidget->setFilename(filename);
180  step1ParamChangedSlot();
181 }
182 
184 {
185  mOptionsWidget->setVisible(!mOptionsWidget->isVisible());
186  settings()->setValue("AngleCorr/AngleCorrShowDetails", mOptionsWidget->isVisible());
187 }
188 
189 QWidget* AngleCorrectionWidget::createOptionsWidget()
190 {
191  QWidget* retval = new QWidget(this);
192  QGridLayout* layout = new QGridLayout(retval);
193  layout->setMargin(0);
194 
195  int line = 0;
196 
197  layout->addWidget(this->createHorizontalLine(), line, 0, 1, 3);
198  ++line;
199 
200  QLabel* velLabel = new QLabel("Velocity data:");
201  layout->addWidget(velLabel,line,0);
202  connect(mVelFileSelectWidget, &FileSelectWidget::fileSelected, this,&AngleCorrectionWidget::selectVelData);
203  // connect(mVelFileSelectWidget, &FileSelectWidget::fileSelected, this,SLOT(Step1ParamChangedSlot()));
204 
205  mVelFileSelectWidget->setNameFilter(QStringList() << "*Velocity.fts");
206  layout->addWidget(mVelFileSelectWidget,line,1);
207  ++line;
208 
209  layout->addWidget(new QLabel("Centerline smoothing:", this), line, 0);
210  mClSmoothing = DoubleProperty::initialize("ClSmoothing", " ", "Smoothing of the centerline", 5, DoubleRange(0, 100, 1), 0, mSettings.getElement());
211  mClSmoothing->setGuiRepresentation(DoublePropertyBase::grSLIDER);
212  connect(mClSmoothing.get(), SIGNAL(changed()), this, SLOT(step1ParamChangedSlot()));
213  layout->addWidget(createDataWidget(mVisServices->view(), mVisServices->patient(), this, mClSmoothing), line, 1);
214  ++line;
215 
216  layout->addWidget(new QLabel("Max angle cut off [deg]:", this), line, 0);
217  mMaxThetaCutoff = DoubleProperty::initialize("maxThetaCutoff", " ", "Data from steeper angle will be ignored", 70.0, DoubleRange(0, 90, 1), 0, mSettings.getElement());
218  mMaxThetaCutoff->setGuiRepresentation(DoublePropertyBase::grSLIDER);
219  connect(mMaxThetaCutoff.get(), SIGNAL(changed()), this, SLOT(step1ParamChangedSlot()));
220  layout->addWidget(createDataWidget(mVisServices->view(), mVisServices->patient(), this, mMaxThetaCutoff), line, 1);
221  ++line;
222 
223  layout->addWidget(new QLabel("FLow direction certainty cut off:", this), line, 0);
224  mUncertaintyLimit = DoubleProperty::initialize("uncertaintyLimit", " ", "Semgents with lower certainty will be ignored", 0.0, DoubleRange(0, 1, 0.1), 1, mSettings.getElement());
225  mUncertaintyLimit->setGuiRepresentation(DoublePropertyBase::grSLIDER);
226  connect(mUncertaintyLimit.get(), SIGNAL(changed()), this, SLOT(step2ParamChangedSlot()));
227  layout->addWidget(createDataWidget(mVisServices->view(), mVisServices->patient(), this, mUncertaintyLimit), line, 1);
228  ++line;
229 
230  layout->addWidget(new QLabel("Min arrow dist. [mm]:", this), line, 0);
231  mMinArrowDist = DoubleProperty::initialize("minArrowDist", " ", "Min dist between visualization arrows [mm]", 0.3, DoubleRange(0, 10, 0.1), 1, mSettings.getElement());
232  mMinArrowDist->setGuiRepresentation(DoublePropertyBase::grSLIDER);
233  connect(mMinArrowDist.get(), SIGNAL(changed()), this, SLOT(step2ParamChangedSlot()));
234  layout->addWidget(createDataWidget(mVisServices->view(), mVisServices->patient(), this, mMinArrowDist), line, 1);
235  ++line;
236 
237  return retval;
238 }
239 
241 {
242  return mOutData;
243 
244 }
245 
247 {
248  mMinArrowDist->setValue(value);
249 }
250 
252 {
253  mUncertaintyLimit->setValue(value);
254 }
255 
257 {
258  mMaxThetaCutoff->setValue(value);
259 }
260 
262 {
263  mClSmoothing->setValue(value);
264 }
265 
267 {
268  mVNyq = value;
269 }
270 
272 {
273  mClDataSelectWidget->setValue(value);
274 }
275 
277  if(mExecuter)
278  {
279  return mExecuter->isRunning();
280  }
281  return false;
282 }
283 
285 {
286  if(!mClDataSelectWidget->getMesh()){
287  reportError("No centerline selected");
288  return;
289  }
290  QString clFilename =QDir(mVisServices->patient()->getActivePatientFolder()).filePath(mClDataSelectWidget->getMesh()->getFilename());
291 
292  QString dataFilename = mVelFileSelectWidget->getFilename();
293  if(dataFilename.length() ==0){
294  reportError("No velocity data selected");
295  mOptionsWidget->setVisible(true);
296  return;
297  }
298  dataFilename.replace(".fts","_");
299  double cutoff = cos(mMaxThetaCutoff->getValue()/180.0*M_PI);
300  int nConvolutions = (int) mClSmoothing->getValue();
301  double uncertainty_limit = mUncertaintyLimit->getValue();
302  double minArrowDist = mMinArrowDist->getValue();
303 
304  mExecuter->setInput(clFilename, dataFilename, mVNyq, cutoff, nConvolutions, uncertainty_limit, minArrowDist);
305 }
306 
307 void AngleCorrectionWidget::preprocessExecuter()
308 {
309  setInput();
310  mRunAngleCorrButton->setEnabled(false);
311 }
312 
314 {
315  mExecuter->execute();
316 }
317 
318 void AngleCorrectionWidget::executionFinished()
319 {
320  mRunAngleCorrButton->setEnabled(true);
321  vtkSmartPointer<vtkPolyData> output = mExecuter->getOutput();
322  if(output==NULL)
323  {
324  reportError("Invalid output from anglecorrection algorithm");
325  return;
326  }
327  if (mExecuter->getNumOfStepsRan() > 1)
328  {
329  mUid = mClDataSelectWidget->getMesh()->getUid() + "_angleCorr%1";
330  mName = mClDataSelectWidget->getMesh()->getName()+" angleCorr%1";
331  mOutData = mVisServices->patient()->createSpecificData<Mesh>(mUid, mName);
332  mOutData->get_rMd_History()->setParentSpace(mClDataSelectWidget->getMesh()->getUid());
333  }
334 
335  mOutData->setVtkPolyData(output);
336 
337  mVisServices->patient()->insertData(mOutData);
338  mVisServices->view()->autoShowData(mOutData);
339  mOutDataSelectWidget->setValue(mOutData->getUid());
340  mStep1ParamChanged=false;
341 }
342 
343 } /* namespace cx */
void fileSelected(QString name)
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:160
void setNameFilter(QStringList filter)
void reportError(QString msg)
Definition: cxLogger.cpp:71
A mesh data set.
Definition: cxMesh.h:45
boost::shared_ptr< class VisServices > VisServicesPtr
Definition: cxMainWindow.h:40
AngleCorrectionWidget(VisServicesPtr visServices, QWidget *parent)
QString getFilename() const
Utility class for describing a bounded numeric range.
Definition: cxDoubleRange.h:32
Show progress for a TimedBaseAlgorithm.
QDomElement getElement()
return the current element
QWidget * createDataWidget(ViewServicePtr viewService, PatientModelServicePtr patientModelService, QWidget *parent, PropertyPtr data, QGridLayout *gridLayout, int row)
Create a widget capable of displaying the input data.
QAction * createAction(QObject *parent, QIcon iconName, QString text, QString tip, T slot, QLayout *layout=NULL, QToolButton *button=new QToolButton())
Definition: cxBaseWidget.h:129
void selectVelData(QString filename)
void setValue(const QString &key, const QVariant &value)
Definition: cxSettings.cpp:58
static QFrame * createHorizontalLine()
Creates a horizontal line which can be inserted into widgets.
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
void setFilename(QString name)
void setPath(QString path)
Settings * settings()
Shortcut for accessing the settings instance.
Definition: cxSettings.cpp:21
void attach(TimedAlgorithmPtr algorithm)
Interface for QWidget which handles widgets uniformly for the system.
Definition: cxBaseWidget.h:88
void report(QString msg)
Definition: cxLogger.cpp:69
static DoublePropertyPtr initialize(const QString &uid, QString name, QString help, double value, DoubleRange range, int decimals, QDomNode root=QDomNode())
static StringPropertySelectMeshPtr New(PatientModelServicePtr patientModelService)
boost::shared_ptr< class Mesh > MeshPtr
Widget for displaying and selecting a single file.
#define M_PI
Namespace for all CustusX production code.