Libraryless. Click here for Pure Java version (11542L/65K).
// both the outer outline and the outline of a hole are called a "trace" // should return outer outline first and then outline of holes // seems to return first point again at the end sometimes srecord noeq RegionBorder_innerPoints(BWImage_FastRegions regions, int iRegion) extends AbstractSteppable { event newTrace(bool isHole); event foundPoint(Pt p); event traceDone; int w, x, y, dir; BWImage_FastRegions.RegionIterator it; byte[] reachedInDirections; // bits 0 to 3 bool tracing; gettable bool tracingHole; // directions (0 = right, 1 = down, 2 = left, 3 = up) static Pt[] directions = { pt(1, 0), pt(0, 1), pt(-1, 0), pt(0, -1) // r, d, l, u }; void init { w = regions.w; reachedInDirections = new byte[w*regions.h]; it = regions.regionIterator(iRegion); } public bool step() { if (reachedInDirections == null) init(); if (tracing) { int pos = y*w+x; if ((reachedInDirections[pos] & (1 << dir)) != 0) { traceDone(); tracing = false; true; } reachedInDirections[pos] |= (byte) 1 << dir; foundPoint(x, y); for (int turn = 3; turn <= 6; turn++) { // try left, straight, right, back int newDir = (dir+turn) & 3; Pt d = directions[newDir]; int x2 = x+d.x, y2 = y+d.y; bool b = regions.inRegion(iRegion, x2, y2); ifdef RegionBorder_innerPoints_debug printVars(+x, +y, +dir, +turn, +newDir, +x2, +y2, +b); endifdef if (b) { x = x2; y = y2; dir = newDir; true; } } true; // i think this ends the trace in next iteration } else { // search for first border pixel if (!it.next()) false; // done x = it.x(); y = it.y(); if (reachedInDirections[y*w+x] != 0) true; // seen pixel before // if pixel above is empty, walk to the right if (!regions.inRegion(iRegion, x, y-1)) ret true with startTrace(0); // if pixel on the left is empty, walk upwards if (!regions.inRegion(iRegion, x-1, y)) ret true with startTrace(3); // if pixel on the right is empty, walk downwards if (!regions.inRegion(iRegion, x+1, y)) ret true with startTrace(1); // if pixel below is empty, walk left if (!regions.inRegion(iRegion, x, y+1)) ret true with startTrace(2); // not a border pixel, continue search true; } } void startTrace(int dir) { this.dir = dir; // mark point reached from all directions in next step reachedInDirections[y*w+x] = (byte) (15 & ~(1 << dir)); set tracing; newTrace(tracingHole); set tracingHole; } void foundPoint(int x, int y) { foundPoint(pt(x, y)); } // get all points as list simplyCached L<Pt> allPoints() { new PtBuffer l; onFoundPoint(p -> l.add(p)); run(); ret l; } // get outline as OnePath simplyCached OnePath onePath() { ret new OnePath(allPoints(), true); } // for debugging void runAndPrint { onNewTrace(hole -> print(!hole ? "new outline" : "new hole")); onTraceDone(-> print("traceDone")); onFoundPoint(p -> print("foundPoint " + p)); stepMaxWithStats(this, 10000); } }
Began life as a copy of #1033938
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1033941 |
Snippet name: | RegionBorder_innerPoints - use RegionBorder_innerPoints_v2 instead |
Eternal ID of this version: | #1033941/33 |
Text MD5: | b5f4842adf5875eddd5a31df2f0e4460 |
Transpilation MD5: | 3b36e55e10ea060a05a582da80c83800 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-03-14 17:15:46 |
Source code size: | 3452 bytes / 121 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 225 / 442 |
Version history: | 32 change(s) |
Referenced in: | #1034080 - RegionBorder_innerPoints - find region outline [backup] #1034167 - Standard Classes + Interfaces (LIVE, continuation of #1003674) #1034656 - RegionBorder_innerPoints_v2 - find region outline [works on IImageRegion] |