14 #include <QTextStream> 15 #include <QStringList> 29 QFile file(mFilename);
32 if (file.open(QIODevice::ReadOnly))
40 if (line.startsWith(key, Qt::CaseInsensitive))
42 QStringList list = line.split(
"=", QString::SkipEmptyParts);
46 return list.join(
"=");
58 QString mod = this->
readKey(
"Modality");
60 if (mod.contains(
"CT", Qt::CaseInsensitive))
62 if (mod.contains(
"MR", Qt::CaseInsensitive))
64 if (mod.contains(
"US", Qt::CaseInsensitive))
66 if (mod.contains(
"OTHER", Qt::CaseInsensitive))
68 mod = mod.remove(
"MET_MOD_");
74 return this->
readKey(
"ImageType3");
80 void CustomMetaImage::remove(QStringList* data, QStringList keys)
82 QRegExp regexp(QString(
"(^%1)").arg(keys.join(
"|^")));
83 QStringList removeThese = data->filter(regexp);
84 for (
int i=0; i<removeThese.size(); ++i)
85 data->removeAll(removeThese[i]);
93 void CustomMetaImage::append(QStringList* data, QString key, QString value)
96 int last = data->lastIndexOf(QRegExp(
"^ElementDataFile.*"));
97 data->insert(last, QString(
"%1 = %2").arg(key).arg(value));
102 QFile file(mFilename);
104 if (!file.open(QIODevice::ReadWrite))
106 reportError(
"Failed to open file " + mFilename +
".");
110 QStringList data = QTextStream(&file).readAll().split(
"\n");
112 this->
remove(&data, QStringList()<<key);
113 this->append(&data, key, value);
116 file.write(data.join(
"\n").toLatin1());
121 QStringList valid_values;
122 valid_values <<
"CT" <<
"MR" <<
"NM" <<
"US" <<
"OTHER" <<
"UNKNOWN";
123 if(!valid_values.contains(value, Qt::CaseInsensitive))
126 this->
setKey(
"Modality", value);
133 this->
setKey(
"ImageType3", value);
145 QFile file(mFilename);
148 if (file.open(QIODevice::ReadOnly))
150 QTextStream t(&file);
156 if (line.startsWith(
"Position", Qt::CaseInsensitive) || line.startsWith(
"Offset", Qt::CaseInsensitive))
158 QStringList list = line.split(
" ", QString::SkipEmptyParts);
160 p_r =
Vector3D(list[2].toDouble(), list[3].toDouble(), list[4].toDouble());
162 else if (line.startsWith(
"TransformMatrix", Qt::CaseInsensitive) || line.startsWith(
"Orientation",
163 Qt::CaseInsensitive))
165 QStringList list = line.split(
" ", QString::SkipEmptyParts);
169 e_x =
Vector3D(list[2].toDouble(), list[3].toDouble(), list[4].toDouble());
170 e_y =
Vector3D(list[5].toDouble(), list[6].toDouble(), list[7].toDouble());
171 e_z =
cross(e_x, e_y);
181 for (
unsigned i = 0; i < 3; ++i)
198 QFile file(mFilename);
200 if (!file.open(QIODevice::ReadWrite))
202 reportWarning(
"Could not save transform because: Failed to open file " + mFilename);
206 QStringList data = QTextStream(&file).readAll().split(
"\n");
208 this->
remove(&data, QStringList()<<
"TransformMatrix"<<
"Offset"<<
"Position"<<
"Orientation");
211 std::stringstream tmList;
212 for (
int c=0; c<dim; ++c)
213 for (
int r=0; r<dim; ++r)
214 tmList <<
" " << M(r,c);
215 this->append(&data,
"TransformMatrix",
qstring_cast(tmList.str()));
217 std::stringstream posList;
218 for (
int r=0; r<dim; ++r)
219 posList <<
" " << M(r,3);
220 this->append(&data,
"Offset",
qstring_cast(posList.str()));
223 file.write(data.join(
"\n").toLatin1());
QString qstring_cast(const T &val)
void reportError(QString msg)
Transform3D Transform3D
Transform3D is a representation of an affine 3D transform.
Vector3D cross(const Vector3D &a, const Vector3D &b)
compute cross product of a and b.
void reportWarning(QString msg)
Eigen::Vector3d Vector3D
Vector3D is a representation of a point or vector in 3D.
Namespace for all CustusX production code.