diff --git a/Mesh.cpp b/Mesh.cpp index ddab49e..125ab80 100644 --- a/Mesh.cpp +++ b/Mesh.cpp @@ -1,3 +1,30 @@ -#include "Mesh.h" +#include "Mesh.hpp" +Mesh::Mesh() +{ +} +size_t Mesh::add_vertice(const Point& pt) { + // Does the vertice already exists? + if (rev_vertices.find(pt) != rev_vertices.end()) + return rev_vertices.find(pt)->second; + + vertices.push_back(pt); + rev_vertices.insert(std::make_pair(pt, vertices.size() - 1)); + return vertices.size() - 1; +} + +void Mesh::add_face(const Face& face) { + faces.push_back(face); +} +void Mesh::add_face(size_t f1, size_t f2, size_t f3) { + add_face(Face(f1, f2, f3)); +} + +const std::vector& Mesh::get_vertices() const { + return vertices; +} + +const std::vector& Mesh::get_faces() const { + return faces; +} diff --git a/Mesh.hpp b/Mesh.hpp index 6e1843a..f66c9d9 100644 --- a/Mesh.hpp +++ b/Mesh.hpp @@ -3,6 +3,8 @@ **/ #include +#include +#include // size_t #include "common_structures.hpp" class Mesh { @@ -24,5 +26,6 @@ class Mesh { private: std::vector vertices; + std::unordered_map rev_vertices; std::vector faces; }; diff --git a/common_structures.hpp b/common_structures.hpp index 0743cb9..173679f 100644 --- a/common_structures.hpp +++ b/common_structures.hpp @@ -2,13 +2,45 @@ * Defines a few widely used, widely spread structures. Imported pervasively. **/ +#include // Hash + struct Point { + Point(double x, double y, double z) : x(x), y(y), z(z) {} double x, y, z; }; struct Face { + Face(int v0, int v1, int v2) { + vert[0] = v0; + vert[1] = v1; + vert[2] = v2; + } int vert[3]; - int operator[](unsigned i) { + int operator[](unsigned i) const { return vert[i % 3]; // dodge errors } }; + +namespace std { + template<> struct hash { typedef Point argument_type; + typedef std::size_t result_type; + result_type operator()(const argument_type& pt) const noexcept { + return (hash()(pt.x) << 2) + ^ (hash()(pt.y) << 1) + ^ hash()(pt.z); + } + }; + + template<> struct hash + { + typedef Face argument_type; + typedef std::size_t result_type; + result_type operator()(argument_type const& s) const noexcept + { + result_type const h1 ( std::hash{}(s[0]) ); + result_type const h2 ( std::hash{}(s[1]) ); + result_type const h3 ( std::hash{}(s[2]) ); + return h1 ^ h2 ^ h3; + } + }; +}