Java Code Examples for org.joml.Matrix4f#rotate()

The following examples show how to use org.joml.Matrix4f#rotate() . 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: AnimMeshesLoader.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
private static void buildTransFormationMatrices(AINodeAnim aiNodeAnim, Node node) {
    int numFrames = aiNodeAnim.mNumPositionKeys();
    AIVectorKey.Buffer positionKeys = aiNodeAnim.mPositionKeys();
    AIVectorKey.Buffer scalingKeys = aiNodeAnim.mScalingKeys();
    AIQuatKey.Buffer rotationKeys = aiNodeAnim.mRotationKeys();

    for (int i = 0; i < numFrames; i++) {
        AIVectorKey aiVecKey = positionKeys.get(i);
        AIVector3D vec = aiVecKey.mValue();

        Matrix4f transfMat = new Matrix4f().translate(vec.x(), vec.y(), vec.z());

        AIQuatKey quatKey = rotationKeys.get(i);
        AIQuaternion aiQuat = quatKey.mValue();
        Quaternionf quat = new Quaternionf(aiQuat.x(), aiQuat.y(), aiQuat.z(), aiQuat.w());
        transfMat.rotate(quat);

        if (i < aiNodeAnim.mNumScalingKeys()) {
            aiVecKey = scalingKeys.get(i);
            vec = aiVecKey.mValue();
            transfMat.scale(vec.x(), vec.y(), vec.z());
        }

        node.addTransformation(transfMat);
    }
}
 
Example 2
Source File: AnimMeshesLoader.java    From lwjglbook with Apache License 2.0 6 votes vote down vote up
private static void buildTransFormationMatrices(AINodeAnim aiNodeAnim, Node node) {
    int numFrames = aiNodeAnim.mNumPositionKeys();
    AIVectorKey.Buffer positionKeys = aiNodeAnim.mPositionKeys();
    AIVectorKey.Buffer scalingKeys = aiNodeAnim.mScalingKeys();
    AIQuatKey.Buffer rotationKeys = aiNodeAnim.mRotationKeys();

    for (int i = 0; i < numFrames; i++) {
        AIVectorKey aiVecKey = positionKeys.get(i);
        AIVector3D vec = aiVecKey.mValue();

        Matrix4f transfMat = new Matrix4f().translate(vec.x(), vec.y(), vec.z());

        AIQuatKey quatKey = rotationKeys.get(i);
        AIQuaternion aiQuat = quatKey.mValue();
        Quaternionf quat = new Quaternionf(aiQuat.x(), aiQuat.y(), aiQuat.z(), aiQuat.w());
        transfMat.rotate(quat);

        if (i < aiNodeAnim.mNumScalingKeys()) {
            aiVecKey = scalingKeys.get(i);
            vec = aiVecKey.mValue();
            transfMat.scale(vec.x(), vec.y(), vec.z());
        }

        node.addTransformation(transfMat);
    }
}
 
Example 3
Source File: Transform3DTest.java    From WraithEngine with Apache License 2.0 6 votes vote down vote up
@Test
public void getLocalMatrix()
{
    Transform3D t = new Transform3D();

    t.setPosition(10f, 30f, 50f);
    t.getRotation()
     .rotateLocalX((float) Math.toRadians(90f));
    t.setSize(4f);

    Matrix4f mat = new Matrix4f();
    t.getLocalMatrix(mat);

    Matrix4f mat2 = new Matrix4f();
    mat2.translate(10f, 30f, 50f);
    mat2.rotate(new Quaternionf().rotateLocalX((float) Math.toRadians(90f)));
    mat2.scale(4f);

    Assert.assertEquals(mat2, mat);
}
 
Example 4
Source File: Transform3DTest.java    From WraithEngine with Apache License 2.0 6 votes vote down vote up
@Test
public void getFullMatrix_NullParent()
{
    Transform3D t = new Transform3D();
    t.setPosition(10f, 30f, 50f);
    t.getRotation()
     .rotateLocalX((float) Math.toRadians(90f));
    t.setSize(4f);

    Matrix4f mat = new Matrix4f();
    t.getFullMatrix(mat);

    Matrix4f mat2 = new Matrix4f();
    mat2.translate(10f, 30f, 50f);
    mat2.rotate(new Quaternionf().rotateLocalX((float) Math.toRadians(90f)));
    mat2.scale(4f);

    Assert.assertEquals(mat2, mat);
}
 
Example 5
Source File: Transform3DTest.java    From WraithEngine with Apache License 2.0 6 votes vote down vote up
@Test
public void getLocalMatrix()
{
    Transform3D t = new Transform3D();

    t.setPosition(10f, 30f, 50f);
    t.getRotation()
     .rotateLocalX((float) Math.toRadians(90f));
    t.setSize(4f);

    Matrix4f mat = new Matrix4f();
    t.getLocalMatrix(mat);

    Matrix4f mat2 = new Matrix4f();
    mat2.translate(10f, 30f, 50f);
    mat2.rotate(new Quaternionf().rotateLocalX((float) Math.toRadians(90f)));
    mat2.scale(4f);

    Assert.assertEquals(mat2, mat);
}
 
Example 6
Source File: Transform3DTest.java    From WraithEngine with Apache License 2.0 6 votes vote down vote up
@Test
public void getFullMatrix_NullParent()
{
    Transform3D t = new Transform3D();
    t.setPosition(10f, 30f, 50f);
    t.getRotation()
     .rotateLocalX((float) Math.toRadians(90f));
    t.setSize(4f);

    Matrix4f mat = new Matrix4f();
    t.getFullMatrix(mat);

    Matrix4f mat2 = new Matrix4f();
    mat2.translate(10f, 30f, 50f);
    mat2.rotate(new Quaternionf().rotateLocalX((float) Math.toRadians(90f)));
    mat2.scale(4f);

    Assert.assertEquals(mat2, mat);
}
 
Example 7
Source File: Transform3D.java    From WraithEngine with Apache License 2.0 5 votes vote down vote up
/**
 * Calculates the local matrix for this transform, and stores it in the output
 * matrix parameter.
 *
 * @param out
 *     - The matrix to store the output into.
 */
public void getLocalMatrix(Matrix4f out)
{
    out.identity();
    out.translate(position);
    out.rotate(rotation);
    out.scale(size);
}
 
Example 8
Source File: Transform3DTest.java    From WraithEngine with Apache License 2.0 5 votes vote down vote up
@Test
public void getFullMatrix()
{
    Transform3D parent = new Transform3D();
    parent.setPosition(10f, 30f, 50f);
    parent.setRotation(new Quaternionf().rotateLocalX((float) Math.toRadians(90f)));
    parent.setSize(4f);

    Transform3D t = new Transform3D();

    t.setParent(parent);
    t.setPosition(-5f, -17f, -5f);
    t.getRotation()
     .rotateLocalX((float) Math.toRadians(45f));
    t.setSize(0.5f);

    Matrix4f mat = new Matrix4f();
    t.getFullMatrix(mat);

    Matrix4f mat2 = new Matrix4f();
    mat2.translate(-5f, -17f, -5f);
    mat2.rotate(new Quaternionf().rotateLocalX((float) Math.toRadians(45f)));
    mat2.scale(0.5f);

    Matrix4f tempMat2 = new Matrix4f();
    parent.getLocalMatrix(tempMat2);
    mat2 = tempMat2.mul(mat2);

    Assert.assertEquals(mat2, mat);
}
 
Example 9
Source File: Transform3D.java    From WraithEngine with Apache License 2.0 5 votes vote down vote up
/**
 * Calculates the local matrix for this transform, and stores it in the output
 * matrix parameter.
 *
 * @param out
 *     - The matrix to store the output into.
 */
public void getLocalMatrix(Matrix4f out)
{
    out.identity();
    out.translate(position);
    out.rotate(rotation);
    out.scale(size);
}
 
Example 10
Source File: Transform3DTest.java    From WraithEngine with Apache License 2.0 5 votes vote down vote up
@Test
public void getFullMatrix()
{
    Transform3D parent = new Transform3D();
    parent.setPosition(10f, 30f, 50f);
    parent.setRotation(new Quaternionf().rotateLocalX((float) Math.toRadians(90f)));
    parent.setSize(4f);

    Transform3D t = new Transform3D();

    t.setParent(parent);
    t.setPosition(-5f, -17f, -5f);
    t.getRotation()
     .rotateLocalX((float) Math.toRadians(45f));
    t.setSize(0.5f);

    Matrix4f mat = new Matrix4f();
    t.getFullMatrix(mat);

    Matrix4f mat2 = new Matrix4f();
    mat2.translate(-5f, -17f, -5f);
    mat2.rotate(new Quaternionf().rotateLocalX((float) Math.toRadians(45f)));
    mat2.scale(0.5f);

    Matrix4f tempMat2 = new Matrix4f();
    parent.getLocalMatrix(tempMat2);
    mat2 = tempMat2.mul(mat2);

    Assert.assertEquals(mat2, mat);
}
 
Example 11
Source File: MeshTransformUtil.java    From cineast with MIT License 4 votes vote down vote up
/**
 * Performs a Karhunen–Loève (KHL) Transformation on the provided Mesh by executing the following steps
 * according to [1]:
 *
 * <ol>
 *     <li>Move the Mesh's barycenter to the origin. </li>
 *     <li>Rotate the Mesh's so that its PCA axis becomes aligned with the coordinate-system's axis. </li>
 *     <li>Scale the mesh (usually to unit-size).</li>
 * </ol>
 *
 * The KHL Transform is a default tool for pose estimation.
 *
 * <strong>Important:</strong> This transformation is done in place and affects the original Mesh.
 *
 * @param mesh Mesh that should be transformed.
 * @param size Length of the longest edge of the mesh's bounding box (for scaling).
 */
public static void khlTransformInPlace(WritableMesh mesh, float size) {
    /* Check if Mesh is empty. */
    if (mesh.isEmpty()) {
      return;
    }

     /* 1) Scale the mesh. */
    MeshTransformUtil.scaleInPlace(mesh, size);

    /* 2) Rotate the mesh along its PCA axis. */
    Vector3fc barycenter = mesh.barycenter();

    /* Prepare an empty covariance matrix. */
    DMatrixRMaj covariance = new DMatrixRMaj(3,3);

    List<Mesh.Face> faces = mesh.getFaces();
    long vertices = 0;
    for (Mesh.Face face : faces) {
        for (Mesh.Vertex v : face.getVertices()) {
            Vector3f vm = (new Vector3f(v.getPosition())).sub(barycenter);
            covariance.add(0,0, vm.x * vm.x);
            covariance.add(0,1, vm.y * vm.x);
            covariance.add(0,2, vm.z * vm.x);
            covariance.add(1,0, vm.y * vm.x);
            covariance.add(1,1, vm.y * vm.y);
            covariance.add(1,2, vm.y * vm.z);
            covariance.add(2,0, vm.z * vm.x);
            covariance.add(2,1, vm.z * vm.y);
            covariance.add(2,2, vm.z * vm.z);
            vertices++;
        }
    }

    /* Normalizes the matrix. */
    for (int i=0; i<covariance.data.length; i++) {
        covariance.data[i] /= vertices;
    }

    /* 2a) List of eigenvectors sorted by eigenvalue in descending order. */
    List<Pair<Float,Vector3f>> eigenvectors = MeshTransformUtil.getEigenvectors(covariance);

    Matrix4f rotation = new Matrix4f();

    /* 2b) Apply first rotation: Largest spread should occur along the x-axis. */
    Vector3f xaxis = new Vector3f(1.0f,0.0f,0.0f);
    Vector3f yaxis = new Vector3f(0.0f,1.0f,0.0f);
    float angleX = xaxis.angle(eigenvectors.get(2).second);
    rotation.rotate(angleX, xaxis.cross(eigenvectors.get(2).second));

    /* 2c) Apply second rotation: Second largest spread should occur along the y-axis. */
    float angleY = yaxis.angle(eigenvectors.get(1).second);
    rotation.rotate(angleY, yaxis.cross(eigenvectors.get(1).second));
    mesh.transform(rotation);

    /* 3) Center the mesh. */
    MeshTransformUtil.centerInPlace(mesh);
}