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;
}
}
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);
}
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;
}
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;
}
}
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);
}
Aggregations