Search in sources :

Example 1 with WavefrontReader

use of maspack.geometry.io.WavefrontReader in project artisynth_core by artisynth.

the class MeshViewer method loadMeshes.

public static void loadMeshes(ArrayList<MeshInfo> infoList, String fileName, AffineTransform3dBase X) {
    try {
        File meshFile = new File(fileName);
        if (!meshFile.exists()) {
            System.out.println("Error: mesh file " + meshFile.getName() + " not found");
            return;
        }
        if (meshFile.getName().endsWith(".obj") && !pointMesh.value) {
            Class meshClass = Class.forName(className.value);
            Class meshBaseClass = new PolygonalMesh().getClass();
            if (meshClass == null) {
                System.out.println("can't find class " + className.value);
                return;
            }
            if (!meshBaseClass.isAssignableFrom(meshClass)) {
                System.out.println(className.value + " is not an instance of " + meshBaseClass.getName());
                return;
            }
            WavefrontReader reader = new WavefrontReader(meshFile);
            reader.parse();
            String[] names = reader.getPolyhedralGroupNames();
            for (int i = 0; i < names.length; i++) {
                if (meshClass == PolygonalMesh.class) {
                    PolygonalMesh mesh = (PolygonalMesh) meshClass.newInstance();
                    reader.setMesh(mesh, names[i]);
                    if (printBounds.value) {
                        doPrintBounds(mesh);
                    }
                    if (mesh.numVertices() > 0) {
                        System.out.print("mesh " + names[i] + ": " + mesh.numVertices() + " vertices, " + mesh.numFaces() + " faces, ");
                        if (mesh.isTriangular()) {
                            System.out.println("triangular");
                        } else if (mesh.isQuad()) {
                            System.out.println("quad");
                        } else {
                            System.out.println("polygonal");
                        }
                        mesh.transform(X);
                        if (names[i].equals("default")) {
                            infoList.add(new MeshInfo(meshFile.getName(), mesh));
                        } else {
                            infoList.add(new MeshInfo(meshFile.getName() + "(" + names[i] + ")", mesh));
                        }
                    }
                } else if (meshClass == PolylineMesh.class) {
                    PolylineMesh mesh = (PolylineMesh) meshClass.newInstance();
                    reader.setMesh(mesh, names[i]);
                    mesh.transform(X);
                    if (printBounds.value) {
                        doPrintBounds(mesh);
                    }
                    if (names[i].equals("default")) {
                        infoList.add(new MeshInfo(meshFile.getName(), mesh));
                    } else {
                        infoList.add(new MeshInfo(meshFile.getName() + "(" + names[i] + ")", mesh));
                    }
                } else if (meshClass == PointMesh.class) {
                    PointMesh mesh = (PointMesh) meshClass.newInstance();
                    reader.setMesh(mesh, names[i]);
                    mesh.transform(X);
                    if (printBounds.value) {
                        doPrintBounds(mesh);
                    }
                    if (names[i].equals("default")) {
                        infoList.add(new MeshInfo(meshFile.getName(), mesh));
                    } else {
                        infoList.add(new MeshInfo(meshFile.getName() + "(" + names[i] + ")", mesh));
                    }
                }
            }
        } else {
            MeshReader reader;
            if (meshFile.getName().endsWith(".xyzb")) {
                XyzbReader xyzbReader = new XyzbReader(meshFile);
                xyzbReader.setSkip(skipCount.value);
                reader = xyzbReader;
            } else {
                reader = new GenericMeshReader(meshFile);
            }
            MeshBase mesh = pointMesh.value ? new PointMesh() : null;
            mesh = reader.readMesh(mesh);
            if (printBounds.value) {
                doPrintBounds(mesh);
            }
            infoList.add(new MeshInfo(meshFile.getName(), mesh));
        }
    } catch (Exception e) {
        System.out.println("Error creating mesh");
        e.printStackTrace();
    }
}
Also used : WavefrontReader(maspack.geometry.io.WavefrontReader) MeshReader(maspack.geometry.io.MeshReader) GenericMeshReader(maspack.geometry.io.GenericMeshReader) PolygonalMesh(maspack.geometry.PolygonalMesh) Point(java.awt.Point) GenericMeshReader(maspack.geometry.io.GenericMeshReader) PointMesh(maspack.geometry.PointMesh) MeshBase(maspack.geometry.MeshBase) PolylineMesh(maspack.geometry.PolylineMesh) File(java.io.File) XyzbReader(maspack.geometry.io.XyzbReader)

Example 2 with WavefrontReader

use of maspack.geometry.io.WavefrontReader in project artisynth_core by artisynth.

the class NURBSViewer method addNURBS.

