Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ShMesh.hpp

Go to the documentation of this file.
00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright 2003-2005 Serious Hack Inc.
00004 // 
00005 // This software is provided 'as-is', without any express or implied
00006 // warranty. In no event will the authors be held liable for any damages
00007 // arising from the use of this software.
00008 // 
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it
00011 // freely, subject to the following restrictions:
00012 // 
00013 // 1. The origin of this software must not be misrepresented; you must
00014 // not claim that you wrote the original software. If you use this
00015 // software in a product, an acknowledgment in the product documentation
00016 // would be appreciated but is not required.
00017 // 
00018 // 2. Altered source versions must be plainly marked as such, and must
00019 // not be misrepresented as being the original software.
00020 // 
00021 // 3. This notice may not be removed or altered from any source
00022 // distribution.
00024 #ifndef SHUTIL_SHMESH_HPP
00025 #define SHUTIL_SHMESH_HPP
00026 
00027 #include <list>
00028 #include <map>
00029 #include <set>
00030 
00031 #include "sh.hpp"
00032 
00033 namespace ShUtil {
00034 
00067 template<typename VertexType, typename FaceType, typename EdgeType>
00068 struct ShMeshType {
00069   typedef VertexType Vertex;
00070   typedef FaceType Face;
00071   typedef EdgeType Edge;
00072 };
00073 
00074 template<typename M>
00075 struct ShMeshVertex {
00076   typedef typename M::Edge Edge;
00077   Edge *edge; //< Edge that starts at this vertex 
00078 
00080   ShMeshVertex();
00081 
00083   ShMeshVertex(const ShMeshVertex<M> &other);
00084 };
00085 
00086 template<typename M>
00087 struct ShMeshFace {
00088   typedef typename M::Edge Edge;
00089   Edge *edge; //< Edge in this face 
00090 
00092   ShMeshFace();
00093 
00095   ShMeshFace(const ShMeshFace<M> &other);
00096 };
00097 
00098 // A half-edge going from start to end that is part of face.
00099 template<typename M>
00100 struct ShMeshEdge {
00101   typedef typename M::Vertex Vertex;
00102   typedef typename M::Face Face;
00103   typedef typename M::Edge Edge;
00104   Vertex *start; //< Start vertex 
00105   Vertex *end;  //< End vertex
00106   Face *face; //< Face  
00107   Edge *sym; //< Edge paired with this edge.
00108   Edge *next; //< Next edge in the face 
00109   Edge *prev; //< Previous edge in the face
00110 
00112   ShMeshEdge();
00113 
00115   ShMeshEdge(const ShMeshEdge<M> &other);
00116 
00119   void setLinks(Vertex *s, Vertex *e, Face *f, 
00120       Edge *next, Edge *prev, Edge *sym);
00121 
00123   void setNext(Edge *n);
00124 
00126   void setPrev(Edge *p);
00127 
00129   void setSym(Edge *s);
00130 };
00131 
00132 
00134 template<typename M>
00135 class ShMesh {
00136   public:
00137     typedef M MeshType; 
00138     typedef typename M::Vertex Vertex; 
00139     typedef typename M::Edge Edge; 
00140     typedef typename M::Face Face; 
00141 
00142     typedef std::set<Vertex*> VertexSet;
00143     typedef std::set<Edge*> EdgeSet;
00144     typedef std::set<Face*> FaceSet;
00145     typedef std::list<Vertex*> VertexList;
00146 
00148     ShMesh();
00149 
00152     ShMesh(const ShMesh<M>& other);
00153 
00155     ~ShMesh();
00156 
00158     ShMesh<M>& operator=(const ShMesh<M>& other); 
00159 
00161     void clear();
00162 
00168     Face* addFace(const VertexList &vl);
00169 
00173     void removeFace(Face *f);
00174 
00179     template<typename VertLess>
00180     void mergeVertices();
00181 
00189     void mergeEdges();
00190 
00193     bool earTriangulate();
00194 
00195     VertexSet verts;
00196     EdgeSet edges;
00197     FaceSet faces;
00198 
00199   protected:
00200     typedef std::map<Vertex*, Vertex*> VertexMap;
00201     typedef std::map<Edge*, Edge*> EdgeMap;
00202     typedef std::map<Face*, Face*> FaceMap;
00203 
00204     // TODO this is a real hack...figure out how to do removeHalfEdge(e) in log or 
00205     // constant time without the incidence map for weird meshes 
00206     // (e.g. with articulation points).
00207     
00208     // On certain meshes, all edges incident to a vertex v can be found
00209     // by traversing v.edge->sym pointers, but this is not always
00210     // the case.
00211     typedef std::multimap<Vertex*, Edge*> IncidenceMap;
00212     typedef typename IncidenceMap::value_type Incidence;
00213     typedef typename IncidenceMap::iterator IncidenceIterator;
00214     typedef std::pair<typename IncidenceMap::iterator, 
00215               typename IncidenceMap::iterator> IncidenceRange;
00216 
00217     IncidenceMap m_incidences; // m_incidences[v] holds all edges e with e.start = v
00218 
00226     void removeHalfEdge(Edge *e);
00227 
00229     void insertHalfEdge(Edge *e);
00230 };
00231 
00232 
00233 }
00234 
00235 #include "ShMeshImpl.hpp"
00236 
00237 #endif

Generated on Wed Jun 15 18:12:40 2005 for Sh by  doxygen 1.4.3-20050530