polyhedron.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2007 by Pablo Diaz-Gutierrez   *
00003  *   pablo@ics.uci.edu   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Library General Public License as       *
00007  *   published by the Free Software Foundation; either version 2 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU Library General Public     *
00016  *   License along with this program; if not, write to the                 *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00025 #ifndef __POLYHEDRON_H__
00026 #define __POLYHEDRON_H__
00027 
00028 #ifdef _WIN32
00029 #pragma warning(disable:4786)
00030 #endif
00031 
00032 #include <cassert>
00033 #include <vector>
00034 #include <map>
00035 #include <set>
00036 #include <fstream>
00037 
00038 #include "halfedge.h"
00039 #include "face.h"
00040 #include "vertex.h"
00041 
00042 
00048 namespace HE
00049 {
00052         enum filetype_e
00053         {
00054     FT_PLY_ASCII, 
00055                 FT_PLY_BIN, 
00056                 FT_OFF, 
00057     FT_OBJ, 
00058     FT_TRI, 
00059     FT_NONE 
00060         };
00061 
00062         class Polyhedron_GL;
00063 
00070         class Polyhedron
00071         {
00072                 public:
00073                         friend class Polyhedron_GL;
00074 
00078          Polyhedron();
00079 
00085          explicit Polyhedron(const char* filename);
00086 
00093                         explicit Polyhedron(const std::vector<Vertex*>& verts, const std::vector<std::vector<int> >& faces);
00094 
00100          Polyhedron* refine() const;
00101 
00105                         ~Polyhedron();
00106 
00113                         bool load(const char* filename);
00114 
00121                         bool readPly(const char* filename);
00122 
00129       bool readOff(const char* filename);
00130 
00137       bool readTri(const char* filename);
00138 
00145                         bool save(const char* filename) const;
00146 
00153                         bool saveAsPly(const char* filename) const;
00154 
00161       bool saveAsOff(const char* filename) const;
00162 
00169       bool saveAsTri(const char* filename) const;
00170 
00174       void clearData();
00175 
00183                         Vertex* addVertex(float x, float y, float z) { return addVertex(Geometry::Vector3Df(x, y, z)); }
00184 
00190          Vertex* addVertex(const Geometry::Vector3Df& v);
00191 
00192 
00198          Face* addFace(const std::vector<int>& corners);
00199 
00207          Face* addFace(int a, int b, int c);
00208 
00210          void loadVertices(const std::vector<float>& verts);
00211 
00213          void loadFaces(const std::vector<std::vector<int> >& faces);
00214 
00219                         int numVertices() const { return (int)_vertices.size(); }
00220                         
00225                         int numFaces() const { return (int)_faces.size(); }
00226 
00231                         int numHalfEdges() const { return (int)_halfEdges.size(); }
00232 
00238                         int numHoles() const;
00239 
00246          bool adjacent(const Vertex* v1, const Vertex* v2) const;
00247                         
00254          bool adjacent(const Face* f1, const Face* f2) const;
00255                         
00261                         filetype_e type() const { return _type; }
00262                         
00267                         void type(filetype_e ft) { _type = ft; }
00268 
00276       HalfEdge* edge(int from, int to) const;
00277       HalfEdge* edge(const Vertex* vFrom, const Vertex* vTo) const { return edge(vFrom->index(), vTo->index()); }
00278 
00279                         Vertex* vertex(int v) { return _vertices[v]; } 
00280                         const Vertex* vertex(int v) const { return _vertices[v]; } 
00281                         Face* face(int f) { return _faces[f]; } 
00282                         const Face* face(int f) const { return _faces[f]; } 
00283                         HalfEdge* halfedge(int h) { return _halfEdges[h]; } 
00284                         const HalfEdge* halfedge(int h) const { return _halfEdges[h]; } 
00285 
00286                         const std::vector<Vertex*>& vertices() const { return _vertices; } 
00287 
00289                         void invertNormals();
00290                         
00292                         void packVertices();
00293                         
00298                         void scaleAndCenter(float side=10);
00299                         
00305                         void boundingBox(Geometry::Vector3Df& m, Geometry::Vector3Df& M) const;
00306 
00310          void finalize();
00311 
00312                         // Iterators/circulators
00313 #include "polyhedron_iterators.h"
00314 
00319           void writeVertices(std::ostream& out) const;
00320 
00325          void writeFaces(std::ostream& out, bool includeHoles=false) const;
00326 
00331          void writeHalfEdges(std::ostream& out) const;
00332 
00338          bool check(bool holesFilled=true) const;
00339 
00340      protected:
00341          void closeSurface(std::map<std::pair<int,int>, HalfEdge*>& connections);
00342          void GL(Polyhedron_GL* gl) const { _gl = gl; }
00343          bool readPlyHeader(std::ifstream& in, int& nv, int& nf, short order[11]);
00344          bool readPlyData(std::ifstream& in, int& nv, int& nf, const short order[11], std::vector<float>& verts, std::vector<std::vector<int> >& faces);
00345 
00346                         std::vector<Face*> _faces;
00347                         std::set<Face*> _holes;
00348                         std::vector<HalfEdge*> _halfEdges;
00349                         std::vector<Vertex*> _vertices;
00350                         mutable Polyhedron_GL* _gl;
00351                         filetype_e _type;
00352         };
00353 
00359    std::ostream& operator<<(std::ostream& out, const Polyhedron& p);
00360 
00366         filetype_e fileFormat(const char* filename);
00367 };
00368 
00369 #endif // __POLYHEDRON_H__

Generated on Wed Apr 9 19:22:37 2008 for HalfEdge library by  doxygen 1.5.3