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

81
LINES

< > BotCompany Repo | #1034908 // SimpleOutlineWalker [dev.]

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

Libraryless. Click here for Pure Java version (6721L/37K).

// backtracks lead to endless loops - must rework the architecture
sclass SimpleOutlineWalker is Steppable {
  IIntIntPred pixelIsInside;
  int x, y;
  settable bool processBacktracks = false;
  int startX, startY;
  NESWDirection direction = NESWDirection.east;
  gettable int nPixels;
  new PtBuffer pixels;
  new L<BacktrackPoint> backtrackStack;
  
  // seen points as intPairToLong
  new CompactLongSet seen;
  
  event foundPoint(int x, int y);
  
  srecord BacktrackPoint(int x, int y, NESWDirection dir, int turn) {}
  
  *(IIntIntPred pixelIsInside, Pt p) {
    this(pixelIsInside, p.x, p.y);
  }
  
  // x, y must be inside the shape
  *(IIntIntPred *pixelIsInside, int *x, int *y) {
    if (!pixelIsInside.get(x, y))
      fail("pixel is not inside shape: " + x + "/" + y);
      
    // go north until we hit the border
    while (pixelIsInside.get(x, y-1))
      --y;
      
    startX = x; startY = y; this.y = y;
  }
  
  public bool step() {
    ++nPixels;
    pixels?.add(x, y);
    foundPoint(x, y);
    
    if (walkOneStep()) true;
    
    if (!processBacktracks) false;
    while (nempty(backtrackStack))
      if (processBacktrack()) true;
    false;
  }
  
  bool walkOneStep(int turn default 3) {
    // try left, straight, right, back
    for (; turn <= 6; turn++) {
      NESWDirection newDirection = direction.plus(turn);
      int x2 = x+newDirection.x, y2 = y+newDirection.y;
      
      bool inside = pixelIsInside.get(x2, y2);
      ifdef SimpleOutlineWalker_debug
        printVars(+x, +y, +direction, +turn, +newDirection, +x2, +y2, +inside);
      endifdef
      if (inside) {
        if (turn < 6)
          backtrackStack.add(new BacktrackPoint(x, y, direction, turn+1));
          
        x = x2; y = y2; direction = newDirection;
        
        break;
      }
    }
    
    ret seen.add(intPairToLong(x, y));
    //ret x != startX || y != startY;
  }
  
  bool processBacktrack() {
    if (empty(backtrackStack)) false;
    var bt = popLast(backtrackStack);
    x = bt.x; y = bt.y; direction = bt.dir;
    
    ret walkOneStep(bt.turn);
  }
  
  Pt currentPt() { ret pt(x, y); }
}

Author comment

Began life as a copy of #1034903

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1034908
Snippet name: SimpleOutlineWalker [dev.]
Eternal ID of this version: #1034908/21
Text MD5: f8afc3d2ed8789b29e8ba949a66df5bc
Transpilation MD5: edeb589480bf5576acac1cc2db3123dc
Author: stefan
Category: javax / image recognition
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-15 17:54:26
Source code size: 2204 bytes / 81 lines
Pitched / IR pitched: No / No
Views / Downloads: 161 / 325
Version history: 20 change(s)
Referenced in: [show references]