CustusX  15.3.4-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(this->defaultWhatsThis());
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 
94 {
95  return "<html>"
96  "<h3>Tool tip calibration.</h3>"
97  "<p><i>Calibrates a tool by sampling it when pointing at a known point on another frame.</i></br>"
98  "By using the test button you can test your calibration by pointing at a known reference point.</br></p>"
99  "</html>";
100 }
101 
102 void ToolTipCalibrateWidget::calibrateSlot()
103 {
104  ToolPtr refTool = mTools->getTool();
105  //Todo, we only allow the reference point with id 1 to be used to calibrate
106  //this could be done more dynamic.
107  if(!refTool || !refTool->hasReferencePointWithId(1))
108  return;
109 
110  ToolPtr tool = mServices->getToolManager()->getActiveTool();
111  CoordinateSystem to = mServices->getSpaceProvider()->getT(tool);
112  Vector3D P_t = mServices->getSpaceProvider()->getActiveToolTipPoint(to);
113 
114  ToolTipCalibrationCalculator calc(mServices->getSpaceProvider(), tool, refTool, P_t);
115  Transform3D calibration = calc.get_calibration_sMt();
116 
117  QMessageBox msgBox;
118  msgBox.setText("Do you want to overwrite "+tool->getName()+"s calibration file?");
119  msgBox.setInformativeText("This cannot be undone.");
120  msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
121  msgBox.setDefaultButton(QMessageBox::Ok);
122  int ret = msgBox.exec();
123 
124  if(ret == QMessageBox::Ok)
125  {
126  tool->setCalibration_sMt(calibration);
127  mCalibrationLabel->setText("Calibration:\n"+qstring_cast(calibration));
128  }
129 }
130 
131 void ToolTipCalibrateWidget::testCalibrationSlot()
132 {
133  ToolPtr selectedTool = mTools->getTool();
134  if(!selectedTool || !selectedTool->hasReferencePointWithId(1))
135  return;
136 
137  CoordinateSystem to = mServices->getSpaceProvider()->getT(mServices->getToolManager()->getActiveTool());
138  Vector3D sampledPoint = mServices->getSpaceProvider()->getActiveToolTipPoint(to);
139 
140  ToolTipCalibrationCalculator calc(mServices->getSpaceProvider(), mServices->getToolManager()->getActiveTool(), selectedTool, sampledPoint);
141  Vector3D delta_selectedTool = calc.get_delta_ref();
142 
143  mDeltaLabel->setText("<b>Delta:</b> "+qstring_cast(delta_selectedTool)+" <br> <b>Length:</b> "+qstring_cast(delta_selectedTool.length()));
144 
145  report("Delta: "+qstring_cast(delta_selectedTool)+" Length: "+qstring_cast(delta_selectedTool.length()));
146 }
147 
148 void ToolTipCalibrateWidget::toolSelectedSlot()
149 {
150  QString text("Ref. point: <UNDEFINED POINT>");
151  mCalibrateButton->setEnabled(false);
152 // mTestButton->setEnabled(false);
153 
154  if(mTools->getTool())
155  {
156  ToolPtr tool = mTools->getTool();
157  if(tool && tool->hasReferencePointWithId(1))
158  {
159  text = "Ref. point: "+qstring_cast(tool->getReferencePoints()[1]);
160  mCalibrateButton->setEnabled(true);
161 // mTestButton->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: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's coord
Composite widget for string selection.
virtual QString defaultWhatsThis() const
Returns a short description of what this widget will do for you.
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