diff --git a/Mesh/SurfaceMesh.cpp b/Mesh/SurfaceMesh.cpp index fd4f84ea17d236fe56d4157e55347d6c107d5c8e..04ba06d6dfbfe36737d1609c6626e1e2bc0b0a41 100644 --- a/Mesh/SurfaceMesh.cpp +++ b/Mesh/SurfaceMesh.cpp @@ -44,7 +44,7 @@ void SurfaceMesh::print() std::cout << "----------------------------\n"; std::cout << "Mesh Info for : " << this->getName() <<"\n\t"; std::cout << "Num. vertices : " << this->getNumberOfVertices() <<"\n\t"; - std::cout << "Num. triangles : " << this->triangles.size() << "\n\t"; + std::cout << "Num. triangles : " << this->getTriangles().size() << "\n\t"; std::cout << "Is mesh textured: " << this->isMeshTextured() << "\n"; std::cout << "----------------------------\n"; } diff --git a/Mesh/SurfaceMesh.h b/Mesh/SurfaceMesh.h index 5ea7e6ab9caba8c81f7b323059a802198e2c78ea..83ca117d7a2473dbd1643381ef2e9e73eff51041 100644 --- a/Mesh/SurfaceMesh.h +++ b/Mesh/SurfaceMesh.h @@ -43,9 +43,91 @@ public: virtual ~SurfaceMesh(); /// - /// \brief load the surface mesh + /// \brief calculates the normal of a triangle /// - bool load(const std::string& fileName); + core::Vec3d computeTriangleNormal(int triangle) + { + auto t = this->triangleArray[triangle]; + + const core::Vec3d &v0 = this->vertices[t[0]]; + + return (this->vertices[t[1]]-v0).cross(this->vertices[t[2]]-v0).normalized(); + } + + /// + /// \brief Calculate normals for all triangles + /// + void computeTriangleNormals() + { + triangleNormals.clear(); + for(const auto &t : this->triangleArray) + { + const core::Vec3d &v0 = this->vertices[t[0]]; + + this->triangleNormals.push_back(this->vertices[t[1]]-v0).cross(this->vertices[t[2]]-v0).normalized(); + } + } + + /// + /// \brief Calculate normals for all triangles + /// + void computeVertexNormals() + { + for (size_t i = 0, end = this->vertices.size(); i < end; ++i) + { + this->vertexNormals.push_back(core::Vec3d::Zero()); + for(auto const &j : this->vertexTriangleNeighbors[i]) + { + this->vertexNormals[i] += this->triangleNormals[j]; + } + this->vertexNormals[i].normalize(); + } + } + + /// + /// \brief Calculate vertex neighbors + /// + void computeVertexNeighbors() + { + vertexNeighbors.resize(this->vertices.size()); + + if(vertexTriangleNeighbors.size() == 0) + { + this->computeVertexTriangleNeighbors(); + } + + for (size_t i = 0, end = this->vertices.size(); i < end; ++i) + { + for (auto const &j : this->vertexTriangleNeighbors[i]) + { + for(auto const &vertex : this->triangleArray[j]) + { + if(vertex != i) + { + this->vertexNeighbors[i].push_back(vertex); + } + } + } + } + } + + /// + /// \brief initializes the vertex neighbors + /// + void computeVertexTriangleNeighbors() + { + vertexTriangleNeighbors.resize(this->vertices.size()); + + int triangle = 0; + for(auto const &t : this->triangleArray) + { + vertexTriangleNeighbors[t[0]].push_back(triangle); + vertexTriangleNeighbors[t[1]].push_back(triangle); + vertexTriangleNeighbors[t[2]].push_back(triangle); + triangle++; + } + } + /// /// \brief print the details of the mesh @@ -56,9 +138,6 @@ private: // SurfaceMesh class specific errors here std::shared_ptr<ErrorLog> logger; - // List of triangles - std::vector<std::array<int,3>> triangles; - // List of triangle normals std::vector<core::Vec3d> triangleNormals; @@ -70,6 +149,14 @@ private: // List of vertex tangents std::vector<core::Vec3d> vertexTangents; + + // List of vertex neighbors + std::vector<std::vector<size_t>> vertexNeighbors; + + // List of vertex neighbors + std::vector<std::vector<size_t>> vertexTriangleNeighbors; }; #endif + +