Example usage for java.nio FloatBuffer put

List of usage examples for java.nio FloatBuffer put

Introduction

In this page you can find the example usage for java.nio FloatBuffer put.

Prototype

public FloatBuffer put(FloatBuffer src) 

Source Link

Document

Writes all the remaining floats of the src float buffer to this buffer's current position, and increases both buffers' position by the number of floats copied.

Usage

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);
    return bArray;
}

From source file:Main.java

public static FloatBuffer ensureLargeEnough(FloatBuffer buffer, final int required) {
    if (buffer == null || (buffer.remaining() < required)) {
        final int position = (buffer != null ? buffer.position() : 0);
        final FloatBuffer newVerts = createFloatBuffer(position + required);
        if (buffer != null) {
            buffer.rewind();/*from   w  w w .  j av a2 s .  c o m*/
            newVerts.put(buffer);
            newVerts.position(position);
        }
        buffer = newVerts;
    }
    return buffer;
}

From source file:Main.java

public static void copyInternal(final FloatBuffer buf, final int fromPos, final int toPos, final int length) {
    final float[] data = new float[length];
    buf.position(fromPos);/* www  .  j a  va 2 s  .  c o m*/
    buf.get(data);
    buf.position(toPos);
    buf.put(data);
}

From source file:Main.java

public static FloatBuffer setupFloatBuffer(FloatBuffer preBuffer, float[] array) {

    if (preBuffer == null || preBuffer.capacity() < array.length) {
        preBuffer = createFloatBuffer(array.length * 2);
    } else {/*from  ww  w  .j a  v a2s  . c  o  m*/
        preBuffer.clear();
    }
    preBuffer.put(array);
    preBuffer.position(0);
    return preBuffer;
}

From source file:Main.java

public static FloatBuffer createFloatBuffer(final FloatBuffer reuseStore, final float... data) {
    if (data == null) {
        return null;
    }// ww w.j  a va 2s  . c om
    final FloatBuffer buff;
    if (reuseStore == null || reuseStore.capacity() != data.length) {
        buff = createFloatBuffer(data.length);
    } else {
        buff = reuseStore;
        buff.clear();
    }
    buff.clear();
    buff.put(data);
    buff.flip();
    return buff;
}

From source file:Main.java

public static FloatBuffer clone(final FloatBuffer buf) {
    if (buf == null) {
        return null;
    }/*from w  ww .j  a va 2 s  .c o m*/
    buf.rewind();

    final FloatBuffer copy;
    if (buf.isDirect()) {
        copy = createFloatBuffer(buf.limit());
    } else {
        copy = createFloatBufferOnHeap(buf.limit());
    }
    copy.put(buf);

    return copy;
}

From source file:Main.java

public static FloatBuffer clone(final FloatBuffer buf) {
    if (buf == null) {
        return null;
    }//from   ww  w  .  ja  v a2  s  .  co m
    buf.rewind();
    final FloatBuffer copy;
    if (buf.isDirect()) {
        copy = createFloatBuffer(buf.limit());
    } else {
        copy = createFloatBufferOnHeap(buf.limit());
    }
    copy.put(buf);

    return copy;
}

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);//from   ww  w.  j  av 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:ivorius.ivtoolkit.rendering.grid.GridQuadCache.java

protected static <T> GridQuadCache<T> createQuadCacheGreedy(int[] size, float[] scale,
        Function<Pair<BlockCoord, ForgeDirection>, T> mapper) {
    Map<QuadContext<T>, CoordGrid> partialCache = new HashMap<>();

    for (int x = 0; x < size[0]; x++)
        for (int y = 0; y < size[1]; y++)
            for (int z = 0; z < size[2]; z++) {
                BlockCoord coord = new BlockCoord(x, y, z);
                addToCache(partialCache, mapper, UP, coord);
                addToCache(partialCache, mapper, DOWN, coord);
                addToCache(partialCache, mapper, NORTH, coord);
                addToCache(partialCache, mapper, EAST, coord);
                addToCache(partialCache, mapper, SOUTH, coord);
                addToCache(partialCache, mapper, WEST, coord);
            }/*from w  w w.j ava 2 s.  c  o m*/

    Set<Map.Entry<QuadContext<T>, CoordGrid>> quads = partialCache.entrySet();
    GridQuadCache<T> cache = new GridQuadCache<>();
    cache.size = new float[3];
    for (int i = 0; i < 3; i++)
        cache.size[i] = size[i] * scale[i];

    for (Map.Entry<QuadContext<T>, CoordGrid> entry : quads) {
        QuadContext<T> context = entry.getKey();

        int[] sAxes = getCacheAxes(context.direction, size);
        float[] scAxes = getCacheAxes(context.direction, scale);

        QuadCollection mesh = entry.getValue().computeMesh(0, 0, sAxes[1], sAxes[2]);
        FloatBuffer cachedQuadCoords = BufferUtils.createFloatBuffer(mesh.quadCount() * 4);

        float pxAxis = scAxes[1];
        float pzAxis = scAxes[2];

        for (int i = 0; i < mesh.quadCount(); i++) {
            cachedQuadCoords.put(mesh.x1(i) * pxAxis).put(mesh.y1(i) * pzAxis).put((mesh.x2(i) + 1) * pxAxis)
                    .put((mesh.y2(i) + 1) * pzAxis);
        }
        cachedQuadCoords.position(0);

        float zLevel;
        zLevel = (context.direction.offsetX + context.direction.offsetY + context.direction.offsetZ > 0
                ? context.layer + 1
                : context.layer) * scAxes[0];

        cache.cachedQuadLevels
                .add(new CachedQuadLevel<>(zLevel, context.direction, context.t, cachedQuadCoords));
    }

    return cache;
}

From source file:org.smurn.jply.lwjgldemo.LWJGLDemo.java

/**
 * Fill the vertex buffer with the data from the PLY file.
 *//* ww w  .jav  a2s.c  o m*/
private static RectBounds fillVertexBuffer(ElementReader reader, FloatBuffer vertexBuffer) throws IOException {

    // Just go though the vertices and store the coordinates in the buffer.
    Element vertex = reader.readElement();
    RectBounds bounds = new RectBounds();
    while (vertex != null) {
        double x = vertex.getDouble("x");
        double y = vertex.getDouble("y");
        double z = vertex.getDouble("z");
        double nx = vertex.getDouble("nx");
        double ny = vertex.getDouble("ny");
        double nz = vertex.getDouble("nz");
        vertexBuffer.put((float) x);
        vertexBuffer.put((float) y);
        vertexBuffer.put((float) z);
        vertexBuffer.put((float) nx);
        vertexBuffer.put((float) ny);
        vertexBuffer.put((float) nz);
        bounds.addPoint(x, y, z);
        vertex = reader.readElement();
    }
    return bounds;
}