Search in sources :

Example 21 with Face

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

the class MeshIntersectingProbe method findNextFace.

// does not add the vertex, goes in a loop around contour until we hit a face
private Face findNextFace(Point3d pnt, LinkedList<Point3d> contour, OBBTree obbt, Intersector2d ti, Vector3d vx, Vector3d vy, Point3d o, SplitStorage info) {
    Face face = null;
    Vector3d dir = new Vector3d();
    if (info.idx < contour.size() - 1) {
        dir.sub(contour.get(info.idx + 1), contour.get(info.idx));
    } else {
        dir.sub(contour.get(info.idx), contour.get(info.idx - 1));
    }
    dir.normalize();
    while (face == null && info.idx < contour.size() - 1) {
        Point3d pntNext = contour.get(info.idx + 1);
        Point2d pnt2d = Intersector2d.get2dCoordinate(pnt, vx, vy, o);
        Point2d pnt2dNext = Intersector2d.get2dCoordinate(pntNext, vx, vy, o);
        Vector2d diff2d = new Vector2d();
        ArrayList<BVNode> bvNodes = new ArrayList<BVNode>();
        // get close nodes
        obbt.intersectLineSegment(bvNodes, pnt, pntNext);
        double minDist = Double.MAX_VALUE;
        Point2d minPnt = null;
        for (BVNode node : bvNodes) {
            for (int i = 0; i < node.getNumElements(); i++) {
                Boundable ps = node.getElements()[i];
                if (ps instanceof Face) {
                    Face f = (Face) ps;
                    Vertex3d[] vtxs = f.getVertices();
                    Point2d p0 = Intersector2d.get2dCoordinate(vtxs[0].getWorldPoint(), vx, vy, o);
                    Point2d p1 = Intersector2d.get2dCoordinate(vtxs[1].getWorldPoint(), vx, vy, o);
                    Point2d p2 = Intersector2d.get2dCoordinate(vtxs[2].getWorldPoint(), vx, vy, o);
                    ArrayList<Point2d> points = new ArrayList<Point2d>();
                    ti.intersectTriangleLineSegment(p0, p1, p2, pnt2d, pnt2dNext, points);
                    // check points
                    for (Point2d p : points) {
                        diff2d.sub(p, pnt2d);
                        if (diff2d.norm() < minDist) {
                            face = f;
                            minDist = diff2d.norm();
                            minPnt = p;
                        }
                    }
                }
            }
        }
        if (face == null || minDist >= pnt2dNext.distance(pnt2d)) {
            face = null;
            // move to next point
            info.idx++;
            pnt = contour.get(info.idx);
            if (info.idx < contour.size() - 1) {
                dir.sub(contour.get(info.idx + 1), pnt);
            }
        } else {
            // snap to edge if within tolerance
            Point3d pos = Intersector2d.get3dCoordinate(minPnt, vx, vy, o);
            if (pos.distance(pnt) < ti.epsilon) {
                pos.set(pnt);
            }
            // check if we have to make a new vertex
            info.vtx = createOrGetVertex(pos, face.getVertices(), ti.epsilon);
        }
    }
    return face;
}
Also used : Vertex3d(maspack.geometry.Vertex3d) ArrayList(java.util.ArrayList) Vector2d(maspack.matrix.Vector2d) Vector3d(maspack.matrix.Vector3d) Point2d(maspack.matrix.Point2d) Point3d(maspack.matrix.Point3d) BVNode(maspack.geometry.BVNode) Boundable(maspack.geometry.Boundable) Face(maspack.geometry.Face)

Example 22 with Face

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

the class FaceComponent method drawFaces.

void drawFaces(Renderer renderer, Shading shading, RenderProps props) {
    boolean useVertexColors = useVertexColouring;
    if (renderer.isSelecting()) {
        useVertexColors = false;
    }
    Face face = myFace;
    boolean useVertexNormals = false;
    ArrayList<Vector3d> normals = null;
    int[] normalIndices = null;
    int normalFaceOff = -1;
    if ((shading == Shading.SMOOTH || shading == Shading.METAL) && myMesh.hasNormals()) {
        useVertexNormals = true;
        normals = myMesh.getNormals();
        normalIndices = myMesh.getNormalIndices();
        normalFaceOff = myMesh.getFeatureIndexOffsets()[face.getIndex()];
    }
    Vector3d faceNrm = face.getNormal();
    ArrayList<float[]> colors = null;
    int[] colorIndices = null;
    int colorFaceOff = -1;
    if (useVertexColouring) {
        colors = myMesh.getColors();
        colorIndices = myMesh.getColorIndices();
        colorFaceOff = myMesh.getFeatureIndexOffsets()[face.getIndex()];
    }
    renderer.beginDraw(DrawMode.TRIANGLE_FAN);
    HalfEdge he = face.firstHalfEdge();
    int k = 0;
    do {
        Vertex3d vtx = he.head;
        Point3d pnt = vtx.myRenderPnt;
        if (useVertexColors) {
            int cidx = colorIndices[colorFaceOff + k];
            if (cidx != -1) {
                float[] color = colors.get(cidx);
                renderer.setColor(color);
            }
        }
        if (useVertexNormals) {
            int nidx = normalIndices[normalFaceOff + k];
            if (nidx != -1) {
                Vector3d normal = normals.get(nidx);
                renderer.setNormal(normal);
            }
        } else {
            renderer.setNormal(faceNrm);
        }
        renderer.addVertex(pnt.x, pnt.y, pnt.z);
        he = he.getNext();
        k++;
    } while (he != face.firstHalfEdge());
    renderer.endDraw();
}
Also used : Vertex3d(maspack.geometry.Vertex3d) Vector3d(maspack.matrix.Vector3d) Point3d(maspack.matrix.Point3d) HalfEdge(maspack.geometry.HalfEdge) Face(maspack.geometry.Face)

Example 23 with Face

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

the class FaceComponent method drawEdges.

private void drawEdges(Renderer renderer, RenderProps props) {
    Face face = myFace;
    HalfEdge he = face.firstHalfEdge();
    ArrayList<float[]> colors;
    int[] colorIndices;
    int faceOff;
    if (useVertexColouring) {
        colors = myMesh.getColors();
        colorIndices = myMesh.getColorIndices();
        faceOff = myMesh.getFeatureIndexOffsets()[face.getIndex()];
    } else {
        colors = null;
        colorIndices = null;
        faceOff = -1;
    }
    int k = 0;
    renderer.beginDraw(DrawMode.LINE_LOOP);
    do {
        if (useVertexColouring) {
            int cidx = colorIndices[faceOff + k];
            if (cidx != -1) {
                float[] color = colors.get(cidx);
                renderer.setColor(color);
            }
        }
        Point3d pnt = he.head.myRenderPnt;
        renderer.addVertex(pnt);
        he = he.getNext();
        k++;
    } while (he != face.firstHalfEdge());
    renderer.endDraw();
}
Also used : Point3d(maspack.matrix.Point3d) HalfEdge(maspack.geometry.HalfEdge) Face(maspack.geometry.Face)

Example 24 with Face

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

the class IntersectionTester method contourIsClockwise.

/**
 * Returns true if the countour is oriented so that it travels
 * clockwise with respect to the orientation of the specified mesh.
 */
boolean contourIsClockwise(IntersectionContour contour, PolygonalMesh mesh1, PolygonalMesh mesh2) {
    int csize = contour.size();
    // find the largest contour segment
    Vector3d dir = new Vector3d();
    Vector3d xprod = new Vector3d();
    double dmax = 0;
    boolean clockwise = true;
    for (int i = 0; i < csize; i++) {
        IntersectionPoint pa = contour.get((i) % csize);
        IntersectionPoint pb = contour.get((i + 1) % csize);
        Face face1 = findSegmentFace(pa, pb, mesh1);
        Face face2 = findSegmentFace(pa, pb, mesh2);
        dir.sub(pb, pa);
        xprod.cross(face2.getWorldNormal(), dir);
        double dot = face1.getWorldNormal().dot(xprod);
        // System.out.println (" dot=" + dot);
        if (Math.abs(dot) > dmax) {
            dmax = Math.abs(dot);
            clockwise = (dot < 0);
        }
    }
    return clockwise;
}
Also used : Vector3d(maspack.matrix.Vector3d) Face(maspack.geometry.Face)

Example 25 with Face

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

the class IntersectionTester method findPenetratingFaces.

private Face[] findPenetratingFaces(ArrayList<PenetratingPoint> points) {
    HashSet<Vertex3d> vertices = new HashSet<Vertex3d>();
    for (PenetratingPoint p : points) {
        vertices.add(p.vertex);
    }
    HashSet<Face> faces = new HashSet<Face>();
    for (PenetratingPoint p : points) {
        Vertex3d vtx = p.vertex;
        Iterator<HalfEdge> it = vtx.getIncidentHalfEdges();
        while (it.hasNext()) {
            HalfEdge he = it.next();
            Face face = he.getFace();
            if (!faces.contains(face) && faceIsPenetrating(face, vertices)) {
                faces.add(face);
            }
        }
    }
    return faces.toArray(new Face[0]);
}
Also used : Vertex3d(maspack.geometry.Vertex3d) HalfEdge(maspack.geometry.HalfEdge) Face(maspack.geometry.Face)

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