| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "opendxreader.h" |
| 7 |
|
| 8 |
#include <avogadro/core/cube.h> |
| 9 |
|
| 10 |
#include <QtCore/QFile> |
| 11 |
#include <QtCore/QTextStream> |
| 12 |
#include <QtCore/QVector> |
| 13 |
|
| 14 |
namespace Avogadro::QtPlugins { |
| 15 |
|
| 16 |
using Core::Cube; |
| 17 |
|
| 18 |
bool OpenDxReader::readFile(const QString& fileName) |
| 19 |
{ |
| 20 |
QFile file(fileName); |
| 21 |
if (!file.open(QFile::ReadOnly)) { |
| 22 |
m_errorString = "Failed to open file for reading"; |
| 23 |
return false; |
| 24 |
} |
| 25 |
|
| 26 |
delete m_cube; |
| 27 |
|
| 28 |
Vector3i dim(0, 0, 0); |
| 29 |
Vector3 origin(0, 0, 0); |
| 30 |
QVector<Vector3> spacings; |
| 31 |
std::vector<float> values; |
| 32 |
|
| 33 |
while (!file.atEnd()) { |
| 34 |
QByteArray line = file.readLine(); |
| 35 |
QTextStream stream(line); |
| 36 |
|
| 37 |
if (line.isEmpty()) { |
| 38 |
// skip empty line |
| 39 |
continue; |
| 40 |
} else if (line[0] == '#') { |
| 41 |
// skip comment line |
| 42 |
continue; |
| 43 |
} else if (line.startsWith("object")) { |
| 44 |
if (dim[0] != 0) |
| 45 |
continue; |
| 46 |
QString unused; |
| 47 |
stream >> unused >> unused >> unused >> unused >> unused; |
| 48 |
stream >> dim[0] >> dim[1] >> dim[2]; |
| 49 |
} else if (line.startsWith("origin")) { |
| 50 |
QString unused; |
| 51 |
stream >> unused >> origin[0] >> origin[1] >> origin[2]; |
| 52 |
} else if (line.startsWith("delta")) { |
| 53 |
QString unused; |
| 54 |
Vector3 delta; |
| 55 |
stream >> unused >> delta[0] >> delta[1] >> delta[2]; |
| 56 |
spacings.append(delta); |
| 57 |
} else if (line.startsWith("attribute")) { |
| 58 |
continue; |
| 59 |
} else if (line.startsWith("component")) { |
| 60 |
continue; |
| 61 |
} else { |
| 62 |
// data line |
| 63 |
while (!stream.atEnd()) { |
| 64 |
double value; |
| 65 |
stream >> value; |
| 66 |
values.push_back(value); |
| 67 |
stream.skipWhiteSpace(); |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
Vector3 spacing(spacings[0][0], spacings[1][1], spacings[2][2]); |
| 73 |
|
| 74 |
// create potential cube |
| 75 |
m_cube = new Cube; |
| 76 |
m_cube->setCubeType(Cube::ESP); |
| 77 |
m_cube->setLimits(origin, dim, spacing); |
| 78 |
m_cube->setData(values); |
| 79 |
|
| 80 |
return true; |
| 81 |
} |
| 82 |
|
| 83 |
QString OpenDxReader::errorString() const |
| 84 |
{ |
| 85 |
return m_errorString; |
| 86 |
} |
| 87 |
|
| 88 |
Cube* OpenDxReader::cube() const |
| 89 |
{ |
| 90 |
return m_cube; |
| 91 |
} |
| 92 |
} // namespace Avogadro::QtPlugins |