mesh.hpp
Go to the documentation of this file.
1 /*
2 ** TP CPE Lyon
3 ** Copyright (C) 2013 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 
20 
21 #ifndef MESH_HPP
22 #define MESH_HPP
23 
24 #include <exception_cpe.hpp>
25 #include <vec3.hpp>
26 #include <vec2.hpp>
27 #include <vector>
28 #include <string>
29 
30 
31 
32 namespace cpe
33 {
34 class matrix3;
35 
37 class mesh
38 {
39  public:
40 
41 
42  // ********************************************* //
43  // ********************************************* //
44  // Access data
45  // ********************************************* //
46  // ********************************************* //
47 
49  const vec3& vertex(const unsigned int& index) const;
51  vec3& vertex(const unsigned int& index);
52 
54  const vec3& vertex_of_polygon(const unsigned int& index_polygon,const unsigned int& index_vertex) const;
56  vec3& vertex_of_polygon(const unsigned int& index_polygon,const unsigned int& index_vertex);
57 
59  const vec3& normal(const unsigned int& index) const;
61  vec3& normal(const unsigned int& index);
62 
64  const vec3& color(const unsigned int& index) const;
66  vec3& color(const unsigned int& index);
67 
69  const int& connectivity(const unsigned int& index) const;
71  int& connectivity(const unsigned int& index);
72 
74  const vec2& texture(const unsigned int& index) const;
76  vec2& texture(const unsigned int& index);
77 
79  const std::vector<vec3>& get_vertices() const;
81  const std::vector<vec3>& get_normal() const;
83  const std::vector<vec3>& get_color() const;
85  const std::vector<int>& get_connectivity() const;
87  const std::vector<vec2>& get_texture() const;
88 
90  std::vector<vec3>& get_vertices();
92  std::vector<vec3>& get_normal();
94  std::vector<vec3>& get_color();
96  std::vector<int>& get_connectivity();
98  std::vector<vec2>& get_texture();
99 
100 
102  void add_vertex(const vec3& vertex_to_add);
104  void add_texture(const vec2& texture_to_add);
106  void add_triangle(const int u0,const int u1,const int u2);
107 
108 
109  // ********************************************* //
110  // ********************************************* //
111  // Size
112  // ********************************************* //
113  // ********************************************* //
114 
116  unsigned int number_of_triangle() const;
117 
119  unsigned int number_of_vertices() const;
120 
121  // ********************************************* //
122  // ********************************************* //
123  // Color
124  // ********************************************* //
125  // ********************************************* //
126 
128  void fill_color(const vec3& c);
130  void fill_color_xyz();
132  void fill_color_normal();
133 
134 
135  // ********************************************* //
136  // ********************************************* //
137  // Normal
138  // ********************************************* //
139  // ********************************************* //
140 
142  void compute_normal();
143 
145  void normal_inversion();
146 
148  std::vector<vec3> get_normal_per_vertex() const;
150  std::vector<vec3> get_normal_per_polygon() const;
151 
152  // ********************************************* //
153  // ********************************************* //
154  // Math Operators
155  // ********************************************* //
156  // ********************************************* //
157 
159  friend mesh operator+(const vec3& x,const mesh& m);
161  friend mesh operator+(const mesh& m,const vec3& x);
163  mesh& operator+=(const vec3& x);
164 
166  friend mesh operator-(const mesh& m,const vec3& x);
168  mesh& operator-=(const vec3& x);
169 
171  friend mesh operator*(const float& s,const mesh& m);
173  friend mesh operator*(const mesh& m,const float& s);
175  mesh& operator*=(const float& s);
176 
177 
179  void scale(const float& sx,const float& sy,const float& sz);
185  void auto_scale(const float& scale=1.0);
186 
188  void apply(const cpe::matrix3 R);
189 
190 
191  // ********************************************* //
192  // ********************************************* //
193  // I/O
194  // ********************************************* //
195  // ********************************************* //
196 
198  void load_file(const std::string &filename);
199 
200 
201 
202 
203  private:
204 
205  // ********************************************* //
206  // ********************************************* //
207  // Internal data
208  // ********************************************* //
209  // ********************************************* //
210 
212  std::vector<vec3> v_vertices;
214  std::vector<vec3> v_normal;
216  std::vector<vec3> v_color;
218  std::vector<vec2> v_texture;
219 
221  std::vector<int> v_connectivity;
222 
223 };
224 
225 
226 
229 {
230  public:
231 
235  exception_mesh(const std::string& msg_param,const std::string& file_param,const std::string& caller_param,const int& line_param):exception_cpe(msg_param,file_param,caller_param,line_param){}
236 };
237 }
238 
239 #endif