CustusX  15.3.4-beta
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxTemporalCalibrationWidget.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 =========================================================================*/
33 
34 
35 #include <QtWidgets>
36 
37 #include <QVBoxLayout>
38 #include "boost/bind.hpp"
39 #include "cxTrackingService.h"
41 #include <vtkDoubleArray.h>
42 #include <vtkPointData.h>
43 #include "cxDoubleWidgets.h"
44 #include "cxTypeConversions.h"
45 #include "cxRecordSessionWidget.h"
46 #include "cxSettings.h"
47 #include "cxToolProperty.h"
49 #include "cxUtilHelpers.h"
50 #include "cxVolumeHelpers.h"
51 #include "vtkImageCorrelation.h"
52 #include "cxLogger.h"
53 #include "cxPatientModelService.h"
54 #include "cxVisServices.h"
55 
56 typedef vtkSmartPointer<vtkImageCorrelation> vtkImageCorrelationPtr;
57 typedef vtkSmartPointer<vtkDoubleArray> vtkDoubleArrayPtr;
58 
59 
60 namespace cx
61 {
62 
63 typedef unsigned char uchar;
64 
65 
67  BaseWidget(parent, "TemporalCalibrationWidget", "Temporal Calibration"),
68  mInfoLabel(new QLabel(""))
69 {
70  mServices = services;
71 
72  mAlgorithm.reset(new TemporalCalibration);
73  connect(mServices->getPatientService().get(), SIGNAL(patientChanged()), this, SLOT(patientChangedSlot()));
74 
75 // connect(mAcquisition.get(), SIGNAL(ready(bool,QString)), mRecordSessionWidget, SLOT(setReady(bool,QString)));
76  connect(acquisitionService.get(), SIGNAL(saveDataCompleted(QString)), this, SLOT(selectData(QString)));
77 // mAcquisition->checkIfReadySlot();
78 
79  mRecordSessionWidget = new RecordSessionWidget(acquisitionService, this, "temporal_calib");
80 
81 // connect(mRecordSessionWidget, SIGNAL(newSession(QString)), mAcquisition.get(), SLOT(saveSession(QString)));
82 // connect(mRecordSessionWidget, SIGNAL(started(QString)), mAcquisition.get(), SLOT(startRecord(QString)));
83 // connect(mRecordSessionWidget, SIGNAL(stopped(bool)), mAcquisition.get(), SLOT(stopRecord(bool)));
84  mRecordSessionWidget->setDescriptionVisibility(false);
85 
86  QVBoxLayout* topLayout = new QVBoxLayout(this);
87 
88  // add recording widgets
89  QLabel* acqLabel = new QLabel("<b>Acquisition</b>");
90  topLayout->addWidget(acqLabel);
91  acqLabel->setToolTip(this->defaultWhatsThis());
92  topLayout->addWidget(mInfoLabel);
93  topLayout->addWidget(mRecordSessionWidget);
94  topLayout->addWidget(new LabeledComboBoxWidget(this, StringPropertyActiveProbeConfiguration::New(mServices->getToolManager())));
95  topLayout->addWidget(new SpinBoxGroupWidget(this, DoublePropertyTimeCalibration::New(mServices->getToolManager())));
96 
97  topLayout->addWidget(this->createHorizontalLine());
98 
99  // add calibration widgets
100  QLabel* calLabel = new QLabel("<b>Calibration</b>");
101  topLayout->addWidget(calLabel);
102  calLabel->setToolTip(this->defaultWhatsThis());
103 
104  mFileSelectWidget = new FileSelectWidget(this);
105  connect(mFileSelectWidget, SIGNAL(fileSelected(QString)), this, SLOT(selectData(QString)));
106  mFileSelectWidget->setNameFilter(QStringList() << "*.fts");
107  topLayout->addWidget(mFileSelectWidget);
108 
109  mVerbose = new QCheckBox("Save data to temporal_calib.txt");
110  topLayout->addWidget(mVerbose);
111 
112  QPushButton* calibrateButton = new QPushButton("Calibrate");
113  calibrateButton->setToolTip("Calculate the temporal shift for the selected acqusition."
114  "The shift is not applied in any way."
115  "<p><b>NB:</b>"
116  " The calculation takes a few seconds, and in this time the program will be unresponsive</p>");
117 
118  connect(calibrateButton, SIGNAL(clicked()), this, SLOT(calibrateSlot()));
119  topLayout->addWidget(calibrateButton);
120 
121  mResult = new QLineEdit;
122  mResult->setReadOnly(true);
123  topLayout->addWidget(mResult);
124 
125  topLayout->addStretch();
126 
127  this->patientChangedSlot();
128 }
129 
131 {}
132 
134 {
135  return "<html>"
136  "<h3>Temporal Calibration.</h3>"
137  "<p><i>Calibrate the time shift between the tracking system and the video acquisition source.</i></br>"
138  "<p>Part 1, Acqusition: Move the probe in a sinusoidal pattern up and down in a water tank or similar."
139  "The <i>first</i> image should be a typical image, as it is used to correlate against all the others."
140  "<p>Part 2, Calibration: Press calibrate to calculate the temporal shift for the selected acquisition."
141  "The shift is not applied in any way. Refer to the log folder for the calibration curves."
142  "<p><b>NB:</b> Previous temporal calibration is applied to probe frames during acqusition.</p>"
143  "</html>";
144 }
145 
147 {
148  mFileSelectWidget->refresh();
149 }
150 
151 void TemporalCalibrationWidget::patientChangedSlot()
152 {
153 // std::cout << "TemporalCalibrationWidget::patientChangedSlot() " << std::endl;
154  QString filename = mServices->getPatientService()->getActivePatientFolder() + "/US_Acq/";
155  mFileSelectWidget->setPath(filename);
156 // this->selectData(filename);
157 }
158 
159 void TemporalCalibrationWidget::selectData(QString filename)
160 {
161 // std::cout << "TemporalCalibrationWidget::selectData " << filename << std::endl;
162  mAlgorithm->selectData(filename);
163  mFileSelectWidget->setFilename(filename);
164  mResult->setText("");
165 }
166 
170 void TemporalCalibrationWidget::calibrateSlot()
171 {
172  if (mVerbose->isChecked())
173  mAlgorithm->setDebugFolder(mServices->getPatientService()->getActivePatientFolder()+"/Logs/");
174  else
175  mAlgorithm->setDebugFolder("");
176 
177  bool success = true;
178  double shift = mAlgorithm->calibrate(&success);
179  if (success)
180  {
181  reportSuccess(QString("Completed temporal calibration, found shift %1 ms").arg(shift,0,'f',0));
182  mResult->setText(QString("Shift = %1 ms").arg(shift, 0, 'f', 0));
183  }
184  else
185  {
186  reportError(QString("Temporal calibration failed"));
187  mResult->setText(QString("failed"));
188  }
189 }
190 
191 
192 }//namespace cx
193 
194 
void setNameFilter(QStringList filter)
vtkSmartPointer< vtkDoubleArray > vtkDoubleArrayPtr
void reportError(QString msg)
Definition: cxLogger.cpp:92
boost::shared_ptr< class AcquisitionService > AcquisitionServicePtr
boost::shared_ptr< class VisServices > VisServicesPtr
Definition: cxMainWindow.h:61
static DoublePropertyBasePtr New(TrackingServicePtr trackingService)
static StringPropertyActiveProbeConfigurationPtr New(TrackingServicePtr trackingService)
Composite widget for string selection.
virtual QString defaultWhatsThis() const
Returns a short description of what this widget will do for you.
static QFrame * createHorizontalLine()
Creates a horizontal line which can be inserted into widgets.
void setFilename(QString name)
void setPath(QString path)
void reportSuccess(QString msg)
Definition: cxLogger.cpp:93
cxLogicManager_EXPORT AcquisitionServicePtr acquisitionService()
Interface for QWidget which handles widgets uniformly for the system.
Definition: cxBaseWidget.h:108
Composite widget for scalar data manipulation.
unsigned char uchar
TemporalCalibrationWidget(VisServicesPtr services, AcquisitionServicePtr acquisitionService, QWidget *parent)
vtkSmartPointer< vtkImageCorrelation > vtkImageCorrelationPtr
Widget for displaying and selecting a single file.