Changeset 493

Show
Ignore:
Timestamp:
02/16/08 19:22:58 (9 months ago)
Author:
tom
Message:

fixed and betterer version of interactive processing version

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/processing/sketches/modest_maps_interactive/modest_maps_interactive.pde

    r492 r493  
    4343 
    4444  // 0 when sideLength == 256, 1 when sideLength == 512, 2 when sideLength == 1024 
    45   int zoom = min(20, max(0, (int)round(log(sideLength/zoom0Length) / log(2)))); 
     45  int zoom = min(20, max(1, (int)round(log(sideLength/zoom0Length) / log(2)))); 
    4646 
    4747  int cols = (int)pow(2,zoom); 
     
    5454 
    5555  // find the biggest box the screen would fit in, aligned with the map: 
    56   float screenMinX = -tx
    57   float screenMinY = -ty
    58   float screenMaxX = width-tx
    59   float screenMaxY = height-ty
     56  float screenMinX = 0
     57  float screenMinY = 0
     58  float screenMaxX = width
     59  float screenMaxY = height
    6060//  println("screen: " + nf(screenMinX,1,3) + " " + nf(screenMinY,1,3) + " : " + nf(screenMaxX,1,3) + " " + nf(screenMaxY,1,3)); 
    6161  // TODO align this box! 
     
    6868//  println("row/col: " + minCol + ", " + minRow + " : " + maxCol + ", " + maxRow); 
    6969 
     70  minCol -= 1; 
     71  minRow -= 1; 
     72  maxCol += 1; 
     73  maxRow += 1; 
     74 
     75  Vector visibleKeys = new Vector(); 
     76 
    7077  pushMatrix(); 
    7178  scale(1.0/pow(2,zoom)); 
    7279  for (int col = minCol; col <= maxCol; col++) { 
    7380    for (int row = minRow; row <= maxRow; row++) { 
    74       Coordinate coord = new Coordinate(row,col,zoom); 
    75       String coordKey = coord.toString(); 
    76       if (images.containsKey(coordKey)) { 
    77         PImage tile = (PImage)images.get(coord.toString()); 
     81      Coordinate coord = provider.sourceCoordinate(new Coordinate(row,col,zoom)); 
     82      coord.row = round(coord.row); 
     83      coord.column = round(coord.column); 
     84      coord.zoom = round(coord.zoom); 
     85      visibleKeys.add(coord); 
     86      if (images.containsKey(coord)) { 
     87        PImage tile = (PImage)images.get(coord); 
    7888        image(tile,col*256,row*256,256,256); 
    7989      } 
    8090      else { 
    81         if (!pending.containsKey(coordKey)) { 
    82           grabTile(coord); 
    83         } 
     91        grabTile(coord); 
    8492        fill(col >= 0 && col < cols && row >= 0 && row < rows ? 128 : 80); 
    8593        stroke(255); 
    8694        rect(col*256,row*256,256,256); 
     95        fill(255); 
     96        noStroke(); 
     97        textAlign(LEFT, TOP); 
     98        text("c:"+col+" "+"r:"+row+" "+"z:"+zoom, col*256, row*256); 
     99/*      textAlign(RIGHT, TOP); 
     100        text("c:"+col+" "+"r:"+row, (1+col)*256, row*256); 
     101        textAlign(LEFT, BOTTOM); 
     102        text("c:"+col+" "+"r:"+row, col*256, (1+row)*256); 
     103        textAlign(RIGHT, BOTTOM); 
     104        text("c:"+col+" "+"r:"+row, (1+col)*256, (1+row)*256); */ 
    87105      }     
    88       fill(255); 
    89       noStroke(); 
    90       textAlign(LEFT, TOP); 
    91       text("c:"+col+" "+"r:"+row+" "+"z:"+zoom, col*256, row*256); 
    92 /*      textAlign(RIGHT, TOP); 
    93       text("c:"+col+" "+"r:"+row, (1+col)*256, row*256); 
    94       textAlign(LEFT, BOTTOM); 
    95       text("c:"+col+" "+"r:"+row, col*256, (1+row)*256); 
    96       textAlign(RIGHT, BOTTOM); 
    97       text("c:"+col+" "+"r:"+row, (1+col)*256, (1+row)*256); */ 
    98106    } 
    99107  } 
     
    101109   
    102110  popMatrix(); 
     111 
     112//  println(pending.size() + " pending..."); 
     113//  println(queue.size() + " in queue, pruning..."); 
     114  queue.retainAll(visibleKeys); 
     115//  println(queue.size() + " in queue"); 
     116//  println(); 
     117 
     118  processQueue(); 
    103119   
    104120  if (keyPressed) { 
     
    139155} 
    140156 
    141 Hashtable pending = new Hashtable(); 
    142 Hashtable images = new Hashtable(); 
     157// loading tiles 
     158Hashtable pending = new Hashtable(); // coord.toString() -> TileLoader 
     159// loaded tiles 
     160Hashtable images = new Hashtable();  // coord.toString() -> PImage 
     161// coords waiting to load 
     162Vector queue = new Vector(); // coord 
    143163 
    144164void grabTile(Coordinate coord) { 
    145   Thread thread = new Thread(new TileLoader(coord)); 
    146   pending.put(coord.toString(), thread); 
    147   thread.start(); // TODO throttle this 
     165  if (!pending.containsKey(coord) && !queue.contains(coord) && !images.containsKey(coord)) { 
     166//    println("adding " + coord.toString() + " to queue"); 
     167    queue.add(coord); 
     168  } 
    148169} 
    149170 
     
    157178    println("loading: " + url); 
    158179    PImage img = loadImage(url); // TODO: layered tiles 
    159     println("loaded: " + url); 
    160     images.put(coord.toString(), img); 
    161     pending.remove(coord.toString()); 
     180//    println("loaded: " + url); 
     181    tileDone(coord, img); 
    162182  } 
    163183} 
     184 
     185void tileDone(Coordinate coord, PImage img) { 
     186  images.put(coord, img); 
     187  pending.remove(coord);   
     188} 
     189 
     190void processQueue() { 
     191  while (pending.size() < 4 && queue.size() > 0) { 
     192    Coordinate coord = (Coordinate)queue.remove(0); 
     193    TileLoader tileLoader = new TileLoader(coord); 
     194    pending.put(coord, tileLoader); 
     195    new Thread(tileLoader).start(); 
     196  }   
     197} 
  • trunk/processing/src/com/modestmaps/AbstractMapProvider.java

    r485 r493  
    3434 
    3535  public Coordinate sourceCoordinate(Coordinate coordinate) { 
    36     float wrappedColumn = coordinate.column % PApplet.pow(2, coordinate.zoom); 
     36    float gridSize = PApplet.pow(2, coordinate.zoom); 
    3737 
     38    float wrappedColumn = coordinate.column % gridSize; 
    3839    while (wrappedColumn < 0) { 
    39       wrappedColumn += PApplet.pow(2, coordinate.zoom)
     40      wrappedColumn += gridSize
    4041    } 
    4142 
    42     return new Coordinate(coordinate.row, wrappedColumn, coordinate.zoom); 
     43    float wrappedRow = coordinate.row % gridSize; 
     44    while (wrappedRow < 0) { 
     45      wrappedRow += gridSize; 
     46    } 
     47 
     48    return new Coordinate(wrappedRow, wrappedColumn, coordinate.zoom); 
    4349  } 
    4450 
  • trunk/processing/src/com/modestmaps/Coordinate.java

    r485 r493  
    1717  public String toString() { 
    1818    return "(" + PApplet.nf(row,1,3) + ", " + PApplet.nf(column,1,3) + " @" + PApplet.nf(zoom,1,3) + ")"; 
     19  } 
     20 
     21  public boolean equals(Object o) { 
     22    Coordinate c = (Coordinate)o; 
     23    //return PApplet.abs(c.row - row) < PApplet.EPSILON && PApplet.abs(c.column - column) < PApplet.EPSILON && PApplet.abs(c.zoom -  zoom) < PApplet.EPSILON; 
     24    return c.row == row && c.column == column && c.zoom == zoom; 
     25  } 
     26 
     27  public int hashCode() { 
     28    return toString().hashCode(); 
    1929  } 
    2030