CustusX  15.3.4-beta
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxLandmarkImageRegistrationWidget.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 
35 #include <sstream>
36 #include <QVBoxLayout>
37 #include <QPushButton>
38 #include <QTableWidget>
39 #include <QTableWidgetItem>
40 #include <QHeaderView>
41 #include <QLabel>
42 #include <QSlider>
43 #include <vtkDoubleArray.h>
44 #include <vtkImageData.h>
45 #include "cxLogger.h"
46 #include "cxPickerRep.h"
48 #include "cxSettings.h"
49 #include "cxLandmarkRep.h"
50 #include "cxView.h"
51 #include "cxTypeConversions.h"
53 #include "cxRegistrationService.h"
54 #include "cxPatientModelService.h"
55 #include "cxViewService.h"
56 #include "cxViewGroupData.h"
57 #include "cxRepContainer.h"
58 #include "cxTrackingService.h"
59 
60 namespace cx
61 {
62 LandmarkImageRegistrationWidget::LandmarkImageRegistrationWidget(RegServices services, QWidget* parent,
63  QString objectName, QString windowTitle) :
64  LandmarkRegistrationWidget(services, parent, objectName, windowTitle)
65 {
67  connect(mCurrentProperty.get(), SIGNAL(changed()), this, SLOT(onCurrentImageChanged()));
69 
71  connect(mActiveToolProxy.get(), SIGNAL(toolVisible(bool)), this, SLOT(enableButtons()));
72  connect(mActiveToolProxy.get(), SIGNAL(activeToolChanged(const QString&)), this, SLOT(enableButtons()));
73 
74  //pushbuttons
75  mAddLandmarkButton = new QPushButton("Add", this);
76  mAddLandmarkButton->setToolTip("Add landmark");
77  mAddLandmarkButton->setDisabled(true);
78  connect(mAddLandmarkButton, SIGNAL(clicked()), this, SLOT(addLandmarkButtonClickedSlot()));
79 
80  mEditLandmarkButton = new QPushButton("Sample", this);
81  mEditLandmarkButton->setToolTip("Resample landmark");
82  mEditLandmarkButton->setDisabled(true);
83  connect(mEditLandmarkButton, SIGNAL(clicked()), this, SLOT(editLandmarkButtonClickedSlot()));
84 
85  mRemoveLandmarkButton = new QPushButton("Clear", this);
86  mRemoveLandmarkButton->setToolTip("Clear selected landmark");
87  mRemoveLandmarkButton->setDisabled(true);
88  connect(mRemoveLandmarkButton, SIGNAL(clicked()), this, SLOT(removeLandmarkButtonClickedSlot()));
89 
90  //layout
94 
95  QHBoxLayout* landmarkButtonsLayout = new QHBoxLayout;
96  landmarkButtonsLayout->addWidget(mAddLandmarkButton);
97  landmarkButtonsLayout->addWidget(mEditLandmarkButton);
98  landmarkButtonsLayout->addWidget(mRemoveLandmarkButton);
99  mVerticalLayout->addLayout(landmarkButtonsLayout);
100 }
101 
103 {
104 }
105 
107 {
108  return "<html>"
109  "<h3>Landmark based image registration.</h3>"
110  "<p>Sample landmarks in the data set. </p>"
111  "<p><i>Click the volume and either add or resample landmarks.</i></p>"
112  "<p>Landmark image registration will move the active image to the fixed image</p>"
113  "</html>";
114 }
115 
117 {
118  DataPtr data = mCurrentProperty->getData();
119 
120  mImageLandmarkSource->setData(data);
121  this->enableButtons();
122 
123  if (data && !mServices.registrationService->getFixedData())
124  mServices.registrationService->setFixedData(data);
125 
126  this->setModified();
127 }
128 
130 {
131  return mServices.visualizationService->get3DReps(0, 0)->findFirst<PickerRep>();
132 // return RepContainer(mServices.visualizationService->get3DView(0, 0))->findFirst<PickerRep>();
133 
134 // if (!mServices.visualizationService->get3DView(0, 0))
135 // return PickerRepPtr();
136 
137 // return RepContainer::findFirstRep<PickerRep>(mServices.visualizationService->get3DView(0, 0)->getReps());
138 }
139 
140 DataPtr LandmarkImageRegistrationWidget::getCurrentData() const
141 {
142  return mImageLandmarkSource->getData();
143 }
144 
146 {
148  if (!PickerRep)
149  {
150  reportError("Could not find a rep to add the landmark to.");
151  return;
152  }
153 
154  DataPtr image = this->getCurrentData();
155  if (!image)
156  return;
157 
158  QString uid = mServices.patientModelService->addLandmark();
159  Vector3D pos_r = PickerRep->getPosition();
160  Vector3D pos_d = image->get_rMd().inv().coord(pos_r);
161  image->getLandmarks()->setLandmark(Landmark(uid, pos_d));
162 
163  this->activateLandmark(uid);
164 }
165 
166 
168 {
170  if (!PickerRep)
171  {
172  reportError("Could not find a rep to edit the landmark for.");
173  return;
174  }
175 
176  DataPtr image = this->getCurrentData();
177  if (!image)
178  return;
179 
180  QString uid = mActiveLandmark;
181  Vector3D pos_r = PickerRep->getPosition();
182  Vector3D pos_d = image->get_rMd().inv().coord(pos_r);
183  image->getLandmarks()->setLandmark(Landmark(uid, pos_d));
184 
185  this->activateLandmark(this->getNextLandmark());
186 }
187 
189 {
190  DataPtr image = this->getCurrentData();
191  if (!image)
192  return;
193 
194  QString next = this->getNextLandmark();
195  image->getLandmarks()->removeLandmark(mActiveLandmark);
196  this->activateLandmark(next);
197 }
198 
200 {
202  this->enableButtons();
203 }
204 
206 {
207  bool selected = !mLandmarkTableWidget->selectedItems().isEmpty();
208  bool loaded = this->getCurrentData() != 0;
209 
210  mEditLandmarkButton->setEnabled(selected);
211  mRemoveLandmarkButton->setEnabled(selected);
212  mAddLandmarkButton->setEnabled(loaded);
213 
214  DataPtr image = this->getCurrentData();
215  if (image)
216  {
217  mAddLandmarkButton->setToolTip(QString("Add landmark to image %1").arg(image->getName()));
218  mEditLandmarkButton->setToolTip(QString("Resample landmark in image %1").arg(image->getName()));
219  }
220 // this->setModified();
221 }
222 
224 {
226 
227  ImagePtr image = mServices.patientModelService->getActiveImage();
228  if (image)
229  mCurrentProperty->setValue(image->getUid());
230 // if (image && !mManager->getFixedData())
231 // mManager->setFixedData(image);
232 // if (image && !mImageLandmarkSource->getData())
233 // mImageLandmarkSource->setData(image);
234 
235  mServices.visualizationService->getGroup(0)->setRegistrationMode(rsIMAGE_REGISTRATED);
236  LandmarkRepPtr rep = mServices.visualizationService->get3DReps(0, 0)->findFirst<LandmarkRep>();
237  if (rep)
238  {
240  rep->setSecondarySource(LandmarksSourcePtr());
241  }
242 }
243 
245 {
247 
248  if(mServices.visualizationService->get3DView(0, 0))
249  {
250  LandmarkRepPtr rep = mServices.visualizationService->get3DReps(0, 0)->findFirst<LandmarkRep>();
251  if (rep)
252  {
254  rep->setSecondarySource(LandmarksSourcePtr());
255  }
256  }
257  mServices.visualizationService->getGroup(0)->setRegistrationMode(rsNOT_REGISTRATED);
258 }
259 
261 {
263 
264  std::vector<Landmark> landmarks = this->getAllLandmarks();
265 
266  //update buttons
267  mRemoveLandmarkButton->setEnabled(!landmarks.empty() && !mActiveLandmark.isEmpty());
268  mEditLandmarkButton->setEnabled(!landmarks.empty() && !mActiveLandmark.isEmpty());
269 }
270 
272 {
273  DataPtr image = this->getCurrentData();
274  if (!image)
275  return LandmarkMap();
276 
277  return image->getLandmarks()->getLandmarks();
278 }
279 
284 {
285  DataPtr image = this->getCurrentData();
286  if (!image)
287  return Transform3D::Identity();
288  return image->get_rMd();
289 }
290 
292 {
293  DataPtr image = this->getCurrentData();
294  if (!image)
295  return;
296  image->getLandmarks()->setLandmark(Landmark(uid, p_target));
297 }
298 
300 {
301  DataPtr image = this->getCurrentData();
302  if (!image)
303  return "None";
304  return image->getName();
305 }
306 
307 
308 }//namespace cx
virtual void setTargetLandmark(QString uid, Vector3D p_target)
virtual void showEvent(QShowEvent *event)
updates internal info before showing the widget
void reportError(QString msg)
Definition: cxLogger.cpp:92
static ImageLandmarksSourcePtr New()
Definition: cxLandmarkRep.h:94
QLabel * mAvarageAccuracyLabel
label showing the average accuracy
One landmark, or fiducial, coordinate.
Definition: cxLandmark.h:61
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
virtual void editLandmarkButtonClickedSlot()
reacts when the Edit Landmark button is clicked
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
void addLandmarkButtonClickedSlot()
reacts when the Add Landmark button is clicked
virtual void cellClickedSlot(int row, int column)
when a landmark is selected from the table
Composite widget for string selection.
QVBoxLayout * mVerticalLayout
vertical layout is used
virtual void hideEvent(QHideEvent *event)
static ActiveToolProxyPtr New(TrackingServicePtr trackingService)
boost::shared_ptr< class Data > DataPtr
boost::shared_ptr< class PickerRep > PickerRepPtr
Picking of points in an image.
Definition: cxPickerRep.h:70
QPushButton * mAddLandmarkButton
the Add Landmark button
virtual void cellClickedSlot(int row, int column)
when a landmark i selected from the table
void setPrimarySource(LandmarksSourcePtr primary)
QPushButton * mRemoveLandmarkButton
the Remove Landmark button
virtual void prePaintEvent()
populates the table widget
virtual QString defaultWhatsThis() const
Returns a short description of what this widget will do for you.
rsNOT_REGISTRATED
static StringPropertySelectDataPtr New(PatientModelServicePtr patientModelService)
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
VisualizationServicePtr visualizationService
Definition: cxVisServices.h:60
void removeLandmarkButtonClickedSlot()
reacts when the Remove Landmark button is clicked
boost::shared_ptr< LandmarksSource > LandmarksSourcePtr
Definition: cxLandmarkRep.h:72
std::map< QString, class Landmark > LandmarkMap
QString mActiveLandmark
uid of surrently selected landmark.
TrackingServicePtr trackingService
std::vector< Landmark > getAllLandmarks() const
get all the landmarks from the image and the datamanager
QPushButton * mEditLandmarkButton
the Edit Landmark button
PatientModelServicePtr patientModelService
virtual void prePaintEvent()
populates the table widget
boost::shared_ptr< class LandmarkRep > LandmarkRepPtr
virtual void showEvent(QShowEvent *event)
updates internal info before showing the widget
RegistrationServicePtr registrationService
Definition: cxRegServices.h:60
rsIMAGE_REGISTRATED
QTableWidget * mLandmarkTableWidget
the table widget presenting the landmarks