Subversion Repositories gelsvn

Rev

Rev 113 | Rev 136 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#ifndef __MANIFOLD_H
#define __MANIFOLD_H

#include <vector>
#include <list>
#include <map>

#include "Vertex.h"
#include "HalfEdge.h"
#include "Face.h"

namespace HMesh
{
        class VertexCirculator;
        class FaceCirculator;

        /** \brief A Data structure representing an open or closed manifold.
                        Manifold keeps lists of the entities making up a halfedge mesh
                        and provides basic functions for creating new faces, halfedges
                        and vertices. There are also primitive operations for editing 
                        such as edge collapse. */
        struct Manifold
        {
                std::list<Vertex> vertex_db;
                std::list<Face> face_db;
                std::list<HalfEdge> halfedge_db;

                /** Remove a face if it contains only two edges.
                                This is an auxiliary function called from collapse_halfedge. */
                void remove_face_if_degenerate(HalfEdgeIter fi);

                /** Empty copy constructor.
                                Copying a manifold will not work, since faces, vertices, and
                                halfedges contain iterators pointing to other faces, vertices,
                                and halfedges. These pointers would need to be changed if the 
                                mesh were copied. In other words, we would need to walk the
                                entire mesh. This may be required but it should not be an 
                                operation that is easily invoked by calling the copy constructor.
                                Hence, this operator is made private.           */
                Manifold(const Manifold& m2) {}

                /** Empty assignment operator.
                                The assignment operator is private for the same reason that the
                                copy constructor is private. */
                const Manifold& operator=(const Manifold& m2) {}

                public:
        
                /// Construct an empty manifold.
                Manifold() {}

                /// Return the bounding box of the manifold.
                void get_bbox(CGLA::Vec3f&, CGLA::Vec3f&);

                /// Get a bounding sphere
    void get_bsphere(CGLA::Vec3f& c, float& r);

                /// Clear the manifold - removing all data.
                void clear()
                {
                        vertex_db.clear();
                        face_db.clear();
                        halfedge_db.clear();
                }

                /// Return the number of faces.
                int no_faces() const {return face_db.size();}

                /// Return the number of halfedges.
                int no_halfedges() const {return halfedge_db.size();}

                /// Return the number of vertices
                int no_vertices() const {return vertex_db.size();}

                /// Create a new vertex at a given position.
                VertexIter create_vertex(const CGLA::Vec3f& pos)
                {
                        vertex_db.push_back(Vertex(pos));
                        return --vertex_db.end();
                }

                /// Create a new face 
                FaceIter create_face()
                {
                        face_db.push_back(Face());
                        return --face_db.end();
                }

                /// Create a new halfedge.
                HalfEdgeIter create_halfedge()
                {
                        halfedge_db.push_back(HalfEdge());
                        return --halfedge_db.end();
                }

                /// Erase halfedge. Assumes it is safe to do so. Use with care.
                void erase_halfedge(HalfEdgeIter he)
                {
                        halfedge_db.erase(he);
                }

                /// Erase vertex. Assumes it is safe to do so. Use with care.
                void erase_vertex(VertexIter v)
                {
                        vertex_db.erase(v);
                }
        
                /// Erase face. Assumes it is safe to do so. Use with care.
                void erase_face(FaceIter f)
                {
                        face_db.erase(f);
                }

                /// Return iterator pointing to the first halfedge.
                HalfEdgeIter halfedges_begin() { return halfedge_db.begin();}

                /// Return iterator pointing to beyond the last halfedge.
                HalfEdgeIter halfedges_end() { return halfedge_db.end(); }
        
                /// Return iterator pointing to the first vertex.
                VertexIter vertices_begin() { return vertex_db.begin();}

                /// Return iterator pointing to beyond the last vertex.
                VertexIter vertices_end() { return vertex_db.end(); }

                /// Return iterator pointing to the first face.
                FaceIter faces_begin() { return face_db.begin();        }

                /// Return iterator pointing to beyond the last face.
                FaceIter faces_end() { return face_db.end(); }

                /** \brief HalfEdge collapse precondition. 

                                If this function does not return true, it is illegal to collapse
                                the halfedge given as second arg. The reason is that the collapse
                                would violate the manifold property of the mesh.

                                The test is as follows.

                                1. For the two vertices adjacent to the edge, we generate
                                a list of all their neighbouring vertices. We then generate a 
                                list of the vertices that occur in both these lists. That is, 
                                we find all vertices connected by edges to both endpoints
                                of the edge and store these in a list.

                                2. For both faces incident on the edge, check whether they
                                are triangular. If this is the case, the face will be removed,
                                and it is ok that the the third vertex is connected to both 
                                endpoints. Thus the third vertex in such a face is removed
                                from the list generated in 1.

                                3. If the list is now empty, all is well. Otherwise, there
                                would be a vertex in the new mesh with two edges connecting
                                it to the same vertex. Return false.

                                4. TETRAHEDRON TEST:
                                If the valency of both vertices is 
                                three, and the incident faces are triangles, we also disallow
                                the operation. Reason: It introduces a vertex of valency two
                                and if the final two polygons incident on the vertices 
                                that are adjacent to the edge being collapsed are triangles, then
                                the construction will collapse

                                5. VALENCY 4 TEST:
                                If a face adjacent to the edge being collapsed is a triangle,
                                it will disappear, and the valency of the final vertex beloning to
                                this edge will be reduced by one. Hence this valency should be at
                                least 4. A valency three vertex will be reduced to a valency two
                                vertex which is considered illegal.

                                6. PREVENT MERGING HOLES:
                                We do not want to collapse an edge if its end points are boundary
                                vertices, but its two adjacent faces are not NULL faces, because
                                in that case we create a vertex where two holes meet. A non manifold
                                situation.      */
                bool collapse_precond(VertexIter, HalfEdgeIter);
                
                /** Collapse the halfedge given as second argument. 
                                The first argument is the vertex that is being removed. The
                                second is a halfedge pointing away from that vertex.

                                The final argument is a boolean indicating whether the vertex
                                that survives the collapse should have a position which is the
                                average of its own position and the vertex that is removed.
                                By default false.

                                This function is not guaranteed to keep the mesh sane unless,
                                collapse_precond has returned true.
                */
                void collapse_halfedge(VertexIter, HalfEdgeIter, bool=false);

                /** Split a face.
                    The face given as first argument is split by connecting two
                                vertices (the next two arguments). */
                FaceIter split_face(FaceIter, VertexIter, VertexIter);

                /** Insert a new vertex on the halfedge given as argument.
                                The new halfedge is insterted as the previous edge to the one
                                given as argument.
                */
                VertexIter Manifold::split_edge(HalfEdgeIter h);

                /** Triangulate a polygonal face by repeatedly calling split_face. 
                                Triangulate iteratively splits triangles off a polygon. The first 
                                triangle split off is the one connecting f->last->vert and
                                f->last->next->next->vert.
                */
                void triangulate(FaceIter);

                /** Triangulate a polygon by inserting a vertex at the barycenter and 
                                connecting all vertices to this vertex. 
                                
                                The word "safe" means that it is less likely to create flipped
                                triangles than the normal triangulate. On the other hand, it 
                                introduces more vertices and probably makes the triangles more
                                acute.
                                
                                This function simply calls face_insert_point.
                */
                VertexIter safe_triangulate(FaceIter f);

                /** Insert a new vertex (second arg.) in a face (first arg.).
                                This operation splits an N-gon into N triangles.
                                In the current implementation the original face is erased. 
                                This means that the iterator is not valid after the function
                                returns. 
                */
                void face_insert_point(FaceIter f, VertexIter);

                /** Merges two faces into a single polygon. The first face is the first
                                argument. The second face is adjacent to the first along the
                                halfedge given as second argument. */
                bool merge_faces(FaceIter f, HalfEdgeIter h);

                /** Flip an edge. Returns false if flipping cannot be performed.
                                This is either because one of the two adjacent faces is not
                                a triangle, or because either end point has valency three or
                                because the vertices that will be connected already are. */
                bool flip(HalfEdgeIter);

                /** Performs a series of tests to check that this is a valid manifold.
                                This function is not rigorously constructed but seems to catch
                                all problems so far. */
                bool is_valid();

                /** Give each vertex a unique id corresponding to its iterator 
                                position */
                void enumerate_vertices();

                /** Give each halfedge a unique id corresponding to its iterator 
                                position */
                void enumerate_halfedges();

                /** Give each face a unique id corresponding to its iterator 
                                position */
                void enumerate_faces();
                
        };


}
#endif