Subversion Repositories gelsvn

Rev

Rev 480 | Blame | Last modification | View Log | RSS feed

#include "HalfEdgeHandle.h"

#include "entities.h"

#include "Manifold.h"
#include "VertexHandle.h"
#include "FaceHandle.h"

#include "VertexCirculator.h"

namespace HMesh
{    
    bool is_boundary(HalfEdgeHandle h)
    { 
        return h.face() == NULL_FACE_HANDLE || h.opp().face() == NULL_FACE_HANDLE;
    }


    float length(HalfEdgeHandle h)
    { 
        return (h.vert().get().pos - h.opp().vert().get().pos).length(); 
    }

    void link(HalfEdgeHandle h0, HalfEdgeHandle h1)
    {
        h0.get().next_idx = h1.get_idx();
        h1.get().prev_idx = h0.get_idx();
    }

    void glue(HalfEdgeHandle h0, HalfEdgeHandle h1)
    {
        h0.get().opp_idx = h1.get_idx();
        h1.get().opp_idx = h0.get_idx();
    }

    bool flip(HalfEdgeHandle h)
    {
        FaceHandle f = h.face();
        HalfEdgeHandle ho = h.opp();
        FaceHandle fo = ho.face();

        if(f == NULL_FACE_HANDLE || fo == NULL_FACE_HANDLE){
            return false;
        }
                // We can only flip an edge if both incident polygons are triangles. 
        if(no_edges(f) != 3 || no_edges(fo) !=3){
                        return false;
        }
                // non boundary vertices with a valency of less than 4(less than 3 after operation) degenerates mesh.
        VertexHandle v = h.vert();
        VertexHandle vo = ho.vert();
        if((valency(v) < 4 && !is_boundary(v)) || (valency(vo) < 4 && !is_boundary(vo))){
            return false;
        }
                // Disallow flip if vertices being connected already are.
        VertexHandle vn = h.next().vert();
        VertexHandle von = ho.next().vert();
        if(is_connected(vn, von)){
            return false;
        }
        HalfEdgeHandle hn = h.next();
        HalfEdgeHandle hp = h.prev();
        HalfEdgeHandle hon = ho.next();
        HalfEdgeHandle hop = ho.prev();

        hop.get().face_idx = f.get_idx();
        hp.get().face_idx = fo.get_idx();
        f.get().last_idx = h.get_idx();
        fo.get().last_idx = ho.get_idx();

        link(hn, h);
        link(h, hop);
        link(hop, hn);

        link(hon, ho);
        link(ho, hp);
        link(hp, hon);

        h.get().vert_idx = von.get_idx();
        ho.get().vert_idx = vn.get_idx();

        v.get().out_idx = hn.get_idx();
        vo.get().out_idx = hon.get_idx();

        check_boundary_consistency(v);
        check_boundary_consistency(vo);

        return true;
    }
}