@Test public void testFindProblemSpots() throws IOException { BufferedImage imgIn = ImageIO.read(new File("C:\\Users\\flux\\Dropbox\\java\\VoxelShop\\Test Files\\grid2poly\\file1.png")); boolean[][] data = new boolean[imgIn.getWidth()][imgIn.getHeight()]; for (int x = 0; x < imgIn.getWidth(); x++) { for (int y = 0; y < imgIn.getHeight(); y++) { data[x][y] = imgIn.getRGB(x,y) != -1; } } short[][][] polys = Grid2PolyHelper.convert(data); int zoom = 40; BufferedImage img = new BufferedImage(zoom*imgIn.getWidth() + zoom, zoom*imgIn.getHeight() + zoom, BufferedImage.TYPE_INT_RGB); Graphics2D gr = (Graphics2D) img.getGraphics(); gr.setColor(Color.WHITE); AffineTransform at = new AffineTransform(); at.translate(zoom/2, zoom/2); gr.setTransform(at); gr.fillRect(-zoom/2, -zoom/2, img.getWidth(), img.getHeight()); gr.setFont(gr.getFont().deriveFont(Font.BOLD,15f)); gr.drawImage(imgIn,0,0,img.getWidth()-zoom,img.getHeight()-zoom, null); gr.setRenderingHint( RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); gr.setRenderingHint( RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); gr.setRenderingHint( RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); for (short[][] poly : polys) { for (int n = 1; n < poly.length; n++) { for (int i = 0, len = poly[n].length - 2; i < len; i+=2) { gr.setColor(Color.BLUE); gr.drawLine(poly[n][i] * zoom, poly[n][i+1] * zoom, poly[n][i+2] * zoom, poly[n][i+3] * zoom); } } for (int i = 0; i < poly[0].length - 2; i+=2) { gr.setColor(Color.RED); gr.drawLine(poly[0][i] * zoom, poly[0][i+1] * zoom, poly[0][i+2] * zoom, poly[0][i+3] * zoom); } } for (short[][] poly : polys) { for (int n = 1; n < poly.length; n++) { for (int i = 0, len = poly[n].length - 2; i < len; i+=2) { gr.setColor(Color.BLUE); gr.drawString(String.valueOf(i/2+1), poly[n][i] * zoom - 10, poly[n][i+1] * zoom + 5); } } for (int i = 0; i < poly[0].length - 2; i+=2) { gr.setColor(Color.RED); gr.drawString(String.valueOf(i/2+1), poly[0][i] * zoom + 5, poly[0][i+1] * zoom + 5); } } for (short[][] poly : polys) { for (short[] outline : poly) { for (int i = 0, len = outline.length - 2; i < len; i += 2) { if (outline[i + 2] == outline[i]) { int step = outline[i + 3] > outline[i + 1] ? 1 : -1; for (int y = outline[i + 1] + step; y != outline[i + 3]; y += step) { int x = outline[i] + (step == 1 ? -1 : 0); gr.setColor(Color.ORANGE); gr.setStroke(new BasicStroke(5f)); gr.drawLine(outline[i] * zoom, y * zoom, outline[i] * zoom - step * 10, y * zoom); gr.setColor(Color.BLACK); gr.setStroke(new BasicStroke(1f)); gr.drawRect((x) * zoom, (y-1) * zoom, zoom, 2*zoom); } } else { int step = outline[i + 2] > outline[i] ? 1 : -1; for (int x = outline[i] + step; x != outline[i + 2]; x += step) { int y = outline[i + 1] + (step == -1 ? -1 : 0); gr.setColor(Color.ORANGE); gr.setStroke(new BasicStroke(5f)); gr.drawLine(x * zoom, outline[i + 1] * zoom, x * zoom, outline[i + 1] * zoom + step * 10); gr.setColor(Color.BLACK); gr.setStroke(new BasicStroke(1f)); gr.drawRect((x - 1) * zoom, (y) * zoom, 2 * zoom, zoom); } } } } } gr.dispose(); ImageIO.write(img, "png", new File("C:\\Users\\flux\\Dropbox\\java\\VoxelShop\\Test Files\\grid2poly\\file1_out.png")); }
public static short[][][] convert(boolean[][] data) { TIntObjectHashMap<ArrayList<short[]>> result = new TIntObjectHashMap<ArrayList<short[]>>(); ArrayList<short[]> vertEdges = new ArrayList<short[]>(); TIntObjectHashMap<short[]> edges = new TIntObjectHashMap<short[]>(); extractEdges(data, vertEdges, edges); TShortArrayList outlineValues = new TShortArrayList(); int id; short[] edgeR; short[] polyIds = new short[data[0].length]; short polyId = 0; for (short[] edge : vertEdges) { if (edge[5] == -1) { if (edge[4] == 1) { edgeR = edge; outlineValues.add(edgeR[0]); outlineValues.add(edgeR[1]); do { outlineValues.add(edgeR[2]); outlineValues.add(edgeR[3]); edgeR[5] = polyId; id = IntegerTools.makeInt(edgeR[2], edgeR[3]) * (edgeR[4] == 1 ? 1 : -1); edgeR = edges.get(id); if (edgeR == null) edgeR = edges.get(-id); } while (edgeR != edge); short[] polyArray = new short[outlineValues.size()]; outlineValues.toArray(polyArray); outlineValues.clear(); ArrayList<short[]> poly = new ArrayList<short[]>(); poly.add(polyArray); result.put(polyId, poly); polyId++; } else { edge[5] = polyIds[edge[1]]; edgeR = edge; outlineValues.add(edgeR[0]); outlineValues.add(edgeR[1]); do { outlineValues.add(edgeR[2]); outlineValues.add(edgeR[3]); edgeR[5] = edge[5]; id = IntegerTools.makeInt(edgeR[2], edgeR[3]) * (edgeR[4] == 1 ? 1 : -1); edgeR = edges.get(id); if (edgeR == null) edgeR = edges.get(-id); } while (edgeR != edge); short[] polyArray = new short[outlineValues.size()]; outlineValues.toArray(polyArray); outlineValues.clear(); ArrayList<short[]> poly = result.get(edge[5]); poly.add(polyArray); } } if (edge[4] == 1) { for (int y = edge[1]; y < edge[3]; y++) { polyIds[y] = edge[5]; } } else { for (int y = edge[3]; y < edge[1]; y++) { polyIds[y] = 0; } } } short[][][] resultArray = new short[result.size()][][]; for (int i = 0; i < result.size(); i++) { ArrayList<short[]> list = result.get(i); resultArray[i] = new short[list.size()][]; list.toArray(resultArray[i]); } return resultArray; }
Grid2PolyHelper { public static short[][][] convert(boolean[][] data) { TIntObjectHashMap<ArrayList<short[]>> result = new TIntObjectHashMap<ArrayList<short[]>>(); ArrayList<short[]> vertEdges = new ArrayList<short[]>(); TIntObjectHashMap<short[]> edges = new TIntObjectHashMap<short[]>(); extractEdges(data, vertEdges, edges); TShortArrayList outlineValues = new TShortArrayList(); int id; short[] edgeR; short[] polyIds = new short[data[0].length]; short polyId = 0; for (short[] edge : vertEdges) { if (edge[5] == -1) { if (edge[4] == 1) { edgeR = edge; outlineValues.add(edgeR[0]); outlineValues.add(edgeR[1]); do { outlineValues.add(edgeR[2]); outlineValues.add(edgeR[3]); edgeR[5] = polyId; id = IntegerTools.makeInt(edgeR[2], edgeR[3]) * (edgeR[4] == 1 ? 1 : -1); edgeR = edges.get(id); if (edgeR == null) edgeR = edges.get(-id); } while (edgeR != edge); short[] polyArray = new short[outlineValues.size()]; outlineValues.toArray(polyArray); outlineValues.clear(); ArrayList<short[]> poly = new ArrayList<short[]>(); poly.add(polyArray); result.put(polyId, poly); polyId++; } else { edge[5] = polyIds[edge[1]]; edgeR = edge; outlineValues.add(edgeR[0]); outlineValues.add(edgeR[1]); do { outlineValues.add(edgeR[2]); outlineValues.add(edgeR[3]); edgeR[5] = edge[5]; id = IntegerTools.makeInt(edgeR[2], edgeR[3]) * (edgeR[4] == 1 ? 1 : -1); edgeR = edges.get(id); if (edgeR == null) edgeR = edges.get(-id); } while (edgeR != edge); short[] polyArray = new short[outlineValues.size()]; outlineValues.toArray(polyArray); outlineValues.clear(); ArrayList<short[]> poly = result.get(edge[5]); poly.add(polyArray); } } if (edge[4] == 1) { for (int y = edge[1]; y < edge[3]; y++) { polyIds[y] = edge[5]; } } else { for (int y = edge[3]; y < edge[1]; y++) { polyIds[y] = 0; } } } short[][][] resultArray = new short[result.size()][][]; for (int i = 0; i < result.size(); i++) { ArrayList<short[]> list = result.get(i); resultArray[i] = new short[list.size()][]; list.toArray(resultArray[i]); } return resultArray; } }
Grid2PolyHelper { public static short[][][] convert(boolean[][] data) { TIntObjectHashMap<ArrayList<short[]>> result = new TIntObjectHashMap<ArrayList<short[]>>(); ArrayList<short[]> vertEdges = new ArrayList<short[]>(); TIntObjectHashMap<short[]> edges = new TIntObjectHashMap<short[]>(); extractEdges(data, vertEdges, edges); TShortArrayList outlineValues = new TShortArrayList(); int id; short[] edgeR; short[] polyIds = new short[data[0].length]; short polyId = 0; for (short[] edge : vertEdges) { if (edge[5] == -1) { if (edge[4] == 1) { edgeR = edge; outlineValues.add(edgeR[0]); outlineValues.add(edgeR[1]); do { outlineValues.add(edgeR[2]); outlineValues.add(edgeR[3]); edgeR[5] = polyId; id = IntegerTools.makeInt(edgeR[2], edgeR[3]) * (edgeR[4] == 1 ? 1 : -1); edgeR = edges.get(id); if (edgeR == null) edgeR = edges.get(-id); } while (edgeR != edge); short[] polyArray = new short[outlineValues.size()]; outlineValues.toArray(polyArray); outlineValues.clear(); ArrayList<short[]> poly = new ArrayList<short[]>(); poly.add(polyArray); result.put(polyId, poly); polyId++; } else { edge[5] = polyIds[edge[1]]; edgeR = edge; outlineValues.add(edgeR[0]); outlineValues.add(edgeR[1]); do { outlineValues.add(edgeR[2]); outlineValues.add(edgeR[3]); edgeR[5] = edge[5]; id = IntegerTools.makeInt(edgeR[2], edgeR[3]) * (edgeR[4] == 1 ? 1 : -1); edgeR = edges.get(id); if (edgeR == null) edgeR = edges.get(-id); } while (edgeR != edge); short[] polyArray = new short[outlineValues.size()]; outlineValues.toArray(polyArray); outlineValues.clear(); ArrayList<short[]> poly = result.get(edge[5]); poly.add(polyArray); } } if (edge[4] == 1) { for (int y = edge[1]; y < edge[3]; y++) { polyIds[y] = edge[5]; } } else { for (int y = edge[3]; y < edge[1]; y++) { polyIds[y] = 0; } } } short[][][] resultArray = new short[result.size()][][]; for (int i = 0; i < result.size(); i++) { ArrayList<short[]> list = result.get(i); resultArray[i] = new short[list.size()][]; list.toArray(resultArray[i]); } return resultArray; }  }
Grid2PolyHelper { public static short[][][] convert(boolean[][] data) { TIntObjectHashMap<ArrayList<short[]>> result = new TIntObjectHashMap<ArrayList<short[]>>(); ArrayList<short[]> vertEdges = new ArrayList<short[]>(); TIntObjectHashMap<short[]> edges = new TIntObjectHashMap<short[]>(); extractEdges(data, vertEdges, edges); TShortArrayList outlineValues = new TShortArrayList(); int id; short[] edgeR; short[] polyIds = new short[data[0].length]; short polyId = 0; for (short[] edge : vertEdges) { if (edge[5] == -1) { if (edge[4] == 1) { edgeR = edge; outlineValues.add(edgeR[0]); outlineValues.add(edgeR[1]); do { outlineValues.add(edgeR[2]); outlineValues.add(edgeR[3]); edgeR[5] = polyId; id = IntegerTools.makeInt(edgeR[2], edgeR[3]) * (edgeR[4] == 1 ? 1 : -1); edgeR = edges.get(id); if (edgeR == null) edgeR = edges.get(-id); } while (edgeR != edge); short[] polyArray = new short[outlineValues.size()]; outlineValues.toArray(polyArray); outlineValues.clear(); ArrayList<short[]> poly = new ArrayList<short[]>(); poly.add(polyArray); result.put(polyId, poly); polyId++; } else { edge[5] = polyIds[edge[1]]; edgeR = edge; outlineValues.add(edgeR[0]); outlineValues.add(edgeR[1]); do { outlineValues.add(edgeR[2]); outlineValues.add(edgeR[3]); edgeR[5] = edge[5]; id = IntegerTools.makeInt(edgeR[2], edgeR[3]) * (edgeR[4] == 1 ? 1 : -1); edgeR = edges.get(id); if (edgeR == null) edgeR = edges.get(-id); } while (edgeR != edge); short[] polyArray = new short[outlineValues.size()]; outlineValues.toArray(polyArray); outlineValues.clear(); ArrayList<short[]> poly = result.get(edge[5]); poly.add(polyArray); } } if (edge[4] == 1) { for (int y = edge[1]; y < edge[3]; y++) { polyIds[y] = edge[5]; } } else { for (int y = edge[3]; y < edge[1]; y++) { polyIds[y] = 0; } } } short[][][] resultArray = new short[result.size()][][]; for (int i = 0; i < result.size(); i++) { ArrayList<short[]> list = result.get(i); resultArray[i] = new short[list.size()][]; list.toArray(resultArray[i]); } return resultArray; }  static short[][][] convert(boolean[][] data); }
Grid2PolyHelper { public static short[][][] convert(boolean[][] data) { TIntObjectHashMap<ArrayList<short[]>> result = new TIntObjectHashMap<ArrayList<short[]>>(); ArrayList<short[]> vertEdges = new ArrayList<short[]>(); TIntObjectHashMap<short[]> edges = new TIntObjectHashMap<short[]>(); extractEdges(data, vertEdges, edges); TShortArrayList outlineValues = new TShortArrayList(); int id; short[] edgeR; short[] polyIds = new short[data[0].length]; short polyId = 0; for (short[] edge : vertEdges) { if (edge[5] == -1) { if (edge[4] == 1) { edgeR = edge; outlineValues.add(edgeR[0]); outlineValues.add(edgeR[1]); do { outlineValues.add(edgeR[2]); outlineValues.add(edgeR[3]); edgeR[5] = polyId; id = IntegerTools.makeInt(edgeR[2], edgeR[3]) * (edgeR[4] == 1 ? 1 : -1); edgeR = edges.get(id); if (edgeR == null) edgeR = edges.get(-id); } while (edgeR != edge); short[] polyArray = new short[outlineValues.size()]; outlineValues.toArray(polyArray); outlineValues.clear(); ArrayList<short[]> poly = new ArrayList<short[]>(); poly.add(polyArray); result.put(polyId, poly); polyId++; } else { edge[5] = polyIds[edge[1]]; edgeR = edge; outlineValues.add(edgeR[0]); outlineValues.add(edgeR[1]); do { outlineValues.add(edgeR[2]); outlineValues.add(edgeR[3]); edgeR[5] = edge[5]; id = IntegerTools.makeInt(edgeR[2], edgeR[3]) * (edgeR[4] == 1 ? 1 : -1); edgeR = edges.get(id); if (edgeR == null) edgeR = edges.get(-id); } while (edgeR != edge); short[] polyArray = new short[outlineValues.size()]; outlineValues.toArray(polyArray); outlineValues.clear(); ArrayList<short[]> poly = result.get(edge[5]); poly.add(polyArray); } } if (edge[4] == 1) { for (int y = edge[1]; y < edge[3]; y++) { polyIds[y] = edge[5]; } } else { for (int y = edge[3]; y < edge[1]; y++) { polyIds[y] = 0; } } } short[][][] resultArray = new short[result.size()][][]; for (int i = 0; i < result.size(); i++) { ArrayList<short[]> list = result.get(i); resultArray[i] = new short[list.size()][]; list.toArray(resultArray[i]); } return resultArray; }  static short[][][] convert(boolean[][] data);  }