rtVTK  0.6.0
Mesh.h
00001 
00002 /******************************************************************************
00003  * Copyright (c) 2012, Grove City College
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are
00008  * met:
00009  * 
00010  *  * Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  *
00013  *  * Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in
00015  *    the documentation and/or other materials provided with the
00016  *    distribution.
00017  *
00018  *  * Neither the name of Grove City College nor the names of its
00019  *    contributors may be used to endorse or promote products derived
00020  *    from this software without specific prior written permission.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00025  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00026  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00027  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00028  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00029  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00030  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00031  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00032  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  */
00035 
00036 
00037 #ifndef Core_Mesh_h
00038 #define Core_Mesh_h
00039 
00040 #include <iosfwd>
00041 using std::ostream;
00042 
00043 #include <fstream>
00044 using std::ifstream;
00045 
00046 #include <map>
00047 using std::map;
00048 
00049 #include <string>
00050 using std::string;
00051 
00052 #include <vector>
00053 using std::vector;
00054 
00055 #include <Core/RGB.t>
00056 
00057 #include <Math/Math.h>
00058 
00059 
00061 // Type definitions
00062 
00063 namespace CoreF
00064 {
00065 
00066   typedef MathF::Vector TexCoord;
00067 
00068 } // namespace CoreF
00069 
00070 
00071 namespace Core
00072 {
00073 
00075   // Using declarations
00076 
00077   using CoreF::TexCoord;
00078 
00079   using MathF::Point;
00080   using MathF::Vector;
00081 
00082 
00084   // Forward declarations
00085 
00086   class Texture;
00087 
00088 
00090   // Class definition
00091 
00095   class Mesh
00096   {
00097   public:
00098     static const int undefined;
00099 
00104     struct Material
00105     {
00106       enum Type { Diffuse = 0, Specular, Emissive, ntypes };
00107 
00108       Material();
00109 
00110       bool read(istream&);
00111       bool write(ostream&) const;
00112 
00118       friend istream& operator>>(istream& in,        Material&) { return in;  }
00119       friend ostream& operator<<(ostream& out, const Material&) { return out; }
00120 
00121       CoreF::RGB color[ntypes];
00122 
00123       string   filename[ntypes];
00124       Texture* tex[ntypes];
00125 
00126       float uscale[ntypes];
00127       float vscale[ntypes];
00128 
00129       float exp;
00130       float r0;
00131       bool  tr;
00132     };
00133 
00137         struct Triangle
00138     {
00139       Triangle(uint, uint, uint, uint);
00140       Triangle();
00141 
00142       bool read(istream&);
00143       bool write(ostream&) const;
00144 
00150       friend istream& operator>>(istream& in,        Triangle&) { return in;  }
00151       friend ostream& operator<<(ostream& out, const Triangle&) { return out; }
00152       
00153       uint vID[3];
00154       uint mID;
00155     };
00156 
00160     struct Vertex
00161     {
00162       bool read(istream&);
00163       bool write(ostream&) const;
00164 
00170       friend istream& operator>>(istream& in,        Vertex&) { return in;  }
00171       // friend ostream& operator<<(ostream& out, const Vertex&) { return out; }
00172 
00173       Point    p;
00174       Vector   n;
00175       TexCoord t;
00176     };
00177 
00178 
00180     // Constructors, destructor
00181 
00182     Mesh(const string&);
00183     Mesh();
00184 
00185     ~Mesh();
00186 
00187 
00189     // Accessors
00190 
00191     const vector<Material>& materials() const;
00192     const vector<Triangle>& triangles() const;
00193     const vector<Vertex>&   vertices()  const;
00194           Vector            diagonal()  const;
00195 
00196 
00198     // Mutators
00199 
00200     vector<Material>& materials();
00201     vector<Triangle>& triangles();
00202     vector<Vertex>&   vertices();
00203 
00204 
00206     // Geometry processing
00207 
00208     bool load(const string&);
00209 
00210 
00212     // Mesh persistence
00213 
00214     bool read(istream&);
00215     bool write(ostream&) const;
00216 
00217 
00218   protected:
00219 
00223         struct Sphere
00224     {
00225       Sphere(float, float, float, float, uint);
00226 
00227       Point c;
00228       float r;
00229       uint  mID;
00230     };
00231 
00232 
00234     // Helpers
00235 
00236     bool loadML(const string&);
00237     void loadML(ifstream&);
00238 
00239     bool loadOBJ(const string&);
00240     bool loadMTL(      vector<Material>&,
00241                        map<string, int>&,
00242                        string&,
00243                  const string&);
00244     bool loadIW( const string&);
00245 
00246     Texture* genTexture(      map<string, Texture*>&,
00247                         const Material&,
00248                         const Material::Type);
00249 
00250 
00252     // Data members
00253 
00254     vector<Material> m;
00255     vector<Triangle> t;
00256     vector<Vertex>   v;
00257 
00258     uint nbytes;
00259   };
00260 
00261 
00263   // Stream I/O operator
00264 
00265   ostream& operator<<(ostream&, const Mesh::Vertex&);
00266 
00267 
00269   // Inline member definitions
00270 
00271   inline Mesh::Material::Material() :
00272     exp(0.f),
00273     r0(0.f),
00274     tr(false) 
00275   {
00276     for (uint i = 0; i < ntypes; ++i)
00277     {
00278       filename[i] = "";
00279       tex[i]      = 0;
00280       uscale[i]   = 0.f;
00281       vscale[i]   = 0.f;
00282     }
00283   }
00284 
00285   inline const vector<Mesh::Material>& Mesh::materials() const
00286   {
00287     return m;
00288   }
00289 
00290   inline const vector<Mesh::Triangle>& Mesh::triangles() const
00291   {
00292     return t;
00293   }
00294 
00295   inline const vector<Mesh::Vertex>& Mesh::vertices() const
00296   {
00297     return v;
00298   }
00299 
00300   inline vector<Mesh::Material>& Mesh::materials()
00301   {
00302     return m;
00303   }
00304 
00305   inline vector<Mesh::Triangle>& Mesh::triangles()
00306   {
00307     return t;
00308   }
00309 
00310   inline vector<Mesh::Vertex>& Mesh::vertices()
00311   {
00312     return v;
00313   }
00314 
00315 } // namespace Core
00316 
00317 #endif // Core_Mesh_h
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends