Search in sources :

Example 51 with NumberFormat

use of maspack.util.NumberFormat in project artisynth_core by artisynth.

the class MatrixBase method write.

/**
 * Writes a principal submatrix of this matrix to a specified file, and
 * returns the PrintWriter that was created to do the writing.
 *
 * @param fileName
 * Path name for the file to written
 * @param msg
 * Optional message - if not <code>null</code>, is printed on
 * a separate line preceeding the matrix information.
 * @param wfmt
 * specifies the matrix output format
 * @param nrows number of rows in the principle submatrix
 * @param ncols number of columns in the principle submatrix
 * @return PrintWriter used to do the writing
 */
public PrintWriter write(String fileName, String msg, WriteFormat wfmt, int nrows, int ncols) throws IOException {
    NumberFormat fmt = new NumberFormat("%g");
    PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
    if (msg != null) {
        pw.println(msg);
    }
    write(pw, fmt, wfmt, nrows, ncols);
    return pw;
}
Also used : FileWriter(java.io.FileWriter) NumberFormat(maspack.util.NumberFormat) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Example 52 with NumberFormat

use of maspack.util.NumberFormat in project artisynth_core by artisynth.

the class TrackingController method apply.

/**
 * Applies the controller, estimating and setting the next
 * set of muscle activations
 */
public void apply(double t0, double t1) {
    // System.out.println("dt = "+(t1-t0)+"     h = "+ TimeBase.round(t1 - t0));
    if (getDebug()) {
        // cleans up the console
        System.out.println("\n--- t = " + t1 + " ---");
    }
    if (t0 == 0) {
        // XXX need better way to zero excitations on reset
        myCostFunction.setSize(numExcitations());
        myExcitations = new VectorNd(numExcitations());
    }
    if (!isEnabled()) {
        return;
    }
    // need to save forces so that we can restore them at the end
    VectorNd savedForces = new VectorNd();
    myMech.getForces(savedForces);
    prevExcitations.set(myExcitations);
    SparseBlockMatrix Jc = (myForceTerm == null ? null : myForceTerm.getForceJacobian());
    SparseBlockMatrix Jm = (myMotionTerm == null ? null : myMotionTerm.getVelocityJacobian());
    // update and store inverse data
    myMotionForceData.update(t0, t1, Jm, Jc);
    if (myMotionTerm.useDeltaAct) {
        VectorNd deltaActivations = myCostFunction.solve(t0, t1);
        myExcitations.add(deltaActivations);
        if (getDebug()) {
            System.out.println("da = [" + deltaActivations.toString("%.4f") + "];");
        }
    } else {
        myExcitations.set(myCostFunction.solve(t0, t1));
    }
    /*
       * limit excitation jumps
       */
    // System.out.println ("excitations="+myExcitations);
    NumberFormat f4 = new NumberFormat("%.4f");
    /* debug info */
    if (getDebug()) {
    // System.out.println("ex = ["+myExcitations.toString (f4)+"];");
    // System.out.println("lb = ["+lowerBound.toString (f4)+"];");
    // System.out.println("ub = ["+upperBound.toString (f4)+"];");
    }
    setExcitations(myExcitations, 0);
    myMech.setForces(savedForces);
// if (kTerm != null) {
// System.out.println("K* = "+kTerm.getStiffnessTargetVec().toString("%8.2f"));
// System.out.println("K  = "+kTerm.getStiffnessVec().toString("%8.2f")+"\n");
// }
// if (cTerm != null) {
// System.out.println("C* = "+cTerm.getComplianceTargetVec().toString("%8.6f"));
// System.out.println("C  = "+cTerm.getComplianceVec().toString("%8.6f")+"\n");
// }
}
Also used : SparseBlockMatrix(maspack.matrix.SparseBlockMatrix) VectorNd(maspack.matrix.VectorNd) NumberFormat(maspack.util.NumberFormat)

Example 53 with NumberFormat

use of maspack.util.NumberFormat in project artisynth_core by artisynth.

the class StlWriter method writeMesh.

public void writeMesh(PolygonalMesh mesh) throws IOException {
    NumberFormat fmt = myFmt;
    PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(myOstream)));
    String name = mesh.getName();
    if (name == null) {
        name = "";
    }
    pw.println("solid " + name);
    for (Face face : mesh.getFaces()) {
        Vector3d n = face.getNormal();
        if (face.isTriangle()) {
            pw.println("facet normal " + fmt.format((float) n.x) + " " + fmt.format((float) n.y) + " " + fmt.format((float) n.z));
        } else {
            pw.println("facet normal 0 0 0");
        }
        pw.println("  outer loop");
        for (int i = 0; i < face.numVertices(); i++) {
            Point3d pos = face.getVertex(i).getPosition();
            pw.println("    vertex " + fmt.format((float) pos.x) + " " + fmt.format((float) pos.y) + " " + fmt.format((float) pos.z));
        }
        pw.println("  endloop");
        pw.println("endfacet");
    }
    pw.println("endsolid " + name);
    pw.close();
}
Also used : Vector3d(maspack.matrix.Vector3d) Point3d(maspack.matrix.Point3d) OutputStreamWriter(java.io.OutputStreamWriter) Face(maspack.geometry.Face) NumberFormat(maspack.util.NumberFormat) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Example 54 with NumberFormat

use of maspack.util.NumberFormat in project artisynth_core by artisynth.

the class LUDecompositionTest method timingTests.

private void timingTests() {
    // Random rand = RandomGenerator.get();
    int baseTimingCnt = 100000;
    int numTrials = 10;
    int[] matsizes = new int[] { 4, 8, 16, 32, 4, 8, 16, 32, 64 };
    NumberFormat ifmt = new NumberFormat("%3d");
    NumberFormat ffmt = new NumberFormat("%7.2f");
    System.out.println("matsize    time");
    for (int k = 0; k < matsizes.length; k++) {
        int n = matsizes[k];
        int timingCnt = baseTimingCnt / (n * n);
        MatrixNd M = new MatrixNd(n, n);
        LUDecomposition lu = new LUDecomposition(n);
        FunctionTimer timer = new FunctionTimer();
        double ludTime = 0;
        for (int cnt = 0; cnt < numTrials; cnt++) {
            M.setRandom();
            timer.start();
            for (int i = 0; i < timingCnt; i++) {
                lu.factor(M);
            }
            timer.stop();
            ludTime += timer.getTimeUsec() / timingCnt;
        }
        ludTime /= numTrials;
        System.out.println("  " + ifmt.format(n) + "    " + ffmt.format(ludTime));
    }
}
Also used : FunctionTimer(maspack.util.FunctionTimer) NumberFormat(maspack.util.NumberFormat)

Example 55 with NumberFormat

use of maspack.util.NumberFormat in project artisynth_core by artisynth.

the class AffineOrthogonalTest method doTest.

public static void doTest() {
    double[][] pnts = { { -1, -1, -1 }, { -1, 1, -1 }, { -1, 1, 1 }, { -1, -1, 1 }, { 1, -1, -1 }, { 1, 1, -1 }, { 1, 1, 1 }, { 1, -1, 1 } };
    NumberFormat fmt = new NumberFormat("%.5f");
    Vector3d.setDefaultFormat(fmt.toString());
    Matrix3d.setDefaultFormat(fmt.toString());
    ArrayList<Point3d> p = new ArrayList<Point3d>();
    ArrayList<Point3d> q = new ArrayList<Point3d>();
    AffineTransform3d transf = new AffineTransform3d();
    Vector3d axis = new Vector3d(1, 1, 0);
    double angle = 1.7;
    Vector3d translation = new Vector3d(5, 2, 3);
    Vector3d scaling = new Vector3d(-13, -2, -7);
    axis.normalize();
    transf.setRotation(new AxisAngle(axis, angle));
    transf.setTranslation(translation);
    transf.applyScaling(scaling.x, scaling.y, scaling.z);
    for (int i = 0; i < pnts.length; i++) {
        Point3d v = new Point3d(pnts[i]);
        Point3d w = new Point3d(pnts[i]);
        w.transform(transf);
        p.add(w);
        q.add(v);
    }
    AffineTransform3d transb = new AffineTransform3d();
    transb.fitOrthogonal(p, q);
    // decompose:
    Vector3d translationb = transb.p;
    Matrix3d Rb = new Matrix3d(transb.A);
    Vector3d scaleb = new Vector3d();
    Vector3d tmp = new Vector3d();
    for (int i = 0; i < 3; i++) {
        Rb.getColumn(i, tmp);
        double s = tmp.norm();
        tmp.scale(1.0 / s);
        scaleb.set(i, s);
        Rb.setColumn(i, tmp);
    }
    // check if we have a flip instead of rotation
    double Rbdet = Rb.determinant();
    if (Rbdet < 0) {
        Rbdet = -Rbdet;
        Rb.getColumn(2, tmp);
        tmp.scale(-1);
        scaleb.set(2, -scaleb.get(2));
        Rb.setColumn(2, tmp);
    }
    RotationMatrix3d Rb2 = new RotationMatrix3d();
    Rb2.setSubMatrix(0, 0, Rb);
    AxisAngle aab = new AxisAngle(Rb2);
    System.out.println("Translation: \t" + translationb + " \t/ " + translation);
    System.out.println("Rotation: \t" + aab.axis + ", " + fmt.format(aab.angle) + " \t/ " + axis + ", " + angle);
    System.out.println("Scaling: \t" + scaleb + " \t/ " + scaling);
    // re-check based on new info
    AffineTransform3d transc = new AffineTransform3d();
    transc.setRotation(aab);
    transc.setTranslation(translationb);
    transc.applyScaling(scaleb.x, scaleb.y, scaleb.z);
    AffineTransform3d transd = new AffineTransform3d();
    transd.fitOrthogonal(p, q, 1e-15);
    System.out.println("Original: \n" + transf);
    System.out.println("Computed: \n" + transb);
    System.out.println("Recomputed transform:\n" + transc);
    System.out.println("Iterated transform:\n" + transd);
}
Also used : ArrayList(java.util.ArrayList) NumberFormat(maspack.util.NumberFormat)

Aggregations

NumberFormat (maspack.util.NumberFormat)55 PrintWriter (java.io.PrintWriter)15 IOException (java.io.IOException)8 Point3d (maspack.matrix.Point3d)6 BufferedWriter (java.io.BufferedWriter)5 File (java.io.File)5 PolygonalMesh (maspack.geometry.PolygonalMesh)5 Point (artisynth.core.mechmodels.Point)4 FileWriter (java.io.FileWriter)4 ArrayList (java.util.ArrayList)4 SparseBlockMatrix (maspack.matrix.SparseBlockMatrix)4 ReaderTokenizer (maspack.util.ReaderTokenizer)4 TestException (maspack.util.TestException)4 Face (maspack.geometry.Face)3 VectorNd (maspack.matrix.VectorNd)3 FunctionTimer (maspack.util.FunctionTimer)3 InternalErrorException (maspack.util.InternalErrorException)3 ContactPoint (artisynth.core.mechmodels.ContactPoint)2 PointParticleAttachment (artisynth.core.mechmodels.PointParticleAttachment)2 Font (java.awt.Font)2