public void addNURBS(File file) throws IOException {
    WavefrontReader wfr = new WavefrontReader(file);
    wfr.parse();
    Vector4d[] allControlPnts = wfr.getHomogeneousPoints();
    for (WavefrontReader.Curve curve : wfr.getCurveList()) {
        NURBSCurve3d curveCopy = new NURBSCurve3d();
        try {
            curveCopy.set(curve, allControlPnts);
        } catch (IllegalArgumentException e) {
            throw new IOException(e.getMessage());
        }
        addNURBS(curveCopy);
    }
    for (WavefrontReader.Surface surf : wfr.getSurfaceList()) {
        NURBSSurface surfCopy = new NURBSSurface();
        try {
            surfCopy.set(surf, allControlPnts);
        } catch (IllegalArgumentException e) {
            throw new IOException(e.getMessage());
        }
        addNURBS(surfCopy);
    }
}
Also used : WavefrontReader(maspack.geometry.io.WavefrontReader) Vector4d(maspack.matrix.Vector4d) NURBSCurve3d(maspack.geometry.NURBSCurve3d) IOException(java.io.IOException) NURBSSurface(maspack.geometry.NURBSSurface)

Example 3 with WavefrontReader

use of maspack.geometry.io.WavefrontReader in project artisynth_core by artisynth.

the class NURBSCurveBase method readFromWavefront.

protected void readFromWavefront(WavefrontReader wfr) throws IOException {
    wfr.parse();
    ArrayList<WavefrontReader.Curve> curveList = wfr.getCurveList();
    WavefrontReader.Curve curve = curveList.get(curveList.size() - 1);
    if (curve == null) {
        throw new IOException("no curve specified in input");
    }
    try {
        set(curve, wfr.getHomogeneousPoints());
    } catch (IllegalArgumentException e) {
        throw new IOException(e.getMessage());
    }
}
Also used : WavefrontReader(maspack.geometry.io.WavefrontReader) IOException(java.io.IOException)

Example 4 with WavefrontReader

use of maspack.geometry.io.WavefrontReader in project artisynth_core by artisynth.

the class PointMesh method read.

/**
 * Reads the contents of this mesh from a ReaderTokenizer. The input is
 * assumed to be supplied in Alias Wavefront obj format, as described for
 * the method {@link #write(PrintWriter,NumberFormat,boolean)}.
 *
 * @param rtok
 * tokenizer supplying the input description of the mesh
 * @param zeroIndexed
 * if true, the index numbering for mesh vertices starts at 0. Otherwise,
 * numbering starts at 1.
 */
public void read(ReaderTokenizer rtok, boolean zeroIndexed) throws IOException {
    clear();
    WavefrontReader wfr = new WavefrontReader(rtok);
    if (zeroIndexed) {
        wfr.setZeroIndexed(true);
    }
    wfr.readMesh(this);
}
Also used : WavefrontReader(maspack.geometry.io.WavefrontReader)

Example 5 with WavefrontReader

use of maspack.geometry.io.WavefrontReader in project artisynth_core by artisynth.

the class PolylineMesh method read.

/**
 * Reads the contents of this mesh from a ReaderTokenizer. The input is
 * assumed to be supplied in Alias Wavefront obj format, as described for
 * the method {@link #write(PrintWriter,NumberFormat,boolean)}.
 *
 * @param rtok
 * tokenizer supplying the input description of the mesh
 * @param zeroIndexed
 * if true, the index numbering for mesh vertices starts at 0. Otherwise,
 * numbering starts at 1.
 */
public void read(ReaderTokenizer rtok, boolean zeroIndexed) throws IOException {
    clear();
    WavefrontReader wfr = new WavefrontReader(rtok);
    if (zeroIndexed) {
        wfr.setZeroIndexed(true);
    }
    wfr.readMesh(this);
}
Also used : WavefrontReader(maspack.geometry.io.WavefrontReader)

Aggregations

WavefrontReader (maspack.geometry.io.WavefrontReader)11 IOException (java.io.IOException)6 File (java.io.File)4 PolygonalMesh (maspack.geometry.PolygonalMesh)4 Color (java.awt.Color)2 Point3d (maspack.matrix.Point3d)2 RigidTransform3d (maspack.matrix.RigidTransform3d)2 Vector3d (maspack.matrix.Vector3d)2 RenderProps (maspack.render.RenderProps)2 FemElement3d (artisynth.core.femmodels.FemElement3d)1 FemMeshComp (artisynth.core.femmodels.FemMeshComp)1 FemMuscleModel (artisynth.core.femmodels.FemMuscleModel)1 MuscleBundle (artisynth.core.femmodels.MuscleBundle)1 FemMaterial (artisynth.core.materials.FemMaterial)1 LinearMaterial (artisynth.core.materials.LinearMaterial)1 MuscleMaterial (artisynth.core.materials.MuscleMaterial)1 SimpleForceMuscle (artisynth.core.materials.SimpleForceMuscle)1 MechModel (artisynth.core.mechmodels.MechModel)1 RigidBody (artisynth.core.mechmodels.RigidBody)1 Point (java.awt.Point)1