Ejemplo n.º 1
0
  /** Inserts point p in face c. Face c is split into 3 triangles. */
  public TriangulationDSVertex_2<X> insertInTriangle(X p, TriangulationDSFace_2<X> t) {
    if (t == null) throw new Error("Trying to star a null cell");
    TriangulationDSFace_2<X> f1 = t.neighbor(1);
    TriangulationDSFace_2<X> f2 = t.neighbor(2);
    TriangulationDSFace_2<X> f0 = t.neighbor(0);
    TriangulationDSVertex_2<X> v0 = t.vertex(0);
    TriangulationDSVertex_2<X> v1 = t.vertex(1);
    TriangulationDSVertex_2<X> v2 = t.vertex(2);

    // create new vertex
    TriangulationDSVertex_2<X> v = new TriangulationDSVertex_2<X>(p);
    vertices.add(v);

    // create and set two new faces
    TriangulationDSFace_2<X> newFace2 = createFace(v0, v1, v, t, null, f2);
    TriangulationDSFace_2<X> newFace1 = createFace(v0, v, v2, t, f1, newFace2);
    newFace2.setNeighbor(1, newFace1);

    // set new vertex and neighbors of old triangle
    t.setVertex(0, v);
    t.setNeighbor(1, newFace1);
    t.setNeighbor(2, newFace2);

    // set the faces incident to v and v0
    v.setFace(t);
    v0.setFace(newFace1);

    // restore the marks of the initial triangle edges
    for (int i = 0; i < 3; i++) t.unmark(i);
    if (t.neighbor(0) != null) {
      HalfedgeHandle<X> e = new HalfedgeHandle<X>(t, 0);
      e.setMark(e.getOpposite().isMarked());
    }
    if (newFace1.neighbor(1) != null) {
      HalfedgeHandle<X> e = new HalfedgeHandle<X>(newFace1, 1);
      e.setMark(e.getOpposite().isMarked());
    }
    if (newFace2.neighbor(2) != null) {
      HalfedgeHandle<X> e = new HalfedgeHandle<X>(newFace2, 2);
      e.setMark(e.getOpposite().isMarked());
    }

    // return the new vertex
    return v;
  }
Ejemplo n.º 2
0
  /** flips an edge in the triangulation and returns the new edge */
  public HalfedgeHandle<X> flipEdge(HalfedgeHandle<X> e) {
    TriangulationDSFace_2<X> f1 = e.getFace();
    TriangulationDSFace_2<X> f2 = e.getOpposite().getFace();

    // retrieve vertices of quadrangle
    ArrayList<TriangulationDSVertex_2<X>> vert = new ArrayList<TriangulationDSVertex_2<X>>();
    vert.add(f1.vertex(e.index()));
    vert.add(f1.vertex((e.index() + 1) % 3));
    vert.add(f2.vertex(e.getOpposite().index()));
    vert.add(f1.vertex((e.index() + 2) % 3));

    // retrieve neighboring faces of quadrangle
    ArrayList<TriangulationDSFace_2<X>> neighb = new ArrayList<TriangulationDSFace_2<X>>();
    neighb.add(f1.neighbor((e.index() + 2) % 3));
    neighb.add(f2.neighbor((e.getOpposite().index() + 1) % 3));
    neighb.add(f2.neighbor((e.getOpposite().index() + 2) % 3));
    neighb.add(f1.neighbor((e.index() + 1) % 3));

    // reset the vertices of the 2 quadrangle faces
    f1.setVertex(0, vert.get(0));
    f1.setVertex(1, vert.get(1));
    f1.setVertex(2, vert.get(2));
    f2.setVertex(0, vert.get(0));
    f2.setVertex(1, vert.get(2));
    f2.setVertex(2, vert.get(3));

    // reset the incident faces of the quandrangle vertices
    vert.get(0).setFace(f1);
    vert.get(1).setFace(f1);
    vert.get(2).setFace(f2);
    vert.get(3).setFace(f2);

    // reset the neighbors of the 2 quadrangle faces
    f1.setNeighbor(0, neighb.get(1));
    f1.setNeighbor(1, f2);
    f1.setNeighbor(2, neighb.get(0));
    f2.setNeighbor(0, neighb.get(2));
    f2.setNeighbor(1, neighb.get(3));
    f2.setNeighbor(2, f1);

    // reset the neighbors of the quadrangle neighbors (only modify what needs to be modified)
    if (neighb.get(1) != null) neighb.get(1).setNeighbor(neighb.get(1).index(f2), f1);
    if (neighb.get(3) != null) neighb.get(3).setNeighbor(neighb.get(3).index(f1), f2);

    // reset the marks of the edges of the two faces
    for (int i = 0; i < 3; i++) {
      f1.unmark(i);
      f2.unmark(i);
    }

    // restore the marks of the quadrangle edges (or rather their inner halfedges)
    for (int i = 0; i < 3; i++) {
      e = new HalfedgeHandle<X>(f1, i);
      if (f1.neighbor(i) != null) e.setMark(e.getOpposite().isMarked());
      e = new HalfedgeHandle<X>(f2, i);
      if (f2.neighbor(i) != null) e.setMark(e.getOpposite().isMarked());
    }

    // return the new diagonal edge
    return new HalfedgeHandle<X>(f1, 1);
  }