cgv
obj_reader.h
1 #pragma once
2 
3 #include <cgv/math/fvec.h>
4 #include <cgv/media/illum/obj_material.hh>
5 #include <cgv/utils/tokenizer.h>
6 #include <set>
7 #include <map>
8 #include <vector>
9 #include <string>
10 
11 #include <cgv/media/lib_begin.h>
12 
13 namespace cgv {
14  namespace media {
15  namespace mesh {
16 
18 class CGV_API obj_reader_base
19 {
20 public:
23 protected:
25  unsigned group_index;
27  unsigned nr_groups;
29  unsigned material_index;
31  unsigned nr_materials;
33  std::map<std::string, unsigned> material_index_lut;
36  color_type parse_color(const std::vector<cgv::utils::token>& t, unsigned off = 0) const;
38  int minus;
39  unsigned nr_normals, nr_texcoords;
40  bool have_default_material;
41  std::set<std::string> mtl_lib_files;
42  void parse_face(const std::vector<cgv::utils::token>& tokens);
43  void parse_material(const std::vector<cgv::utils::token>& tokens);
44  virtual void parse_and_process_vertex(const std::vector<cgv::utils::token>& tokens) = 0;
45  virtual void parse_and_process_normal(const std::vector<cgv::utils::token>& tokens) = 0;
46  virtual void parse_and_process_texcoord(const std::vector<cgv::utils::token>& tokens) = 0;
48 
51  std::string path_name;
54  unsigned get_current_group() const;
56  unsigned get_current_material() const;
58 
60  virtual void process_comment(const std::string& comment);
63  virtual void process_color(const color_type& c);
65  void convert_to_positive(unsigned vcount, int *vertices,
66  int *texcoords, int *normals,
67  unsigned v, unsigned n, unsigned t);
69  virtual void process_face(unsigned vcount, int *vertices,
70  int *texcoords = 0, int *normals = 0);
72  virtual void process_group(const std::string& name, const std::string& parameters);
74  virtual void process_material(const cgv::media::illum::obj_material& mtl, unsigned idx);
76 public:
80  virtual bool read_obj(const std::string& file_name);
82  virtual bool read_mtl(const std::string& file_name);
84  virtual void clear();
85 };
86 
91 template <typename T>
92 class CGV_API obj_reader_generic : public obj_reader_base
93 {
94 public:
96  typedef T crd_type;
102  static bool is_double(const char* begin, const char* end, crd_type& value);
103 protected:
106  v2d_type parse_v2d(const std::vector<cgv::utils::token>& t) const;
109  v3d_type parse_v3d(const std::vector<cgv::utils::token>& t) const;
111  void parse_and_process_vertex(const std::vector<cgv::utils::token>& tokens);
113  void parse_and_process_normal(const std::vector<cgv::utils::token>& tokens);
115  void parse_and_process_texcoord(const std::vector<cgv::utils::token>& tokens);
117 
120  virtual void process_vertex(const v3d_type& p);
123  virtual void process_texcoord(const v2d_type& t);
125  virtual void process_normal(const v3d_type& n);
127 public:
130 };
131 
135 
136  }
137  }
138 }
139 
140 #include <cgv/config/lib_end.h>
cgv::media::mesh::obj_reader_generic
Definition: obj_reader.h:93
cgv::media::mesh::obj_reader_base::nr_groups
unsigned nr_groups
number of groups
Definition: obj_reader.h:27
cgv::media::mesh::obj_reader_generic::v3d_type
cgv::math::fvec< T, 3 > v3d_type
type used to store positions and normal vectors
Definition: obj_reader.h:100
cgv::math::fvec
Definition: fvec.h:18
cgv::media::illum::obj_material
>extension of a phong material with support for texture mapped color channels
Definition: obj_material.hh:12
cgv::media::mesh::obj_reader_base::material_index_lut
std::map< std::string, unsigned > material_index_lut
mapping from material names to material indices
Definition: obj_reader.h:33
cgv::media::color< float, RGB, OPACITY >
cgv::media::mesh::obj_reader_base::group_index
unsigned group_index
keep track of the current group
Definition: obj_reader.h:25
cgv::media::mesh::obj_reader_base
Definition: obj_reader.h:19
cgv::media::mesh::obj_reader_base::color_type
illum::obj_material::color_type color_type
type used for rgba colors
Definition: obj_reader.h:22
cgv::media::mesh::obj_reader_base::nr_materials
unsigned nr_materials
number of materials
Definition: obj_reader.h:31
cgv::media::mesh::obj_reader_base::material_index
unsigned material_index
keep track of current material index
Definition: obj_reader.h:29
cgv::media::mesh::obj_reader_generic::crd_type
T crd_type
type of coordinates
Definition: obj_reader.h:96
cgv::media::mesh::obj_reader_generic::v2d_type
cgv::math::fvec< T, 2 > v2d_type
type used to store texture coordinates
Definition: obj_reader.h:98
cgv
the cgv namespace
Definition: vr_calib.cxx:9