Fraxinus  17.12
An IGT application
cxManualImage2ImageRegistrationWidget.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 #include "cxRegistrationService.h"
35 #include "cxData.h"
36 #include "cxImage.h"
39 #include "cxPatientModelService.h"
41 #include "cxLandmark.h"
42 
43 #include "vtkMath.h"
44 
45 namespace cx
46 {
47 
48 
50  ManualImageRegistrationWidget(services, parent, objectName, "Manual Image to Image Registration")
51 {
52  StringPropertyBasePtr fixedImage(new StringPropertyRegistrationFixedImage(services->registration(), services->patient()));
53  StringPropertyBasePtr movingImage(new StringPropertyRegistrationMovingImage(services->registration(), services->patient()));
54 
55  LabeledComboBoxWidget* fixed = new LabeledComboBoxWidget(this, fixedImage);
56  LabeledComboBoxWidget* moving = new LabeledComboBoxWidget(this, movingImage);
57 
58  mAvarageAccuracyLabel = new QLabel(QString(" "), this);
59 
60  mVerticalLayout->insertWidget(0, fixed);
61  mVerticalLayout->insertWidget(1, moving);
62  mVerticalLayout->insertWidget(2, mAvarageAccuracyLabel);
63 }
64 
66 {
67  if (this->isValid())
68  return QString("<b>Matrix fMm from moving to fixed image</b>");
69  else
70  return "<Invalid matrix>";
71 }
72 
74 {
75  return mServices->registration()->getMovingData() && mServices->registration()->getFixedData();
76 }
77 
79 {
80  if (!this->isValid()) // Check if fixed and moving data are defined
81  return Transform3D::Identity();
82 
83  Transform3D rMm = mServices->registration()->getMovingData()->get_rMd();
84  Transform3D rMf = mServices->registration()->getFixedData()->get_rMd();
85  Transform3D fMm = rMf.inv() * rMm;
86 
87  RegistrationHistoryPtr history = mServices->registration()->getMovingData()->get_rMd_History();
88  Transform3D init_rMd;
89  if(!history->getData().empty()) // Is vector with RegistrationTransforms empty ?
90  init_rMd = history->getData().front().mValue;
91  else
92  init_rMd = Transform3D::Identity();
93 
94  Transform3D current_rMd = history->getCurrentRegistration().mValue;
95  fMm = current_rMd * init_rMd.inv();
96 
97  return fMm;
98 }
99 
101 {
102  if (!this->isValid()) // Check if fixed and moving data are defined
103  return;
104 
105  Transform3D rMm = mServices->registration()->getMovingData()->get_rMd();
106 
107  RegistrationHistoryPtr history = mServices->registration()->getMovingData()->get_rMd_History();
108 
109  Transform3D init_rMd;
110 
111  if(!history->getData().empty()) // Is vector with RegistrationTransforms empty ?
112  init_rMd = history->getData().front().mValue;
113  else
114  init_rMd = Transform3D::Identity();
115 
116  Transform3D new_rMd = M * init_rMd;
117  Transform3D delta = new_rMd * rMm.inv();
118 
119  mServices->registration()->addImage2ImageRegistration(delta, "Manual Image");
120  this->updateAverageAccuracyLabel();
121 
122 }
123 
125 {
126  std::map<QString, LandmarkProperty> props = mServices->patient()->getLandmarkProperties();
127 
128  double sum = 0;
129  numActiveLandmarks = 0;
130  std::map<QString, LandmarkProperty>::iterator it = props.begin();
131  for (; it != props.end(); ++it)
132  {
133  if (!it->second.getActive()) //we don't want to take into account not active landmarks
134  continue;
135  QString uid = it->first;
136  double val = this->getAccuracy(uid);
137  if (!similar(val, 1000.0))
138  {
139  sum = sum + val;
140  numActiveLandmarks++;
141  }
142  }
143  if (numActiveLandmarks == 0)
144  return 1000.0;
145  return (sqrt(sum / (double)numActiveLandmarks));
146 }
147 
148 double ManualImage2ImageRegistrationWidget::getAccuracy(QString uid)
149 {
150  DataPtr fixedData = mServices->registration()->getFixedData();
151  if (!fixedData)
152  return 1000.0;
153  DataPtr movingData = mServices->registration()->getMovingData();
154  if (!movingData)
155  return 1000.0;
156 
157  Landmark masterLandmark = fixedData->getLandmarks()->getLandmarks()[uid];
158  Landmark targetLandmark = movingData->getLandmarks()->getLandmarks()[uid];
159  if (masterLandmark.getUid().isEmpty() || targetLandmark.getUid().isEmpty())
160  return 1000.0;
161 
162  Vector3D p_master_master = masterLandmark.getCoord();
163  Vector3D p_target_target = targetLandmark.getCoord();
164  Transform3D rMmaster = fixedData->get_rMd();
165  Transform3D rMtarget = movingData->get_rMd();
166 
167  Vector3D p_target_r = rMtarget.coord(p_target_target);
168  Vector3D p_master_r = rMmaster.coord(p_master_master);
169 
170  double targetPoint[3];
171  double masterPoint[3];
172  targetPoint[0] = p_target_r[0];
173  targetPoint[1] = p_target_r[1];
174  targetPoint[2] = p_target_r[2];
175  masterPoint[0] = p_master_r[0];
176  masterPoint[1] = p_master_r[1];
177  masterPoint[2] = p_master_r[2];
178 
179  return (vtkMath::Distance2BetweenPoints(targetPoint, masterPoint));
180 }
181 
182 void ManualImage2ImageRegistrationWidget::updateAverageAccuracyLabel()
183 {
184  QString fixedName;
185  QString movingName;
186  DataPtr fixedData = boost::dynamic_pointer_cast<Data>(mServices->registration()->getFixedData());
187  DataPtr movingData = boost::dynamic_pointer_cast<Data>(mServices->registration()->getMovingData());
188  if (fixedData)
189  fixedName = fixedData->getName();
190  if (movingData)
191  movingName = movingData->getName();
192 
193  int numberOfActiveLandmarks = 0;
194  if(this->isAverageAccuracyValid(numberOfActiveLandmarks))
195  {
196  mAvarageAccuracyLabel->setText(tr("Root mean square accuracy (Landmarks) %1 mm, calculated in %2 landmarks").
197  arg(this->getAverageAccuracy(numberOfActiveLandmarks), 0, 'f', 2).arg(numberOfActiveLandmarks));
198  mAvarageAccuracyLabel->setToolTip(QString("Root Mean Square landmark accuracy from target [%1] to fixed [%2].").
199  arg(movingName).arg(fixedName));
200  }
201  else
202  {
203  mAvarageAccuracyLabel->setText(" ");
204  mAvarageAccuracyLabel->setToolTip("");
205  }
206 }
207 
208 bool ManualImage2ImageRegistrationWidget::isAverageAccuracyValid(int& numberOfActiveLandmarks)
209 {
210  int numActiveLandmarks = 0;
211  this->getAverageAccuracy(numActiveLandmarks);
212  if(numActiveLandmarks < 1)
213  return false;
214  return true;
215 }
216 
217 
218 } // cx
QString getUid() const
Definition: cxLandmark.cpp:54
boost::shared_ptr< class RegistrationHistory > RegistrationHistoryPtr
Definition: cxDataManager.h:58
One landmark, or fiducial, coordinate.
Definition: cxLandmark.h:61
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
Composite widget for string selection.
boost::shared_ptr< class Data > DataPtr
boost::shared_ptr< class StringPropertyBase > StringPropertyBasePtr
virtual QString getName() const
Definition: cxData.cpp:90
boost::shared_ptr< class RegServices > RegServicesPtr
Definition: cxRegServices.h:41
Vector3D getCoord() const
Definition: cxLandmark.cpp:59
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
Superclass for all data objects.
Definition: cxData.h:109
bool similar(const CameraInfo &lhs, const CameraInfo &rhs, double tol)
ManualImage2ImageRegistrationWidget(RegServicesPtr services, QWidget *parent, QString objectName)
Namespace for all CustusX production code.