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