Implement basic mesh functions

This commit is contained in:
Théophile Bastian 2018-01-27 16:28:36 +01:00
parent ba7245eeea
commit a1347434e7
3 changed files with 64 additions and 2 deletions

View file

@ -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<Point>& Mesh::get_vertices() const {
return vertices;
}
const std::vector<Face>& Mesh::get_faces() const {
return faces;
}

View file

@ -3,6 +3,8 @@
**/
#include <vector>
#include <unordered_map>
#include <cstddef> // size_t
#include "common_structures.hpp"
class Mesh {
@ -24,5 +26,6 @@ class Mesh {
private:
std::vector<Point> vertices;
std::unordered_map<Point, size_t> rev_vertices;
std::vector<Face> faces;
};

View file

@ -2,13 +2,45 @@
* Defines a few widely used, widely spread structures. Imported pervasively.
**/
#include <functional> // 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<Point> { typedef Point argument_type;
typedef std::size_t result_type;
result_type operator()(const argument_type& pt) const noexcept {
return (hash<double>()(pt.x) << 2)
^ (hash<double>()(pt.y) << 1)
^ hash<double>()(pt.z);
}
};
template<> struct hash<Face>
{
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<int>{}(s[0]) );
result_type const h2 ( std::hash<int>{}(s[1]) );
result_type const h3 ( std::hash<int>{}(s[2]) );
return h1 ^ h2 ^ h3;
}
};
}