Example usage for java.lang Math ceil

List of usage examples for java.lang Math ceil

Introduction

In this page you can find the example usage for java.lang Math ceil.

Prototype

public static double ceil(double a) 

Source Link

Document

Returns the smallest (closest to negative infinity) double value that is greater than or equal to the argument and is equal to a mathematical integer.

Usage

From source file:fr.amap.lidar.amapvox.voxelisation.postproc.MultiBandRaster.java

public static BSQ computeRaster(float startingHeight, float step, int bandNumber, int resolution,
        VoxelSpaceInfos infos, Voxel[][][] voxels, Raster dtm) {

    float[] altitudes = new float[bandNumber];
    for (int i = 0; i < bandNumber; i++) {
        altitudes[i] = startingHeight + (i * step);
    }/*www  . j a  v  a  2s.c  o m*/

    float scale = (float) (resolution / infos.getResolution());

    int rasterXSize = (int) (Math.ceil(infos.getSplit().x / scale));
    int rasterYSize = (int) (Math.ceil(infos.getSplit().y / scale));

    BHeader header = new BHeader(rasterXSize, rasterYSize, altitudes.length, BCommon.NumberOfBits.N_BITS_32);
    header.setUlxmap(infos.getMinCorner().x + (resolution / 2.0f));
    header.setUlymap(infos.getMinCorner().y - (infos.getResolution() / 2.0f)
            + (infos.getSplit().y * infos.getResolution()));
    header.setXdim(resolution);
    header.setYdim(resolution);

    BSQ raster = new BSQ(null, header);

    Statistic[][][] padMean = new Statistic[rasterXSize][rasterYSize][altitudes.length];

    if (dtm != null) {

        if (altitudes.length > 0) {

            float altitudeMin = altitudes[0];

            for (int i = 0; i < infos.getSplit().x; i++) {
                for (int j = infos.getSplit().y - 1; j >= 0; j--) {
                    for (int k = 0; k < infos.getSplit().z; k++) {

                        Voxel vox = voxels[i][j][k];

                        //on calcule l'indice de la couche auquel appartient le voxel
                        if (vox != null && vox.ground_distance > altitudeMin) {
                            int layer = (int) ((vox.ground_distance - altitudeMin) / step);

                            if (layer < altitudes.length) {

                                int indiceI = (int) (i / scale);
                                int indiceJ = (int) (j / scale);

                                if (padMean[indiceI][indiceJ][layer] == null) {
                                    padMean[indiceI][indiceJ][layer] = new Statistic();
                                }

                                if (!Float.isNaN(vox.PadBVTotal)) {
                                    padMean[indiceI][indiceJ][layer].addValue(vox.PadBVTotal);
                                }
                            }
                        }
                    }
                }
            }

            long l = 4294967295L;

            try {
                //on crit la moyenne
                for (int i = 0; i < rasterXSize; i++) {
                    for (int j = rasterYSize - 1, j2 = 0; j >= 0; j--, j2++) {
                        for (int k = 0; k < altitudes.length; k++) {

                            if (padMean[i][j][k] != null) {

                                double meanPAD = padMean[i][j][k].getMean();
                                //float value = (meanOfPAD-0)/(MAX_PAD-0);

                                long value = (long) ((meanPAD / (double) infos.getMaxPAD()) * (l));
                                String binaryString = Long.toBinaryString(value);
                                byte[] bval = new BigInteger(binaryString, 2).toByteArray();
                                ArrayUtils.reverse(bval);
                                byte b0 = 0x0, b1 = 0x0, b2 = 0x0, b3 = 0x0;
                                if (bval.length > 0) {
                                    b0 = bval[0];
                                }
                                if (bval.length > 1) {
                                    b1 = bval[1];
                                }
                                if (bval.length > 2) {
                                    b2 = bval[2];
                                }
                                if (bval.length > 3) {
                                    b3 = bval[3];
                                }
                                raster.setPixel(i, j2, k, b0, b1, b2, b3);

                            }
                        }
                    }
                }

            } catch (Exception ex) {
                LOGGER.error(ex);
            }
        }

    }

    return raster;
}

From source file:Main.java

public static Rectangle calculateBoundsForComponent(JComponent comp, String text, int xOffset, int yOffset) {

    FontMetrics fm = comp.getFontMetrics(comp.getFont());
    Rectangle2D bounds = fm.getStringBounds(text, comp.getGraphics());

    return new Rectangle(xOffset + (int) bounds.getX(), yOffset,
            (int) Math.ceil(bounds.getWidth()) + (PADDING * 2),
            (int) Math.ceil(bounds.getHeight() + fm.getDescent()) + (PADDING * 2));
}

