NorMIT-nav  22.09
An IGT application
cxTransformFile.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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 
12 #include "cxTransformFile.h"
13 
14 #include <QFileInfo>
15 #include <QFile>
16 
17 #include "cxTypeConversions.h"
18 #include "cxLogger.h"
19 
20 namespace cx
21 {
22 
23 TransformFile::TransformFile(QString fileName) :
24  mFileName(fileName)
25 {
26 }
27 
34 {
35  if (ok)
36  *ok = false;
37  Transform3D retval = Transform3D::Identity();
38  QFile file(mFileName);
39  if (!QFileInfo(mFileName).exists() || !file.open(QIODevice::ReadOnly))
40  {
41  if (!ok)
42  reportWarning(QString("Failed to open file %1 for reading ").arg(file.fileName()));
43  return retval;
44  }
45  bool localOk = true;
46  if(!file.atEnd())
47  {
48  QString positionString = file.readLine();
49  positionString += " " + file.readLine();
50  positionString += " " + file.readLine();
51  positionString += " " + file.readLine();
52  retval = Transform3D::fromString(positionString, &localOk);
53  }
54  if (!localOk)
55  {
56  if (!ok)
57  reportWarning(QString("Invalid format in file %1. Values: [%2]").arg(mFileName).arg(qstring_cast(retval(0, 0))));
58  return retval;
59  }
60  if (ok)
61  *ok = true;
62  return retval;
63 }
64 
66 {
67  QFile file(mFileName);
68  if (!file.open(QIODevice::Truncate | QIODevice::WriteOnly | QIODevice::Text))
69  {
70  reportWarning(QString("Failed to open file %1 for writing.").arg(file.fileName()));
71  return;
72  }
73  file.write(qstring_cast(transform).toLatin1());
74 }
75 
76 }
cxLogger.h
qstring_cast
QString qstring_cast(const T &val)
Definition: cxTypeConversions.h:46
cx
Namespace for all CustusX production code.
Definition: cx_dev_group_definitions.h:13
cx::TransformFile::read
Transform3D read(bool *ok=0)
Definition: cxTransformFile.cpp:33
cx::Transform3D
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
Definition: cxLandmarkPatientRegistrationWidget.h:33
cx::TransformFile::write
void write(const Transform3D &transform)
Definition: cxTransformFile.cpp:65
cxTypeConversions.h
cx::TransformFile::TransformFile
TransformFile(QString fileName="")
Definition: cxTransformFile.cpp:23
cxTransformFile.h
cx::transform
DoubleBoundingBox3D transform(const Transform3D &m, const DoubleBoundingBox3D &bb)
Definition: cxTransform3D.cpp:150
cx::reportWarning
void reportWarning(QString msg)
Definition: cxLogger.cpp:70