mpri-graphics-project/Mesh.hpp

71 lines
2.1 KiB
C++
Raw Normal View History

/**
* Defines a mesh, ready to be OpenGL-rendered
**/
2018-02-06 19:07:07 +01:00
#pragma once
#include <vector>
2018-01-27 16:28:36 +01:00
#include <unordered_map>
#include <cstddef> // size_t
2018-01-27 15:23:05 +01:00
#include "common_structures.hpp"
class Mesh {
public:
Mesh();
/// Adds a fresh vertice at `pt`, and returns its ID for further use
size_t add_vertice(const Point& pt);
2018-02-13 14:01:57 +01:00
size_t add_vertice(const Point& pt, const Point& normal);
/// Creates a new face out of the three given point IDs
void add_face(const Face& face);
void add_face(size_t f1, size_t f2, size_t f3);
/// Center manipulation
const Point& get_center() const;
void set_center(const Point& pt);
void translate(const Point& tr); ///< Translate by the vector `tr`
/** Translates the vertices to make (0, 0, 0) the mesh's barycenter.
* If `keep_position == true`, this also sets the center to the
* previous barycenter.
*/
void normalize_center(bool keep_position=false);
2018-02-13 14:01:57 +01:00
/// Get the normal vector for vertice `vert_id`
const Point& get_normal(size_t vert_id);
/// Gets the various vertices
const std::vector<Point>& get_vertices() const;
/// Gets the various faces
const std::vector<Face>& get_faces() const;
const Color& get_color() const { return color; }
void set_color(const Color& _color) { color = _color; }
2018-02-13 14:01:57 +01:00
private: //struct
struct NormalVect {
NormalVect(const Point& vect, bool manual=false)
: vect(vect), dirty(false), manual(manual) {}
NormalVect()
: vect(Point(0., 0., 0.)), dirty(true), manual(false) {}
Point vect;
bool dirty;
bool manual;
};
private: //meth
size_t inner_add_vertice(const Point& pt, const NormalVect& normal);
void compute_normal(size_t vert);
private:
std::vector<Point> vertices;
2018-02-13 14:01:57 +01:00
std::vector<NormalVect> normals;
std::vector<Face> faces;
2018-02-13 14:01:57 +01:00
std::vector<std::vector<size_t> > faces_with_vert;
Point center;
Color color;
};