Search in sources :

Example 1 with ShortBuffer

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

the class ConnectionCostsCompilerTest method setUp.

@BeforeClass
public static void setUp() throws IOException {
    File costsFile = File.createTempFile("kuromoji-connectioncosts-", ".bin");
    costsFile.deleteOnExit();
    String costs = "" + "3 3\n" + "0 0 1\n" + "0 1 2\n" + "0 2 3\n" + "1 0 4\n" + "1 1 5\n" + "1 2 6\n" + "2 0 7\n" + "2 1 8\n" + "2 2 9\n";
    ConnectionCostsCompiler compiler = new ConnectionCostsCompiler(new FileOutputStream(costsFile));
    compiler.readCosts(new ByteArrayInputStream(costs.getBytes(StandardCharsets.UTF_8)));
    compiler.compile();
    DataInputStream dataInput = new DataInputStream(new FileInputStream(costsFile));
    int size = dataInput.readInt();
    ShortBuffer costsBuffer = ByteBufferIO.read(dataInput).asShortBuffer();
    dataInput.close();
    connectionCosts = new ConnectionCosts(size, costsBuffer);
}
Also used : ShortBuffer(java.nio.ShortBuffer) ConnectionCosts(com.atilika.kuromoji.dict.ConnectionCosts) BeforeClass(org.junit.BeforeClass)

Example 2 with ShortBuffer

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

the class ConnectionCosts method read.

private static ConnectionCosts read(InputStream input) throws IOException {
    DataInputStream dataInput = new DataInputStream(new BufferedInputStream(input));
    int size = dataInput.readInt();
    ByteBuffer byteBuffer = ByteBufferIO.read(dataInput);
    ShortBuffer costs = byteBuffer.asShortBuffer();
    return new ConnectionCosts(size, costs);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) DataInputStream(java.io.DataInputStream) ByteBuffer(java.nio.ByteBuffer) ShortBuffer(java.nio.ShortBuffer)

Example 3 with ShortBuffer

use of java.nio.ShortBuffer in project jmonkeyengine by jMonkeyEngine.

the class TangentBinormalGenerator method splitVertices.

