CustusX  15.3.4-beta
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
cxElastixManager.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 
33 
34 #include "cxElastixManager.h"
35 
36 #include <QDir>
37 
38 #include "cxTime.h"
39 #include "cxProfile.h"
40 #include "cxElastixExecuter.h"
41 #include "cxSettings.h"
42 #include "cxDataReaderWriter.h"
43 #include "cxRegistrationService.h"
45 #include "cxPatientModelService.h"
46 #include "cxViewService.h"
47 #include "cxVolumeHelpers.h"
48 #include "cxLogger.h"
49 
50 namespace cx
51 {
52 
54  mServices(services)
55 {
56  mOptions = profile()->getXmlSettings().descend("elastix");
57 
58  mParameters.reset(new ElastixParameters(mOptions));
59  connect(mParameters.get(), SIGNAL(elastixParametersChanged()), this, SIGNAL(elastixChanged()));
60 
61  mDisplayProcessMessages = BoolProperty::initialize("displayProcessMessages",
62  "Show Messages",
63  "Display messages from the running registration process in CustusX",
64  false,
65  mOptions.getElement());
66 
67  mDisableRendering = BoolProperty::initialize("disableRendering",
68  "Disable Rendering",
69  "Disable rendering while running process.\n"
70  "Can be used to avoid clash in GPU usage.",
71  false,
72  mOptions.getElement());
73 
74  mExecuter.reset(new ElastixExecuter(services));
75  connect(mExecuter.get(), SIGNAL(finished()), this, SLOT(executionFinishedSlot()));
76  connect(mExecuter.get(), SIGNAL(aboutToStart()), this, SLOT(preprocessExecuter()));
77 }
78 
80 {
81 }
82 
84 {
85  mExecuter->execute();
86 }
87 
88 void ElastixManager::preprocessExecuter()
89 {
90  QStringList parameterFiles = mParameters->getActiveParameterFiles();
91  QString timestamp = QDateTime::currentDateTime().toString(timestampSecondsFormat());
92  QDir outDir(mServices.patientModelService->getActivePatientFolder()+"/elastix/"+timestamp);
93 
94  mExecuter->setDisplayProcessMessages(mDisplayProcessMessages->getValue());
95  mExecuter->setInput(mParameters->getActiveExecutable(),
96  mServices.registrationService->getFixedData(),
97  mServices.registrationService->getMovingData(),
98  outDir.absolutePath(),
99  parameterFiles);
100 
101  if (mDisableRendering->getValue())
102  {
103  mServices.visualizationService->enableRender(false);
104  }
105 }
106 
107 void ElastixManager::executionFinishedSlot()
108 {
109  if (mDisableRendering->getValue())
110  mServices.visualizationService->enableRender(true);
111 
112  bool ok = false;
113  Transform3D mMf = mExecuter->getAffineResult_mMf(&ok);
114 
115  if (!ok)
116  return;
117 
118 // std::cout << "ElastixManager::executionFinishedSlot(), Linear Result mMf: \n" << mMf << std::endl;
119 
120  QStringList parameterFiles = mParameters->getActiveParameterFiles();
121  QString desc = QString("Image2Image [exe=%1]").arg(QFileInfo(mParameters->getActiveExecutable()).fileName());
122  for (unsigned i=0; i<parameterFiles.size(); ++i)
123  desc += QString("[par=%1]").arg(QFileInfo(parameterFiles[i]).fileName());
124 
125  // Start with fMr * D * rMm = fMm'
126  // where the lhs is the existing data and the delta that is input to regmanager,
127  // and the rhs is the (inverse of the) output from ElastiX.
128  // This gives
129  // D = rMf * fMm' * mMr
130  // as the input to regmanager applyImage2ImageRegistration()
131 
132  Transform3D delta_pre_rMd =
133  mServices.registrationService->getFixedData()->get_rMd()
134  * mMf.inv()
135  * mServices.registrationService->getMovingData()->get_rMd().inv();
136 
137 // std::cout << "ElastixManager::executionFinishedSlot(), delta_pre_rMd: \n" << delta_pre_rMd << std::endl;
138 // std::cout << "ElastixManager::executionFinishedSlot(), expected new rMdm: \n" << mServices.registrationService->getFixedData()->get_rMd() * mMf.inv() << std::endl;
139 
140 // mServices.registrationService->applyImage2ImageRegistration(mMf.inv(), desc);
141  mServices.registrationService->applyImage2ImageRegistration(delta_pre_rMd, desc);
142 
143  // add nonlinear data AFTER registering - we dont want these data to be double-registered!
144  this->addNonlinearData();
145 }
146 
151 void ElastixManager::addNonlinearData()
152 {
153  bool ok = true;
154  QString nonlinearVolumeFilename = mExecuter->getNonlinearResultVolume(&ok);
155 
156  if (!ok)
157  return;
158 
159  ImagePtr movingImage = boost::dynamic_pointer_cast<Image>(mServices.registrationService->getMovingData());
160  ImagePtr raw = boost::dynamic_pointer_cast<Image>(MetaImageReader().load(nonlinearVolumeFilename, nonlinearVolumeFilename));
161 
162  if (!raw)
163  {
164  report(QString("Failed to import nonlinear volume %1").arg(nonlinearVolumeFilename));
165  return;
166  }
167 
168  QString uid = movingImage->getUid() + "_nl%1";
169  QString name = movingImage->getName()+" nl%1";
170 
171  ImagePtr nlVolume = createDerivedImage(mServices.patientModelService,
172  uid, name,
173  raw->getBaseVtkImageData(), movingImage);
174 
175  // volume is resampled into the space of the fixed data:
176  nlVolume->get_rMd_History()->setRegistration(mServices.registrationService->getFixedData()->get_rMd());
177 
178  mServices.patientModelService->insertData(nlVolume);
179 
180  report(QString("Added volume %1, created by a nonlinear transform").arg(nlVolume->getName()));
181 }
182 
183 } /* namespace cx */
cxResource_EXPORT ProfilePtr profile()
Definition: cxProfile.cpp:142
static BoolPropertyPtr initialize(const QString &uid, QString name, QString help, bool value, QDomNode root=QDomNode())
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
boost::shared_ptr< class Image > ImagePtr
Definition: cxDicomWidget.h:48
Parameter and preset handling for the ElastiX registration.
QString timestampSecondsFormat()
Definition: cxTime.cpp:39
QDomElement getElement()
return the current element
ImagePtr createDerivedImage(PatientModelServicePtr dataManager, QString uid, QString name, vtkImageDataPtr raw, ImagePtr parent)
ElastiX command-line wrapper.
ElastixManager(RegServices services)
VisualizationServicePtr visualizationService
Definition: cxVisServices.h:60
void report(QString msg)
Definition: cxLogger.cpp:90
PatientModelServicePtr patientModelService
RegistrationServicePtr registrationService
Definition: cxRegServices.h:60