use of maspack.util.NumberFormat in project artisynth_core by artisynth.
the class WavefrontToMaya method stringifyPolyline.
private static String stringifyPolyline(int[] vtxIndices, ArrayList<Point3d> vtxList, String name, NumberFormat fmt) {
if (fmt == null) {
fmt = new NumberFormat();
}
String lineStr = "";
String transname = name + "trans";
lineStr += "createNode transform -n \"" + transname + "\";\n";
lineStr += "createNode nurbsCurve -n \"" + name + "\";\n";
lineStr += "\tsetAttr -k off \".v\";\n";
lineStr += "\tsetAttr \".cc\" -type \"nurbsCurve\"\n";
int nverts = vtxIndices.length;
lineStr += "\t\t1 " + (nverts - 1) + " 0 no 3\n";
lineStr += "\t\t" + nverts;
for (int i = 0; i < nverts; i++) {
lineStr += " " + i;
}
lineStr += "\n\t\t" + nverts + "\n";
// vertex locations
for (int i = 0; i < nverts; i++) {
Point3d pos = vtxList.get(vtxIndices[i]);
lineStr += "\t\t" + fmt.format(pos.x) + " " + fmt.format(pos.y) + " " + fmt.format(pos.z) + "\n";
}
lineStr += "\t\t;\n";
return lineStr;
}
use of maspack.util.NumberFormat in project artisynth_core by artisynth.
the class PolygonalMesh method write.
/**
* Writes this mesh to a File, using an Alias Wavefront "obj" file
* format. Behaves the same as {@link
* #write(java.io.PrintWriter,maspack.util.NumberFormat,boolean,boolean)}
* with <code>zeroIndexed</code> and <code>facesClockwise</code> set to
* false.
*
* @param file
* File to write this mesh to
* @param fmtStr
* format string for writing the vertex coordinates. If <code>null</code>,
* a format of <code>"%.8g"</code> is assumed.
* @param zeroIndexed
* if true, index numbering for mesh vertices starts at 0. Otherwise,
* numbering starts at 1.
*/
public void write(File file, String fmtStr, boolean zeroIndexed) throws IOException {
if (fmtStr == null) {
fmtStr = "%.8g";
}
NumberFormat fmt = new NumberFormat(fmtStr);
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file)));
write(pw, fmt, zeroIndexed, /*facesClockwise=*/
false);
}
use of maspack.util.NumberFormat in project artisynth_core by artisynth.
the class PointMesh method write.
/**
* Writes this mesh to a PrintWriter, using an Alias Wavefront "obj" file
* format. Vertices are printed first, each starting with the letter "v" and
* followed by x, y, and z coordinates. Normals, if present, are printed
* next, starting with the letter "vn" and followed by x, y, and z
* coordinates.
*
* <p>
* The format used to print vertex coordinates is specified by a
* {@link maspack.util.NumberFormat NumberFormat}.
*
* @param pw
* PrintWriter to write this mesh to
* @param fmt
* (optional) format for writing the vertex and normals coordinates. If <code>null</code>,
* a format of <code>"%.8g"</code> is assumed.
* @param zeroIndexed
* if true, index numbering for mesh vertices starts at 0. Otherwise,
* numbering starts at 1.
*/
public void write(PrintWriter pw, NumberFormat fmt, boolean zeroIndexed) throws IOException {
if (fmt == null) {
fmt = new NumberFormat("%.8g");
}
for (Vertex3d vertex : myVertices) {
Point3d pnt = vertex.pnt;
pw.println("v " + fmt.format(pnt.x) + " " + fmt.format(pnt.y) + " " + fmt.format(pnt.z));
}
if (myNormals != null) {
for (Vector3d nrm : myNormals) {
pw.println("vn " + fmt.format(nrm.x) + " " + fmt.format(nrm.y) + " " + fmt.format(nrm.z));
}
}
pw.flush();
}
use of maspack.util.NumberFormat in project artisynth_core by artisynth.
the class CholeskyDecompositionTest method timingTests.
private void timingTests() {
int numTrials = 10;
int timingCnt = 1;
int[] matsizes = new int[] { 32, 16, 32, 64, 128, 256 };
NumberFormat ifmt = new NumberFormat("%3d");
NumberFormat ffmt = new NumberFormat("%8.2f");
System.out.println("\ntimes are in usec\n");
System.out.println("matsize factor add row/col del row/col");
// XXX XXXXX.XX XXXXX.XX XXXXX.XX
FunctionTimer timer = new FunctionTimer();
for (int k = 0; k < matsizes.length; k++) {
int n = matsizes[k];
MatrixNd M = new MatrixNd(n, n);
VectorNd col = new VectorNd(n);
CholeskyDecomposition chol = new CholeskyDecomposition(n);
double factorTime = 0;
double addTime = 0;
double delTime = 0;
for (int cnt = 0; cnt < numTrials; cnt++) {
M.setRandom();
M.mulTranspose(M);
M.getColumn(0, col);
double x0 = col.get(0);
for (int i = 0; i < n - 1; i++) {
col.set(i, col.get(i + 1));
}
col.set(n - 1, x0);
timer.start();
for (int i = 0; i < timingCnt; i++) {
chol.factor(M);
}
timer.stop();
factorTime += timer.getTimeUsec() / timingCnt;
timer.start();
for (int i = 0; i < timingCnt; i++) {
chol.deleteRowAndColumn(0);
}
timer.stop();
delTime += timer.getTimeUsec() / timingCnt;
timer.start();
for (int i = 0; i < timingCnt; i++) {
chol.addRowAndColumn(col);
}
timer.stop();
addTime += timer.getTimeUsec() / timingCnt;
}
factorTime /= numTrials;
addTime /= numTrials;
delTime /= numTrials;
if (k > 0) {
System.out.println(" " + ifmt.format(n) + " " + ffmt.format(factorTime) + " " + ffmt.format(addTime) + " " + ffmt.format(delTime));
}
}
}
use of maspack.util.NumberFormat in project artisynth_core by artisynth.
the class AffineOrthogonalTest method doSkewTest.
public static void doSkewTest() {
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 } };
Vector3d skewVec = new Vector3d(2, 1, 2);
Vector3d axis = new Vector3d(1, 1, 0);
double angle = 1.7;
Vector3d translation = new Vector3d(5, 2, 3);
Vector3d scaling = new Vector3d(3, 4, 1);
Matrix3d skew = new Matrix3d(1, -skewVec.z, skewVec.y, skewVec.z, 1, -skewVec.x, -skewVec.y, skewVec.x, 1);
RotationMatrix3d R = new RotationMatrix3d();
R.setAxisAngle(new AxisAngle(axis, angle));
AffineTransform3d trans = new AffineTransform3d();
trans.setRotation(R);
trans.A.mul(skew);
trans.applyScaling(scaling.x, scaling.y, scaling.z);
trans.setTranslation(translation);
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>();
for (int i = 0; i < pnts.length; i++) {
Point3d v = new Point3d(pnts[i]);
Point3d w = new Point3d(pnts[i]);
w.transform(trans);
p.add(w);
q.add(v);
}
System.out.println("Original: \n" + trans);
AffineTransform3d transb = new AffineTransform3d();
transb.fitOrthogonal(p, q);
System.out.println("Computed: \n" + transb);
AffineTransform3d transc = new AffineTransform3d();
transc.fitOrthogonal(p, q, 1e-15);
System.out.println("Iterated: \n" + transc);
// 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();
if (tmp.get(i) < 0) {
s = -s;
}
tmp.scale(1.0 / s);
scaleb.set(i, s);
Rb.setColumn(i, tmp);
}
RotationMatrix3d Rb2 = new RotationMatrix3d();
Rb2.setSubMatrix(0, 0, Rb);
AxisAngle aab = new AxisAngle(Rb2);
System.out.println("Translation: " + translationb);
System.out.println("Rotation: " + aab.axis + ", " + fmt.format(aab.angle));
System.out.println("Scaling: " + scaleb);
double err = 0;
double errOrig = 0;
double errIter = 0;
for (int i = 0; i < q.size(); i++) {
Vector3d diff = new Vector3d(q.get(i));
diff.sub(p.get(i));
errOrig += diff.normSquared();
diff.set(q.get(i));
diff.transform(transb);
diff.sub(p.get(i));
err += diff.normSquared();
diff.set(q.get(i));
diff.transform(transc);
diff.sub(p.get(i));
errIter += diff.normSquared();
}
System.out.println("P = [");
for (int i = 0; i < q.size(); i++) {
System.out.println(p.get(i));
}
System.out.println("];");
System.out.println("Q = [");
for (int i = 0; i < q.size(); i++) {
Point3d pnt = new Point3d(q.get(i));
pnt.transform(transb);
System.out.println(pnt);
}
System.out.println("];");
System.out.println();
System.out.println("Original error = " + errOrig);
System.out.println("Error = " + err);
System.out.println("Iterated Error = " + errIter);
}
Aggregations