Search in sources :

Example 6 with PointMesh

use of maspack.geometry.PointMesh in project artisynth_core by artisynth.

the class PlyReader method readMesh.

public MeshBase readMesh(MeshBase mesh) throws IOException {
    DataInputStream is = new DataInputStream(myIstream);
    parseHeader(is);
    ArrayList<Point3d> verts = new ArrayList<Point3d>();
    ArrayList<Vector3d> nrmls = new ArrayList<Vector3d>();
    ArrayList<int[]> faces = new ArrayList<int[]>();
    if (myDataFormat == DataFormat.ASCII) {
        ReaderTokenizer rtok = new ReaderTokenizer(new BufferedReader(new InputStreamReader(myIstream)));
        readVertexInfo(rtok, verts, nrmls);
        readFaceInfo(rtok, faces);
    } else {
        BinaryInputStream bis = new BinaryInputStream(new BufferedInputStream(myIstream));
        if (myDataFormat == DataFormat.BINARY_LITTLE_ENDIAN) {
            bis.setLittleEndian(true);
        }
        readVertexInfo(bis, verts, nrmls);
        readFaceInfo(bis, faces);
    }
    if (mesh == null) {
        if (myNumFaces == 0) {
            mesh = new PointMesh();
        } else {
            mesh = new PolygonalMesh();
        }
    }
    if (mesh instanceof PolygonalMesh) {
        PolygonalMesh pmesh = (PolygonalMesh) mesh;
        int icnt = 0;
        for (Point3d pnt : verts) {
            pmesh.addVertex(pnt);
        }
        for (int[] idxs : faces) {
            pmesh.addFace(idxs);
            icnt += idxs.length;
        }
        if (nrmls.size() > 0) {
            // we have to assume here the there is one normal per vertex,
            // and assign the normal indices accordingly]
            int k = 0;
            int[] normalIndices = new int[icnt];
            for (int i = 0; i < faces.size(); i++) {
                int[] idxs = pmesh.getFaces().get(i).getVertexIndices();
                for (int j = 0; j < idxs.length; j++) {
                    normalIndices[k++] = idxs[j];
                }
            }
            pmesh.setNormals(nrmls, normalIndices);
            pmesh.setHardEdgesFromNormals();
        }
    } else if (mesh instanceof PointMesh) {
        PointMesh pmesh = (PointMesh) mesh;
        pmesh.set(verts.toArray(new Point3d[0]), nrmls.toArray(new Vector3d[0]));
    } else {
        throw new UnsupportedOperationException("Mesh type " + mesh.getClass() + " not supported by this reader");
    }
    return mesh;
}
Also used : InputStreamReader(java.io.InputStreamReader) BinaryInputStream(maspack.util.BinaryInputStream) ArrayList(java.util.ArrayList) DataInputStream(java.io.DataInputStream) PolygonalMesh(maspack.geometry.PolygonalMesh) PointMesh(maspack.geometry.PointMesh) Vector3d(maspack.matrix.Vector3d) BufferedInputStream(java.io.BufferedInputStream) Point3d(maspack.matrix.Point3d) ReaderTokenizer(maspack.util.ReaderTokenizer) BufferedReader(java.io.BufferedReader)

Example 7 with PointMesh

use of maspack.geometry.PointMesh in project artisynth_core by artisynth.

the class XyzbReader method readMesh.

public PointMesh readMesh(PointMesh mesh, InputStream in) throws IOException {
    BinaryInputStream bin = new BinaryInputStream(new BufferedInputStream(in));
    bin.setLittleEndian(myLittleEndian);
    if (mesh == null) {
        mesh = new PointMesh();
    }
    mesh.clear();
    ArrayList<Point3d> vtxs = new ArrayList<Point3d>();
    ArrayList<Vector3d> nrms = new ArrayList<Vector3d>();
    boolean done = false;
    int cnt = 0;
    while (!done) {
        try {
            double vx = bin.readFloat();
            double vy = bin.readFloat();
            double vz = bin.readFloat();
            double nx = bin.readFloat();
            double ny = bin.readFloat();
            double nz = bin.readFloat();
            if (cnt % mySkip == 0) {
                vtxs.add(new Point3d(vx, vy, vz));
                nrms.add(new Vector3d(nx, ny, nz));
            }
        } catch (EOFException e) {
            done = true;
        }
        cnt++;
    }
    bin.close();
    mesh.set(vtxs.toArray(new Point3d[0]), nrms.toArray(new Vector3d[0]));
    return mesh;
}
Also used : PointMesh(maspack.geometry.PointMesh) BinaryInputStream(maspack.util.BinaryInputStream) Vector3d(maspack.matrix.Vector3d) Point3d(maspack.matrix.Point3d) ArrayList(java.util.ArrayList)

Aggregations

PointMesh (maspack.geometry.PointMesh)7 PolygonalMesh (maspack.geometry.PolygonalMesh)4 ArrayList (java.util.ArrayList)3 Point3d (maspack.matrix.Point3d)3 Vector3d (maspack.matrix.Vector3d)3 MeshBase (maspack.geometry.MeshBase)2 PolylineMesh (maspack.geometry.PolylineMesh)2 BinaryInputStream (maspack.util.BinaryInputStream)2 ReaderTokenizer (maspack.util.ReaderTokenizer)2 ArgParser (argparser.ArgParser)1 Point (java.awt.Point)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedReader (java.io.BufferedReader)1 DataInputStream (java.io.DataInputStream)1 File (java.io.File)1 InputStreamReader (java.io.InputStreamReader)1 GenericMeshReader (maspack.geometry.io.GenericMeshReader)1 MeshReader (maspack.geometry.io.MeshReader)1 WavefrontReader (maspack.geometry.io.WavefrontReader)1 XyzbReader (maspack.geometry.io.XyzbReader)1