GEL  2
GEL is a library for Geometry and Linear Algebra
/Users/jab/Documents/Teaching/02585/GEL2_and_demos/GEL/src/HMesh/Manifold.h
00001 /* ----------------------------------------------------------------------- *
00002  * This file is part of GEL, www.imm.dtu.dk/GEL
00003  * Copyright (C) the authors (see AUTHORS.txt) and DTU Informatics
00004  *
00005  * Principal authors:
00006  *  Christian Thode Larsen (thode2d@gmail.com)
00007  *  J. Andreas Baerentzen (jab@imm.dtu.dk)
00008  *
00009  * See LICENSE.txt for licensing information
00010  * ----------------------------------------------------------------------- */
00011 
00012 #ifndef __HMESH_MANIFOLD_H__
00013 #define __HMESH_MANIFOLD_H__
00014 
00015 #include <algorithm>
00016 #include <CGLA/Vec3f.h>
00017 
00018 #include "ConnectivityKernel.h"
00019 #include "Iterators.h"
00020 #include "HalfEdgeWalker.h"
00021 #include "AttributeVector.h"
00022 
00023 
00024 namespace Geometry
00025 {
00026     // forward declaration
00027     class TriMesh;
00028     class IndexedFaceSet;
00029 }
00030 
00031 namespace HMesh
00032 {       
00033     class Manifold
00034     {
00035     public:
00037         Manifold();
00038 
00043         void build( size_t no_vertices,
00044                     const float* vertvec,
00045                     size_t no_faces,
00046                     const int* facevec,
00047                     const int* indices);
00048 
00050         void build(const Geometry::TriMesh& mesh);
00051                 
00064                 std::vector<HalfEdgeID> bridge_faces(FaceID f0, FaceID f1, const std::vector<std::pair<VertexID, VertexID> >& pairs);
00065 
00066 
00072         void collapse_edge(HalfEdgeID h, bool avg_vertices = false);
00073 
00078         FaceID split_face_by_edge(FaceID f, VertexID v0, VertexID v1);
00079 
00084         VertexID split_face_by_vertex(FaceID f);
00085        // VertexID split_face_by_vertex(HalfEdgeID h);
00086 
00090         VertexID split_edge(HalfEdgeID h);
00091 
00096         bool merge_faces(FaceID f, HalfEdgeID h);
00097                 
00112                 FaceID merge_one_ring(VertexID v, float max_loop_length = FLT_MAX);
00113 
00115         void close_hole(HalfEdgeID h);
00116 
00118         void flip_edge(HalfEdgeID h);
00119 
00121         size_t active_vertices() const;
00123         size_t active_halfedges() const;
00125         size_t active_faces() const;
00126 
00128         size_t total_vertices() const;
00130         size_t total_halfedges() const;
00132         size_t total_faces() const;
00133 
00134         bool in_use(VertexID id) const;
00135         bool in_use(FaceID id) const;
00136         bool in_use(HalfEdgeID id) const;
00137 
00139         VertexIDIterator vertices_begin(bool skip = true) const;
00141         FaceIDIterator faces_begin(bool skip = true) const;
00143         HalfEdgeIDIterator halfedges_begin(bool skip = true) const;
00144 
00146         VertexIDIterator vertices_end() const;
00148         FaceIDIterator faces_end() const;
00150         HalfEdgeIDIterator halfedges_end() const;
00151 
00153         HalfEdgeWalker halfedgewalker(VertexID id) const;
00155         HalfEdgeWalker halfedgewalker(FaceID id) const;
00157         HalfEdgeWalker halfedgewalker(HalfEdgeID id) const;
00158 
00160         CGLA::Vec3f& pos(VertexID id);
00162         const CGLA::Vec3f& pos(VertexID id) const;
00163 
00165         void clear();
00166 
00168         void cleanup(IDRemap& map);
00170         void cleanup();
00171     private:
00172         ConnectivityKernel ck;
00173         VertexAttributeVector<CGLA::Vec3f> positions;
00174 
00175         // private template for building the manifold from various types
00176         template<typename size_type, typename float_type, typename int_type>
00177         void build_template(size_type no_vertices,
00178                             const float_type* vertvec,
00179                             size_type no_faces,
00180                             const int_type* facevec,
00181                             const int_type* indices);
00182 
00184         void link(HalfEdgeID h0, HalfEdgeID h1);
00185 
00187         void glue(HalfEdgeID h0, HalfEdgeID h1);
00188 
00190         void remove_face_if_degenerate(HalfEdgeID h);
00191 
00193         void ensure_boundary_consistency(VertexID v);
00194     };
00195 
00200     bool valid(const Manifold& m);
00201 
00203     void bbox(const Manifold& m, CGLA::Vec3f& pmin, CGLA::Vec3f& pmax);
00204 
00206     void bsphere(const Manifold& m, CGLA::Vec3f& c, float& r);
00207 
00234     bool precond_collapse_edge(const Manifold& m, HalfEdgeID h);
00235 
00241     bool precond_flip_edge(const Manifold& m, HalfEdgeID h);
00242 
00244     bool boundary(const Manifold& m, HalfEdgeID h);
00245 
00247     float length(const Manifold& m, HalfEdgeID h);
00248 
00250     bool boundary(const Manifold& m, VertexID v);
00251 
00253     int valency(const Manifold& m, VertexID v);
00254 
00256     CGLA::Vec3f normal(const Manifold& m, VertexID v);
00257 
00259     bool connected(const Manifold& m, VertexID v0, VertexID v1);
00260 
00262     int no_edges(const Manifold& m, FaceID f);
00263 
00267     CGLA::Vec3f normal(const Manifold& m, FaceID f);
00268 
00270     float area(const Manifold& m, FaceID f);
00271 
00273     float perimeter(const Manifold& m, FaceID f);
00274 
00276     CGLA::Vec3f centre(const Manifold& m, FaceID f);
00277 
00278     /*******************************************************************
00279     * Manifold code
00280     *******************************************************************/
00281 
00282     inline Manifold::Manifold(){}
00283 
00284 
00285     inline size_t Manifold::active_vertices() const
00286     { return ck.active_vertices(); }
00287     inline size_t Manifold::active_halfedges() const
00288     { return ck.active_halfedges(); }
00289     inline size_t Manifold::active_faces() const
00290     { return ck.active_faces(); }
00291 
00292     inline size_t Manifold::total_vertices() const
00293     { return ck.total_vertices(); }
00294     inline size_t Manifold::total_halfedges() const
00295     { return ck.total_halfedges(); }
00296     inline size_t Manifold::total_faces() const
00297     { return ck.total_faces(); }
00298 
00299     inline bool Manifold::in_use(VertexID id) const
00300     { return ck.in_use(id); }
00301     inline bool Manifold::in_use(FaceID id) const
00302     { return ck.in_use(id); }
00303     inline bool Manifold::in_use(HalfEdgeID id) const
00304     { return ck.in_use(id); }
00305 
00306     inline VertexIDIterator Manifold::vertices_begin(bool skip) const
00307     { return VertexIDIterator(ck, ck.vertices_begin(skip), skip); }
00308     inline  FaceIDIterator Manifold::faces_begin(bool skip) const
00309     { return FaceIDIterator(ck, ck.faces_begin(skip), skip); }
00310     inline HalfEdgeIDIterator Manifold:: halfedges_begin(bool skip) const
00311     { return HalfEdgeIDIterator(ck, ck.halfedges_begin(skip), skip); }
00312 
00313 
00314     inline VertexIDIterator Manifold::vertices_end() const
00315     { return VertexIDIterator(ck, ck.vertices_end()); }
00316     inline FaceIDIterator Manifold::faces_end() const
00317     { return FaceIDIterator(ck, ck.faces_end()); }
00318     inline HalfEdgeIDIterator Manifold::halfedges_end() const
00319     { return HalfEdgeIDIterator(ck, ck.halfedges_end()); }
00320 
00321 
00322     inline HalfEdgeWalker Manifold::halfedgewalker(VertexID id) const
00323     { return HalfEdgeWalker(ck, ck.out(id)); }
00324     inline HalfEdgeWalker Manifold::halfedgewalker(FaceID id) const
00325     { return HalfEdgeWalker(ck, ck.last(id)); }
00326     inline HalfEdgeWalker Manifold::halfedgewalker(HalfEdgeID id) const
00327     { return HalfEdgeWalker(ck, id); }
00328 
00329 
00330     inline CGLA::Vec3f& Manifold::pos(VertexID id)
00331     { return positions[id]; }
00332     inline const CGLA::Vec3f& Manifold::pos(VertexID id) const
00333     { return positions[id]; }
00334 
00335 
00336     inline void Manifold::clear()
00337     { 
00338         ck.clear();
00339         positions.clear();
00340     }
00341 
00342 
00343     inline void Manifold::cleanup(IDRemap& map)
00344     {   
00345         ck.cleanup(map);
00346         positions.cleanup(map.vmap);
00347     }
00348     inline void Manifold::cleanup()
00349     {
00350         IDRemap map;
00351         cleanup(map);
00352     }
00353 }
00354 
00355 
00356 #endif __HMESH_MANIFOLD_H__
00357 
00358 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations