Java Code Examples for java.nio.Buffer#rewind()

The following examples show how to use java.nio.Buffer#rewind() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RagdollUtils.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Test whether the indexed bone has at least one vertex in the specified
 * meshes with a weight greater than the specified threshold.
 *
 * @param boneIndex the index of the bone (≥0)
 * @param targets the meshes to search (not null, no null elements)
 * @param weightThreshold the threshold (≥0, ≤1)
 * @return true if at least 1 vertex found, otherwise false
 */
public static boolean hasVertices(int boneIndex, Mesh[] targets,
        float weightThreshold) {
    for (Mesh mesh : targets) {
        VertexBuffer biBuf = mesh.getBuffer(VertexBuffer.Type.BoneIndex);
        Buffer boneIndices = biBuf.getDataReadOnly();
        FloatBuffer boneWeight
                = (FloatBuffer) mesh.getBuffer(Type.BoneWeight).getData();

        boneIndices.rewind();
        boneWeight.rewind();

        int vertexComponents = mesh.getVertexCount() * 3;
        for (int i = 0; i < vertexComponents; i += 3) {
            int start = i / 3 * 4;
            for (int k = start; k < start + 4; k++) {
                if (readIndex(boneIndices, k) == boneIndex
                        && boneWeight.get(k) >= weightThreshold) {
                    return true;
                }
            }
        }
    }

    return false;
}
 
Example 2
Source File: BaseLoader.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Load an ndarray from a blob
 *
 * @param blob the blob to load from
 * @return the loaded ndarray
 */
@Override
public INDArray load(Blob blob) throws SQLException {
    if (blob == null)
        return null;
    try(InputStream is = blob.getBinaryStream()) {
        ByteBuffer direct = ByteBuffer.allocateDirect((int) blob.length());
        ReadableByteChannel readableByteChannel = Channels.newChannel(is);
        readableByteChannel.read(direct);
        Buffer byteBuffer = (Buffer) direct;
        byteBuffer.rewind();
        return BinarySerde.toArray(direct);
    } catch (Exception e) {
       throw new RuntimeException(e);
    }


}
 
Example 3
Source File: BaseLoader.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * Load an ndarray from a blob
 *
 * @param blob the blob to load from
 * @return the loaded ndarray
 */
@Override
public INDArray load(Blob blob) throws SQLException {
    if (blob == null)
        return null;
    try(InputStream is = blob.getBinaryStream()) {
        ByteBuffer direct = ByteBuffer.allocateDirect((int) blob.length());
        ReadableByteChannel readableByteChannel = Channels.newChannel(is);
        readableByteChannel.read(direct);
        Buffer byteBuffer = (Buffer) direct;
        byteBuffer.rewind();
        return BinarySerde.toArray(direct);
    } catch (Exception e) {
       throw new RuntimeException(e);
    }


}
 
Example 4
Source File: LodGenerator.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void gatherIndexData(Mesh mesh, List<Vertex> vertexLookup) {
    VertexBuffer indexBuffer = mesh.getBuffer(VertexBuffer.Type.Index);
    indexCount = indexBuffer.getNumElements() * 3;
    Buffer b = indexBuffer.getDataReadOnly();
    b.rewind();
    
    while (b.remaining() != 0) {
        Triangle tri = new Triangle();
        tri.isRemoved = false;
        triangleList.add(tri);            
        for (int i = 0; i < 3; i++) {
            if (b instanceof IntBuffer) {
                tri.vertexId[i] = ((IntBuffer) b).get();
            } else {
                //bit shift to avoid negative values due to conversion form short to int.
                //we need an unsigned int here.
                tri.vertexId[i] = ((ShortBuffer) b).get()& 0xffff;
            }
           // assert (tri.vertexId[i] < vertexLookup.size());
            tri.vertex[i] = vertexLookup.get(tri.vertexId[i]);
            //debug only;
            tri.vertex[i].index = tri.vertexId[i];
        }
        if (tri.isMalformed()) {
            if (!tri.isRemoved) {
                logger.log(Level.FINE, "malformed triangle found with ID:{0}\n{1} It will be excluded from Lod level calculations.", new Object[]{triangleList.indexOf(tri), tri.toString()});
                tri.isRemoved = true;
                indexCount -= 3;
            }
            
        } else {
            tri.computeNormal();
            addTriangleToEdges(tri);
        }
    }
    b.rewind();
}
 
Example 5
Source File: BaseLoader.java    From nd4j with Apache License 2.0 5 votes vote down vote up
/**
 * Convert an ndarray to a blob
 *
 * @param toConvert the ndarray to convert
 * @return the converted ndarray
 */
@Override
public Blob convert(INDArray toConvert) throws SQLException {
    ByteBuffer byteBuffer = BinarySerde.toByteBuffer(toConvert);
    Buffer buffer = (Buffer) byteBuffer;
    buffer.rewind();
    byte[] arr = new byte[byteBuffer.capacity()];
    byteBuffer.get(arr);
    Connection c = dataSource.getConnection();
    Blob b = c.createBlob();
    b.setBytes(1, arr);
    return b;
}
 
Example 6
Source File: BaseLoader.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * Convert an ndarray to a blob
 *
 * @param toConvert the ndarray to convert
 * @return the converted ndarray
 */
@Override
public Blob convert(INDArray toConvert) throws SQLException {
    ByteBuffer byteBuffer = BinarySerde.toByteBuffer(toConvert);
    Buffer buffer = (Buffer) byteBuffer;
    buffer.rewind();
    byte[] arr = new byte[byteBuffer.capacity()];
    byteBuffer.get(arr);
    Connection c = dataSource.getConnection();
    Blob b = c.createBlob();
    b.setBytes(1, arr);
    return b;
}
 
Example 7
Source File: JoglRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void updateBufferData(VertexBuffer vb) {
    int bufId = vb.getId();
    if (bufId == -1) {
        // create buffer
        gl.glGenBuffers(1, ib1);
        bufId = ib1.get(0);
        vb.setId(bufId);
        objManager.registerForCleanup(vb);
    }

    int target;
    if (vb.getBufferType() == VertexBuffer.Type.Index) {
        target = gl.GL_ELEMENT_ARRAY_BUFFER;
        if (context.boundElementArrayVBO != bufId) {
            gl.glBindBuffer(target, bufId);
            context.boundElementArrayVBO = bufId;
        }
    } else {
        target = gl.GL_ARRAY_BUFFER;
        if (context.boundArrayVBO != bufId) {
            gl.glBindBuffer(target, bufId);
            context.boundArrayVBO = bufId;
        }
    }

    int usage = convertUsage(vb.getUsage());
    Buffer data = vb.getData();
    data.rewind();

    gl.glBufferData(target,
            data.capacity() * vb.getFormat().getComponentSize(),
            data,
            usage);

    vb.clearUpdateNeeded();
}
 
Example 8
Source File: JoglRenderer.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void updateBufferData(VertexBuffer vb) {
    GL gl = GLContext.getCurrentGL();
    int bufId = vb.getId();
    if (bufId == -1) {
        // create buffer
        gl.glGenBuffers(1, intBuf1);
        bufId = intBuf1.get(0);
        vb.setId(bufId);
        objManager.registerForCleanup(vb);
    }

    int target;
    if (vb.getBufferType() == VertexBuffer.Type.Index) {
        target = GL.GL_ELEMENT_ARRAY_BUFFER;
        if (context.boundElementArrayVBO != bufId) {
            gl.glBindBuffer(target, bufId);
            context.boundElementArrayVBO = bufId;
        }
    }
    else {
        target = GL.GL_ARRAY_BUFFER;
        if (context.boundArrayVBO != bufId) {
            gl.glBindBuffer(target, bufId);
            context.boundArrayVBO = bufId;
        }
    }

    int usage = convertUsage(vb.getUsage());
    Buffer data = vb.getData();
    data.rewind();

    gl.glBufferData(target, data.capacity() * vb.getFormat().getComponentSize(), data, usage);

    vb.clearUpdateNeeded();
}
 
Example 9
Source File: RagdollUtils.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Enumerate vertices that meet the weight threshold for the indexed bone.
 *
 * @param mesh the mesh to analyze (not null)
 * @param boneIndex the index of the bone (&ge;0)
 * @param initialScale a scale applied to vertex positions (not null,
 * unaffected)
 * @param offset an offset subtracted from vertex positions (not null,
 * unaffected)
 * @param weightThreshold the minimum bone weight for inclusion in the
 * result (&ge;0, &le;1)
 * @return a new list of vertex coordinates (not null, length a multiple of
 * 3)
 */
private static List<Float> getPoints(Mesh mesh, int boneIndex, Vector3f initialScale, Vector3f offset, float weightThreshold) {

    FloatBuffer vertices = mesh.getFloatBuffer(Type.Position);
    VertexBuffer biBuf = mesh.getBuffer(VertexBuffer.Type.BoneIndex);
    Buffer boneIndices = biBuf.getDataReadOnly();
    FloatBuffer boneWeight = (FloatBuffer) mesh.getBuffer(Type.BoneWeight).getData();

    vertices.rewind();
    boneIndices.rewind();
    boneWeight.rewind();

    ArrayList<Float> results = new ArrayList<Float>();

    int vertexComponents = mesh.getVertexCount() * 3;

    for (int i = 0; i < vertexComponents; i += 3) {
        int k;
        boolean add = false;
        int start = i / 3 * 4;
        for (k = start; k < start + 4; k++) {
            if (readIndex(boneIndices, k) == boneIndex
                    && boneWeight.get(k) >= weightThreshold) {
                add = true;
                break;
            }
        }
        if (add) {

            Vector3f pos = new Vector3f();
            pos.x = vertices.get(i);
            pos.y = vertices.get(i + 1);
            pos.z = vertices.get(i + 2);
            pos.subtractLocal(offset).multLocal(initialScale);
            results.add(pos.x);
            results.add(pos.y);
            results.add(pos.z);

        }
    }

    return results;
}
 
