cgv
simple_mesh.h
1 #pragma once
2 
3 #include <vector>
4 #include <cgv/math/fvec.h>
5 #include <cgv/math/fmat.h>
6 #include <cgv/utils/file.h>
7 #include <cgv/media/illum/textured_surface_material.h>
8 #include <cgv/media/axis_aligned_box.h>
9 #include <cgv/media/colored_model.h>
10 
11 #include "../lib_begin.h"
12 
13 namespace cgv {
14  namespace media {
15  namespace mesh {
16 
17 template <typename T>
18 class simple_mesh_obj_reader;
19 
21 class CGV_API simple_mesh_base : public colored_model
22 {
23 public:
32 protected:
33  std::vector<idx_type> position_indices;
34  std::vector<idx_type> normal_indices;
35  std::vector<idx_type> tex_coord_indices;
36  std::vector<idx_type> faces;
37  std::vector<idx_type> group_indices;
38  std::vector<std::string> group_names;
39  std::vector<idx_type> material_indices;
40  std::vector<mat_type> materials;
41 public:
47  simple_mesh_base& operator=(const simple_mesh_base& smb);
49  virtual idx_type get_nr_positions() const = 0;
51  idx_type start_face();
53  idx_type new_corner(idx_type position_index, idx_type normal_index = -1, idx_type tex_coord_index = -1);
55  idx_type c2p(idx_type ci) const { return position_indices[ci]; }
57  idx_type c2n(idx_type ci) const { return normal_indices[ci]; }
59  idx_type c2t(idx_type ci) const { return tex_coord_indices[ci]; }
61  idx_type get_nr_faces() const { return idx_type(faces.size()); }
63  idx_type get_nr_corners() const { return idx_type(position_indices.size()); }
65  idx_type begin_corner(idx_type fi) const { return faces[fi]; }
67  idx_type end_corner(idx_type fi) const { return fi + 1 == faces.size() ? idx_type(position_indices.size()) : faces[fi + 1]; }
69  idx_type face_degree(idx_type fi) const { return end_corner(fi) - begin_corner(fi); }
71  size_t get_nr_materials() const { return materials.size(); }
73  idx_type new_material() { materials.push_back(mat_type()); return idx_type(materials.size() - 1); }
75  const mat_type& get_material(size_t i) const { return materials[i]; }
77  mat_type& ref_material(size_t i) { return materials[i]; }
79  const idx_type& material_index(idx_type fi) const { return material_indices[fi]; }
81  idx_type& material_index(idx_type fi) { return material_indices[fi]; }
83  size_t get_nr_groups() const { return group_names.size(); }
85  const std::string& group_name(size_t i) const { return group_names[i]; }
87  std::string& group_name(size_t i) { return group_names[i]; }
89  idx_type new_group(const std::string& name) { group_names.push_back(name); return idx_type(group_names.size() - 1); }
91  const idx_type& group_index(idx_type fi) const { return group_indices[fi]; }
93  idx_type& group_index(idx_type fi) { return group_indices[fi]; }
95  void revert_face_orientation();
97  void sort_faces(std::vector<idx_type>& perm, bool by_group = true, bool by_material = true) const;
99  void merge_indices(std::vector<idx_type>& vertex_indices, std::vector<vec3i>& unique_triples, bool* include_tex_coords_ptr = 0, bool* include_normals_ptr = 0) const;
101  void extract_triangle_element_buffer(const std::vector<idx_type>& vertex_indices, std::vector<idx_type>& triangle_element_buffer,
102  const std::vector<idx_type>* face_perm_ptr = 0, std::vector<vec3i>* material_group_start_ptr = 0) const;
104  void extract_wireframe_element_buffer(const std::vector<idx_type>& vertex_indices, std::vector<idx_type>& edge_element_buffer) const;
106  void compute_inv(std::vector<uint32_t>& inv, std::vector<uint32_t>* p2c_ptr = 0, std::vector<uint32_t>* next_ptr = 0, std::vector<uint32_t>* prev_ptr = 0);
108  uint32_t compute_c2e(const std::vector<uint32_t>& inv, std::vector<uint32_t>& c2e, std::vector<uint32_t>* e2c_ptr = 0);
110  void compute_c2f(std::vector<uint32_t>& c2f);
111 };
112 
114 template <typename T = float>
115 class CGV_API simple_mesh : public simple_mesh_base
116 {
117 public:
123  typedef typename cgv::math::fvec<T, 3> vec3;
125  typedef typename cgv::math::fvec<T, 2> vec2;
127  typedef typename cgv::math::fmat<T, 3, 3> mat3;
134 protected:
135  friend class simple_mesh_obj_reader<T>;
136  std::vector<vec3> positions;
137  std::vector<vec3> normals;
138  std::vector<vec2> tex_coords;
139 
140  vec3 compute_normal(const vec3& p0, const vec3& p1, const vec3& p2);
141 public:
143  simple_mesh(const simple_mesh<T>& sm);
145  simple_mesh(const std::string& conway_notation = "");
147  simple_mesh<T>& operator = (const simple_mesh<T>& sm);
149  void clear();
150 
152  idx_type new_position(const vec3& p) { positions.push_back(p); return idx_type(positions.size()-1); }
154  idx_type get_nr_positions() const { return idx_type(positions.size()); }
155  vec3& position(idx_type pi) { return positions[pi]; }
156  const vec3& position(idx_type pi) const { return positions[pi]; }
157  const std::vector<vec3>& get_positions() const { return positions; }
158 
160  idx_type new_normal(const vec3& n) { normals.push_back(n); return idx_type(normals.size()-1); }
162  bool has_normals() const { return get_nr_normals() > 0; }
163  idx_type get_nr_normals() const { return idx_type(normals.size()); }
164  vec3& normal(idx_type ni) { return normals[ni]; }
165  const vec3& normal(idx_type ni) const { return normals[ni]; }
166 
168  idx_type new_tex_coord(const vec2& tc) { tex_coords.push_back(tc); return idx_type(tex_coords.size() - 1); }
170  bool has_tex_coords() const { return get_nr_tex_coords() > 0; }
171  idx_type get_nr_tex_coords() const { return idx_type(tex_coords.size()); }
172  vec2& tex_coord(idx_type ti) { return tex_coords[ti]; }
173  const vec2& tex_coord(idx_type ti) const { return tex_coords[ti]; }
175  void compute_face_normals();
177  void ambo();
179  void truncate(T lambda = 0.33333f);
181  void snub(T lambda = 0.33333f);
183  void dual();
185  void gyro(T lambda = 0.3333f);
187  void join();
189  void ortho();
191  void construct_conway_polyhedron(const std::string& conway_notation);
192 
194  box_type compute_box() const;
196  void compute_vertex_normals();
198  bool read(const std::string& file_name);
200  bool write(const std::string& file_name) const;
202  unsigned extract_vertex_attribute_buffer(
203  const std::vector<idx_type>& vertex_indices,
204  const std::vector<vec3i>& unique_triples,
205  bool include_tex_coords, bool include_normals,
206  std::vector<T>& attrib_buffer, bool *include_colors_ptr = 0) const;
208  void transform(const mat3& linear_transformation, const vec3& translation);
210  void transform(const mat3& linear_transform, const vec3& translation, const mat3& inverse_linear_transform);
211 };
212 
213  }
214  }
215 }
216 
217 #include <cgv/config/lib_end.h>
cgv::media::mesh::simple_mesh_base::get_nr_materials
size_t get_nr_materials() const
return number of materials in mesh
Definition: simple_mesh.h:71
cgv::media::mesh::simple_mesh_base::c2t
idx_type c2t(idx_type ci) const
return texture index of corner
Definition: simple_mesh.h:59
cgv::math::fmat
matrix of fixed size dimensions
Definition: fmat.h:23
cgv::media::mesh::simple_mesh_base::c2p
idx_type c2p(idx_type ci) const
return position index of corner
Definition: simple_mesh.h:55
cgv::math::fvec
Definition: fvec.h:18
cgv::media::mesh::simple_mesh_base::get_nr_groups
size_t get_nr_groups() const
return number of face groups
Definition: simple_mesh.h:83
cgv::media::mesh::simple_mesh_base::end_corner
idx_type end_corner(idx_type fi) const
return index of end corner (one after the last one) of face with index fi
Definition: simple_mesh.h:67
cgv::media::mesh::simple_mesh_base::group_index
idx_type & group_index(idx_type fi)
return reference to group index of given face
Definition: simple_mesh.h:93
cgv::media::mesh::simple_mesh::vec2
cgv::math::fvec< T, 2 > vec2
type of 2d vector
Definition: simple_mesh.h:125
cgv::media::mesh::simple_mesh_base::group_name
const std::string & group_name(size_t i) const
return the name of the i-th face group
Definition: simple_mesh.h:85
cgv::media::mesh::simple_mesh_base
Definition: simple_mesh.h:22
cgv::media::mesh::simple_mesh_base::c2n
idx_type c2n(idx_type ci) const
return normal index of corner
Definition: simple_mesh.h:57
cgv::media::mesh::simple_mesh::mesh_type
simple_mesh< T > mesh_type
type of axis aligned 3d box
Definition: simple_mesh.h:119
cgv::media::mesh::simple_mesh::has_normals
bool has_normals() const
access to normals
Definition: simple_mesh.h:162
cgv::media::mesh::simple_mesh::mat_type
illum::textured_surface_material mat_type
textured surface materials are supported by mat_type
Definition: simple_mesh.h:131
cgv::media::mesh::simple_mesh::vec3
cgv::math::fvec< T, 3 > vec3
type of 3d vector
Definition: simple_mesh.h:123
cgv::media::color< float, RGB >
cgv::media::axis_aligned_box
Definition: axis_aligned_box.h:15
cgv::media::mesh::simple_mesh_base::vec2i
cgv::math::fvec< idx_type, 2 > vec2i
define index pair type
Definition: simple_mesh.h:27
cgv::media::mesh::simple_mesh::clr_type
illum::surface_material::color_type clr_type
color type used in surface materials
Definition: simple_mesh.h:129
cgv::media::mesh::simple_mesh_base::group_name
std::string & group_name(size_t i)
set a new group name
Definition: simple_mesh.h:87
cgv::media::mesh::simple_mesh_base::begin_corner
idx_type begin_corner(idx_type fi) const
return index of first corner of face with index fi
Definition: simple_mesh.h:65
cgv::media::mesh::simple_mesh::new_position
idx_type new_position(const vec3 &p)
add a new position and return position index
Definition: simple_mesh.h:152
cgv::media::mesh::simple_mesh_base::idx_type
cgv::type::uint32_type idx_type
define index type
Definition: simple_mesh.h:25
cgv::media::mesh::simple_mesh_base::get_nr_positions
virtual idx_type get_nr_positions() const =0
position count
cgv::media::mesh::simple_mesh::get_nr_positions
idx_type get_nr_positions() const
access to positions
Definition: simple_mesh.h:154
cgv::media::mesh::simple_mesh_base::material_index
idx_type & material_index(idx_type fi)
return reference to material index of given face
Definition: simple_mesh.h:81
cgv::media::mesh::simple_mesh::mat3
cgv::math::fmat< T, 3, 3 > mat3
linear transformation
Definition: simple_mesh.h:127
cgv::media::mesh::simple_mesh::new_normal
idx_type new_normal(const vec3 &n)
add a new normal and return normal index
Definition: simple_mesh.h:160
cgv::media::mesh::simple_mesh_base::face_degree
idx_type face_degree(idx_type fi) const
return number of edges/corners of face with index fi
Definition: simple_mesh.h:69
cgv::type::uint32_type
unsigned int uint32_type
this type provides an 32 bit unsigned integer type
Definition: standard_types.h:20
cgv::media::mesh::simple_mesh::box_type
cgv::media::axis_aligned_box< T, 3 > box_type
type of axis aligned 3d box
Definition: simple_mesh.h:121
cgv::media::mesh::simple_mesh::has_tex_coords
bool has_tex_coords() const
access to texture coordinates
Definition: simple_mesh.h:170
cgv::media::mesh::simple_mesh_base::group_index
const idx_type & group_index(idx_type fi) const
return group index of given face
Definition: simple_mesh.h:91
cgv::media::mesh::simple_mesh
the simple_mesh class is templated over the coordinate type that defaults to float
Definition: simple_mesh.h:116
cgv::media::mesh::simple_mesh_base::mat_type
illum::textured_surface_material mat_type
define material type
Definition: simple_mesh.h:31
cgv::media::mesh::simple_mesh_base::get_nr_faces
idx_type get_nr_faces() const
return the number of faces
Definition: simple_mesh.h:61
cgv::media::mesh::simple_mesh_base::get_nr_corners
idx_type get_nr_corners() const
return the number of corners
Definition: simple_mesh.h:63
cgv::media::illum::textured_surface_material
simple class to hold the material properties of a phong material
Definition: textured_surface_material.h:15
cgv::media::mesh::simple_mesh_base::vec3i
cgv::math::fvec< idx_type, 3 > vec3i
define index triple type
Definition: simple_mesh.h:29
cgv::media::mesh::simple_mesh_base::new_group
idx_type new_group(const std::string &name)
add a new group and return its index
Definition: simple_mesh.h:89
cgv
the cgv namespace
Definition: vr_calib.cxx:9
cgv::media::mesh::simple_mesh_base::new_material
idx_type new_material()
add a new material and return its index
Definition: simple_mesh.h:73
cgv::media::colored_model
Definition: colored_model.h:12
cgv::media::mesh::simple_mesh::new_tex_coord
idx_type new_tex_coord(const vec2 &tc)
add a new normal and return normal index
Definition: simple_mesh.h:168
cgv::media::mesh::simple_mesh_base::ref_material
mat_type & ref_material(size_t i)
return reference to i-th material
Definition: simple_mesh.h:77
cgv::media::mesh::simple_mesh::idx_type
cgv::type::uint32_type idx_type
32bit index
Definition: simple_mesh.h:133
cgv::media::mesh::simple_mesh_base::material_index
const idx_type & material_index(idx_type fi) const
return material index of given face
Definition: simple_mesh.h:79
cgv::media::mesh::simple_mesh_base::get_material
const mat_type & get_material(size_t i) const
return const reference to i-th material
Definition: simple_mesh.h:75