00001 #ifndef OBJ_OBJ_PARSER_HPP_INCLUDED
00002 #define OBJ_OBJ_PARSER_HPP_INCLUDED
00003
00004 #include <fstream>
00005 #include <istream>
00006 #include <string>
00007
00008 #include <tr1/functional>
00009
00010 #include <obj/obj.hpp>
00011
00012 namespace obj {
00013
00014 class obj_parser
00015 {
00016 public:
00017 typedef std::tr1::function<void (std::size_t, const std::string&)> info_callback_type;
00018 typedef std::tr1::function<void (std::size_t, const std::string&)> warning_callback_type;
00019 typedef std::tr1::function<void (std::size_t, const std::string&)> error_callback_type;
00020 typedef std::tr1::function<void (float_type, float_type, float_type)> geometric_vertex_callback_type;
00021 typedef std::tr1::function<void (float_type, float_type)> texture_vertex_callback_type;
00022 typedef std::tr1::function<void (float_type, float_type, float_type)> vertex_normal_callback_type;
00023 typedef std::tr1::function<void (index_type, index_type, index_type)> triangular_face_geometric_vertices_callback_type;
00024 typedef std::tr1::function<void (const index_2_tuple_type&, const index_2_tuple_type&, const index_2_tuple_type&)> triangular_face_geometric_vertices_texture_vertices_callback_type;
00025 typedef std::tr1::function<void (const index_2_tuple_type&, const index_2_tuple_type&, const index_2_tuple_type&)> triangular_face_geometric_vertices_vertex_normals_callback_type;
00026 typedef std::tr1::function<void (const index_3_tuple_type&, const index_3_tuple_type&, const index_3_tuple_type&)> triangular_face_geometric_vertices_texture_vertices_vertex_normals_callback_type;
00027 typedef std::tr1::function<void (index_type, index_type, index_type, index_type)> quadrilateral_face_geometric_vertices_callback_type;
00028 typedef std::tr1::function<void (const index_2_tuple_type&, const index_2_tuple_type&, const index_2_tuple_type&, const index_2_tuple_type&)> quadrilateral_face_geometric_vertices_texture_vertices_callback_type;
00029 typedef std::tr1::function<void (const index_2_tuple_type&, const index_2_tuple_type&, const index_2_tuple_type&, const index_2_tuple_type&)> quadrilateral_face_geometric_vertices_vertex_normals_callback_type;
00030 typedef std::tr1::function<void (const index_3_tuple_type&, const index_3_tuple_type&, const index_3_tuple_type&, const index_3_tuple_type&)> quadrilateral_face_geometric_vertices_texture_vertices_vertex_normals_callback_type;
00031 typedef std::tr1::function<void (index_type, index_type, index_type)> polygonal_face_geometric_vertices_begin_callback_type;
00032 typedef std::tr1::function<void (index_type)> polygonal_face_geometric_vertices_vertex_callback_type;
00033 typedef std::tr1::function<void ()> polygonal_face_geometric_vertices_end_callback_type;
00034 typedef std::tr1::function<void (const index_2_tuple_type&, const index_2_tuple_type&, const index_2_tuple_type&)> polygonal_face_geometric_vertices_texture_vertices_begin_callback_type;
00035 typedef std::tr1::function<void (const index_2_tuple_type&)> polygonal_face_geometric_vertices_texture_vertices_vertex_callback_type;
00036 typedef std::tr1::function<void ()> polygonal_face_geometric_vertices_texture_vertices_end_callback_type;
00037 typedef std::tr1::function<void (const index_2_tuple_type&, const index_2_tuple_type&, const index_2_tuple_type&)> polygonal_face_geometric_vertices_vertex_normals_begin_callback_type;
00038 typedef std::tr1::function<void (const index_2_tuple_type&)> polygonal_face_geometric_vertices_vertex_normals_vertex_callback_type;
00039 typedef std::tr1::function<void ()> polygonal_face_geometric_vertices_vertex_normals_end_callback_type;
00040 typedef std::tr1::function<void (const index_3_tuple_type&, const index_3_tuple_type&, const index_3_tuple_type&)> polygonal_face_geometric_vertices_texture_vertices_vertex_normals_begin_callback_type;
00041 typedef std::tr1::function<void (const index_3_tuple_type&)> polygonal_face_geometric_vertices_texture_vertices_vertex_normals_vertex_callback_type;
00042 typedef std::tr1::function<void ()> polygonal_face_geometric_vertices_texture_vertices_vertex_normals_end_callback_type;
00043 typedef std::tr1::function<void (const std::string&)> group_name_callback_type;
00044 typedef std::tr1::function<void (size_type)> smoothing_group_callback_type;
00045 typedef std::tr1::function<void (const std::string&)> object_name_callback_type;
00046 typedef std::tr1::function<void (const std::string&)> material_library_callback_type;
00047 typedef std::tr1::function<void (const std::string&)> material_name_callback_type;
00048 typedef std::tr1::function<void (const std::string&)> comment_callback_type;
00049 typedef int flags_type;
00050 enum {
00051 parse_blank_lines_as_comment = 1 << 0,
00052 triangulate_faces = 1 << 1,
00053 translate_negative_indices = 1 << 2
00054 };
00055 obj_parser(flags_type flags = 0);
00056 void info_callback(const info_callback_type& info_callback);
00057 void warning_callback(const warning_callback_type& warning_callback);
00058 void error_callback(const error_callback_type& error_callback);
00059 void geometric_vertex_callback(const geometric_vertex_callback_type& geometric_vertex_callback);
00060 void texture_vertex_callback(const texture_vertex_callback_type& texture_vertex_callback);
00061 void vertex_normal_callback(const vertex_normal_callback_type& vertex_normal_callback);
00062 void face_callbacks(const triangular_face_geometric_vertices_callback_type& triangular_face_geometric_vertices_callback, const triangular_face_geometric_vertices_texture_vertices_callback_type& triangular_face_geometric_vertices_texture_vertices_callback, const triangular_face_geometric_vertices_vertex_normals_callback_type& triangular_face_geometric_vertices_vertex_normals_callback, const triangular_face_geometric_vertices_texture_vertices_vertex_normals_callback_type& triangular_face_geometric_vertices_texture_vertices_vertex_normals_callback, const quadrilateral_face_geometric_vertices_callback_type& quadrilateral_face_geometric_vertices_callback, const quadrilateral_face_geometric_vertices_texture_vertices_callback_type& quadrilateral_face_geometric_vertices_texture_vertices_callback, const quadrilateral_face_geometric_vertices_vertex_normals_callback_type& quadrilateral_face_geometric_vertices_vertex_normals_callback, const quadrilateral_face_geometric_vertices_texture_vertices_vertex_normals_callback_type& quadrilateral_face_geometric_vertices_texture_vertices_vertex_normals_callback, const polygonal_face_geometric_vertices_begin_callback_type& polygonal_face_geometric_vertices_begin_callback, const polygonal_face_geometric_vertices_vertex_callback_type& polygonal_face_geometric_vertices_vertex_callback, const polygonal_face_geometric_vertices_end_callback_type& polygonal_face_geometric_vertices_end_callback, const polygonal_face_geometric_vertices_texture_vertices_begin_callback_type& polygonal_face_geometric_vertices_texture_vertices_begin_callback, const polygonal_face_geometric_vertices_texture_vertices_vertex_callback_type& polygonal_face_geometric_vertices_texture_vertices_vertex_callback, const polygonal_face_geometric_vertices_texture_vertices_end_callback_type& polygonal_face_geometric_vertices_texture_vertices_end_callback, const polygonal_face_geometric_vertices_vertex_normals_begin_callback_type& polygonal_face_geometric_vertices_vertex_normals_begin_callback, const polygonal_face_geometric_vertices_vertex_normals_vertex_callback_type& polygonal_face_geometric_vertices_vertex_normals_vertex_callback, const polygonal_face_geometric_vertices_vertex_normals_end_callback_type& polygonal_face_geometric_vertices_vertex_normals_end_callback, const polygonal_face_geometric_vertices_texture_vertices_vertex_normals_begin_callback_type& polygonal_face_geometric_vertices_texture_vertices_vertex_normals_begin_callback, const polygonal_face_geometric_vertices_texture_vertices_vertex_normals_vertex_callback_type& polygonal_face_geometric_vertices_texture_vertices_vertex_normals_vertex_callback, const polygonal_face_geometric_vertices_texture_vertices_vertex_normals_end_callback_type& polygonal_face_geometric_vertices_texture_vertices_vertex_normals_end_callback);
00063 void group_name_callback(const group_name_callback_type& group_name_callback);
00064 void smoothing_group_callback(const smoothing_group_callback_type& smoothing_group_callback);
00065 void object_name_callback(const object_name_callback_type& object_name_callback);
00066 void material_library_callback(const material_library_callback_type& material_library_callback);
00067 void material_name_callback(const material_name_callback_type& material_name_callback);
00068 void comment_callback(const comment_callback_type& comment_callback);
00069 bool parse(std::istream& istream);
00070 bool parse(const std::string& filename);
00071 private:
00072 flags_type flags_;
00073 info_callback_type info_callback_;
00074 warning_callback_type warning_callback_;
00075 error_callback_type error_callback_;
00076 geometric_vertex_callback_type geometric_vertex_callback_;
00077 texture_vertex_callback_type texture_vertex_callback_;
00078 vertex_normal_callback_type vertex_normal_callback_;
00079 triangular_face_geometric_vertices_callback_type triangular_face_geometric_vertices_callback_;
00080 triangular_face_geometric_vertices_texture_vertices_callback_type triangular_face_geometric_vertices_texture_vertices_callback_;
00081 triangular_face_geometric_vertices_vertex_normals_callback_type triangular_face_geometric_vertices_vertex_normals_callback_;
00082 triangular_face_geometric_vertices_texture_vertices_vertex_normals_callback_type triangular_face_geometric_vertices_texture_vertices_vertex_normals_callback_;
00083 quadrilateral_face_geometric_vertices_callback_type quadrilateral_face_geometric_vertices_callback_;
00084 quadrilateral_face_geometric_vertices_texture_vertices_callback_type quadrilateral_face_geometric_vertices_texture_vertices_callback_;
00085 quadrilateral_face_geometric_vertices_vertex_normals_callback_type quadrilateral_face_geometric_vertices_vertex_normals_callback_;
00086 quadrilateral_face_geometric_vertices_texture_vertices_vertex_normals_callback_type quadrilateral_face_geometric_vertices_texture_vertices_vertex_normals_callback_;
00087 polygonal_face_geometric_vertices_begin_callback_type polygonal_face_geometric_vertices_begin_callback_;
00088 polygonal_face_geometric_vertices_vertex_callback_type polygonal_face_geometric_vertices_vertex_callback_;
00089 polygonal_face_geometric_vertices_end_callback_type polygonal_face_geometric_vertices_end_callback_;
00090 polygonal_face_geometric_vertices_texture_vertices_begin_callback_type polygonal_face_geometric_vertices_texture_vertices_begin_callback_;
00091 polygonal_face_geometric_vertices_texture_vertices_vertex_callback_type polygonal_face_geometric_vertices_texture_vertices_vertex_callback_;
00092 polygonal_face_geometric_vertices_texture_vertices_end_callback_type polygonal_face_geometric_vertices_texture_vertices_end_callback_;
00093 polygonal_face_geometric_vertices_vertex_normals_begin_callback_type polygonal_face_geometric_vertices_vertex_normals_begin_callback_;
00094 polygonal_face_geometric_vertices_vertex_normals_vertex_callback_type polygonal_face_geometric_vertices_vertex_normals_vertex_callback_;
00095 polygonal_face_geometric_vertices_vertex_normals_end_callback_type polygonal_face_geometric_vertices_vertex_normals_end_callback_;
00096 polygonal_face_geometric_vertices_texture_vertices_vertex_normals_begin_callback_type polygonal_face_geometric_vertices_texture_vertices_vertex_normals_begin_callback_;
00097 polygonal_face_geometric_vertices_texture_vertices_vertex_normals_vertex_callback_type polygonal_face_geometric_vertices_texture_vertices_vertex_normals_vertex_callback_;
00098 polygonal_face_geometric_vertices_texture_vertices_vertex_normals_end_callback_type polygonal_face_geometric_vertices_texture_vertices_vertex_normals_end_callback_;
00099 group_name_callback_type group_name_callback_;
00100 smoothing_group_callback_type smoothing_group_callback_;
00101 object_name_callback_type object_name_callback_;
00102 material_library_callback_type material_library_callback_;
00103 material_name_callback_type material_name_callback_;
00104 comment_callback_type comment_callback_;
00105 };
00106
00107 }
00108
00109 inline obj::obj_parser::obj_parser(flags_type flags)
00110 : flags_(flags)
00111 {
00112 }
00113
00114 inline bool obj::obj_parser::parse(const std::string& filename)
00115 {
00116 std::ifstream ifstream(filename.c_str());
00117 return parse(ifstream);
00118 }
00119
00120 inline void obj::obj_parser::info_callback(const info_callback_type& info_callback)
00121 {
00122 info_callback_ = info_callback;
00123 }
00124
00125 inline void obj::obj_parser::warning_callback(const warning_callback_type& warning_callback)
00126 {
00127 warning_callback_ = warning_callback;
00128 }
00129
00130 inline void obj::obj_parser::error_callback(const error_callback_type& error_callback)
00131 {
00132 error_callback_ = error_callback;
00133 }
00134
00135 inline void obj::obj_parser::geometric_vertex_callback(const geometric_vertex_callback_type& geometric_vertex_callback)
00136 {
00137 geometric_vertex_callback_ = geometric_vertex_callback;
00138 }
00139
00140 inline void obj::obj_parser::texture_vertex_callback(const texture_vertex_callback_type& texture_vertex_callback)
00141 {
00142 texture_vertex_callback_ = texture_vertex_callback;
00143 }
00144
00145 inline void obj::obj_parser::vertex_normal_callback(const vertex_normal_callback_type& vertex_normal_callback)
00146 {
00147 vertex_normal_callback_ = vertex_normal_callback;
00148 }
00149
00150 inline void obj::obj_parser::face_callbacks(const triangular_face_geometric_vertices_callback_type& triangular_face_geometric_vertices_callback, const triangular_face_geometric_vertices_texture_vertices_callback_type& triangular_face_geometric_vertices_texture_vertices_callback, const triangular_face_geometric_vertices_vertex_normals_callback_type& triangular_face_geometric_vertices_vertex_normals_callback, const triangular_face_geometric_vertices_texture_vertices_vertex_normals_callback_type& triangular_face_geometric_vertices_texture_vertices_vertex_normals_callback, const quadrilateral_face_geometric_vertices_callback_type& quadrilateral_face_geometric_vertices_callback, const quadrilateral_face_geometric_vertices_texture_vertices_callback_type& quadrilateral_face_geometric_vertices_texture_vertices_callback, const quadrilateral_face_geometric_vertices_vertex_normals_callback_type& quadrilateral_face_geometric_vertices_vertex_normals_callback, const quadrilateral_face_geometric_vertices_texture_vertices_vertex_normals_callback_type& quadrilateral_face_geometric_vertices_texture_vertices_vertex_normals_callback, const polygonal_face_geometric_vertices_begin_callback_type& polygonal_face_geometric_vertices_begin_callback, const polygonal_face_geometric_vertices_vertex_callback_type& polygonal_face_geometric_vertices_vertex_callback, const polygonal_face_geometric_vertices_end_callback_type& polygonal_face_geometric_vertices_end_callback, const polygonal_face_geometric_vertices_texture_vertices_begin_callback_type& polygonal_face_geometric_vertices_texture_vertices_begin_callback, const polygonal_face_geometric_vertices_texture_vertices_vertex_callback_type& polygonal_face_geometric_vertices_texture_vertices_vertex_callback, const polygonal_face_geometric_vertices_texture_vertices_end_callback_type& polygonal_face_geometric_vertices_texture_vertices_end_callback, const polygonal_face_geometric_vertices_vertex_normals_begin_callback_type& polygonal_face_geometric_vertices_vertex_normals_begin_callback, const polygonal_face_geometric_vertices_vertex_normals_vertex_callback_type& polygonal_face_geometric_vertices_vertex_normals_vertex_callback, const polygonal_face_geometric_vertices_vertex_normals_end_callback_type& polygonal_face_geometric_vertices_vertex_normals_end_callback, const polygonal_face_geometric_vertices_texture_vertices_vertex_normals_begin_callback_type& polygonal_face_geometric_vertices_texture_vertices_vertex_normals_begin_callback, const polygonal_face_geometric_vertices_texture_vertices_vertex_normals_vertex_callback_type& polygonal_face_geometric_vertices_texture_vertices_vertex_normals_vertex_callback, const polygonal_face_geometric_vertices_texture_vertices_vertex_normals_end_callback_type& polygonal_face_geometric_vertices_texture_vertices_vertex_normals_end_callback)
00151 {
00152 triangular_face_geometric_vertices_callback_ = triangular_face_geometric_vertices_callback;
00153 triangular_face_geometric_vertices_texture_vertices_callback_ = triangular_face_geometric_vertices_texture_vertices_callback;
00154 triangular_face_geometric_vertices_vertex_normals_callback_ = triangular_face_geometric_vertices_vertex_normals_callback;
00155 triangular_face_geometric_vertices_texture_vertices_vertex_normals_callback_ = triangular_face_geometric_vertices_texture_vertices_vertex_normals_callback;
00156 quadrilateral_face_geometric_vertices_callback_ = quadrilateral_face_geometric_vertices_callback;
00157 quadrilateral_face_geometric_vertices_texture_vertices_callback_ = quadrilateral_face_geometric_vertices_texture_vertices_callback;
00158 quadrilateral_face_geometric_vertices_vertex_normals_callback_ = quadrilateral_face_geometric_vertices_vertex_normals_callback;
00159 quadrilateral_face_geometric_vertices_texture_vertices_vertex_normals_callback_ = quadrilateral_face_geometric_vertices_texture_vertices_vertex_normals_callback;
00160 polygonal_face_geometric_vertices_begin_callback_ = polygonal_face_geometric_vertices_begin_callback;
00161 polygonal_face_geometric_vertices_vertex_callback_ = polygonal_face_geometric_vertices_vertex_callback;
00162 polygonal_face_geometric_vertices_end_callback_ = polygonal_face_geometric_vertices_end_callback;
00163 polygonal_face_geometric_vertices_texture_vertices_begin_callback_ = polygonal_face_geometric_vertices_texture_vertices_begin_callback;
00164 polygonal_face_geometric_vertices_texture_vertices_vertex_callback_ = polygonal_face_geometric_vertices_texture_vertices_vertex_callback;
00165 polygonal_face_geometric_vertices_texture_vertices_end_callback_ = polygonal_face_geometric_vertices_texture_vertices_end_callback;
00166 polygonal_face_geometric_vertices_vertex_normals_begin_callback_ = polygonal_face_geometric_vertices_vertex_normals_begin_callback;
00167 polygonal_face_geometric_vertices_vertex_normals_vertex_callback_ = polygonal_face_geometric_vertices_vertex_normals_vertex_callback;
00168 polygonal_face_geometric_vertices_vertex_normals_end_callback_ = polygonal_face_geometric_vertices_vertex_normals_end_callback;
00169 polygonal_face_geometric_vertices_texture_vertices_vertex_normals_begin_callback_ = polygonal_face_geometric_vertices_texture_vertices_vertex_normals_begin_callback;
00170 polygonal_face_geometric_vertices_texture_vertices_vertex_normals_vertex_callback_ = polygonal_face_geometric_vertices_texture_vertices_vertex_normals_vertex_callback;
00171 polygonal_face_geometric_vertices_texture_vertices_vertex_normals_end_callback_ = polygonal_face_geometric_vertices_texture_vertices_vertex_normals_end_callback;
00172 }
00173
00174 inline void obj::obj_parser::group_name_callback(const group_name_callback_type& group_name_callback)
00175 {
00176 group_name_callback_ = group_name_callback;
00177 }
00178
00179 inline void obj::obj_parser::smoothing_group_callback(const smoothing_group_callback_type& smoothing_group_callback)
00180 {
00181 smoothing_group_callback_ = smoothing_group_callback;
00182 }
00183
00184 inline void obj::obj_parser::object_name_callback(const object_name_callback_type& object_name_callback)
00185 {
00186 object_name_callback_ = object_name_callback;
00187 }
00188
00189 inline void obj::obj_parser::material_library_callback(const material_library_callback_type& material_library_callback)
00190 {
00191 material_library_callback_ = material_library_callback;
00192 }
00193
00194 inline void obj::obj_parser::material_name_callback(const material_name_callback_type& material_name_callback)
00195 {
00196 material_name_callback_ = material_name_callback;
00197 }
00198
00199 inline void obj::obj_parser::comment_callback(const comment_callback_type& comment_callback)
00200 {
00201 comment_callback_ = comment_callback;
00202 }
00203
00204 #endif // OBJ_OBJ_PARSER_HPP_INCLUDED