Search in sources :

Example 41 with ShortBuffer

use of java.nio.ShortBuffer in project libgdx by libgdx.

the class BufferUtilsTest method create.

@Override
public void create() {
    //Not emulated in gwt
    //ByteBuffer bytebuffer = BufferUtils.newUnsafeByteBuffer(1000 * 1000);
    //BufferUtils.disposeUnsafeByteBuffer(bytebuffer);
    ByteBuffer bb = BufferUtils.newByteBuffer(8);
    CharBuffer cb = BufferUtils.newCharBuffer(8);
    ShortBuffer sb = BufferUtils.newShortBuffer(8);
    IntBuffer ib = BufferUtils.newIntBuffer(8);
    LongBuffer lb = BufferUtils.newLongBuffer(8);
    FloatBuffer fb = BufferUtils.newFloatBuffer(8);
    DoubleBuffer db = BufferUtils.newDoubleBuffer(8);
    bb.position(4);
    BufferUtils.copy(new byte[] { 1, 2, 3, 4 }, 0, bb, 4);
    checkInt(bb.get(), 1);
    checkInt(bb.get(), 2);
    checkInt(bb.get(), 3);
    checkInt(bb.get(), 4);
    cb.position(4);
    BufferUtils.copy(new char[] { 1, 2, 3, 4 }, 0, cb, 4);
    checkInt(cb.get(), 1);
    checkInt(cb.get(), 2);
    checkInt(cb.get(), 3);
    checkInt(cb.get(), 4);
    cb.position(0);
    BufferUtils.copy(new char[] { 5, 6, 7, 8 }, 1, cb, 3);
    checkInt(cb.get(), 6);
    checkInt(cb.get(), 7);
    checkInt(cb.get(), 8);
    sb.position(4);
    BufferUtils.copy(new short[] { 1, 2, 3, 4 }, 0, sb, 4);
    checkInt(sb.get(), 1);
    checkInt(sb.get(), 2);
    checkInt(sb.get(), 3);
    checkInt(sb.get(), 4);
    sb.position(0);
    BufferUtils.copy(new short[] { 5, 6, 7, 8 }, 1, sb, 3);
    checkInt(sb.get(), 6);
    checkInt(sb.get(), 7);
    checkInt(sb.get(), 8);
    ib.position(4);
    BufferUtils.copy(new int[] { 1, 2, 3, 4 }, 0, ib, 4);
    checkInt(ib.get(), 1);
    checkInt(ib.get(), 2);
    checkInt(ib.get(), 3);
    checkInt(ib.get(), 4);
    ib.position(0);
    BufferUtils.copy(new int[] { 5, 6, 7, 8 }, 1, ib, 3);
    checkInt(ib.get(), 6);
    checkInt(ib.get(), 7);
    checkInt(ib.get(), 8);
    lb.position(4);
    BufferUtils.copy(new long[] { 1, 2, 3, 4 }, 0, lb, 4);
    checkInt(lb.get(), 1);
    checkInt(lb.get(), 2);
    checkInt(lb.get(), 3);
    checkInt(lb.get(), 4);
    lb.position(0);
    BufferUtils.copy(new long[] { 5, 6, 7, 8 }, 1, lb, 3);
    checkInt(lb.get(), 6);
    checkInt(lb.get(), 7);
    checkInt(lb.get(), 8);
    fb.position(4);
    BufferUtils.copy(new float[] { 1, 2, 3, 4 }, 0, fb, 4);
    checkFloat(fb.get(), 1);
    checkFloat(fb.get(), 2);
    checkFloat(fb.get(), 3);
    checkFloat(fb.get(), 4);
    fb.position(0);
    BufferUtils.copy(new float[] { 5, 6, 7, 8 }, 1, fb, 3);
    checkFloat(fb.get(), 6);
    checkFloat(fb.get(), 7);
    checkFloat(fb.get(), 8);
    if (Gdx.app.getType() != ApplicationType.WebGL) {
        // gwt throws: NYI: Numbers.doubleToRawLongBits
        db.position(4);
        BufferUtils.copy(new double[] { 1, 2, 3, 4 }, 0, db, 4);
        checkFloat(db.get(), 1);
        checkFloat(db.get(), 2);
        checkFloat(db.get(), 3);
        checkFloat(db.get(), 4);
        db.position(0);
        BufferUtils.copy(new double[] { 5, 6, 7, 8 }, 1, db, 3);
        checkFloat(db.get(), 6);
        checkFloat(db.get(), 7);
        checkFloat(db.get(), 8);
    }
    ByteBuffer bb2 = BufferUtils.newByteBuffer(4);
    bb.position(4);
    BufferUtils.copy(bb, bb2, 4);
    checkInt(bb2.get(), 1);
    checkInt(bb2.get(), 2);
    checkInt(bb2.get(), 3);
    checkInt(bb2.get(), 4);
    bench();
}
Also used : DoubleBuffer(java.nio.DoubleBuffer) LongBuffer(java.nio.LongBuffer) IntBuffer(java.nio.IntBuffer) CharBuffer(java.nio.CharBuffer) FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer) ShortBuffer(java.nio.ShortBuffer)

