public ImagePolygon(ImageEdge e) { orderedEdges = new ArrayList(); orderedEdges.Add(e); }
public bool AddEdge(ImageEdge e) { // Check if the edge connects the previous edge ImagePoint lastPoint = ((ImageEdge)orderedEdges[orderedEdges.Count-1]).v2; // Make sure the points are ordered correctly if (e.v1 == lastPoint) { orderedEdges.Add(e); return true; } else if (e.v2 == lastPoint) { e.Reverse(); orderedEdges.Add(e); return true; } // Otherwise, Check if it shares with the first edge ImagePoint firstPoint = ((ImageEdge)orderedEdges[0]).v1; if (e.v2 == firstPoint) { orderedEdges.Insert(0,e); return true; } else if (e.v1 == firstPoint) { e.Reverse(); orderedEdges.Insert(0, e); return true; } return false; }
public ImageProcessor(Color[] pixels, int imageWidth, int imageHeight, float threshold) { edges = new ArrayList(); points = new ArrayList(); polygons = new ArrayList(); // -- Iterate through the pixels for (int x = 0; x < imageWidth; x++) { float uvX = x + 0.5f; for (int y = 0; y < imageHeight; y++) { float uvY = y+0.5f; // Exit early if the current pixel is not opaque if (pixels[x + (imageWidth * y)].a < threshold) continue; // Get the surrounding pixels' alpha values using pixel space (origin is bottom left) float UpAlpha = 0.0f; float DownAlpha = 0.0f; float RightAlpha = 0.0f; float LeftAlpha = 0.0f; float UpRightAlpha = 0.0f; float UpLeftAlpha = 0.0f; float DownRightAlpha = 0.0f; if (x>0){ LeftAlpha = pixels[x - 1 + (imageWidth * y)].a; if (y<imageHeight-1){ UpLeftAlpha = pixels[x - 1 + (imageWidth * (y + 1))].a; } } if (x<imageWidth-1){ if (y>0){ DownRightAlpha = pixels[x + 1 + (imageWidth * (y - 1))].a; } RightAlpha = pixels[x + 1 + (imageWidth * y)].a; if (y<imageHeight-1){ UpRightAlpha = pixels[x + 1 + (imageWidth * (y + 1))].a; } } if (y>0){ DownAlpha = pixels[x + (imageWidth * (y-1))].a; } if (y<imageHeight-1){ UpAlpha = pixels[x + (imageWidth * (y+1))].a; } //========================================= // Try to add an edge between the current pixel and the one directly above it // Above if (UpAlpha >= threshold) { if (UpRightAlpha < threshold && RightAlpha < threshold) { if (UpLeftAlpha >= threshold || LeftAlpha >= threshold) { ImageEdge e = new ImageEdge(GetPoint(x,y,uvX/imageWidth,uvY/imageHeight), GetPoint(x,y+1,uvX/imageWidth,(uvY+1)/imageHeight)); edges.Add(e); } } else if ( UpLeftAlpha < threshold && LeftAlpha < threshold) { if (UpRightAlpha >= threshold || RightAlpha >= threshold) { ImageEdge e = new ImageEdge(GetPoint(x,y,uvX/imageWidth,uvY/imageHeight), GetPoint(x,y+1,uvX/imageWidth,(uvY+1)/imageHeight)); edges.Add(e); } } } // Try to add an edge between the current pixel and one diagonally above it // Above and right if (UpRightAlpha >= threshold) { if (UpAlpha < threshold && RightAlpha >= threshold) { // . // *. ImageEdge e = new ImageEdge(GetPoint(x,y,uvX/imageWidth,uvY/imageHeight), GetPoint(x+1,y+1,(uvX+1)/imageWidth,(uvY+1)/imageHeight)); edges.Add(e); } else if (UpAlpha >= threshold && RightAlpha < threshold) { // .. // * ImageEdge e = new ImageEdge(GetPoint(x,y,uvX/imageWidth,uvY/imageHeight), GetPoint(x+1,y+1,(uvX+1)/imageWidth,(uvY+1)/imageHeight)); edges.Add(e); } } // Try to add an edge between the current pixel and the one directly right of it // Right if (RightAlpha >= threshold) { if (UpAlpha < threshold && UpRightAlpha < threshold) { if (DownAlpha >= threshold || DownRightAlpha >= threshold) { // *. or *. *. // . . .. ImageEdge e = new ImageEdge(GetPoint(x,y,uvX/imageWidth,uvY/imageHeight), GetPoint(x+1,y,(uvX+1)/imageWidth,uvY/imageHeight)); edges.Add(e); } } else if ( DownAlpha < threshold && DownRightAlpha < threshold) { if (UpAlpha >= threshold || UpRightAlpha >= threshold) { // add the horizontal edge ImageEdge e = new ImageEdge(GetPoint(x,y,uvX/imageWidth,uvY/imageHeight), GetPoint(x+1,y,(uvX+1)/imageWidth,uvY/imageHeight)); edges.Add(e); } } } //Down Right if (DownRightAlpha >= threshold) { if ( RightAlpha < threshold && DownAlpha >= threshold) { // * // .. ImageEdge e = new ImageEdge(GetPoint(x,y,uvX/imageWidth,uvY/imageHeight), GetPoint(x+1,y-1,(uvX+1)/imageWidth,(uvY-1)/imageHeight)); edges.Add(e); } else if (RightAlpha >= threshold && DownAlpha < threshold) { // *. // . ImageEdge e = new ImageEdge(GetPoint(x,y,uvX/imageWidth,uvY/imageHeight), GetPoint(x+1,y-1,(uvX+1)/imageWidth,(uvY-1)/imageHeight)); edges.Add(e); } } } } CreateFaces(); }