Developer Documentation
OBJReader.hh
1 /* ========================================================================= *
2  * *
3  * OpenMesh *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openmesh.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenMesh. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40  * ========================================================================= */
41 
42 
43 
44 
45 //=============================================================================
46 //
47 // Implements an reader module for OBJ files
48 //
49 //=============================================================================
50 
51 
52 #ifndef __OBJREADER_HH__
53 #define __OBJREADER_HH__
54 
55 
56 //=== INCLUDES ================================================================
57 
58 
59 #include <iosfwd>
60 #include <string>
61 #include <map>
62 
64 #include <OpenMesh/Core/Utils/SingletonT.hh>
65 #include <OpenMesh/Core/IO/importer/BaseImporter.hh>
66 #include <OpenMesh/Core/IO/reader/BaseReader.hh>
67 
68 
69 //== NAMESPACES ===============================================================
70 
71 
72 namespace OpenMesh {
73 namespace IO {
74 
75 
76 //== IMPLEMENTATION ===========================================================
77 
78 
82 class OPENMESHDLLEXPORT _OBJReader_ : public BaseReader
83 {
84 public:
85 
86  _OBJReader_();
87 
88  virtual ~_OBJReader_() { }
89 
90  std::string get_description() const override { return "Alias/Wavefront"; }
91  std::string get_extensions() const override { return "obj"; }
92 
93  bool read(const std::string& _filename,
94  BaseImporter& _bi,
95  Options& _opt) override;
96 
97  bool read(std::istream& _in,
98  BaseImporter& _bi,
99  Options& _opt) override;
100 
101 private:
102 
103 #ifndef DOXY_IGNORE_THIS
104  class Material
105  {
106  public:
107 
108  Material():Tr_(0),index_Kd_(0) { cleanup(); }
109 
110  void cleanup()
111  {
112  Kd_is_set_ = false;
113  Ka_is_set_ = false;
114  Ks_is_set_ = false;
115  Tr_is_set_ = false;
116  map_Kd_is_set_ = false;
117  }
118 
119  bool is_valid(void) const
120  { return Kd_is_set_ || Ka_is_set_ || Ks_is_set_ || Tr_is_set_ || map_Kd_is_set_; }
121 
122  bool has_Kd(void) { return Kd_is_set_; }
123  bool has_Ka(void) { return Ka_is_set_; }
124  bool has_Ks(void) { return Ks_is_set_; }
125  bool has_Tr(void) { return Tr_is_set_; }
126  bool has_map_Kd(void) { return map_Kd_is_set_; }
127 
128  void set_Kd( float r, float g, float b )
129  { Kd_=Vec3f(r,g,b); Kd_is_set_=true; }
130 
131  void set_Ka( float r, float g, float b )
132  { Ka_=Vec3f(r,g,b); Ka_is_set_=true; }
133 
134  void set_Ks( float r, float g, float b )
135  { Ks_=Vec3f(r,g,b); Ks_is_set_=true; }
136 
137  void set_Tr( float t )
138  { Tr_=t; Tr_is_set_=true; }
139 
140  void set_map_Kd( const std::string& _name, int _index_Kd )
141  { map_Kd_ = _name, index_Kd_ = _index_Kd; map_Kd_is_set_ = true; };
142 
143  const Vec3f& Kd( void ) const { return Kd_; }
144  const Vec3f& Ka( void ) const { return Ka_; }
145  const Vec3f& Ks( void ) const { return Ks_; }
146  float Tr( void ) const { return Tr_; }
147  const std::string& map_Kd( void ) { return map_Kd_ ; }
148  const int& map_Kd_index( void ) { return index_Kd_ ; }
149 
150  private:
151 
152  Vec3f Kd_; bool Kd_is_set_; // diffuse
153  Vec3f Ka_; bool Ka_is_set_; // ambient
154  Vec3f Ks_; bool Ks_is_set_; // specular
155  float Tr_; bool Tr_is_set_; // transperency
156 
157  std::string map_Kd_; int index_Kd_; bool map_Kd_is_set_; // Texture
158 
159  };
160 #endif
161 
162  typedef std::map<std::string, Material> MaterialList;
163 
164  MaterialList materials_;
165 
166  bool read_material( std::fstream& _in );
167 
168 
169 private:
170 
171  bool read_vertices(std::istream& _in, BaseImporter& _bi, Options& _opt,
172  std::vector<Vec3f> & normals,
173  std::vector<Vec3f> & colors,
174  std::vector<Vec3f> & texcoords3d,
175  std::vector<Vec2f> & texcoords,
176  std::vector<VertexHandle> & vertexHandles,
177  Options & fileOptions);
178 
179  std::string path_;
180 
181 };
182 
183 
184 //== TYPE DEFINITION ==========================================================
185 
186 
187 extern _OBJReader_ __OBJReaderInstance;
188 OPENMESHDLLEXPORT _OBJReader_& OBJReader();
189 
190 
191 //=============================================================================
192 } // namespace IO
193 } // namespace OpenMesh
194 //=============================================================================
195 #endif
196 //=============================================================================
VectorT< float, 3 > Vec3f
Definition: Vector11T.hh:831
std::string get_extensions() const override
Definition: OBJReader.hh:91
Set options for reader/writer modules.
Definition: Options.hh:90
std::string get_description() const override
Returns a brief description of the file type that can be parsed.
Definition: OBJReader.hh:90