Example 42 with ShortBuffer

use of java.nio.ShortBuffer in project libgdx by libgdx.

the class BufferUtilsTest method benchShort.

private void benchShort() {
    ShortBuffer sb = BufferUtils.newShortBuffer(1024 * 1024 / 2);
    short[] shorts = new short[1024 * 1024 / 2];
    int len = shorts.length;
    // relative put
    long start = TimeUtils.nanoTime();
    for (int j = 0; j < NUM_MB; j++) {
        sb.clear();
        for (int i = 0; i < len; i++) sb.put(shorts[i]);
    }
    Gdx.app.log("BufferUtilsTest", "ShortBuffer relative put: " + (TimeUtils.nanoTime() - start) / 1000000000.0f);
    // absolute put
    start = TimeUtils.nanoTime();
    for (int j = 0; j < NUM_MB; j++) {
        sb.clear();
        for (int i = 0; i < len; i++) sb.put(i, shorts[i]);
    }
    Gdx.app.log("BufferUtilsTest", "ShortBuffer absolute put: " + (TimeUtils.nanoTime() - start) / 1000000000.0f);
    // bulk put
    start = TimeUtils.nanoTime();
    for (int j = 0; j < NUM_MB; j++) {
        sb.clear();
        sb.put(shorts);
    }
    Gdx.app.log("BufferUtilsTest", "ShortBuffer bulk put: " + (TimeUtils.nanoTime() - start) / 1000000000.0f);
    // JNI put
    start = TimeUtils.nanoTime();
    for (int j = 0; j < NUM_MB; j++) {
        sb.clear();
        BufferUtils.copy(shorts, 0, sb, len);
    }
    Gdx.app.log("BufferUtilsTest", "ShortBuffer native bulk put: " + (TimeUtils.nanoTime() - start) / 1000000000.0f);
}
Also used : ShortBuffer(java.nio.ShortBuffer)

Example 43 with ShortBuffer

use of java.nio.ShortBuffer in project libgdx by libgdx.

the class TriangleRaycastTest method tap.

@Override
public boolean tap(float screenX, float screenY, int count, int button) {
    Ray ray = camera.getPickRay(screenX, screenY);
    rayFrom.set(ray.origin);
    rayTo.set(ray.direction).scl(100).add(rayFrom);
    // Clean the callback object for reuse.
    triangleRaycastCallback.setHitFraction(1);
    triangleRaycastCallback.clearReport();
    triangleRaycastCallback.setFrom(rayFrom);
    triangleRaycastCallback.setTo(rayTo);
    // Ray casting is performed directly on the collision shape.
    // The callback specifies the intersected MeshPart as well as triangle.
    triangleShape.performRaycast(triangleRaycastCallback, rayFrom, rayTo);
    int currentTriangleIndex = triangleRaycastCallback.triangleIndex;
    int currentPartId = triangleRaycastCallback.partId;
    if (currentTriangleIndex == -1 || currentPartId == -1) {
        // No intersection was found.
        return false;
    }
    // Get the position coordinates of the vertices belonging to intersected triangle.
    Mesh mesh = model.meshParts.get(currentPartId).mesh;
    FloatBuffer verticesBuffer = mesh.getVerticesBuffer();
    ShortBuffer indicesBuffer = mesh.getIndicesBuffer();
    int posOffset = mesh.getVertexAttributes().findByUsage(VertexAttributes.Usage.Position).offset / 4;
    int vertexSize = mesh.getVertexSize() / 4;
    int currentTriangleFirstVertexIndex = currentTriangleIndex * 3;
    // Store the three vertices belonging to the selected triangle.
    for (int i = 0; i < 3; i++) {
        int currentVertexIndex = indicesBuffer.get(currentTriangleFirstVertexIndex + i);
        int j = currentVertexIndex * vertexSize + posOffset;
        float x = verticesBuffer.get(j++);
        float y = verticesBuffer.get(j++);
        float z = verticesBuffer.get(j);
        selectedTriangleVertices[i].set(x, y, z);
    }
    return true;
}
Also used : Mesh(com.badlogic.gdx.graphics.Mesh) FloatBuffer(java.nio.FloatBuffer) Ray(com.badlogic.gdx.math.collision.Ray) ShortBuffer(java.nio.ShortBuffer)

Example 44 with ShortBuffer

use of java.nio.ShortBuffer in project bigbluebutton by bigbluebutton.

the class Frame method createIndexer.

