Example usage for java.nio ByteBuffer asFloatBuffer

List of usage examples for java.nio ByteBuffer asFloatBuffer

Introduction

In this page you can find the example usage for java.nio ByteBuffer asFloatBuffer.

Prototype

public abstract FloatBuffer asFloatBuffer();

Source Link

Document

Returns a float buffer which is based on the remaining content of this byte buffer.

Usage

From source file:Main.java

public static FloatBuffer makeFloatBuffer(int length) {
    final int floatSize = Float.SIZE / 8;
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(length * floatSize);
    byteBuffer.order(ByteOrder.nativeOrder());
    FloatBuffer floatBuffer = byteBuffer.asFloatBuffer();
    floatBuffer.position(0);//from  w w w.  ja va  2  s.  co  m
    return floatBuffer;
}

From source file:Main.java

public static FloatBuffer makeFloatBuffer(float[] array) {
    final int floatSize = Float.SIZE / 8;
    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(array.length * floatSize);
    byteBuffer.order(ByteOrder.nativeOrder());
    FloatBuffer floatBuffer = byteBuffer.asFloatBuffer();
    floatBuffer.put(array);//w w  w  .  java  2 s .c om
    floatBuffer.position(0);
    return floatBuffer;
}

From source file:org.bimserver.utils.BinUtils.java

public static byte[] floatToByteArray(Float inFloat) {
    byte[] bArray = new byte[4];
    ByteBuffer bBuffer = ByteBuffer.wrap(bArray);
    FloatBuffer lBuffer = bBuffer.asFloatBuffer();
    lBuffer.put(inFloat);//from   w  w  w.  java2s  . c  o m
    return bArray;
}

From source file:de.ailis.threedee.utils.BufferUtils.java

/**
 * Creates a direct float buffer with native byte order.
 *
 * @param size//from   w  w  w.  ja v a 2 s. com
 *            The data
 * @return The created direct float buffer
 */

public static FloatBuffer createDirectFloatBuffer(final int size) {
    final ByteBuffer tmp = ByteBuffer.allocateDirect(size * FLOAT_BYTES);
    tmp.order(ByteOrder.nativeOrder());
    return tmp.asFloatBuffer();
}

From source file:de.ailis.threedee.utils.BufferUtils.java

/**
 * Converts the specified float buffer to native endian and returns this new
 * buffer. If buffer is already in correct endian format then it is returned
 * right away./*from  w w w  .  j a  v  a 2s.c  o  m*/
 *
 * @param buffer
 *            The float buffer to convert
 * @return The converted float buffer or the source buffer if no conversion
 *         is needed
 */

public static FloatBuffer convertToNativeEndian(final FloatBuffer buffer) {
    if (buffer.order() == ByteOrder.nativeOrder())
        return buffer;

    if (log.isTraceEnabled())
        log.trace("Converting endianess of " + buffer.capacity() + " floats");

    final ByteBuffer bytes = ByteBuffer.allocateDirect(buffer.capacity());
    bytes.order(ByteOrder.nativeOrder());
    final FloatBuffer floats = bytes.asFloatBuffer();
    floats.put(buffer).rewind();
    return floats;
}

From source file:org.bimserver.collada.SupportFunctions.java

public static void printMatrix(PrintWriter out, GeometryInfo geometryInfo) {
    ByteBuffer transformation = ByteBuffer.wrap(geometryInfo.getTransformation());
    transformation.order(ByteOrder.LITTLE_ENDIAN);
    FloatBuffer floatBuffer = transformation.asFloatBuffer();
    // Prepare to create the transform matrix.
    float[] matrix = new float[16];
    // Add the first 16 values of the buffer.
    for (int i = 0; i < matrix.length; i++)
        matrix[i] = floatBuffer.get();/*from w  ww . j  a  v  a2  s. c o  m*/
    // Switch from column-major (x.x ... x.y ... x.z ... 0 ...) to row-major orientation (x.x x.y x.z 0 ...)?
    matrix = Matrix.changeOrientation(matrix);
    // List all 16 elements of the matrix as a single space-delimited String object.
    if (!matrix.equals(identity))
        out.println("    <matrix>" + floatArrayToSpaceDelimitedString(matrix) + "</matrix>");
}

From source file:org.mrgeo.data.raster.RasterWritable.java