From source file:Main.java

public static Bitmap cropMaxVisibleBitmap(Drawable drawable, int iconSize) {
    Bitmap tmp = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            Bitmap.Config.ARGB_8888);/*from   ww w .  j a  v a  2  s  .co m*/
    Canvas canvas = new Canvas(tmp);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    Rect crop = new Rect(tmp.getWidth(), tmp.getHeight(), -1, -1);
    for (int y = 0; y < tmp.getHeight(); y++) {
        for (int x = 0; x < tmp.getWidth(); x++) {
            int alpha = (tmp.getPixel(x, y) >> 24) & 255;
            if (alpha > 0) { // pixel is not 100% transparent
                if (x < crop.left)
                    crop.left = x;
                if (x > crop.right)
                    crop.right = x;
                if (y < crop.top)
                    crop.top = y;
                if (y > crop.bottom)
                    crop.bottom = y;
            }
        }
    }

    if (crop.width() <= 0 || crop.height() <= 0) {
        return Bitmap.createScaledBitmap(tmp, iconSize, iconSize, true);
    }

    // We want to crop a square region.
    float size = Math.max(crop.width(), crop.height());
    float xShift = (size - crop.width()) * 0.5f;
    crop.left -= Math.floor(xShift);
    crop.right += Math.ceil(xShift);

    float yShift = (size - crop.height()) * 0.5f;
    crop.top -= Math.floor(yShift);
    crop.bottom += Math.ceil(yShift);

    Bitmap finalImage = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888);
    canvas.setBitmap(finalImage);
    float scale = iconSize / size;

    canvas.scale(scale, scale);
    canvas.drawBitmap(tmp, -crop.left, -crop.top, new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
    canvas.setBitmap(null);
    return finalImage;
}

From source file:Main.java

public static Bitmap scaleToFillBitmap(Bitmap dst, byte[] byImage) {
    Bitmap src = BitmapFactory.decodeByteArray(byImage, 0, byImage.length);

    float scaled = 1.0f;
    if ((float) dst.getWidth() / (float) src.getWidth() < (float) dst.getHeight() / (float) src.getHeight()) {
        scaled = (float) dst.getHeight() / (float) src.getHeight();
    } else {/*w w  w .  ja  v  a 2  s. c  o  m*/
        scaled = (float) dst.getWidth() / (float) src.getWidth();
    }

    Bitmap bmpScaled = Bitmap.createScaledBitmap(src, (int) Math.ceil(src.getWidth() * scaled),
            (int) Math.ceil(src.getHeight() * scaled), true);

    int offsetX = 0;
    int offsetY = 0;
    offsetX = bmpScaled.getWidth() - dst.getWidth() != 0 ? (bmpScaled.getWidth() - dst.getWidth()) / 2 : 0;
    offsetY = bmpScaled.getHeight() - dst.getHeight() != 0 ? (bmpScaled.getHeight() - dst.getHeight()) / 2 : 0;

    return Bitmap.createBitmap(bmpScaled, offsetX, offsetY, dst.getWidth(), dst.getHeight());
}

From source file:Main.java

@Override
public void start(Stage primaryStage) {
    BorderPane root = new BorderPane();
    Scene scene = new Scene(root, 180, 250);

    String[] keys = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "*", "0", "#" };

    GridPane numPad = new GridPane();
    for (int i = 0; i < 12; i++) {
        Button button = new Button(keys[i]);
        button.getStyleClass().add("num-button");
        numPad.add(button, i % 3, (int) Math.ceil(i / 3));
    }/*from  w  ww  .  j  a  v  a2s. c  o m*/

    Button call = new Button("Call");
    call.setId("call-button");
    call.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
    numPad.add(call, 0, 4);

    GridPane.setColumnSpan(call, 3);
    GridPane.setHgrow(call, Priority.ALWAYS);

    root.setCenter(numPad);
    primaryStage.setScene(scene);
    primaryStage.show();
}

From source file:Main.java

/**
 * Find the smallest area that contains multiples rect defined by width & height
 *
 * @param width// ww w  . ja  va  2  s .c o m
 * @param height
 * @param num
 * @return
 */
public static Point getSmallestTextureSize(final int width, final int height, final int num,
        final int maxTextureSize, final boolean forcePo2) {
    int minWidth = 0;
    int minHeight = 0;
    int minArea = Integer.MAX_VALUE;
    for (int row = 1; row <= num; row++) {
        int col = (int) Math.ceil((float) num / (float) row);
        int po2Width = forcePo2 ? getNextPO2(col * width) : col * width;
        int po2Height = forcePo2 ? getNextPO2(row * height) : row * height;
        int area = po2Width * po2Height;
        if (area < minArea && po2Width <= maxTextureSize) {
            minArea = area;
            minWidth = po2Width;
            minHeight = (po2Height < maxTextureSize) ? po2Height : maxTextureSize;
        }
    }

    return new Point(minWidth, minHeight);
}

