Search in sources :

Example 26 with Face

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

the class RenderObjectExamples method example2.

/**
 * Example showing a simplified mesh renderer
 */
public static void example2(Renderer renderer, PolygonalMesh mesh, boolean drawEdges, boolean drawVertices) {
    RenderObject r = new RenderObject();
    int[] nidxs = null;
    int[] cidxs = null;
    int[] tidxs = null;
    // add all appropriate info
    for (Vertex3d vtx : mesh.getVertices()) {
        Point3d pos = vtx.getPosition();
        r.addPosition((float) pos.x, (float) pos.y, (float) pos.z);
    }
    if (mesh.getNormals() != null) {
        for (Vector3d nrm : mesh.getNormals()) {
            r.addNormal((float) nrm.x, (float) nrm.y, (float) nrm.z);
        }
        nidxs = mesh.getNormalIndices();
    }
    if (mesh.getColors() != null) {
        for (float[] color : mesh.getColors()) {
            r.addColor(color);
        }
        cidxs = mesh.getColorIndices();
    }
    if (mesh.getTextureCoords() != null) {
        for (Vector3d texCoord : mesh.getTextureCoords()) {
            // according to existing MeshRenderer, we need to flip y
            r.addTextureCoord((float) texCoord.x, (float) (1 - texCoord.y));
        }
        tidxs = mesh.getTextureIndices();
    }
    // build faces
    int[] indexOffs = mesh.getFeatureIndexOffsets();
    List<Face> faces = mesh.getFaces();
    for (int i = 0; i < faces.size(); i++) {
        Face f = faces.get(i);
        int foff = indexOffs[f.idx];
        int[] pidxs = f.getVertexIndices();
        // vertex indices
        int[] vidxs = new int[pidxs.length];
        for (int j = 0; j < pidxs.length; j++) {
            vidxs[j] = r.addVertex(pidxs[j], nidxs != null ? nidxs[foff + j] : -1, cidxs != null ? cidxs[foff + j] : -1, tidxs != null ? tidxs[foff + j] : -1);
        }
        // triangle fan for faces, line loop for edges
        r.addTriangleFan(vidxs);
        if (drawEdges) {
            r.addLineLoop(vidxs);
        }
    }
    // draw mesh
    // <set face color and maybe polygon offset>
    // draw faces
    renderer.drawTriangles(r);
    if (drawEdges) {
        // <set edge color>
        // draw edges
        renderer.drawLines(r);
    }
    if (drawVertices) {
        // <set vertex color>
        // Rather than set up a set of point primitives, which would result
        // in an extra index array in corresponding VBOs, draw the vertex
        // array directly with an appropriate mode.
        // draw all vertices as points
        renderer.drawVertices(r, DrawMode.POINTS);
    // less-efficient alternative:
    // for (int i=0; i<r.numVertices(); i++) {
    // r.addPoint(i);
    // }
    // renderer.drawPoints(r);
    }
}
Also used : Vertex3d(maspack.geometry.Vertex3d) Vector3d(maspack.matrix.Vector3d) Point3d(maspack.matrix.Point3d) Face(maspack.geometry.Face)

Example 27 with Face

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

the class RenderObjectFactory method createFromMesh.

/**
 * Creates a RenderObject from a PolygonalMesh, with one group of triangle
 * primitives for faces.  Non-triangular faces are replaced by a triangle
 * fan.
 * @param mesh mesh for which the render object is to be created
 * @param flatNormals use "flat" shading normals
 * @param addEdges add edge primitives
 * @return created RenderObject
 */
