Search in sources :

Example 46 with Vertex3d

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

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

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

the class ColouredSphereTest method addContent.

@Override
protected void addContent(MultiViewer mv) {
    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);
    mv.addRenderable(mesh);
}
Also used : HueColorMap(maspack.render.color.HueColorMap) Vertex3d(maspack.geometry.Vertex3d) Color(java.awt.Color) RenderProps(maspack.render.RenderProps) PolygonalMesh(maspack.geometry.PolygonalMesh)

Example 49 with Vertex3d

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

the class IntersectionTester method computePenetratingFaceArea.

double computePenetratingFaceArea(ArrayList<PenetratingPoint> points) {
    double area = 0;
    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)) {
                area += face.computeArea();
                System.out.println(" adding face " + face.getIndex() + " " + area);
                faces.add(face);
            }
        }
    }
    return area;
}
Also used : Vertex3d(maspack.geometry.Vertex3d) HalfEdge(maspack.geometry.HalfEdge) Face(maspack.geometry.Face)

Example 50 with Vertex3d

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

the class PolygonalMeshTest method squareTest.

public void squareTest() {
    PolygonalMesh mesh = new PolygonalMesh();
    Vertex3d vt0 = new Vertex3d(new Point3d(0, 0, 0));
    Vertex3d vt1 = new Vertex3d(new Point3d(1, 0, 0));
    Vertex3d vt2 = new Vertex3d(new Point3d(1, 1, 0));
    Vertex3d vt3 = new Vertex3d(new Point3d(0, 1, 0));
    mesh.addVertex(vt0);
    mesh.addVertex(vt1);
    mesh.addVertex(vt2);
    mesh.addVertex(vt3);
    mesh.addFace(new int[] { 0, 1, 3 });
    mesh.addFace(new int[] { 1, 2, 3 });
    mesh.checkConsistency();
}
Also used : Vertex3d(maspack.geometry.Vertex3d)

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