mesh.hpp
Go to the documentation of this file.
1 /*
2 ** TP CPE Lyon
3 ** Copyright (C) 2014 Damien Rohmer
4 **
5 ** This program is free software: you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation, either version 3 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 
21 #ifndef MESH_HPP
22 #define MESH_HPP
23 
24 #include "../libcommon/exception_cpe.hpp"
25 #include "../lib3d/vec3.hpp"
26 #include "../lib3d/vec2.hpp"
27 
28 #include <vector>
29 #include <string>
30 
31 
32 
33 namespace cpe
34 {
35 class matrix3;
36 
38 class mesh
39 {
40  public:
41 
42 
43  // ********************************************* //
44  // ********************************************* //
45  // Access data
46  // ********************************************* //
47  // ********************************************* //
48 
50  const vec3& vertex(const unsigned int& index) const;
52  vec3& vertex(const unsigned int& index);
53 
55  const vec3& vertex_of_polygon(const unsigned int& index_polygon,const unsigned int& index_vertex) const;
57  vec3& vertex_of_polygon(const unsigned int& index_polygon,const unsigned int& index_vertex);
58 
60  const vec3& normal(const unsigned int& index) const;
62  vec3& normal(const unsigned int& index);
63 
65  const vec3& color(const unsigned int& index) const;
67  vec3& color(const unsigned int& index);
68 
70  const int& connectivity(const unsigned int& index) const;
72  int& connectivity(const unsigned int& index);
73 
75  const vec2& texture(const unsigned int& index) const;
77  vec2& texture(const unsigned int& index);
78 
80  const std::vector<vec3>& get_vertices() const;
82  const std::vector<vec3>& get_normal() const;
84  const std::vector<vec3>& get_color() const;
86  const std::vector<int>& get_connectivity() const;
88  const std::vector<vec2>& get_texture() const;
89 
91  std::vector<vec3>& get_vertices();
93  std::vector<vec3>& get_normal();
95  std::vector<vec3>& get_color();
97  std::vector<int>& get_connectivity();
99  std::vector<vec2>& get_texture();
100 
101 
103  void add_vertex(const vec3& vertex_to_add);
105  void add_texture(const vec2& texture_to_add);
107  void add_triangle(const int u0,const int u1,const int u2);
108 
109 
110  // ********************************************* //
111  // ********************************************* //
112  // Size
113  // ********************************************* //
114  // ********************************************* //
115 
117  unsigned int number_of_triangle() const;
118 
120  unsigned int number_of_vertices() const;
121 
122  // ********************************************* //
123  // ********************************************* //
124  // Color
125  // ********************************************* //
126  // ********************************************* //
127 
129  void fill_color(const vec3& c);
131  void fill_color_xyz();
133  void fill_color_normal();
134 
135 
136  // ********************************************* //
137  // ********************************************* //
138  // Normal
139  // ********************************************* //
140  // ********************************************* //
141 
143  void compute_normal();
144 
146  std::vector<vec3> get_normal_per_vertex() const;
148  std::vector<vec3> get_normal_per_polygon() const;
149 
150  // ********************************************* //
151  // ********************************************* //
152  // Math Operators
153  // ********************************************* //
154  // ********************************************* //
155 
157  friend mesh operator+(const vec3& x,const mesh& m);
159  friend mesh operator+(const mesh& m,const vec3& x);
161  mesh& operator+=(const vec3& x);
162 
164  friend mesh operator-(const mesh& m,const vec3& x);
166  mesh& operator-=(const vec3& x);
167 
169  friend mesh operator*(const double& s,const mesh& m);
171  friend mesh operator*(const mesh& m,const double& s);
173  mesh& operator*=(const double& s);
174 
175 
177  void scale(const double& sx,const double& sy,const double& sz);
183  void auto_scale(const double& scale=1.0);
184 
186  void apply(const cpe::matrix3 R);
187 
188 
189  // ********************************************* //
190  // ********************************************* //
191  // I/O
192  // ********************************************* //
193  // ********************************************* //
194 
196  void load_file(const std::string &filename);
197 
198 
199 
200 
201  private:
202 
203  // ********************************************* //
204  // ********************************************* //
205  // Internal data
206  // ********************************************* //
207  // ********************************************* //
208 
210  std::vector<vec3> v_vertices;
212  std::vector<vec3> v_normal;
214  std::vector<vec3> v_color;
216  std::vector<vec2> v_texture;
217 
219  std::vector<int> v_connectivity;
220 
221 };
222 
223 
224 
225 
226 }
227 
228 #endif
void scale(const double &sx, const double &sy, const double &sz)
scale the mesh with anisotropical values
Definition: mesh.cpp:126
std::vector< vec2 > v_texture
internal texture coordinate storage
Definition: mesh.hpp:216
const std::vector< vec3 > & get_vertices() const
return internal vertices vector
Definition: mesh.cpp:36
unsigned int number_of_vertices() const
get the number of vertices
Definition: mesh.cpp:296
void add_vertex(const vec3 &vertex_to_add)
Add a vertex to the mesh.
Definition: mesh.cpp:307
void add_triangle(const int u0, const int u1, const int u2)
Add a triangle to the mesh.
Definition: mesh.cpp:309
const vec3 & vertex(const unsigned int &index) const
Accessor to the vertex value.
Definition: mesh.cpp:167
std::vector< int > v_connectivity
internal connectivity storage
Definition: mesh.hpp:219
mesh & operator+=(const vec3 &x)
internal translation
Definition: mesh.cpp:67
const std::vector< int > & get_connectivity() const
return internal connectivity vector
Definition: mesh.cpp:39
const int & connectivity(const unsigned int &index) const
Accessor to the connectivity value.
Definition: mesh.cpp:267
std::vector< vec3 > v_color
internal color storage
Definition: mesh.hpp:214
void fill_color_xyz()
fill mesh with color depending of (x,y,z)-coordinates
Definition: mesh.cpp:131
const vec2 & texture(const unsigned int &index) const
Accessor to the uv-texture value.
Definition: mesh.cpp:242
const std::vector< vec3 > & get_color() const
return internal color vector
Definition: mesh.cpp:38
std::vector< vec3 > get_normal_per_vertex() const
get the vector per vertex
Definition: mesh.cpp:325
Matrix 3x3.
Definition: matrix3.hpp:36
const vec3 & vertex_of_polygon(const unsigned int &index_polygon, const unsigned int &index_vertex) const
Accessor to the vertex value.
Definition: mesh.cpp:383
friend mesh operator-(const mesh &m, const vec3 &x)
translation
Definition: mesh.cpp:74
unsigned int number_of_triangle() const
get the number of triangle
Definition: mesh.cpp:292
void apply(const cpe::matrix3 R)
Apply matrix to every vertices.
Definition: mesh.cpp:301
Container class for a generic mesh with normal, color, texture, ...
Definition: mesh.hpp:38
void add_texture(const vec2 &texture_to_add)
Add a texture to the mesh.
Definition: mesh.cpp:308
std::vector< vec3 > v_normal
internal normal storage
Definition: mesh.hpp:212
void load_file(const std::string &filename)
load a off file
Definition: mesh.cpp:46
const std::vector< vec3 > & get_normal() const
return internal normal vector
Definition: mesh.cpp:37
Vectors/Points 2D.
Definition: vec2.hpp:34
std::vector< vec3 > v_vertices
internal vertices storage
Definition: mesh.hpp:210
mesh & operator*=(const double &s)
uniform scaling
Definition: mesh.cpp:84
const vec3 & color(const unsigned int &index) const
Accessor to the color value.
Definition: mesh.cpp:217
void fill_color_normal()
fill mesh with color depending of normal orientation
Definition: mesh.cpp:157
const vec3 & normal(const unsigned int &index) const
Accessor to the normal value.
Definition: mesh.cpp:192
std::vector< vec3 > get_normal_per_polygon() const
get the vector per polygon
Definition: mesh.cpp:358
Vectors/Points 3D.
Definition: vec3.hpp:36
friend mesh operator+(const vec3 &x, const mesh &m)
translation
Definition: mesh.cpp:65
const std::vector< vec2 > & get_texture() const
return internal uv texture vector
Definition: mesh.cpp:316
void compute_normal()
Compute a per-vertex normal and fill the internal vector.
Definition: mesh.cpp:60
friend mesh operator*(const double &s, const mesh &m)
uniform scaling
Definition: mesh.cpp:82
void auto_scale(const double &scale=1.0)
Set vertices to fit in scalex[-1,1]x[-1,1].
Definition: mesh.cpp:91
mesh & operator-=(const vec3 &x)
internal translation
Definition: mesh.cpp:75
void fill_color(const vec3 &c)
fill mesh with constant color
Definition: mesh.cpp:120