CustusX  15.4.0-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, bool useRegistrationFixedPropertyInsteadOfActiveImage) :
64  LandmarkRegistrationWidget(services, parent, objectName, windowTitle),
65  mUseRegistrationFixedPropertyInsteadOfActiveImage(useRegistrationFixedPropertyInsteadOfActiveImage)
66 {
67  if(mUseRegistrationFixedPropertyInsteadOfActiveImage)
69  else
73 
75  connect(mActiveToolProxy.get(), SIGNAL(toolVisible(bool)), this, SLOT(enableButtons()));
76  connect(mActiveToolProxy.get(), SIGNAL(activeToolChanged(const QString&)), this, SLOT(enableButtons()));
77 
78  //pushbuttons
79  mAddLandmarkButton = new QPushButton("Add", this);
80  mAddLandmarkButton->setToolTip("Add landmark");
81  mAddLandmarkButton->setDisabled(true);
82  connect(mAddLandmarkButton, SIGNAL(clicked()), this, SLOT(addLandmarkButtonClickedSlot()));
83 
84  mEditLandmarkButton = new QPushButton("Resample", this);
85  mEditLandmarkButton->setToolTip("Resample existing landmark");
86  mEditLandmarkButton->setDisabled(true);
87  connect(mEditLandmarkButton, SIGNAL(clicked()), this, SLOT(editLandmarkButtonClickedSlot()));
88 
89  mRemoveLandmarkButton = new QPushButton("Clear", this);
90  mRemoveLandmarkButton->setToolTip("Clear selected landmark");
91  mRemoveLandmarkButton->setDisabled(true);
92  connect(mRemoveLandmarkButton, SIGNAL(clicked()), this, SLOT(removeLandmarkButtonClickedSlot()));
93 
94  //layout
98 
99  QHBoxLayout* landmarkButtonsLayout = new QHBoxLayout;
100  landmarkButtonsLayout->addWidget(mAddLandmarkButton);
101  landmarkButtonsLayout->addWidget(mEditLandmarkButton);
102  landmarkButtonsLayout->addWidget(mRemoveLandmarkButton);
103  mVerticalLayout->addLayout(landmarkButtonsLayout);
104 }
105 
107 {
108 }
109 
111 {
112  DataPtr data = mCurrentProperty->getData();
113 
114  mImageLandmarkSource->setData(data);
115  this->enableButtons();
116 
117  if (data && !mServices.registrationService->getFixedData())
118  mServices.registrationService->setFixedData(data);
119 
120  this->setModified();
121 }
122 
124 {
125  return mServices.visualizationService->get3DReps(0, 0)->findFirst<PickerRep>();
126 // return RepContainer(mServices.visualizationService->get3DView(0, 0))->findFirst<PickerRep>();
127 
128 // if (!mServices.visualizationService->get3DView(0, 0))
129 // return PickerRepPtr();
130 
131 // return RepContainer::findFirstRep<PickerRep>(mServices.visualizationService->get3DView(0, 0)->getReps());
132 }
133 
134 DataPtr LandmarkImageRegistrationWidget::getCurrentData() const
135 {
136  return mImageLandmarkSource->getData();
137 }
138 
140 {
142  if (!PickerRep)
143  {
144  reportError("Could not find a rep to add the landmark to.");
145  return;
146  }
147 
148  DataPtr image = this->getCurrentData();
149  if (!image)
150  return;
151 
152  QString uid = mServices.patientModelService->addLandmark();
153  Vector3D pos_r = PickerRep->getPosition();
154  Vector3D pos_d = image->get_rMd().inv().coord(pos_r);
155  image->getLandmarks()->setLandmark(Landmark(uid, pos_d));
156 
157  this->activateLandmark(uid);
158 }
159 
160 
162 {
164  if (!PickerRep)
165  {
166  reportError("Could not find a rep to edit the landmark for.");
167  return;
168  }
169 
170  DataPtr image = this->getCurrentData();
171  if (!image)
172  return;
173 
174  QString uid = mActiveLandmark;
175  Vector3D pos_r = PickerRep->getPosition();
176  Vector3D pos_d = image->get_rMd().inv().coord(pos_r);
177  image->getLandmarks()->setLandmark(Landmark(uid, pos_d));
178 
179  this->activateLandmark(this->getNextLandmark());
180 }
181 
183 {
184  DataPtr image = this->getCurrentData();
185  if (!image)
186  return;
187 
188  QString next = this->getNextLandmark();
189  image->getLandmarks()->removeLandmark(mActiveLandmark);
190  this->activateLandmark(next);
191 }
192 
194 {
196  this->enableButtons();
197 }
198 
200 {
201  bool selected = !mLandmarkTableWidget->selectedItems().isEmpty();
202  bool loaded = this->getCurrentData() != 0;
203 
204  mEditLandmarkButton->setEnabled(selected);
205  mRemoveLandmarkButton->setEnabled(selected);
206  mAddLandmarkButton->setEnabled(loaded);
207 
208  DataPtr image = this->getCurrentData();
209  if (image)
210  {
211  mAddLandmarkButton->setToolTip(QString("Add landmark to image %1").arg(image->getName()));
212  mEditLandmarkButton->setToolTip(QString("Resample landmark in image %1").arg(image->getName()));
213  }
214 // this->setModified();
215 }
216 
218 {
220 
221  if(!mUseRegistrationFixedPropertyInsteadOfActiveImage)
222  {
223  ImagePtr image = mServices.patientModelService->getActiveImage();
224  if (image)
225  mCurrentProperty->setValue(image->getUid());
226  }
227 
228  mServices.visualizationService->getGroup(0)->setRegistrationMode(rsIMAGE_REGISTRATED);
229 
230  LandmarkRepPtr rep = mServices.visualizationService->get3DReps(0, 0)->findFirst<LandmarkRep>();
231  if (rep)
232  {
234  rep->setSecondarySource(LandmarksSourcePtr());
235  }
236 }
237 
239 {
241 
242  if(mServices.visualizationService->get3DView(0, 0))
243  {
244  LandmarkRepPtr rep = mServices.visualizationService->get3DReps(0, 0)->findFirst<LandmarkRep>();
245  if (rep)
246  {
248  rep->setSecondarySource(LandmarksSourcePtr());
249  }
250  }
251  mServices.visualizationService->getGroup(0)->setRegistrationMode(rsNOT_REGISTRATED);
252 }
253 
255 {
257 
258  std::vector<Landmark> landmarks = this->getAllLandmarks();
259 
260  //update buttons
261  mRemoveLandmarkButton->setEnabled(!landmarks.empty() && !mActiveLandmark.isEmpty());
262  mEditLandmarkButton->setEnabled(!landmarks.empty() && !mActiveLandmark.isEmpty());
263 }
264 
266 {
267  DataPtr image = this->getCurrentData();
268  if (!image)
269  return LandmarkMap();
270 
271  return image->getLandmarks()->getLandmarks();
272 }
273 
278 {
279  DataPtr image = this->getCurrentData();
280  if (!image)
281  return Transform3D::Identity();
282  return image->get_rMd();
283 }
284 
286 {
287  DataPtr image = this->getCurrentData();
288  if (!image)
289  return;
290  image->getLandmarks()->setLandmark(Landmark(uid, p_target));
291 }
292 
294 {
295  DataPtr image = this->getCurrentData();
296  if (!image)
297  return "None";
298  return image->getName();
299 }
300 
301 
302 }//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
rsNOT_REGISTRATED
static StringPropertySelectDataPtr New(PatientModelServicePtr patientModelService)
void changed()
emit when the underlying data value is changed: The user interface will be updated.
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