/** Returns an {@link Indexer} for the <i>i</i>th image plane. */
public <I extends Indexer> I createIndexer(boolean direct, int i) {
    long[] sizes = { imageHeight, imageWidth, imageChannels };
    long[] strides = { imageStride, imageChannels, 1 };
    Buffer buffer = image[i];
    Object array = buffer.hasArray() ? buffer.array() : null;
    switch(imageDepth) {
        case DEPTH_UBYTE:
            return array != null ? (I) UByteIndexer.create((byte[]) array, sizes, strides) : direct ? (I) UByteIndexer.create((ByteBuffer) buffer, sizes, strides) : (I) UByteIndexer.create(new BytePointer((ByteBuffer) buffer), sizes, strides, false);
        case DEPTH_BYTE:
            return array != null ? (I) ByteIndexer.create((byte[]) array, sizes, strides) : direct ? (I) ByteIndexer.create((ByteBuffer) buffer, sizes, strides) : (I) ByteIndexer.create(new BytePointer((ByteBuffer) buffer), sizes, strides, false);
        case DEPTH_USHORT:
            return array != null ? (I) UShortIndexer.create((short[]) array, sizes, strides) : direct ? (I) UShortIndexer.create((ShortBuffer) buffer, sizes, strides) : (I) UShortIndexer.create(new ShortPointer((ShortBuffer) buffer), sizes, strides, false);
        case DEPTH_SHORT:
            return array != null ? (I) ShortIndexer.create((short[]) array, sizes, strides) : direct ? (I) ShortIndexer.create((ShortBuffer) buffer, sizes, strides) : (I) ShortIndexer.create(new ShortPointer((ShortBuffer) buffer), sizes, strides, false);
        case DEPTH_INT:
            return array != null ? (I) IntIndexer.create((int[]) array, sizes, strides) : direct ? (I) IntIndexer.create((IntBuffer) buffer, sizes, strides) : (I) IntIndexer.create(new IntPointer((IntBuffer) buffer), sizes, strides, false);
        case DEPTH_LONG:
            return array != null ? (I) LongIndexer.create((long[]) array, sizes, strides) : direct ? (I) LongIndexer.create((LongBuffer) buffer, sizes, strides) : (I) LongIndexer.create(new LongPointer((LongBuffer) buffer), sizes, strides, false);
        case DEPTH_FLOAT:
            return array != null ? (I) FloatIndexer.create((float[]) array, sizes, strides) : direct ? (I) FloatIndexer.create((FloatBuffer) buffer, sizes, strides) : (I) FloatIndexer.create(new FloatPointer((FloatBuffer) buffer), sizes, strides, false);
        case DEPTH_DOUBLE:
            return array != null ? (I) DoubleIndexer.create((double[]) array, sizes, strides) : direct ? (I) DoubleIndexer.create((DoubleBuffer) buffer, sizes, strides) : (I) DoubleIndexer.create(new DoublePointer((DoubleBuffer) buffer), sizes, strides, false);
        default:
            assert false;
    }
    return null;
}
Also used : FloatBuffer(java.nio.FloatBuffer) DoubleBuffer(java.nio.DoubleBuffer) ShortBuffer(java.nio.ShortBuffer) ByteBuffer(java.nio.ByteBuffer) IntBuffer(java.nio.IntBuffer) Buffer(java.nio.Buffer) LongBuffer(java.nio.LongBuffer) DoubleBuffer(java.nio.DoubleBuffer) LongBuffer(java.nio.LongBuffer) BytePointer(org.bytedeco.javacpp.BytePointer) DoublePointer(org.bytedeco.javacpp.DoublePointer) FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer) ShortPointer(org.bytedeco.javacpp.ShortPointer) LongPointer(org.bytedeco.javacpp.LongPointer) FloatPointer(org.bytedeco.javacpp.FloatPointer) IntBuffer(java.nio.IntBuffer) IntPointer(org.bytedeco.javacpp.IntPointer) ShortBuffer(java.nio.ShortBuffer)

Example 45 with ShortBuffer

use of java.nio.ShortBuffer in project processing by processing.

the class PGL method allocateShortBuffer.

protected static ShortBuffer allocateShortBuffer(short[] arr) {
    if (USE_DIRECT_BUFFERS) {
        ShortBuffer buf = allocateDirectShortBuffer(arr.length);
        buf.put(arr);
        buf.position(0);
        return buf;
    } else {
        return ShortBuffer.wrap(arr);
    }
}
Also used : ShortBuffer(java.nio.ShortBuffer)

Aggregations

ShortBuffer (java.nio.ShortBuffer)227 ByteBuffer (java.nio.ByteBuffer)78 FloatBuffer (java.nio.FloatBuffer)54 IntBuffer (java.nio.IntBuffer)45 DoubleBuffer (java.nio.DoubleBuffer)23 LongBuffer (java.nio.LongBuffer)16 Test (org.junit.Test)14 Buffer (java.nio.Buffer)11 BufferOverflowException (java.nio.BufferOverflowException)11 CharBuffer (java.nio.CharBuffer)11 VertexBuffer (com.jme3.scene.VertexBuffer)8 BufferUnderflowException (java.nio.BufferUnderflowException)7 BytePointer (org.bytedeco.javacpp.BytePointer)7 IndexBuffer (com.jme3.scene.mesh.IndexBuffer)6 IOException (java.io.IOException)5 Vector3f (com.jme3.math.Vector3f)4 ArrayList (java.util.ArrayList)4 Bitmap (android.graphics.Bitmap)3 Mesh (com.jme3.scene.Mesh)3 InvalidMarkException (java.nio.InvalidMarkException)3