Example usage for com.google.common.math DoubleMath roundToInt

List of usage examples for com.google.common.math DoubleMath roundToInt

Introduction

In this page you can find the example usage for com.google.common.math DoubleMath roundToInt.

Prototype

@GwtIncompatible("#roundIntermediate")
public static int roundToInt(double x, RoundingMode mode) 

Source Link

Document

Returns the int value that is equal to x rounded with the specified rounding mode, if possible.

Usage

From source file:org.terasology.polyworld.sampling.PoissonDiscTestView.java

public static void main(String[] args) {
    //      int rows = DoubleMath.roundToInt(area.height() / cellSize, RoundingMode.HALF_UP);
    //      int cols = DoubleMath.roundToInt(area.width() / cellSize, RoundingMode.HALF_UP);

    Rect2f area = Rect2f.createFromMinAndSize(30, 10, 512, 256);
    int numSites = DoubleMath.roundToInt(area.area() * graphDensity / 1000, RoundingMode.HALF_UP);

    logger.info("START GRID");
    PoissonDiscSampling sampling = new PoissonDiscSampling();
    List<Vector2f> points = sampling.create(area, numSites);
    logger.info("END GRID");

    System.out.println("SHOULD BE: " + numSites);
    System.out.println("REALITY  : " + points.size());

    JFrame frame = new JFrame();
    frame.setSize(1200, 600);//from  w ww. jav a  2 s.c  o m
    frame.setLocationRelativeTo(null);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(new JPanel() {
        private static final long serialVersionUID = 7926239205123946814L;

        @Override
        protected void paintComponent(Graphics g1) {
            super.paintComponent(g1);
            int scale = 2;

            Graphics2D g = (Graphics2D) g1;
            g.setColor(Color.GRAY);
            g.setStroke(new BasicStroke(0f));
            g.setTransform(AffineTransform.getScaleInstance(scale, scale));

            drawDebug(area, g);

            g.setColor(Color.BLACK);
            g.setStroke(new BasicStroke(1f));
            for (Vector2f pt : points) {
                g.draw(new Line2D.Float(pt.getX(), pt.getY(), pt.getX(), pt.getY()));
            }
        }

        private void drawDebug(Rect2f bounds, Graphics2D g) {
            g.setColor(Color.LIGHT_GRAY);
            Vector2i dims = sampling.getGridDimensions(bounds, numSites);
            int cols = dims.getX();
            int rows = dims.getY();

            float cellWidth = (float) bounds.width() / cols;
            float cellHeight = (float) bounds.height() / rows;

            g.translate(bounds.minX(), bounds.minY());
            for (int i = 0; i <= cols; i++) {
                g.drawLine((int) (i * cellWidth), 0, (int) (i * cellWidth), (int) bounds.height());
            }
            for (int i = 0; i <= rows; i++) {
                g.drawLine(0, (int) (i * cellHeight), (int) bounds.width(), (int) (i * cellHeight));
            }
            g.translate(-bounds.minX(), -bounds.minY());
        }
    });
    frame.setVisible(true);
}

From source file:ru.aifgi.recognizer.model.preprosessing.OtsuAlgorithm.java

private static int[] computeHistogram(final double[][] image) {
    final int[] histogram = new int[256];
    for (final double[] line : image) {
        for (final double value : line) {
            final int brightness = DoubleMath.roundToInt(value, RoundingMode.HALF_UP);
            ++histogram[brightness];//from   ww  w  .j  av a2s. c  o m
        }
    }
    return histogram;
}

From source file:ru.aifgi.recognizer.model.ImageUtil.java

public static void writeImage(final double[][] brightnesses, final File file) {
    final int width = brightnesses.length;
    final int height = brightnesses[0].length;
    final BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    for (int i = 0; i < width; ++i) {
        for (int j = 0; j < height; ++j) {
            final int rgb = 0xFFFF * DoubleMath.roundToInt(1 - brightnesses[i][j], RoundingMode.HALF_UP);
            bufferedImage.setRGB(i, j, rgb);
        }/*w  ww. ja  va2 s.  com*/
    }
    writeImage(bufferedImage, file);
}

From source file:org.terasology.utilities.procedural.HeightmapFileReader.java

public static void convertFileToTexture() throws IOException {
    float[][] heightmap = readFile();

    double scaleFactor = 256 * 256 * 12.8f;

    //        Slick's PNGDecoder does not support 16 bit textures

    //        BufferedImage image = new BufferedImage(512, 512, BufferedImage.TYPE_USHORT_GRAY);
    //        DataBufferUShort buffer = (DataBufferUShort) image.getRaster().getDataBuffer();
    //        scaleFactor *= 256.0f;

    //        Slick's PNGDecoder does not support grayscale textures

    //        BufferedImage image = new BufferedImage(512, 512, BufferedImage.TYPE_BYTE_GRAY);
    //        DataBufferByte buffer = (DataBufferByte) image.getRaster().getDataBuffer();

    BufferedImage image = new BufferedImage(512, 512, BufferedImage.TYPE_INT_RGB);
    DataBufferInt buffer = (DataBufferInt) image.getRaster().getDataBuffer();

    for (int x = 0; x < 512; x++) {
        for (int z = 0; z < 512; z++) {
            double doubleVal = heightmap[x][z] * scaleFactor;
            int val = DoubleMath.roundToInt(doubleVal, RoundingMode.HALF_UP);
            buffer.setElem(z * 512 + x, val);
        }/*  w w w  .jav a  2  s.co m*/
    }

    ImageIO.write(image, "png", new File("platec_heightmap.png"));
}

