Search in sources :

Example 96 with PolygonalMesh

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

the class NagataDistanceTest method createTetMesh.

public void createTetMesh() {
    myBaseMesh = new PolygonalMesh();
    myBaseMesh.addVertex(1, 0, 0);
    myBaseMesh.addVertex(-0.5, 0.5, 0.5);
    myBaseMesh.addVertex(-0.5, -0.5, 0.5);
    myBaseMesh.addVertex(-0.5, 0, -0.5);
    myBaseMesh.addFace(new int[] { 0, 1, 2 });
    myBaseMesh.addFace(new int[] { 0, 2, 3 });
    myBaseMesh.addFace(new int[] { 0, 3, 1 });
    myBaseMesh.addFace(new int[] { 3, 2, 1 });
    ArrayList<Vector3d> normals = myBaseMesh.getNormals();
    System.out.println("normals 0: " + normals.get(0));
    System.out.println("normals 1: " + normals.get(1));
    System.out.println("normals 2: " + normals.get(2));
}
Also used : Vector3d(maspack.matrix.Vector3d) PolygonalMesh(maspack.geometry.PolygonalMesh)

Example 97 with PolygonalMesh

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

the class MeshColliderTest method testEdgeEdge.

boolean testEdgeEdge() {
    PolygonalMesh mesh0 = MeshFactory.createBox(1, 1, 1);
    PolygonalMesh mesh1 = MeshFactory.createBox(1, 1, 1);
    RigidTransform3d trans0 = new RigidTransform3d(new Vector3d(1, 0, 1), new AxisAngle());
    mesh0.setMeshToWorld(trans0);
    RigidTransform3d trans1 = new RigidTransform3d();
    trans1.mulAxisAngle(new AxisAngle(0, 1, 0, Math.PI / 4));
    trans1.mulAxisAngle(new AxisAngle(1, 0, 0, Math.PI / 4));
    mesh1.setMeshToWorld(trans1);
    MeshCollider collider = new MeshCollider();
    // first way
    ContactInfo info = collider.getContacts(mesh0, mesh1);
    if (info == null) {
        return false;
    }
    ArrayList<ContactPlane> regions = info.getContactPlanes();
    if (regions.size() != 1)
        return false;
    ContactPlane region = regions.get(0);
    if (region.points.size() != 1)
        return false;
    if (!region.points.get(0).epsilonEquals(new Vector3d(0.5, 0, 0.5), epsilon))
        return false;
    if (!region.normal.epsilonEquals(new Vector3d(1.0 / Math.sqrt(2.0), 0, 1.0 / Math.sqrt(2.0)), epsilon))
        return false;
    // second way
    info = collider.getContacts(mesh1, mesh0);
    if (info == null)
        return false;
    if (regions.size() != 1)
        return false;
    region = regions.get(0);
    if (region.points.size() != 1)
        return false;
    if (!region.points.get(0).epsilonEquals(new Vector3d(0.5, 0, 0.5), epsilon))
        return false;
    if (!region.normal.epsilonEquals(new Vector3d(-1.0 / Math.sqrt(2.0), 0, -1.0 / Math.sqrt(2.0)), epsilon))
        return false;
    return true;
}
Also used : RigidTransform3d(maspack.matrix.RigidTransform3d) AxisAngle(maspack.matrix.AxisAngle) Vector3d(maspack.matrix.Vector3d) PolygonalMesh(maspack.geometry.PolygonalMesh)

Example 98 with PolygonalMesh

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

the class MeshColliderTest method testEdgeFace.