Example 10
Source File: LodGenerator.java    From jmonkeyengine with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private VertexBuffer makeLod(Mesh mesh) {
    VertexBuffer indexBuffer = mesh.getBuffer(VertexBuffer.Type.Index);
    
    boolean isShortBuffer = indexBuffer.getFormat() == VertexBuffer.Format.UnsignedShort;
    // Create buffers.
    VertexBuffer lodBuffer = new VertexBuffer(VertexBuffer.Type.Index);
    int bufsize = indexCount == 0 ? 3 : indexCount;
    
    if (isShortBuffer) {
        lodBuffer.setupData(VertexBuffer.Usage.Static, 3, VertexBuffer.Format.UnsignedShort, BufferUtils.createShortBuffer(bufsize));
    } else {
        lodBuffer.setupData(VertexBuffer.Usage.Static, 3, VertexBuffer.Format.UnsignedInt, BufferUtils.createIntBuffer(bufsize));
    }
    
    
    
    lodBuffer.getData().rewind();
    //Check if we should fill it with a "dummy" triangle.
    if (indexCount == 0) {
        if (isShortBuffer) {
            for (int m = 0; m < 3; m++) {
                ((ShortBuffer) lodBuffer.getData()).put((short) 0);
            }
        } else {
            for (int m = 0; m < 3; m++) {
                ((IntBuffer) lodBuffer.getData()).put(0);
            }
        }
    }

    // Fill buffers.       
    Buffer buf = lodBuffer.getData();
    buf.rewind();
    for (Triangle triangle : triangleList) {
        if (!triangle.isRemoved) {
        //    assert (indexCount != 0);
            if (isShortBuffer) {
                for (int m = 0; m < 3; m++) {
                    ((ShortBuffer) buf).put((short) triangle.vertexId[m]);
                    
                }
            } else {
                for (int m = 0; m < 3; m++) {
                    ((IntBuffer) buf).put(triangle.vertexId[m]);
                }
                
            }
        }
    }
    buf.clear();
    lodBuffer.updateData(buf);
    return lodBuffer;
}
 
Example 11
Source File: RagdollUtils.java    From MikuMikuStudio with BSD 2-Clause "Simplified" License 4 votes vote down vote up
/**
 * returns a list of points for the given bone
 * @param mesh
 * @param boneIndex
 * @param offset
 * @param link
 * @return 
 */
private static List<Float> getPoints(Mesh mesh, int boneIndex, Vector3f initialScale, Vector3f offset, float weightThreshold) {
    if (mesh == null) {
        throw new RuntimeException("mesh is null ");
    }
    if (initialScale == null) {
        throw new RuntimeException("initialScale is null ");
    }
    if (offset == null) {
        throw new RuntimeException("offset is null ");
    }
    if (mesh.getFloatBuffer(Type.Position) == null) {
        throw new RuntimeException("verticies is null ");
    }
    if (mesh.getBuffer(Type.BoneIndex) == null) {
        throw new RuntimeException("boneIndices is null ");
    }
    if (mesh.getBuffer(Type.BoneWeight) == null) {
        throw new RuntimeException("boneWeight is null ");
    }
    FloatBuffer vertices = mesh.getFloatBuffer(Type.Position);
    Buffer boneIndices = (Buffer) mesh.getBuffer(Type.BoneIndex).getData();
    FloatBuffer boneWeight = (FloatBuffer) mesh.getBuffer(Type.BoneWeight).getData();
    vertices.rewind();
    boneIndices.rewind();
    boneWeight.rewind();

    ArrayList<Float> results = new ArrayList<Float>();

    int vertexComponents = mesh.getVertexCount() * 3;

    for (int i = 0; i < vertexComponents; i += 3) {
        int k;
        boolean add = false;
        int start = i / 3 * 4;
        for (k = start; k < start + 4; k++) {
            if (getBoneIndex(boneIndices, k) == boneIndex && boneWeight.get(k) >= weightThreshold) {
                add = true;
                break;
            }
        }
        if (add) {

            Vector3f pos = new Vector3f();
            pos.x = vertices.get(i);
            pos.y = vertices.get(i + 1);
            pos.z = vertices.get(i + 2);
            pos.subtractLocal(offset).multLocal(initialScale);
            results.add(pos.x);
            results.add(pos.y);
            results.add(pos.z);

        }
    }

    return results;
}