Fraxinus  17.12
An IGT application
cxElastixExecuter.h
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 #ifndef CXELASTIXEXECUTER_H_
35 #define CXELASTIXEXECUTER_H_
36 
37 #include <QString>
38 #include "cxForwardDeclarations.h"
39 #include "cxTransform3D.h"
40 #include <QObject>
41 #include <QProcess>
42 #include <QFile>
43 #include "cxTimedAlgorithm.h"
44 #include "cxLogger.h"
45 #include "cxRegServices.h"
46 #include "org_custusx_registration_method_commandline_Export.h"
47 
48 namespace cx
49 {
70 class org_custusx_registration_method_commandline_EXPORT ElastixExecuter : public TimedBaseAlgorithm
71 {
72  Q_OBJECT
73 public:
74  ElastixExecuter(RegServicesPtr services, QObject* parent=NULL);
75  virtual ~ElastixExecuter();
76 
77  void setDisplayProcessMessages(bool on);
78 
79  bool setInput(QString application,
80  DataPtr fixed,
81  DataPtr moving,
82  QString outdir,
83  QStringList parameterfiles);
84  virtual void execute();
85  virtual bool isFinished() const;
86  virtual bool isRunning() const;
87 
93  Transform3D getAffineResult_mMf(bool* ok = 0) ;
94  QString getNonlinearResultVolume(bool* ok = 0);
95 
96 private slots:
97  void processStateChanged(QProcess::ProcessState newState);
98  void processError(QProcess::ProcessError error);
99  void processFinished(int, QProcess::ExitStatus);
100  void processReadyRead();
101 
102 private:
106  QString writeInitTransformToElastixfile(DataPtr fixed, DataPtr moving, QString outdir);
110  QString writeInitTransformToCalfile(DataPtr fixed, DataPtr moving, QString outdir);
115  QString findMostRecentTransformOutputFile() const;
134  Transform3D getAffineResult_mmMff(bool* ok = 0) ;
141  Transform3D getFileTransform_ddMd(DataPtr volume);
142 
143  QString mLastOutdir;
144  QProcess* mProcess;
145  DataPtr mFixed;
146  DataPtr mMoving;
147  RegServicesPtr mServices;
148 };
149 
154 {
155 public:
156  ElastixParameterFile(QString filename);
157 
158  bool isValid() const;
159  QString readParameterString(QString key) ;
160  bool readParameterBool(QString key) ;
161  int readParameterInt(QString key) ;
162  std::vector<double> readParameterDoubleVector(QString key) ;
163 // void writeParameter(QString key, QStringList value);
164  Transform3D readEulerTransform() ;
165  Transform3D readAffineTransform();
166 
167 private:
168  QString readParameterRawValue(QString key);
169  QFile mFile;
170  QString mText;
171 };
172 
178 {
179 public:
183 
184  static ElastixEulerTransform create(Vector3D angles_xyz, Vector3D translation, Vector3D centerOfRotation)
185  {
186  ElastixEulerTransform retval;
187  retval.mAngles_xyz = angles_xyz;
188  retval.mTranslation = translation;
189  retval.mCenterOfRotation = centerOfRotation;
190  return retval;
191  }
192  static ElastixEulerTransform create(Transform3D M, Vector3D centerOfRotation)
193  {
194  ElastixEulerTransform retval;
195  retval.mAngles_xyz = M.matrix().block<3, 3> (0, 0).eulerAngles(0, 1, 2);
196  retval.mCenterOfRotation = centerOfRotation;
197 
198  Transform3D R = retval.getRotationMatrix();
200  // solve M = T*Tc*R*Tc.inv() with respect to T:
201  Transform3D T = M*C*R.inv()*C.inv();
202 
203  retval.mTranslation = T.matrix().block<3, 1> (0, 3);
204 // retval.mCenterOfRotation = Vector3D(0,0,0);
205  return retval;
206  }
208  {
209  Transform3D T = createTransformTranslate(mTranslation);
210  Transform3D C = createTransformTranslate(mCenterOfRotation);
211  Transform3D R = this->getRotationMatrix();
212  Transform3D Q = T*C*R*C.inv();
213  return Q;
214  }
215  static void test()
216  {
217  std::cout << "==========TEST==============" << std::endl;
219 
220  ElastixEulerTransform E = create(M, Vector3D(30,40,50));
221  Transform3D Q = E.toMatrix();
222 
223  std::cout << "M\n" << M << std::endl;
224  std::cout << "Q\n" << Q << std::endl;
225  Transform3D diff = Q*M.inv();
226  std::cout << "Q*M.inv\n" << diff << std::endl;
227  if (!similar(Transform3D::Identity(), diff))
228  reportError("assertion failure in ElastixEulerTransform");
229  }
230 private:
232 
233  Transform3D getRotationMatrix() const
234  {
235  Eigen::Matrix3d m;
236  m = Eigen::AngleAxisd(mAngles_xyz[0], Eigen::Vector3d::UnitX())
237  * Eigen::AngleAxisd(mAngles_xyz[1], Eigen::Vector3d::UnitY())
238  * Eigen::AngleAxisd(mAngles_xyz[2], Eigen::Vector3d::UnitZ());
239 
240  Transform3D R = Transform3D::Identity();
241  R.matrix().block<3, 3> (0, 0) = m;
242  return R;
243  }
244 };
245 
246 
250 } /* namespace cx */
251 #endif /* CXELASTIXEXECUTER_H_ */
Transform3D createTransformRotateY(const double angle)
void reportError(QString msg)
Definition: cxLogger.cpp:92
Base class for algorithms that wants to time their execution.
static ElastixEulerTransform create(Vector3D angles_xyz, Vector3D translation, Vector3D centerOfRotation)
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
Transform3D toMatrix() const
static ElastixEulerTransform create(Transform3D M, Vector3D centerOfRotation)
boost::shared_ptr< class Data > DataPtr
ElastiX command-line wrapper.
boost::shared_ptr< class RegServices > RegServicesPtr
Definition: cxRegServices.h:41
Transform3D createTransformTranslate(const Vector3D &translation)
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Definition: cxVector3D.h:63
bool similar(const CameraInfo &lhs, const CameraInfo &rhs, double tol)
Transform3D createTransformRotateX(const double angle)
#define M_PI
Namespace for all CustusX production code.