Search in sources :

Example 6 with ArgParser

use of argparser.ArgParser in project artisynth_core by artisynth.

the class MeshCollisionViewer method main.

public static void main(String[] args) {
    IntHolder width = new IntHolder(640);
    IntHolder height = new IntHolder(480);
    ArgParser parser = new ArgParser("java maspack.geometry.TwoMeshViewer [options] meshFile1 meshFile2");
    parser.addOption("-width %d #width (pixels)", width);
    parser.addOption("-height %d #height (pixels)", height);
    parser.addOption("-drawAxes %v #draw coordinate axes", drawAxes);
    parser.addOption("-drawEdges %v #draw mesh edges", drawEdges);
    parser.addOption("-noDrawFaces %v #do not draw faces", noDrawFaces);
    parser.addOption("-edgeColor %fX3 #edge color", edgeColor);
    parser.addOption("-axisLength %f #coordinate axis length", axisLength);
    parser.addOption("-smooth %v #use smooth shading", smooth);
    parser.addOption("-oneSided %v #draw only front faces", oneSided);
    String[] otherArgs = parser.matchAllArgs(args, 0, 0);
    if (otherArgs == null || otherArgs.length != 2) {
        System.out.println("Usage: " + parser.getSynopsisString());
        System.out.println("Use -help for more info");
        System.exit(1);
    }
    PolygonalMesh mesh1 = null;
    PolygonalMesh mesh2 = null;
    try {
        mesh1 = new PolygonalMesh(new File(otherArgs[0]));
        mesh2 = new PolygonalMesh(new File(otherArgs[1]));
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
    MeshCollisionViewer frame = new MeshCollisionViewer(mesh1, mesh2, width.value, height.value);
    frame.setVisible(true);
}
Also used : IntHolder(argparser.IntHolder) ArgParser(argparser.ArgParser) PolygonalMesh(maspack.geometry.PolygonalMesh) File(java.io.File)

Example 7 with ArgParser

use of argparser.ArgParser in project artisynth_core by artisynth.

the class MeshMaker method main.

public static void main(String[] args) {
    ArgParser parser = new ArgParser("java maspack.geometry.MeshMaker");
    parser.addOption("-file %s #mesh file name", fileName);
    parser.addOption("-radius %f #radius", radius);
    parser.addOption("-wx %f #x width", wx);
    parser.addOption("-wy %f #y width", wy);
    parser.addOption("-wz %f #z width", wz);
    parser.addOption("-nslices %d #number of slices", nslices);
    args = parser.matchAllArgs(args, 0, ArgParser.EXIT_ON_ERROR);
    PolygonalMesh mesh = null;
    if (args[0].equals("sphere")) {
        mesh = MeshFactory.createQuadSphere(radius.value, nslices.value);
    } else if (args[0].equals("box")) {
        mesh = MeshFactory.createQuadBox(wx.value, wy.value, wz.value);
    } else if (args[0].equals("cylinder")) {
        mesh = MeshFactory.createCylinder(radius.value, wz.value, nslices.value);
    } else if (args[0].equals("roundedCylinder")) {
        mesh = MeshFactory.createQuadRoundedCylinder(radius.value, wz.value, nslices.value, /*nsegs=*/
        1, /*flatBottom=*/
        false);
    } else {
        System.err.println("Unknown mesh type: " + args[0]);
        System.exit(1);
    }
    try {
        mesh.write(new PrintWriter(new BufferedWriter(new FileWriter(fileName.value))), "%10.5f");
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
}
Also used : FileWriter(java.io.FileWriter) ArgParser(argparser.ArgParser) PolygonalMesh(maspack.geometry.PolygonalMesh) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Example 8 with ArgParser

use of argparser.ArgParser in project artisynth_core by artisynth.

the class MeshSeparator method main.

public static void main(String[] args) {
    ArgParser parser = new ArgParser("[options] <infileName>");
    // parser.addOption ("-inFile %s #input file name", inFileName);
    parser.addOption("-out %s #base name for output file", outFileName);
    parser.addOption("-format %s #printf-syle format string for vertex output", formatStr);
    int idx = 0;
    while (idx < args.length) {
        try {
            idx = parser.matchArg(args, idx);
            if (parser.getUnmatchedArgument() != null) {
                String fileName = parser.getUnmatchedArgument();
                if (inFileName.value == null) {
                    inFileName.value = fileName;
                } else {
                    System.out.println("Ignoring extra input file " + fileName);
                }
            }
        } catch (Exception e) {
            // malformed or erroneous argument
            parser.printErrorAndExit(e.getMessage());
        }
    }
    if (inFileName.value == null) {
        parser.printErrorAndExit("input file name missing");
    }
    if (outFileName.value == null) {
        int dotIdx = inFileName.value.lastIndexOf('.');
        if (dotIdx == -1) {
            outFileName.value = inFileName.value;
        } else {
            outFileName.value = inFileName.value.substring(0, dotIdx);
        }
    }
    try {
        File meshFile = new File(inFileName.value);
        if (!meshFile.exists()) {
            System.out.println("Error: mesh file " + meshFile.getName() + " not found");
            meshFile = null;
            System.exit(1);
        }
        PolygonalMesh mesh = new PolygonalMesh(meshFile);
        PolygonalMesh[] meshes = mesh.partitionIntoConnectedMeshes();
        if (meshes == null) {
            System.out.println("Mesh " + meshFile + " has no separate components");
            System.exit(0);
        }
        double[] numv = new double[meshes.length];
        int[] idxs = new int[meshes.length];
        for (int i = 0; i < meshes.length; i++) {
            numv[i] = meshes[i].numVertices();
            idxs[i] = i;
        }
        ArraySort.quickSort(numv, idxs);
        for (int i = 0; i < meshes.length; i++) {
            NumberFormat fmt = new NumberFormat("%03d");
            File outFile = new File(outFileName.value + "_" + fmt.format(meshes.length - 1 - i) + ".obj");
            meshes[idxs[i]].write(outFile, formatStr.value);
        }
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
}
Also used : ArgParser(argparser.ArgParser) PolygonalMesh(maspack.geometry.PolygonalMesh) NumberFormat(maspack.util.NumberFormat)

Example 9 with ArgParser

use of argparser.ArgParser in project artisynth_core by artisynth.

the class MeshThicken method main.

public static void main(String[] args) {
    ArgParser parser = new ArgParser("java maspack.apps.MeshThicken [options] ...");
    // parser.addOption ("-file %s #mesh file names", meshFileList);
    parser.addOption("-mesh %s #mesh file", meshFile);
    parser.addOption("-regions %s #region file", regionFile);
    parser.addOption("-skip %d #for .xyzb point meshes, use every n-th point", skipCount);
    parser.matchAllArgs(args);
    // int idx = 0;
    // while (idx < args.length) {
    // try {
    // idx = parser.matchArg (args, idx);
    // }
    // catch (Exception e) {
    // // malformed or erroneous argument
    // parser.printErrorAndExit (e.getMessage());
    // }
    // }
    MeshThicken thickener = new MeshThicken(meshFile.value, regionFile.value);
    // tester.addTestCurves();
    // tester.addBox();
    thickener.getViewer().autoFit();
    thickener.getViewer().setBackgroundColor(new Color(0f, 0f, 0.2f));
    thickener.setVisible(true);
}
Also used : Color(java.awt.Color) ArgParser(argparser.ArgParser)

Example 10 with ArgParser

use of argparser.ArgParser in project artisynth_core by artisynth.

the class MeshTransform method main.

// new StringHolder ("maspack.geometry.PolygonalMesh");
public static void main(String[] args) {
    ArgParser parser = new ArgParser("[options] <infileName>");
    // parser.addOption ("-inFile %s #input file name", inFileName);
    parser.addOption("-out %s #output file name", outFileName);
    parser.addOption("-xyz %fX3 #x,y,z translation values", xyz);
    parser.addOption("-scaleXyz %fX3 #x,y,z scale values", scaleXyz);
    parser.addOption("-laplacianSmooth %fX3 #iter count, lambda, mu", laplacianSmoothing);
    parser.addOption("-removeDisconnectedFaces %v #remove disconnected faces", removeDisconnectedFaces);
    parser.addOption("-removeDisconnectedVertices %v #remove disconnected vertices", removeDisconnectedVertices);
    parser.addOption("-mergeCloseVertices %f #(close vertex distance)/(mesh radius)", closeVertexTol);
    parser.addOption("-axisAngle %fX4 #x,y,z,deg axis-angle values", axisAngle);
    parser.addOption("-scale %f #scaling value", scaling);
    parser.addOption("-format %s #printf-syle format string for vertex output", formatStr);
    // parser.addOption (
    // "-zeroIndexedIn %v #input is zero indexed", zeroIndexedIn);
    // parser.addOption (
    // "-zeroIndexedOut %v #output should be zero indexed", zeroIndexedOut);
    parser.addOption("-class %s #use PolygonalMesh sub class", className);
    parser.addOption("-pointMesh %v #mesh is a point cloud", pointMesh);
    int idx = 0;
    while (idx < args.length) {
        try {
            idx = parser.matchArg(args, idx);
            if (parser.getUnmatchedArgument() != null) {
                String fileName = parser.getUnmatchedArgument();
                if (inFileName.value == null) {
                    inFileName.value = fileName;
                } else {
                    System.out.println("Ignoring extra input " + fileName);
                }
            }
        } catch (Exception e) {
            // malformed or erroneous argument
            parser.printErrorAndExit(e.getMessage());
        }
    }
    MeshBase mesh = null;
    if (inFileName.value == null) {
        parser.printErrorAndExit("input file name missing");
    }
    if (outFileName.value == null) {
        outFileName.value = inFileName.value;
    }
    File outFile = new File(outFileName.value);
    if (outFile.exists()) {
        System.out.print("File " + outFileName.value + " exists. Overwrite? (y/n) ");
        System.out.flush();
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String input = null;
        try {
            input = reader.readLine();
        } catch (Exception e) {
        // ignore
        }
        if (input == null || !input.equalsIgnoreCase("y")) {
            System.out.println("aborting");
            System.exit(1);
        }
    }
    if (!(new File(inFileName.value)).exists()) {
        System.out.println("Error: mesh file " + inFileName.value + " not found");
        System.exit(1);
    }
    GenericMeshReader reader = null;
    try {
        // File meshFile = new File (inFileName.value);
        reader = new GenericMeshReader(inFileName.value);
        if (className.value != null) {
            Class meshClass = Class.forName(className.value);
            Class meshBaseClass = new PolygonalMesh().getClass();
            if (meshClass == null) {
                System.out.println("can't find class " + className.value);
                System.exit(1);
            }
            if (!meshBaseClass.isAssignableFrom(meshClass)) {
                System.out.println(className.value + " is not an instance of " + meshBaseClass.getName());
                System.exit(1);
            }
            Constructor constructor = meshClass.getDeclaredConstructor(new Class[] {});
            if (constructor == null) {
                System.out.println("can't find constructor " + className.value + "()");
                System.exit(1);
            }
            mesh = (PolygonalMesh) constructor.newInstance(new Object[] {});
        } else {
            mesh = null;
        }
        mesh = reader.readMesh(mesh);
    // mesh.read (
    // new BufferedReader (new FileReader (meshFile)),
    // zeroIndexedIn.value);
    } catch (Exception e) {
        System.out.println("Error creating mesh object");
        e.printStackTrace();
        System.exit(1);
    }
    PolygonalMesh polyMesh = null;
    if (mesh instanceof PolygonalMesh) {
        polyMesh = (PolygonalMesh) mesh;
    }
    if (removeDisconnectedVertices.value) {
        if (polyMesh == null) {
            System.out.println("Not a polygonal mesh; ignoring -removeDisconnectedVertices");
        } else {
            System.out.println("removing disconnected vertices ...");
            int nv = polyMesh.removeDisconnectedVertices();
            if (nv > 0) {
                System.out.println(" removed " + nv);
            }
            System.out.println("done");
        }
    }
    if (closeVertexTol.value >= 0) {
        if (polyMesh == null) {
            System.out.println("Not a polygonal mesh; ignoring -mergeCloseVertices");
        } else {
            double rad = polyMesh.computeRadius();
            double dist = rad * closeVertexTol.value;
            System.out.println("removing vertices within " + dist + " ...");
            int nv = polyMesh.mergeCloseVertices(dist);
            if (nv > 0) {
                System.out.println(" removed " + nv);
            }
            polyMesh.checkForDegenerateFaces();
            System.out.println("done");
        }
    }
    if (removeDisconnectedFaces.value) {
        if (polyMesh == null) {
            System.out.println("Not a polygonal mesh; ignoring -removeDisconnectedFaces");
        } else {
            System.out.println("removing disconnected faces ...");
            int nf = polyMesh.removeDisconnectedFaces();
            if (nf > 0) {
                System.out.println(" removed " + nf);
            }
            System.out.println("done");
        }
    }
    if (laplacianSmoothing[0] != 0) {
        if (polyMesh == null) {
            System.out.println("Not a polygonal mesh; ignoring -laplacianSmooth");
        } else {
            System.out.println("smoothing ...");
            int iter = (int) laplacianSmoothing[0];
            double lam = laplacianSmoothing[1];
            double mu = laplacianSmoothing[2];
            LaplacianSmoother.smooth(polyMesh, iter, lam, mu);
            System.out.println("done");
        }
    }
    if (polyMesh != null && !polyMesh.isClosed()) {
        System.out.println("WARNING: mesh is not closed");
    }
    boolean translate = false;
    boolean rotate = false;
    if (xyz[0] != 0 || xyz[1] != 0 || xyz[2] != 0) {
        translate = true;
    }
    if (axisAngle[3] != 0) {
        rotate = true;
    }
    axisAngle[3] = Math.toRadians(axisAngle[3]);
    AffineTransform3dBase X = null;
    if (scaling.value != 1 || scaleXyz[0] != 1 || scaleXyz[1] != 1 || scaleXyz[2] != 1) {
        AffineTransform3d A = new AffineTransform3d();
        X = A;
        X.setTranslation(new Point3d(xyz));
        X.setRotation(new AxisAngle(axisAngle));
        A.applyScaling(scaleXyz[0] * scaling.value, scaleXyz[1] * scaling.value, scaleXyz[2] * scaling.value);
    } else if (translate || rotate) {
        X = new RigidTransform3d();
        X.setTranslation(new Point3d(xyz));
        X.setRotation(new AxisAngle(axisAngle));
    }
    boolean facesClockwise = false;
    if (X != null) {
        mesh.transform(X);
        if (X.getMatrix().determinant() < 0) {
            // then mesh has been flipped and we need to flip face ordering
            facesClockwise = true;
        }
    }
    try {
        GenericMeshWriter writer = new GenericMeshWriter(outFileName.value);
        writer.setFormat(reader);
        writer.writeMesh(mesh);
    } catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
}
Also used : RigidTransform3d(maspack.matrix.RigidTransform3d) InputStreamReader(java.io.InputStreamReader) Constructor(java.lang.reflect.Constructor) ArgParser(argparser.ArgParser) AxisAngle(maspack.matrix.AxisAngle) Point3d(maspack.matrix.Point3d) BufferedReader(java.io.BufferedReader) File(java.io.File) AffineTransform3dBase(maspack.matrix.AffineTransform3dBase) AffineTransform3d(maspack.matrix.AffineTransform3d)

Aggregations

ArgParser (argparser.ArgParser)12 IntHolder (argparser.IntHolder)4 File (java.io.File)4 PolygonalMesh (maspack.geometry.PolygonalMesh)4 StringHolder (argparser.StringHolder)2 IOException (java.io.IOException)2 InputStreamReader (java.io.InputStreamReader)2 ArrayList (java.util.ArrayList)2 RigidTransform3d (maspack.matrix.RigidTransform3d)2 GLVersion (maspack.render.GL.GLViewer.GLVersion)2 DoubleHolder (argparser.DoubleHolder)1 MovieMaker (artisynth.core.moviemaker.MovieMaker)1 WayPoint (artisynth.core.probes.WayPoint)1 RootModel (artisynth.core.workspace.RootModel)1 Color (java.awt.Color)1 Point (java.awt.Point)1 WindowEvent (java.awt.event.WindowEvent)1 WindowListener (java.awt.event.WindowListener)1 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1