Search in sources :

Example 16 with PolygonalMesh

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

the class OpenSimAscReader method readMesh.

public PolygonalMesh readMesh(PolygonalMesh mesh) throws IOException {
    if (mesh == null) {
        mesh = new PolygonalMesh();
    } else {
        mesh.clear();
    }
    ReaderTokenizer rtok = new ReaderTokenizer(new InputStreamReader(myIstream));
    rtok.eolIsSignificant(false);
    boolean oldFormat = false;
    int nVertices = 0;
    int nFaces = 0;
    double[] tmp = new double[6];
    int nReadVals = 6;
    // read first symbol, which should be NORM_ASCII or a number of vertices
    if (rtok.nextToken() != ReaderTokenizer.TT_WORD) {
        if (rtok.ttype == ReaderTokenizer.TT_NUMBER) {
            oldFormat = true;
            nReadVals = 3;
            nVertices = (int) rtok.nval;
        } else {
            throw new IOException("Expected " + ReaderTokenizer.TT_WORD + " on line " + rtok.lineno());
        }
    }
    if (!oldFormat) {
        String type = rtok.sval;
        if (type.compareTo("NORM_ASCII") != 0) {
            throw new IOException("Unknown file type: " + type);
        }
        if (rtok.nextToken() != ReaderTokenizer.TT_NUMBER) {
            throw new IOException("Expected " + ReaderTokenizer.TT_NUMBER + " on line " + rtok.lineno());
        }
        nVertices = (int) rtok.nval;
    }
    if (rtok.nextToken() != ReaderTokenizer.TT_NUMBER) {
        throw new IOException("Expected " + ReaderTokenizer.TT_NUMBER + " on line " + rtok.lineno());
    }
    nFaces = (int) rtok.nval;
    if (!oldFormat) {
        // bounding box
        int iRead = rtok.scanNumbers(tmp, 6);
        if (iRead != 6) {
            throw new IOException("Expected bounding box on line " + rtok.lineno());
        }
    }
    // load all vertices now
    for (int i = 0; i < nVertices; i++) {
        int nRead = rtok.scanNumbers(tmp, nReadVals);
        if (nRead != nReadVals) {
            throw new IOException("Expected number of values on line " + rtok.lineno());
        }
        mesh.addVertex(tmp[0], tmp[1], tmp[2]);
    }
    // load all faces
    for (int i = 0; i < nFaces; i++) {
        int nV = (int) rtok.scanNumber();
        int[] face = new int[nV];
        if (scanIntegers(rtok, face, nV) != nV) {
            throw new IOException("Expected number of vertices on line " + rtok.lineno());
        }
        if (oldFormat) {
            for (int j = 0; j < nV; j++) {
                // start indices at zero
                face[j] -= 1;
            }
        }
        mesh.addFace(face);
    }
    return mesh;
}
Also used : InputStreamReader(java.io.InputStreamReader) ReaderTokenizer(maspack.util.ReaderTokenizer) IOException(java.io.IOException) PolygonalMesh(maspack.geometry.PolygonalMesh)

Example 17 with PolygonalMesh

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

the class MultiViewerTesterBase method loadStanfordBunny.

protected static PolygonalMesh loadStanfordBunny() {
    // read Standford bunny directly
    String bunnyURL = "http://graphics.stanford.edu/~mdfisher/Data/Meshes/bunny.obj";
    // bunny
    File bunnyFile = new File("tmp/data/stanford_bunny.obj");
    PolygonalMesh bunny = null;
    try {
        if (!bunnyFile.exists()) {
            bunnyFile.getParentFile().mkdirs();
            // read file directly from remote
            FileCacher cacher = new FileCacher();
            cacher.initialize();
            cacher.cache(new URIx(bunnyURL), bunnyFile);
            cacher.release();
        }
        WavefrontReader reader = new WavefrontReader(bunnyFile);
        bunny = new PolygonalMesh();
        reader.readMesh(bunny);
        // bunny.computeVertexNormals();
        // normalize bunny
        double r = bunny.computeRadius();
        Vector3d c = new Vector3d();
        bunny.computeCentroid(c);
        c.negate();
        bunny.scale(1.0 / r);
        c.z -= 0.5;
        bunny.transform(new RigidTransform3d(c, new AxisAngle(1, 0, 0, Math.PI / 2)));
        reader.close();
    } catch (IOException e1) {
        e1.printStackTrace();
        System.out.println("Unable to load stanford bunny... requires internet connection");
        bunny = null;
    }
    return bunny;
}
Also used : WavefrontReader(maspack.geometry.io.WavefrontReader) RigidTransform3d(maspack.matrix.RigidTransform3d) AxisAngle(maspack.matrix.AxisAngle) Vector3d(maspack.matrix.Vector3d) FileCacher(maspack.fileutil.FileCacher) URIx(maspack.fileutil.uri.URIx) IOException(java.io.IOException) File(java.io.File) PolygonalMesh(maspack.geometry.PolygonalMesh)

Example 18 with PolygonalMesh

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

the class SingleGL2Tester method addRenderObjects.

protected static void addRenderObjects(MultiViewer tester) {
    addCube(tester);
    addAxes(tester);
    addTransRotator(tester);
    addCylinder(tester);
    PolygonalMesh bunny = loadStanfordBunny();
    addStanfordBunnies(tester, bunny);
    addSolidBunny(tester, bunny);
    addHalfBunny(tester, bunny);
}
Also used : PolygonalMesh(maspack.geometry.PolygonalMesh)

Example 19 with PolygonalMesh

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

the class SingleGL3Tester method addRenderObjects.

protected static void addRenderObjects(MultiViewer tester) {
    addCube(tester);
    addAxes(tester);
    addTransRotator(tester);
    addCylinder(tester);
    PolygonalMesh bunny = loadStanfordBunny();
    addStanfordBunnies(tester, bunny);
    addSolidBunny(tester, bunny);
    addHalfBunny(tester, bunny);
}
Also used : PolygonalMesh(maspack.geometry.PolygonalMesh)

Example 20 with PolygonalMesh

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

the class TextureEgyptianTest method addContent.

@Override
protected void addContent(MultiViewer mv) {
    JFrame frame = new JFrame();
    JPanel controls = new JPanel();
    frame.add(controls);
    PolygonalMesh plane = MeshFactory.createRectangle(4000, 400, 32, 8, /*texture=*/
    true);
    RenderProps rprops = plane.getRenderProps();
    if (rprops == null) {
        rprops = new RenderProps();
    }
    rprops.setShading(Shading.SMOOTH);
    rprops.setShininess(20);
    rprops.setFaceColor(new Color(155, 196, 30));
    rprops.setSpecular(new Color(255, 113, 0));
    rprops.setFaceStyle(FaceStyle.FRONT_AND_BACK);
    plane.transform(new RigidTransform3d(Vector3d.ZERO, AxisAngle.ROT_X_90));
    String srcDir = PathFinder.findSourceDir(this);
    ColorMapProps dprops = new ColorMapProps();
    dprops.setFileName(srcDir + "/data/specular_map.jpg");
    dprops.setColorMixing(ColorMixing.MODULATE);
    dprops.setEnabled(true);
    NormalMapProps normalProps = new NormalMapProps();
    normalProps.setFileName(srcDir + "/data/foil_normal_map2.png");
    normalProps.setScaling(0.3f);
    normalProps.setEnabled(true);
    BumpMapProps bumpProps = new BumpMapProps();
    bumpProps.setFileName(srcDir + "/data/egyptian_friz_2.png");
    bumpProps.setScaling(2.5f);
    bumpProps.setEnabled(true);
    rprops.setColorMap(dprops);
    rprops.setNormalMap(normalProps);
    rprops.setBumpMap(bumpProps);
    // FixedMeshBody fm = new FixedMeshBody(plane);
    // fm.setRenderProps(rprops);
    mv.addRenderable(plane);
    if (false) {
        mv.addRenderable(new IsRenderable() {

            @Override
            public void updateBounds(Vector3d pmin, Vector3d pmax) {
                Point3d.X_UNIT.updateBounds(pmin, pmax);
                Point3d.Y_UNIT.updateBounds(pmin, pmax);
                Point3d.Z_UNIT.updateBounds(pmin, pmax);
                Point3d.NEG_X_UNIT.updateBounds(pmin, pmax);
                Point3d.NEG_Y_UNIT.updateBounds(pmin, pmax);
                Point3d.NEG_Z_UNIT.updateBounds(pmin, pmax);
            }

            @Override
            public void render(Renderer renderer, int flags) {
                renderer.setColor(Color.CYAN);
                renderer.setFaceStyle(FaceStyle.FRONT_AND_BACK);
                renderer.drawSphere(Point3d.ZERO, 1);
            }

            @Override
            public void prerender(RenderList list) {
            // TODO Auto-generated method stub
            }

            @Override
            public int getRenderHints() {
                // TODO Auto-generated method stub
                return 0;
            }
        });
    }
    mv.autoFitViewers();
    LabeledComponentBase base = PropertyWidget.create("Color texture", rprops.getColorMap(), "enabled");
    controls.add(base);
    base = PropertyWidget.create("Normal map", rprops.getNormalMap(), "enabled");
    controls.add(base);
    base = PropertyWidget.create("Bump map", rprops.getBumpMap(), "enabled");
    controls.add(base);
    base = PropertyWidget.create("Specular", rprops.getColorMap(), "specularColoring");
    controls.add(base);
    base = PropertyWidget.create("Bump map scale", rprops.getBumpMap(), "scaling");
    controls.add(base);
    base = PropertyWidget.create("Normal map scale", rprops.getNormalMap(), "scaling");
    controls.add(base);
    frame.pack();
    frame.setVisible(true);
}
Also used : JPanel(javax.swing.JPanel) RigidTransform3d(maspack.matrix.RigidTransform3d) Color(java.awt.Color) ColorMapProps(maspack.render.ColorMapProps) RenderProps(maspack.render.RenderProps) PolygonalMesh(maspack.geometry.PolygonalMesh) RenderList(maspack.render.RenderList) BumpMapProps(maspack.render.BumpMapProps) IsRenderable(maspack.render.IsRenderable) JFrame(javax.swing.JFrame) Vector3d(maspack.matrix.Vector3d) Renderer(maspack.render.Renderer) LabeledComponentBase(maspack.widgets.LabeledComponentBase) NormalMapProps(maspack.render.NormalMapProps)

Aggregations

PolygonalMesh (maspack.geometry.PolygonalMesh)128 Point3d (maspack.matrix.Point3d)30 Vertex3d (maspack.geometry.Vertex3d)24 Vector3d (maspack.matrix.Vector3d)23 RigidTransform3d (maspack.matrix.RigidTransform3d)21 IOException (java.io.IOException)18 ArrayList (java.util.ArrayList)18 File (java.io.File)14 Face (maspack.geometry.Face)14 ContactPoint (artisynth.core.mechmodels.ContactPoint)11 Point (artisynth.core.mechmodels.Point)11 PointParticleAttachment (artisynth.core.mechmodels.PointParticleAttachment)10 Color (java.awt.Color)10 MeshBase (maspack.geometry.MeshBase)10 RigidBody (artisynth.core.mechmodels.RigidBody)9 MechModel (artisynth.core.mechmodels.MechModel)8 BufferedReader (java.io.BufferedReader)8 AxisAngle (maspack.matrix.AxisAngle)8 RenderProps (maspack.render.RenderProps)8 HashMap (java.util.HashMap)7