Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- // Std. Includes
- #include <string>
- #include <fstream>
- #include <sstream>
- #include <iostream>
- #include <map>
- #include <vector>
- using namespace std;
- // GL Includes
- #include <SOIL.h>
- #include <glew.h> // Contains all the necessery OpenGL includes
- #include <glm.hpp>
- #include <gtc/matrix_transform.hpp>
- #include <assimp/Importer.hpp>
- #include <assimp/scene.h>
- #include <assimp/postprocess.h>
- #include "Mesh.h"
- #include "MeshPathlines.h"
- class ModelPathlines
- {
- public:
- /* Functions */
- // Constructor, expects a filepath to a 3D model.
- ModelPathlines(GLchar* path)
- {
- this->loadModel(path);
- }
- // Draws the model, and thus all its meshes
- void Draw(Shader shader)
- {
- for (GLuint i = 0; i < this->meshes.size(); i++)
- this->meshes[i].Draw(shader);
- }
- void DrawOIT(Shader shader)
- {
- for (GLuint i = 0; i < this->meshes.size(); i++)
- this->meshes[i].Draw(shader);
- }
- glm::vec3 getMeshcenter() {
- return glm::vec3(this->meshcenter[0], this->meshcenter[1], this->meshcenter[2]);
- }
- glm::vec3 getMinMaxVelocity() {//returns min and max velocity at indicies 0 and 1 and the longest time Range a pixel occupied
- return glm::vec3(this->minMaxVelocity[0], this->minMaxVelocity[1], this->minMaxVelocity[2]);
- }
- private:
- /* Model Data */
- vector<GLdouble> meshcenter;
- vector<GLfloat> minMaxVelocity;
- vector<MeshPathlines> meshes;
- string directory;
- vector<Texture> textures_loaded; // Stores all the textures loaded so far, optimization to make sure textures aren't loaded more than once.
- /* Functions */
- // Loads a model with supported ASSIMP extensions from file and stores the resulting meshes in the meshes vector.
- void loadModel(string path)
- {
- //read file with vtk polydata reader
- vtkSmartPointer<vtkXMLPolyDataReader> reader = vtkSmartPointer<vtkXMLPolyDataReader>::New();
- const char* filepath = path.c_str();
- reader->SetFileName(filepath);
- reader->Update();
- // Retrieve the directory path of the filepath
- this->directory = path.substr(0, path.find_last_of('/'));
- vtkSmartPointer<vtkPolyData> polydata = reader->GetOutput();
- this->processNodesFromVtkPolydataReader(polydata);
- }
- void processNodesFromVtkPolydataReader(vtkSmartPointer<vtkPolyData> polydata) {
- vtkSmartPointer<vtkPointData> polydata_pointdata = polydata->GetPointData();
- const int numLines = polydata->GetNumberOfCells();
- int numVertTotal = 0;
- for (int i = 0; i < numLines; ++i)
- {
- numVertTotal += polydata->GetCell(i)->GetNumberOfPoints();
- }
- const int num_properties = polydata_pointdata->GetNumberOfArrays();
- vtkSmartPointer<vtkDataArray> time = nullptr;
- vtkSmartPointer<vtkDataArray> velocity = nullptr;
- //Find Time and Velocity
- for (int i = 0; i < num_properties; ++i)
- {
- if (strcmp(polydata_pointdata->GetArrayName(i), "time") == 0)
- {
- time = polydata_pointdata->GetArray(i);
- }
- else if (strcmp(polydata_pointdata->GetArrayName(i), "velocity") == 0)
- {
- velocity = polydata_pointdata->GetArray(i);
- }
- }
- //initialize Meshcenter Vector
- this->meshcenter.push_back(0.0);
- this->meshcenter.push_back(0.0f);
- this->meshcenter.push_back(0.0f);
- //initialize MinMaxVelocityVector
- this->minMaxVelocity.push_back(0.0f);
- this->minMaxVelocity.push_back(0.0f);
- this->minMaxVelocity.push_back(0.0f);
- // Data to fill
- vector<VertexPathlines> vertices;
- vector<GLuint> indices;
- vector<Texture> textures;
- std::vector<GLfloat> vertVelo;
- vertVelo.reserve(numVertTotal * (4 + 3 + 1));
- //std::vector<GLuint> indices;
- //indices.reserve(numVertTotal + numLines);
- //GLuint indCnt = 0;
- minMaxVelocity[0] = std::numeric_limits<GLfloat>::max();
- minMaxVelocity[1] = -minMaxVelocity[0];
- minMaxVelocity[2] = 0.0f;
- glm::vec3 vector;
- glm::vec3 vectorNext;
- glm::vec3 vectorLast;
- glm::vec2 timeAndVelocity;
- glm::vec3 tangent;
- VertexPathlines vertex;
- GLuint indCnt = 0;
- for (int l = 0; l < numLines; ++l)//per line
- {
- if (l % 1 == 0) {
- vtkSmartPointer<vtkCell> p = polydata->GetCell(l);
- const int numVert = p->GetNumberOfPoints();
- double* ptcoords_spatial_next;
- double* ptcoords_spatial_last;
- for (int v = 0; v < numVert; ++v)
- {
- const int ptId = p->GetPointId(v);
- int ptIdLast = 0;
- int ptIdNext = 0;
- //get point ids of adjacent points
- if (v > 0 && v < numVert - 1) {
- ptIdNext = p->GetPointId(v + 1);
- ptcoords_spatial_next = polydata->GetPoint(ptIdNext);
- vectorNext.x = static_cast<GLfloat>(ptcoords_spatial_next[0]);
- vectorNext.y = static_cast<GLfloat>(ptcoords_spatial_next[1]);
- vectorNext.z = static_cast<GLfloat>(ptcoords_spatial_next[2]);
- ptIdLast = p->GetPointId(v - 1);
- ptcoords_spatial_last = polydata->GetPoint(ptIdLast);
- vectorLast.x = static_cast<GLfloat>(ptcoords_spatial_last[0]);
- vectorLast.y = static_cast<GLfloat>(ptcoords_spatial_last[1]);
- vectorLast.z = static_cast<GLfloat>(ptcoords_spatial_last[2]);
- }
- else {
- if (v == 0) {
- ptIdNext = p->GetPointId(v + 1);
- ptcoords_spatial_next = polydata->GetPoint(ptIdNext);
- vectorNext.x = static_cast<GLfloat>(ptcoords_spatial_next[0]);
- vectorNext.y = static_cast<GLfloat>(ptcoords_spatial_next[1]);
- vectorNext.z = static_cast<GLfloat>(ptcoords_spatial_next[2]);
- }
- if (v == numVert - 1) {
- ptIdLast = p->GetPointId(v - 1);
- ptcoords_spatial_last = polydata->GetPoint(ptIdLast);
- vectorLast.x = static_cast<GLfloat>(ptcoords_spatial_last[0]);
- vectorLast.y = static_cast<GLfloat>(ptcoords_spatial_last[1]);
- vectorLast.z = static_cast<GLfloat>(ptcoords_spatial_last[2]);
- }
- }
- const double* ptcoords_spatial = polydata->GetPoint(ptId);//coordinates
- const double ptcoords_temporal = time->GetTuple1(ptId);//time
- const GLfloat velo = velocity->GetTuple1(ptId);//velo
- vector.x = static_cast<GLfloat>(ptcoords_spatial[0]);
- vector.y = static_cast<GLfloat>(ptcoords_spatial[1]);
- vector.z = static_cast<GLfloat>(ptcoords_spatial[2]);
- vertex.Position = vector;
- meshcenter[0] += vector.x / numVertTotal;
- meshcenter[1] += vector.y / numVertTotal;
- meshcenter[2] += vector.z / numVertTotal;
- // Time and Velocity
- timeAndVelocity.x = static_cast<GLfloat>(ptcoords_temporal); //Time
- timeAndVelocity.y = velo; //Velocity
- vertex.TimeAndVelocity = timeAndVelocity;
- //vertex.Tangents = glm::vec3(1.0f, 0.0f, 0.0f);
- //Handle Tangents
- if (v == 0 || v == numVert - 1) {
- if (v == 0) {
- vertex.Tangents = vectorNext - vector;
- }
- else {
- vertex.Tangents = vector - vectorLast;
- }
- }
- else {
- glm::vec3 w = vectorNext - vectorLast;
- w = glm::normalize(w);
- vertex.Tangents = w;
- }
- minMaxVelocity[0] = std::min(velo, minMaxVelocity[0]);
- minMaxVelocity[1] = std::max(velo, minMaxVelocity[1]);
- minMaxVelocity[2] = std::max(minMaxVelocity[2], static_cast<GLfloat>(ptcoords_temporal));//time Range
- indices.push_back(static_cast<GLuint>(indCnt++));//indices
- vertices.push_back(vertex);
- }
- indices.push_back(std::numeric_limits<GLuint>::max());
- }
- }
- GLuint maxNumLim = std::numeric_limits<GLuint>::max();
- //compute meshcenter
- //meshcenter[0] /= vertices.size();
- //meshcenter[1] /= vertices.size();
- //meshcenter[2] /= vertices.size();
- MeshPathlines m = MeshPathlines(vertices, indices, textures);
- this->meshes.push_back(m);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement