CustusX  2023.01.05-dev+develop.0da12
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"
23 #include "cxTool.h"
24 #include "cxVisServices.h"
26 #include "cxSpaceProvider.h"
27 
28 namespace cx
29 {
30 
31 //------------------------------------------------------------------------------
33  BaseWidget(parent, "tool_tip_calibrate_widget", "ToolTip Calibrate"),
34  mServices(services),
35  mCalibrateButton(new QPushButton("Calibrate")),
36  mReferencePointLabel(new QLabel("Ref. point:")),
37  mTestButton(new QPushButton("Test calibration")),
38  mCalibrationLabel(new QLabel("Calibration: \n")),
39  mDeltaLabel(new QLabel("Delta:"))
40 {
41  QVBoxLayout* toplayout = new QVBoxLayout(this);
42 
43  mTools = StringPropertySelectTool::New(mServices->tracking());
44  mTools->setValueName("Reference tool");
45  mTools->setHelp("Select a tool with a known reference point");
46  mCalibrateToolComboBox = new LabeledComboBoxWidget(this, mTools);
47  this->setToolTip("Calibrate tool position part of sMt matrix");
48 
49  //toplayout->addWidget(new QLabel("<b>Select a tool with a known reference point:</b>"));
50  toplayout->addWidget(mCalibrateToolComboBox);
51  toplayout->addWidget(mReferencePointLabel);
52  toplayout->addWidget(mCalibrateButton);
53  toplayout->addWidget(mCalibrationLabel);
54  toplayout->addWidget(this->createHorizontalLine());
55  toplayout->addWidget(mTestButton);
56  toplayout->addWidget(mDeltaLabel);
57  toplayout->addStretch();
58 
59  connect(mCalibrateButton, SIGNAL(clicked()), this, SLOT(calibrateSlot()));
60  connect(mTestButton, SIGNAL(clicked()), this, SLOT(testCalibrationSlot()));
61 
62  connect(mTools.get(), SIGNAL(changed()), this, SLOT(toolSelectedSlot()));
63  connect(mServices->tracking().get(), &TrackingService::stateChanged, this, &ToolTipCalibrateWidget::onTrackingSystemStateChanged);
64 
65  //setting default state
66  this->toolSelectedSlot();
67 }
68 
70 {}
71 
72 void ToolTipCalibrateWidget::onTrackingSystemStateChanged()
73 {
74  if (mServices->tracking()->getTool(mTools->getValue()))
75  return;
76  if (!mServices->tracking()->getReferenceTool())
77  return;
78 
79  mTools->setValue(mServices->tracking()->getReferenceTool()->getUid());
80 }
81 
82 void ToolTipCalibrateWidget::calibrateSlot()
83 {
84  ToolPtr refTool = mTools->getTool();
85  //Todo, we only allow the reference point with id 1 to be used to calibrate
86  //this could be done more dynamic.
87  if(!refTool || !refTool->hasReferencePointWithId("1"))
88  return;
89 
90  ToolPtr tool = mServices->tracking()->getActiveTool();
91  CoordinateSystem to = mServices->spaceProvider()->getT(tool);
92  Vector3D P_t = mServices->spaceProvider()->getActiveToolTipPoint(to);
93 
94  ToolTipCalibrationCalculator calc(mServices->spaceProvider(), tool, refTool, P_t);
95  Transform3D calibration = calc.get_calibration_sMt();
96 
97  QMessageBox msgBox;
98  msgBox.setText("Do you want to overwrite "+tool->getName()+"s calibration file?");
99  msgBox.setInformativeText("This cannot be undone.");
100  msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
101  msgBox.setDefaultButton(QMessageBox::Ok);
102  int ret = msgBox.exec();
103 
104  if(ret == QMessageBox::Ok)
105  {
106  try
107  {
108  tool->setCalibration_sMt(calibration);
109  }
110  catch(std::exception& e)
111  {
112  QMessageBox msgBox2;
113  msgBox2.setText("Unknown error, could not calibrate the tool: "+tool->getName()+".");
114  msgBox2.setInformativeText(QString(e.what()));
115  msgBox2.setStandardButtons(QMessageBox::Ok);
116  msgBox2.setDefaultButton(QMessageBox::Ok);
117  int ret2 = msgBox2.exec();
118  return;
119  }
120  mCalibrationLabel->setText("Calibration:\n"+qstring_cast(calibration));
121  }
122 }
123 
124 void ToolTipCalibrateWidget::testCalibrationSlot()
125 {
126  ToolPtr selectedTool = mTools->getTool();
127  if(!selectedTool || !selectedTool->hasReferencePointWithId("1"))
128  return;
129 
130  CoordinateSystem to = mServices->spaceProvider()->getT(mServices->tracking()->getActiveTool());
131  Vector3D sampledPoint = mServices->spaceProvider()->getActiveToolTipPoint(to);
132 
133  ToolTipCalibrationCalculator calc(mServices->spaceProvider(), mServices->tracking()->getActiveTool(), selectedTool, sampledPoint);
134  Vector3D delta_selectedTool = calc.get_delta_ref();
135 
136  mDeltaLabel->setText("<b>Delta:</b> "+qstring_cast(delta_selectedTool)+" <br> <b>Length:</b> "+qstring_cast(delta_selectedTool.length()));
137 
138  report("Delta: "+qstring_cast(delta_selectedTool)+" Length: "+qstring_cast(delta_selectedTool.length()));
139 }
140 
141 void ToolTipCalibrateWidget::toolSelectedSlot()
142 {
143  QString text("Ref. point: <UNDEFINED POINT>");
144  mCalibrateButton->setEnabled(false);
145 
146  if(mTools->getTool())
147  {
148  ToolPtr tool = mTools->getTool();
149  if(tool && tool->hasReferencePointWithId("1"))
150  {
151  text = "Ref. point: "+qstring_cast(tool->getReferencePoints()["1"]);
152  mCalibrateButton->setEnabled(true);
153  }
154  else
155  reportWarning("Selected tool have no known reference point");
156  if(tool)
157  {
158  mCalibrationLabel->setText("Calibration:\n"+qstring_cast(tool->getCalibration_sMt()));
159  }
160  }
161 
162  mReferencePointLabel->setText(text);
163 }
164 //------------------------------------------------------------------------------
165 
166 
167 
169  mTool(tool), mRef(ref), mP_t(p_t), mSpaces(spaces)
170 {}
171 
173 {}
174 
176 {
177  return get_referencePoint_ref() - get_sampledPoint_ref();
178 }
179 
181 {
182  return this->get_sMt_new();
183 }
184 
185 Vector3D ToolTipCalibrationCalculator::get_sampledPoint_t()
186 {
187  return mP_t;
188 }
189 
190 Vector3D ToolTipCalibrationCalculator::get_sampledPoint_ref()
191 {
192  CoordinateSystem csT = mSpaces->getT(mTool); //from
193  CoordinateSystem csRef = mSpaces->getT(mRef); //to
194 
195  Transform3D refMt = mSpaces->get_toMfrom(csT, csRef);
196 
197  Vector3D P_ref = refMt.coord(mP_t);
198 
199  return P_ref;
200 }
201 
202 Vector3D ToolTipCalibrationCalculator::get_referencePoint_ref()
203 {
204  return mRef->getReferencePoints()["1"];
205 }
206 
207 Transform3D ToolTipCalibrationCalculator::get_sMt_new()
208 {
209  Transform3D sMt_old = mTool->getCalibration_sMt();
210 
211  CoordinateSystem csT = mSpaces->getT(mTool); //to
212  CoordinateSystem csRef = mSpaces->getT(mRef); //from
213  Transform3D tMref = mSpaces->get_toMfrom(csRef, csT);
214 
215  Vector3D delta_t = tMref.vector(this->get_delta_ref());
216  Transform3D T_delta_t = createTransformTranslate(delta_t);
217 
218  return sMt_old * T_delta_t;
219 }
220 //------------------------------------------------------------------------------
221 //------------------------------------------------------------------------------
222 }//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