Search in sources :

Example 21 with FloatBuffer

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

the class HelloOpenCL method testImages.

private boolean testImages(Context clContext, CommandQueue clQueue) {
    try {
        //query supported formats
        for (MemoryAccess ma : MemoryAccess.values()) {
            for (Image.ImageType type : Image.ImageType.values()) {
                try {
                    System.out.println("Formats for " + ma + " and " + type + ": " + Arrays.toString(clContext.querySupportedFormats(ma, type)));
                } catch (UnsupportedOperationException e) {
                    LOG.warning(e.getLocalizedMessage());
                }
            }
        }
        //create an image
        Image.ImageFormat format = new Image.ImageFormat(Image.ImageChannelOrder.RGBA, Image.ImageChannelType.FLOAT);
        Image.ImageDescriptor descr = new Image.ImageDescriptor(Image.ImageType.IMAGE_2D, 1920, 1080, 0, 0);
        Image image = clContext.createImage(MemoryAccess.READ_WRITE, format, descr);
        System.out.println("image created");
        //check queries
        assertEquals(descr.type, image.getImageType(), "Wrong image type");
        assertEquals(format, image.getImageFormat(), "Wrong image format");
        assertEquals(descr.width, image.getWidth(), "Wrong width");
        assertEquals(descr.height, image.getHeight(), "Wrong height");
        //fill with red and blue
        ColorRGBA color1 = ColorRGBA.Red;
        ColorRGBA color2 = ColorRGBA.Blue;
        Event e1 = image.fillAsync(clQueue, new long[] { 0, 0, 0 }, new long[] { descr.width / 2, descr.height, 1 }, color1);
        Event e2 = image.fillAsync(clQueue, new long[] { descr.width / 2, 0, 0 }, new long[] { descr.width / 2, descr.height, 1 }, color2);
        e1.waitForFinished();
        e2.waitForFinished();
        //copy to a buffer
        Buffer buffer = clContext.createBuffer(4 * 4 * 500 * 1024);
        Event e3 = image.copyToBufferAsync(clQueue, buffer, new long[] { 10, 10, 0 }, new long[] { 500, 1024, 1 }, 0);
        e3.release();
        //this buffer must be completely red
        ByteBuffer map1 = buffer.map(clQueue, MappingAccess.MAP_READ_ONLY);
        FloatBuffer map1F = map1.asFloatBuffer();
        map1F.rewind();
        for (int x = 0; x < 500; ++x) {
            for (int y = 0; y < 1024; ++y) {
                float r = map1F.get();
                float g = map1F.get();
                float b = map1F.get();
                float a = map1F.get();
                assertEquals(1, r, "Wrong red component");
                assertEquals(0, g, "Wrong green component");
                assertEquals(0, b, "Wrong blue component");
                assertEquals(1, a, "Wrong alpha component");
            }
        }
        buffer.unmap(clQueue, map1);
        //create a second image
        format = new Image.ImageFormat(Image.ImageChannelOrder.RGBA, Image.ImageChannelType.FLOAT);
        descr = new Image.ImageDescriptor(Image.ImageType.IMAGE_2D, 512, 512, 0, 0);
        Image image2 = clContext.createImage(MemoryAccess.READ_WRITE, format, descr);
        //copy an area of image1 to image2
        image.copyTo(clQueue, image2, new long[] { 1000, 20, 0 }, new long[] { 0, 0, 0 }, new long[] { 512, 512, 1 });
        //this area should be completely blue
        Image.ImageMapping map2 = image2.map(clQueue, new long[] { 0, 0, 0 }, new long[] { 512, 512, 1 }, MappingAccess.MAP_READ_WRITE);
        FloatBuffer map2F = map2.buffer.asFloatBuffer();
        for (int y = 0; y < 512; ++y) {
            for (int x = 0; x < 512; ++x) {
                long index = 4 * x + y * (map2.rowPitch / 4);
                map2F.position((int) index);
                float r = map2F.get();
                float g = map2F.get();
                float b = map2F.get();
                float a = map2F.get();
                assertEquals(0, r, "Wrong red component");
                assertEquals(0, g, "Wrong green component");
                assertEquals(1, b, "Wrong blue component");
                assertEquals(1, a, "Wrong alpha component");
            }
        }
        image2.unmap(clQueue, map2);
        //release
        image.release();
        image2.release();
        buffer.release();
    } catch (AssertionError ex) {
        LOG.log(Level.SEVERE, "image test failed with an assertion error");
        return false;
    } catch (Exception ex) {
        LOG.log(Level.SEVERE, "image test failed with:", ex);
        return false;
    }
    return true;
}
Also used : FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer) FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer) ColorRGBA(com.jme3.math.ColorRGBA)

