OpenMesh
OpenMesh/Core/IO/reader/OBJReader.hh
00001 /*===========================================================================*\
00002  *                                                                           *
00003  *                               OpenMesh                                    *
00004  *      Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen      *
00005  *                           www.openmesh.org                                *
00006  *                                                                           *
00007  *---------------------------------------------------------------------------*
00008  *  This file is part of OpenMesh.                                           *
00009  *                                                                           *
00010  *  OpenMesh is free software: you can redistribute it and/or modify         *
00011  *  it under the terms of the GNU Lesser General Public License as           *
00012  *  published by the Free Software Foundation, either version 3 of           *
00013  *  the License, or (at your option) any later version with the              *
00014  *  following exceptions:                                                    *
00015  *                                                                           *
00016  *  If other files instantiate templates or use macros                       *
00017  *  or inline functions from this file, or you compile this file and         *
00018  *  link it with other files to produce an executable, this file does        *
00019  *  not by itself cause the resulting executable to be covered by the        *
00020  *  GNU Lesser General Public License. This exception does not however       *
00021  *  invalidate any other reasons why the executable file might be            *
00022  *  covered by the GNU Lesser General Public License.                        *
00023  *                                                                           *
00024  *  OpenMesh is distributed in the hope that it will be useful,              *
00025  *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
00026  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
00027  *  GNU Lesser General Public License for more details.                      *
00028  *                                                                           *
00029  *  You should have received a copy of the GNU LesserGeneral Public          *
00030  *  License along with OpenMesh.  If not,                                    *
00031  *  see <http://www.gnu.org/licenses/>.                                      *
00032  *                                                                           *
00033 \*===========================================================================*/
00034 
00035 /*===========================================================================*\
00036  *                                                                           *
00037  *   $Revision: 362 $                                                         *
00038  *   $Date: 2011-01-26 10:21:12 +0100 (Mi, 26 Jan 2011) $                   *
00039  *                                                                           *
00040 \*===========================================================================*/
00041 
00042 
00043 //=============================================================================
00044 //
00045 //  Implements an reader module for OBJ files
00046 //
00047 //=============================================================================
00048 
00049 
00050 #ifndef __OBJREADER_HH__
00051 #define __OBJREADER_HH__
00052 
00053 
00054 //=== INCLUDES ================================================================
00055 
00056 
00057 #include <iostream>
00058 #include <string>
00059 #include <vector>
00060 #include <map>
00061 #include <fstream>
00062 
00063 #include <OpenMesh/Core/System/config.h>
00064 #include <OpenMesh/Core/Utils/SingletonT.hh>
00065 #include <OpenMesh/Core/IO/importer/BaseImporter.hh>
00066 #include <OpenMesh/Core/IO/reader/BaseReader.hh>
00067 
00068 
00069 //== NAMESPACES ===============================================================
00070 
00071 
00072 namespace OpenMesh {
00073 namespace IO {
00074 
00075 
00076 //== IMPLEMENTATION ===========================================================
00077 
00078 
00082 class _OBJReader_ : public BaseReader
00083 {
00084 public:
00085 
00086   _OBJReader_();
00087 
00088   virtual ~_OBJReader_() { }
00089 
00090   std::string get_description() const { return "Alias/Wavefront"; }
00091   std::string get_extensions()  const { return "obj"; }
00092 
00093   bool read(const std::string& _filename,
00094             BaseImporter& _bi,
00095             Options& _opt);
00096 
00097   bool read(std::istream& _in,
00098           BaseImporter& _bi,
00099           Options& _opt);
00100 
00101 private:
00102 
00103 #ifndef DOXY_IGNORE_THIS
00104   class Material
00105   {
00106   public:
00107 
00108     Material() { cleanup(); }
00109 
00110     void cleanup()
00111     {
00112       Kd_is_set_     = false;
00113       Ka_is_set_     = false;
00114       Ks_is_set_     = false;
00115       Tr_is_set_     = false;
00116       map_Kd_is_set_ = false;
00117     }
00118 
00119     bool is_valid(void) const
00120     { return Kd_is_set_ || Ka_is_set_ || Ks_is_set_ || Tr_is_set_; }
00121 
00122     bool has_Kd(void)     { return Kd_is_set_;     }
00123     bool has_Ka(void)     { return Ka_is_set_;     }
00124     bool has_Ks(void)     { return Ks_is_set_;     }
00125     bool has_Tr(void)     { return Tr_is_set_;     }
00126     bool has_map_Kd(void) { return map_Kd_is_set_; }
00127 
00128     void set_Kd( float r, float g, float b )
00129     { Kd_=Vec3f(r,g,b); Kd_is_set_=true; }
00130 
00131     void set_Ka( float r, float g, float b )
00132     { Ka_=Vec3f(r,g,b); Ka_is_set_=true; }
00133 
00134     void set_Ks( float r, float g, float b )
00135     { Ks_=Vec3f(r,g,b); Ks_is_set_=true; }
00136 
00137     void set_Tr( float t )
00138     { Tr_=t;            Tr_is_set_=true; }
00139 
00140     void set_map_Kd( std::string _name, int _index_Kd )
00141     { map_Kd_ = _name, index_Kd_ = _index_Kd; map_Kd_is_set_ = true; };
00142 
00143     const Vec3f& Kd( void ) const { return Kd_; }
00144     const Vec3f& Ka( void ) const { return Ka_; }
00145     const Vec3f& Ks( void ) const { return Ks_; }
00146     float  Tr( void ) const { return Tr_; }
00147     const std::string& map_Kd( void ) { return map_Kd_ ; }
00148     const int& map_Kd_index( void ) { return index_Kd_ ; }
00149 
00150   private:
00151 
00152     Vec3f Kd_;                          bool Kd_is_set_; // diffuse
00153     Vec3f Ka_;                          bool Ka_is_set_; // ambient
00154     Vec3f Ks_;                          bool Ks_is_set_; // specular
00155     float Tr_;                          bool Tr_is_set_; // transperency
00156 
00157     std::string map_Kd_; int index_Kd_; bool map_Kd_is_set_; // Texture
00158 
00159   };
00160 #endif
00161 
00162   typedef std::map<std::string, Material> MaterialList;
00163 
00164   MaterialList materials_;
00165 
00166   bool read_material( std::fstream& _in );
00167 
00168 private:
00169 
00170   std::string path_;
00171 
00172 };
00173 
00174 
00175 //== TYPE DEFINITION ==========================================================
00176 
00177 
00178 extern _OBJReader_  __OBJReaderInstance;
00179 _OBJReader_& OBJReader();
00180 
00181 
00182 //=============================================================================
00183 } // namespace IO
00184 } // namespace OpenMesh
00185 //=============================================================================
00186 #endif
00187 //=============================================================================