|
| 1 | +#include "meshloader.h" |
| 2 | + |
| 3 | +#include <QFileInfo> |
| 4 | +#include <QTextStream> |
| 5 | + |
| 6 | +#include "util.h" |
| 7 | +#include "../application.h" |
| 8 | + |
| 9 | + |
| 10 | +MeshLoader::MeshLoader(QFile & file, std::vector<VertexRecordPtr> & vertexes, |
| 11 | + std::vector<CellDefRecordPtr> & cellDefs, |
| 12 | + uint & data_line_count, QObject * parent) : |
| 13 | + QObject(parent), |
| 14 | + _file(file), |
| 15 | + m_vertexes( vertexes ), |
| 16 | + m_cellDefs( cellDefs ), |
| 17 | + _data_line_count(data_line_count), |
| 18 | + _finished(false) |
| 19 | +{ |
| 20 | + |
| 21 | +} |
| 22 | + |
| 23 | +void MeshLoader::doLoad() |
| 24 | +{ |
| 25 | + QTextStream in(&_file); |
| 26 | + long bytesReadSofar = 0; |
| 27 | + QStringList valuesAsString; |
| 28 | + |
| 29 | + bool isInVertexesSection = false; |
| 30 | + |
| 31 | + bool isInCellDefsSection = false; |
| 32 | + |
| 33 | + for (int i = 0; !in.atEnd(); ++i) |
| 34 | + { |
| 35 | + //read file line by line |
| 36 | + QString line = in.readLine(); |
| 37 | + |
| 38 | + //Clear the list with the tokenized data values as string |
| 39 | + valuesAsString.clear(); |
| 40 | + |
| 41 | + //updates the progress |
| 42 | + bytesReadSofar += line.size() + 1; //account for line break char (in Windows may have 2, though.) |
| 43 | + |
| 44 | + if( ! ( i % 100 ) ){ //update progress for each 100 lines to not impact performance much |
| 45 | + // allows tracking progress of a file up to about 400GB |
| 46 | + emit progress( (int)(bytesReadSofar / 100) ); |
| 47 | + } |
| 48 | + |
| 49 | + if( i == 0 || i == 1 ){} //first and second lines are ignored |
| 50 | + else { //parse lines containing data (must be within the target interval) |
| 51 | + |
| 52 | + if( !isInVertexesSection && !isInCellDefsSection && |
| 53 | + line.startsWith( "VERTEX LOCATIONS", Qt::CaseInsensitive ) ){ |
| 54 | + isInVertexesSection = true; |
| 55 | + continue; //move on to the next file line |
| 56 | + } |
| 57 | + |
| 58 | + if( isInVertexesSection && !isInCellDefsSection && |
| 59 | + line.startsWith( "CELL VERTEX INDEXES", Qt::CaseInsensitive ) ){ |
| 60 | + isInVertexesSection = false; |
| 61 | + isInCellDefsSection = true; |
| 62 | + continue; //move on to the next file line |
| 63 | + } |
| 64 | + |
| 65 | + //read line from the GSLib data file |
| 66 | + Util::fastSplit( line, valuesAsString ); |
| 67 | + |
| 68 | + if( isInVertexesSection ){ |
| 69 | + if( valuesAsString.size() != 3 ){ |
| 70 | + Application::instance()->logError( QString("MeshLoader::doLoad(): vertex coordinate count different from 3 in line ").append(QString::number(i)) ); |
| 71 | + m_vertexes.push_back( VertexRecordPtr( new VertexRecord{ 0.0, 0.0, 0.0 } ) ); |
| 72 | + } else { |
| 73 | + bool ok[] = {true, true, true}; |
| 74 | + double x = valuesAsString[0].toDouble( &ok[0] ); |
| 75 | + double y = valuesAsString[1].toDouble( &ok[1] ); |
| 76 | + double z = valuesAsString[2].toDouble( &ok[2] ); |
| 77 | + if( !ok[0] || !ok[1] || !ok[2] ) |
| 78 | + Application::instance()->logError( QString("MeshLoader::doLoad(): error in vertex section of mesh file (line "). |
| 79 | + append(QString::number(i)).append("): could not convert a value to double.") ); |
| 80 | + m_vertexes.push_back( VertexRecordPtr( new VertexRecord{ x, y, z } ) ) ; |
| 81 | + } |
| 82 | + } else if( isInCellDefsSection ) { |
| 83 | + if( valuesAsString.size() != 8 ){ |
| 84 | + Application::instance()->logError( QString("MeshLoader::doLoad(): vertex id count in cell definition different from 8 in line ").append(QString::number(i)) ); |
| 85 | + m_cellDefs.push_back( CellDefRecordPtr( new CellDefRecord{ {0, 0, 0, 0, 0, 0, 0, 0} } ) ); |
| 86 | + } else { |
| 87 | + bool ok[] = {true, true, true, true, true, true, true, true}; |
| 88 | + int vId[8]; |
| 89 | + vId[0] = valuesAsString[0].toInt( &ok[0] ); |
| 90 | + vId[1] = valuesAsString[1].toInt( &ok[1] ); |
| 91 | + vId[2] = valuesAsString[2].toInt( &ok[2] ); |
| 92 | + vId[3] = valuesAsString[3].toInt( &ok[3] ); |
| 93 | + vId[4] = valuesAsString[4].toInt( &ok[4] ); |
| 94 | + vId[5] = valuesAsString[5].toInt( &ok[5] ); |
| 95 | + vId[6] = valuesAsString[6].toInt( &ok[6] ); |
| 96 | + vId[7] = valuesAsString[7].toInt( &ok[7] ); |
| 97 | + if( !ok[0] || !ok[1] || !ok[2] || !ok[3] || !ok[4] || !ok[5] || !ok[6] || !ok[7] ) |
| 98 | + Application::instance()->logError( QString("MeshLoader::doLoad(): error in cell definition section of mesh file (line "). |
| 99 | + append(QString::number(i)).append("): could not convert a value to unsigned integer.") ); |
| 100 | + m_cellDefs.push_back( CellDefRecordPtr( new CellDefRecord{ { vId[0], vId[1], vId[2], vId[3], vId[4], vId[5], vId[6], vId[7] } } ) ); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + ++_data_line_count; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + _finished = true; |
| 109 | +} |
0 commit comments