CustusX  2023.01.05-dev+develop.0da12
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 }
QString qstring_cast(const T &val)
DoubleBoundingBox3D transform(const Transform3D &m, const DoubleBoundingBox3D &bb)
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
TransformFile(QString fileName="")
void write(const Transform3D &transform)
Transform3D read(bool *ok=0)
void reportWarning(QString msg)
Definition: cxLogger.cpp:70
Namespace for all CustusX production code.