Program: 29970
Project: 29970
Buggy source:
@Override protected HE_Mesh createBase(){
  final HE_Mesh mesh=new HE_Mesh();
  if ((faces != null) && (vertices != null)) {
    if (faces.length == 0) {
      return mesh;
    }
    final boolean useUVW=(uvws != null) && (uvws.length == vertices.length);
    final HE_Vertex[] uniqueVertices=new HE_Vertex[vertices.length];
    final boolean[] duplicated=new boolean[vertices.length];
    if (duplicate) {
      final WB_KDTree<WB_Coord,Integer> kdtree=new WB_KDTree<WB_Coord,Integer>();
      WB_KDEntry<WB_Coord,Integer>[] neighbors;
      HE_Vertex v=new HE_Vertex(vertices[0]);
      if (useUVW) {
        v.setUVW(uvws[0]);
      }
      kdtree.add(v,0);
      uniqueVertices[0]=v;
      duplicated[0]=false;
      mesh.add(v);
      for (int i=1; i < vertices.length; i++) {
        v=new HE_Vertex(vertices[i]);
        if (useUVW) {
          v.setUVW(uvws[i]);
        }
        neighbors=kdtree.getNearestNeighbors(v,1);
        if (neighbors[0].d2 < WB_Epsilon.SQEPSILON) {
          uniqueVertices[i]=uniqueVertices[neighbors[0].value];
          duplicated[i]=true;
        }
 else {
          kdtree.add(v,i);
          uniqueVertices[i]=v;
          mesh.add(uniqueVertices[i]);
          duplicated[i]=false;
        }
      }
    }
 else {
      HE_Vertex v;
      for (int i=0; i < vertices.length; i++) {
        v=new HE_Vertex(vertices[i]);
        if (useUVW) {
          v.setUVW(uvws[i]);
        }
        v.setTemporaryLabel(i);
        uniqueVertices[i]=v;
        duplicated[i]=false;
        mesh.add(uniqueVertices[i]);
      }
    }
    int id=0;
    HE_Halfedge he;
    final List<Long> nmedges=new ArrayList<Long>();
    if (normalcheck) {
      final FastMap<Long,int[]> edges=new FastMap<Long,int[]>();
      for (int i=0; i < faces.length; i++) {
        final int[] face=faces[i];
        final int fl=face.length;
        for (int j=0; j < fl; j++) {
          final long ohash=ohash(face[j],face[(j + 1) % fl]);
          final int[] faces=edges.get(ohash);
          if (faces == null) {
            edges.put(ohash,new int[]{i,-1});
          }
 else {
            if (faces[1] > -1) {
              nmedges.add(ohash);
            }
            faces[1]=i;
          }
        }
      }
      final boolean[] visited=new boolean[faces.length];
      final LinkedList<Integer> queue=new LinkedList<Integer>();
      boolean facesleft=false;
      int starti=0;
      do {
        queue.add(starti);
        int temp;
        while (!queue.isEmpty()) {
          final Integer index=queue.poll();
          final int[] face=faces[index];
          final int fl=face.length;
          visited[index]=true;
          for (int j=0; j < fl; j++) {
            final long ohash=ohash(face[j],face[(j + 1) % fl]);
            final int[] ns=edges.get(ohash);
            if (ns != null) {
              edges.remove(ohash);
              Integer neighbor;
              if (ns[0] == index) {
                neighbor=ns[1];
              }
 else {
                neighbor=ns[0];
              }
              if (neighbor > -1) {
                if (visited[neighbor] == false) {
                  if (!queue.contains(neighbor)) {
                    queue.add(neighbor);
                  }
                  if (consistentOrder(j,(j + 1) % fl,face,faces[neighbor]) == -1) {
                    final int fln=faces[neighbor].length;
                    for (int k=0; k < (fln / 2); k++) {
                      temp=faces[neighbor][k];
                      faces[neighbor][k]=faces[neighbor][fln - k - 1];
                      faces[neighbor][fln - k - 1]=temp;
                    }
                  }
                  visited[neighbor]=true;
                }
              }
            }
          }
        }
        facesleft=false;
        for (; starti < faces.length; starti++) {
          if (!visited[starti]) {
            facesleft=true;
            break;
          }
        }
      }
 while (facesleft);
    }
    final boolean useFaceTextures=(faceTextureIds != null) && (faceTextureIds.length == faces.length);
    for (    final int[] face : faces) {
      final ArrayList<HE_Halfedge> faceEdges=new ArrayList<HE_Halfedge>();
      final HE_Face hef=new HE_Face();
      hef.setTemporaryLabel(id);
      if (useFaceTextures) {
        hef.setTextureId(faceTextureIds[id]);
      }
      id++;
      final int fl=face.length;
      final int[] locface=new int[fl];
      int li=0;
      locface[li++]=face[0];
      for (int i=1; i < (fl - 1); i++) {
        if (uniqueVertices[face[i]] != uniqueVertices[face[i - 1]]) {
          locface[li++]=face[i];
        }
      }
      if ((uniqueVertices[face[fl - 1]] != uniqueVertices[face[fl - 2]]) && (uniqueVertices[face[fl - 1]] != uniqueVertices[face[0]])) {
        locface[li++]=face[fl - 1];
      }
      if (li > 2) {
        for (int i=0; i < li; i++) {
          he=new HE_Halfedge();
          faceEdges.add(he);
          mesh.setFace(he,hef);
          if (hef.getHalfedge() == null) {
            mesh.setHalfedge(hef,he);
          }
          mesh.setVertex(he,uniqueVertices[locface[i]]);
          if (useUVW) {
            if (duplicated[locface[i]]) {
              final HE_TextureCoordinate uvw=uniqueVertices[locface[i]].getVertexUVW();
              if ((uvw.ud() != uvws[locface[i]].xd()) || (uvw.vd() != uvws[locface[i]].yd()) || (uvw.wd() != uvws[locface[i]].zd())) {
                he.setUVW(uvws[locface[i]]);
              }
            }
          }
          mesh.setHalfedge(he.getVertex(),he);
        }
        mesh.add(hef);
        mesh.cycleHalfedges(faceEdges);
        mesh.addHalfedges(faceEdges);
      }
    }
    mesh.pairHalfedges();
    mesh.capHalfedges();
    if (normalcheck) {
      final HE_FaceIterator fitr=mesh.fItr();
      HE_Face f;
      HE_Face left=null;
      WB_Coord fcleft=new WB_Point(Double.MAX_VALUE,Double.MAX_VALUE,Double.MAX_VALUE);
      while (fitr.hasNext()) {
        f=fitr.next();
        if (f.getFaceCenter().xd() < fcleft.xd()) {
          left=f;
          fcleft=left.getFaceCenter();
        }
      }
      final WB_Coord leftn=left.getFaceNormal();
      if (leftn.xd() > 0) {
        mesh.flipAllFaces();
      }
    }
  }
  return mesh;
}

