migrate SVO reading to use QDataStream in step toward reading from HTTP url

This commit is contained in:
ZappoMan 2015-03-10 10:49:53 -07:00
parent 89f3a091f1
commit c8298ca617
2 changed files with 129 additions and 111 deletions

View file

@ -18,7 +18,10 @@
#include <cmath> #include <cmath>
#include <fstream> // to load voxels from file #include <fstream> // to load voxels from file
#include <QDataStream>
#include <QDebug> #include <QDebug>
#include <QFile>
#include <QFileInfo>
#include <QVector> #include <QVector>
#include <GeometryUtil.h> #include <GeometryUtil.h>
@ -1836,18 +1839,33 @@ int Octree::encodeTreeBitstreamRecursion(OctreeElement* element,
bool Octree::readFromSVOFile(const char* fileName) { bool Octree::readFromSVOFile(const char* fileName) {
bool fileOk = false; bool fileOk = false;
PacketVersion gotVersion = 0; QFile file(fileName);
std::ifstream file(fileName, std::ios::in|std::ios::binary|std::ios::ate); QFileInfo fileInfo(fileName);
bool isOpen = file.open(QIODevice::ReadOnly);
QDataStream fileInputStream(&file); // read the data serialized from the file
if(file.is_open()) { // get file length....
unsigned long fileLength = fileInfo.size();
if(isOpen) {
emit importSize(1.0f, 1.0f, 1.0f); emit importSize(1.0f, 1.0f, 1.0f);
emit importProgress(0); emit importProgress(0);
qDebug("Loading file %s...", fileName); qDebug("Loading file %s...", fileName);
// get file length.... fileOk = readFromStream(fileLength, fileInputStream);
unsigned long fileLength = file.tellg();
file.seekg( 0, std::ios::beg ); emit importProgress(100);
file.close();
}
return fileOk;
}
bool Octree::readFromStream(unsigned long streamLength, QDataStream& inputStream) {
bool fileOk = false;
PacketVersion gotVersion = 0;
unsigned long headerLength = 0; // bytes in the header unsigned long headerLength = 0; // bytes in the header
@ -1863,7 +1881,9 @@ bool Octree::readFromSVOFile(const char* fileName) {
// read just enough of the file to parse the header... // read just enough of the file to parse the header...
const unsigned long HEADER_LENGTH = sizeof(PacketType) + sizeof(PacketVersion); const unsigned long HEADER_LENGTH = sizeof(PacketType) + sizeof(PacketVersion);
unsigned char fileHeader[HEADER_LENGTH]; unsigned char fileHeader[HEADER_LENGTH];
file.read((char*)&fileHeader, HEADER_LENGTH); //file.read((char*)&fileHeader, HEADER_LENGTH);
int bytesRead = inputStream.readRawData((char*)&fileHeader, HEADER_LENGTH);
qDebug() << "HEADER_LENGTH... bytesRead:" << bytesRead;
headerLength = HEADER_LENGTH; // we need this later to skip to the data headerLength = HEADER_LENGTH; // we need this later to skip to the data
@ -1913,9 +1933,10 @@ bool Octree::readFromSVOFile(const char* fileName) {
if (!hasBufferBreaks) { if (!hasBufferBreaks) {
// read the entire file into a buffer, WHAT!? Why not. // read the entire file into a buffer, WHAT!? Why not.
unsigned long dataLength = fileLength - headerLength; unsigned long dataLength = streamLength - headerLength;
unsigned char* entireFileDataSection = new unsigned char[dataLength]; unsigned char* entireFileDataSection = new unsigned char[dataLength];
file.read((char*)entireFileDataSection, dataLength); inputStream.readRawData((char*)entireFileDataSection, dataLength);
unsigned char* dataAt = entireFileDataSection; unsigned char* dataAt = entireFileDataSection;
ReadBitstreamToTreeParams args(WANT_COLOR, NO_EXISTS_BITS, NULL, 0, ReadBitstreamToTreeParams args(WANT_COLOR, NO_EXISTS_BITS, NULL, 0,
@ -1927,7 +1948,7 @@ bool Octree::readFromSVOFile(const char* fileName) {
} else { } else {
unsigned long dataLength = fileLength - headerLength; unsigned long dataLength = streamLength - headerLength;
unsigned long remainingLength = dataLength; unsigned long remainingLength = dataLength;
const unsigned long MAX_CHUNK_LENGTH = MAX_OCTREE_PACKET_SIZE * 2; const unsigned long MAX_CHUNK_LENGTH = MAX_OCTREE_PACKET_SIZE * 2;
unsigned char* fileChunk = new unsigned char[MAX_CHUNK_LENGTH]; unsigned char* fileChunk = new unsigned char[MAX_CHUNK_LENGTH];
@ -1935,7 +1956,7 @@ bool Octree::readFromSVOFile(const char* fileName) {
while (remainingLength > 0) { while (remainingLength > 0) {
quint16 chunkLength = 0; quint16 chunkLength = 0;
file.read((char*)&chunkLength, sizeof(chunkLength)); // read the chunk size from the file inputStream.readRawData((char*)&chunkLength, sizeof(chunkLength));
remainingLength -= sizeof(chunkLength); remainingLength -= sizeof(chunkLength);
if (chunkLength > remainingLength) { if (chunkLength > remainingLength) {
@ -1950,7 +1971,7 @@ bool Octree::readFromSVOFile(const char* fileName) {
break; break;
} }
file.read((char*)fileChunk, chunkLength); // read in a section of the file larger than the chunk; inputStream.readRawData((char*)fileChunk, chunkLength);
remainingLength -= chunkLength; remainingLength -= chunkLength;
@ -1967,10 +1988,6 @@ bool Octree::readFromSVOFile(const char* fileName) {
} }
} }
emit importProgress(100);
file.close();
}
return fileOk; return fileOk;
} }

View file

@ -327,6 +327,7 @@ public:
// these will read/write files that match the wireformat, excluding the 'V' leading // these will read/write files that match the wireformat, excluding the 'V' leading
void writeToSVOFile(const char* filename, OctreeElement* element = NULL); void writeToSVOFile(const char* filename, OctreeElement* element = NULL);
bool readFromSVOFile(const char* filename); bool readFromSVOFile(const char* filename);
bool readFromStream(unsigned long streamLength, QDataStream& inputStream);
unsigned long getOctreeElementsCount(); unsigned long getOctreeElementsCount();