Search in sources :

Example 41 with Vertex3d

use of maspack.geometry.Vertex3d 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 42 with Vertex3d

use of maspack.geometry.Vertex3d 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 43 with Vertex3d

use of maspack.geometry.Vertex3d 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)

Example 44 with Vertex3d

use of maspack.geometry.Vertex3d 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 45 with Vertex3d

use of maspack.geometry.Vertex3d 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)

Aggregations

Vertex3d (maspack.geometry.Vertex3d)81 Point3d (maspack.matrix.Point3d)35 Face (maspack.geometry.Face)30 PolygonalMesh (maspack.geometry.PolygonalMesh)24 Vector3d (maspack.matrix.Vector3d)23 ContactPoint (artisynth.core.mechmodels.ContactPoint)22 Point (artisynth.core.mechmodels.Point)22 ArrayList (java.util.ArrayList)19 PointParticleAttachment (artisynth.core.mechmodels.PointParticleAttachment)15 HalfEdge (maspack.geometry.HalfEdge)13 PointAttachment (artisynth.core.mechmodels.PointAttachment)10 HashMap (java.util.HashMap)10 Vector2d (maspack.matrix.Vector2d)8 VectorNd (maspack.matrix.VectorNd)8 BVFeatureQuery (maspack.geometry.BVFeatureQuery)7 RenderProps (maspack.render.RenderProps)6 PointFem3dAttachment (artisynth.core.femmodels.PointFem3dAttachment)5 Color (java.awt.Color)5 FemNode (artisynth.core.femmodels.FemNode)4 BufferedWriter (java.io.BufferedWriter)4