Advertisement
Guest User

Untitled

a guest
May 12th, 2015
358
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.21 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> minGridBounds   = new ArrayList<>();
  11.     private static float                minGridWidth    = 0, minGridHeight = 0;
  12.     private static int                  minGridWSize    = 0, minGridHSize = 0;
  13.     private static boolean[][]          minGrid;
  14.     private static boolean[][]          finalMinGrid;
  15.  
  16.     public static ArrayList<Rectangle> generate(QuadtreeTerrain tree, int resLevel, int minGridLevel) {
  17.         minGridBounds.clear();
  18.         minGridWidth = tree.getWidth() / minGridLevel;
  19.         minGridHeight = tree.getHeight() / minGridLevel;
  20.         minGridWSize = minGridLevel;
  21.         minGridHSize = minGridLevel;
  22.         minGrid = new boolean[minGridWSize][minGridHSize];
  23.         finalMinGrid = new boolean[minGridWSize][minGridHSize];
  24.  
  25.         // Goes down the tree and get all solid bounds
  26.         System.out.println("Start");
  27.         long time = System.nanoTime();
  28.         long total = System.nanoTime();
  29.  
  30.         // Run through the mingrid
  31.         for (int i = 0; i < minGridWSize; i++) {
  32.             for (int j = 0; j < minGridHSize; j++) {
  33.  
  34.                 // Check if is contained
  35.                 boolean solid = tree.hasSolidsInRange(
  36.                         i * minGridWidth,
  37.                         j * minGridHeight,
  38.                         minGridWidth,
  39.                         minGridHeight,
  40.                         resLevel);
  41.                 minGrid[i][j] = solid;
  42.                 finalMinGrid[i][j] = solid;
  43.             }
  44.         }
  45.         System.out.println("mingrid make: " + (System.nanoTime() - time) / 1000000 + "ms");
  46.         time = System.nanoTime();
  47.  
  48.         // Simplify the mingrid - removes all non edges
  49.         for (int x = 1; x < minGridWSize - 1; x++) {
  50.             for (int y = 1; y < minGridHSize - 1; y++) {
  51.                 if (minGrid[x + 1][y] && minGrid[x - 1][y] && minGrid[x][y + 1] && minGrid[x][y - 1]
  52.                         && minGrid[x][y])
  53.                     finalMinGrid[x][y] = false;
  54.             }
  55.         }
  56.         System.out.println("mingrid edge: " + (System.nanoTime() - time) / 1000000 + "ms");
  57.         time = System.nanoTime();
  58.  
  59.         //Build minGridBounds
  60.         for (int x = 0; x < minGridWSize; x++) {
  61.             for (int y = 0; y < minGridHSize; y++) {
  62.                 if (finalMinGrid[x][y]) {
  63.                     // Store the rectangle start
  64.                     Point start = new Point(x * minGridWidth, y * minGridHeight);
  65.  
  66.                     // Find if the block spans in x or y
  67.                     int spanx = x + 1;
  68.                     int spany = y + 1;
  69.                     if (spanx < minGridWSize-1 && finalMinGrid[spanx][y]) { // X span
  70.                         while (spanx < minGridWSize - 1 && finalMinGrid[spanx][y]) {
  71.                             finalMinGrid[spanx][y] = false;
  72.                             spanx++;
  73.                         }
  74.                         minGridBounds.add(new Rectangle(start, minGridWidth * (spanx - x), minGridHeight));
  75.                     } else if (spany < minGridHSize-1 && finalMinGrid[x][spany]) { // Y span
  76.                         while (spany < minGridHSize-1 && finalMinGrid[x][spany]) {
  77.                             finalMinGrid[x][spany] = false;
  78.                             spany++;
  79.                         }
  80.                         minGridBounds.add(new Rectangle(start, minGridWidth, minGridHeight * (spany - y)));
  81.                     } else { // No span
  82.                         minGridBounds.add(new Rectangle(start, minGridWidth, minGridHeight));
  83.                     }
  84.                 }
  85.             }
  86.         }
  87.  
  88.         System.out.println("mingrid merge: " + (System.nanoTime() - time) / 1000000 + "ms");
  89.  
  90.         System.out.println("total mingrid: " + (System.nanoTime() - total) / 1000000 + "ms");
  91.         System.out.println("# generated: " + minGridBounds.size());
  92.  
  93.         return minGridBounds;
  94.     }
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement