Search in sources :

Example 51 with PolygonalMesh

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

the class EditableMeshTest method build.

@Override
public void build(String[] args) throws IOException {
    super.build(args);
    Color[] colors = { Color.RED, Color.GREEN, Color.BLUE, Color.CYAN, Color.GRAY, Color.MAGENTA, Color.ORANGE, Color.PINK, Color.YELLOW, Color.RED.darker().darker(), Color.GREEN.darker().darker(), Color.BLUE.darker().darker(), Color.CYAN.darker().darker(), Color.GRAY.darker().darker(), Color.MAGENTA.darker().darker(), Color.ORANGE.darker().darker(), Color.PINK.darker().darker(), Color.YELLOW.darker().darker(), Color.RED.brighter().brighter(), Color.GREEN.brighter().brighter(), Color.BLUE.brighter().brighter(), Color.CYAN.brighter().brighter(), Color.GRAY.brighter().brighter(), Color.MAGENTA.brighter().brighter(), Color.ORANGE.brighter().brighter(), Color.PINK.brighter().brighter(), Color.YELLOW.brighter().brighter() };
    PolygonalMesh mesh = MeshFactory.createIcosahedralSphere(1, 0);
    mesh.setFeatureColoringEnabled();
    for (int i = 0; i < mesh.numColors(); ++i) {
        mesh.setColor(i, colors[i % colors.length]);
    }
    EditablePolygonalMeshComp comp = new EditablePolygonalMeshComp(mesh);
    addRenderable(comp);
}
Also used : EditablePolygonalMeshComp(artisynth.core.renderables.EditablePolygonalMeshComp) Color(java.awt.Color) PolygonalMesh(maspack.geometry.PolygonalMesh)

Example 52 with PolygonalMesh

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

the class CSGCubeTest method build.

@Override
public void build(String[] args) throws IOException {
    super.build(args);
    PolygonalMesh cube1 = MeshFactory.createBox(1, 1, 1);
    PolygonalMesh cube2 = MeshFactory.createBox(0.5, 0.5, 0.5);
    cube2.transform(new RigidTransform3d(new Vector3d(0.25, 0.25, 0.25), AxisAngle.IDENTITY));
    cube1.setHardEdgesFromFaceNormals(0.707);
    cube2.setHardEdgesFromFaceNormals(0.707);
    PolygonalMesh isect = MeshFactory.getIntersection(cube1, cube2);
    isect.setHardEdgesFromFaceNormals(0.707);
    PolygonalMesh sub = MeshFactory.getSubtraction(cube1, cube2);
    sub.setHardEdgesFromFaceNormals(0.707);
    PolygonalMesh union = MeshFactory.getUnion(cube1, cube2);
    union.setHardEdgesFromFaceNormals(0.707);
    addRenderable(new FixedMeshBody("cube1", cube1));
    addRenderable(new FixedMeshBody("cube2", cube2));
    addRenderable(new FixedMeshBody("intersection", isect));
    addRenderable(new FixedMeshBody("subtraction", sub));
    addRenderable(new FixedMeshBody("union", union));
    for (Renderable r : renderables()) {
        if (r instanceof FixedMeshBody) {
            FixedMeshBody fmesh = (FixedMeshBody) r;
            PolygonalMesh mesh = (PolygonalMesh) (fmesh.getMesh());
            System.out.println("Mesh: " + fmesh.getName());
            System.out.println("  # verts: " + mesh.numVertices());
            System.out.println("  # faces: " + mesh.numFaces());
            System.out.println("  closed:  " + mesh.isClosed());
            System.out.println("  manifo:  " + mesh.isManifold());
            System.out.println("  area:    " + mesh.computeArea());
            System.out.println("  volume:  " + mesh.computeVolume());
        }
    }
    // PolygonalMesh cube3 = new PolygonalMesh(cube1);
    // cube3 = MeshFactory.subdivide(cube3);
    // cube3.mergeCoplanarFaces(0.99);
    // for (Face f : cube3.getFaces()) {
    // System.out.println(f.getIndex() + ", " + f.numEdges());
    // }
    // 
    // int[] nrmIdxs = cube3.getNormalIndices();
    // int[] offsets = cube3.getFeatureIndexOffsets();
    // for (int i=0; i<offsets.length-1; ++i) {
    // System.out.println("Face " + i + ": ");
    // for (int j = offsets[i]; j<offsets[i+1]; ++j) {
    // Vector3d nrm = cube3.getNormal(nrmIdxs[j]);
    // System.out.println(nrm.toString());
    // }
    // }
    // FixedMeshBody fm = new FixedMeshBody("cube3", cube3);
    // addRenderable(fm);
    // RenderProps.setFaceStyle(fm, FaceStyle.FRONT_AND_BACK);
    // RenderProps.setFaceColor(fm, Color.BLUE);
    // RenderProps.setBackColor(fm, Color.YELLOW);
    RenderProps.setDrawEdges(this, true);
}
Also used : RigidTransform3d(maspack.matrix.RigidTransform3d) Renderable(maspack.render.Renderable) Vector3d(maspack.matrix.Vector3d) FixedMeshBody(artisynth.core.mechmodels.FixedMeshBody) PolygonalMesh(maspack.geometry.PolygonalMesh)

Example 53 with PolygonalMesh

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

the class ColoredSphereTest method build.

@Override
public void build(String[] args) throws IOException {
    super.build(args);
    PolygonalMesh mesh = MeshFactory.createOctahedralSphere(1, 4);
    HueColorMap map = new HueColorMap();
    mesh.setVertexColoringEnabled();
    for (int i = 0; i < mesh.numVertices(); ++i) {
        // hsv interpolation of colors based on height (-1 to 1)
        Vertex3d vtx = mesh.getVertex(i);
        double pos = vtx.getPosition().z;
        Color c = map.getColor((pos + 1) / 2);
        mesh.setColor(i, c);
    }
    RenderProps rprops = new RenderProps();
    rprops.setShading(Shading.SMOOTH);
    rprops.setShininess(128);
    rprops.setSpecular(Color.WHITE);
    mesh.setRenderProps(rprops);
    FixedMeshBody fm = new FixedMeshBody(mesh);
    addRenderable(fm);
}
Also used : HueColorMap(maspack.render.color.HueColorMap) Vertex3d(maspack.geometry.Vertex3d) Color(java.awt.Color) RenderProps(maspack.render.RenderProps) FixedMeshBody(artisynth.core.mechmodels.FixedMeshBody) PolygonalMesh(maspack.geometry.PolygonalMesh)

Example 54 with PolygonalMesh

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

the class IntersectionTester method prerender.

@Override
public void prerender(RenderList list) {
    super.prerender(list);
    if (myZPerturb0 != null) {
        for (Vertex3d v : myMesh0.getVertices()) {
            if (isInterior(v)) {
                v.pnt.z = RandomGenerator.nextDouble(myZPerturb0.getLowerBound(), myZPerturb0.getUpperBound());
            }
        }
        myMesh0.notifyVertexPositionsModified();
        myMesh0.updateFaceNormals();
    }
    if (myZPerturb1 != null) {
        for (Vertex3d v : myMesh1.getVertices()) {
            if (isInterior(v)) {
                v.pnt.z = RandomGenerator.nextDouble(myZPerturb1.getLowerBound(), myZPerturb1.getUpperBound());
            }
        }
        myMesh1.notifyVertexPositionsModified();
        myMesh1.updateFaceNormals();
    }
    PolygonalMesh csgMesh = null;
    FunctionTimer timer = new FunctionTimer();
    if (myContoursOnly) {
        timer.start();
        System.out.println("finding contours");
        myContours = myIntersector.findContours(myMesh0, myMesh1);
        System.out.println("num contours=" + myContours.size());
        timer.stop();
        if (myContours.size() == 0) {
            return;
        }
    } else {
        timer.start();
        if (myCSGOperation == SurfaceMeshIntersector.CSG.INTERSECTION) {
            myContactInfo = myIntersector.findContoursAndRegions(myMesh0, RegionType.INSIDE, myMesh1, RegionType.INSIDE);
            csgMesh = myIntersector.createCSGMesh(myContactInfo);
        } else if (myCSGOperation == SurfaceMeshIntersector.CSG.UNION) {
            myContactInfo = myIntersector.findContoursAndRegions(myMesh0, RegionType.OUTSIDE, myMesh1, RegionType.OUTSIDE);
            csgMesh = myIntersector.createCSGMesh(myContactInfo);
        } else if (myCSGOperation == SurfaceMeshIntersector.CSG.DIFFERENCE01) {
            myContactInfo = myIntersector.findContoursAndRegions(myMesh0, RegionType.OUTSIDE, myMesh1, RegionType.INSIDE);
            csgMesh = myIntersector.createCSGMesh(myContactInfo);
        } else if (myCSGOperation == SurfaceMeshIntersector.CSG.DIFFERENCE10) {
            myContactInfo = myIntersector.findContoursAndRegions(myMesh0, RegionType.INSIDE, myMesh1, RegionType.OUTSIDE);
            csgMesh = myIntersector.createCSGMesh(myContactInfo);
        } else {
            myContactInfo = myIntersector.findContoursAndRegions(myMesh0, RegionType.INSIDE, myMesh1, RegionType.INSIDE);
        }
        timer.stop();
        if (myContactInfo == null) {
            myContours = null;
            return;
        }
        myContours = myContactInfo.getContours();
        ArrayList<PenetratingPoint> points0 = myContactInfo.getPenetratingPoints(0);
        // if (points0 != null) {
        // System.out.println ("num verts0= " + points0.size());
        // }
        // else {
        // System.out.println ("num verts0= null");
        // }
        ArrayList<PenetratingPoint> points1 = myContactInfo.getPenetratingPoints(1);
        // if (points1 != null) {
        // System.out.println ("num verts1= " + points1.size());
        // }
        // else {
        // System.out.println ("num verts1= null");
        // }
        ArrayList<PenetrationRegion> regions;
        if (false) {
            System.out.println("Regions 0:");
            regions = myContactInfo.getRegions(0);
            for (int i = 0; i < regions.size(); i++) {
                PenetrationRegion r = regions.get(i);
                int[] faceIdxs = SurfaceMeshIntersectorTest.getFaceIndices(r.getFaces());
                ArraySort.sort(faceIdxs);
                int[] vtxIdxs = SurfaceMeshIntersectorTest.getVertexIndices(r);
                ArraySort.sort(vtxIdxs);
                System.out.println(" " + i + " area=" + r.getArea() + " faces=[ " + ArraySupport.toString(faceIdxs) + " vtxs=[ " + ArraySupport.toString(vtxIdxs) + "]");
            }
            System.out.println("Regions 1:");
            regions = myContactInfo.getRegions(1);
            for (int i = 0; i < regions.size(); i++) {
                PenetrationRegion r = regions.get(i);
                int[] faceIdxs = SurfaceMeshIntersectorTest.getFaceIndices(r.getFaces());
                ArraySort.sort(faceIdxs);
                int[] vtxIdxs = SurfaceMeshIntersectorTest.getVertexIndices(r);
                ArraySort.sort(vtxIdxs);
                System.out.println(" " + i + " area=" + r.getArea() + " faces=[ " + ArraySupport.toString(faceIdxs) + " vtxs=[ " + ArraySupport.toString(vtxIdxs) + "]");
            }
            System.out.println("contours:");
            RigidTransform3d T = new RigidTransform3d();
            ArrayList<IntersectionContour> contours = myContactInfo.getContours();
            for (int k = 0; k < contours.size(); k++) {
                IntersectionContour c = contours.get(k);
                c.printCornerPoints("contour " + k + " closed=" + c.isClosed(), "%20.16f", T);
            }
        }
        if (false) {
            String pfx = "         ";
            System.out.print(pfx + "new int[] {");
            int k = 0;
            Collections.sort(points0, new CPPVertexComparator());
            for (PenetratingPoint cpp : points0) {
                System.out.print((k++ > 0 ? ", " : " ") + cpp.vertex.getIndex());
            }
            System.out.println(" },");
            System.out.print(pfx + "new int[] {");
            k = 0;
            Collections.sort(points1, new CPPVertexComparator());
            for (PenetratingPoint cpp : points1) {
                System.out.print((k++ > 0 ? ", " : " ") + cpp.vertex.getIndex());
            }
            System.out.println(" },");
        }
        if (myRenderCSGMesh && csgMesh != null) {
            csgMesh.prerender(myRenderProps);
            myCSGMesh = csgMesh;
        } else {
            myCSGMesh = null;
        }
    }
// System.out.println ("time=" + timer.result(1));
}
Also used : Vertex3d(maspack.geometry.Vertex3d) RigidTransform3d(maspack.matrix.RigidTransform3d) FunctionTimer(maspack.util.FunctionTimer) PolygonalMesh(maspack.geometry.PolygonalMesh)

Example 55 with PolygonalMesh

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

the class IntersectionTester method getPointEdge.

/**
 * Return the edge of a face that contains an intersection point.
 */
private HalfEdge getPointEdge(IntersectionPoint pa, Face face) {
    PolygonalMesh mesh = (PolygonalMesh) face.getMesh();
    if (edgeOnMesh(pa.edge, mesh)) {
        if (pa.edge.getFace() == face) {
            return pa.edge;
        } else if (getOppositeFace(pa.edge) == face) {
            return pa.edge.opposite;
        } else {
            throw new InternalErrorException("Face edge not found for point " + pa);
        }
    } else {
        // convert pa to mesh local coordinates
        Point3d paLoc = new Point3d(pa);
        paLoc.inverseTransform(mesh.getMeshToWorld());
        HalfEdge he0 = face.firstHalfEdge();
        HalfEdge he = he0;
        HalfEdge heMin = null;
        double dmin = Double.POSITIVE_INFINITY;
        do {
            double d = LineSegment.distance(he.getHead().pnt, he.getTail().pnt, paLoc);
            if (d < dmin) {
                heMin = he;
                dmin = d;
            }
            he = he.getNext();
        } while (he != he0);
        return heMin;
    }
}
Also used : Point3d(maspack.matrix.Point3d) HalfEdge(maspack.geometry.HalfEdge) InternalErrorException(maspack.util.InternalErrorException) PolygonalMesh(maspack.geometry.PolygonalMesh)

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