CustusX  15.3.4-beta
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxLandmarkPatientRegistrationWidget.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 <QVBoxLayout>
36 #include <QPushButton>
37 #include <QTableWidget>
38 #include <QTableWidgetItem>
39 #include <QHeaderView>
40 #include <QLabel>
41 #include <QSlider>
42 #include <QGridLayout>
43 #include <QSpinBox>
44 #include <vtkDoubleArray.h>
45 #include "cxVector3D.h"
46 #include "cxLogger.h"
47 #include "cxTypeConversions.h"
49 #include "cxLandmarkRep.h"
50 #include "cxView.h"
51 #include "cxRegistrationService.h"
52 #include "cxViewService.h"
53 #include "cxPatientModelService.h"
54 #include "cxViewGroupData.h"
55 #include "cxTrackingService.h"
56 #include "cxRepContainer.h"
57 #include "cxReporter.h"
58 #include "cxSettings.h"
59 
60 namespace cx
61 {
62 LandmarkPatientRegistrationWidget::LandmarkPatientRegistrationWidget(RegServices services,
63  QWidget* parent, QString objectName, QString windowTitle) :
64  LandmarkRegistrationWidget(services, parent, objectName, windowTitle), mToolSampleButton(new QPushButton(
65  "Sample Tool", this))
66 {
72 
73  //buttons
74  mToolSampleButton->setDisabled(true);
75  connect(mToolSampleButton, SIGNAL(clicked()), this, SLOT(toolSampleButtonClickedSlot()));
76 
77  mRemoveLandmarkButton = new QPushButton("Clear", this);
78  mRemoveLandmarkButton->setToolTip("Clear selected landmark");
79  // mRemoveLandmarkButton->setDisabled(true);
80  connect(mRemoveLandmarkButton, SIGNAL(clicked()), this, SLOT(removeLandmarkButtonClickedSlot()));
81 
82  mRegisterButton = new QPushButton("Register", this);
83  mRegisterButton->setToolTip("Perform registration");
84  connect(mRegisterButton, SIGNAL(clicked()), this, SLOT(registerSlot()));
85 
86  //toolmanager
88  connect(mActiveToolProxy.get(), SIGNAL(toolVisible(bool)), this, SLOT(updateToolSampleButton()));
89  connect(mActiveToolProxy.get(), SIGNAL(activeToolChanged(const QString&)), this, SLOT(updateToolSampleButton()));
90 
91  connect(settings(), &Settings::valueChangedFor, this, &LandmarkPatientRegistrationWidget::globalConfigurationFileChangedSlot);
92 
93  //layout
98 
99  QHBoxLayout* buttonsLayout = new QHBoxLayout;
100  buttonsLayout->addWidget(mRegisterButton);
101  buttonsLayout->addWidget(mRemoveLandmarkButton);
102  mVerticalLayout->addLayout(buttonsLayout);
103 
104  this->updateToolSampleButton();
105 }
106 
108 {
109 }
110 
112 {
113  return "<html>"
114  "<h3>Landmark based patient registration.</h3>"
115  "<p>Sample points on the patient that corresponds to 3 or more landmarks already sampled in the data set. </p>"
116  "<p><i>Point on the patient using a tool and click the Sample button.</i></p>"
117  "<p>Landmark patient registration will move the patient into the global coordinate system (r).</p>"
118  "</html>";
119 }
120 
121 void LandmarkPatientRegistrationWidget::globalConfigurationFileChangedSlot(QString key)
122 {
123  if (key == "giveManualToolPhysicalProperties")
124  this->updateToolSampleButton();
125 }
126 
128 {
129  this->performRegistration();
130 }
131 
133 {
134  mImageLandmarkSource->setData(mServices.registrationService->getFixedData());
135 }
136 
138 {
139  ToolPtr tool = mServices.trackingService->getActiveTool();
140 
141  bool enabled = false;
142  enabled = tool && tool->getVisible() && (!tool->hasType(Tool::TOOL_MANUAL) || settings()->value("giveManualToolPhysicalProperties").toBool()); // enable only for non-manual tools.
143  mToolSampleButton->setEnabled(enabled);
144 
145  if (mServices.trackingService->getActiveTool())
146  mToolSampleButton->setText("Sample " + qstring_cast(tool->getName()));
147  else
148  mToolSampleButton->setText("No tool");
149 }
150 
152 {
153  ToolPtr tool = mServices.trackingService->getActiveTool();
154 
155  if (!tool)
156  {
157  reportError("mToolToSample is NULL!");
158  return;
159  }
160  //TODO What if the reference frame isnt visible?
161  Transform3D lastTransform_prMt = tool->get_prMt();
162  Vector3D p_pr = lastTransform_prMt.coord(Vector3D(0, 0, tool->getTooltipOffset()));
163 
164  // TODO: do we want to allow sampling points not defined in image??
165  if (mActiveLandmark.isEmpty() && !mServices.patientModelService->getLandmarkProperties().empty())
166  mActiveLandmark = mServices.patientModelService->getLandmarkProperties().begin()->first;
167 
168  mServices.patientModelService->getPatientLandmarks()->setLandmark(Landmark(mActiveLandmark, p_pr));
169  reporter()->playSampleSound();
170 
171  this->activateLandmark(this->getNextLandmark());
172 
173  this->performRegistration(); // automatic when sampling in physical patient space (Mantis #0000674)s
174 }
175 
177 {
178 // std::cout << "LandmarkPatientRegistrationWidget::showEvent" << std::endl;
180  connect(mServices.patientModelService->getPatientLandmarks().get(), SIGNAL(landmarkAdded(QString)), this, SLOT(landmarkUpdatedSlot()));
181  connect(mServices.patientModelService->getPatientLandmarks().get(), SIGNAL(landmarkRemoved(QString)), this, SLOT(landmarkUpdatedSlot()));
182 
183  mServices.visualizationService->getGroup(0)->setRegistrationMode(rsPATIENT_REGISTRATED);
184 
185  LandmarkRepPtr rep = mServices.visualizationService->get3DReps(0, 0)->findFirst<LandmarkRep>();
186  if (rep)
187  {
190  rep->setSecondaryColor(QColor::fromRgbF(0, 0.6, 0.8));
191  }
192 }
193 
195 {
196 // std::cout << "LandmarkPatientRegistrationWidget::hideEvent" << std::endl;
198  disconnect(mServices.patientModelService->getPatientLandmarks().get(), SIGNAL(landmarkAdded(QString)), this, SLOT(landmarkUpdatedSlot()));
199  disconnect(mServices.patientModelService->getPatientLandmarks().get(), SIGNAL(landmarkRemoved(QString)), this, SLOT(landmarkUpdatedSlot()));
200 
201  if(mServices.visualizationService->get3DView(0, 0))
202  {
203  LandmarkRepPtr rep = mServices.visualizationService->get3DReps(0, 0)->findFirst<LandmarkRep>();
204  if (rep)
205  {
207  rep->setSecondarySource(LandmarksSourcePtr());
208  }
209  }
210  mServices.visualizationService->getGroup(0)->setRegistrationMode(rsNOT_REGISTRATED);
211 }
212 
214 {
215  QString next = this->getNextLandmark();
216  mServices.patientModelService->getPatientLandmarks()->removeLandmark(mActiveLandmark);
217  this->activateLandmark(next);
218 }
219 
221 {
223 
224  mRemoveLandmarkButton->setEnabled(true);
225 }
226 
228 {
230 
231  std::vector<Landmark> landmarks = this->getAllLandmarks();
232  mRemoveLandmarkButton->setEnabled(!landmarks.empty() && !mActiveLandmark.isEmpty());
233 }
234 
238 {
239  return mServices.patientModelService->getPatientLandmarks()->getLandmarks();
240 }
241 
246 {
247  Transform3D rMpr = mServices.patientModelService->get_rMpr();
248  return rMpr;
249 }
250 
252 {
253  mServices.patientModelService->getPatientLandmarks()->setLandmark(Landmark(uid, p_target));
254  reporter()->playSampleSound();
255 }
256 
258 {
259  if (!mServices.registrationService->getFixedData())
260  mServices.registrationService->setFixedData(mServices.patientModelService->getActiveImage());
261 
262  if (mServices.patientModelService->getPatientLandmarks()->getLandmarks().size() < 3)
263  return;
264 
265  mServices.registrationService->doPatientRegistration();
266 
268 }
269 
271 {
272  return "Patient";
273 }
274 
275 }//namespace cx
QString qstring_cast(const T &val)
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
ReporterPtr reporter()
Definition: cxReporter.cpp:59
virtual void prePaintEvent()
populates the table widget
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.
StringPropertyRegistrationFixedImagePtr mFixedProperty
virtual QString defaultWhatsThis() const
Returns a short description of what this widget will do for you.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
Definition: cxSettings.cpp:99
void toolSampleButtonClickedSlot()
reacts when the Sample Tool button is clicked
Composite widget for string selection.
QVBoxLayout * mVerticalLayout
vertical layout is used
void fixedDataChanged(QString uid)
virtual void hideEvent(QHideEvent *event)
static ActiveToolProxyPtr New(TrackingServicePtr trackingService)
void valueChangedFor(QString key)
static PatientLandmarksSourcePtr New(PatientModelServicePtr dataManager)
Definition: cxLandmarkRep.h:79
virtual void cellClickedSlot(int row, int column)
when a landmark i selected from the table
void setPrimarySource(LandmarksSourcePtr primary)
virtual void cellClickedSlot(int row, int column)
when a landmark i selected from the table
virtual void prePaintEvent()
populates the table widget
Representation of a mouse/keyboard-controlled virtual tool.
Definition: cxTool.h:91
rsNOT_REGISTRATED
Settings * settings()
Shortcut for accessing the settings instance.
Definition: cxSettings.cpp:42
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
VisualizationServicePtr visualizationService
Definition: cxVisServices.h:60
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 * mToolSampleButton
the Sample Tool button
virtual void setTargetLandmark(QString uid, Vector3D p_target)
PatientModelServicePtr patientModelService
virtual void showEvent(QShowEvent *event)
updates internal info before showing the widget
virtual void fixedDataChanged()
listens to the datamanager for when the active image is changed
boost::shared_ptr< class LandmarkRep > LandmarkRepPtr
RegistrationServicePtr registrationService
Definition: cxRegServices.h:60
QTableWidget * mLandmarkTableWidget
the table widget presenting the landmarks
boost::shared_ptr< class Tool > ToolPtr