NorMIT-nav  22.09
An IGT application
cxCenterlinePointsWidget.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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
13 
14 #include <sstream>
15 #include <QVBoxLayout>
16 #include <QPushButton>
17 #include <QTableWidget>
18 #include <QTableWidgetItem>
19 #include <QHeaderView>
20 #include <QLabel>
21 #include <QSlider>
22 #include <vtkDoubleArray.h>
23 #include <vtkImageData.h>
24 #include "cxLogger.h"
25 #include "cxPickerRep.h"
27 #include "cxSettings.h"
28 #include "cxLandmarkRep.h"
29 #include "cxView.h"
30 #include "cxTypeConversions.h"
32 #include "cxRegistrationService.h"
33 #include "cxPatientModelService.h"
34 #include "cxViewService.h"
35 #include "cxRepContainer.h"
36 #include "cxTrackingService.h"
37 #include "cxLandmarkListener.h"
38 #include "cxActiveData.h"
39 #include "cxMesh.h"
40 #include "cxTime.h"
41 
42 namespace cx
43 {
44 CenterlinePointsWidget::CenterlinePointsWidget(RegServicesPtr services, QWidget* parent,
45  QString objectName, QString windowTitle, bool useRegistrationFixedPropertyInsteadOfActiveImage) :
46  LandmarkRegistrationWidget(services, parent, objectName, windowTitle, false),
47  mUseRegistrationFixedPropertyInsteadOfActiveImage(useRegistrationFixedPropertyInsteadOfActiveImage)
48 {
49  if(mUseRegistrationFixedPropertyInsteadOfActiveImage)
50  mCurrentProperty.reset(new StringPropertyRegistrationFixedImage(services->registration(), services->patient()));
51  else
54 
55  mLandmarkListener->useOnlyOneSourceUpdatedFromOutside();
56 
57  mActiveToolProxy = ActiveToolProxy::New(services->tracking());
58  connect(mActiveToolProxy.get(), SIGNAL(toolVisible(bool)), this, SLOT(enableButtons()));
59  connect(mActiveToolProxy.get(), SIGNAL(activeToolChanged(const QString&)), this, SLOT(enableButtons()));
60 
61  //pushbuttons
62  mAddLandmarkButton = new QPushButton("New Landmark", this);
63  mAddLandmarkButton->setToolTip("Add landmark");
64  mAddLandmarkButton->setDisabled(true);
65  connect(mAddLandmarkButton, SIGNAL(clicked()), this, SLOT(addLandmarkButtonClickedSlot()));
66 
67  mEditLandmarkButton = new QPushButton("Resample", this);
68  mEditLandmarkButton->setToolTip("Resample existing landmark");
69  mEditLandmarkButton->setDisabled(true);
70  connect(mEditLandmarkButton, SIGNAL(clicked()), this, SLOT(editLandmarkButtonClickedSlot()));
71 
72  mRemoveLandmarkButton = new QPushButton("Clear", this);
73  mRemoveLandmarkButton->setToolTip("Clear selected landmark");
74  mRemoveLandmarkButton->setDisabled(true);
75  connect(mRemoveLandmarkButton, SIGNAL(clicked()), this, SLOT(removeLandmarkButtonClickedSlot()));
76 
77  mCreateCenterlineButton = new QPushButton("Create centerline", this);
78  mCreateCenterlineButton->setToolTip("Create centerline from landmarks");
79  mCreateCenterlineButton->setDisabled(true);
80  connect(mCreateCenterlineButton, SIGNAL(clicked()), this, SLOT(createCenterlineButtonClickedSlot()));
81 
82  //layout
86 
87  QHBoxLayout* landmarkButtonsLayout = new QHBoxLayout;
88  landmarkButtonsLayout->addWidget(mAddLandmarkButton);
89  landmarkButtonsLayout->addWidget(mEditLandmarkButton);
90  landmarkButtonsLayout->addWidget(mRemoveLandmarkButton);
91  landmarkButtonsLayout->addWidget(mCreateCenterlineButton);
92  mVerticalLayout->addLayout(landmarkButtonsLayout);
93 }
94 
96 {
97 }
98 
100 {
101  DataPtr data = mCurrentProperty->getData();
102 
103  mLandmarkListener->setLandmarkSource(data);
104  this->enableButtons();
105 
106  if (data && !mServices->registration()->getFixedData())
107  mServices->registration()->setFixedData(data);
108 
109  this->setModified();
110 }
111 
113 {
114  return mServices->view()->get3DReps(0, 0)->findFirst<PickerRep>();
115 }
116 
117 DataPtr CenterlinePointsWidget::getCurrentData() const
118 {
119  return mLandmarkListener->getLandmarkSource();
120 }
121 
123 {
125  if (!PickerRep)
126  {
127  reportError("Need a 3D view to set landmarks.");
128  return;
129  }
130 
131  DataPtr image = this->getCurrentData();
132  if (!image)
133  return;
134 
135  QString uid = mServices->patient()->addLandmark();
136  Vector3D pos_r = PickerRep->getPosition();
137  Vector3D pos_d = image->get_rMd().inv().coord(pos_r);
138  image->getLandmarks()->setLandmark(Landmark(uid, pos_d));
139 
140  this->activateLandmark(uid);
141 }
142 
143 
145 {
147  if (!PickerRep)
148  {
149  reportError("Need a 3D view to edit landmarks.");
150  return;
151  }
152 
153  DataPtr image = this->getCurrentData();
154  if (!image)
155  return;
156 
157  QString uid = mActiveLandmark;
158  Vector3D pos_r = PickerRep->getPosition();
159  Vector3D pos_d = image->get_rMd().inv().coord(pos_r);
160  image->getLandmarks()->setLandmark(Landmark(uid, pos_d));
161 
162  this->activateLandmark(this->getNextLandmark());
163 }
164 
166 {
167  DataPtr image = this->getCurrentData();
168  if (!image)
169  return;
170 
171  QString next = this->getNextLandmark();
172  image->getLandmarks()->removeLandmark(mActiveLandmark);
173  this->activateLandmark(next);
174 }
175 
177 {
178  DataPtr image = this->getCurrentData();
179  if (!image)
180  return;
181 
182  Transform3D rMd = image->get_rMd();
183 
184  std::vector<Landmark> landmarkVector = this-> getAllLandmarks();
185 
186  vtkPolyDataPtr outputPositions = vtkPolyDataPtr::New();
187  vtkPointsPtr points = vtkPointsPtr::New();
188  vtkCellArrayPtr lines = vtkCellArrayPtr::New();
189 
190  for(int i=0; i<landmarkVector.size(); i++)
191  {
192  LandmarkProperty prop = mServices->patient()->getLandmarkProperties()[landmarkVector[i].getUid()];
193  if (prop.getActive())
194  {
195  Vector3D pos = landmarkVector[i].getCoord();
196  pos = rMd.coord(pos);
197  points->InsertNextPoint(pos(0),pos(1),pos(2));
198  }
199  }
200  for(int i=0; i<points->GetNumberOfPoints()-1; i++)
201  {
202  vtkIdType connection[2] = {i, i+1};
203  lines->InsertNextCell(2, connection);
204  }
205 
206  outputPositions->SetPoints(points);
207  outputPositions->SetLines(lines);
208 
209  QString filename;
210  QString format = timestampSecondsFormatNice();
211  filename = "CenterlineFromPoints " + QDateTime::currentDateTime().toString(format);
212 
213  MeshPtr mesh = mServices->patient()->createSpecificData<Mesh>(filename, filename);
214  mesh->setVtkPolyData(outputPositions);
215  mesh->setColor(QColor(0, 0, 255, 255));
216  mServices->patient()->insertData(mesh);
217  mServices->view()->autoShowData(mesh);
218 }
219 
221 {
223  this->enableButtons();
224 }
225 
227 {
228  bool selected = !mLandmarkTableWidget->selectedItems().isEmpty();
229  bool loaded = this->getCurrentData() != 0;
230 
231  mEditLandmarkButton->setEnabled(selected);
232  mRemoveLandmarkButton->setEnabled(selected);
233  mCreateCenterlineButton->setEnabled(selected);
234  mAddLandmarkButton->setEnabled(loaded);
235 
236  DataPtr image = this->getCurrentData();
237  if (image)
238  {
239  mAddLandmarkButton->setToolTip(QString("Add landmark to image %1").arg(image->getName()));
240  mEditLandmarkButton->setToolTip(QString("Resample landmark in image %1").arg(image->getName()));
241  }
242 // this->setModified();
243 }
244 
245 void CenterlinePointsWidget::showEvent(QShowEvent* event)
246 {
247  mServices->view()->setRegistrationMode(rsIMAGE_REGISTRATED);
249 
250  if(!mUseRegistrationFixedPropertyInsteadOfActiveImage)
251  {
252  ActiveDataPtr activeData = mServices->patient()->getActiveData();
253  ImagePtr image = activeData->getActive<Image>();
254  if (image)
255  mCurrentProperty->setValue(image->getUid());
256  }
257 }
258 
259 void CenterlinePointsWidget::hideEvent(QHideEvent* event)
260 {
261  mServices->view()->setRegistrationMode(rsNOT_REGISTRATED);
263 
264 }
265 
267 {
269 
270  std::vector<Landmark> landmarks = this->getAllLandmarks();
271 
272  //update buttons
273  mCreateCenterlineButton->setEnabled(!landmarks.empty() && !mActiveLandmark.isEmpty());
274  mEditLandmarkButton->setEnabled(!landmarks.empty() && !mActiveLandmark.isEmpty());
275 }
276 
278 {
279  DataPtr image = this->getCurrentData();
280  if (!image)
281  return LandmarkMap();
282 
283  return image->getLandmarks()->getLandmarks();
284 }
285 
290 {
291  DataPtr image = this->getCurrentData();
292  if (!image)
293  return Transform3D::Identity();
294  return image->get_rMd();
295 }
296 
298 {
299  DataPtr image = this->getCurrentData();
300  if (!image)
301  return;
302  image->getLandmarks()->setLandmark(Landmark(uid, p_target));
303 }
304 
306 {
307  DataPtr image = this->getCurrentData();
308  if (!image)
309  return "None";
310  return image->getName();
311 }
312 
313 
314 }//namespace cx
cx::LabeledComboBoxWidget
Composite widget for string selection.
Definition: cxLabeledComboBoxWidget.h:36
cx::LandmarkRegistrationWidget
Definition: cxLandmarkRegistrationWidget.h:45
cx::LandmarkRegistrationWidget::cellClickedSlot
virtual void cellClickedSlot(int row, int column)
when a landmark is selected from the table
Definition: cxLandmarkRegistrationWidget.cpp:61
vtkCellArrayPtr
vtkSmartPointer< class vtkCellArray > vtkCellArrayPtr
Definition: vtkForwardDeclarations.h:41
cx::LandmarkProperty
Definition: cxLandmark.h:90
cx::StringPropertySelectData::New
static StringPropertySelectDataPtr New(PatientModelServicePtr patientModelService, QString typeRegexp=".*")
Definition: cxSelectDataStringProperty.h:101
cx::LandmarkRegistrationWidget::mLandmarkListener
LandmarkListenerPtr mLandmarkListener
Definition: cxLandmarkRegistrationWidget.h:92
cxLogger.h
cx::CenterlinePointsWidget::mRemoveLandmarkButton
QPushButton * mRemoveLandmarkButton
the Remove Landmark button
Definition: cxCenterlinePointsWidget.h:86
cx::Mesh::setVtkPolyData
void setVtkPolyData(const vtkPolyDataPtr &polyData)
Definition: cxMesh.cpp:92
cx::CenterlinePointsWidget::editLandmarkButtonClickedSlot
virtual void editLandmarkButtonClickedSlot()
reacts when the Edit Landmark button is clicked
Definition: cxCenterlinePointsWidget.cpp:144
cxActiveData.h
cx::LandmarkRegistrationWidget::getNextLandmark
QString getNextLandmark()
Definition: cxLandmarkRegistrationWidget.cpp:234
cx::CenterlinePointsWidget::enableButtons
void enableButtons()
Definition: cxCenterlinePointsWidget.cpp:226
cx::OptimizedUpdateWidget::setModified
virtual void setModified()
Definition: cxOptimizedUpdateWidget.cpp:36
rsNOT_REGISTRATED
rsNOT_REGISTRATED
Definition: cxDefinitions.h:128
cx::CenterlinePointsWidget::createCenterlineButtonClickedSlot
void createCenterlineButtonClickedSlot()
reacts when the Create centerline button is clicked
Definition: cxCenterlinePointsWidget.cpp:176
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::Property::changed
void changed()
emit when the underlying data value is changed: The user interface will be updated.
cx::CenterlinePointsWidget::hideEvent
virtual void hideEvent(QHideEvent *event)
Definition: cxCenterlinePointsWidget.cpp:259
cx::LandmarkRegistrationWidget::getAllLandmarks
std::vector< Landmark > getAllLandmarks() const
get all the landmarks from the image and the datamanager
Definition: cxLandmarkRegistrationWidget.cpp:250
cx::CenterlinePointsWidget::mEditLandmarkButton
QPushButton * mEditLandmarkButton
the Edit Landmark button
Definition: cxCenterlinePointsWidget.h:85
cx::LandmarkMap
std::map< QString, class Landmark > LandmarkMap
Definition: cxLandmarkRegistrationWidget.h:31
cxLabeledComboBoxWidget.h
cx::CenterlinePointsWidget::removeLandmarkButtonClickedSlot
void removeLandmarkButtonClickedSlot()
reacts when the Remove Landmark button is clicked
Definition: cxCenterlinePointsWidget.cpp:165
cx::timestampSecondsFormatNice
QString timestampSecondsFormatNice()
Definition: cxTime.cpp:26
cx::LandmarkRegistrationWidget::mLandmarkTableWidget
QTableWidget * mLandmarkTableWidget
the table widget presenting the landmarks
Definition: cxLandmarkRegistrationWidget.h:86
cx::CenterlinePointsWidget::getTargetLandmarks
virtual LandmarkMap getTargetLandmarks() const
Definition: cxCenterlinePointsWidget.cpp:277
cx::CenterlinePointsWidget::mCurrentProperty
SelectDataStringPropertyBasePtr mCurrentProperty
Definition: cxCenterlinePointsWidget.h:81
cxLandmarkListener.h
cx::CenterlinePointsWidget::mActiveToolProxy
ActiveToolProxyPtr mActiveToolProxy
Definition: cxCenterlinePointsWidget.h:82
cx::LandmarkRegistrationWidget::mAvarageAccuracyLabel
QLabel * mAvarageAccuracyLabel
label showing the average accuracy
Definition: cxLandmarkRegistrationWidget.h:87
cx::Landmark
One landmark, or fiducial, coordinate.
Definition: cxLandmark.h:40
cx::CenterlinePointsWidget::mCreateCenterlineButton
QPushButton * mCreateCenterlineButton
the Create centerline button
Definition: cxCenterlinePointsWidget.h:87
cx::RegistrationBaseWidget::mServices
RegServicesPtr mServices
Definition: cxRegistrationBaseWidget.h:35
cx::LandmarkRegistrationWidget::mActiveLandmark
QString mActiveLandmark
uid of currently selected landmark.
Definition: cxLandmarkRegistrationWidget.h:91
cx::MeshPtr
boost::shared_ptr< class Mesh > MeshPtr
Definition: cxForwardDeclarations.h:48
cx::Transform3D
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
Definition: cxLandmarkPatientRegistrationWidget.h:33
cxViewService.h
cx::CenterlinePointsWidget::getPickerRep
PickerRepPtr getPickerRep()
Definition: cxCenterlinePointsWidget.cpp:112
cx::LandmarkRegistrationWidget::mVerticalLayout
QVBoxLayout * mVerticalLayout
vertical layout is used
Definition: cxLandmarkRegistrationWidget.h:85
cxRepContainer.h
cx::vtkPointsPtr
vtkSmartPointer< vtkPoints > vtkPointsPtr
Definition: cxCenterlineRegistration.h:41
cxTypeConversions.h
cx::DataPtr
boost::shared_ptr< class Data > DataPtr
Definition: cxRegistrationApplicator.h:22
cxView.h
cx::CenterlinePointsWidget::~CenterlinePointsWidget
virtual ~CenterlinePointsWidget()
empty
Definition: cxCenterlinePointsWidget.cpp:95
cxPatientModelService.h
cx::ImagePtr
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:27
cx::PickerRep::getPosition
Vector3D getPosition() const
Definition: cxPickerRep.cpp:443
cx::LandmarkRegistrationWidget::showEvent
virtual void showEvent(QShowEvent *event)
updates internal info before showing the widget
Definition: cxLandmarkRegistrationWidget.cpp:100
cxSettings.h
cxCenterlinePointsWidget.h
cx::RegServicesPtr
boost::shared_ptr< class RegServices > RegServicesPtr
Definition: cxRegServices.h:20
cx::vtkPolyDataPtr
vtkSmartPointer< vtkPolyData > vtkPolyDataPtr
Definition: cxCenterlineRegistration.h:42
cx::CenterlinePointsWidget::onCurrentImageChanged
void onCurrentImageChanged()
Definition: cxCenterlinePointsWidget.cpp:99
cx::StringPropertyRegistrationFixedImage
Definition: cxRegistrationProperties.h:31
cx::LandmarkRegistrationWidget::hideEvent
virtual void hideEvent(QHideEvent *event)
Definition: cxLandmarkRegistrationWidget.cpp:133
cxLandmarkRep.h
cx::PickerRepPtr
boost::shared_ptr< class PickerRep > PickerRepPtr
Definition: cxForwardDeclarations.h:83
cxTime.h
cx::CenterlinePointsWidget::showEvent
virtual void showEvent(QShowEvent *event)
updates internal info before showing the widget
Definition: cxCenterlinePointsWidget.cpp:245
cx::Mesh
A mesh data set.
Definition: cxMesh.h:45
cx::PickerRep
Picking of points in an image.
Definition: cxPickerRep.h:49
cx::LandmarkRegistrationWidget::prePaintEvent
virtual void prePaintEvent()
populates the table widget
Definition: cxLandmarkRegistrationWidget.cpp:144
cx::CenterlinePointsWidget::getTargetName
virtual QString getTargetName() const
Definition: cxCenterlinePointsWidget.cpp:305
cx::Image
A volumetric data set.
Definition: cxImage.h:45
cxRegistrationService.h
cxSelectDataStringProperty.h
cxPickerRep.h
cx::ActiveToolProxy::New
static ActiveToolProxyPtr New(TrackingServicePtr trackingService)
Definition: cxActiveToolProxy.h:45
cx::CenterlinePointsWidget::cellClickedSlot
virtual void cellClickedSlot(int row, int column)
when a landmark is selected from the table
Definition: cxCenterlinePointsWidget.cpp:220
cx::CenterlinePointsWidget::prePaintEvent
virtual void prePaintEvent()
populates the table widget
Definition: cxCenterlinePointsWidget.cpp:266
cx::ActiveDataPtr
boost::shared_ptr< class ActiveData > ActiveDataPtr
Definition: cxColorWidget.h:21
cx::reportError
void reportError(QString msg)
Definition: cxLogger.cpp:71
cxMesh.h
cx::CenterlinePointsWidget::addLandmarkButtonClickedSlot
void addLandmarkButtonClickedSlot()
reacts when the Add Landmark button is clicked
Definition: cxCenterlinePointsWidget.cpp:122
cx::Vector3D
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:42
cxTrackingService.h
cx::CenterlinePointsWidget::getTargetTransform
virtual Transform3D getTargetTransform() const
Definition: cxCenterlinePointsWidget.cpp:289
rsIMAGE_REGISTRATED
rsIMAGE_REGISTRATED
Definition: cxDefinitions.h:129
cx::CenterlinePointsWidget::setTargetLandmark
virtual void setTargetLandmark(QString uid, Vector3D p_target)
Definition: cxCenterlinePointsWidget.cpp:297
cx::LandmarkProperty::getActive
bool getActive() const
Definition: cxLandmark.cpp:179
cx::CenterlinePointsWidget::mAddLandmarkButton
QPushButton * mAddLandmarkButton
the Add Landmark button
Definition: cxCenterlinePointsWidget.h:84
cx::LandmarkRegistrationWidget::activateLandmark
void activateLandmark(QString uid)
Definition: cxLandmarkRegistrationWidget.cpp:224