boolean testEdgeFace() {
    PolygonalMesh mesh0 = new PolygonalMesh();
    PolygonalMesh mesh1 = new PolygonalMesh();
    mesh0.addVertex(new Point3d(-1, -0.1, 0));
    mesh0.addVertex(new Point3d(1, -0.1, 0));
    mesh0.addVertex(new Point3d(0, 1, 0));
    mesh0.addFace(new int[] { 0, 1, 2 });
    mesh1.addVertex(new Point3d(0, 0.1, -1));
    mesh1.addVertex(new Point3d(0, 0.1, 1));
    mesh1.addVertex(new Point3d(0, -1, 0));
    mesh1.addFace(new int[] { 0, 1, 2 });
    MeshCollider collider = new MeshCollider();
    // first way
    ContactInfo info = collider.getContacts(mesh0, mesh1);
    if (info == null) {
        return false;
    }
    ArrayList<ContactPlane> regions = info.getContactPlanes();
    if (regions.size() != 1)
        return false;
    ContactPlane region = regions.get(0);
    if (region.points.size() != 2)
        return false;
    if (!(region.points.get(0).epsilonEquals(new Vector3d(0, 0.1, 0), epsilon) || region.points.get(1).epsilonEquals(new Vector3d(0, 0.1, 0), epsilon)) || !(region.points.get(0).epsilonEquals(new Vector3d(0, -0.1, 0), epsilon) || region.points.get(1).epsilonEquals(new Vector3d(0, -0.1, 0), epsilon)))
        return false;
    if (!region.normal.epsilonEquals(new Vector3d(1.0 / Math.sqrt(2), 0, -1.0 / Math.sqrt(2)), epsilon))
        return false;
    // second way
    info = collider.getContacts(mesh1, mesh0);
    if (info == null) {
        return false;
    }
    regions = info.getContactPlanes();
    if (regions.size() != 1)
        return false;
    region = regions.get(0);
    if (region.points.size() != 2)
        return false;
    if (!(region.points.get(0).epsilonEquals(new Vector3d(0, 0.1, 0), epsilon) || region.points.get(1).epsilonEquals(new Vector3d(0, 0.1, 0), epsilon)) || !(region.points.get(0).epsilonEquals(new Vector3d(0, -0.1, 0), epsilon) || region.points.get(1).epsilonEquals(new Vector3d(0, -0.1, 0), epsilon)))
        return false;
    if (!region.normal.epsilonEquals(new Vector3d(-1.0 / Math.sqrt(2), 0, 1.0 / Math.sqrt(2)), epsilon))
        return false;
    return true;
}
Also used : Vector3d(maspack.matrix.Vector3d) Point3d(maspack.matrix.Point3d) PolygonalMesh(maspack.geometry.PolygonalMesh)

Example 99 with PolygonalMesh

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

the class MeshColliderTest method testVertexFace.

boolean testVertexFace() {
    PolygonalMesh mesh0 = MeshFactory.createBox(1, 1, 1);
    PolygonalMesh mesh1 = MeshFactory.createBox(1, 1, 1);
    RigidTransform3d trans0 = new RigidTransform3d(new Vector3d(Math.sqrt(3.0) / 2.0 + 0.5, 0, 0.25), new AxisAngle());
    mesh0.setMeshToWorld(trans0);
    RigidTransform3d trans1 = new RigidTransform3d();
    trans1.mulAxisAngle(new AxisAngle(0, 1, 0, Math.atan(Math.sqrt(2))));
    trans1.mulAxisAngle(new AxisAngle(1, 0, 0, Math.PI / 4));
    mesh1.setMeshToWorld(trans1);
    MeshCollider collider = new MeshCollider();
    // first way
    ContactInfo info = collider.getContacts(mesh0, mesh1);
    if (info == null) {
        return false;
    }
    ArrayList<ContactPlane> regions = info.getContactPlanes();
    if (regions.size() != 1)
        return false;
    ContactPlane region = regions.get(0);
    if (region.points.size() != 1)
        return false;
    if (!region.points.get(0).epsilonEquals(new Vector3d(Math.sqrt(3.0 / 4.0), 0, 0), epsilon))
        return false;
    if (!region.normal.epsilonEquals(new Vector3d(1, 0, 0), epsilon))
        return false;
    // first way
    info = collider.getContacts(mesh1, mesh0);
    if (info == null)
        return false;
    if (regions.size() != 1)
        return false;
    region = regions.get(0);
    if (region.points.size() != 1)
        return false;
    if (!region.points.get(0).epsilonEquals(new Vector3d(Math.sqrt(3.0 / 4.0), 0, 0), epsilon))
        return false;
    if (!region.normal.epsilonEquals(new Vector3d(-1, 0, 0), epsilon))
        return false;
    return true;
}
Also used : RigidTransform3d(maspack.matrix.RigidTransform3d) AxisAngle(maspack.matrix.AxisAngle) Vector3d(maspack.matrix.Vector3d) PolygonalMesh(maspack.geometry.PolygonalMesh)

