Fraxinus  16.5.0-fx-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxBronchoscopyRegistrationWidget.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 <vtkPolyData.h>
35 #include "cxTransform3D.h"
36 #include "cxDataSelectWidget.h"
37 #include "cxTrackingService.h"
38 #include "cxMesh.h"
40 #include "cxRecordSessionWidget.h"
41 #include "cxRecordSession.h"
42 #include "cxView.h"
43 #include "cxToolRep3D.h"
44 #include "cxToolTracer.h"
46 #include "cxLogger.h"
47 #include "cxTypeConversions.h"
48 #include "cxPatientModelService.h"
49 #include "cxRegistrationService.h"
50 #include "cxViewService.h"
51 #include "cxStringProperty.h"
53 #include "cxTrackingService.h"
54 #include "cxDoubleProperty.h"
55 #include "cxProfile.h"
56 #include "cxHelperWidgets.h"
57 #include "cxBoolProperty.h"
58 #include "cxCheckBoxWidget.h"
59 #include "cxRepContainer.h"
61 #include "cxViewGroupData.h"
63 #include "cxHelperWidgets.h"
64 #include "cxAcquisitionService.h"
65 #include "cxRegServices.h"
66 #include "cxRecordTrackingWidget.h"
67 
68 
69 namespace cx
70 {
72  RegistrationBaseWidget(services, parent, "org_custusx_registration_method_bronchoscopy_widget",
73  "Bronchoscopy Registration"),
74  mBronchoscopyRegistration(new BronchoscopyRegistration()),
75  mServices(services),
76  mRecordTrackingWidget(NULL)
77 {
78  mVerticalLayout = new QVBoxLayout(this);
79 }
80 
82 {
83  if (!mRecordTrackingWidget)
84  {
85  this->setup();
86  }
87 }
88 
89 void BronchoscopyRegistrationWidget::setup()
90 {
91  mOptions = profile()->getXmlSettings().descend("bronchoscopyregistrationwidget");
92 
93  mSelectMeshWidget = StringPropertySelectMesh::New(mServices->patient());
94  mSelectMeshWidget->setValueName("Centerline: ");
95 
96  //this->initializeTrackingService();
97 
98  connect(mServices->patient().get(),&PatientModelService::patientChanged,this,&BronchoscopyRegistrationWidget::clearDataOnNewPatient);
99 
100  mProcessCenterlineButton = new QPushButton("Process centerline");
101  connect(mProcessCenterlineButton, SIGNAL(clicked()), this, SLOT(processCenterlineSlot()));
102  mProcessCenterlineButton->setToolTip(this->defaultWhatsThis());
103 
104 // mBronchoscopeRegistrationPtr = BronchoscopeRegistrationPtr(new BronchoscopePositionProjection());
105 // mProjectionCenterlinePtr->createMaxDistanceToCenterlineOption(mOptions.getElement());
106 
107  mRegisterButton = new QPushButton("Register");
108  connect(mRegisterButton, SIGNAL(clicked()), this, SLOT(registerSlot()));
109  mRegisterButton->setToolTip(this->defaultWhatsThis());
110 
111  mRecordTrackingWidget = new RecordTrackingWidget(mOptions.descend("recordTracker"),
112  mServices->acquisition(), mServices,
113  "bronc_path",
114  this);
115  mRecordTrackingWidget->getSessionSelector()->setHelp("Select bronchoscope path for registration");
116  mRecordTrackingWidget->getSessionSelector()->setDisplayName("Bronchoscope path");
117 
118  mVerticalLayout->setMargin(0);
119  mVerticalLayout->addWidget(new DataSelectWidget(mServices->view(), mServices->patient(), this, mSelectMeshWidget));
120 
121  this->selectSubsetOfBranches(mOptions.getElement());
122  this->createMaxNumberOfGenerations(mOptions.getElement());
123  this->useLocalRegistration(mOptions.getElement());
124  this->createMaxLocalRegistrationDistance(mOptions.getElement());
125 
126 // PropertyPtr maxLocalRegistrationDistance = mProjectionCenterlinePtr->getMaxLocalRegistrationDistanceOption();
127 
128  mVerticalLayout->addWidget(new CheckBoxWidget(this, mUseSubsetOfGenerations));
129  mVerticalLayout->addWidget(createDataWidget(mServices->view(), mServices->patient(), this, mMaxNumberOfGenerations));
130  mVerticalLayout->addWidget(mProcessCenterlineButton);
131  mVerticalLayout->addWidget(mRecordTrackingWidget);
132  mVerticalLayout->addWidget(new CheckBoxWidget(this, mUseLocalRegistration));
133  mVerticalLayout->addWidget(createDataWidget(mServices->view(), mServices->patient(), this, mMaxLocalRegistrationDistance));
134  mVerticalLayout->addWidget(mRegisterButton);
135 
136  mVerticalLayout->addStretch();
137 }
138 
140 {
141  return QString();
142 }
143 
144 void BronchoscopyRegistrationWidget::initializeTrackingService()
145 {
146  if(mServices->tracking()->getState() < Tool::tsCONFIGURED)
147  mServices->tracking()->setState(Tool::tsCONFIGURED);
148 }
149 
150 void BronchoscopyRegistrationWidget::processCenterlineSlot()
151 {
152  this->initializeTrackingService();
153 
154  if(!mSelectMeshWidget->getMesh())
155  {
156  reportError("No centerline");
157  return;
158  }
159  vtkPolyDataPtr centerline = mSelectMeshWidget->getMesh()->getVtkPolyData();//input
160  Transform3D rMd = mSelectMeshWidget->getMesh()->get_rMd();
161  vtkPolyDataPtr processedCenterline;
162  if (mUseSubsetOfGenerations->getValue())
163  processedCenterline = mBronchoscopyRegistration->processCenterline(centerline, rMd, mMaxNumberOfGenerations->getValue());
164  else
165  processedCenterline = mBronchoscopyRegistration->processCenterline(centerline, rMd);
166 
167  if (!mMesh)
168  {
169  QString uid = mSelectMeshWidget->getMesh()->getUid() + "_cl%1";
170  QString name = mSelectMeshWidget->getMesh()->getName()+" cl_processed%1";
171  std::cout << "name: " << name << std::endl;
172  std::cout << "uid: " << uid << std::endl;
173  mMesh = mServices->patient()->createSpecificData<Mesh>(uid, name);
174  }
175  mMesh->setVtkPolyData(processedCenterline);
176  mMesh->setColor(QColor(0, 0, 255, 255));
177  mServices->patient()->insertData(mMesh);
178  mServices->view()->autoShowData(mMesh);
179 }
180 
181 void BronchoscopyRegistrationWidget::registerSlot()
182 {
183  if(!mBronchoscopyRegistration->isCenterlineProcessed())
184  {
185  reportError("Centerline not processed");
186  return;
187  }
188 
189  Transform3D old_rMpr = mServices->patient()->get_rMpr();//input to registrationAlgorithm
190  //std::cout << "rMpr: " << std::endl;
191  //std::cout << old_rMpr << std::endl;
192 
193  TimedTransformMap trackerRecordedData_prMt = mRecordTrackingWidget->getRecordedTrackerData_prMt();
194 
195  if(trackerRecordedData_prMt.empty())
196  {
197  reportError("No positions");
198  return;
199  }
200 
201  Transform3D new_rMpr;
202 
203  if(mUseLocalRegistration->getValue()){
204  std::cout << "Running local registration with max distance " << mMaxLocalRegistrationDistance->getValue() << " mm." << std::endl;
205  new_rMpr = Transform3D(mBronchoscopyRegistration->runBronchoscopyRegistration(trackerRecordedData_prMt,old_rMpr,mMaxLocalRegistrationDistance->getValue()));
206  }
207  else{
208  std::cout << "Running global registration." << std::endl;
209  new_rMpr = Transform3D(mBronchoscopyRegistration->runBronchoscopyRegistration(trackerRecordedData_prMt,old_rMpr,0));
210  }
211 
212  new_rMpr = new_rMpr*old_rMpr;//output
213  mServices->registration()->applyPatientRegistration(new_rMpr, "Bronchoscopy centerline to tracking data");
214 
215  Eigen::Matrix4d display_rMpr = Eigen::Matrix4d::Identity();
216  display_rMpr = new_rMpr*display_rMpr;
217  std::cout << "New prMt: " << std::endl;
218  for (int i = 0; i < 4; i++)
219  std::cout << display_rMpr.row(i) << std::endl;
220 
221 // mRecordTrackingWidget->showSelectedRecordingInView();
222 
223 }
224 
225 void BronchoscopyRegistrationWidget::createMaxNumberOfGenerations(QDomElement root)
226 {
227  mMaxNumberOfGenerations = DoubleProperty::initialize("Max number of generations in centerline", "",
228  "Set max number of generations centerline", 4, DoubleRange(0, 10, 1), 0,
229  root);
230  mMaxNumberOfGenerations->setGuiRepresentation(DoublePropertyBase::grSLIDER);
231 }
232 
233 void BronchoscopyRegistrationWidget::selectSubsetOfBranches(QDomElement root)
234 {
235  mUseSubsetOfGenerations = BoolProperty::initialize("Select branch generations to be used in registration", "",
236  "Select branch generations to be used in registration", false,
237  root);
238 }
239 
240 void BronchoscopyRegistrationWidget::useLocalRegistration(QDomElement root)
241 {
242  mUseLocalRegistration = BoolProperty::initialize("Use local registration", "",
243  "Use local registration", false,
244  root);
245 }
246 
247 void BronchoscopyRegistrationWidget::createMaxLocalRegistrationDistance(QDomElement root)
248 {
249  mMaxLocalRegistrationDistance = DoubleProperty::initialize("Max local registration distance (mm)", "",
250  "Set max distance for local registration in mm", 30, DoubleRange(1, 200, 1), 0,
251  root);
252  mMaxLocalRegistrationDistance->setGuiRepresentation(DoubleProperty::grSLIDER);
253 }
254 
255 void BronchoscopyRegistrationWidget::clearDataOnNewPatient()
256 {
257  mMesh.reset();
258 }
259 } //namespace cx
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:176
static BoolPropertyPtr initialize(const QString &uid, QString name, QString help, bool value, QDomNode root=QDomNode())
void reportError(QString msg)
Definition: cxLogger.cpp:92
boost::shared_ptr< class RegServices > RegServicesPtr
Definition: cxRegServices.h:41
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
Widget for the BoolPropertyBase.
TimedTransformMap getRecordedTrackerData_prMt()
StringPropertyPtr getSessionSelector()
QDomElement getElement()
return the current element
configured with basic info
Definition: cxTool.h:96
QWidget * createDataWidget(ViewServicePtr viewService, PatientModelServicePtr patientModelService, QWidget *parent, PropertyPtr data, QGridLayout *gridLayout, int row)
Create a widget capable of displaying the input data.
BronchoscopyRegistrationWidget(RegServicesPtr services, QWidget *parent)
vtkSmartPointer< class vtkPolyData > vtkPolyDataPtr
static DoublePropertyPtr initialize(const QString &uid, QString name, QString help, double value, DoubleRange range, int decimals, QDomNode root=QDomNode())
static StringPropertySelectMeshPtr New(PatientModelServicePtr patientModelService)
std::map< double, Transform3D > TimedTransformMap
XmlOptionFile descend(QString element) const
step one level down in the xml tree