Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Viewer::readFile() // reads a file
- {
- makeCurrent();
- //emit(killOtherGl());
- xrot = 0;
- yrot = 0;
- ColorMapItr* itr;
- MapNode* temp;
- itr = smap -> getIter();
- itr->reset();
- while(!itr->end())
- {
- temp = itr->getCursor();
- itr->advance();
- temp -> die();
- }
- QFileDialog filediag(this, tr("Open File"));
- filediag.setNameFilter("All Data Files (*.hdr)");
- QString filename = filediag.getOpenFileName(this, tr("Open File"), "./","All Data Files (*.hdr)");
- //QString filename = QFileDialog::getOpenFileName(this, tr("Open File"));
- if(filename != 0 && (filename.contains(".dat") || filename.contains("hdr")))
- {
- QString line; //current line and file name
- float maxValue;
- float minValue;
- int isVector = 0;
- int xDim, yDim, zDim;
- QStringList coordList; // list of floats
- int typeChoice = 0;
- quint8 currentNumberByte;
- qint16 currentNumberShort;
- qint32 currentNumberInt;
- //float currentNumberFloat;
- double currentNumberDouble;
- float currentNumber;
- float X;
- float Y;
- float Z;
- /*quint8 currentNumberByte;
- qint16 currentNumberShort;
- qint32 currentNumberInt;
- float currentNumberFloat;
- double currentNumberDouble;
- float currentNumber;*/
- QFile* theSource;// file pointer so i can handle X number of files
- theSource = 0; // set file pointer to null
- filename.chop(4);
- filename += ".hdr";
- theSource = new QFile(filename);
- theSource -> open(QIODevice::ReadOnly | QIODevice::Text);// | QIODevice::Text);// opens file
- QTextStream stream( theSource );
- line = stream.readLine();
- coordList = line.split(" ");
- xDim = coordList.at(0).toInt();
- yDim = coordList.at(1).toInt();
- zDim = coordList.at(2).toInt();
- size = xDim;
- line = stream.readLine();
- if(line.contains("BYTE"))
- {
- typeChoice = 0;
- //we want to use uchars
- }
- else if(line.contains("FLOAT"))
- {
- typeChoice = 1;
- //we want to use floats
- }
- else if(line.contains("SHORT"))
- {
- typeChoice = 2;
- //we want to use shorts
- }
- else if(line.contains("INT"))
- {
- typeChoice = 3;
- //we want to use ints
- }
- else if(line.contains("DOUBLE"))
- {
- typeChoice = 4;
- //we want to use doubles
- }
- line = stream.readLine();
- if(line.contains("VECTOR"))
- {
- isVector = 1;
- }
- theSource -> close();
- delete theSource;
- filename.chop(4);
- if(isVector == 1)
- {
- convertedData = (float*)malloc((xDim * yDim * zDim) * sizeof(float));///
- convertedVectorData = (float*)malloc((xDim * yDim * zDim * 3) * sizeof(float));
- filename += ".vec";
- theSource = new QFile(filename);
- theSource -> open(QIODevice::ReadOnly);// | QIODevice::Text);// opens file
- QDataStream in(theSource);
- //char* s = (char*)malloc(1*sizeof(float));
- // for(int i = 0; i < (xDim*yDim*zDim * 3); i++)
- // {
- // if(typeChoice == 0)
- // {
- // in >> currentNumberByte;
- // currentNumber = float(currentNumberByte);
- // }
- // else if(typeChoice == 1)
- // {
- // in.readRawData(s, sizeof(float));
- // currentNumber = float(*s);
- // }
- // else if(typeChoice == 2)
- // {
- // in >> currentNumberShort;
- // currentNumber = float(currentNumberShort);
- // }
- // else if(typeChoice == 3)
- // {
- // in >> currentNumberInt;
- // currentNumber = float(currentNumberInt);
- // }
- // else
- // {
- // in >> currentNumberDouble;
- // currentNumber = float(currentNumberDouble);
- // }
- //
- // convertedVectorData[i] = currentNumber;
- // }
- FILE *myFile;
- myFile = fopen( filename.toStdString().c_str(), "r");
- if(myFile == NULL) qDebug() << "FUUUCK";
- fread( convertedVectorData, sizeof(float), xDim*yDim*zDim*3, myFile);
- for(int i = 0; i < (xDim*yDim*zDim); i++)
- {
- X = convertedVectorData[(3*i) + 0];
- Y = convertedVectorData[(3*i) + 1];
- Z = convertedVectorData[(3*i) + 2];
- X *= 1000;
- Y *= 1000;
- Z *= 1000;
- // qDebug() << X << Y << Z;
- currentNumber = pow((pow(X, 2)+pow(Y, 2)+pow(Z, 2)), 0.5);
- // qDebug() << currentNumber;
- convertedData[i] = currentNumber;
- // break;
- if(i == 0)//first run we store both into min and max of both
- {
- maxValue = currentNumber;
- minValue = currentNumber;
- }
- else
- {
- if(currentNumber > maxValue)
- {
- maxValue = currentNumber;
- }
- if(currentNumber < minValue)
- {
- minValue = currentNumber;
- }
- }
- }
- makeCurrent();
- //glEnable(GL_TEXTURE_3D);
- glActiveTexture(GL_TEXTURE2);
- glGenTextures(1, &texname2);
- glBindTexture(GL_TEXTURE_3D, texname2);
- glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
- glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
- glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER);
- makeCurrent();
- glActiveTexture(GL_TEXTURE2);
- glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB32F_ARB, xDim, yDim, zDim, 0, GL_RGB, GL_FLOAT, convertedVectorData);
- vectorMap = convertedVectorData;
- ///generate noise texture!;
- createNoiseTexture(xDim, yDim, zDim);
- }
- else
- {
- convertedData = (float*)malloc((xDim * yDim * zDim) * sizeof(float));
- filename += ".dat";
- theSource = new QFile(filename);
- theSource -> open(QIODevice::ReadOnly);// | QIODevice::Text);// opens file
- QDataStream in(theSource);
- char* s = (char*)malloc(1*sizeof(float));
- ///load and convert
- for(int i = 0; i < (xDim*yDim*zDim) && !in.atEnd(); i++)
- {
- if(typeChoice == 0)
- {
- in >> currentNumberByte;
- currentNumber = float(currentNumberByte);
- }
- else if(typeChoice == 1)
- {
- //in >> currentNumberFloat;
- in.readRawData(s, sizeof(float));
- currentNumber = float(*s);
- //currentNumber = float(currentNumberFloat);
- }
- else if(typeChoice == 2)
- {
- in >> currentNumberShort;
- currentNumber = float(currentNumberShort);
- }
- else if(typeChoice == 3)
- {
- in >> currentNumberInt;
- currentNumber = float(currentNumberInt);
- }
- else
- {
- in >> currentNumberDouble;
- currentNumber = float(currentNumberDouble);
- }
- convertedData[i] = currentNumber;
- // if(currentNumber <= 0)
- // {
- // qDebug() << currentNumber << endl;
- // }
- if(i == 0)//first run we store both into min and max of both
- {
- maxValue = currentNumber;
- minValue = currentNumber;
- }
- else
- {
- if(currentNumber > maxValue)
- {
- maxValue = currentNumber;
- }
- if(currentNumber < minValue)
- {
- minValue = currentNumber;
- }
- }
- }
- }
- ///normalize
- //qDebug() << maxValue << endl;
- //qDebug() << minValue << endl;
- if(minValue < 0)
- {
- minValue = fabs(minValue);
- maxValue = fabs(maxValue);
- maxValue = minValue + maxValue;
- }
- else
- {
- minValue = 0;
- }
- for(int i = 0; i < (xDim*yDim*zDim); i++)
- {
- convertedData[i] = ((convertedData[i]) + minValue)/float(maxValue);
- }
- makeCurrent();
- glActiveTexture(GL_TEXTURE1);
- //glEnable(GL_TEXTURE_3D);
- if(fileSelected == 0)
- {
- makeCurrent();
- //glEnable(GL_TEXTURE_3D);
- glGenTextures(1, &texname);
- glBindTexture(GL_TEXTURE_3D, texname);
- glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
- glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
- glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER);
- }
- makeCurrent();
- glActiveTexture(GL_TEXTURE1);
- glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE, xDim, yDim, zDim, 0, GL_LUMINANCE, GL_FLOAT, convertedData);
- fileSelected = 1;
- theSource -> close();
- //qDebug() << maxValue << endl;
- emit(newFileLoaded(convertedData, xDim, yDim, zDim, maxValue));
- delete theSource;
- sizex = xDim;
- sizey = yDim;
- sizez = zDim;
- size = xDim;
- if(yDim > xDim)
- {
- size = yDim;
- }
- if(zDim > yDim)
- {
- size = zDim;
- }
- maxRange = maxValue;
- //free(convertedData);
- //qDebug() << "got here yo" << maxRange << endl;
- }
- zoomAmount = size * 2.5;
- reloadShaders();
- updateGL();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement