Libraryless. Click here for Pure Java version (10632L/59K).
1 | // both the outer outline and the outline of a hole are called a "trace" |
2 | // should return outer outline first and then outline of holes |
3 | |
4 | // seems to return first point again at the end sometimes |
5 | |
6 | srecord noeq RegionBorder_innerPoints_v2(IImageRegion region) extends AbstractBorderTracer { |
7 | Rect bounds; // region bounds |
8 | int x, y, dir; |
9 | Iterator<Pt> it; |
10 | byte[] reachedInDirections; // bits 0 to 3 |
11 | bool tracing; |
12 | int nTrace; // 1 for outline, 2+ for hole |
13 | |
14 | // directions (0 = right, 1 = down, 2 = left, 3 = up) |
15 | static Pt[] directions = { |
16 | pt(1, 0), pt(0, 1), pt(-1, 0), pt(0, -1) // r, d, l, u |
17 | }; |
18 | |
19 | void init { |
20 | bounds = region.bounds(); |
21 | reachedInDirections = new byte[area(bounds)]; |
22 | it = region.pixelIterator(); |
23 | } |
24 | |
25 | public bool step() { |
26 | if (reachedInDirections == null) init(); |
27 | |
28 | if (tracing) |
29 | ret walkOnePixel(); |
30 | else |
31 | ret findFirstPixel(); |
32 | } |
33 | |
34 | bool walkOnePixel() { |
35 | int posInBounds = (y-bounds.y)*bounds.w+x-bounds.x; |
36 | if ((reachedInDirections[posInBounds] & (1 << dir)) != 0) { |
37 | traceDone(); |
38 | tracing = false; |
39 | ret includeHoles; |
40 | } |
41 | |
42 | reachedInDirections[posInBounds] |= (byte) 1 << dir; |
43 | foundPoint(x, y); |
44 | |
45 | for (int turn = 3; turn <= 6; turn++) { // try left, straight, right, back |
46 | int newDir = (dir+turn) & 3; |
47 | Pt d = directions[newDir]; |
48 | int x2 = x+d.x, y2 = y+d.y; |
49 | bool b = region.contains(x2, y2); |
50 | ifdef RegionBorder_innerPoints_debug |
51 | printVars(+x, +y, +dir, +turn, +newDir, +x2, +y2, +b); |
52 | endifdef |
53 | if (b) { |
54 | x = x2; y = y2; dir = newDir; |
55 | true; |
56 | } |
57 | } |
58 | |
59 | true; // no black pixels found in any direction - region must be a single pixel |
60 | } |
61 | |
62 | bool findFirstPixel() { |
63 | // search for first border pixel |
64 | |
65 | if (!it.hasNext()) false; // done |
66 | Pt p = it.next(); |
67 | x = p.x; y = p.y; |
68 | |
69 | int posInBounds = (y-bounds.y)*bounds.w+x-bounds.x; |
70 | if (reachedInDirections[posInBounds] != 0) true; // seen pixel before |
71 | |
72 | // if pixel above is empty, walk to the right |
73 | if (!region.contains(x, y-1)) |
74 | ret true with startTrace(0); |
75 | |
76 | // if pixel on the left is empty, walk upwards |
77 | if (!region.contains(x-1, y)) |
78 | ret true with startTrace(3); |
79 | |
80 | // if pixel on the right is empty, walk downwards |
81 | if (!region.contains(x+1, y)) |
82 | ret true with startTrace(1); |
83 | |
84 | // if pixel below is empty, walk left |
85 | if (!region.contains(x, y+1)) |
86 | ret true with startTrace(2); |
87 | |
88 | // not a border pixel, continue search |
89 | true; |
90 | } |
91 | |
92 | void startTrace(int dir) { |
93 | this.dir = dir; |
94 | |
95 | // mark point reached from all directions in next step |
96 | int posInBounds = (y-bounds.y)*bounds.w+x-bounds.x; |
97 | reachedInDirections[posInBounds] = (byte) (15 & ~(1 << dir)); |
98 | |
99 | set tracing; |
100 | newTrace(++nTrace > 1); |
101 | } |
102 | |
103 | void foundPoint(int x, int y) { foundPoint(pt(x, y)); } |
104 | |
105 | // get all points as list |
106 | |
107 | simplyCached L<Pt> allPoints() { |
108 | new PtBuffer l; |
109 | onFoundPoint(p -> l.add(p)); |
110 | run(); |
111 | ret l; |
112 | } |
113 | |
114 | // get outline as OnePath |
115 | |
116 | simplyCached OnePath onePath() { |
117 | includeHoles(false); |
118 | ret new OnePath(allPoints(), true); |
119 | } |
120 | |
121 | // or as OnePathWithOrigin |
122 | |
123 | simplyCached OnePathWithOrigin onePathWithOrigin() { |
124 | includeHoles(false); |
125 | ret new OnePathWithOrigin(allPoints(), true); |
126 | } |
127 | |
128 | // for debugging |
129 | void runAndPrint { |
130 | onNewTrace(hole -> print(!hole ? "new outline" : "new hole")); |
131 | onTraceDone(-> print("traceDone")); |
132 | onFoundPoint(p -> print("foundPoint " + p)); |
133 | stepMaxWithStats(this, 10000); |
134 | } |
135 | |
136 | bool tracingHole() { ret nTrace > 1; } |
137 | } |
Began life as a copy of #1033941
download show line numbers debug dex old transpilations
Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1034656 |
Snippet name: | RegionBorder_innerPoints_v2 - find region outline [works on IImageRegion] |
Eternal ID of this version: | #1034656/14 |
Text MD5: | 31258f0a35e6cb784eeb3137b8e738cf |
Transpilation MD5: | 2f20de586ec79fb890ef3bbe315c8f6c |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-04-28 16:51:51 |
Source code size: | 3805 bytes / 137 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 201 / 401 |
Version history: | 13 change(s) |
Referenced in: | [show references] |