From source file:edu.umbc.cs.maple.utils.WekaUtils.java

/** Take a certain percentage of a set of instances.
 * @param instances/*from  w w  w .j  a  v  a2 s  . c om*/
 * @param percentage
 * @return a reduced set of instances according to the given percentage
 */
public static Instances trimInstances(Instances instances, double percentage) {
    int numInstancesToKeep = (int) Math.ceil(instances.numInstances() * percentage);
    return trimInstances(instances, numInstancesToKeep);
}

From source file:com.facebook.presto.operator.aggregation.ApproximateUtils.java

public static String formatApproximateResult(double mean, double error, double confidence, boolean integral) {
    double zScore;
    try {/*from   w  w  w  . j a v a2s  .c  o m*/
        zScore = NORMAL_DISTRIBUTION.inverseCumulativeProbability((1 + confidence) / 2);
    } catch (OutOfRangeException e) {
        throw Throwables.propagate(e);
    }

    StringBuilder builder = new StringBuilder();
    if (integral) {
        builder.append((long) mean);
    } else {
        builder.append(mean);
    }
    builder.append(" +/- ");
    if (integral) {
        builder.append((long) Math.ceil(zScore * error));
    } else {
        builder.append(zScore * error);
    }
    return builder.toString();
}

From source file:Main.java

private static <T> Set<T> _asSet(T[] a, boolean makeImmutable) {
    int count = (a != null) ? a.length : 0;

    Set<T> outSet;/*from w  w w .j  a  va  2s .  c om*/

    if (count == 0) {
        outSet = Collections.emptySet();
    } else {
        if (count == 1) {
            outSet = Collections.singleton(a[0]);
        } else {
            // make the initial size big enough that we don't have to rehash to fit the array, for
            // the .75 load factor we pass in
            int initialSize = (int) Math.ceil(count / .75d);

            outSet = new HashSet<T>(initialSize, .75f);

            for (int i = 0; i < count; i++) {
                outSet.add(a[i]);
            }

            if (makeImmutable) {
                outSet = Collections.unmodifiableSet(outSet);
            }
        }
    }

    return outSet;
}

From source file:eu.amidst.core.inference.ImportanceSamplingExperiments.java

private static Assignment randomEvidence(long seed, double evidenceRatio, BayesianNetwork bn,
        Variable varInterest) throws UnsupportedOperationException {

    if (evidenceRatio <= 0 || evidenceRatio >= 1) {
        throw new UnsupportedOperationException("Error: invalid ratio");
    }//from   w ww  .  ja v  a  2  s .  c o m

    int numVariables = bn.getVariables().getNumberOfVars();

    Random random = new Random(seed); //1823716125
    int numVarEvidence = (int) Math.ceil(numVariables * evidenceRatio); // Evidence on 20% of variables
    //numVarEvidence = 0;
    //List<Variable> varEvidence = new ArrayList<>(numVarEvidence);
    double[] evidence = new double[numVarEvidence];
    Variable aux;
    HashMapAssignment assignment = new HashMapAssignment(2);

    int[] indexesEvidence = new int[numVarEvidence + 1];
    indexesEvidence[0] = varInterest.getVarID();
    //if (Main.VERBOSE) System.out.println(variable.getVarID());

    if (Main.VERBOSE)
        System.out.println("Evidence:");
    for (int k = 0; k < numVarEvidence; k++) {
        int varIndex = -1;
        do {
            varIndex = random.nextInt(bn.getNumberOfVars());
            //if (Main.VERBOSE) System.out.println(varIndex);
            aux = bn.getVariables().getVariableById(varIndex);

            double thisEvidence;
            if (aux.isMultinomial()) {
                thisEvidence = random.nextInt(aux.getNumberOfStates());
            } else {
                thisEvidence = random.nextGaussian();
            }
            evidence[k] = thisEvidence;

        } while (ArrayUtils.contains(indexesEvidence, varIndex));

        indexesEvidence[k + 1] = varIndex;
        //if (Main.VERBOSE) System.out.println(Arrays.toString(indexesEvidence));
        if (Main.VERBOSE)
            System.out.println("Variable " + aux.getName() + " = " + evidence[k]);

        assignment.setValue(aux, evidence[k]);
    }
    if (Main.VERBOSE)
        System.out.println();

    return assignment;
}