private static byte[] rasterToBytes(final Raster raster) {
    final int datatype = raster.getTransferType();

    byte[] pixels;

    final Object elements = raster.getDataElements(raster.getMinX(), raster.getMinY(), raster.getWidth(),
            raster.getHeight(), null);/* w ww  .  j a v  a  2 s  .  co m*/

    switch (datatype) {
    case DataBuffer.TYPE_BYTE: {
        pixels = (byte[]) elements;
        break;
    }
    case DataBuffer.TYPE_FLOAT: {
        final float[] floatElements = (float[]) elements;

        pixels = new byte[floatElements.length * RasterUtils.FLOAT_BYTES];

        final ByteBuffer bytebuff = ByteBuffer.wrap(pixels);
        final FloatBuffer floatbuff = bytebuff.asFloatBuffer();
        floatbuff.put(floatElements);

        break;
    }
    case DataBuffer.TYPE_DOUBLE: {
        final double[] doubleElements = (double[]) elements;

        pixels = new byte[doubleElements.length * RasterUtils.DOUBLE_BYTES];

        final ByteBuffer bytebuff = ByteBuffer.wrap(pixels);
        final DoubleBuffer doubleBuff = bytebuff.asDoubleBuffer();
        doubleBuff.put(doubleElements);

        break;
    }
    case DataBuffer.TYPE_INT: {
        final int[] intElements = (int[]) elements;

        pixels = new byte[intElements.length * RasterUtils.INT_BYTES];

        final ByteBuffer bytebuff = ByteBuffer.wrap(pixels);
        final IntBuffer intBuff = bytebuff.asIntBuffer();
        intBuff.put(intElements);

        break;
    }
    case DataBuffer.TYPE_SHORT:
    case DataBuffer.TYPE_USHORT: {
        final short[] shortElements = (short[]) elements;

        pixels = new byte[shortElements.length * RasterUtils.SHORT_BYTES];

        final ByteBuffer bytebuff = ByteBuffer.wrap(pixels);
        final ShortBuffer shortbuff = bytebuff.asShortBuffer();
        shortbuff.put(shortElements);

        break;
    }
    default:
        throw new RasterWritableException("Error trying to append raster.  Bad raster data type");
    }

    return pixels;
}

From source file:Triangle.java

public Triangle() {
    int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
    int fragmentShader = loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
    mProgram = GLES20.glCreateProgram();
    GLES20.glAttachShader(mProgram, vertexShader);
    GLES20.glAttachShader(mProgram, fragmentShader);
    GLES20.glLinkProgram(mProgram);/*  w ww  .  jav a2  s .  c o  m*/

    ByteBuffer bb = ByteBuffer.allocateDirect(triangleCoords.length * 4);
    bb.order(ByteOrder.nativeOrder());

    vertexBuffer = bb.asFloatBuffer();
    vertexBuffer.put(triangleCoords);
    vertexBuffer.position(0);
}

From source file:com.metamx.collections.spatial.search.RectangularBound.java

@Override
public byte[] getCacheKey() {
    ByteBuffer minCoordsBuffer = ByteBuffer.allocate(minCoords.length * Floats.BYTES);
    minCoordsBuffer.asFloatBuffer().put(minCoords);
    final byte[] minCoordsCacheKey = minCoordsBuffer.array();

    ByteBuffer maxCoordsBuffer = ByteBuffer.allocate(maxCoords.length * Floats.BYTES);
    maxCoordsBuffer.asFloatBuffer().put(maxCoords);
    final byte[] maxCoordsCacheKey = maxCoordsBuffer.array();

    final ByteBuffer cacheKey = ByteBuffer.allocate(1 + minCoordsCacheKey.length + maxCoordsCacheKey.length)
            .put(minCoordsCacheKey).put(maxCoordsCacheKey).put(CACHE_TYPE_ID);
    return cacheKey.array();
}

From source file:io.druid.query.aggregation.HistogramAggregatorFactory.java

@Override
public byte[] getCacheKey() {
    byte[] fieldNameBytes = StringUtils.toUtf8(fieldName);
    ByteBuffer buf = ByteBuffer.allocate(1 + fieldNameBytes.length + Floats.BYTES * breaks.length)
            .put(CACHE_TYPE_ID).put(fieldNameBytes).put((byte) 0xFF);
    buf.asFloatBuffer().put(breaks);

    return buf.array();
}