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

67
LINES

< > BotCompany Repo | #1034119 // G22Walker - for video input. follows a rectangular remembered image around the animation

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

Libraryless. Click here for Pure Java version (5805L/32K).

// has a rectangular window (visionW*visionH)
// walking range per frame is a circle with radius "range"
asclass G22Walker<Img extends WidthAndHeight> {
  // configuration (set)
  int range = 1; // how far we can walk in each frame
  double roundingPolicy = .9; // for making the range circle (between 0 and 1)
  int visionW = 3, visionH = 3; // window rectangle size (both should be odd)
  
  // current position (set/get)
  Pt position; // current position (center), not null
  
  // status variables (read)
  bool stuck; // can neither stay nor move anywhere for some reason
  int guessWork; // how many "optimal" new positions we chose from
  
  void move(Img image) {
    stuck = false;
    guessWork = 0;
    
    // How far can I move until I hit the image's borders
    Rect movement1 = rectFromPoints(visionW/2, visionH/2,
      image.getWidth()-(visionW+1)/2, (visionH+1)/2);
    
    // How far can I walk in one frame
    Rect movement2 = rectAroundPt(position, range*2+1);
    
    Rect movement = intersectRects(movement1, movement2);
    if (empty(movement)) ret with set stuck;
    
    printVars(+movement, +movement1, +movement2);
    
    if (hasRememberedImage())
      ret with rememberImage(image);
    
    Rect r = window_uncropped();
    
    new MultiBest<Pt> bestPositions;
    
    int mx1 = movement.x1(), mx2 = movement.x2();
    int my1 = movement.y1(), my2 = movement.y2();
    for (int y = my1; y < my2; y++)
      for (int x = mx1; x < mx2; x++) {
        var newPos = pt(x, y);
        bestPositions.put(newPos, scorePosition(newPos, image));
      }
    var best = bestPositions!;
    guessWork = l(best);
    position = random(best);
    
    // It's actually a question how often we grab a fresh image.
    // For now, every time.
    rememberImage(image);
  }
  
  // only works when we already have a rememberedImage
  abstract double scorePosition(Pt pos, Img image);
  
  abstract void rememberImage(Img image);
  abstract bool hasRememberedImage();

  int visionX1() { ret position.x-visionW; }
  int visionY1() { ret position.y-visionH; }
  
  Rect window_uncropped(Pt center default position) {
    ret rectAroundPt(center, visionW, visionH);
  }
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1034119
Snippet name: G22Walker - for video input. follows a rectangular remembered image around the animation
Eternal ID of this version: #1034119/21
Text MD5: 266dafe6352855a632bf4e12a578a49a
Transpilation MD5: 4a61e51aec79a1540a029249593e658d
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-14 15:33:17
Source code size: 2259 bytes / 67 lines
Pitched / IR pitched: No / No
Views / Downloads: 201 / 388
Version history: 20 change(s)
Referenced in: [show references]