Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

106
LINES

< > BotCompany Repo | #1034965 // G22RegionThinner - thin a region [abandoned, replaced by G22RegionThinner_v2]

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (8993L) is out of date.

1  
srecord noeq G22RegionThinner(IImageRegion<?> originalRegion) is Steppable {
2  
  Rect bounds;
3  
  
4  
  // true = part of region
5  
  // index is in bounds coordinates
6  
  bool[] pixels;
7  
  
8  
  gettable IImageRegion region = new {
9  
    public Rect bounds() { ret bounds; }
10  
  
11  
    public bool contains(int x, int y) {
12  
      ret containsPt(bounds, x, y) && pixels[idx(x, y)];
13  
    }
14  
    
15  
    public ItIt<Pt> pixelIterator() {
16  
      ret iff_null(new IF0<Pt> {
17  
        int idx = 0;
18  
        
19  
        public Pt get() {
20  
          for (; idx < pixels.length; idx++)
21  
            if (pixels[idx])
22  
              ret idxToPt(idx++);
23  
          null;
24  
        }
25  
      });
26  
    }
27  
  };
28  
  
29  
  int idx(int x, int y) {
30  
    ret (y-bounds.y)*bounds.w+x-bounds.x;
31  
  }
32  
  
33  
  Pt idxToPt(int idx) {
34  
    ret pt(bounds.x+(idx % bounds.w), bounds.y+idx/bounds.w);
35  
  }
36  
  
37  
  void init {
38  
    if (bounds != null) ret;
39  
    bounds = originalRegion.bounds();
40  
    pixels = new bool[area(bounds)];
41  
    for (Pt p : originalRegion.pixelIterator())
42  
      pixels[idx(p.x, p.y)] = true;
43  
  }
44  
  
45  
  bool change;
46  
  
47  
  public bool step() {
48  
    init();
49  
    
50  
    change = false;
51  
52  
    L<PtBuffer> traces = g22_allBorderTraces_withDiagonals(region);
53  
    
54  
    for (points : traces) {
55  
      if (eq(first(points), last(points))) removeLast(points);
56  
      
57  
      for i over points: {
58  
        ping();
59  
        if (deletableBorderPoint(points, i)) {
60  
          Pt p = points.get(i);
61  
          pixels[idx(p.x, p.y)] = false;
62  
          set change;
63  
        }
64  
      }
65  
      points.clear();
66  
    }
67  
68  
    ret change;
69  
  }
70  
  
71  
  bool deletableBorderPoint(PtBuffer points, int i) {
72  
    L<Pt> range = cyclicSubList(points, i-1, i+2);
73  
    Pt lastPoint = cyclicGet(points, i-1);
74  
    Pt p = points.get(i);
75  
    Pt diff = ptDiff(p, lastPoint);
76  
    Pt l = ptPlus(p, rotatePtLeft(diff));
77  
    Pt r = ptPlus(p, rotatePtRight(diff));
78  
    bool deletable = !range.contains(l) && region.contains(l)
79  
      || !range.contains(r) && region.contains(r);
80  
    printVars ifdef G22RegionThinner_debug(+range, +p, +l, +r, +deletable);
81  
    ret deletable;
82  
  }
83  
  
84  
  bool deletableBorderPoint_v1(PtBuffer points, int i) {
85  
    L<Pt> range = cyclicSubList(points, i-1, i+2);
86  
    Pt p = points.get(i);
87  
        
88  
    // check all 8 points around border point
89  
    for (int dir = 1; dir <= 8; dir++) {
90  
      Pt p2 = ptPlus(p, onePathDirection(dir));
91  
      
92  
      // If neighboring point is in region but not in current border,
93  
      // it makes the border point deletable
94  
      
95  
      if (region.contains(p2.x, p2.y)
96  
        && !range.contains(p2)) {
97  
          printVars ifdef G22RegionThinner_debug("deletable point", +p, +dir, +p2, +range);
98  
          ret true;
99  
        }
100  
    }
101  
102  
    false;
103  
  }
104  
  
105  
  IImageRegion get() { ret region; }
106  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034965
Snippet name: G22RegionThinner - thin a region [abandoned, replaced by G22RegionThinner_v2]
Eternal ID of this version: #1034965/22
Text MD5: 6c348b0fa62b2d5cf3e1a7e59656be20
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-21 22:54:50
Source code size: 2816 bytes / 106 lines
Pitched / IR pitched: No / No
Views / Downloads: 160 / 292
Version history: 21 change(s)
Referenced in: [show references]