Example 100 with PolygonalMesh

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

the class MeshColliderTest method displayContacts.

void displayContacts(PolygonalMesh m0, PolygonalMesh m1) {
    final PolygonalMesh mesh0 = m0;
    final PolygonalMesh mesh1 = m1;
    MeshCollider collider = new MeshCollider();
    final ContactInfo info = collider.getContacts(mesh0, mesh1);
    // final ContactInfo info = new ContactInfo(mesh0, mesh1);
    // System.out.println("intersections " + info.intersections.size());
    // System.out.println("regions " + info.regions.size());
    GLViewerFrame frame = new GLViewerFrame("", 512, 512);
    frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    mesh0.getRenderProps().setDrawEdges(true);
    // mesh0.getRenderProps().setAlpha(0.3);
    mesh1.getRenderProps().setDrawEdges(true);
    // mesh1.getRenderProps().setAlpha(0.3);
    frame.getViewer().addRenderable(mesh0);
    frame.getViewer().addRenderable(mesh1);
    frame.getViewer().addRenderable(new IsRenderable() {

        public int getRenderHints() {
            // TODO Auto-generated method stub
            return 0;
        }

        public void prerender(RenderList list) {
        }

        public void render(Renderer renderer, int flags) {
            renderer.setShading(Shading.NONE);
            if (info != null) {
                renderer.setColor(0, 0, 1);
                renderer.setPointSize(6);
                ArrayList<TriTriIntersection> intersections = info.getIntersections();
                if (intersections != null) {
                    renderer.beginDraw(DrawMode.POINTS);
                    for (TriTriIntersection isect : intersections) {
                        for (Point3d p : isect.points) {
                            renderer.addVertex(p);
                        }
                    }
                    renderer.endDraw();
                }
                renderer.setColor(1, 0, 0);
                renderer.beginDraw(DrawMode.LINES);
                for (ContactPlane region : info.getContactPlanes()) {
                    Point3d avg = new Point3d();
                    int np = 0;
                    for (Point3d rp : region.points) {
                        avg.add(rp);
                        np++;
                    }
                    avg.scale(1.0 / np);
                    renderer.addVertex(avg);
                    avg.add(region.normal);
                    renderer.addVertex(avg);
                }
                renderer.endDraw();
            }
            ;
            // mesh0.getObbtree().render(renderer);
            // mesh1.getObbtree().render(renderer);
            // ////////////////////////////
            // draw mesh numbers
            Vector3d avg0 = new Vector3d();
            Vector3d avg1 = new Vector3d();
            for (Vertex3d v : mesh0.getVertices()) avg0.add(v.pnt);
            avg0.scale(1.0 / mesh0.getVertices().size());
            avg0.add(mesh0.getMeshToWorld().p);
            for (Vertex3d v : mesh1.getVertices()) avg1.add(v.pnt);
            avg1.scale(1.0 / mesh1.getVertices().size());
            avg1.add(mesh1.getMeshToWorld().p);
            // GLUT glut = new GLUT();
            renderer.setColor(1, 1, 1);
            // gl.glRasterPos3d (avg0.x, avg0.y, avg0.z);
            // glut.glutBitmapString (GLUT.BITMAP_HELVETICA_18, "0");
            // gl.glRasterPos3d (avg1.x, avg1.y, avg1.z);
            // glut.glutBitmapString (GLUT.BITMAP_HELVETICA_18, "1");
            // draw mesh normals
            // //////////////////////////////
            renderer.setShading(Shading.FLAT);
        }

        public void updateBounds(Vector3d pmin, Vector3d pmax) {
        // TODO Auto-generated method stub
        }
    });
    frame.getViewer().rerender();
    frame.getViewer().autoFit();
    frame.setVisible(true);
}
Also used : Vertex3d(maspack.geometry.Vertex3d) ArrayList(java.util.ArrayList) TriTriIntersection(maspack.geometry.TriTriIntersection) PolygonalMesh(maspack.geometry.PolygonalMesh) RenderList(maspack.render.RenderList) IsRenderable(maspack.render.IsRenderable) Vector3d(maspack.matrix.Vector3d) GLViewerFrame(maspack.render.GL.GLViewerFrame) Point3d(maspack.matrix.Point3d) Renderer(maspack.render.Renderer)

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