public static RenderObject createFromMesh(PolygonalMesh mesh, boolean flatNormals, boolean addEdges) {
    RenderObject r = new RenderObject();
    int[] nidxs = null;
    int[] cidxs = null;
    int[] tidxs = null;
    // add all appropriate info
    for (Vertex3d vtx : mesh.getVertices()) {
        Point3d pos = vtx.getPosition();
        r.addPosition((float) pos.x, (float) pos.y, (float) pos.z);
    }
    if (!flatNormals) {
        if (mesh.getNormals() != null) {
            for (Vector3d nrm : mesh.getNormals()) {
                r.addNormal((float) nrm.x, (float) nrm.y, (float) nrm.z);
            }
            nidxs = mesh.getNormalIndices();
        }
    } else {
        for (Face f : mesh.getFaces()) {
            Vector3d nrm = new Vector3d();
            f.computeNormal(nrm);
            r.addNormal((float) nrm.x, (float) nrm.y, (float) nrm.z);
        }
    }
    if (mesh.getColors() != null) {
        for (float[] color : mesh.getColors()) {
            r.addColor(color);
        }
        cidxs = mesh.getColorIndices();
    }
    if (mesh.getTextureCoords() != null) {
        for (Vector3d texCoord : mesh.getTextureCoords()) {
            // according to existing MeshRenderer, we need to flip y
            r.addTextureCoord((float) texCoord.x, (float) (1 - texCoord.y));
        }
        tidxs = mesh.getTextureIndices();
    }
    // keep a map of unique vertices to reduce storage requirements
    HashMap<VertexIndexSet, Integer> uniqueVerts = new HashMap<>();
    // build faces
    int[] indexOffs = mesh.getFeatureIndexOffsets();
    List<Face> faces = mesh.getFaces();
    for (int i = 0; i < faces.size(); i++) {
        Face f = faces.get(i);
        int foff = indexOffs[f.idx];
        int[] pidxs = f.getVertexIndices();
        // vertex indices
        int[] vidxs = new int[pidxs.length];
        int nidx = f.idx;
        for (int j = 0; j < pidxs.length; j++) {
            if (!flatNormals) {
                nidx = nidxs != null ? nidxs[foff + j] : -1;
            }
            // only add if unique combination
            VertexIndexSet v = new VertexIndexSet(pidxs[j], nidx, cidxs != null ? cidxs[foff + j] : -1, tidxs != null ? tidxs[foff + j] : -1);
            Integer vidx = uniqueVerts.get(v);
            if (vidx != null) {
                vidxs[j] = vidx.intValue();
            } else {
                vidxs[j] = r.addVertex(v.pidx, v.nidx, v.cidx, v.tidx);
                uniqueVerts.put(v, vidxs[j]);
            }
        }
        // triangle fan for faces, line loop for edges
        r.addTriangleFan(vidxs);
        if (addEdges) {
            r.addLineLoop(vidxs);
        }
    }
    // r.commit();  // finalize construction
    return r;
}
Also used : Vertex3d(maspack.geometry.Vertex3d) HashMap(java.util.HashMap) Vector3d(maspack.matrix.Vector3d) Point3d(maspack.matrix.Point3d) Face(maspack.geometry.Face)

Example 28 with Face

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

the class MultiViewerTesterBase method addHalfBunny.

protected static void addHalfBunny(MultiViewer tester, PolygonalMesh bunny) {
    RenderProps rprops = new RenderProps();
    rprops.setFaceStyle(FaceStyle.FRONT_AND_BACK);
    rprops.setShading(Shading.SMOOTH);
    rprops.setBackColor(Color.MAGENTA.darker());
    rprops.setSpecular(Color.WHITE);
    rprops.setShininess(1000);
    if (bunny != null) {
        RenderObject r = new RenderObject();
        // add all appropriate info
        for (Vertex3d vtx : bunny.getVertices()) {
            Point3d pos = vtx.getPosition();
            r.addPosition((float) pos.x, (float) pos.y, (float) pos.z);
        }
        for (Vector3d nrm : bunny.getNormals()) {
            r.addNormal((float) nrm.x, (float) nrm.y, (float) nrm.z);
        }
        int[] nidxs = bunny.getNormalIndices();
        // left
        r.createTriangleGroup();
        // right
        r.createTriangleGroup();
        // build faces
        List<Face> faces = bunny.getFaces();
        int[] indexOffs = bunny.getFeatureIndexOffsets();
        Vector3d centroid = new Vector3d();
        for (int i = 0; i < faces.size(); i++) {
            Face f = faces.get(i);
            int foff = indexOffs[f.idx];
            int[] pidxs = f.getVertexIndices();
            // vertex indices
            int[] vidxs = new int[pidxs.length];
            for (int j = 0; j < pidxs.length; j++) {
                // only add if unique combination
                vidxs[j] = r.addVertex(pidxs[j], nidxs[foff + j], -1, -1);
            }
            // triangle fan for faces
            f.computeCentroid(centroid);
            if (centroid.x < centroid.y) {
                r.triangleGroup(0);
            } else {
                r.triangleGroup(1);
            }
            r.addTriangleFan(vidxs);
        }
        MultiTriangleGroupWrapper rbunny = new MultiTriangleGroupWrapper(r);
        rbunny.setTransform(new RigidTransform3d(new Vector3d(1.3, 1.3, 0.5), AxisAngle.IDENTITY));
        rbunny.setRenderProps(rprops);
        rbunny.setFaceColors(Color.RED, Color.BLUE);
        tester.addRenderable(rbunny);
    }
}
Also used : Vertex3d(maspack.geometry.Vertex3d) RigidTransform3d(maspack.matrix.RigidTransform3d) Vector3d(maspack.matrix.Vector3d) Point3d(maspack.matrix.Point3d) RenderProps(maspack.render.RenderProps) RenderObject(maspack.render.RenderObject) Face(maspack.geometry.Face)

