NorMIT-nav  16.5
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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, "ToolTipCalibrateWidget", "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  tool->setCalibration_sMt(calibration);
129  mCalibrationLabel->setText("Calibration:\n"+qstring_cast(calibration));
130  }
131 }
132 
133 void ToolTipCalibrateWidget::testCalibrationSlot()
134 {
135  ToolPtr selectedTool = mTools->getTool();
136  if(!selectedTool || !selectedTool->hasReferencePointWithId(1))
137  return;
138 
139  CoordinateSystem to = mServices->spaceProvider()->getT(mServices->tracking()->getActiveTool());
140  Vector3D sampledPoint = mServices->spaceProvider()->getActiveToolTipPoint(to);
141 
142  ToolTipCalibrationCalculator calc(mServices->spaceProvider(), mServices->tracking()->getActiveTool(), selectedTool, sampledPoint);
143  Vector3D delta_selectedTool = calc.get_delta_ref();
144 
145  mDeltaLabel->setText("<b>Delta:</b> "+qstring_cast(delta_selectedTool)+" <br> <b>Length:</b> "+qstring_cast(delta_selectedTool.length()));
146 
147  report("Delta: "+qstring_cast(delta_selectedTool)+" Length: "+qstring_cast(delta_selectedTool.length()));
148 }
149 
150 void ToolTipCalibrateWidget::toolSelectedSlot()
151 {
152  QString text("Ref. point: <UNDEFINED POINT>");
153  mCalibrateButton->setEnabled(false);
154 
155  if(mTools->getTool())
156  {
157  ToolPtr tool = mTools->getTool();
158  if(tool && tool->hasReferencePointWithId(1))
159  {
160  text = "Ref. point: "+qstring_cast(tool->getReferencePoints()[1]);
161  mCalibrateButton->setEnabled(true);
162  }
163  else
164  reportWarning("Selected tool have no known reference point");
165  if(tool)
166  {
167  mCalibrationLabel->setText("Calibration:\n"+qstring_cast(tool->getCalibration_sMt()));
168  }
169  }
170 
171  mReferencePointLabel->setText(text);
172 }
173  //------------------------------------------------------------------------------
174 
175 
176 
178  mTool(tool), mRef(ref), mP_t(p_t), mSpaces(spaces)
179 {}
180 
182 {}
183 
185 {
186  return get_referencePoint_ref() - get_sampledPoint_ref();
187 }
188 
190 {
191  return this->get_sMt_new();
192 }
193 
194 Vector3D ToolTipCalibrationCalculator::get_sampledPoint_t()
195 {
196  return mP_t;
197 }
198 
199 Vector3D ToolTipCalibrationCalculator::get_sampledPoint_ref()
200 {
201  CoordinateSystem csT = mSpaces->getT(mTool); //from
202  CoordinateSystem csRef = mSpaces->getT(mRef); //to
203 
204  Transform3D refMt = mSpaces->get_toMfrom(csT, csRef);
205 
206  Vector3D P_ref = refMt.coord(mP_t);
207 
208  return P_ref;
209 }
210 
211 Vector3D ToolTipCalibrationCalculator::get_referencePoint_ref()
212 {
213  return mRef->getReferencePoints()[1];
214 }
215 
216 Transform3D ToolTipCalibrationCalculator::get_sMt_new()
217 {
218  Transform3D sMt_old = mTool->getCalibration_sMt();
219 
220  CoordinateSystem csT = mSpaces->getT(mTool); //to
221  CoordinateSystem csRef = mSpaces->getT(mRef); //from
222  Transform3D tMref = mSpaces->get_toMfrom(csRef, csT);
223 
224  Vector3D delta_t = tMref.vector(this->get_delta_ref());
225  Transform3D T_delta_t = createTransformTranslate(delta_t);
226 
227  return sMt_old * T_delta_t;
228 }
229 //------------------------------------------------------------------------------
230 //------------------------------------------------------------------------------
231 }//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:62
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'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)
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:108
static StringPropertySelectToolPtr New(TrackingServicePtr trackingService)
void report(QString msg)
Definition: cxLogger.cpp:90
boost::shared_ptr< class Tool > ToolPtr