java.nio.Buffer#rewind ( )源码实例Demo

下面列出了java.nio.Buffer#rewind ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: jmonkeyengine   文件: RagdollUtils.java
/**
 * 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;
}
 
源代码2 项目: nd4j   文件: BaseLoader.java
/**
 * 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);
    }


}
 
源代码3 项目: deeplearning4j   文件: BaseLoader.java
/**
 * 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);
    }


}
 
源代码4 项目: jmonkeyengine   文件: LodGenerator.java
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();
}
 
源代码5 项目: nd4j   文件: BaseLoader.java
/**
 * 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;
}
 
源代码6 项目: deeplearning4j   文件: BaseLoader.java
/**
 * 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;
}
 
源代码7 项目: MikuMikuStudio   文件: JoglRenderer.java
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();
}
 
源代码8 项目: MikuMikuStudio   文件: JoglRenderer.java
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();
}
 
源代码9 项目: jmonkeyengine   文件: RagdollUtils.java
/**
 * 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;
}
 
源代码10 项目: jmonkeyengine   文件: LodGenerator.java
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;
}
 
源代码11 项目: MikuMikuStudio   文件: RagdollUtils.java
/**
 * 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;
}