CustusX  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxICPRegistrationBaseWidget.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 #include <QPushButton>
35 #include <QLabel>
36 #include <QSpinBox>
37 #include <QCheckBox>
38 #include <QGroupBox>
39 #include <vtkCellArray.h>
40 #include "cxTypeConversions.h"
41 #include "cxLogger.h"
42 #include "cxTimedAlgorithm.h"
46 #include "cxMesh.h"
47 #include "cxView.h"
48 #include "cxGeometricRep.h"
49 #include "cxGraphicalPrimitives.h"
50 #include "cxRegistrationService.h"
51 #include "cxViewService.h"
52 #include "cxPatientModelService.h"
53 #include "cxICPWidget.h"
54 #include "cxMeshInView.h"
56 #include "cxSpaceProvider.h"
57 #include "cxSpaceListener.h"
58 #include "cxProfile.h"
59 
60 namespace cx
61 {
62 
63 ICPRegistrationBaseWidget::ICPRegistrationBaseWidget(RegServicesPtr services, QWidget* parent, QString uid, QString name) :
64  RegistrationBaseWidget(services, parent, uid, name)
65 {
66  this->setLayout(new QVBoxLayout); // we need something, otherwise the widget might not be painted at all.
67 }
68 
70 {
71 }
72 
74 {
75  if (!mRegistrator)
76  {
77  this->initialize();
78  this->setup();
79  }
80 
81  this->initializeRegistrator();
82 
84  this->updateDifferenceLines();
85  mICPWidget->setRMS(mRegistrator->getResultMetric());
86 }
87 
88 void ICPRegistrationBaseWidget::initialize()
89 {
90  delete this->layout(); // dummy layout from construct
91 
92  mRegistrator.reset(new SeansVesselReg());
93  mOptions = profile()->getXmlSettings().descend("uid");
94 
95  this->initializeProperties();
96 
97  mICPWidget = new ICPWidget(this);
98  mICPWidget->setSettings(this->getAllProperties());
100 
101  mObscuredListener.reset(new WidgetObscuredListener(this));
102  connect(mObscuredListener.get(), SIGNAL(obscured(bool)), this, SLOT(obscuredSlot(bool)));
103 }
104 
105 
106 void ICPRegistrationBaseWidget::initializeProperties()
107 {
108  mAutoLTS = BoolProperty::initialize("autoLTS","Auto LTS",
109  "Ignore LTS, instead attempt to find optimal value",
112 
113  mLTSRatio = DoubleProperty::initialize("LTSRatio", "LTS Ratio",
114  "Fraction of points in the lesser point set to use during each iteration.",
115  80, DoubleRange(20,100,1), 0, mOptions.getElement());
117 
118  mNumberOfIterations = DoubleProperty::initialize("Iterations", "Iterations",
119  "Number of iterations",
120  500, DoubleRange(1,1000,1), 0, mOptions.getElement());
121  connect(mNumberOfIterations.get(), &DoubleProperty::changed, this, &ICPRegistrationBaseWidget::onSettingsChanged);
122 
123  mStopThreshold = DoubleProperty::initialize("StopThreshold", "Stop Threshold",
124  "Differential RMS stop threshold, between iterations",
125  0.001, DoubleRange(0.0001,1,0.0001), 4, mOptions.getElement());
126  connect(mStopThreshold.get(), &DoubleProperty::changed, this, &ICPRegistrationBaseWidget::onSettingsChanged);
127 
128  mMaxTime = DoubleProperty::initialize("MaxTime", "Max Time",
129  "Maximum time spent iterating, in seconds",
130  60, DoubleRange(1,300,1), 0, mOptions.getElement());
132 
133  mMargin = DoubleProperty::initialize("Margin", "Margin",
134  "Data outside a bounding box defined by the intersection\n"
135  "of the fixed/moving by a margin are cropped.",
136  40, DoubleRange(10,100,1), 0, mOptions.getElement());
138 
139  mLinear = BoolProperty::initialize("linear","Linear",
140  "Use only linear iteration",
141  true, mOptions.getElement());
143 
144  mDisplayProgress = BoolProperty::initialize("progress","Display Progress",
145  "Display metric and difference lines between point sets",
146  true, mOptions.getElement());
147  connect(mDisplayProgress.get(), &DoubleProperty::changed, this, &ICPRegistrationBaseWidget::onDisplayProgressChanged);
148 
149  mOneStep = BoolProperty::initialize("onestep","One Step",
150  "Registration is done one iteration at a time.",
151  false); // note: do not store the value in this case: always reset to default.
152 }
153 
154 std::vector<PropertyPtr> ICPRegistrationBaseWidget::getAllProperties()
155 {
156  std::vector<PropertyPtr> properties;
157  properties.push_back(mLTSRatio);
158  properties.push_back(mAutoLTS);
159  properties.push_back(mMargin);
160  properties.push_back(mLinear);
161  properties.push_back(mDisplayProgress);
162  properties.push_back(mOneStep);
163  properties.push_back(mNumberOfIterations);
164  properties.push_back(mStopThreshold);
165  properties.push_back(mMaxTime);
166  return properties;
167 }
168 
169 void ICPRegistrationBaseWidget::obscuredSlot(bool obscured)
170 {
171  if (obscured)
172  {
173  mMeshInView.reset();
174  }
175  else
176  {
177  this->onSettingsChanged();
178  this->onShown();
179  }
180 }
181 
183 {
184  if (mObscuredListener->isObscured())
185  return;
186 
187  this->setModified();
188 // this->initializeRegistrator();
189 
190 // mICPWidget->enableRegistration(mRegistrator->isValid());
191 // this->updateDifferenceLines();
192 // mICPWidget->setRMS(mRegistrator->getResultMetric());
193 }
194 
196 {
197  if (mObscuredListener->isObscured())
198  return;
199  mRegistrator->mt_auto_lts = mAutoLTS->getValue();
200  mRegistrator->mt_ltsRatio = mLTSRatio->getValue();
201  mRegistrator->mt_doOnlyLinear = mLinear->getValue();
202  mRegistrator->margin = mMargin->getValue();
203  mRegistrator->mt_maximumNumberOfIterations = mNumberOfIterations->getValue();
204  mRegistrator->mt_distanceDeltaStopThreshold = mStopThreshold->getValue();
205  mRegistrator->mt_maximumDurationSeconds = mMaxTime->getValue();
206 }
207 
209 {
210 // int lts_ratio = mLTSRatioSpinBox->value();
211 // double stop_delta = 0.001; //TODO, add user interface
212 // double lambda = 0; //TODO, add user interface
213 // double sigma = 1.0; //TODO, add user interface
214 // bool lin_flag = mLinearCheckBox->isChecked(); //TODO, add user interface
215 // int sample = 1; //TODO, add user interface
216 // int single_point_thre = 1; //TODO, add user interface
217 // bool verbose = 1; //TODO, add user interface
218 
219  mRegistrator->notifyPreRegistrationWarnings();
220 
221  if (mRegistrator->mt_auto_lts)
222  {
223  reportDebug("Using automatic lts_ratio");
224  }
225  else
226  {
227  reportDebug("Using lts_ratio: " + qstring_cast(mRegistrator->mt_ltsRatio));
228  }
229 
230  bool success = false;
231  if (mOneStep->getValue())
232  success = mRegistrator->performOneRegistration();
233  else
234  success = mRegistrator->execute();
235 
236  if (!success)
237  {
238  reportWarning("ICP registration failed.");
239  return;
240  }
241 
242  Transform3D linearTransform = mRegistrator->getLinearResult();
243 
244  std::cout << "v2v linear result:\n" << linearTransform << std::endl;
245  //std::cout << "v2v inverted linear result:\n" << linearTransform.inverse() << std::endl;
246 
247  if ((boost::math::isnan)(linearTransform(0,0)))
248  {
249  reportWarning("ICP registration failed.");
250  return;
251  }
252 
253  mRegistrator->checkQuality(linearTransform);
254 
255  // The registration is performed in space r. Thus, given an old data position rMd, we find the
256  // new one as rM'd = Q * rMd, where Q is the inverted registration output.
257  // Delta is thus equal to Q:
258  Transform3D delta = linearTransform.inv();
259  //std::cout << "delta:\n" << delta << std::endl;
260 
261  this->applyRegistration(delta);
262 }
263 
264 
265 void ICPRegistrationBaseWidget::updateDifferenceLines()
266 {
267  if (!mMeshInView)
268  mMeshInView.reset(new MeshInView(mServices->view()));
269 
270  bool show = mDisplayProgress->getValue() && mRegistrator->isValid();
271  if (show)
272  mMeshInView->show(mRegistrator->getDifferenceLines());
273  else
274  mMeshInView->hide();
275 }
276 
277 void ICPRegistrationBaseWidget::onDisplayProgressChanged()
278 {
279  this->updateDifferenceLines();
280 }
281 
282 
283 } // namespace cx
QString qstring_cast(const T &val)
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:176
static BoolPropertyPtr initialize(const QString &uid, QString name, QString help, bool value, QDomNode root=QDomNode())
boost::shared_ptr< class RegServices > RegServicesPtr
Definition: cxRegServices.h:41
void setSettings(std::vector< PropertyPtr > properties)
Definition: cxICPWidget.cpp:96
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
virtual void initializeRegistrator()=0
QDomElement getElement()
return the current element
void reportWarning(QString msg)
Definition: cxLogger.cpp:91
void requestRegister()
boost::shared_ptr< class WidgetObscuredListener > mObscuredListener
virtual void applyRegistration(Transform3D delta)=0
ICPRegistrationBaseWidget(RegServicesPtr services, QWidget *parent, QString uid, QString name)
void changed()
emit when the underlying data value is changed: The user interface will be updated.
void setRMS(double val)
void enableRegistration(bool on)
static DoublePropertyPtr initialize(const QString &uid, QString name, QString help, double value, DoubleRange range, int decimals, QDomNode root=QDomNode())
void reportDebug(QString msg)
Definition: cxLogger.cpp:89