Example 22 with FloatBuffer

use of java.nio.FloatBuffer in project mongomvcc by igd-geo.

the class DefaultConvertStrategy method convert.

@Override
public long convert(Object data) {
    GridFSInputFile file;
    if (data instanceof byte[]) {
        file = _gridFS.createFile((byte[]) data);
        file.put(BINARY_TYPE, BYTEARRAY);
    } else if (data instanceof float[]) {
        file = _gridFS.createFile(new FloatArrayInputStream((float[]) data));
        file.put(BINARY_TYPE, FLOATARRAY);
    } else if (data instanceof InputStream) {
        file = _gridFS.createFile((InputStream) data);
        file.put(BINARY_TYPE, INPUTSTREAM);
    } else if (data instanceof ByteBuffer) {
        ByteBuffer bb = (ByteBuffer) data;
        byte[] buf;
        if (bb.hasArray()) {
            buf = bb.array();
        } else {
            bb.rewind();
            buf = new byte[bb.remaining()];
            bb.get(buf);
        }
        file = _gridFS.createFile(buf);
        file.put(BINARY_TYPE, BYTEBUFFER);
    } else if (data instanceof FloatBuffer) {
        FloatBuffer bb = (FloatBuffer) data;
        float[] buf;
        if (bb.hasArray()) {
            buf = bb.array();
        } else {
            bb.rewind();
            buf = new float[bb.remaining()];
            bb.get(buf);
        }
        file = _gridFS.createFile(new FloatArrayInputStream(buf));
        file.put(BINARY_TYPE, FLOATBUFFER);
    } else {
        return 0;
    }
    long oid = _counter.getNextId();
    file.put(MongoDBVLargeCollection.OID, oid);
    _convertedFiles.add(file);
    return oid;
}
Also used : GridFSInputFile(com.mongodb.gridfs.GridFSInputFile) DataInputStream(java.io.DataInputStream) BufferedInputStream(java.io.BufferedInputStream) FloatArrayInputStream(de.fhg.igd.mongomvcc.helper.FloatArrayInputStream) InputStream(java.io.InputStream) FloatArrayInputStream(de.fhg.igd.mongomvcc.helper.FloatArrayInputStream) FloatBuffer(java.nio.FloatBuffer) ByteBuffer(java.nio.ByteBuffer)

Example 23 with FloatBuffer

use of java.nio.FloatBuffer in project mongomvcc by igd-geo.

the class MongoDBVLargeCollectionTest method largeObjectFloat.

/**
	 * Tests if large objects with float arrays/streams/buffers can be saved in the database
	 * @throws Exception if something goes wrong
	 */
@Test
public void largeObjectFloat() throws Exception {
    VCollection coll = _master.getLargeCollection("images");
    float[] test = new float[1024 * 1024];
    for (int i = 0; i < test.length; ++i) {
        test[i] = (byte) (i & 0xFF);
    }
    Map<String, Object> obj = new HashMap<String, Object>();
    obj.put("name", "Mona Lisa");
    obj.put("data", test);
    coll.insert(obj);
    VCursor vc = coll.find();
    assertEquals(1, vc.size());
    Map<String, Object> obj2 = vc.iterator().next();
    assertEquals("Mona Lisa", obj2.get("name"));
    assertArrayEquals(test, (float[]) obj2.get("data"), 0.00001f);
    FloatBuffer bb = FloatBuffer.wrap(test);
    obj = new HashMap<String, Object>();
    obj.put("name", "Mona Lisa");
    obj.put("data", bb);
    coll.insert(obj);
    Map<String, Object> obj4 = coll.findOne(_factory.createDocument("uid", obj.get("uid")));
    assertEquals("Mona Lisa", obj4.get("name"));
    FloatBuffer bb4 = (FloatBuffer) obj4.get("data");
    bb4.rewind();
    float[] test4 = new float[bb4.remaining()];
    bb4.get(test4);
    assertArrayEquals(test, test4, 0.00001f);
}
Also used : HashMap(java.util.HashMap) FloatBuffer(java.nio.FloatBuffer) VCursor(de.fhg.igd.mongomvcc.VCursor) VCollection(de.fhg.igd.mongomvcc.VCollection) Test(org.junit.Test)

Example 24 with FloatBuffer

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

the class Cylinder method updateGeometry.

/**
     * Rebuilds the cylinder based on a new set of parameters.
     *
     * @param axisSamples the number of samples along the axis.
     * @param radialSamples the number of samples around the radial.
     * @param radius the radius of the bottom of the cylinder.
     * @param radius2 the radius of the top of the cylinder.
     * @param height the cylinder's height.
     * @param closed should the cylinder have top and bottom surfaces.
     * @param inverted is the cylinder is meant to be viewed from the inside.
     */
public void updateGeometry(int axisSamples, int radialSamples, float radius, float radius2, float height, boolean closed, boolean inverted) {
    this.axisSamples = axisSamples;
    this.radialSamples = radialSamples;
    this.radius = radius;
    this.radius2 = radius2;
    this.height = height;
    this.closed = closed;
    this.inverted = inverted;
    //        VertexBuffer pvb = getBuffer(Type.Position);
    //        VertexBuffer nvb = getBuffer(Type.Normal);
    //        VertexBuffer tvb = getBuffer(Type.TexCoord);
    axisSamples += (closed ? 2 : 0);
    // Vertices
    int vertCount = axisSamples * (radialSamples + 1) + (closed ? 2 : 0);
    setBuffer(Type.Position, 3, createVector3Buffer(getFloatBuffer(Type.Position), vertCount));
    // Normals
    setBuffer(Type.Normal, 3, createVector3Buffer(getFloatBuffer(Type.Normal), vertCount));
    // Texture co-ordinates
    setBuffer(Type.TexCoord, 2, createVector2Buffer(vertCount));
    int triCount = ((closed ? 2 : 0) + 2 * (axisSamples - 1)) * radialSamples;
    setBuffer(Type.Index, 3, createShortBuffer(getShortBuffer(Type.Index), 3 * triCount));
    // generate geometry
    float inverseRadial = 1.0f / radialSamples;
    float inverseAxisLess = 1.0f / (closed ? axisSamples - 3 : axisSamples - 1);
    float inverseAxisLessTexture = 1.0f / (axisSamples - 1);
    float halfHeight = 0.5f * height;
    // Generate points on the unit circle to be used in computing the mesh
    // points on a cylinder slice.
    float[] sin = new float[radialSamples + 1];
    float[] cos = new float[radialSamples + 1];
    for (int radialCount = 0; radialCount < radialSamples; radialCount++) {
        float angle = FastMath.TWO_PI * inverseRadial * radialCount;
        cos[radialCount] = FastMath.cos(angle);
        sin[radialCount] = FastMath.sin(angle);
    }
    sin[radialSamples] = sin[0];
    cos[radialSamples] = cos[0];
    // calculate normals
    Vector3f[] vNormals = null;
    Vector3f vNormal = Vector3f.UNIT_Z;
    if ((height != 0.0f) && (radius != radius2)) {
        vNormals = new Vector3f[radialSamples];
        Vector3f vHeight = Vector3f.UNIT_Z.mult(height);
        Vector3f vRadial = new Vector3f();
        for (int radialCount = 0; radialCount < radialSamples; radialCount++) {
            vRadial.set(cos[radialCount], sin[radialCount], 0.0f);
            Vector3f vRadius = vRadial.mult(radius);
            Vector3f vRadius2 = vRadial.mult(radius2);
            Vector3f vMantle = vHeight.subtract(vRadius2.subtract(vRadius));
            Vector3f vTangent = vRadial.cross(Vector3f.UNIT_Z);
            vNormals[radialCount] = vMantle.cross(vTangent).normalize();
        }
    }
    FloatBuffer nb = getFloatBuffer(Type.Normal);
    FloatBuffer pb = getFloatBuffer(Type.Position);
    FloatBuffer tb = getFloatBuffer(Type.TexCoord);
    // generate the cylinder itself
    Vector3f tempNormal = new Vector3f();
    for (int axisCount = 0, i = 0; axisCount < axisSamples; axisCount++, i++) {
        float axisFraction;
        float axisFractionTexture;
        int topBottom = 0;
        if (!closed) {
            // in [0,1]
            axisFraction = axisCount * inverseAxisLess;
            axisFractionTexture = axisFraction;
        } else {
            if (axisCount == 0) {
                // bottom
                topBottom = -1;
                axisFraction = 0;
                axisFractionTexture = inverseAxisLessTexture;
            } else if (axisCount == axisSamples - 1) {
                // top
                topBottom = 1;
                axisFraction = 1;
                axisFractionTexture = 1 - inverseAxisLessTexture;
            } else {
                axisFraction = (axisCount - 1) * inverseAxisLess;
                axisFractionTexture = axisCount * inverseAxisLessTexture;
            }
        }
        // compute center of slice
        float z = -halfHeight + height * axisFraction;
        Vector3f sliceCenter = new Vector3f(0, 0, z);
        // compute slice vertices with duplication at end point
        int save = i;
        for (int radialCount = 0; radialCount < radialSamples; radialCount++, i++) {
            // in [0,1)
            float radialFraction = radialCount * inverseRadial;
            tempNormal.set(cos[radialCount], sin[radialCount], 0.0f);
            if (vNormals != null) {
                vNormal = vNormals[radialCount];
            } else if (radius == radius2) {
                vNormal = tempNormal;
            }
            if (topBottom == 0) {
                if (!inverted)
                    nb.put(vNormal.x).put(vNormal.y).put(vNormal.z);
                else
                    nb.put(-vNormal.x).put(-vNormal.y).put(-vNormal.z);
            } else {
                nb.put(0).put(0).put(topBottom * (inverted ? -1 : 1));
            }
            tempNormal.multLocal((radius - radius2) * axisFraction + radius2).addLocal(sliceCenter);
            pb.put(tempNormal.x).put(tempNormal.y).put(tempNormal.z);
            tb.put((inverted ? 1 - radialFraction : radialFraction)).put(axisFractionTexture);
        }
        BufferUtils.copyInternalVector3(pb, save, i);
        BufferUtils.copyInternalVector3(nb, save, i);
        tb.put((inverted ? 0.0f : 1.0f)).put(axisFractionTexture);
    }
    if (closed) {
        // bottom center
        pb.put(0).put(0).put(-halfHeight);
        nb.put(0).put(0).put(-1 * (inverted ? -1 : 1));
        tb.put(0.5f).put(0);
        // top center
        pb.put(0).put(0).put(halfHeight);
        nb.put(0).put(0).put(1 * (inverted ? -1 : 1));
        tb.put(0.5f).put(1);
    }
    IndexBuffer ib = getIndexBuffer();
    int index = 0;
    // Connectivity
    for (int axisCount = 0, axisStart = 0; axisCount < axisSamples - 1; axisCount++) {
        int i0 = axisStart;
        int i1 = i0 + 1;
        axisStart += radialSamples + 1;
        int i2 = axisStart;
        int i3 = i2 + 1;
        for (int i = 0; i < radialSamples; i++) {
            if (closed && axisCount == 0) {
                if (!inverted) {
                    ib.put(index++, i0++);
                    ib.put(index++, vertCount - 2);
                    ib.put(index++, i1++);
                } else {
                    ib.put(index++, i0++);
                    ib.put(index++, i1++);
                    ib.put(index++, vertCount - 2);
                }
            } else if (closed && axisCount == axisSamples - 2) {
                ib.put(index++, i2++);
                ib.put(index++, inverted ? vertCount - 1 : i3++);
                ib.put(index++, inverted ? i3++ : vertCount - 1);
            } else {
                ib.put(index++, i0++);
                ib.put(index++, inverted ? i2 : i1);
                ib.put(index++, inverted ? i1 : i2);
                ib.put(index++, i1++);
                ib.put(index++, inverted ? i2++ : i3++);
                ib.put(index++, inverted ? i3++ : i2++);
            }
        }
    }
    updateBound();
    setStatic();
}
Also used : IndexBuffer(com.jme3.scene.mesh.IndexBuffer) Vector3f(com.jme3.math.Vector3f) FloatBuffer(java.nio.FloatBuffer)

