Search in sources :

Example 46 with Face

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

the class MFreeMeshComp method createPointAttachment.

public PointFem3dAttachment createPointAttachment(Point pnt) {
    if (!(getMesh() instanceof PolygonalMesh)) {
        return null;
    }
    PolygonalMesh mesh = (PolygonalMesh) getMesh();
    if (!mesh.isTriangular()) {
        return null;
    }
    // Find nearest face to the point. The vertices of this face will be used
    // to find the nodes and weight for the attachment.
    BVFeatureQuery query = new BVFeatureQuery();
    Point3d near = new Point3d();
    Vector2d uv = new Vector2d();
    Face face = query.nearestFaceToPoint(near, uv, mesh, pnt.getPosition());
    Vertex3d[] vtxs = face.getTriVertices();
    double[] wgts = new double[] { 1 - uv.x - uv.y, uv.x, uv.y };
    HashMap<FemNode, Double> nodeWeights = new HashMap<FemNode, Double>();
    for (int i = 0; i < vtxs.length; i++) {
        PointAttachment va = myVertexAttachments.get(vtxs[i].getIndex());
        if (va instanceof PointParticleAttachment) {
            PointParticleAttachment ppa = (PointParticleAttachment) va;
            FemNode node = (FemNode) ppa.getParticle();
            accumulateNodeWeights(node, wgts[i], nodeWeights);
        } else if (va instanceof PointFem3dAttachment) {
            PointFem3dAttachment pfa = (PointFem3dAttachment) va;
            for (int k = 0; k < pfa.numMasters(); k++) {
                FemNode node = pfa.getNodes()[k];
                double w = pfa.getCoordinate(k);
                accumulateNodeWeights(node, w * wgts[i], nodeWeights);
            }
        }
    }
    // Create a new PointFem3dAttachment
    PointFem3dAttachment ax = new PointFem3dAttachment(pnt);
    VectorNd weightVec = new VectorNd();
    for (Double d : nodeWeights.values()) {
        weightVec.append(d);
    }
    ax.setFromNodes(nodeWeights.keySet(), weightVec);
    return ax;
}
Also used : Vertex3d(maspack.geometry.Vertex3d) FemNode(artisynth.core.femmodels.FemNode) HashMap(java.util.HashMap) PointAttachment(artisynth.core.mechmodels.PointAttachment) PolygonalMesh(maspack.geometry.PolygonalMesh) BVFeatureQuery(maspack.geometry.BVFeatureQuery) ContactPoint(artisynth.core.mechmodels.ContactPoint) Point(artisynth.core.mechmodels.Point) Vector2d(maspack.matrix.Vector2d) Point3d(maspack.matrix.Point3d) VectorNd(maspack.matrix.VectorNd) PointFem3dAttachment(artisynth.core.femmodels.PointFem3dAttachment) Face(maspack.geometry.Face) PointParticleAttachment(artisynth.core.mechmodels.PointParticleAttachment)

Example 47 with Face

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

the class MFreeMeshComp method writeMesh.

public boolean writeMesh(PrintWriter pw, boolean nodeFormat) {
    PolygonalMesh mesh = null;
    if (!(getMesh() instanceof PolygonalMesh)) {
        return false;
    }
    mesh = (PolygonalMesh) getMesh();
    pw.print("[ ");
    NumberFormat fmt = new NumberFormat("%.8g");
    IndentingPrintWriter.addIndentation(pw, 2);
    if (!nodeFormat) {
        for (Vertex3d vtx : mesh.getVertices()) {
            writeVertexInfo(pw, vtx, fmt);
        }
    }
    ArrayList<Integer> nodeNums = new ArrayList<Integer>();
    for (Face face : mesh.getFaces()) {
        HalfEdge he0 = face.firstHalfEdge();
        HalfEdge he = he0;
        pw.print("f");
        do {
            int vidx = he.head.getIndex();
            if (nodeFormat) {
                PointParticleAttachment ppa = (PointParticleAttachment) getAttachment(vidx);
                MFreeNode3d node = (MFreeNode3d) ppa.getParticle();
                pw.print(" " + node.getNumber());
            } else {
                pw.print(" " + (vidx + 1));
            }
            he = he.getNext();
        } while (he != he0);
        pw.println("");
    }
    IndentingPrintWriter.addIndentation(pw, -2);
    pw.println("]");
    return true;
}
Also used : Vertex3d(maspack.geometry.Vertex3d) ArrayList(java.util.ArrayList) HalfEdge(maspack.geometry.HalfEdge) Face(maspack.geometry.Face) PolygonalMesh(maspack.geometry.PolygonalMesh) PointParticleAttachment(artisynth.core.mechmodels.PointParticleAttachment) ContactPoint(artisynth.core.mechmodels.ContactPoint) Point(artisynth.core.mechmodels.Point) NumberFormat(maspack.util.NumberFormat)

Example 48 with Face

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

the class RigidMeshComp method getDistanceGrid.

@Override
public DistanceGrid getDistanceGrid() {
    if (getMesh() instanceof PolygonalMesh) {
        if (mySDGrid == null) {
            List<Face> faces = ((PolygonalMesh) getMesh()).getFaces();
            mySDGrid = new DistanceGrid(faces, myGridMargin, myMaxGridDivisions, /*signed=*/
            true);
        }
    } else {
        mySDGrid = null;
    }
    return mySDGrid;
}
Also used : DistanceGrid(maspack.geometry.DistanceGrid) Face(maspack.geometry.Face) PolygonalMesh(maspack.geometry.PolygonalMesh)

Example 49 with Face

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

the class StlWriter method writeMesh.

public void writeMesh(PolygonalMesh mesh) throws IOException {
    NumberFormat fmt = myFmt;
    PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(myOstream)));
    String name = mesh.getName();
    if (name == null) {
        name = "";
    }
    pw.println("solid " + name);
    for (Face face : mesh.getFaces()) {
        Vector3d n = face.getNormal();
        if (face.isTriangle()) {
            pw.println("facet normal " + fmt.format((float) n.x) + " " + fmt.format((float) n.y) + " " + fmt.format((float) n.z));
        } else {
            pw.println("facet normal 0 0 0");
        }
        pw.println("  outer loop");
        for (int i = 0; i < face.numVertices(); i++) {
            Point3d pos = face.getVertex(i).getPosition();
            pw.println("    vertex " + fmt.format((float) pos.x) + " " + fmt.format((float) pos.y) + " " + fmt.format((float) pos.z));
        }
        pw.println("  endloop");
        pw.println("endfacet");
    }
    pw.println("endsolid " + name);
    pw.close();
}
Also used : Vector3d(maspack.matrix.Vector3d) Point3d(maspack.matrix.Point3d) OutputStreamWriter(java.io.OutputStreamWriter) Face(maspack.geometry.Face) NumberFormat(maspack.util.NumberFormat) PrintWriter(java.io.PrintWriter) BufferedWriter(java.io.BufferedWriter)

Aggregations

Face (maspack.geometry.Face)49 Vertex3d (maspack.geometry.Vertex3d)30 Point3d (maspack.matrix.Point3d)25 Vector3d (maspack.matrix.Vector3d)20 HalfEdge (maspack.geometry.HalfEdge)16 PolygonalMesh (maspack.geometry.PolygonalMesh)14 ArrayList (java.util.ArrayList)11 Vector2d (maspack.matrix.Vector2d)9 ContactPoint (artisynth.core.mechmodels.ContactPoint)7 Point (artisynth.core.mechmodels.Point)7 HashMap (java.util.HashMap)7 BVFeatureQuery (maspack.geometry.BVFeatureQuery)6 PointParticleAttachment (artisynth.core.mechmodels.PointParticleAttachment)5 IntersectionPoint (maspack.collision.IntersectionPoint)5 BufferedWriter (java.io.BufferedWriter)4 OutputStreamWriter (java.io.OutputStreamWriter)4 PrintWriter (java.io.PrintWriter)4 DistanceGrid (maspack.geometry.DistanceGrid)4 PointAttachment (artisynth.core.mechmodels.PointAttachment)3 PenetratingPoint (maspack.collision.PenetratingPoint)3