From source file:de.fhg.igd.iva.colormaps.impl.WainerAndFrancolini.java

@Override
public Color getColor(double x, double y) {

    if (colorMaps == null)
        initializeColorMap();/* w  w  w.ja  v  a2s.  com*/

    checkRanges(x, y);

    double df = 1 / 3.0;

    int indexX = DoubleMath.roundToInt(x / df, RoundingMode.FLOOR);
    int indexY = DoubleMath.roundToInt(y / df, RoundingMode.FLOOR);
    double fx = 3.0 * (x % df);
    double fy = 3.0 * (y % df);

    // explicitly test for this corner cases -> rounding errors
    if (x == 1.0) {
        indexX = 2;
        fx = 1.0;
    }

    if (y == 1.0) {
        indexY = 2;
        fy = 1.0;
    }

    return colorMaps[indexX][indexY].getColor(fx, fy);
}

From source file:ru.aifgi.recognizer.model.test.images.ImageUtil.java

public static BufferedImage createBufferedImage(final double[][] brightnesses) {
    final int width = brightnesses.length;
    final int height = brightnesses[0].length;
    final BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    for (int i = 0; i < width; ++i) {
        for (int j = 0; j < height; ++j) {
            bufferedImage.setRGB(i, j, DoubleMath.roundToInt(brightnesses[i][j], RoundingMode.HALF_UP));
        }//from w w w .  j a v a  2s. co m
    }
    return bufferedImage;
}

From source file:com.github.rinde.rinsim.scenario.ScenarioTestUtil.java

public static Scenario create(long seed) {
    final int endTime = 3 * 60 * 60 * 1000;
    Scenario.Builder b = Scenario.builder()
            .addModel(PlaneRoadModel.supplier(new Point(0, 0), new Point(10, 10), SI.KILOMETER,
                    Measure.valueOf(50d, NonSI.KILOMETERS_PER_HOUR)))
            .addModel(DefaultPDPModel.supplier(TimeWindowPolicies.LIBERAL)).addEvents(Collections.nCopies(10,
                    new AddVehicleEvent(-1, VehicleDTO.builder().startPosition(new Point(5, 5)).build())));

    RandomGenerator rng = new MersenneTwister(seed);
    for (int i = 0; i < 20; i++) {
        long announceTime = rng.nextInt(DoubleMath.roundToInt(endTime * .8, RoundingMode.FLOOR));
        b.addEvent(new AddParcelEvent(ParcelDTO
                .builder(new Point(rng.nextDouble() * 10, rng.nextDouble() * 10),
                        new Point(rng.nextDouble() * 10, rng.nextDouble() * 10))
                .orderAnnounceTime(announceTime).pickupTimeWindow(new TimeWindow(announceTime, endTime))
                .deliveryTimeWindow(new TimeWindow(announceTime, endTime)).neededCapacity(0).build()));
    }/*from  w w  w. j  a  v  a2s  . c  o  m*/

    b.addEvent(new TimedEvent(PDPScenarioEvent.TIME_OUT, endTime)).scenarioLength(endTime)
            .stopCondition(new EndTimeStopCondition(endTime));

    b.addEventType(PDPScenarioEvent.ADD_DEPOT);

    return b.build();
}

From source file:org.terasology.rendering.nui.databinding.IntToFloatBinding.java

@Override
public void set(Float value) {
    int val = DoubleMath.roundToInt(value, roundingMode);
    intBinding.set(Integer.valueOf(val));
}

From source file:org.terasology.polyworld.sampling.PoissonDiscSampling.java

protected Vector2i getGridDimensions(Rect2f bounds, int numSites) {

    float ratio = bounds.width() / bounds.height();
    double perRow = Math.sqrt(numSites / ratio);

    int rows = DoubleMath.roundToInt(perRow, RoundingMode.FLOOR);
    int cols = DoubleMath.roundToInt(perRow * ratio, RoundingMode.FLOOR);

    // clamp to a minimum value of 2 to avoid polygons that touch
    // two opposing borders of the bounding rectangle
    rows = Math.max(rows, 2);//w ww  .  j  av a 2 s. com
    cols = Math.max(cols, 2);

    return new Vector2i(cols, rows);
}

From source file:org.amcgala.renderer.Pixel.java

/**
 * Erzeugt einen neuen Pixel an der Stelle (x,y).
 * Die doubles werden entsprechend auf die Integerpositionen des Pixels gerundet.
 *
 * @param x die x-Koordinate des Pixels/*from ww w.j a v  a2 s  . com*/
 * @param y die y-Koordinate des Pixels
 */
public Pixel(double x, double y) {
    this.x = DoubleMath.roundToInt(x, RoundingMode.HALF_DOWN);
    this.y = DoubleMath.roundToInt(y, RoundingMode.HALF_DOWN);
}