NorMIT-nav  2023.01.05-dev+develop.0da12
An IGT application
cxImageLandmarksWidget.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 
12 #include "cxImageLandmarksWidget.h"
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 <QCheckBox>
23 #include <vtkDoubleArray.h>
24 #include <vtkImageData.h>
25 #include "cxLogger.h"
26 #include "cxPickerRep.h"
28 #include "cxSettings.h"
29 #include "cxLandmarkRep.h"
30 #include "cxView.h"
31 #include "cxTypeConversions.h"
33 #include "cxRegistrationService.h"
34 #include "cxPatientModelService.h"
35 #include "cxViewService.h"
36 #include "cxRepContainer.h"
37 #include "cxTrackingService.h"
38 #include "cxLandmarkListener.h"
39 #include "cxActiveData.h"
40 #include "cxPointMetric.h"
41 #include "cxSpaceProvider.h"
42 
43 namespace cx
44 {
45 ImageLandmarksWidget::ImageLandmarksWidget(RegServicesPtr services, QWidget* parent,
46  QString objectName, QString windowTitle,
47  bool useRegistrationFixedPropertyInsteadOfActiveImage) :
48  LandmarkRegistrationWidget(services, parent, objectName, windowTitle),
49  mUseRegistrationFixedPropertyInsteadOfActiveImage(useRegistrationFixedPropertyInsteadOfActiveImage),
50  mLandmarksShowAdvancedSettingsString("Landmarks/ShowAdvanced")
51 {
52  if(mUseRegistrationFixedPropertyInsteadOfActiveImage)
53  mCurrentProperty.reset(new StringPropertyRegistrationFixedImage(services->registration(), services->patient()));
54  else
57 
58  mLandmarkListener->useOnlyOneSourceUpdatedFromOutside();
59 
60  mActiveToolProxy = ActiveToolProxy::New(services->tracking());
61  connect(mActiveToolProxy.get(), SIGNAL(toolVisible(bool)), this, SLOT(enableButtons()));
62  connect(mActiveToolProxy.get(), SIGNAL(activeToolChanged(const QString&)), this, SLOT(enableButtons()));
63 
64  //pushbuttons
65  mAddLandmarkButton = new QPushButton("New Landmark", this);
66  mAddLandmarkButton->setToolTip("Add landmark");
67  mAddLandmarkButton->setDisabled(true);
68  connect(mAddLandmarkButton, SIGNAL(clicked()), this, SLOT(addLandmarkButtonClickedSlot()));
69 
70  mEditLandmarkButton = new QPushButton("Resample", this);
71  mEditLandmarkButton->setToolTip("Resample the selected landmark");
72  mEditLandmarkButton->setDisabled(true);
73  connect(mEditLandmarkButton, SIGNAL(clicked()), this, SLOT(editLandmarkButtonClickedSlot()));
74 
75  mRemoveLandmarkButton = new QPushButton("Clear", this);
76  mRemoveLandmarkButton->setToolTip("Clear the selected landmark");
77  mRemoveLandmarkButton->setDisabled(true);
78  connect(mRemoveLandmarkButton, SIGNAL(clicked()), this, SLOT(removeLandmarkButtonClickedSlot()));
79 
80  mDeleteLandmarksButton = new QPushButton("Delete All", this);
81  mDeleteLandmarksButton->setToolTip("Delete all landmarks");
82  connect(mDeleteLandmarksButton, SIGNAL(clicked()), this, SLOT(deleteLandmarksButtonClickedSlot()));
83 
84  mImportLandmarksFromPointMetricsButton = new QPushButton("Import Point Metrics", this);
85  mImportLandmarksFromPointMetricsButton->setToolTip("Import point metrics as landmarks. See the help pages for the details.");
87 
88  //layout
92 
93  QHBoxLayout* landmarkButtonsLayout = new QHBoxLayout;
94  landmarkButtonsLayout->addWidget(mAddLandmarkButton);
95  landmarkButtonsLayout->addWidget(mEditLandmarkButton);
96  landmarkButtonsLayout->addWidget(mRemoveLandmarkButton);
97  landmarkButtonsLayout->addWidget(mDeleteLandmarksButton);
98  mDetailsAction = this->createAction(this,
99  QIcon(":/icons/open_icon_library/system-run-5.png"),
100  "Advanced", "Toggle advanced options",
101  SLOT(toggleDetailsSlot()),
102  landmarkButtonsLayout);
103  mVerticalLayout->addLayout(landmarkButtonsLayout);
104 
105  QHBoxLayout* landmarkAdvancedButtonsLayout = new QHBoxLayout;
106  landmarkAdvancedButtonsLayout = new QHBoxLayout;
107  landmarkAdvancedButtonsLayout->addWidget(mImportLandmarksFromPointMetricsButton);
108  mVerticalLayout->addLayout(landmarkAdvancedButtonsLayout);
109 
110  mMouseClickSample->show();
111  connect(mMouseClickSample, &QCheckBox::stateChanged, this, &ImageLandmarksWidget::mouseClickSampleStateChanged);
113 
114  this->showOrHideDetails();
115 }
116 
118 {
119 }
120 
122 {
123  DataPtr data = mCurrentProperty->getData();
124 
125  mLandmarkListener->setLandmarkSource(data);
126  this->enableButtons();
127 
128  if (data && !mServices->registration()->getFixedData())
129  mServices->registration()->setFixedData(data);
130 
131  this->setModified();
132 }
133 
135 {
136  bool newShowAdvancedValue = !settings()->value(mLandmarksShowAdvancedSettingsString, "true").toBool();
137  settings()->setValue(mLandmarksShowAdvancedSettingsString, newShowAdvancedValue);
138  this->showOrHideDetails();
139 }
140 
141 void ImageLandmarksWidget::showOrHideDetails()
142 {
143  bool showAdvanced = settings()->value(mLandmarksShowAdvancedSettingsString).toBool();
144  mImportLandmarksFromPointMetricsButton->setVisible(showAdvanced);
145 }
146 
148 {
149  return mServices->view()->get3DReps(0, 0)->findFirst<PickerRep>();
150 }
151 
152 DataPtr ImageLandmarksWidget::getCurrentData() const
153 {
154  return mLandmarkListener->getLandmarkSource();
155 }
156 
157 
159 {
160  this->resampleLandmark(p_r);
161  //Only use the anyplane sampler for resample for now
162  //this->addLandmark(p_r);
163 }
164 
166 {
168  if (!PickerRep)
169  {
170  reportError("Need a 3D view to set landmarks.");
171  return;
172  }
173  Vector3D pos_r = PickerRep->getPosition();
174  this->addLandmark(pos_r);
175 }
176 
178 {
179  DataPtr image = this->getCurrentData();
180  if (!image)
181  return;
182  QString uid = mServices->patient()->addLandmark();
183  Vector3D pos_d = image->get_rMd().inv().coord(p_r);
184  image->getLandmarks()->setLandmark(Landmark(uid, pos_d));
185  this->activateLandmark(uid);
186 }
187 
188 
190 {
192  if (!PickerRep)
193  {
194  reportError("Need a 3D view to edit landmarks.");
195  return;
196  }
197 
198  Vector3D pos_r = PickerRep->getPosition();
199  this->resampleLandmark(pos_r);
200 }
201 
203 {
204  DataPtr image = this->getCurrentData();
205  if (!image)
206  return;
207  QString uid = mActiveLandmark;
208  Vector3D pos_d = image->get_rMd().inv().coord(p_r);
209  image->getLandmarks()->setLandmark(Landmark(uid, pos_d));
210 
211  this->activateLandmark(this->getNextLandmark());
212 }
213 
215 {
216  DataPtr image = this->getCurrentData();
217  if (!image)
218  return;
219 
220  QString next = this->getNextLandmark();
221  image->getLandmarks()->removeLandmark(mActiveLandmark);
222  this->activateLandmark(next);
223 }
224 
226 {
227  DataPtr image = this->getCurrentData();
228  if (!image)
229  return;
230 
231  image->getLandmarks()->clear();
232  this->setModified();
233  mServices->patient()->deleteLandmarks();
234 }
235 
237 {
238  DataPtr image = this->getCurrentData();
239  if(!image)
240  return;
241 
242  std::map<QString, DataPtr> point_metrics = mServices->patient()->getChildren(image->getUid(), PointMetric::getTypeName());
243  std::map<QString, DataPtr>::iterator it = point_metrics.begin();
244 
245  //Make sure we have enough landmarks
246  int number_of_landmarks = mServices->patient()->getLandmarkProperties().size();
247  int number_of_metrics = point_metrics.size();
248  for(int i=number_of_landmarks; i<number_of_metrics; ++i)
249  {
250  QString uid = mServices->patient()->addLandmark();
251  }
252 
253  for(; it != point_metrics.end(); ++it)
254  {
255  PointMetricPtr point_metric = boost::static_pointer_cast<PointMetric>(it->second);
256  if(!point_metric)
257  continue;
258 
259  Vector3D pos_x = point_metric->getCoordinate();
260  //Transform3D d_M_x = mServices->spaceProvider()->get_toMfrom(CoordinateSystem::fromString(image->getSpace()), point_metric->getSpace());
261  //Vector3D pos_d = d_M_x.coord(pos_x);
262  QString point_metric_name = point_metric->getName();
263  image->getLandmarks()->setLandmark(Landmark(point_metric_name, pos_x));
264  this->activateLandmark(point_metric_name);
265  }
266 }
267 
268 void ImageLandmarksWidget::cellClickedSlot(int row, int column)
269 {
271  this->enableButtons();
272 }
273 
275 {
276  bool selected = !mLandmarkTableWidget->selectedItems().isEmpty();
277  bool loaded = this->getCurrentData() != 0;
278 
279  mEditLandmarkButton->setEnabled(selected);
280  mRemoveLandmarkButton->setEnabled(selected);
281  mDeleteLandmarksButton->setEnabled(loaded);
282  mAddLandmarkButton->setEnabled(loaded);
283  mImportLandmarksFromPointMetricsButton->setEnabled(loaded);
284 
285  DataPtr image = this->getCurrentData();
286  if (image)
287  {
288  mAddLandmarkButton->setToolTip(QString("Add landmark to image %1").arg(image->getName()));
289  mEditLandmarkButton->setToolTip(QString("Resample landmark in image %1").arg(image->getName()));
290  }
291 }
292 
293 void ImageLandmarksWidget::showEvent(QShowEvent* event)
294 {
295  mServices->view()->setRegistrationMode(rsIMAGE_REGISTRATED);
297 
298  if(!mUseRegistrationFixedPropertyInsteadOfActiveImage)
299  {
300  ActiveDataPtr activeData = mServices->patient()->getActiveData();
301  ImagePtr image = activeData->getActive<Image>();
302  if (image)
303  mCurrentProperty->setValue(image->getUid());
304  }
305 }
306 
307 void ImageLandmarksWidget::hideEvent(QHideEvent* event)
308 {
309  mServices->view()->setRegistrationMode(rsNOT_REGISTRATED);
311 
312 }
313 
315 {
317 
318  std::vector<Landmark> landmarks = this->getAllLandmarks();
319 
320  //update buttons
321  mRemoveLandmarkButton->setEnabled(!landmarks.empty() && !mActiveLandmark.isEmpty());
322  mEditLandmarkButton->setEnabled(!landmarks.empty() && !mActiveLandmark.isEmpty());
323  this->showOrHideDetails();
324 }
325 
327 {
328  DataPtr image = this->getCurrentData();
329  if (!image)
330  return LandmarkMap();
331 
332  return image->getLandmarks()->getLandmarks();
333 }
334 
339 {
340  DataPtr image = this->getCurrentData();
341  if (!image)
342  return Transform3D::Identity();
343  return image->get_rMd();
344 }
345 
347 {
348  DataPtr image = this->getCurrentData();
349  if (!image)
350  return;
351  image->getLandmarks()->setLandmark(Landmark(uid, p_target));
352 }
353 
355 {
356  DataPtr image = this->getCurrentData();
357  if (!image)
358  return "None";
359  return image->getName();
360 }
361 
362 
363 }//namespace cx
cx::ImageLandmarksWidget::getPickerRep
PickerRepPtr getPickerRep()
Definition: cxImageLandmarksWidget.cpp:147
cx::LabeledComboBoxWidget
Composite widget for string selection.
Definition: cxLabeledComboBoxWidget.h:36
cx::ImageLandmarksWidget::prePaintEvent
virtual void prePaintEvent()
populates the table widget
Definition: cxImageLandmarksWidget.cpp:314
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
cx::ImageLandmarksWidget::mEditLandmarkButton
QPushButton * mEditLandmarkButton
the Edit Landmark button
Definition: cxImageLandmarksWidget.h:88
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::ImageLandmarksWidget::getTargetTransform
virtual Transform3D getTargetTransform() const
Definition: cxImageLandmarksWidget.cpp:338
cx::ImageLandmarksWidget::addLandmarkButtonClickedSlot
void addLandmarkButtonClickedSlot()
reacts when the Add Landmark button is clicked
Definition: cxImageLandmarksWidget.cpp:165
cx::Settings::value
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Definition: cxSettings.cpp:66
cxActiveData.h
cx::LandmarkRegistrationWidget::getNextLandmark
QString getNextLandmark()
Definition: cxLandmarkRegistrationWidget.cpp:234
cx::ImageLandmarksWidget::mRemoveLandmarkButton
QPushButton * mRemoveLandmarkButton
the Remove Landmark button
Definition: cxImageLandmarksWidget.h:89
cx::OptimizedUpdateWidget::setModified
virtual void setModified()
Definition: cxOptimizedUpdateWidget.cpp:36
rsNOT_REGISTRATED
rsNOT_REGISTRATED
Definition: cxDefinitions.h:128
cx::ImageLandmarksWidget::removeLandmarkButtonClickedSlot
void removeLandmarkButtonClickedSlot()
reacts when the Remove Landmark button is clicked
Definition: cxImageLandmarksWidget.cpp:214
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::LandmarkRegistrationWidget::getAllLandmarks
std::vector< Landmark > getAllLandmarks() const
get all the landmarks from the image and the datamanager
Definition: cxLandmarkRegistrationWidget.cpp:250
cx::ImageLandmarksWidget::onCurrentImageChanged
void onCurrentImageChanged()
Definition: cxImageLandmarksWidget.cpp:121
cx::ImageLandmarksWidget::setTargetLandmark
virtual void setTargetLandmark(QString uid, Vector3D p_target)
Definition: cxImageLandmarksWidget.cpp:346
cx::PointMetric::getTypeName
static QString getTypeName()
Definition: cxPointMetric.h:58
cx::ImageLandmarksWidget::mCurrentProperty
SelectDataStringPropertyBasePtr mCurrentProperty
Definition: cxImageLandmarksWidget.h:84
cxImageLandmarksWidget.h
cx::LandmarkMap
std::map< QString, class Landmark > LandmarkMap
Definition: cxLandmarkRegistrationWidget.h:31
cx::ImageLandmarksWidget::addLandmark
void addLandmark(Vector3D p_r)
Definition: cxImageLandmarksWidget.cpp:177
cxLabeledComboBoxWidget.h
cx::LandmarkRegistrationWidget::mMouseClickSample
QCheckBox * mMouseClickSample
Definition: cxLandmarkRegistrationWidget.h:88
cx::ImageLandmarksWidget::mAddLandmarkButton
QPushButton * mAddLandmarkButton
the Add Landmark button
Definition: cxImageLandmarksWidget.h:87
cx::LandmarkRegistrationWidget::mLandmarkTableWidget
QTableWidget * mLandmarkTableWidget
the table widget presenting the landmarks
Definition: cxLandmarkRegistrationWidget.h:86
cx::BaseWidget::createAction
QAction * createAction(QObject *parent, QIcon iconName, QString text, QString tip, T slot, QLayout *layout=NULL, QToolButton *button=new QToolButton())
Definition: cxBaseWidget.h:129
cx::LandmarkRegistrationWidget::mouseClickSampleStateChanged
void mouseClickSampleStateChanged()
Definition: cxLandmarkRegistrationWidget.cpp:392
cx::ImageLandmarksWidget::mDetailsAction
QAction * mDetailsAction
Definition: cxImageLandmarksWidget.h:92
cxLandmarkListener.h
cx::ImageLandmarksWidget::~ImageLandmarksWidget
virtual ~ImageLandmarksWidget()
empty
Definition: cxImageLandmarksWidget.cpp:117
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::ImageLandmarksWidget::mDeleteLandmarksButton
QPushButton * mDeleteLandmarksButton
the Delete Landmarks button
Definition: cxImageLandmarksWidget.h:90
cx::RegistrationBaseWidget::mServices
RegServicesPtr mServices
Definition: cxRegistrationBaseWidget.h:35
cx::LandmarkRegistrationWidget::mActiveLandmark
QString mActiveLandmark
uid of currently selected landmark.
Definition: cxLandmarkRegistrationWidget.h:91
cx::ImageLandmarksWidget::toggleDetailsSlot
void toggleDetailsSlot()
Definition: cxImageLandmarksWidget.cpp:134
cx::ImageLandmarksWidget::pointSampled
virtual void pointSampled(Vector3D p_r)
Definition: cxImageLandmarksWidget.cpp:158
cx::ImageLandmarksWidget::importPointMetricsToLandmarkButtonClickedSlot
void importPointMetricsToLandmarkButtonClickedSlot()
Definition: cxImageLandmarksWidget.cpp:236
cx::ImageLandmarksWidget::deleteLandmarksButtonClickedSlot
void deleteLandmarksButtonClickedSlot()
reacts when the Delete Landmarks button is clicked
Definition: cxImageLandmarksWidget.cpp:225
cx::Transform3D
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
Definition: cxLandmarkPatientRegistrationWidget.h:33
cxViewService.h
cx::LandmarkRegistrationWidget::mVerticalLayout
QVBoxLayout * mVerticalLayout
vertical layout is used
Definition: cxLandmarkRegistrationWidget.h:85
cxRepContainer.h
cxTypeConversions.h
cx::DataPtr
boost::shared_ptr< class Data > DataPtr
Definition: cxRegistrationApplicator.h:22
cxView.h
cxPatientModelService.h
cx::ImageLandmarksWidget::resampleLandmark
void resampleLandmark(Vector3D p_r)
Definition: cxImageLandmarksWidget.cpp:202
cx::ImageLandmarksWidget::mImportLandmarksFromPointMetricsButton
QPushButton * mImportLandmarksFromPointMetricsButton
the Import Landmarks button
Definition: cxImageLandmarksWidget.h:91
cx::ImagePtr
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:27
cx::ImageLandmarksWidget::mActiveToolProxy
ActiveToolProxyPtr mActiveToolProxy
Definition: cxImageLandmarksWidget.h:85
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
cx::ImageLandmarksWidget::enableButtons
void enableButtons()
Definition: cxImageLandmarksWidget.cpp:274
cx::RegServicesPtr
boost::shared_ptr< class RegServices > RegServicesPtr
Definition: cxRegServices.h:20
cx::ImageLandmarksWidget::getTargetName
virtual QString getTargetName() const
Definition: cxImageLandmarksWidget.cpp:354
cxPointMetric.h
cx::StringPropertyRegistrationFixedImage
Definition: cxRegistrationProperties.h:31
cx::LandmarkRegistrationWidget::hideEvent
virtual void hideEvent(QHideEvent *event)
Definition: cxLandmarkRegistrationWidget.cpp:133
cxLandmarkRep.h
cx::ImageLandmarksWidget::editLandmarkButtonClickedSlot
virtual void editLandmarkButtonClickedSlot()
reacts when the Edit Landmark button is clicked
Definition: cxImageLandmarksWidget.cpp:189
cx::PickerRepPtr
boost::shared_ptr< class PickerRep > PickerRepPtr
Definition: cxForwardDeclarations.h:83
cx::ImageLandmarksWidget::hideEvent
virtual void hideEvent(QHideEvent *event)
Definition: cxImageLandmarksWidget.cpp:307
cx::PickerRep
Picking of points in an image.
Definition: cxPickerRep.h:49
cx::Settings::setValue
void setValue(const QString &key, const QVariant &value)
Definition: cxSettings.cpp:58
cx::LandmarkRegistrationWidget::prePaintEvent
virtual void prePaintEvent()
populates the table widget
Definition: cxLandmarkRegistrationWidget.cpp:144
cx::Image
A volumetric data set.
Definition: cxImage.h:45
cxRegistrationService.h
cxSelectDataStringProperty.h
cxPickerRep.h
cx::ImageLandmarksWidget::cellClickedSlot
virtual void cellClickedSlot(int row, int column)
when a landmark is selected from the table
Definition: cxImageLandmarksWidget.cpp:268
cx::ActiveToolProxy::New
static ActiveToolProxyPtr New(TrackingServicePtr trackingService)
Definition: cxActiveToolProxy.h:45
cx::ActiveDataPtr
boost::shared_ptr< class ActiveData > ActiveDataPtr
Definition: cxColorWidget.h:21
cx::PointMetricPtr
boost::shared_ptr< class PointMetric > PointMetricPtr
Definition: cxForwardDeclarations.h:84
cx::reportError
void reportError(QString msg)
Definition: cxLogger.cpp:71
cx::Vector3D
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:42
cxTrackingService.h
rsIMAGE_REGISTRATED
rsIMAGE_REGISTRATED
Definition: cxDefinitions.h:129
cx::ImageLandmarksWidget::showEvent
virtual void showEvent(QShowEvent *event)
updates internal info before showing the widget
Definition: cxImageLandmarksWidget.cpp:293
cx::settings
Settings * settings()
Shortcut for accessing the settings instance.
Definition: cxSettings.cpp:21
cxSpaceProvider.h
cx::LandmarkRegistrationWidget::activateLandmark
void activateLandmark(QString uid)
Definition: cxLandmarkRegistrationWidget.cpp:224
cx::ImageLandmarksWidget::getTargetLandmarks
virtual LandmarkMap getTargetLandmarks() const
Definition: cxImageLandmarksWidget.cpp:326