Package: com.multiversesocial.hyperview
Class: com.multiversesocial.hyperview.OutlineTracer
Source: src/com/multiversesocial/hyperview/OutlineTracer.java
The outline polygon of the opaque part of an image (V3.2). Gob.computeImageOutline() uses it when the Gob has the GOB_COLLISION_OUTLINE flag; nothing else calls it, so Gobs without the flag pay nothing. It needs no HyperView and no display: it works on an int ARGB pixel array.
(argb >>> 24) >= alphaMin (default 128).components and componentPixels).fallbackBox = true. No opaque pixel: null.Typical results (OutlineTest): circle 64 px: 180 contour pixels, 16 points; circle 200 px: 564 contour pixels, 29 points; L shape 60x60: 6 points; plus sign with a hole: 12 points; opaque 40x30 box: 4 points (0,0)-(39,29). Concave corners of the contour may be cut by one pixel (8-connected tracing).
Extends: none
Implements: none
Used by: Gob.computeImageOutline(), CollisionManager (through Gob.ensureImageOutline())
public class OutlineTracer
| Declaration | Value / initializer | Description | Source |
|---|---|---|---|
public static final int DEFAULT_ALPHA | = 128 | Opaque if alpha is at least this (0..255). | |
public static final double DEFAULT_EPSILON | = 1.0 | Douglas-Peucker tolerance in pixels. | |
public static final int DEFAULT_MAX_POINTS | = 48 | Upper limit of polygon points (the tolerance grows until it fits). | |
static final int[] DX | = {-1,-1, 0, 1, 1, 1, 0,-1} | clockwise from West (image coordinates, y down) | |
static final int[] DY | = { 0,-1,-1,-1, 0, 1, 1, 1} | clockwise from West (image coordinates, y down) | |
public static volatile int traceCount | = 0 | Number of trace() calls made in this JVM (a test reads it to prove nothing is traced without the flag). |
| Signature | Description | Source |
|---|
| Signature | Description | Source |
|---|---|---|
static void dp(int[] px, int[] py, int m, int lo, int hi, double eps, boolean[] keep) | Douglas-Peucker on the open run lo..hi (indices modulo m; hi may be m = point 0). | |
static void simplify(int[] px, int[] py, int m, double eps, boolean[] keep) | Closed Douglas-Peucker over px[0..m-1] (a ring). keep[i] = point i stays. Split the ring at point 0 and the point farthest from it, then simplify both halves. | |
public static Result trace(int[] argb, int w, int h) | trace() with the default tolerance, alpha and point limit. | |
public static Result trace(int[] argb, int w, int h, int alphaMin, double epsilon, int maxPoints) | Outer contour polygon of the opaque pixels of the w x h ARGB image argb[] (row after row). Returns null if no pixel has alpha >= alphaMin. |
public static final class Result — what trace() returns.
| Field | Description |
|---|---|
public final Polygon polygon | The simplified outer contour (3 or more points), image pixel coordinates. |
public final int opaque | Opaque pixels in the whole image. |
public final int components | 8-connected opaque pieces. |
public final int componentPixels | Pixels of the piece which was traced. |
public final int contourPixels | Boundary points traced before simplification. |
public final double epsilon | Tolerance finally used (grows above the start value when maxPoints forced it). |
public final boolean fallbackBox | true: the piece was too thin, polygon = bounding box of the piece. |
Gob g = new Gob(view,pixels,w,h,GOB_ON_DISPLAY); g.setImageOutlineEnabled(true); // GOB_COLLISION_OUTLINE: traced at the first collision pass view.addGob(g); // on its own (no display needed): OutlineTracer.Result r = OutlineTracer.trace(argb,w,h); // alpha >= 128, epsilon 1.0, max 48 points OutlineTracer.Result s = OutlineTracer.trace(argb,w,h,0x20,0.5,64); // own threshold / tolerance / limit if(r != null) System.out.println(r.polygon.npoints + " points from " + r.contourPixels + " contour pixels");