#ifndef MESH_H
#define MESH_H
#include <vector>
#include "geometry.h"
class Mesh {
public:
class Vertex {
public:
Vector3 pos;
Vector3 normal;
explicit inline Vertex(const Vector3 &pos = Vector3(), const Vector3 &normal = Vector3()):
pos(pos), normal(normal) { }
};
class Triangle {
public:
int v[3];
explicit inline Triangle(int v0 = 0, int v1 = 0, int v2 = 0)
{ v[0] = v0; v[1] = v1; v[2] = v2; }
};
typedef std::vector<Vertex> VertexList;
typedef std::vector<Triangle> TriangleList;
VertexList vertices;
TriangleList triangles;
mutable unsigned int vbuf, ibuf, elements;
inline Mesh(): vbuf(), ibuf() { }
inline ~Mesh() { dirty(); }
void createbuf() const;
void dirty() const;
void clear();
void draw() const;
};
class LinkedMesh {
public:
class Vertex {
public:
Vector3 pos;
Real value;
explicit inline Vertex(const Vector3 &pos = Vector3(), Real value = Real()):
pos(pos), value(value) { }
};
class Edge {
public:
int v[3];
int e[2];
explicit inline Edge(int v0 = 0, int v2 = 0)
{ v[0] = v0; v[1] = -1; v[2] = v2; e[0] = 0; e[1] = 0; }
};
class Triangle {
public:
int e[3];
bool d[3];
explicit inline Triangle(
int e0 = 0, bool d0 = false,
int e1 = 0, bool d1 = false,
int e2 = 0, bool d2 = false )
{
e[0] = e0; e[1] = e1; e[2] = e2;
d[0] = d0; d[1] = d1; d[2] = d2;
}
};
class Level {
public:
Real k;
int vertex0, vertex1;
int edge0, edge1;
int triangle0, triangle1;
Level():
k(1),
vertex0(), vertex1(),
edge0(), edge1(),
triangle0(), triangle1() { }
};
typedef std::vector<Vertex> VertexList;
typedef std::vector<Edge> EdgeList;
typedef std::vector<Triangle> TriangleList;
typedef std::vector<Level> LevelList;
typedef Vector3 (*VertexFunc)(const Vertex&, Real, Real);
VertexList vertices;
EdgeList edges;
TriangleList triangles;
LevelList levels;
static Vertex average(const Vertex &a, const Vertex &b);
static Vector3 sphere_func(const Vertex &v, Real r, Real kr);
static Vector3 z_func(const Vertex &v, Real r, Real kr);
static Real random();
void check() const;
void clear();
void next_level(Real k);
void generate_level(Real k = sqrt2);
void generate_levels(int count, Real k = sqrt2);
void smooth();
void smooth(int count);
void triangle(Real k = 1);
void tetrahedron(Real k = 1);
void icosahedron(Real k = 1);
void to_mesh(Mesh &mesh, VertexFunc func, Real r = 0, Real kr = 1, int level = -1) const;
inline void to_sphere_mesh(Mesh &mesh, Real r = 1, Real kr = 0.5, int level = -1) const
{ to_mesh(mesh, &sphere_func, r, kr, level); }
inline void to_z_mesh(Mesh &mesh, Real r = 0, Real kr = 1, int level = -1) const
{ to_mesh(mesh, &z_func, r, kr, level); }
};
#endif