/** 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;
  }
  /**
   * creates a new face in the triangulation, with the given incident vertices and adjacent
   * triangles.
   */
  public TriangulationDSFace_2<X> createFace(
      TriangulationDSVertex_2<X> v1,
      TriangulationDSVertex_2<X> v2,
      TriangulationDSVertex_2<X> v3,
      TriangulationDSFace_2<X> c1,
      TriangulationDSFace_2<X> c2,
      TriangulationDSFace_2<X> c3) {

    TriangulationDSFace_2<X> newFace = new TriangulationDSFace_2<X>(v1, v2, v3, c1, c2, c3);
    for (int i = 0; i < 3; i++) {
      TriangulationDSFace_2<X> face = newFace.neighbors.get(i);
      if (face != null) {
        // update neighbor pointer in neighboring cell
        face.setNeighbor(face.index(newFace), newFace);
      }
    }
    this.faces.add(newFace);
    return newFace;
  }
  /**
   * Create a new triangulation from the face-vertex incidence relations It takes linear space and
   * linear time.
   */
  public TriangulationDS_2(X[] points, int[][] neighbors) {

    if (points == null || neighbors == null || points[0] == null)
      throw new Error("error: null vertices or null faces");
    System.out.print("Creating triangulated 3D mesh... ");

    this.faces = new ArrayList<TriangulationDSFace_2<X>>();
    this.vertices = new ArrayList<TriangulationDSVertex_2<X>>();

    for (int i = 0; i < points.length; i++) {
      if (points[i] == null) throw new Error("null vertex error");
      TriangulationDSVertex_2<X> v = new TriangulationDSVertex_2<X>(points[i]);
      this.vertices.add(v);
    }

    // first pair (the key) represents the edges of the triangulation
    // second pair (the associated value) represents the two neighboring faces (sharing an edge)
    HashMap<Pair<Integer>, Pair<Integer>> edges = new HashMap<Pair<Integer>, Pair<Integer>>();
    for (int i = 0; i < neighbors.length; i++) {
      int i0 = neighbors[i][0], i1 = neighbors[i][1], i2 = neighbors[i][2];
      createFace(this.vertices.get(i0), vertices.get(i1), vertices.get(i2), null, null, null);

      for (int j = 0; j < 3; j++) {
        int index1 = neighbors[i][(j + 1) % 3];
        int index2 = neighbors[i][(j + 2) % 3];
        Pair<Integer> edge;
        if (index1 <= index2) edge = new Pair<Integer>(index1, index2);
        else edge = new Pair<Integer>(index2, index1);

        if (edges.containsKey(edge) == false) {
          Pair<Integer> face;
          if (index1 <= index2) face = new Pair<Integer>(i, -1);
          else face = new Pair<Integer>(-1, i);
          edges.put(edge, face);
        } else {
          Pair<Integer> face = edges.get(edge);
          if (face.getFirst() == -1) face.setFirst(i);
          else face.setSecond(i);
        }
      }
    }

    // System.out.println("setting neighboring faces");
    for (int i = 0; i < neighbors.length; i++) {
      TriangulationDSFace_2<X> currentFace = this.faces.get(i);
      for (int j = 0; j < 3; j++) {

        // setting vertex adjacent face
        TriangulationDSVertex_2<X> v = this.vertices.get(neighbors[i][j]);
        v.setFace(this.faces.get(i));

        int index1 = neighbors[i][(j + 1) % 3];
        int index2 = neighbors[i][(j + 2) % 3];
        Pair<Integer> edge;
        if (index1 <= index2) edge = new Pair<Integer>(index1, index2);
        else edge = new Pair<Integer>(index2, index1);

        if (edges.containsKey(edge) == false) throw new Error("error: edge not found");
        else {
          Pair<Integer> face = edges.get(edge);
          if (face.getFirst() == -1 && face.getSecond() == -1)
            throw new Error("error: wrong adjacent faces");
          if (face.getFirst() == -1 || face.getSecond() == -1) ; // boundary edge
          else {
            int neighborFace;
            if (index1 <= index2) neighborFace = face.getSecond();
            else neighborFace = face.getFirst();
            if (neighborFace < 0 || neighborFace >= this.faces.size())
              throw new Error("error neighbor face index");
            currentFace.setNeighbor(j, this.faces.get(neighborFace));
          }
        }
      }
    }
    System.out.println("done");
  }
  /** 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);
  }