Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. void Viewer::readFile() // reads a file
  2. {
  3.     makeCurrent();
  4.     //emit(killOtherGl());
  5.    
  6.     xrot = 0;
  7.     yrot = 0;
  8.     ColorMapItr* itr;
  9.     MapNode* temp;
  10.    
  11.     itr = smap -> getIter();
  12.     itr->reset();
  13.    
  14.     while(!itr->end())
  15.     {
  16.         temp = itr->getCursor();
  17.         itr->advance();
  18.         temp -> die();
  19.     }
  20.    
  21.     QFileDialog filediag(this, tr("Open File"));
  22.     filediag.setNameFilter("All Data Files (*.hdr)");
  23.    
  24.     QString filename = filediag.getOpenFileName(this, tr("Open File"), "./","All Data Files (*.hdr)");
  25.     //QString filename = QFileDialog::getOpenFileName(this, tr("Open File"));
  26.    
  27.    
  28.     if(filename != 0 && (filename.contains(".dat") || filename.contains("hdr")))
  29.     {
  30.        
  31.         QString line; //current line and file name
  32.         float maxValue;
  33.         float minValue;
  34.         int isVector = 0;
  35.         int xDim, yDim, zDim;
  36.         QStringList coordList; // list of floats
  37.         int typeChoice = 0;
  38.         quint8 currentNumberByte;
  39.         qint16 currentNumberShort;
  40.         qint32 currentNumberInt;
  41.         //float currentNumberFloat;
  42.         double currentNumberDouble;
  43.         float currentNumber;
  44.         float X;
  45.         float Y;
  46.         float Z;
  47.         /*quint8 currentNumberByte;
  48.         qint16 currentNumberShort;
  49.         qint32 currentNumberInt;
  50.         float currentNumberFloat;
  51.         double currentNumberDouble;
  52.         float currentNumber;*/
  53.         QFile* theSource;// file pointer so i can handle X number of files
  54.         theSource = 0; // set file pointer to null
  55.         filename.chop(4);
  56.        
  57.         filename += ".hdr";
  58.         theSource = new QFile(filename);
  59.         theSource -> open(QIODevice::ReadOnly | QIODevice::Text);// | QIODevice::Text);// opens file
  60.         QTextStream stream( theSource );
  61.         line = stream.readLine();
  62.         coordList = line.split(" ");
  63.         xDim = coordList.at(0).toInt();
  64.         yDim = coordList.at(1).toInt();
  65.         zDim = coordList.at(2).toInt();
  66.         size = xDim;
  67.        
  68.         line = stream.readLine();
  69.        
  70.         if(line.contains("BYTE"))
  71.         {
  72.             typeChoice = 0;
  73.             //we want to use uchars
  74.         }
  75.         else if(line.contains("FLOAT"))
  76.         {
  77.             typeChoice = 1;
  78.             //we want to use floats
  79.         }
  80.         else if(line.contains("SHORT"))
  81.         {
  82.             typeChoice = 2;
  83.             //we want to use shorts
  84.         }
  85.         else if(line.contains("INT"))
  86.         {
  87.             typeChoice = 3;
  88.             //we want to use ints
  89.         }
  90.         else if(line.contains("DOUBLE"))
  91.         {
  92.             typeChoice = 4;
  93.             //we want to use doubles
  94.         }
  95.        
  96.         line = stream.readLine();
  97.        
  98.         if(line.contains("VECTOR"))
  99.         {
  100.             isVector = 1;
  101.         }
  102.         theSource -> close();
  103.        
  104.         delete theSource;
  105.         filename.chop(4);
  106.         if(isVector == 1)
  107.         {
  108.            
  109.             convertedData = (float*)malloc((xDim * yDim * zDim) * sizeof(float));///
  110.             convertedVectorData = (float*)malloc((xDim * yDim * zDim * 3) * sizeof(float));
  111.             filename += ".vec";
  112.             theSource = new QFile(filename);
  113.             theSource -> open(QIODevice::ReadOnly);// | QIODevice::Text);// opens file
  114.             QDataStream in(theSource);
  115.             //char* s = (char*)malloc(1*sizeof(float));
  116.            
  117. //          for(int i = 0; i < (xDim*yDim*zDim * 3); i++)
  118. //          {
  119. //              if(typeChoice == 0)
  120. //              {
  121. //                  in >> currentNumberByte;
  122. //                  currentNumber = float(currentNumberByte);
  123. //              }
  124. //              else if(typeChoice == 1)
  125. //              {
  126. //                  in.readRawData(s, sizeof(float));
  127. //                  currentNumber = float(*s);
  128. //              }
  129. //              else if(typeChoice == 2)
  130. //              {
  131. //                  in >> currentNumberShort;
  132. //                  currentNumber = float(currentNumberShort);
  133. //              }
  134. //              else if(typeChoice == 3)
  135. //              {
  136. //                  in >> currentNumberInt;
  137. //                  currentNumber = float(currentNumberInt);
  138. //              }
  139. //              else
  140. //              {
  141. //                  in >> currentNumberDouble;
  142. //                  currentNumber = float(currentNumberDouble);
  143. //              }
  144. //             
  145. //              convertedVectorData[i] = currentNumber;
  146. //          }
  147.             FILE *myFile;
  148.             myFile = fopen( filename.toStdString().c_str(), "r");
  149.             if(myFile == NULL) qDebug() << "FUUUCK";
  150.             fread( convertedVectorData, sizeof(float), xDim*yDim*zDim*3, myFile);
  151.             for(int i = 0; i < (xDim*yDim*zDim); i++)
  152.             {
  153.                 X = convertedVectorData[(3*i) + 0];
  154.                 Y = convertedVectorData[(3*i) + 1];
  155.                 Z = convertedVectorData[(3*i) + 2];
  156.                 X *= 1000;
  157.                 Y *= 1000;
  158.                 Z *= 1000;
  159. //              qDebug() << X << Y << Z;
  160.                 currentNumber = pow((pow(X, 2)+pow(Y, 2)+pow(Z, 2)), 0.5);
  161. //              qDebug() << currentNumber;
  162.                 convertedData[i] = currentNumber;
  163. //              break;
  164.                 if(i == 0)//first run we store both into min and max of both
  165.                 {
  166.                     maxValue = currentNumber;
  167.                     minValue = currentNumber;
  168.                 }
  169.                 else
  170.                 {
  171.                     if(currentNumber > maxValue)
  172.                     {
  173.                         maxValue = currentNumber;
  174.                     }
  175.                     if(currentNumber < minValue)
  176.                     {
  177.                         minValue = currentNumber;
  178.                     }
  179.                 }
  180.             }
  181.            
  182.             makeCurrent();
  183.             //glEnable(GL_TEXTURE_3D);
  184.             glActiveTexture(GL_TEXTURE2);
  185.             glGenTextures(1, &texname2);
  186.             glBindTexture(GL_TEXTURE_3D, texname2);
  187.             glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  188.             glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  189.             glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
  190.             glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
  191.             glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER);
  192.             makeCurrent();
  193.             glActiveTexture(GL_TEXTURE2);
  194.             glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB32F_ARB, xDim, yDim, zDim, 0, GL_RGB, GL_FLOAT, convertedVectorData);
  195.            
  196.             vectorMap = convertedVectorData;
  197.            
  198.             ///generate noise texture!;
  199.             createNoiseTexture(xDim, yDim, zDim);
  200.            
  201.         }
  202.         else
  203.         {
  204.             convertedData = (float*)malloc((xDim * yDim * zDim) * sizeof(float));
  205.             filename += ".dat";
  206.             theSource = new QFile(filename);
  207.             theSource -> open(QIODevice::ReadOnly);// | QIODevice::Text);// opens file
  208.             QDataStream in(theSource);
  209.             char* s = (char*)malloc(1*sizeof(float));
  210.             ///load and convert
  211.            
  212.             for(int i = 0; i < (xDim*yDim*zDim) && !in.atEnd(); i++)
  213.             {
  214.                 if(typeChoice == 0)
  215.                 {
  216.                     in >> currentNumberByte;
  217.                     currentNumber = float(currentNumberByte);
  218.                 }
  219.                 else if(typeChoice == 1)
  220.                 {
  221.                     //in >> currentNumberFloat;
  222.                     in.readRawData(s, sizeof(float));
  223.                     currentNumber = float(*s);
  224.                     //currentNumber = float(currentNumberFloat);
  225.                 }
  226.                 else if(typeChoice == 2)
  227.                 {
  228.                     in >> currentNumberShort;
  229.                     currentNumber = float(currentNumberShort);
  230.                 }
  231.                 else if(typeChoice == 3)
  232.                 {
  233.                     in >> currentNumberInt;
  234.                     currentNumber = float(currentNumberInt);
  235.                 }
  236.                 else
  237.                 {
  238.                     in >> currentNumberDouble;
  239.                     currentNumber = float(currentNumberDouble);
  240.                 }
  241.                
  242.                 convertedData[i] = currentNumber;
  243.     //          if(currentNumber <= 0)
  244.     //          {
  245.     //              qDebug() << currentNumber << endl;
  246.     //          }
  247.                 if(i == 0)//first run we store both into min and max of both
  248.                 {
  249.                     maxValue = currentNumber;
  250.                     minValue = currentNumber;
  251.                 }
  252.                 else
  253.                 {
  254.                     if(currentNumber > maxValue)
  255.                     {
  256.                         maxValue = currentNumber;
  257.                     }
  258.                     if(currentNumber < minValue)
  259.                     {
  260.                         minValue = currentNumber;
  261.                     }
  262.                 }
  263.             }
  264.         }
  265.        
  266.        
  267.        
  268.        
  269.        
  270.         ///normalize
  271.         //qDebug() << maxValue << endl;
  272.         //qDebug() << minValue << endl;
  273.         if(minValue < 0)
  274.         {
  275.             minValue = fabs(minValue);
  276.             maxValue = fabs(maxValue);
  277.             maxValue = minValue + maxValue;
  278.         }
  279.         else
  280.         {
  281.             minValue = 0;
  282.         }
  283.         for(int i = 0; i < (xDim*yDim*zDim); i++)
  284.         {
  285.            
  286.            
  287.             convertedData[i] = ((convertedData[i]) + minValue)/float(maxValue);
  288.         }
  289.         makeCurrent();
  290.         glActiveTexture(GL_TEXTURE1);
  291.         //glEnable(GL_TEXTURE_3D);
  292.         if(fileSelected == 0)
  293.         {
  294.             makeCurrent();
  295.             //glEnable(GL_TEXTURE_3D);
  296.             glGenTextures(1, &texname);
  297.             glBindTexture(GL_TEXTURE_3D, texname);
  298.             glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  299.             glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  300.             glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
  301.             glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
  302.             glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER);
  303.         }
  304.         makeCurrent();
  305.         glActiveTexture(GL_TEXTURE1);
  306.         glTexImage3D(GL_TEXTURE_3D, 0, GL_LUMINANCE, xDim, yDim, zDim, 0, GL_LUMINANCE, GL_FLOAT, convertedData);
  307.         fileSelected = 1;
  308.         theSource -> close();
  309.         //qDebug() << maxValue << endl;
  310.         emit(newFileLoaded(convertedData, xDim, yDim, zDim, maxValue));
  311.        
  312.         delete theSource;
  313.        
  314.         sizex = xDim;
  315.         sizey = yDim;
  316.         sizez = zDim;
  317.        
  318.         size = xDim;
  319.         if(yDim > xDim)
  320.         {
  321.             size = yDim;
  322.         }
  323.         if(zDim > yDim)
  324.         {
  325.             size = zDim;
  326.         }
  327.        
  328.         maxRange = maxValue;
  329.         //free(convertedData);
  330.         //qDebug() << "got here yo" << maxRange << endl;
  331.     }
  332.    
  333.     zoomAmount = size * 2.5;
  334.    
  335.     reloadShaders();
  336.    
  337.     updateGL();
  338. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement