CustusX  15.3.3-beta
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"
41 #include "cxRecordSessionWidget.h"
42 #include "cxRecordSession.h"
43 #include "cxView.h"
44 #include "cxToolRep3D.h"
45 #include "cxToolTracer.h"
47 #include "cxLogger.h"
48 #include "cxTypeConversions.h"
49 #include "cxPatientModelService.h"
50 #include "cxRegistrationService.h"
51 #include "cxViewService.h"
52 #include "cxStringProperty.h"
54 #include "cxTrackingService.h"
55 #include "cxDoubleProperty.h"
56 #include "cxProfile.h"
57 #include "cxHelperWidgets.h"
58 #include "cxBoolProperty.h"
59 #include "cxCheckBoxWidget.h"
60 #include "cxRepContainer.h"
62 #include "cxViewGroupData.h"
64 #include "cxHelperWidgets.h"
65 #include "cxAcquisitionService.h"
66 #include "cxRegServices.h"
67 
68 
69 namespace cx
70 {
72  RegistrationBaseWidget(services, parent, "BronchoscopyRegistrationWidget",
73  "Bronchoscopy Registration"),
74  mBronchoscopyRegistration(new BronchoscopyRegistration()),
75  mServices(services)
76 {
77  mVerticalLayout = new QVBoxLayout(this);
78  mOptions = profile()->getXmlSettings().descend("bronchoscopyregistrationwidget");
79 
80  mSelectMeshWidget = StringPropertySelectMesh::New(mServices.patientModelService);
81  mSelectMeshWidget->setValueName("Centerline: ");
82 
83  mSelectToolWidget = StringPropertySelectTool::New(services.getToolManager());
84  //this->initializeTrackingService();
85 
86  mProcessCenterlineButton = new QPushButton("Process centerline");
87  connect(mProcessCenterlineButton, SIGNAL(clicked()), this, SLOT(processCenterlineSlot()));
88  mProcessCenterlineButton->setToolTip(this->defaultWhatsThis());
89 
90  mRegisterButton = new QPushButton("Register");
91  connect(mRegisterButton, SIGNAL(clicked()), this, SLOT(registerSlot()));
92  mRegisterButton->setToolTip(this->defaultWhatsThis());
93 
94  this->initSessionSelector();
95 
96 // mTrackedCenterLine = new TrackedCenterlineWidget(mAcquisitionData, this);
97 
98  mRecordSessionWidget.reset(new RecordSessionWidget(services.acquisitionService, this, "Bronchoscope path", false));
99 
100  mVerticalLayout->setMargin(0);
101  mVerticalLayout->addWidget(new DataSelectWidget(mServices.visualizationService, mServices.patientModelService, this, mSelectMeshWidget));
102 // mVerticalLayout->addWidget(mTrackedCenterLine);
103 
104  this->selectSubsetOfBranches(mOptions.getElement());
105  this->createMaxNumberOfGenerations(mOptions.getElement());
106  this->useLocalRegistration(mOptions.getElement());
107 
108 // QHBoxLayout* activeToolLayout = new QHBoxLayout;
109 // activeToolLayout->addWidget(new QLabel("Name:", this));
110 // mToolNameLabel = new QLabel(this);
111 // activeToolLayout->addWidget(mToolNameLabel);
112 // mActiveToolVisibleLabel = new QLabel("Visible: NA");
113 // activeToolLayout->addWidget(mActiveToolVisibleLabel);
114 // activeGroupLayout->addLayout(activeToolLayout);
115 
116  mVerticalLayout->addWidget(new CheckBoxWidget(this, mUseSubsetOfGenerations));
117  mVerticalLayout->addWidget(createDataWidget(mServices.visualizationService, mServices.patientModelService, this, mMaxNumberOfGenerations));
118  mVerticalLayout->addWidget(mProcessCenterlineButton);
119  mVerticalLayout->addWidget(sscCreateDataWidget(this, mSelectToolWidget));
120  mVerticalLayout->addWidget(mRecordSessionWidget.get());
121  mVerticalLayout->addWidget(new LabeledComboBoxWidget(this, mSessionSelector));
122  mVerticalLayout->addWidget(new CheckBoxWidget(this, mUseLocalRegistration));
123  mVerticalLayout->addWidget(mRegisterButton);
124 
125  mVerticalLayout->addStretch();
126 
127  mObscuredListener.reset(new WidgetObscuredListener(this));
128  connect(mObscuredListener.get(), SIGNAL(obscured(bool)), this, SLOT(obscuredSlot(bool)));
129 
130 }
131 
132 void BronchoscopyRegistrationWidget::initSessionSelector()
133 {
134  QStringList sessionUids = getSessionList();
135  QString defaultValue;
136  if(!sessionUids.isEmpty())
137  defaultValue = sessionUids.last();
138  mSessionSelector = StringProperty::initialize("bronchoscopy_session", "Bronchoscope path", "Select bronchoscope path for registration", defaultValue, sessionUids, QDomNode());
139 
140  //TODO: Let mSessionSelector display displaynames instead of uids (StringDataAdapterXml::setDisplayNames)
141 }
142 
143 QStringList BronchoscopyRegistrationWidget::getSessionList()
144 {
145  std::vector<RecordSessionPtr> sessions = mServices.acquisitionService->getSessions();
146  std::vector<RecordSessionPtr>::iterator it = sessions.begin();
147  QStringList sessionUids;
148 
149  for(; it != sessions.end(); ++it)
150  {
151  QString uid = (*it)->getUid();
152  sessionUids << uid;
153  }
154 
155  sessionUids.sort();
156  return sessionUids;
157 }
158 
160 {
161  return QString();
162 }
163 
164 void BronchoscopyRegistrationWidget::initializeTrackingService()
165 {
166  if(mServices.trackingService->getState() < Tool::tsCONFIGURED)
167  mServices.trackingService->setState(Tool::tsCONFIGURED);
168 }
169 
170 void BronchoscopyRegistrationWidget::processCenterlineSlot()
171 {
172  this->initializeTrackingService();
173 
174  if(!mSelectMeshWidget->getMesh())
175  {
176  reportError("No centerline");
177  return;
178  }
179  vtkPolyDataPtr centerline = mSelectMeshWidget->getMesh()->getVtkPolyData();//input
180  Transform3D rMd = mSelectMeshWidget->getMesh()->get_rMd();
181  vtkPolyDataPtr processedCenterline;
182  if (mUseSubsetOfGenerations->getValue())
183  processedCenterline = mBronchoscopyRegistration->processCenterline(centerline, rMd, mMaxNumberOfGenerations->getValue());
184  else
185  processedCenterline = mBronchoscopyRegistration->processCenterline(centerline, rMd);
186 
187  if (!mMesh)
188  {
189  QString uid = mSelectMeshWidget->getMesh()->getUid() + "_cl%1";
190  QString name = mSelectMeshWidget->getMesh()->getName()+" cl_processed%1";
191  mMesh = mServices.patientModelService->createSpecificData<Mesh>(uid, name);
192  }
193  mMesh->setVtkPolyData(processedCenterline);
194  mMesh->setColor(QColor(0, 0, 255, 255));
195  mServices.patientModelService->insertData(mMesh);
196  mServices.visualizationService->autoShowData(mMesh);
197 }
198 
199 void BronchoscopyRegistrationWidget::registerSlot()
200 {
201  if(!mBronchoscopyRegistration->isCenterlineProcessed())
202  {
203  reportError("Centerline not processed");
204  return;
205  }
206 
207  Transform3D old_rMpr = mServices.patientModelService->get_rMpr();//input to registrationAlgorithm
208  //std::cout << "rMpr: " << std::endl;
209  //std::cout << old_rMpr << std::endl;
210 
211  mTool = mSelectToolWidget->getTool();
212  if(!mTool)
213  mTool = mServices.trackingService->getActiveTool();
214 
215  if(!mTool)
216  {
217  reportError("No tool");
218  return;
219  }
220  std::cout << "Tool name: " << mTool->getName() << std::endl;
221 
222  RecordSessionPtr session;
223  QString sessionUid = mSessionSelector->getValue();
224  if(!sessionUid.isEmpty())
225  session = mServices.acquisitionService->getSession(sessionUid);
226 // else
227 // session = mAcquisition->getLatestSession();
228 
229  if(!session)
230  reportError("No session");
231 
232  TimedTransformMap trackerRecordedData_prMt = RecordSession::getToolHistory_prMt(mTool, session);//input
233 // TimedTransformMap trackerRecordedData = mTrackedCenterLine->getRecording();
234 
235  if(trackerRecordedData_prMt.size() == 0)
236  {
237  reportError("No positions");
238  return;
239  }
240 
241  Transform3D new_rMpr;
242  double maxDistanceForLocalRegistration = 30; //mm
243  if(mUseLocalRegistration->getValue())
244  new_rMpr = Transform3D(mBronchoscopyRegistration->runBronchoscopyRegistration(trackerRecordedData_prMt,old_rMpr,maxDistanceForLocalRegistration));
245  else
246  new_rMpr = Transform3D(mBronchoscopyRegistration->runBronchoscopyRegistration(trackerRecordedData_prMt,old_rMpr,0));
247 
248  new_rMpr = new_rMpr*old_rMpr;//output
249  mServices.registrationService->applyPatientRegistration(new_rMpr, "Bronchoscopy centerline to tracking data");
250 
251  Eigen::Matrix4d display_rMpr = Eigen::Matrix4d::Identity();
252  display_rMpr = new_rMpr*display_rMpr;
253  std::cout << "New prMt: " << std::endl;
254  for (int i = 0; i < 4; i++)
255  std::cout << display_rMpr.row(i) << std::endl;
256 
257  ToolRep3DPtr activeRep3D = getToolRepIn3DView(mTool);
258  if(activeRep3D && activeRep3D->getTracer())
259  activeRep3D->getTracer()->clear();
260 
261  QColor colorGreen = QColor(0, 255, 0, 255);
262  activeRep3D->getTracer()->setColor(colorGreen);
263  activeRep3D->getTracer()->addManyPositions(trackerRecordedData_prMt);
264 
265 
266 }
267 
268 void BronchoscopyRegistrationWidget::acquisitionStarted()
269 {
270  std::cout << "acquisitionStarted" << std::endl;
271 
272  mTool = mSelectToolWidget->getTool();
273  if(!mTool)
274  mTool = mServices.trackingService->getActiveTool();
275 
276  ToolRep3DPtr activeRep3D = this->getToolRepIn3DView(mTool);
277  if (!activeRep3D)
278  return;
279 
280  activeRep3D->getTracer()->start();
281 }
282 void BronchoscopyRegistrationWidget::acquisitionStopped()
283 {
284  std::cout << "acquisitionStopped" << std::endl;
285 
286  this->acquisitionCancelled();
287  QString newUid = mServices.acquisitionService->getLatestSession()->getUid();
288  QStringList range = mSessionSelector->getValueRange();
289  range << newUid;
290  mSessionSelector->setValueRange(range);
291  mSessionSelector->setValue(newUid);
292 
293  mServices.patientModelService->autoSave();
294 
295 // this->saveSessions();
296 }
297 
298 void BronchoscopyRegistrationWidget::acquisitionCancelled()
299 {
300  ToolRep3DPtr activeRep3D = this->getToolRepIn3DView(mTool);
301  if (!activeRep3D)
302  return;
303 
304  activeRep3D->getTracer()->stop();
305 }
306 
307 void BronchoscopyRegistrationWidget::recordedSessionsChanged()
308 {
309  QStringList sessionUids = getSessionList();
310  mSessionSelector->setValueRange(sessionUids);
311  if(mSessionSelector->getValue().isEmpty() && !sessionUids.isEmpty())
312  mSessionSelector->setValue(sessionUids.last());
313 }
314 
315 ToolRep3DPtr BronchoscopyRegistrationWidget::getToolRepIn3DView(ToolPtr tool)
316 {
317  RepContainerPtr repContainer = mServices.visualizationService->get3DReps(0, 0);
318  if (repContainer)
319  return repContainer->findFirst<ToolRep3D>(tool);
320  else
321  return ToolRep3DPtr();
322 }
323 
324 void BronchoscopyRegistrationWidget::obscuredSlot(bool obscured)
325 {
326 // std::cout << "obscuredSlot: " << obscured << std::endl;
327 
328  if (!obscured)
329  {
330  connect(mServices.acquisitionService.get(), &AcquisitionService::started, this, &BronchoscopyRegistrationWidget::acquisitionStarted);
331  connect(mServices.acquisitionService.get(), &AcquisitionService::acquisitionStopped, this, &BronchoscopyRegistrationWidget::acquisitionStopped, Qt::QueuedConnection);
332  connect(mServices.acquisitionService.get(), &AcquisitionService::cancelled, this, &BronchoscopyRegistrationWidget::acquisitionCancelled);
333  connect(mServices.acquisitionService.get(), &AcquisitionService::recordedSessionsChanged, this, &BronchoscopyRegistrationWidget::recordedSessionsChanged);
334  return;
335  }
336 
337  disconnect(mServices.acquisitionService.get(), &AcquisitionService::started, this, &BronchoscopyRegistrationWidget::acquisitionStarted);
338  disconnect(mServices.acquisitionService.get(), &AcquisitionService::acquisitionStopped, this, &BronchoscopyRegistrationWidget::acquisitionStopped);
339  disconnect(mServices.acquisitionService.get(), &AcquisitionService::cancelled, this, &BronchoscopyRegistrationWidget::acquisitionCancelled);
340  disconnect(mServices.acquisitionService.get(), &AcquisitionService::recordedSessionsChanged, this, &BronchoscopyRegistrationWidget::recordedSessionsChanged);
341 
342  ToolRep3DPtr activeRep3D = this->getToolRepIn3DView(mTool);
343  if (!activeRep3D)
344  return;
345  //std::cout << "Slot is cleared" << std::endl;
346  activeRep3D->getTracer()->clear();
347 }
348 
349 void BronchoscopyRegistrationWidget::createMaxNumberOfGenerations(QDomElement root)
350 {
351  mMaxNumberOfGenerations = DoubleProperty::initialize("Max number of generations in centerline", "",
352  "Set max number of generations centerline", 4, DoubleRange(0, 10, 1), 0,
353  root);
354  mMaxNumberOfGenerations->setGuiRepresentation(DoublePropertyBase::grSLIDER);
355 }
356 
357 void BronchoscopyRegistrationWidget::selectSubsetOfBranches(QDomElement root)
358 {
359  mUseSubsetOfGenerations = BoolProperty::initialize("Select branch generations to be used in registration", "",
360  "Select branch generations to be used in registration", false,
361  root);
362 }
363 
364 void BronchoscopyRegistrationWidget::useLocalRegistration(QDomElement root)
365 {
366  mUseLocalRegistration = BoolProperty::initialize("Use local registration", "",
367  "Use local registration", false,
368  root);
369 }
370 
371 } //namespace cx
AcquisitionServicePtr acquisitionService
Definition: cxRegServices.h:62
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:142
static BoolPropertyPtr initialize(const QString &uid, QString name, QString help, bool value, QDomNode root=QDomNode())
void reportError(QString msg)
Definition: cxLogger.cpp:92
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
Widget for the BoolPropertyBase.
QWidget * createDataWidget(VisualizationServicePtr visualizationService, PatientModelServicePtr patientModelService, QWidget *parent, PropertyPtr data, QGridLayout *gridLayout, int row)
Create a widget capable of displaying the input data.
TrackingServicePtr getToolManager()
Composite widget for string selection.
static TimedTransformMap getToolHistory_prMt(ToolPtr tool, RecordSessionPtr session)
QDomElement getElement()
return the current element
vtkSmartPointer< class vtkPolyData > vtkPolyDataPtr
Definition: cxProbeSector.h:47
configured with basic info
Definition: cxTool.h:81
BronchoscopyRegistrationWidget(RegServices services, QWidget *parent)
boost::shared_ptr< class RepContainer > RepContainerPtr
boost::shared_ptr< class RecordSession > RecordSessionPtr
virtual QString defaultWhatsThis() const
Returns a short description of what this widget will do for you.
static StringPropertyPtr initialize(const QString &uid, QString name, QString help, QString value, QStringList range, QDomNode root=QDomNode())
VisualizationServicePtr visualizationService
Definition: cxVisServices.h:60
static StringPropertySelectToolPtr New(TrackingServicePtr trackingService)
TrackingServicePtr trackingService
static DoublePropertyPtr initialize(const QString &uid, QString name, QString help, double value, DoubleRange range, int decimals, QDomNode root=QDomNode())
QWidget * sscCreateDataWidget(QWidget *parent, PropertyPtr data, QGridLayout *gridLayout, int row)
Create a widget capable of displaying the input data.
static StringPropertySelectMeshPtr New(PatientModelServicePtr patientModelService)
boost::shared_ptr< class ToolRep3D > ToolRep3DPtr
PatientModelServicePtr patientModelService
std::map< double, Transform3D > TimedTransformMap
RegistrationServicePtr registrationService
Definition: cxRegServices.h:60
boost::shared_ptr< class Tool > ToolPtr