public int countImagesInSector(Sector sector, int levelNumber) {
    if (sector == null) {
      String msg = Logging.getMessage("nullValue.SectorIsNull");
      Logging.logger().severe(msg);
      throw new IllegalArgumentException(msg);
    }

    Level targetLevel = this.levels.getLastLevel();
    if (levelNumber >= 0) {
      for (int i = levelNumber; i < this.getLevels().getLastLevel().getLevelNumber(); i++) {
        if (this.levels.isLevelEmpty(i)) continue;

        targetLevel = this.levels.getLevel(i);
        break;
      }
    }

    // Collect all the tiles intersecting the input sector.
    LatLon delta = targetLevel.getTileDelta();
    Angle latOrigin = this.levels.getTileOrigin().getLatitude();
    Angle lonOrigin = this.levels.getTileOrigin().getLongitude();
    final int nwRow = Tile.computeRow(delta.getLatitude(), sector.getMaxLatitude(), latOrigin);
    final int nwCol = Tile.computeColumn(delta.getLongitude(), sector.getMinLongitude(), lonOrigin);
    final int seRow = Tile.computeRow(delta.getLatitude(), sector.getMinLatitude(), latOrigin);
    final int seCol = Tile.computeColumn(delta.getLongitude(), sector.getMaxLongitude(), lonOrigin);

    int numRows = nwRow - seRow + 1;
    int numCols = seCol - nwCol + 1;

    return numRows * numCols;
  }
  protected int determineAdjustmentSide(Movable dragObject, double factor) {
    if (dragObject instanceof SurfaceSector) {
      SurfaceSector quad = (SurfaceSector) dragObject;
      Sector s = quad.getSector(); // TODO: go over all sectors
      Position p = this.getWwd().getCurrentPosition();

      if (p == null) {
        return NONE;
      }

      double dN = abs(s.getMaxLatitude().subtract(p.getLatitude()).degrees);
      double dS = abs(s.getMinLatitude().subtract(p.getLatitude()).degrees);
      double dW = abs(s.getMinLongitude().subtract(p.getLongitude()).degrees);
      double dE = abs(s.getMaxLongitude().subtract(p.getLongitude()).degrees);

      double sLat = factor * s.getDeltaLatDegrees();
      double sLon = factor * s.getDeltaLonDegrees();

      if (dN < sLat && dW < sLon) return NORTHWEST;
      if (dN < sLat && dE < sLon) return NORTHEAST;
      if (dS < sLat && dW < sLon) return SOUTHWEST;
      if (dS < sLat && dE < sLon) return SOUTHEAST;
      if (dN < sLat) return NORTH;
      if (dS < sLat) return SOUTH;
      if (dW < sLon) return WEST;
      if (dE < sLon) return EAST;
    }

    return NONE;
  }
  protected Sector resizeShape(Movable dragObject, int side) {
    if (dragObject instanceof SurfaceSector) {
      SurfaceSector quad = (SurfaceSector) dragObject;
      Sector s = quad.getSector(); // TODO: go over all sectors
      Position p = this.getWwd().getCurrentPosition();

      if (p == null || this.getPreviousPosition() == null) {
        return null;
      }

      Angle dLat = p.getLatitude().subtract(this.getPreviousPosition().getLatitude());
      Angle dLon = p.getLongitude().subtract(this.getPreviousPosition().getLongitude());

      Angle newMinLat = s.getMinLatitude();
      Angle newMinLon = s.getMinLongitude();
      Angle newMaxLat = s.getMaxLatitude();
      Angle newMaxLon = s.getMaxLongitude();

      if (side == NORTH) {
        newMaxLat = s.getMaxLatitude().add(dLat);
      } else if (side == SOUTH) {
        newMinLat = s.getMinLatitude().add(dLat);
      } else if (side == EAST) {
        newMaxLon = s.getMaxLongitude().add(dLon);
      } else if (side == WEST) {
        newMinLon = s.getMinLongitude().add(dLon);
      } else if (side == NORTHWEST) {
        newMaxLat = s.getMaxLatitude().add(dLat);
        newMinLon = s.getMinLongitude().add(dLon);
      } else if (side == NORTHEAST) {
        newMaxLat = s.getMaxLatitude().add(dLat);
        newMaxLon = s.getMaxLongitude().add(dLon);
      } else if (side == SOUTHWEST) {
        newMinLat = s.getMinLatitude().add(dLat);
        newMinLon = s.getMinLongitude().add(dLon);
      } else if (side == SOUTHEAST) {
        newMinLat = s.getMinLatitude().add(dLat);
        newMaxLon = s.getMaxLongitude().add(dLon);
      }

      return new Sector(newMinLat, newMaxLat, newMinLon, newMaxLon);
    }

    return null;
  }
  public static VirtualEarthTile[] createTiles(
      Sector bbox /*int wwLevel, int wwRow, int wwCol*/, VirtualEarthLayer layer)
      throws WWRuntimeException {
    if (null == bbox) {
      String message = Logging.getMessage("nullValue.SectorIsNull");
      Logging.logger().severe(message);
      throw new WWRuntimeException(message);
    }

    if (null == layer) {
      String message = Logging.getMessage("nullValue.LayerIsNull");
      Logging.logger().severe(message);
      throw new WWRuntimeException(message);
    }

    int level = getZoomLevelByTrueViewRange(bbox.getDeltaLatDegrees());

    Point startPixel =
        LatLongToPixelXY(bbox.getMaxLatitude().degrees, bbox.getMinLongitude().degrees, level);
    Point endPixel =
        LatLongToPixelXY(bbox.getMinLatitude().degrees, bbox.getMaxLongitude().degrees, level);

    Point startTile = PixelXYToTileXY(startPixel.x, startPixel.y);
    Point endTile = PixelXYToTileXY(endPixel.x, endPixel.y);

    ArrayList<VirtualEarthTile> tileList = new ArrayList<VirtualEarthTile>();

    for (int y = startTile.y; y <= endTile.y; y++) {
      for (int x = startTile.x; x <= endTile.x; x++) {
        try {
          int ulPixelX = x * VE_MAX_TILE_SIZE;
          int ulPixelY = y * VE_MAX_TILE_SIZE;
          LatLon ul = PixelXYToLatLong(ulPixelX, ulPixelY, level);

          int lrPixelX = ulPixelX + VE_MAX_TILE_SIZE;
          int lrPixelY = ulPixelY + VE_MAX_TILE_SIZE;
          LatLon lr = PixelXYToLatLong(lrPixelX, lrPixelY, level);

          Sector tileSector = Sector.boundingSector(ul, lr);

          tileList.add(new VirtualEarthTile(x, y, level, layer, tileSector));
        } catch (Exception ex) {
          Logging.logger().log(Level.SEVERE, ex.getMessage(), ex);
        }
      }
    }

    VirtualEarthTile[] tiles = new VirtualEarthTile[tileList.size()];
    return tileList.toArray(tiles);
  }
  private MercatorTextureTile[][] getTilesInSector(Sector sector, int levelNumber) {
    if (sector == null) {
      String msg = Logging.getMessage("nullValue.SectorIsNull");
      Logging.logger().severe(msg);
      throw new IllegalArgumentException(msg);
    }

    Level targetLevel = this.levels.getLastLevel();
    if (levelNumber >= 0) {
      for (int i = levelNumber; i < this.getLevels().getLastLevel().getLevelNumber(); i++) {
        if (this.levels.isLevelEmpty(i)) continue;

        targetLevel = this.levels.getLevel(i);
        break;
      }
    }

    // Collect all the tiles intersecting the input sector.
    LatLon delta = targetLevel.getTileDelta();
    Angle latOrigin = this.levels.getTileOrigin().getLatitude();
    Angle lonOrigin = this.levels.getTileOrigin().getLongitude();
    final int nwRow = Tile.computeRow(delta.getLatitude(), sector.getMaxLatitude(), latOrigin);
    final int nwCol = Tile.computeColumn(delta.getLongitude(), sector.getMinLongitude(), lonOrigin);
    final int seRow = Tile.computeRow(delta.getLatitude(), sector.getMinLatitude(), latOrigin);
    final int seCol = Tile.computeColumn(delta.getLongitude(), sector.getMaxLongitude(), lonOrigin);

    int numRows = nwRow - seRow + 1;
    int numCols = seCol - nwCol + 1;
    MercatorTextureTile[][] sectorTiles = new MercatorTextureTile[numRows][numCols];

    for (int row = nwRow; row >= seRow; row--) {
      for (int col = nwCol; col <= seCol; col++) {
        TileKey key =
            new TileKey(targetLevel.getLevelNumber(), row, col, targetLevel.getCacheName());
        Sector tileSector = this.levels.computeSectorForKey(key);
        MercatorSector mSector = MercatorSector.fromSector(tileSector); // TODO: check
        sectorTiles[nwRow - row][col - nwCol] =
            new MercatorTextureTile(mSector, targetLevel, row, col);
      }
    }

    return sectorTiles;
  }
    protected List<Position> buildGrid(Sector sector, double height, int nLatCells, int nLonCells) {
      List<Position> grid = new ArrayList<Position>((nLatCells + 1) * (nLonCells + 1));

      double dLat = sector.getDeltaLatDegrees() / nLatCells;
      double dLon = sector.getDeltaLonDegrees() / nLonCells;

      for (int j = 0; j <= nLatCells; j++) {
        double lat =
            j == nLatCells
                ? sector.getMaxLatitude().degrees
                : sector.getMinLatitude().degrees + j * dLat;

        for (int i = 0; i <= nLonCells; i++) {
          double lon =
              i == nLonCells
                  ? sector.getMaxLongitude().degrees
                  : sector.getMinLongitude().degrees + i * dLon;

          grid.add(Position.fromDegrees(lat, lon, height));
        }
      }

      return grid;
    }