//Don't remove splitmirorred boolean,It's not used right now, but i intend to
//make this method also split vertice with rotated tangent space and I'll
//add another splitRotated boolean 
private static List<VertexData> splitVertices(Mesh mesh, List<VertexData> vertexData, boolean splitMirorred) {
    int nbVertices = mesh.getBuffer(Type.Position).getNumElements();
    List<VertexData> newVertices = new ArrayList<VertexData>();
    Map<Integer, Integer> indiceMap = new HashMap<Integer, Integer>();
    FloatBuffer normalBuffer = mesh.getFloatBuffer(Type.Normal);
    for (int i = 0; i < vertexData.size(); i++) {
        ArrayList<TriangleData> triangles = vertexData.get(i).triangles;
        Vector3f givenNormal = new Vector3f();
        populateFromBuffer(givenNormal, normalBuffer, i);
        ArrayList<TriangleData> trianglesUp = new ArrayList<TriangleData>();
        ArrayList<TriangleData> trianglesDown = new ArrayList<TriangleData>();
        for (int j = 0; j < triangles.size(); j++) {
            TriangleData triangleData = triangles.get(j);
            if (parity(givenNormal, triangleData.normal) > 0) {
                trianglesUp.add(triangleData);
            } else {
                trianglesDown.add(triangleData);
            }
        }
        //if the vertex has triangles with opposite parity it has to be split
        if (!trianglesUp.isEmpty() && !trianglesDown.isEmpty()) {
            log.log(Level.FINE, "Splitting vertex {0}", i);
            //assigning triangle with the same parity to the original vertex
            vertexData.get(i).triangles.clear();
            vertexData.get(i).triangles.addAll(trianglesUp);
            //creating a new vertex
            VertexData newVert = new VertexData();
            //assigning triangles with opposite parity to it
            newVert.triangles.addAll(trianglesDown);
            newVertices.add(newVert);
            //keep vertex index to fix the index buffers later
            indiceMap.put(nbVertices, i);
            for (TriangleData tri : newVert.triangles) {
                for (int j = 0; j < tri.index.length; j++) {
                    if (tri.index[j] == i) {
                        tri.index[j] = nbVertices;
                    }
                }
            }
            nbVertices++;
        }
    }
    if (!newVertices.isEmpty()) {
        //we have new vertices, we need to update the mesh's buffers.
        for (Type type : VertexBuffer.Type.values()) {
            //skip tangent buffer as we're gonna overwrite it later
            if (type == Type.Tangent || type == Type.BindPoseTangent)
                continue;
            VertexBuffer vb = mesh.getBuffer(type);
            //They'll be initialized when Hardware Skinning is engaged
            if (vb == null || vb.getNumComponents() == 0)
                continue;
            Buffer buffer = vb.getData();
            //IndexBuffer has special treatement, only swapping the vertex indices is needed                
            if (type == Type.Index) {
                boolean isShortBuffer = vb.getFormat() == VertexBuffer.Format.UnsignedShort;
                for (VertexData vertex : newVertices) {
                    for (TriangleData tri : vertex.triangles) {
                        for (int i = 0; i < tri.index.length; i++) {
                            if (isShortBuffer) {
                                ((ShortBuffer) buffer).put(tri.triangleOffset + i, (short) tri.index[i]);
                            } else {
                                ((IntBuffer) buffer).put(tri.triangleOffset + i, tri.index[i]);
                            }
                        }
                    }
                }
                vb.setUpdateNeeded();
            } else {
                //copy the buffer in a bigger one and append nex vertices to the end
                Buffer newVerts = VertexBuffer.createBuffer(vb.getFormat(), vb.getNumComponents(), nbVertices);
                if (buffer != null) {
                    buffer.rewind();
                    bulkPut(vb.getFormat(), newVerts, buffer);
                    int index = vertexData.size();
                    newVerts.position(vertexData.size() * vb.getNumComponents());
                    for (int j = 0; j < newVertices.size(); j++) {
                        int oldInd = indiceMap.get(index);
                        for (int i = 0; i < vb.getNumComponents(); i++) {
                            putValue(vb.getFormat(), newVerts, buffer, oldInd * vb.getNumComponents() + i);
                        }
                        index++;
                    }
                    vb.updateData(newVerts);
                    //destroy previous buffer as it's no longer needed
                    destroyDirectBuffer(buffer);
                }
            }
        }
        vertexData.addAll(newVertices);
        mesh.updateCounts();
    }
    return vertexData;
}
Also used : FloatBuffer(java.nio.FloatBuffer) ShortBuffer(java.nio.ShortBuffer) IndexBuffer(com.jme3.scene.mesh.IndexBuffer) ByteBuffer(java.nio.ByteBuffer) IntBuffer(java.nio.IntBuffer) Buffer(java.nio.Buffer) DoubleBuffer(java.nio.DoubleBuffer) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FloatBuffer(java.nio.FloatBuffer) Type(com.jme3.scene.VertexBuffer.Type) Vector3f(com.jme3.math.Vector3f) IntBuffer(java.nio.IntBuffer) ShortBuffer(java.nio.ShortBuffer)

Example 4 with ShortBuffer

use of java.nio.ShortBuffer in project jmonkeyengine by jMonkeyEngine.

the class BufferUtils method createShortBuffer.

public static ShortBuffer createShortBuffer(short... data) {
    if (data == null) {
        return null;
    }
    ShortBuffer buff = createShortBuffer(data.length);
    buff.clear();
    buff.put(data);
    buff.flip();
    return buff;
}
Also used : ShortBuffer(java.nio.ShortBuffer)

Example 5 with ShortBuffer

use of java.nio.ShortBuffer in project jmonkeyengine by jMonkeyEngine.

the class BufferUtils method clone.

/**
     * Creates a new ShortBuffer with the same contents as the given
     * ShortBuffer. The new ShortBuffer is separate from the old one and changes
     * are not reflected across. If you want to reflect changes, consider using
     * Buffer.duplicate().
     * 
     * @param buf
     *            the ShortBuffer to copy
     * @return the copy
     */
public static ShortBuffer clone(ShortBuffer buf) {
    if (buf == null) {
        return null;
    }
    buf.rewind();
    ShortBuffer copy;
    if (isDirect(buf)) {
        copy = createShortBuffer(buf.limit());
    } else {
        copy = ShortBuffer.allocate(buf.limit());
    }
    copy.put(buf);
    return copy;
}
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