35 #include <QTextStream>
36 #include <QStringList>
50 QFile file(mFilename);
53 if (file.open(QIODevice::ReadOnly))
61 if (line.startsWith(key, Qt::CaseInsensitive))
63 QStringList list = line.split(
"=", QString::SkipEmptyParts);
67 return list.join(
"=");
79 QString mod = this->
readKey(
"Modality");
81 if (mod.contains(
"CT", Qt::CaseInsensitive))
83 if (mod.contains(
"MR", Qt::CaseInsensitive))
85 if (mod.contains(
"US", Qt::CaseInsensitive))
87 if (mod.contains(
"OTHER", Qt::CaseInsensitive))
89 mod = mod.remove(
"MET_MOD_");
95 return this->
readKey(
"ImageType3");
101 void CustomMetaImage::remove(QStringList* data, QStringList keys)
103 QRegExp regexp(QString(
"(^%1)").arg(keys.join(
"|^")));
104 QStringList removeThese = data->filter(regexp);
105 for (
int i=0; i<removeThese.size(); ++i)
106 data->removeAll(removeThese[i]);
114 void CustomMetaImage::append(QStringList* data, QString key, QString value)
117 int last = data->lastIndexOf(QRegExp(
"^ElementDataFile.*"));
118 data->insert(last, QString(
"%1 = %2").arg(key).arg(value));
123 QFile file(mFilename);
125 if (!file.open(QIODevice::ReadWrite))
127 reportError(
"Failed to open file " + mFilename +
".");
131 QStringList data = QTextStream(&file).readAll().split(
"\n");
133 this->
remove(&data, QStringList()<<key);
134 this->append(&data, key, value);
137 file.write(data.join(
"\n").toLatin1());
142 QStringList valid_values;
143 valid_values <<
"CT" <<
"MR" <<
"NM" <<
"US" <<
"OTHER" <<
"UNKNOWN";
144 if(!valid_values.contains(value, Qt::CaseInsensitive))
147 this->
setKey(
"Modality", value);
154 this->
setKey(
"ImageType3", value);
166 QFile file(mFilename);
169 if (file.open(QIODevice::ReadOnly))
171 QTextStream t(&file);
177 if (line.startsWith(
"Position", Qt::CaseInsensitive) || line.startsWith(
"Offset", Qt::CaseInsensitive))
179 QStringList list = line.split(
" ", QString::SkipEmptyParts);
181 p_r =
Vector3D(list[2].toDouble(), list[3].toDouble(), list[4].toDouble());
183 else if (line.startsWith(
"TransformMatrix", Qt::CaseInsensitive) || line.startsWith(
"Orientation",
184 Qt::CaseInsensitive))
186 QStringList list = line.split(
" ", QString::SkipEmptyParts);
190 e_x =
Vector3D(list[2].toDouble(), list[3].toDouble(), list[4].toDouble());
191 e_y =
Vector3D(list[5].toDouble(), list[6].toDouble(), list[7].toDouble());
192 e_z =
cross(e_x, e_y);
202 for (
unsigned i = 0; i < 3; ++i)
219 QFile file(mFilename);
221 if (!file.open(QIODevice::ReadWrite))
223 reportWarning(
"Could not save transform because: Failed to open file " + mFilename);
227 QStringList data = QTextStream(&file).readAll().split(
"\n");
229 this->
remove(&data, QStringList()<<
"TransformMatrix"<<
"Offset"<<
"Position"<<
"Orientation");
232 std::stringstream tmList;
233 for (
int c=0; c<dim; ++c)
234 for (
int r=0; r<dim; ++r)
235 tmList <<
" " << M(r,c);
236 this->append(&data,
"TransformMatrix",
qstring_cast(tmList.str()));
238 std::stringstream posList;
239 for (
int r=0; r<dim; ++r)
240 posList <<
" " << M(r,3);
241 this->append(&data,
"Offset",
qstring_cast(posList.str()));
244 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.