Example 25 with FloatBuffer

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

the class Torus method setGeometryData.

private void setGeometryData() {
    // allocate vertices
    int vertCount = (circleSamples + 1) * (radialSamples + 1);
    FloatBuffer fpb = BufferUtils.createVector3Buffer(vertCount);
    setBuffer(Type.Position, 3, fpb);
    // allocate normals if requested
    FloatBuffer fnb = BufferUtils.createVector3Buffer(vertCount);
    setBuffer(Type.Normal, 3, fnb);
    // allocate texture coordinates
    FloatBuffer ftb = BufferUtils.createVector2Buffer(vertCount);
    setBuffer(Type.TexCoord, 2, ftb);
    // generate geometry
    float inverseCircleSamples = 1.0f / circleSamples;
    float inverseRadialSamples = 1.0f / radialSamples;
    int i = 0;
    // generate the cylinder itself
    Vector3f radialAxis = new Vector3f(), torusMiddle = new Vector3f(), tempNormal = new Vector3f();
    for (int circleCount = 0; circleCount < circleSamples; circleCount++) {
        // compute center point on torus circle at specified angle
        float circleFraction = circleCount * inverseCircleSamples;
        float theta = FastMath.TWO_PI * circleFraction;
        float cosTheta = FastMath.cos(theta);
        float sinTheta = FastMath.sin(theta);
        radialAxis.set(cosTheta, sinTheta, 0);
        radialAxis.mult(outerRadius, torusMiddle);
        // compute slice vertices with duplication at end point
        int iSave = i;
        for (int radialCount = 0; radialCount < radialSamples; radialCount++) {
            float radialFraction = radialCount * inverseRadialSamples;
            // in [0,1)
            float phi = FastMath.TWO_PI * radialFraction;
            float cosPhi = FastMath.cos(phi);
            float sinPhi = FastMath.sin(phi);
            tempNormal.set(radialAxis).multLocal(cosPhi);
            tempNormal.z += sinPhi;
            fnb.put(tempNormal.x).put(tempNormal.y).put(tempNormal.z);
            tempNormal.multLocal(innerRadius).addLocal(torusMiddle);
            fpb.put(tempNormal.x).put(tempNormal.y).put(tempNormal.z);
            ftb.put(radialFraction).put(circleFraction);
            i++;
        }
        BufferUtils.copyInternalVector3(fpb, iSave, i);
        BufferUtils.copyInternalVector3(fnb, iSave, i);
        ftb.put(1.0f).put(circleFraction);
        i++;
    }
    // duplicate the cylinder ends to form a torus
    for (int iR = 0; iR <= radialSamples; iR++, i++) {
        BufferUtils.copyInternalVector3(fpb, iR, i);
        BufferUtils.copyInternalVector3(fnb, iR, i);
        BufferUtils.copyInternalVector2(ftb, iR, i);
        ftb.put(i * 2 + 1, 1.0f);
    }
}
Also used : Vector3f(com.jme3.math.Vector3f) FloatBuffer(java.nio.FloatBuffer)

Aggregations

FloatBuffer (java.nio.FloatBuffer)287 ByteBuffer (java.nio.ByteBuffer)82 IntBuffer (java.nio.IntBuffer)43 ShortBuffer (java.nio.ShortBuffer)39 Vector3f (com.jme3.math.Vector3f)27 VertexBuffer (com.jme3.scene.VertexBuffer)27 DoubleBuffer (java.nio.DoubleBuffer)17 IndexBuffer (com.jme3.scene.mesh.IndexBuffer)16 LongBuffer (java.nio.LongBuffer)10 Mesh (com.jme3.scene.Mesh)9 CharBuffer (java.nio.CharBuffer)9 FrameBuffer2D (androidx.media.filterfw.FrameBuffer2D)8 OutputPort (androidx.media.filterfw.OutputPort)7 Matrix4f (com.jme3.math.Matrix4f)7 Buffer (java.nio.Buffer)7 TempVars (com.jme3.util.TempVars)6 IOException (java.io.IOException)6 BufferOverflowException (java.nio.BufferOverflowException)6 BufferUnderflowException (java.nio.BufferUnderflowException)6 ArrayList (java.util.ArrayList)6