Advertisement
CaptainSpaceCat

ImageMesh - PolyOutline

Jul 26th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.75 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. /* This support class contains information
  4.  * on the outline of a certain region
  5.  * within the formatted image.
  6.  * */
  7.  
  8. public class PolyOutline {
  9.  
  10.   public ArrayList<int[]> nodeMap;
  11.   public boolean[][] nodeGrid;
  12.   public boolean[][] edgeGrid;
  13.  
  14.   public int[] boundingBox; //0-max left, 1-max up, 2-max right, 3-max down
  15.                             //also interpreted as: 0-Ax, 1-Ay, 2-Bx, 3-By
  16.  
  17.   public PolyOutline(int w, int h) {
  18.     nodeMap = new ArrayList<int[]>();
  19.     nodeGrid = new boolean[w+1][h+1];
  20.     edgeGrid = new boolean[w+1][h];
  21.   }
  22.  
  23.   public void add(int x, int y) {
  24.     nodeMap.add(new int[] {x, y});
  25.     nodeGrid[x][y] = true;
  26.   }
  27.  
  28.   public void add(int[] p) {
  29.     nodeMap.add(new int[] {p[0], p[1]});
  30.     nodeGrid[p[0]][p[1]] = true;
  31.   }
  32.  
  33.   public void remove(int i) {
  34.     int[] c = nodeMap.remove(i);
  35.     nodeGrid[c[0]][c[1]] = false;
  36.   }
  37.  
  38.   public int[] getPrevPos() {
  39.     return nodeMap.get(nodeMap.size()-2);
  40.   }
  41.  
  42.   public void addEdge(int x, int y) {
  43.     edgeGrid[x][y] = true;
  44.   }
  45.  
  46.   public void addEdge(int[] p) {
  47.     edgeGrid[p[0]][p[1]] = true;
  48.   }
  49.  
  50.   public int optimize(int finalNodeNum) {
  51.     int[] temp = nodeMap.get(0);
  52.     int index = 0;
  53.     double n = 0;
  54.     int s = nodeMap.size();
  55.     double c = finalNodeNum*1d/s;
  56.     if (finalNodeNum < s) {
  57.       //System.out.println(finalNodeNum + "/" + s + " = " + c);
  58.       for (int i = 0; i < s; i++) {
  59.         n += c;
  60.         if (n < 1) {
  61.           remove(index);
  62.         } else {
  63.           n--;
  64.           index++;
  65.         }
  66.       }
  67.       if (nodeMap.size() < finalNodeNum) {
  68.         nodeMap.add(temp);
  69.       }
  70.     }
  71.     System.out.println(nodeMap.size());
  72.     return nodeMap.size();
  73.   }
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement