Search in sources :

Example 1 with SVDecomposition3d

use of maspack.matrix.SVDecomposition3d in project artisynth_core by artisynth.

the class FemMuscleModel method computeAverageFiberDirection.

/**
 * Computes the average fiber direction in the vicinity of a point based on
 * the line segments contained in a PolylineMesh. Returns the number of
 * supporting line segments used for the calculation. If no segments were
 * found, the method returns 0 and the direction is undefined.
 *
 * @param dir returns the normalized direction
 * @param pos position at which direction should be computed
 * @param rad radius of influence within which polyline mesh segments are
 * considerd
 * @param mesh mesh containing line segments used to determine the direction
 * @return number of supporting line segment
 */
public static int computeAverageFiberDirection(Vector3d dir, Point3d pos, double rad, PolylineMesh mesh) {
    BVTree bvh = mesh.getBVTree();
    ArrayList<BVNode> nodes = new ArrayList<BVNode>();
    Matrix3d cov = new Matrix3d();
    SVDecomposition3d svd = new SVDecomposition3d();
    Vector3d tmp = new Vector3d();
    Matrix3d tmp2 = new Matrix3d();
    bvh.intersectSphere(nodes, pos, rad);
    dir.setZero();
    int nsegs = 0;
    // for computing sign of direction vector
    Vector3d segmentSum = new Vector3d();
    // System.out.println("p=[");
    for (BVNode n : nodes) {
        Boundable[] elements = n.getElements();
        for (int i = 0; i < elements.length; i++) {
            LineSegment seg = (LineSegment) elements[i];
            seg = getSegmentInsideSphere(seg, pos, rad);
            if (seg != null) {
                tmp.sub(seg.myVtx1.pnt, seg.myVtx0.pnt);
                if (tmp.norm() >= 1e-8 * rad) {
                    // System.out.println(seg.myVtx0.getPosition() + " " +
                    // seg.myVtx1.getPosition());
                    nsegs++;
                    // prepare to average directions using SVD
                    computeCov(tmp2, tmp);
                    cov.add(tmp2);
                    segmentSum.add(tmp);
                }
            }
        }
    }
    if (nsegs > 0) {
        // we are technically including both +/- directions, so
        // we have twice the number of points
        cov.scale(2.0 / (2.0 * nsegs - 1));
        try {
            svd.factor(cov);
        } catch (Exception e) {
        // System.err.println(e.getMessage());
        }
        // principal components
        tmp2 = svd.getU();
        tmp2.getColumn(0, dir);
        dir.normalize();
        // most line segments
        if (dir.dot(segmentSum) < 0) {
            dir.scale(-1);
        }
        return nsegs;
    } else {
        return 0;
    }
}
Also used : BVTree(maspack.geometry.BVTree) Matrix3d(maspack.matrix.Matrix3d) SymmetricMatrix3d(maspack.matrix.SymmetricMatrix3d) Vector3d(maspack.matrix.Vector3d) BVNode(maspack.geometry.BVNode) ArrayList(java.util.ArrayList) SVDecomposition3d(maspack.matrix.SVDecomposition3d) Boundable(maspack.geometry.Boundable) IOException(java.io.IOException) LineSegment(maspack.geometry.LineSegment)

Example 2 with SVDecomposition3d

use of maspack.matrix.SVDecomposition3d in project artisynth_core by artisynth.

the class StiffnessWarper3d method computeRotation.

/**
 * Computes a corotated rotation based on the deformation gradient
 * @param F deformation gradient
 * @param P symmetric part of gradient after rotation
 */
protected void computeRotation(Matrix3d F, SymmetricMatrix3d P) {
    if (R == null) {
        R = new RotationMatrix3d();
    }
    SVDecomposition3d SVD = new SVDecomposition3d();
    SVD.polarDecomposition(R, P, F);
}
Also used : SVDecomposition3d(maspack.matrix.SVDecomposition3d) RotationMatrix3d(maspack.matrix.RotationMatrix3d)

Example 3 with SVDecomposition3d

use of maspack.matrix.SVDecomposition3d in project artisynth_core by artisynth.

the class LinearMaterialBase method computeRotation.

protected RotationMatrix3d computeRotation(Matrix3d F, SymmetricMatrix3d P) {
    if (mySVD == null) {
        mySVD = new SVDecomposition3d();
    }
    RotationMatrix3d R = new RotationMatrix3d();
    mySVD.polarDecomposition(R, P, F);
    return R;
}
Also used : SVDecomposition3d(maspack.matrix.SVDecomposition3d) RotationMatrix3d(maspack.matrix.RotationMatrix3d)

Example 4 with SVDecomposition3d

use of maspack.matrix.SVDecomposition3d in project artisynth_core by artisynth.

the class MFreeMuscleModel method computeAverageFiberDirection.

