CustusX  15.4.0-beta
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->getToolManager());
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 
86  //setting default state
87  this->toolSelectedSlot();
88 }
89 
91 {}
92 
93 void ToolTipCalibrateWidget::calibrateSlot()
94 {
95  ToolPtr refTool = mTools->getTool();
96  //Todo, we only allow the reference point with id 1 to be used to calibrate
97  //this could be done more dynamic.
98  if(!refTool || !refTool->hasReferencePointWithId(1))
99  return;
100 
101  ToolPtr tool = mServices->getToolManager()->getActiveTool();
102  CoordinateSystem to = mServices->getSpaceProvider()->getT(tool);
103  Vector3D P_t = mServices->getSpaceProvider()->getActiveToolTipPoint(to);
104 
105  ToolTipCalibrationCalculator calc(mServices->getSpaceProvider(), tool, refTool, P_t);
106  Transform3D calibration = calc.get_calibration_sMt();
107 
108  QMessageBox msgBox;
109  msgBox.setText("Do you want to overwrite "+tool->getName()+"s calibration file?");
110  msgBox.setInformativeText("This cannot be undone.");
111  msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
112  msgBox.setDefaultButton(QMessageBox::Ok);
113  int ret = msgBox.exec();
114 
115  if(ret == QMessageBox::Ok)
116  {
117  tool->setCalibration_sMt(calibration);
118  mCalibrationLabel->setText("Calibration:\n"+qstring_cast(calibration));
119  }
120 }
121 
122 void ToolTipCalibrateWidget::testCalibrationSlot()
123 {
124  ToolPtr selectedTool = mTools->getTool();
125  if(!selectedTool || !selectedTool->hasReferencePointWithId(1))
126  return;
127 
128  CoordinateSystem to = mServices->getSpaceProvider()->getT(mServices->getToolManager()->getActiveTool());
129  Vector3D sampledPoint = mServices->getSpaceProvider()->getActiveToolTipPoint(to);
130 
131  ToolTipCalibrationCalculator calc(mServices->getSpaceProvider(), mServices->getToolManager()->getActiveTool(), selectedTool, sampledPoint);
132  Vector3D delta_selectedTool = calc.get_delta_ref();
133 
134  mDeltaLabel->setText("<b>Delta:</b> "+qstring_cast(delta_selectedTool)+" <br> <b>Length:</b> "+qstring_cast(delta_selectedTool.length()));
135 
136  report("Delta: "+qstring_cast(delta_selectedTool)+" Length: "+qstring_cast(delta_selectedTool.length()));
137 }
138 
139 void ToolTipCalibrateWidget::toolSelectedSlot()
140 {
141  QString text("Ref. point: <UNDEFINED POINT>");
142  mCalibrateButton->setEnabled(false);
143 
144  if(mTools->getTool())
145  {
146  ToolPtr tool = mTools->getTool();
147  if(tool && tool->hasReferencePointWithId(1))
148  {
149  text = "Ref. point: "+qstring_cast(tool->getReferencePoints()[1]);
150  mCalibrateButton->setEnabled(true);
151  }
152  else
153  reportWarning("Selected tool have no known reference point");
154  if(tool)
155  {
156  mCalibrationLabel->setText("Calibration:\n"+qstring_cast(tool->getCalibration_sMt()));
157  }
158  }
159 
160  mReferencePointLabel->setText(text);
161 }
162  //------------------------------------------------------------------------------
163 
164 
165 
167  mTool(tool), mRef(ref), mP_t(p_t), mSpaces(spaces)
168 {}
169 
171 {}
172 
174 {
175  return get_referencePoint_ref() - get_sampledPoint_ref();
176 }
177 
179 {
180  return this->get_sMt_new();
181 }
182 
183 Vector3D ToolTipCalibrationCalculator::get_sampledPoint_t()
184 {
185  return mP_t;
186 }
187 
188 Vector3D ToolTipCalibrationCalculator::get_sampledPoint_ref()
189 {
190  CoordinateSystem csT = mSpaces->getT(mTool); //from
191  CoordinateSystem csRef = mSpaces->getT(mRef); //to
192 
193  Transform3D refMt = mSpaces->get_toMfrom(csT, csRef);
194 
195  Vector3D P_ref = refMt.coord(mP_t);
196 
197  return P_ref;
198 }
199 
200 Vector3D ToolTipCalibrationCalculator::get_referencePoint_ref()
201 {
202  return mRef->getReferencePoints()[1];
203 }
204 
205 Transform3D ToolTipCalibrationCalculator::get_sMt_new()
206 {
207  Transform3D sMt_old = mTool->getCalibration_sMt();
208 
209  CoordinateSystem csT = mSpaces->getT(mTool); //to
210  CoordinateSystem csRef = mSpaces->getT(mRef); //from
211  Transform3D tMref = mSpaces->get_toMfrom(csRef, csT);
212 
213  Vector3D delta_t = tMref.vector(this->get_delta_ref());
214  Transform3D T_delta_t = createTransformTranslate(delta_t);
215 
216  return sMt_old * T_delta_t;
217 }
218 //------------------------------------------------------------------------------
219 //------------------------------------------------------------------------------
220 }//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)
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:108
static StringPropertySelectToolPtr New(TrackingServicePtr trackingService)
void report(QString msg)
Definition: cxLogger.cpp:90
boost::shared_ptr< class Tool > ToolPtr