Fraxinus  18.10
An IGT application
cxToolTipCalibrationWidget.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 <QPushButton>
15 #include <QTextStream>
16 #include <QFileDialog>
17 #include <QMessageBox>
18 #include "cxTypeConversions.h"
19 #include "cxLogger.h"
20 #include "cxTrackingService.h"
21 #include "cxVector3D.h"
22 #include "cxDefinitionStrings.h"
24 #include "cxTool.h"
25 #include "cxVisServices.h"
27 #include "cxSpaceProvider.h"
28 
29 namespace cx
30 {
31 
32 //------------------------------------------------------------------------------
34  BaseWidget(parent, "tool_tip_calibrate_widget", "ToolTip Calibrate"),
35  mServices(services),
36  mCalibrateButton(new QPushButton("Calibrate")),
37  mReferencePointLabel(new QLabel("Ref. point:")),
38  mTestButton(new QPushButton("Test calibration")),
39  mCalibrationLabel(new QLabel("Calibration: \n")),
40  mDeltaLabel(new QLabel("Delta:"))
41 {
42  QVBoxLayout* toplayout = new QVBoxLayout(this);
43 
44  mTools = StringPropertySelectTool::New(mServices->tracking());
45  mTools->setValueName("Reference tool");
46  mTools->setHelp("Select a tool with a known reference point");
47  mCalibrateToolComboBox = new LabeledComboBoxWidget(this, mTools);
48  this->setToolTip("Calibrate tool position part of sMt matrix");
49 
50  //toplayout->addWidget(new QLabel("<b>Select a tool with a known reference point:</b>"));
51  toplayout->addWidget(mCalibrateToolComboBox);
52  toplayout->addWidget(mReferencePointLabel);
53  toplayout->addWidget(mCalibrateButton);
54  toplayout->addWidget(mCalibrationLabel);
55  toplayout->addWidget(this->createHorizontalLine());
56  toplayout->addWidget(mTestButton);
57  toplayout->addWidget(mDeltaLabel);
58  toplayout->addStretch();
59 
60  connect(mCalibrateButton, SIGNAL(clicked()), this, SLOT(calibrateSlot()));
61  connect(mTestButton, SIGNAL(clicked()), this, SLOT(testCalibrationSlot()));
62 
63  connect(mTools.get(), SIGNAL(changed()), this, SLOT(toolSelectedSlot()));
64  connect(mServices->tracking().get(), &TrackingService::stateChanged, this, &ToolTipCalibrateWidget::onTrackingSystemStateChanged);
65 
66  //setting default state
67  this->toolSelectedSlot();
68 }
69 
71 {}
72 
73 void ToolTipCalibrateWidget::onTrackingSystemStateChanged()
74 {
75  if (mServices->tracking()->getTool(mTools->getValue()))
76  return;
77  if (!mServices->tracking()->getReferenceTool())
78  return;
79 
80  mTools->setValue(mServices->tracking()->getReferenceTool()->getUid());
81 }
82 
83 void ToolTipCalibrateWidget::calibrateSlot()
84 {
85  ToolPtr refTool = mTools->getTool();
86  //Todo, we only allow the reference point with id 1 to be used to calibrate
87  //this could be done more dynamic.
88  if(!refTool || !refTool->hasReferencePointWithId(1))
89  return;
90 
91  ToolPtr tool = mServices->tracking()->getActiveTool();
92  CoordinateSystem to = mServices->spaceProvider()->getT(tool);
93  Vector3D P_t = mServices->spaceProvider()->getActiveToolTipPoint(to);
94 
95  ToolTipCalibrationCalculator calc(mServices->spaceProvider(), tool, refTool, P_t);
96  Transform3D calibration = calc.get_calibration_sMt();
97 
98  QMessageBox msgBox;
99  msgBox.setText("Do you want to overwrite "+tool->getName()+"s calibration file?");
100  msgBox.setInformativeText("This cannot be undone.");
101  msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
102  msgBox.setDefaultButton(QMessageBox::Ok);
103  int ret = msgBox.exec();
104 
105  if(ret == QMessageBox::Ok)
106  {
107  try
108  {
109  tool->setCalibration_sMt(calibration);
110  }
111  catch(std::exception& e)
112  {
113  QMessageBox msgBox2;
114  msgBox2.setText("Unknown error, could not calibrate the tool: "+tool->getName()+".");
115  msgBox2.setInformativeText(QString(e.what()));
116  msgBox2.setStandardButtons(QMessageBox::Ok);
117  msgBox2.setDefaultButton(QMessageBox::Ok);
118  int ret2 = msgBox2.exec();
119  return;
120  }
121  mCalibrationLabel->setText("Calibration:\n"+qstring_cast(calibration));
122  }
123 }
124 
125 void ToolTipCalibrateWidget::testCalibrationSlot()
126 {
127  ToolPtr selectedTool = mTools->getTool();
128  if(!selectedTool || !selectedTool->hasReferencePointWithId(1))
129  return;
130 
131  CoordinateSystem to = mServices->spaceProvider()->getT(mServices->tracking()->getActiveTool());
132  Vector3D sampledPoint = mServices->spaceProvider()->getActiveToolTipPoint(to);
133 
134  ToolTipCalibrationCalculator calc(mServices->spaceProvider(), mServices->tracking()->getActiveTool(), selectedTool, sampledPoint);
135  Vector3D delta_selectedTool = calc.get_delta_ref();
136 
137  mDeltaLabel->setText("<b>Delta:</b> "+qstring_cast(delta_selectedTool)+" <br> <b>Length:</b> "+qstring_cast(delta_selectedTool.length()));
138 
139  report("Delta: "+qstring_cast(delta_selectedTool)+" Length: "+qstring_cast(delta_selectedTool.length()));
140 }
141 
142 void ToolTipCalibrateWidget::toolSelectedSlot()
143 {
144  QString text("Ref. point: <UNDEFINED POINT>");
145  mCalibrateButton->setEnabled(false);
146 
147  if(mTools->getTool())
148  {
149  ToolPtr tool = mTools->getTool();
150  if(tool && tool->hasReferencePointWithId(1))
151  {
152  text = "Ref. point: "+qstring_cast(tool->getReferencePoints()[1]);
153  mCalibrateButton->setEnabled(true);
154  }
155  else
156  reportWarning("Selected tool have no known reference point");
157  if(tool)
158  {
159  mCalibrationLabel->setText("Calibration:\n"+qstring_cast(tool->getCalibration_sMt()));
160  }
161  }
162 
163  mReferencePointLabel->setText(text);
164 }
165  //------------------------------------------------------------------------------
166 
167 
168 
170  mTool(tool), mRef(ref), mP_t(p_t), mSpaces(spaces)
171 {}
172 
174 {}
175 
177 {
178  return get_referencePoint_ref() - get_sampledPoint_ref();
179 }
180 
182 {
183  return this->get_sMt_new();
184 }
185 
186 Vector3D ToolTipCalibrationCalculator::get_sampledPoint_t()
187 {
188  return mP_t;
189 }
190 
191 Vector3D ToolTipCalibrationCalculator::get_sampledPoint_ref()
192 {
193  CoordinateSystem csT = mSpaces->getT(mTool); //from
194  CoordinateSystem csRef = mSpaces->getT(mRef); //to
195 
196  Transform3D refMt = mSpaces->get_toMfrom(csT, csRef);
197 
198  Vector3D P_ref = refMt.coord(mP_t);
199 
200  return P_ref;
201 }
202 
203 Vector3D ToolTipCalibrationCalculator::get_referencePoint_ref()
204 {
205  return mRef->getReferencePoints()[1];
206 }
207 
208 Transform3D ToolTipCalibrationCalculator::get_sMt_new()
209 {
210  Transform3D sMt_old = mTool->getCalibration_sMt();
211 
212  CoordinateSystem csT = mSpaces->getT(mTool); //to
213  CoordinateSystem csRef = mSpaces->getT(mRef); //from
214  Transform3D tMref = mSpaces->get_toMfrom(csRef, csT);
215 
216  Vector3D delta_t = tMref.vector(this->get_delta_ref());
217  Transform3D T_delta_t = createTransformTranslate(delta_t);
218 
219  return sMt_old * T_delta_t;
220 }
221 //------------------------------------------------------------------------------
222 //------------------------------------------------------------------------------
223 }//namespace cx
boost::shared_ptr< class SpaceProvider > SpaceProviderPtr
QString qstring_cast(const T &val)
ToolTipCalibrateWidget(VisServicesPtr services, QWidget *parent)
boost::shared_ptr< class VisServices > VisServicesPtr
Definition: cxMainWindow.h:40
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
Vector3D get_delta_ref()
how far from the reference point the sampled point is, in pr&#39;s coord
Composite widget for string selection.
static QFrame * createHorizontalLine()
Creates a horizontal line which can be inserted into widgets.
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
ToolTipCalibrationCalculator(SpaceProviderPtr spaces, ToolPtr tool, ToolPtr ref, Vector3D p_t=Vector3D())
Transform3D createTransformTranslate(const Vector3D &translation)
Identification of a Coordinate system.
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:42
Interface for QWidget which handles widgets uniformly for the system.
Definition: cxBaseWidget.h:88
static StringPropertySelectToolPtr New(TrackingServicePtr trackingService)
void report(QString msg)
Definition: cxLogger.cpp:69
Namespace for all CustusX production code.
boost::shared_ptr< class Tool > ToolPtr