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