Advertisement
Guest User

Untitled

a guest
May 12th, 2015
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.34 KB | None | 0 0
  1. package com.carvalab.terrain.test;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import com.carvalab.terrain.algorithms.QuadtreeTerrain;
  6.  
  7.  
  8. public class QuadtreeBounds {
  9.  
  10.     private static ArrayList<Rectangle> solidBounds = new ArrayList<>();
  11.     private static ArrayList<Rectangle> minGridBounds   = new ArrayList<>();
  12.     private static float                minGridWidth    = 0, minGridHeight = 0;
  13.     private static int                  minGridWSize    = 0, minGridHSize = 0;
  14.     private static boolean[][]          minGrid;
  15.     private static boolean[][]          finalMinGrid;
  16.  
  17.     public static ArrayList<Rectangle> generate(QuadtreeTerrain tree, int resLevel, int minGridLevel) {
  18.         solidBounds.clear();
  19.         minGridBounds.clear();
  20.         minGridWidth = tree.getWidth() / minGridLevel;
  21.         minGridHeight = tree.getHeight() / minGridLevel;
  22.         minGridWSize = minGridLevel;
  23.         minGridHSize = minGridLevel;
  24.         minGrid = new boolean[minGridWSize][minGridHSize];
  25.         finalMinGrid = new boolean[minGridWSize][minGridHSize];
  26.  
  27.         // Goes down the tree and get all solid bounds
  28.         analizeTree(tree, resLevel);
  29.  
  30.         // Run through the mingrid
  31.         for (int i = 0; i < minGridWSize; i++) {
  32.             for (int j = 0; j < minGridHSize; j++) {
  33.                 Rectangle min = new Rectangle(new Point(i * minGridWidth, j * minGridHeight), minGridWidth,
  34.                         minGridHeight);
  35.  
  36.                 // Check if is contained
  37.                 for (Rectangle r : solidBounds)
  38.                     if (!(r.getX() > min.getX() + min.width || r.getX() + r.width < min.getX()
  39.                             || r.getY() > min.getY() + min.height || r.getY() + r.height < min.getY())) {
  40.                         minGrid[i][j] = true;
  41.                         finalMinGrid[i][j] = true;
  42.                     }
  43.             }
  44.         }
  45.         /*
  46.          * System.out.println("mingrid: ");
  47.          * printMatrix(minGrid);
  48.          */
  49.  
  50.         //Simplify the mingrid
  51.         for (int x = 1; x < minGridWSize - 1; x++) {
  52.             for (int y = 1; y < minGridHSize - 1; y++) {
  53.                 if (minGrid[x + 1][y] && minGrid[x - 1][y] && minGrid[x][y + 1] && minGrid[x][y - 1]
  54.                         && minGrid[x][y])
  55.                     finalMinGrid[x][y] = false;
  56.             }
  57.         }
  58.         /*
  59.          * System.out.println("simplified: ");
  60.          * printMatrix(finalMinGrid);
  61.          */
  62.  
  63.         //Build minGridBounds
  64.         for (int x = 0; x < minGridWSize; x++) {
  65.             for (int y = 0; y < minGridHSize; y++) {
  66.                 if (finalMinGrid[x][y]) {
  67.                     // Store the rectangle start
  68.                     Point start = new Point(x * minGridWidth, y * minGridHeight);
  69.  
  70.                     // Find if the block spans in x or y
  71.                     int spanx = x + 1;
  72.                     int spany = y + 1;
  73.                     if (spanx < minGridWSize && finalMinGrid[spanx][y]) { // X span
  74.                         while (finalMinGrid[spanx][y] && spanx < minGridWSize) {
  75.                             finalMinGrid[spanx][y] = false;
  76.                             spanx++;
  77.                         }
  78.                         minGridBounds.add(new Rectangle(start, minGridWidth * (spanx - x), minGridHeight));
  79.                     } else if (spany < minGridHSize && finalMinGrid[x][spany]) { // Y span
  80.                         while (finalMinGrid[x][spany] && spanx < minGridHSize) {
  81.                             finalMinGrid[x][spany] = false;
  82.                             spany++;
  83.                         }
  84.                         minGridBounds.add(new Rectangle(start, minGridWidth, minGridHeight * (spany - y)));
  85.                     } else { // No span
  86.                         minGridBounds.add(new Rectangle(start, minGridWidth, minGridHeight));
  87.                     }
  88.                 }
  89.  
  90.                 // Point start = new Point(x * minGridWidth, y * minGridHeight);
  91.                 // minGridBounds.add(new Rectangle(start, minGridWidth, minGridHeight));
  92.             }
  93.  
  94.         }
  95.  
  96.         return minGridBounds;
  97.     }
  98.  
  99.     /**
  100.      * Goes down the tree and get all solid bounds
  101.      *
  102.      * @param tree
  103.      */
  104.     private static void analizeTree(QuadtreeTerrain tree, int resLevel) {
  105.         if (tree != null) {
  106.             if (tree.isLeaf() || tree.getLevel() == resLevel) {
  107.                 // Analise each son
  108.                 analizeNode(tree);
  109.             } else {
  110.                 // Go down the tree
  111.                 for (QuadtreeTerrain child : tree.getChildren())
  112.                     analizeTree(child, resLevel);
  113.             }
  114.         }
  115.     }
  116.  
  117.     /**
  118.      * Only leaf are analized.
  119.      * Store all solid rectangles.
  120.      *
  121.      * @param node
  122.      */
  123.     private static void analizeNode(QuadtreeTerrain node) {
  124.         if (node.isFilled())
  125.             solidBounds.add(new Rectangle(new Point((int) node.getX(), (int) node.getY()), (int) node
  126.                     .getWidth(), (int) node.getHeight()));
  127.     }
  128.  
  129.     private static void printMatrix(boolean[][] m) {
  130.         try {
  131.             int rows = m.length;
  132.             int columns = m[0].length;
  133.             String str = " ";
  134.  
  135.             for (int i = 0; i < rows; i++) {
  136.                 for (int j = 0; j < columns; j++) {
  137.                     str += m[i][j] + " ";
  138.                 }
  139.  
  140.                 System.out.println(str + "");
  141.                 str = " ";
  142.             }
  143.  
  144.         } catch (Exception e) {
  145.             System.out.println("Matrix is empty!!");
  146.         }
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement