Java Code Examples for org.joml.Vector3f#angle()

The following examples show how to use org.joml.Vector3f#angle() . 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: MeshColoringUtil.java    From cineast with MIT License 6 votes vote down vote up
/**
 *
 * @param mesh
 */
public static void normalColoring(WritableMesh mesh) {
    Vector3f axis = new Vector3f(1.0f,0.0f, 0.0f);

    Vector3fc center = mesh.barycenter();
    Mesh.Vertex farthestVertex = MeshMathUtil.farthestVertex(mesh, center);
    float ds_max = center.distance(farthestVertex.getPosition());

    for (int i=0; i<mesh.numberOfVertices(); i++) {
        float hue = (float)(axis.angle(mesh.getVertex(i).getNormal())/Math.PI);
        float saturation = center.distance(mesh.getVertex(i).getPosition())/ds_max;

        Color color = Color.getHSBColor(hue, saturation, 1.0f);
        mesh.updateColor(i, color);
    }
}
 
Example 2
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);
}