Fixed version (ground truth):
@Override protected HE_Mesh createBase(){
  final HE_Mesh mesh=new HE_Mesh();
  if ((faces != null) && (vertices != null)) {
    if (faces.length == 0) {
      return mesh;
    }
    final boolean useUVW=(uvws != null) && (uvws.length == vertices.length);
    final HE_Vertex[] uniqueVertices=new HE_Vertex[vertices.length];
    final boolean[] duplicated=new boolean[vertices.length];
    if (duplicate) {
      final WB_KDTree<WB_Coord,Integer> kdtree=new WB_KDTree<WB_Coord,Integer>();
      WB_KDEntry<WB_Coord,Integer>[] neighbors;
      HE_Vertex v=new HE_Vertex(vertices[0]);
      if (useUVW) {
        v.setUVW(uvws[0]);
      }
      kdtree.add(v,0);
      uniqueVertices[0]=v;
      duplicated[0]=false;
      mesh.add(v);
      for (int i=1; i < vertices.length; i++) {
        v=new HE_Vertex(vertices[i]);
        if (useUVW) {
          v.setUVW(uvws[i]);
        }
        neighbors=kdtree.getNearestNeighbors(v,1);
        if (neighbors[0].d2 < WB_Epsilon.SQEPSILON) {
          uniqueVertices[i]=uniqueVertices[neighbors[0].value];
          duplicated[i]=true;
        }
 else {
          kdtree.add(v,i);
          uniqueVertices[i]=v;
          mesh.add(uniqueVertices[i]);
          duplicated[i]=false;
        }
      }
    }
 else {
      HE_Vertex v;
      for (int i=0; i < vertices.length; i++) {
        v=new HE_Vertex(vertices[i]);
        if (useUVW) {
          v.setUVW(uvws[i]);
        }
        v.setInternalLabel(i);
        uniqueVertices[i]=v;
        duplicated[i]=false;
        mesh.add(uniqueVertices[i]);
      }
    }
    int id=0;
    HE_Halfedge he;
    final List<Long> nmedges=new ArrayList<Long>();
    if (normalcheck) {
      final FastMap<Long,int[]> edges=new FastMap<Long,int[]>();
      for (int i=0; i < faces.length; i++) {
        final int[] face=faces[i];
        final int fl=face.length;
        for (int j=0; j < fl; j++) {
          final long ohash=ohash(face[j],face[(j + 1) % fl]);
          final int[] faces=edges.get(ohash);
          if (faces == null) {
            edges.put(ohash,new int[]{i,-1});
          }
 else {
            if (faces[1] > -1) {
              nmedges.add(ohash);
            }
            faces[1]=i;
          }
        }
      }
      final boolean[] visited=new boolean[faces.length];
      final LinkedList<Integer> queue=new LinkedList<Integer>();
      boolean facesleft=false;
      int starti=0;
      do {
        queue.add(starti);
        int temp;
        while (!queue.isEmpty()) {
          final Integer index=queue.poll();
          final int[] face=faces[index];
          final int fl=face.length;
          visited[index]=true;
          for (int j=0; j < fl; j++) {
            final long ohash=ohash(face[j],face[(j + 1) % fl]);
            final int[] ns=edges.get(ohash);
            if (ns != null) {
              edges.remove(ohash);
              Integer neighbor;
              if (ns[0] == index) {
                neighbor=ns[1];
              }
 else {
                neighbor=ns[0];
              }
              if (neighbor > -1) {
                if (visited[neighbor] == false) {
                  if (!queue.contains(neighbor)) {
                    queue.add(neighbor);
                  }
                  if (consistentOrder(j,(j + 1) % fl,face,faces[neighbor]) == -1) {
                    final int fln=faces[neighbor].length;
                    for (int k=0; k < (fln / 2); k++) {
                      temp=faces[neighbor][k];
                      faces[neighbor][k]=faces[neighbor][fln - k - 1];
                      faces[neighbor][fln - k - 1]=temp;
                    }
                  }
                  visited[neighbor]=true;
                }
              }
            }
          }
        }
        facesleft=false;
        for (; starti < faces.length; starti++) {
          if (!visited[starti]) {
            facesleft=true;
            break;
          }
        }
      }
 while (facesleft);
    }
    final boolean useFaceTextures=(faceTextureIds != null) && (faceTextureIds.length == faces.length);
    for (    final int[] face : faces) {
      final ArrayList<HE_Halfedge> faceEdges=new ArrayList<HE_Halfedge>();
      final HE_Face hef=new HE_Face();
      hef.setInternalLabel(id);
      if (useFaceTextures) {
        hef.setTextureId(faceTextureIds[id]);
      }
      id++;
      final int fl=face.length;
      final int[] locface=new int[fl];
      int li=0;
      locface[li++]=face[0];
      for (int i=1; i < (fl - 1); i++) {
        if (uniqueVertices[face[i]] != uniqueVertices[face[i - 1]]) {
          locface[li++]=face[i];
        }
      }
      if ((uniqueVertices[face[fl - 1]] != uniqueVertices[face[fl - 2]]) && (uniqueVertices[face[fl - 1]] != uniqueVertices[face[0]])) {
        locface[li++]=face[fl - 1];
      }
      if (li > 2) {
        for (int i=0; i < li; i++) {
          he=new HE_Halfedge();
          faceEdges.add(he);
          mesh.setFace(he,hef);
          if (hef.getHalfedge() == null) {
            mesh.setHalfedge(hef,he);
          }
          mesh.setVertex(he,uniqueVertices[locface[i]]);
          if (useUVW) {
            if (duplicated[locface[i]]) {
              final HE_TextureCoordinate uvw=uniqueVertices[locface[i]].getVertexUVW();
              if ((uvw.ud() != uvws[locface[i]].xd()) || (uvw.vd() != uvws[locface[i]].yd()) || (uvw.wd() != uvws[locface[i]].zd())) {
                he.setUVW(uvws[locface[i]]);
              }
            }
          }
          mesh.setHalfedge(he.getVertex(),he);
        }
        mesh.add(hef);
        mesh.cycleHalfedges(faceEdges);
        mesh.addHalfedges(faceEdges);
      }
    }
    mesh.pairHalfedges();
    mesh.capHalfedges();
    if (normalcheck) {
      final HE_FaceIterator fitr=mesh.fItr();
      HE_Face f;
      HE_Face left=null;
      WB_Coord fcleft=new WB_Point(Double.MAX_VALUE,Double.MAX_VALUE,Double.MAX_VALUE);
      while (fitr.hasNext()) {
        f=fitr.next();
        if (f.getFaceCenter().xd() < fcleft.xd()) {
          left=f;
          fcleft=left.getFaceCenter();
        }
      }
      final WB_Coord leftn=left.getFaceNormal();
      if (leftn.xd() > 0) {
        mesh.flipFaces();
      }
    }
  }
  return mesh;
}