Search in sources :

Example 21 with Matrix3f

use of com.jme3.math.Matrix3f in project jmonkeyengine by jMonkeyEngine.

the class Kernel method setArg.

public void setArg(int index, Matrix3f mat) {
    TempVars vars = TempVars.get();
    try {
        Matrix4f m = vars.tempMat4;
        m.zero();
        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < 3; ++j) {
                m.set(i, j, mat.get(i, j));
            }
        }
        setArg(index, m);
    } finally {
        vars.release();
    }
}
Also used : TempVars(com.jme3.util.TempVars)

Example 22 with Matrix3f

use of com.jme3.math.Matrix3f in project jmonkeyengine by jMonkeyEngine.

the class Line method orthogonalLineFit.

public void orthogonalLineFit(FloatBuffer points) {
    if (points == null) {
        return;
    }
    TempVars vars = TempVars.get();
    Vector3f compVec1 = vars.vect1;
    Vector3f compVec2 = vars.vect2;
    Matrix3f compMat1 = vars.tempMat3;
    Eigen3f compEigen1 = vars.eigen;
    points.rewind();
    // compute average of points
    int length = points.remaining() / 3;
    BufferUtils.populateFromBuffer(origin, points, 0);
    for (int i = 1; i < length; i++) {
        BufferUtils.populateFromBuffer(compVec1, points, i);
        origin.addLocal(compVec1);
    }
    origin.multLocal(1f / (float) length);
    // compute sums of products
    float sumXX = 0.0f, sumXY = 0.0f, sumXZ = 0.0f;
    float sumYY = 0.0f, sumYZ = 0.0f, sumZZ = 0.0f;
    points.rewind();
    for (int i = 0; i < length; i++) {
        BufferUtils.populateFromBuffer(compVec1, points, i);
        compVec1.subtract(origin, compVec2);
        sumXX += compVec2.x * compVec2.x;
        sumXY += compVec2.x * compVec2.y;
        sumXZ += compVec2.x * compVec2.z;
        sumYY += compVec2.y * compVec2.y;
        sumYZ += compVec2.y * compVec2.z;
        sumZZ += compVec2.z * compVec2.z;
    }
    //find the smallest eigen vector for the direction vector
    compMat1.m00 = sumYY + sumZZ;
    compMat1.m01 = -sumXY;
    compMat1.m02 = -sumXZ;
    compMat1.m10 = -sumXY;
    compMat1.m11 = sumXX + sumZZ;
    compMat1.m12 = -sumYZ;
    compMat1.m20 = -sumXZ;
    compMat1.m21 = -sumYZ;
    compMat1.m22 = sumXX + sumYY;
    compEigen1.calculateEigen(compMat1);
    direction = compEigen1.getEigenVector(0);
    vars.release();
}
Also used : TempVars(com.jme3.util.TempVars)

Example 23 with Matrix3f

use of com.jme3.math.Matrix3f in project jmonkeyengine by jMonkeyEngine.

the class TestOpenCLLibraries method testMatrix3f.

private boolean testMatrix3f(Context clContext, CommandQueue clQueue) {
    try {
        String code = "" + "#import \"Common/OpenCL/Matrix3f.clh\"\n" + "\n" + "__kernel void TestMatrix3f_1(__global char* result)\n" + "{\n" + "  mat3 id = mat3Identity();\n" + "  mat3 m1 = mat3FromRows( (float3)(23,-3,10.4f), (float3)(5,-8,2.22f), (float3)(-1,0,34) );\n" + "  mat3 m1Inv = mat3Invert(m1);\n" + "  mat3 m1Res = mat3Mult(m1, m1Inv);\n" + "  result[0] = mat3Equals(id, m1Res, 0.0001f) ? 1 : 0;\n" + "}\n" + "\n" + "__kernel void TestMatrix3f_2(mat3 m1, float a, mat3 m2, mat3 mRes, __global char* result)\n" + "{\n" + "  mat3 m = mat3Transpose(m1);\n" + "  m = mat3Add(mat3Scale(m, a), m2);\n" + "  result[0] = mat3Equals(mRes, m, 0.01f) ? 1 : 0;\n" + "}\n";
        Program program = clContext.createProgramFromSourceCodeWithDependencies(code, assetManager);
        program.build();
        com.jme3.opencl.Buffer buffer = clContext.createBuffer(1);
        Kernel testMatrix3fKernel1 = program.createKernel("TestMatrix3f_1");
        testMatrix3fKernel1.Run1NoEvent(clQueue, new Kernel.WorkSize(1), buffer);
        ByteBuffer bb = buffer.map(clQueue, MappingAccess.MAP_READ_ONLY);
        if (bb.get() == 0) {
            LOG.severe("Matrix inversion failed");
            return false;
        }
        buffer.unmap(clQueue, bb);
        testMatrix3fKernel1.release();
        Kernel testMatrix3fKernel2 = program.createKernel("TestMatrix3f_2");
        Matrix3f m1 = new Matrix3f(13.24f, -0.234f, 42, 83.23f, -34.2f, 3.2f, 0.25f, -42, 7.64f);
        Matrix3f m2 = new Matrix3f(-5.2f, 0.757f, 2.01f, 12.0f, -6, 2, 0.01f, 9, 2.255f);
        Matrix3f mRes = new Matrix3f(-31.68f, -165.703f, 1.51f, 12.468f, 62.4f, 86, -83.99f, 2.6f, -13.025f);
        testMatrix3fKernel2.Run1NoEvent(clQueue, new Kernel.WorkSize(1), m1, -2.0f, m2, mRes, buffer);
        bb = buffer.map(clQueue, MappingAccess.MAP_READ_ONLY);
        if (bb.get() == 0) {
            LOG.severe("Matrix add, multiply, transpose failed");
            return false;
        }
        buffer.unmap(clQueue, bb);
        testMatrix3fKernel2.release();
        buffer.release();
    } catch (AssertionError ex) {
        LOG.log(Level.SEVERE, "matrix3f test failed with an assertion error");
        return false;
    } catch (Exception ex) {
        LOG.log(Level.SEVERE, "matrix3f test failed with:", ex);
        return false;
    }
    return true;
}
Also used : com.jme3.opencl(com.jme3.opencl) Matrix3f(com.jme3.math.Matrix3f)

Example 24 with Matrix3f

use of com.jme3.math.Matrix3f in project jmonkeyengine by jMonkeyEngine.

the class TestOpenCLLibraries method simpleInitApp.

@Override
public void simpleInitApp() {
    BitmapFont fnt = assetManager.loadFont("Interface/Fonts/Default.fnt");
    Context clContext = context.getOpenCLContext();
    if (clContext == null) {
        BitmapText txt = new BitmapText(fnt);
        txt.setText("No OpenCL Context created!\nSee output log for details.");
        txt.setLocalTranslation(5, settings.getHeight() - 5, 0);
        guiNode.attachChild(txt);
        return;
    }
    CommandQueue clQueue = clContext.createQueue(clContext.getDevices().get(0));
    StringBuilder str = new StringBuilder();
    str.append("OpenCL Context created:\n  Platform: ").append(clContext.getDevices().get(0).getPlatform().getName()).append("\n  Devices: ").append(clContext.getDevices());
    str.append("\nTests:");
    str.append("\n  Random numbers: ").append(testRandom(clContext, clQueue));
    str.append("\n  Matrix3f: ").append(testMatrix3f(clContext, clQueue));
    str.append("\n  Matrix4f: ").append(testMatrix4f(clContext, clQueue));
    clQueue.release();
    BitmapText txt1 = new BitmapText(fnt);
    txt1.setText(str.toString());
    txt1.setLocalTranslation(5, settings.getHeight() - 5, 0);
    guiNode.attachChild(txt1);
    flyCam.setEnabled(false);
    inputManager.setCursorVisible(true);
}
Also used : BitmapText(com.jme3.font.BitmapText) BitmapFont(com.jme3.font.BitmapFont)

Example 25 with Matrix3f

use of com.jme3.math.Matrix3f in project jmonkeyengine by jMonkeyEngine.

the class SixDofJoint method read.

@Override
public void read(JmeImporter im) throws IOException {
    super.read(im);
    InputCapsule capsule = im.getCapsule(this);
    Transform transA = new Transform(Converter.convert(new Matrix3f()));
    Converter.convert(pivotA, transA.origin);
    Transform transB = new Transform(Converter.convert(new Matrix3f()));
    Converter.convert(pivotB, transB.origin);
    constraint = new Generic6DofConstraint(nodeA.getObjectId(), nodeB.getObjectId(), transA, transB, useLinearReferenceFrameA);
    gatherMotors();
    setAngularUpperLimit((Vector3f) capsule.readSavable("angularUpperLimit", new Vector3f(Vector3f.POSITIVE_INFINITY)));
    setAngularLowerLimit((Vector3f) capsule.readSavable("angularLowerLimit", new Vector3f(Vector3f.NEGATIVE_INFINITY)));
    setLinearUpperLimit((Vector3f) capsule.readSavable("linearUpperLimit", new Vector3f(Vector3f.POSITIVE_INFINITY)));
    setLinearLowerLimit((Vector3f) capsule.readSavable("linearLowerLimit", new Vector3f(Vector3f.NEGATIVE_INFINITY)));
    for (int i = 0; i < 3; i++) {
        RotationalLimitMotor rotationalLimitMotor = getRotationalLimitMotor(i);
        rotationalLimitMotor.setBounce(capsule.readFloat("rotMotor" + i + "_Bounce", 0.0f));
        rotationalLimitMotor.setDamping(capsule.readFloat("rotMotor" + i + "_Damping", 1.0f));
        rotationalLimitMotor.setERP(capsule.readFloat("rotMotor" + i + "_ERP", 0.5f));
        rotationalLimitMotor.setHiLimit(capsule.readFloat("rotMotor" + i + "_HiLimit", Float.POSITIVE_INFINITY));
        rotationalLimitMotor.setLimitSoftness(capsule.readFloat("rotMotor" + i + "_LimitSoftness", 0.5f));
        rotationalLimitMotor.setLoLimit(capsule.readFloat("rotMotor" + i + "_LoLimit", Float.NEGATIVE_INFINITY));
        rotationalLimitMotor.setMaxLimitForce(capsule.readFloat("rotMotor" + i + "_MaxLimitForce", 300.0f));
        rotationalLimitMotor.setMaxMotorForce(capsule.readFloat("rotMotor" + i + "_MaxMotorForce", 0.1f));
        rotationalLimitMotor.setTargetVelocity(capsule.readFloat("rotMotor" + i + "_TargetVelocity", 0));
        rotationalLimitMotor.setEnableMotor(capsule.readBoolean("rotMotor" + i + "_EnableMotor", false));
    }
    getTranslationalLimitMotor().setAccumulatedImpulse((Vector3f) capsule.readSavable("transMotor_AccumulatedImpulse", Vector3f.ZERO));
    getTranslationalLimitMotor().setDamping(capsule.readFloat("transMotor_Damping", 1.0f));
    getTranslationalLimitMotor().setLimitSoftness(capsule.readFloat("transMotor_LimitSoftness", 0.7f));
    getTranslationalLimitMotor().setLowerLimit((Vector3f) capsule.readSavable("transMotor_LowerLimit", Vector3f.ZERO));
    getTranslationalLimitMotor().setRestitution(capsule.readFloat("transMotor_Restitution", 0.5f));
    getTranslationalLimitMotor().setUpperLimit((Vector3f) capsule.readSavable("transMotor_UpperLimit", Vector3f.ZERO));
}
Also used : Matrix3f(com.jme3.math.Matrix3f) InputCapsule(com.jme3.export.InputCapsule) Vector3f(com.jme3.math.Vector3f) Generic6DofConstraint(com.bulletphysics.dynamics.constraintsolver.Generic6DofConstraint) RotationalLimitMotor(com.jme3.bullet.joints.motors.RotationalLimitMotor) Transform(com.bulletphysics.linearmath.Transform) Generic6DofConstraint(com.bulletphysics.dynamics.constraintsolver.Generic6DofConstraint)

Aggregations

Matrix3f (com.jme3.math.Matrix3f)21 Vector3f (com.jme3.math.Vector3f)10 TempVars (com.jme3.util.TempVars)8 ChildCollisionShape (com.jme3.bullet.collision.shapes.infos.ChildCollisionShape)5 InputCapsule (com.jme3.export.InputCapsule)5 Transform (com.bulletphysics.linearmath.Transform)3 OutputCapsule (com.jme3.export.OutputCapsule)3 CompoundShape (com.bulletphysics.collision.shapes.CompoundShape)2 BoxCollisionShape (com.jme3.bullet.collision.shapes.BoxCollisionShape)2 CollisionShape (com.jme3.bullet.collision.shapes.CollisionShape)2 CompoundCollisionShape (com.jme3.bullet.collision.shapes.CompoundCollisionShape)2 Geometry (com.jme3.scene.Geometry)2 Node (com.jme3.scene.Node)2 Spatial (com.jme3.scene.Spatial)2 VertexBuffer (com.jme3.scene.VertexBuffer)2 ByteBuffer (java.nio.ByteBuffer)2 FloatBuffer (java.nio.FloatBuffer)2 Generic6DofConstraint (com.bulletphysics.dynamics.constraintsolver.Generic6DofConstraint)1 RotationalLimitMotor (com.jme3.bullet.joints.motors.RotationalLimitMotor)1 BitmapFont (com.jme3.font.BitmapFont)1