Package: com.multiversesocial.hyperview
Class: com.multiversesocial.hyperview.OutlineTracer
Source: src/com/multiversesocial/hyperview/OutlineTracer.java

OutlineTracer

HyperView V3.2 — class
version V3.2 class

Summary

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.

  1. Mask: a pixel is opaque when (argb >>> 24) >= alphaMin (default 128).
  2. Components: iterative 8-connected flood fill; only the component with the most pixels is traced (specks and a second blob are ignored; the result reports components and componentPixels).
  3. Contour: Moore-neighbour boundary tracing (clockwise, Jacob’s stopping criterion) of the OUTER boundary. The points are pixel centres, so a fully opaque w x h image traces 0,0 .. w-1,h-1, exactly the default bounding box. Holes are not traced; concave shapes are fine.
  4. Simplify: closed Douglas-Peucker, tolerance epsilon (default 1.0 px). If more than maxPoints (default 48) remain, epsilon is multiplied by 1.5 until it fits.
  5. Too thin (single pixel, 1 px line: fewer than 3 points): the 4 corners of the piece’s bounding box, 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).

Source file: OutlineTracer.java. Tests: test/.../OutlineTest.java (part 1 is the tracer alone).

Hierarchy

Extends: none

Implements: none

Used by: Gob.computeImageOutline(), CollisionManager (through Gob.ensureImageOutline())

Referenced HyperView classes

Media files used

Declaration & members

public class OutlineTracer

Declared in src/com/multiversesocial/hyperview/OutlineTracer.java, lines 1–333. 6 field(s), 0 constructor(s), 4 method(s). Every member has an anchor you can link to (e.g. OutlineTracer.html#<id>).

Fields (6)

DeclarationValue / initializerDescriptionSource
public static final int DEFAULT_ALPHA= 128 (constant)Opaque if alpha is at least this (0..255).line 33
public static final double DEFAULT_EPSILON= 1.0 (constant)Douglas-Peucker tolerance in pixels.line 35
public static final int DEFAULT_MAX_POINTS= 48 (constant)Upper limit of polygon points (the tolerance grows until it fits).line 37
static final int[] DX= {-1,-1, 0, 1, 1, 1, 0,-1} (constant)clockwise from West (image coordinates, y down)line 59
static final int[] DY= { 0,-1,-1,-1, 0, 1, 1, 1} (constant)clockwise from West (image coordinates, y down)line 60
public static volatile int traceCount= 0Number of trace() calls made in this JVM (a test reads it to prove nothing is traced without the flag).line 40

Constructors (0)

SignatureDescriptionSource

Methods (4)

SignatureDescriptionSource
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).line 283
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.line 259
public static Result trace(int[] argb, int w, int h)trace() with the default tolerance, alpha and point limit.line 63
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.line 72

OutlineTracer.Result

public static final class Result — what trace() returns.

FieldDescription
public final Polygon polygonThe simplified outer contour (3 or more points), image pixel coordinates.
public final int opaqueOpaque pixels in the whole image.
public final int components8-connected opaque pieces.
public final int componentPixelsPixels of the piece which was traced.
public final int contourPixelsBoundary points traced before simplification.
public final double epsilonTolerance finally used (grows above the start value when maxPoints forced it).
public final boolean fallbackBoxtrue: the piece was too thin, polygon = bounding box of the piece.

Examples

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");
Hits--