public static boolean computeAverageFiberDirection(Vector3d dir, Point3d pos, double rad, PolylineMesh mesh) {
    BVTree bvh = mesh.getBVTree();
    ArrayList<BVNode> nodes = new ArrayList<BVNode>();
    Matrix3d cov = new Matrix3d();
    SVDecomposition3d svd = new SVDecomposition3d();
    Vector3d tmp = new Vector3d();
    Matrix3d tmp2 = new Matrix3d();
    bvh.intersectSphere(nodes, pos, rad);
    dir.setZero();
    int nsegs = 0;
    // for computing sign of direction vector
    Vector3d segmentSum = new Vector3d();
    // System.out.println("p=[");
    for (BVNode n : nodes) {
        Boundable[] elements = n.getElements();
        for (int i = 0; i < elements.length; i++) {
            LineSegment seg = (LineSegment) elements[i];
            seg = getSegmentInsideSphere(seg, pos, rad);
            if (seg != null) {
                tmp.sub(seg.myVtx1.pnt, seg.myVtx0.pnt);
                if (tmp.norm() >= 1e-8 * rad) {
                    // System.out.println(seg.myVtx0.getPosition() + " " +
                    // seg.myVtx1.getPosition());
                    nsegs++;
                    // prepare to average directions using SVD
                    computeCov(tmp2, tmp);
                    cov.add(tmp2);
                    segmentSum.add(tmp);
                }
            }
        }
    }
    if (nsegs > 0) {
        // we are technically including both +/- directions, so
        // we have twice the number of points
        cov.scale(2.0 / (2.0 * nsegs - 1));
        try {
            svd.factor(cov);
        } catch (Exception e) {
        // System.err.println(e.getMessage());
        }
        // principal components
        tmp2 = svd.getU();
        tmp2.getColumn(0, dir);
        dir.normalize();
        // most line segments
        if (dir.dot(segmentSum) < 0) {
            dir.scale(-1);
        }
        return true;
    } else {
        return false;
    }
}
Also used : BVTree(maspack.geometry.BVTree) Matrix3d(maspack.matrix.Matrix3d) SymmetricMatrix3d(maspack.matrix.SymmetricMatrix3d) Vector3d(maspack.matrix.Vector3d) BVNode(maspack.geometry.BVNode) ArrayList(java.util.ArrayList) SVDecomposition3d(maspack.matrix.SVDecomposition3d) Boundable(maspack.geometry.Boundable) IOException(java.io.IOException) LineSegment(maspack.geometry.LineSegment)

Example 5 with SVDecomposition3d

use of maspack.matrix.SVDecomposition3d in project artisynth_core by artisynth.

the class MatricesUBO method updateMatrices.

public void updateMatrices(GL3 gl, Matrix4d proj, RigidTransform3d view, AffineTransform3dBase model, AffineTransform2dBase texture) {
    ByteBuffer buff = getBuffer();
    // model view
    AffineTransform3d modelview = new AffineTransform3d(view);
    modelview.mul(model);
    // pvm matrix
    buff.position(getByteOffset(PVM_IDX));
    mul(proj, modelview, buff);
    // model-view
    buff.position(getByteOffset(VM_IDX));
    putMatrix(buff, modelview);
    // model matrix
    buff.position(getByteOffset(M_IDX));
    putMatrix(buff, model);
    // normal
    buff.position(getByteOffset(NORMAL_IDX));
    if (model instanceof RigidTransform3d) {
        putMatrix(buff, modelview);
    } else {
        // invert model matrix
        Matrix3d A = new Matrix3d(model.getMatrix());
        if (!A.invert()) {
            // deal with non-invertible
            SVDecomposition3d svd3 = new SVDecomposition3d(model.getMatrix());
            svd3.pseudoInverse(A);
        }
        mulTranspose4x4(view.R, A, buff);
    }
    buff.position(getByteOffset(TEXTURE_IDX));
    putMatrix(buff, texture);
    buff.flip();
    update(gl, buff);
}
Also used : RigidTransform3d(maspack.matrix.RigidTransform3d) Matrix3d(maspack.matrix.Matrix3d) SVDecomposition3d(maspack.matrix.SVDecomposition3d) ByteBuffer(java.nio.ByteBuffer) AffineTransform3d(maspack.matrix.AffineTransform3d)

Aggregations

SVDecomposition3d (maspack.matrix.SVDecomposition3d)9 Matrix3d (maspack.matrix.Matrix3d)7 RotationMatrix3d (maspack.matrix.RotationMatrix3d)4 Vector3d (maspack.matrix.Vector3d)4 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 BVNode (maspack.geometry.BVNode)2 BVTree (maspack.geometry.BVTree)2 Boundable (maspack.geometry.Boundable)2 LineSegment (maspack.geometry.LineSegment)2 AffineTransform3d (maspack.matrix.AffineTransform3d)2 Point3d (maspack.matrix.Point3d)2 SymmetricMatrix3d (maspack.matrix.SymmetricMatrix3d)2 ByteBuffer (java.nio.ByteBuffer)1 RigidTransform3d (maspack.matrix.RigidTransform3d)1 ScaledRigidTransform3d (maspack.matrix.ScaledRigidTransform3d)1