Advertisement
Guest User

ModelPathlines

a guest
Dec 8th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #pragma once
  2. // Std. Includes
  3. #include <string>
  4. #include <fstream>
  5. #include <sstream>
  6. #include <iostream>
  7. #include <map>
  8. #include <vector>
  9. using namespace std;
  10. // GL Includes
  11. #include <SOIL.h>
  12. #include <glew.h> // Contains all the necessery OpenGL includes
  13. #include <glm.hpp>
  14. #include <gtc/matrix_transform.hpp>
  15. #include <assimp/Importer.hpp>
  16. #include <assimp/scene.h>
  17. #include <assimp/postprocess.h>
  18.  
  19. #include "Mesh.h"
  20. #include "MeshPathlines.h"
  21.  
  22. class ModelPathlines
  23. {
  24. public:
  25.     /*  Functions   */
  26.     // Constructor, expects a filepath to a 3D model.
  27.     ModelPathlines(GLchar* path)
  28.     {
  29.         this->loadModel(path);
  30.     }
  31.  
  32.     // Draws the model, and thus all its meshes
  33.     void Draw(Shader shader)
  34.     {
  35.         for (GLuint i = 0; i < this->meshes.size(); i++)
  36.             this->meshes[i].Draw(shader);
  37.     }
  38.  
  39.     void DrawOIT(Shader shader)
  40.     {
  41.         for (GLuint i = 0; i < this->meshes.size(); i++)
  42.             this->meshes[i].Draw(shader);
  43.     }
  44.  
  45.     glm::vec3 getMeshcenter() {
  46.         return glm::vec3(this->meshcenter[0], this->meshcenter[1], this->meshcenter[2]);
  47.     }
  48.  
  49.     glm::vec3 getMinMaxVelocity() {//returns min and max velocity at indicies 0 and 1 and the longest time Range a pixel occupied
  50.         return glm::vec3(this->minMaxVelocity[0], this->minMaxVelocity[1], this->minMaxVelocity[2]);
  51.     }
  52.  
  53. private:
  54.     /*  Model Data  */
  55.     vector<GLdouble> meshcenter;
  56.     vector<GLfloat> minMaxVelocity;
  57.     vector<MeshPathlines> meshes;
  58.     string directory;
  59.     vector<Texture> textures_loaded;    // Stores all the textures loaded so far, optimization to make sure textures aren't loaded more than once.
  60.  
  61.     /*  Functions   */
  62.     // Loads a model with supported ASSIMP extensions from file and stores the resulting meshes in the meshes vector.
  63.     void loadModel(string path)
  64.     {
  65.         //read file with vtk polydata reader
  66.         vtkSmartPointer<vtkXMLPolyDataReader> reader = vtkSmartPointer<vtkXMLPolyDataReader>::New();
  67.         const char* filepath = path.c_str();
  68.         reader->SetFileName(filepath);
  69.         reader->Update();
  70.  
  71.         // Retrieve the directory path of the filepath
  72.         this->directory = path.substr(0, path.find_last_of('/'));
  73.  
  74.         vtkSmartPointer<vtkPolyData> polydata = reader->GetOutput();
  75.  
  76.         this->processNodesFromVtkPolydataReader(polydata);
  77.     }
  78.  
  79.     void processNodesFromVtkPolydataReader(vtkSmartPointer<vtkPolyData> polydata) {
  80.        
  81.         vtkSmartPointer<vtkPointData> polydata_pointdata = polydata->GetPointData();
  82.  
  83.         const int numLines = polydata->GetNumberOfCells();
  84.  
  85.         int numVertTotal = 0;
  86.         for (int i = 0; i < numLines; ++i)
  87.         {
  88.             numVertTotal += polydata->GetCell(i)->GetNumberOfPoints();
  89.         }
  90.  
  91.         const int num_properties = polydata_pointdata->GetNumberOfArrays();
  92.         vtkSmartPointer<vtkDataArray> time = nullptr;
  93.         vtkSmartPointer<vtkDataArray> velocity = nullptr;
  94.  
  95.         //Find Time and Velocity
  96.         for (int i = 0; i < num_properties; ++i)
  97.         {
  98.             if (strcmp(polydata_pointdata->GetArrayName(i), "time") == 0)
  99.             {
  100.                 time = polydata_pointdata->GetArray(i);
  101.             }
  102.             else if (strcmp(polydata_pointdata->GetArrayName(i), "velocity") == 0)
  103.             {
  104.                 velocity = polydata_pointdata->GetArray(i);
  105.             }
  106.         }
  107.  
  108.         //initialize Meshcenter Vector
  109.         this->meshcenter.push_back(0.0);
  110.         this->meshcenter.push_back(0.0f);
  111.         this->meshcenter.push_back(0.0f);
  112.         //initialize MinMaxVelocityVector
  113.         this->minMaxVelocity.push_back(0.0f);
  114.         this->minMaxVelocity.push_back(0.0f);
  115.         this->minMaxVelocity.push_back(0.0f);
  116.  
  117.         // Data to fill
  118.         vector<VertexPathlines> vertices;
  119.         vector<GLuint> indices;
  120.         vector<Texture> textures;
  121.  
  122.         std::vector<GLfloat> vertVelo;
  123.         vertVelo.reserve(numVertTotal * (4 + 3 + 1));
  124.  
  125.         //std::vector<GLuint> indices;
  126.         //indices.reserve(numVertTotal + numLines);
  127.         //GLuint indCnt = 0;
  128.  
  129.         minMaxVelocity[0] = std::numeric_limits<GLfloat>::max();
  130.         minMaxVelocity[1] = -minMaxVelocity[0];
  131.         minMaxVelocity[2] = 0.0f;
  132.  
  133.         glm::vec3 vector;
  134.         glm::vec3 vectorNext;
  135.         glm::vec3 vectorLast;
  136.         glm::vec2 timeAndVelocity;
  137.         glm::vec3 tangent;
  138.         VertexPathlines vertex;
  139.         GLuint indCnt = 0;
  140.  
  141.         for (int l = 0; l < numLines; ++l)//per line
  142.         {
  143.           if (l % 1 == 0) {
  144.             vtkSmartPointer<vtkCell> p = polydata->GetCell(l);
  145.             const int numVert = p->GetNumberOfPoints();
  146.  
  147.             double* ptcoords_spatial_next;
  148.             double* ptcoords_spatial_last;
  149.  
  150.             for (int v = 0; v < numVert; ++v)
  151.             {
  152.                 const int ptId = p->GetPointId(v);
  153.                 int ptIdLast = 0;
  154.                 int ptIdNext = 0;
  155.                 //get point ids of adjacent points
  156.                 if (v > 0 && v < numVert - 1) {
  157.                     ptIdNext = p->GetPointId(v + 1);
  158.                     ptcoords_spatial_next = polydata->GetPoint(ptIdNext);
  159.  
  160.                     vectorNext.x = static_cast<GLfloat>(ptcoords_spatial_next[0]);
  161.                     vectorNext.y = static_cast<GLfloat>(ptcoords_spatial_next[1]);
  162.                     vectorNext.z = static_cast<GLfloat>(ptcoords_spatial_next[2]);
  163.  
  164.                     ptIdLast = p->GetPointId(v - 1);
  165.                     ptcoords_spatial_last = polydata->GetPoint(ptIdLast);
  166.                     vectorLast.x = static_cast<GLfloat>(ptcoords_spatial_last[0]);
  167.                     vectorLast.y = static_cast<GLfloat>(ptcoords_spatial_last[1]);
  168.                     vectorLast.z = static_cast<GLfloat>(ptcoords_spatial_last[2]);
  169.                 }
  170.                 else {
  171.                     if (v == 0) {
  172.                         ptIdNext = p->GetPointId(v + 1);
  173.                         ptcoords_spatial_next = polydata->GetPoint(ptIdNext);
  174.  
  175.                         vectorNext.x = static_cast<GLfloat>(ptcoords_spatial_next[0]);
  176.                         vectorNext.y = static_cast<GLfloat>(ptcoords_spatial_next[1]);
  177.                         vectorNext.z = static_cast<GLfloat>(ptcoords_spatial_next[2]);
  178.  
  179.                     }
  180.                     if (v == numVert - 1) {
  181.                         ptIdLast = p->GetPointId(v - 1);
  182.                         ptcoords_spatial_last = polydata->GetPoint(ptIdLast);
  183.  
  184.                         vectorLast.x = static_cast<GLfloat>(ptcoords_spatial_last[0]);
  185.                         vectorLast.y = static_cast<GLfloat>(ptcoords_spatial_last[1]);
  186.                         vectorLast.z = static_cast<GLfloat>(ptcoords_spatial_last[2]);
  187.                     }
  188.  
  189.                 }
  190.  
  191.                 const double* ptcoords_spatial = polydata->GetPoint(ptId);//coordinates
  192.                 const double ptcoords_temporal = time->GetTuple1(ptId);//time
  193.                 const GLfloat velo = velocity->GetTuple1(ptId);//velo
  194.  
  195.                 vector.x = static_cast<GLfloat>(ptcoords_spatial[0]);
  196.                 vector.y = static_cast<GLfloat>(ptcoords_spatial[1]);
  197.                 vector.z = static_cast<GLfloat>(ptcoords_spatial[2]);
  198.  
  199.                 vertex.Position = vector;
  200.  
  201.                 meshcenter[0] += vector.x / numVertTotal;
  202.                 meshcenter[1] += vector.y / numVertTotal;
  203.                 meshcenter[2] += vector.z / numVertTotal;
  204.  
  205.                 // Time and Velocity
  206.                 timeAndVelocity.x = static_cast<GLfloat>(ptcoords_temporal); //Time
  207.                 timeAndVelocity.y = velo; //Velocity
  208.  
  209.                 vertex.TimeAndVelocity = timeAndVelocity;
  210.  
  211.                 //vertex.Tangents = glm::vec3(1.0f, 0.0f, 0.0f);
  212.                 //Handle Tangents
  213.                 if (v == 0 || v == numVert - 1) {
  214.                     if (v == 0) {
  215.                         vertex.Tangents = vectorNext - vector;
  216.                     }
  217.                     else {
  218.                         vertex.Tangents = vector - vectorLast;
  219.                     }
  220.                 }
  221.                 else {
  222.                     glm::vec3 w = vectorNext - vectorLast;
  223.                     w = glm::normalize(w);
  224.                     vertex.Tangents = w;
  225.                 }
  226.  
  227.                 minMaxVelocity[0] = std::min(velo, minMaxVelocity[0]);
  228.                 minMaxVelocity[1] = std::max(velo, minMaxVelocity[1]);
  229.                 minMaxVelocity[2] = std::max(minMaxVelocity[2], static_cast<GLfloat>(ptcoords_temporal));//time Range
  230.  
  231.                 indices.push_back(static_cast<GLuint>(indCnt++));//indices
  232.  
  233.                 vertices.push_back(vertex);
  234.             }
  235.             indices.push_back(std::numeric_limits<GLuint>::max());
  236.           }
  237.         }
  238.         GLuint maxNumLim = std::numeric_limits<GLuint>::max();
  239.         //compute meshcenter
  240.         //meshcenter[0] /= vertices.size();
  241.         //meshcenter[1] /= vertices.size();
  242.         //meshcenter[2] /= vertices.size();
  243.  
  244.         MeshPathlines m = MeshPathlines(vertices, indices, textures);
  245.  
  246.         this->meshes.push_back(m);
  247.     }
  248.  
  249. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement