Search in sources :

Example 11 with PolygonalMesh

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

the class PenetrationRender method createHemiBody.

// Creates and returns a rigid body built from a hemispherical mesh.  The
// body is centered at the origin, has a radius of 'rad', and the z axis is
// scaled by 'zscale'.
RigidBody createHemiBody(MechModel mech, String name, double rad, double zscale, boolean flipMesh) {
    PolygonalMesh mesh = MeshFactory.createHemisphere(rad, /*slices=*/
    20, /*levels=*/
    10);
    // scale mesh in the z direction
    mesh.scale(1, 1, zscale);
    if (flipMesh) {
        // flip upside down is requested
        mesh.transform(new RigidTransform3d(0, 0, 0, 0, 0, Math.PI));
    }
    RigidBody body = RigidBody.createFromMesh(name, mesh, /*density=*/
    1000, /*scale=*/
    1.0);
    mech.addRigidBody(body);
    // body is only parametrically controlled
    body.setDynamic(false);
    return body;
}
Also used : RigidTransform3d(maspack.matrix.RigidTransform3d) PolygonalMesh(maspack.geometry.PolygonalMesh)

Example 12 with PolygonalMesh

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

the class SDGridTest method build.

@Override
public void build(String[] args) throws IOException {
    super.build(args);
    PolygonalMesh mesh = new PolygonalMesh();
    Vertex3d[] v = new Vertex3d[9 * 2 + 8];
    Face[] f = new Face[8 * 6];
    int idx = 0;
    int fidx = 0;
    double c = 64;
    v[idx++] = mesh.addVertex(-c, -c, -c);
    v[idx++] = mesh.addVertex(-c, -c, 0);
    v[idx++] = mesh.addVertex(-c, -c, c);
    v[idx++] = mesh.addVertex(-c, 0, -c);
    v[idx++] = mesh.addVertex(-c, 0, 0);
    v[idx++] = mesh.addVertex(-c, 0, c);
    v[idx++] = mesh.addVertex(-c, c, -c);
    v[idx++] = mesh.addVertex(-c, c, 0);
    v[idx++] = mesh.addVertex(-c, c, c);
    // left
    f[fidx++] = mesh.addFace(v[0], v[1], v[4]);
    f[fidx++] = mesh.addFace(v[1], v[2], v[4]);
    f[fidx++] = mesh.addFace(v[2], v[5], v[4]);
    f[fidx++] = mesh.addFace(v[5], v[8], v[4]);
    f[fidx++] = mesh.addFace(v[8], v[7], v[4]);
    f[fidx++] = mesh.addFace(v[7], v[6], v[4]);
    f[fidx++] = mesh.addFace(v[6], v[3], v[4]);
    f[fidx++] = mesh.addFace(v[3], v[0], v[4]);
    v[idx++] = mesh.addVertex(0, -c, -c);
    v[idx++] = mesh.addVertex(0, -c, 0);
    v[idx++] = mesh.addVertex(0, -c, c);
    v[idx++] = mesh.addVertex(0, 0, -c);
    // test.addVertex( 0, 0, 0);
    v[idx++] = mesh.addVertex(0, 0, c);
    v[idx++] = mesh.addVertex(0, c, -c);
    v[idx++] = mesh.addVertex(0, c, 0);
    v[idx++] = mesh.addVertex(0, c, c);
    // back
    f[fidx++] = mesh.addFace(v[0], v[9], v[10]);
    f[fidx++] = mesh.addFace(v[1], v[0], v[10]);
    f[fidx++] = mesh.addFace(v[2], v[1], v[10]);
    f[fidx++] = mesh.addFace(v[11], v[2], v[10]);
    // top
    f[fidx++] = mesh.addFace(v[2], v[11], v[13]);
    f[fidx++] = mesh.addFace(v[5], v[2], v[13]);
    f[fidx++] = mesh.addFace(v[8], v[5], v[13]);
    f[fidx++] = mesh.addFace(v[16], v[8], v[13]);
    // front
    f[fidx++] = mesh.addFace(v[8], v[16], v[15]);
    f[fidx++] = mesh.addFace(v[7], v[8], v[15]);
    f[fidx++] = mesh.addFace(v[6], v[7], v[15]);
    f[fidx++] = mesh.addFace(v[14], v[6], v[15]);
    // bottom
    f[fidx++] = mesh.addFace(v[3], v[6], v[12]);
    f[fidx++] = mesh.addFace(v[6], v[14], v[12]);
    f[fidx++] = mesh.addFace(v[0], v[3], v[12]);
    f[fidx++] = mesh.addFace(v[9], v[0], v[12]);
    v[idx++] = mesh.addVertex(c, -c, -c);
    v[idx++] = mesh.addVertex(c, -c, 0);
    v[idx++] = mesh.addVertex(c, -c, c);
    v[idx++] = mesh.addVertex(c, 0, -c);
    v[idx++] = mesh.addVertex(c, 0, 0);
    v[idx++] = mesh.addVertex(c, 0, c);
    v[idx++] = mesh.addVertex(c, c, -c);
    v[idx++] = mesh.addVertex(c, c, 0);
    v[idx++] = mesh.addVertex(c, c, c);
    // bottom
    f[fidx++] = mesh.addFace(v[17], v[9], v[12]);
    f[fidx++] = mesh.addFace(v[20], v[17], v[12]);
    f[fidx++] = mesh.addFace(v[23], v[20], v[12]);
    f[fidx++] = mesh.addFace(v[14], v[23], v[12]);
    f[fidx++] = mesh.addFace(v[9], v[17], v[10]);
    f[fidx++] = mesh.addFace(v[17], v[18], v[10]);
    f[fidx++] = mesh.addFace(v[18], v[19], v[10]);
    f[fidx++] = mesh.addFace(v[19], v[11], v[10]);
    f[fidx++] = mesh.addFace(v[11], v[19], v[13]);
    f[fidx++] = mesh.addFace(v[19], v[22], v[13]);
    f[fidx++] = mesh.addFace(v[22], v[25], v[13]);
    f[fidx++] = mesh.addFace(v[25], v[16], v[13]);
    f[fidx++] = mesh.addFace(v[16], v[25], v[15]);
    f[fidx++] = mesh.addFace(v[25], v[24], v[15]);
    f[fidx++] = mesh.addFace(v[24], v[23], v[15]);
    f[fidx++] = mesh.addFace(v[23], v[14], v[15]);
    f[fidx++] = mesh.addFace(v[25], v[22], v[21]);
    f[fidx++] = mesh.addFace(v[22], v[19], v[21]);
    f[fidx++] = mesh.addFace(v[19], v[18], v[21]);
    f[fidx++] = mesh.addFace(v[18], v[17], v[21]);
    f[fidx++] = mesh.addFace(v[17], v[20], v[21]);
    f[fidx++] = mesh.addFace(v[20], v[23], v[21]);
    f[fidx++] = mesh.addFace(v[23], v[24], v[21]);
    f[fidx++] = mesh.addFace(v[24], v[25], v[21]);
    // Move in some corner(s)
    v[0].setPosition(new Point3d(0, 0, 0));
    // v[25].setPosition(new Point3d(0,0,0));
    int divisions = 3;
    mesh = MeshFactory.subdivide(mesh, divisions);
    // randomize vertex order and re-number (to test different chiralities)
    ArrayList<Vertex3d> vertices = mesh.getVertices();
    Collections.shuffle(vertices);
    for (int i = 0; i < vertices.size(); ++i) {
        vertices.get(i).setIndex(i);
    }
    FixedMeshBody fm = new FixedMeshBody("cube", mesh);
    RenderProps.setFaceStyle(fm, FaceStyle.FRONT_AND_BACK);
    addRenderable(fm);
    int cells = 1 << (divisions + 2);
    double margin = 1.0 / cells;
    cells += 2;
    sdgrid = new DistanceGrid(mesh.getFaces(), margin, cells, /*signed=*/
    true);
    // test a bunch of inside points
    Vector3d norm = new Vector3d();
    double dx = 2 * c / ((1 << (divisions + 1)));
    for (double x = -c + dx; x < c; x += dx) {
        for (double y = -c + dx; y < c; y += dx) {
            for (double z = -c + dx; z < c; z += dx) {
                double d = sdgrid.getLocalDistanceAndNormal(norm, x, y, z);
                // check bottom corner
                if ((x < 0 && y < 0 && z < 0)) {
                    // ||(x > 0 && y > 0 && z > 0)) {
                    if (d < 0) {
                        System.err.println("Point (" + x + "," + y + "," + z + ") incorrectly labelled as inside");
                    }
                } else {
                    if (d > 0) {
                        System.err.println("Point (" + x + "," + y + "," + z + ") incorrectly labelled as outside");
                    }
                }
            }
        }
    }
    RenderProps.setDrawEdges(fm, true);
}
Also used : Vertex3d(maspack.geometry.Vertex3d) Vector3d(maspack.matrix.Vector3d) Point3d(maspack.matrix.Point3d) DistanceGrid(maspack.geometry.DistanceGrid) Face(maspack.geometry.Face) FixedMeshBody(artisynth.core.mechmodels.FixedMeshBody) PolygonalMesh(maspack.geometry.PolygonalMesh)

Example 13 with PolygonalMesh

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

the class SpecularTest method createMesh.

FixedMeshBody createMesh(MechModel mech, double z) {
    PolygonalMesh mesh = MeshFactory.createRectangle(3, 1, 20, 20, /*texture=*/
    true);
    FixedMeshBody body = new FixedMeshBody(mesh);
    mech.addMeshBody(body);
    body.setPose(new RigidTransform3d(0, 0, z, 0, 0, Math.PI / 2));
    return body;
}
Also used : RigidTransform3d(maspack.matrix.RigidTransform3d) FixedMeshBody(artisynth.core.mechmodels.FixedMeshBody) PolygonalMesh(maspack.geometry.PolygonalMesh)

Example 14 with PolygonalMesh

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

the class WavefrontReader method readFromString.

public static PolygonalMesh readFromString(String input, boolean zeroIndexed) {
    WavefrontReader reader = new WavefrontReader(new StringReader(input));
    reader.setZeroIndexed(zeroIndexed);
    try {
        return (PolygonalMesh) reader.readMesh(null);
    } catch (Exception e) {
        throw new IllegalArgumentException("Illegal mesh input string: " + e.getMessage());
    }
}
Also used : StringReader(java.io.StringReader) PolygonalMesh(maspack.geometry.PolygonalMesh) IOException(java.io.IOException)

Example 15 with PolygonalMesh

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

the class MayaAsciiReader method recursiveAddPolygonalMeshes.

private void recursiveAddPolygonalMeshes(Node<MayaNode> root, PolygonalMesh mesh, AffineTransform3d trans, UnitInfo units) {
    // make copy so can traverse
    trans = new AffineTransform3d(trans);
    // children independently
    MayaNode data = root.getData();
    if (data instanceof MayaTransform) {
        MayaTransform dtrans = (MayaTransform) data;
        AffineTransform3d tu = new AffineTransform3d();
        dtrans.getTransform(tu);
        // convert units
        tu.p.scale(dtrans.units.length.getSI() / units.length.getSI());
        // only multiply if inherited
        if (dtrans.inheritsTransform()) {
            trans.mul(tu);
        } else {
            trans.set(tu);
        }
    } else if (data instanceof MayaMesh) {
        MayaMesh mm = (MayaMesh) data;
        PolygonalMesh mmesh = mm.getMesh();
        if (mmesh != null) {
            // transform mesh
            HashMap<Vertex3d, Vertex3d> vtxMap = new HashMap<Vertex3d, Vertex3d>();
            for (Vertex3d vtx : mmesh.getVertices()) {
                Vertex3d nvtx = new Vertex3d(vtx.pnt);
                // XXX prevent transform
                nvtx.pnt.scale(mm.units.length.getSI() / units.length.getSI());
                nvtx.pnt.transform(trans);
                vtxMap.put(vtx, nvtx);
                mesh.addVertex(nvtx);
            }
            for (Face face : mmesh.getFaces()) {
                Vertex3d[] oface = face.getVertices();
                Vertex3d[] nface = new Vertex3d[oface.length];
                for (int i = 0; i < oface.length; i++) {
                    nface[i] = vtxMap.get(oface[i]);
                }
                mesh.addFace(nface);
            }
        }
    }
    for (Node<MayaNode> child : root.getChildren()) {
        recursiveAddPolygonalMeshes(child, mesh, trans, units);
    }
}
Also used : Vertex3d(maspack.geometry.Vertex3d) HashMap(java.util.HashMap) Face(maspack.geometry.Face) PolygonalMesh(maspack.geometry.PolygonalMesh) AffineTransform3d(maspack.matrix.AffineTransform3d)

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