Example 29 with Face

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

the class CollisionRenderer method buildFaceSegments.

protected void buildFaceSegments(RenderObject ro, CollisionHandler handler, ArrayList<TriTriIntersection> intersections) {
    BVFeatureQuery query = new BVFeatureQuery();
    PolygonalMesh mesh0 = handler.getCollidable(0).getCollisionMesh();
    PolygonalMesh mesh1 = handler.getCollidable(1).getCollisionMesh();
    ArrayList<Face> faces = new ArrayList<Face>();
    // mark faces as visited and add segments
    for (TriTriIntersection isect : intersections) {
        isect.face0.setVisited();
        isect.face1.setVisited();
    // add partials?
    }
    // mark interior faces and add segments
    for (TriTriIntersection isect : intersections) {
        if (isect.face0.getMesh() != mesh0) {
            findInsideFaces(isect.face0, query, mesh0, faces);
            findInsideFaces(isect.face1, query, mesh1, faces);
        } else {
            findInsideFaces(isect.face0, query, mesh1, faces);
            findInsideFaces(isect.face1, query, mesh0, faces);
        }
    }
    for (TriTriIntersection isect : intersections) {
        isect.face0.clearVisited();
        isect.face1.clearVisited();
    }
    // add faces to render object and clear visited flag
    Vector3d nrm = new Vector3d();
    Point3d p0 = new Point3d();
    Point3d p1 = new Point3d();
    Point3d p2 = new Point3d();
    for (Face face : faces) {
        face.clearVisited();
        face.getWorldNormal(nrm);
        ro.addNormal((float) nrm.x, (float) nrm.y, (float) nrm.z);
        HalfEdge he = face.firstHalfEdge();
        he.head.getWorldPoint(p0);
        he = he.getNext();
        he.head.getWorldPoint(p1);
        he = he.getNext();
        he.head.getWorldPoint(p2);
        int v0idx = ro.vertex((float) p0.x, (float) p0.y, (float) p0.z);
        int v1idx = ro.vertex((float) p1.x, (float) p1.y, (float) p1.z);
        int v2idx = ro.vertex((float) p2.x, (float) p2.y, (float) p2.z);
        ro.addTriangle(v0idx, v1idx, v2idx);
    }
}
Also used : Vector3d(maspack.matrix.Vector3d) Point3d(maspack.matrix.Point3d) ArrayList(java.util.ArrayList) TriTriIntersection(maspack.geometry.TriTriIntersection) HalfEdge(maspack.geometry.HalfEdge) Face(maspack.geometry.Face) BVFeatureQuery(maspack.geometry.BVFeatureQuery) PolygonalMesh(maspack.geometry.PolygonalMesh) IntersectionPoint(maspack.collision.IntersectionPoint) PenetratingPoint(maspack.collision.PenetratingPoint)

Example 30 with Face

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

the class CollisionRenderer method findInsideFaces.

protected void findInsideFaces(Face face, BVFeatureQuery query, PolygonalMesh mesh, ArrayList<Face> faces) {
    face.setVisited();
    Point3d pnt = new Point3d();
    HalfEdge he = face.firstHalfEdge();
    for (int i = 0; i < 3; i++) {
        if (he.opposite != null) {
            Face oFace = he.opposite.getFace();
            if (!oFace.isVisited()) {
                // check if inside
                oFace.computeWorldCentroid(pnt);
                boolean inside = query.isInsideOrientedMesh(mesh, pnt, -1);
                if (inside) {
                    faces.add(oFace);
                    findInsideFaces(oFace, query, mesh, faces);
                }
            }
        }
        he = he.getNext();
    }
}
Also used : Point3d(maspack.matrix.Point3d) HalfEdge(maspack.geometry.HalfEdge) Face(maspack.geometry.Face) IntersectionPoint(maspack.collision.IntersectionPoint) PenetratingPoint(maspack.collision.PenetratingPoint)

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