Search in sources :

Example 36 with ShortBuffer

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

the class ShortBufferTest method testEquals.

public void testEquals() {
    // equal to self
    assertTrue(buf.equals(buf));
    ShortBuffer readonly = buf.asReadOnlyBuffer();
    assertTrue(buf.equals(readonly));
    ShortBuffer duplicate = buf.duplicate();
    assertTrue(buf.equals(duplicate));
    // always false, if type mismatch
    assertFalse(buf.equals(Boolean.TRUE));
    assertTrue(buf.capacity() > 5);
    buf.limit(buf.capacity()).position(0);
    readonly.limit(readonly.capacity()).position(1);
    assertFalse(buf.equals(readonly));
    buf.limit(buf.capacity() - 1).position(0);
    duplicate.limit(duplicate.capacity()).position(0);
    assertFalse(buf.equals(duplicate));
}
Also used : ShortBuffer(java.nio.ShortBuffer)

Example 37 with ShortBuffer

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

the class ReadOnlyShortBufferTest method testPutShortBuffer.

public void testPutShortBuffer() {
    ShortBuffer other = ShortBuffer.allocate(1);
    try {
        buf.put(other);
        //$NON-NLS-1$
        fail("Should throw ReadOnlyBufferException");
    } catch (ReadOnlyBufferException e) {
    // expected
    }
    try {
        buf.put((ShortBuffer) null);
        //$NON-NLS-1$
        fail("Should throw ReadOnlyBufferException");
    } catch (ReadOnlyBufferException e) {
    // expected
    }
    try {
        buf.put(buf);
        //$NON-NLS-1$
        fail("Should throw ReadOnlyBufferException");
    } catch (ReadOnlyBufferException e) {
    // expected
    }
}
Also used : ReadOnlyBufferException(java.nio.ReadOnlyBufferException) ShortBuffer(java.nio.ShortBuffer)

Example 38 with ShortBuffer

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

the class ReadOnlyShortBufferTest method testHashCode.

public void testHashCode() {
    ShortBuffer duplicate = buf.duplicate();
    assertEquals(buf.hashCode(), duplicate.hashCode());
}
Also used : ShortBuffer(java.nio.ShortBuffer)

Example 39 with ShortBuffer

use of java.nio.ShortBuffer in project platform_frameworks_base by android.

the class IpUtils method checksum.

/**
     * Performs an IP checksum (used in IP header and across UDP
     * payload) on the specified portion of a ByteBuffer.  The seed
     * allows the checksum to commence with a specified value.
     */
private static int checksum(ByteBuffer buf, int seed, int start, int end) {
    int sum = seed;
    final int bufPosition = buf.position();
    // set position of original ByteBuffer, so that the ShortBuffer
    // will be correctly initialized
    buf.position(start);
    ShortBuffer shortBuf = buf.asShortBuffer();
    // re-set ByteBuffer position
    buf.position(bufPosition);
    final int numShorts = (end - start) / 2;
    for (int i = 0; i < numShorts; i++) {
        sum += intAbs(shortBuf.get(i));
    }
    start += numShorts * 2;
    // see if a singleton byte remains
    if (end != start) {
        short b = buf.get(start);
        // make it unsigned
        if (b < 0) {
            b += 256;
        }
        sum += b * 256;
    }
    sum = ((sum >> 16) & 0xFFFF) + (sum & 0xFFFF);
    sum = ((sum + ((sum >> 16) & 0xFFFF)) & 0xFFFF);
    int negated = ~sum;
    return intAbs((short) negated);
}
Also used : ShortBuffer(java.nio.ShortBuffer)

Example 40 with ShortBuffer

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

the class Mesh method calculateRadiusSquared.

/** Calculates the squared radius of the bounding sphere around the specified center for the specified part.
	 * @param centerX The X coordinate of the center of the bounding sphere
	 * @param centerY The Y coordinate of the center of the bounding sphere
	 * @param centerZ The Z coordinate of the center of the bounding sphere
	 * @param offset the start index of the part.
	 * @param count the amount of indices the part contains.
	 * @return the squared radius of the bounding sphere. */
public float calculateRadiusSquared(final float centerX, final float centerY, final float centerZ, int offset, int count, final Matrix4 transform) {
    int numIndices = getNumIndices();
    if (offset < 0 || count < 1 || offset + count > numIndices)
        throw new GdxRuntimeException("Not enough indices");
    final FloatBuffer verts = vertices.getBuffer();
    final ShortBuffer index = indices.getBuffer();
    final VertexAttribute posAttrib = getVertexAttribute(Usage.Position);
    final int posoff = posAttrib.offset / 4;
    final int vertexSize = vertices.getAttributes().vertexSize / 4;
    final int end = offset + count;
    float result = 0;
    switch(posAttrib.numComponents) {
        case 1:
            for (int i = offset; i < end; i++) {
                final int idx = index.get(i) * vertexSize + posoff;
                tmpV.set(verts.get(idx), 0, 0);
                if (transform != null)
                    tmpV.mul(transform);
                final float r = tmpV.sub(centerX, centerY, centerZ).len2();
                if (r > result)
                    result = r;
            }
            break;
        case 2:
            for (int i = offset; i < end; i++) {
                final int idx = index.get(i) * vertexSize + posoff;
                tmpV.set(verts.get(idx), verts.get(idx + 1), 0);
                if (transform != null)
                    tmpV.mul(transform);
                final float r = tmpV.sub(centerX, centerY, centerZ).len2();
                if (r > result)
                    result = r;
            }
            break;
        case 3:
            for (int i = offset; i < end; i++) {
                final int idx = index.get(i) * vertexSize + posoff;
                tmpV.set(verts.get(idx), verts.get(idx + 1), verts.get(idx + 2));
                if (transform != null)
                    tmpV.mul(transform);
                final float r = tmpV.sub(centerX, centerY, centerZ).len2();
                if (r > result)
                    result = r;
            }
            break;
    }
    return result;
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) FloatBuffer(java.nio.FloatBuffer) 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