Search in sources :

Example 81 with PolygonalMesh

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

the class FemModel3dAgent method createPreviewModel.

private void createPreviewModel() {
    fem = new FemModel3d();
    setProperties(fem, getPrototypeComponent(myComponentType));
    setProperties(myPrototype, myPrototype);
    FemElementType elemType = null;
    FemMeshType meshType = (FemMeshType) meshSelector.getValue();
    if (elemSelector.isEnabledAll()) {
        elemType = (FemElementType) elemSelector.getValue();
    }
    switch(meshType) {
        case Grid:
            {
                VectorBase dims = gridDimField.getVectorValue();
                int[] divs = gridDivField.getVectorValue();
                FemFactory.createGrid(fem, elemType, dims.get(0), dims.get(1), dims.get(2), divs[0], divs[1], divs[2]);
                break;
            }
        case Tube:
            {
                VectorBase dims = tubeDimField.getVectorValue();
                int[] divs = tubeDivField.getVectorValue();
                FemFactory.createTube(fem, elemType, dims.get(0), dims.get(1), dims.get(2), divs[0], divs[1], divs[2]);
                break;
            }
        case Torus:
            {
                VectorBase dims = torusDimField.getVectorValue();
                int[] divs = torusDivField.getVectorValue();
                FemFactory.createTorus(fem, elemType, dims.get(0), dims.get(1), dims.get(2), divs[0], divs[1], divs[2]);
                break;
            }
        case Sphere:
            {
                int nodes = (Integer) sphereNodesField.getValue();
                String meshPath;
                if (nodes == SPHERE_NODE_OPTIONS[0]) {
                    meshPath = ArtisynthPath.getHomeRelativePath(SPHERE_54_MESH_PATH, ".");
                } else if (nodes == SPHERE_NODE_OPTIONS[1]) {
                    meshPath = ArtisynthPath.getHomeRelativePath(SPHERE_196_MESH_PATH, ".");
                } else {
                    EditorUtils.showError(myDisplay, "Invalid number of nodes for sphere");
                    return;
                }
                try {
                    TetGenReader.read(fem, 1000, meshPath + ".node", meshPath + ".ele", new Vector3d(1, 1, 1));
                } catch (Exception e) {
                    EditorUtils.showError(myDisplay, "Error reading file: " + e.getMessage());
                    return;
                }
                break;
            }
        case Extrusion:
            {
                double d = extrusDepthField.getDoubleValue();
                int n = extrusLayersField.getIntValue();
                String meshFileName = extrusFileField.getStringValue();
                try {
                    PolygonalMesh mesh = new PolygonalMesh(new File(meshFileName));
                    FemFactory.createExtrusion(fem, elemType, n, d, 0, mesh);
                } catch (Exception e) {
                    EditorUtils.showError(myDisplay, "Error reading file: " + e.getMessage());
                    return;
                }
                break;
            }
        case AnsysMesh:
            {
                String nodeFileName = ansysNodeFileField.getStringValue();
                String elemFileName = ansysElemFileField.getStringValue();
                try {
                    AnsysReader.read(fem, nodeFileName, elemFileName, 1000, null, /*options=*/
                    0);
                } catch (Exception e) {
                    EditorUtils.showError(myDisplay, "Error reading file: " + e.getMessage());
                    return;
                }
                break;
            }
        case TetgenMesh:
            {
                String nodeFileName = tetgenNodeFileField.getStringValue();
                String eleFileName = tetgenEleFileField.getStringValue();
                try {
                    TetGenReader.read(fem, 1000, nodeFileName, eleFileName, new Vector3d(1, 1, 1));
                } catch (Exception e) {
                    EditorUtils.showError(myDisplay, "Error reading file: " + e.getMessage());
                    return;
                }
                break;
            }
        case UCDMesh:
            {
                String ucdFileName = ucdMeshFileField.getStringValue();
                try {
                    UCDReader.read(fem, ucdFileName, 1000);
                } catch (Exception e) {
                    EditorUtils.showError(myDisplay, "Error reading file: " + e.getMessage());
                    return;
                }
                break;
            }
        case SurfaceMesh:
            {
                String objFileName = surfaceMeshFileField.getStringValue();
                PolygonalMesh surfaceMesh = null;
                try {
                    surfaceMesh = new PolygonalMesh(new File(objFileName));
                } catch (Exception e) {
                    EditorUtils.showError(myDisplay, "Error reading file: " + e.getMessage());
                    return;
                }
                try {
                    FemFactory.createFromMesh(fem, surfaceMesh, /*quality=*/
                    2.0);
                } catch (Exception e) {
                    e.printStackTrace();
                    EditorUtils.showError(myDisplay, "Error tessellating mesh: " + e.getMessage());
                    return;
                }
                break;
            }
        default:
            {
                throw new InternalErrorException("Unimplemented mesh type");
            }
    }
    RigidTransform3d X = new RigidTransform3d();
    X.p.set(positionField.getVectorValue());
    X.R.setAxisAngle(orientationField.getAxisAngleValue());
    PolygonalMesh mesh = fem.getSurfaceMesh();
    RenderProps props = mesh.createRenderProps();
    props.setFaceStyle(Renderer.FaceStyle.NONE);
    props.setDrawEdges(true);
    props.setLineColor(Color.LIGHT_GRAY);
    mesh.setRenderProps(props);
    mesh.setMeshToWorld(X);
    mesh.setFixed(false);
    mesh.setRenderBuffered(false);
    if (meshPropPanel.getComponentIndex(scaleField) != -1) {
        scaleField.maskValueChangeListeners(true);
        scaleField.setValue(1.0);
        scaleField.maskValueChangeListeners(false);
        lastScale = 1.0;
    }
    myMain.getWorkspace().getViewerManager().addRenderable(mesh);
    rotator = new Transrotator3d();
    GLViewer viewer = myMain.getMain().getViewer();
    rotator.setDraggerToWorld(X);
    rotator.setSize(viewer.distancePerPixel(viewer.getCenter()) * viewer.getScreenWidth() / 6);
    rotator.addListener(new FemModelDraggerListener());
    myMain.getWorkspace().getViewerManager().addDragger(rotator);
    myMain.rerender();
}
Also used : RigidTransform3d(maspack.matrix.RigidTransform3d) FemModel3d(artisynth.core.femmodels.FemModel3d) FemElementType(artisynth.core.femmodels.FemFactory.FemElementType) RenderProps(maspack.render.RenderProps) Transrotator3d(maspack.render.Transrotator3d) InternalErrorException(maspack.util.InternalErrorException) PolygonalMesh(maspack.geometry.PolygonalMesh) InternalErrorException(maspack.util.InternalErrorException) GLViewer(maspack.render.GL.GLViewer) Vector3d(maspack.matrix.Vector3d) VectorBase(maspack.matrix.VectorBase) File(java.io.File)

Example 82 with PolygonalMesh

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

the class FemDisplayProbe method updateVertexIndicators.

/**
 *  determines which vertices are inside the supplied mesh
 */
protected void updateVertexIndicators() {
    vtxIndicatorMap = new HashMap<Vertex3d, Boolean>(myPlaneSurface.numVertices());
    TriangleIntersector ti = new TriangleIntersector();
    BVFeatureQuery query = new BVFeatureQuery();
    ti.setEpsilon(myIntersectionTolerance);
    if (myFem != null) {
        PolygonalMesh mesh = myFem.getSurfaceMesh();
        for (Vertex3d vtx : myPlaneSurface.getVertices()) {
            Point3d pnt = vtx.getWorldPoint();
            if (query.isInsideOrientedMesh(mesh, pnt, myIntersectionTolerance)) {
                vtxIndicatorMap.put(vtx, true);
            } else {
                vtxIndicatorMap.put(vtx, false);
            }
        }
    }
}
Also used : Vertex3d(maspack.geometry.Vertex3d) Point3d(maspack.matrix.Point3d) TriangleIntersector(maspack.geometry.TriangleIntersector) BVFeatureQuery(maspack.geometry.BVFeatureQuery) PolygonalMesh(maspack.geometry.PolygonalMesh)

Example 83 with PolygonalMesh

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

the class MDLMeshIO method read.

/**
 * Creates a PolygonalMesh based on MDL data read from a Reader. The node
 * coordinate data can be scaled non-uniformly using an optional parameter
 * giving scale values about the x, y, and z axes.
 *
 * @param reader
 * the Reader which references MDL data to be read
 * @param scale
 * if non-null, gives scaling about the x, y, and z axes
 * @return created polygonal mesh
 * @throws IOException
 * if this is a problem reading the file
 */
public static PolygonalMesh read(Reader reader, Vector3d scale) throws IOException {
    PolygonalMesh mesh = new PolygonalMesh();
    ReaderTokenizer rtok = new ReaderTokenizer(new BufferedReader(reader));
    rtok.wordChars("<>.:/\\");
    // read top header
    MDLHeader header = MDLHeader.scan(rtok);
    if (!header.equals(defaultTopHeader)) {
    // throw new IOException ("MDLReader: bad top header, " + header);
    }
    // read file name
    if (rtok.nextToken() != ReaderTokenizer.TT_WORD) {
        throw new IOException("MDLReader: expecting filename, got " + rtok.tokenName());
    }
    // read vertices header information
    header = MDLHeader.scan(rtok);
    if (!header.equals(defaultVerticesHeader)) {
        // throw new IOException ("MDLReader: bad vertices header, " + header);
        System.err.println("MDLReader: bad vertices header, no vertices read...");
        return mesh;
    }
    // read vertices
    int numVertices = rtok.scanInteger();
    Point3d coords = new Point3d();
    for (int i = 0; i < numVertices; i++) {
        coords.x = rtok.scanNumber();
        coords.y = rtok.scanNumber();
        coords.z = rtok.scanNumber();
        if (scale != null) {
            coords.x *= scale.x;
            coords.y *= scale.y;
            coords.z *= scale.z;
        }
        mesh.addVertex(coords);
    }
    // read normals header information
    try {
        header = MDLHeader.scan(rtok);
    } catch (IOException e) {
        System.out.println("MDLReader: no normals read...");
        return mesh;
    }
    if (!header.equals(defaultNormalsHeader)) {
        // throw new IOException ("MDLReader: bad normals header, " + header);
        System.out.println("MDLReader: no normals read...");
        return mesh;
    }
    // read vertices
    int numNormals = rtok.scanInteger();
    ArrayList<Vector3d> vn = new ArrayList<Vector3d>(numNormals);
    for (int i = 0; i < numNormals; i++) {
        Vector3d vec = new Vector3d();
        vec.x = rtok.scanNumber();
        vec.y = rtok.scanNumber();
        vec.z = rtok.scanNumber();
        if (scale != null) {
            vec.x *= scale.x;
            vec.y *= scale.y;
            vec.z *= scale.z;
        }
        vn.add(vec);
    }
    // read faces header information
    try {
        header = MDLHeader.scan(rtok);
    } catch (IOException e) {
        System.out.println("MDLReader: no faces read...");
        return mesh;
    }
    if (!header.equals(defaultFacesHeader)) {
        // throw new IOException ("MDLReader: bad faces header, " + header);
        System.out.println("MDLReader: no faces read...");
        return mesh;
    }
    // read vertices
    int numFaces = rtok.scanInteger();
    int[] vi = new int[3];
    boolean faceIdxWarningGiven = false;
    int[] normalIdxs = new int[numFaces * 3];
    int k = 0;
    for (int i = 0; i < numFaces; i++) {
        int[] ni = new int[3];
        vi[0] = rtok.scanInteger();
        vi[1] = rtok.scanInteger();
        vi[2] = rtok.scanInteger();
        ni[0] = rtok.scanInteger();
        ni[1] = rtok.scanInteger();
        ni[2] = rtok.scanInteger();
        if (!faceIdxWarningGiven && (vi[0] != ni[0] || vi[0] != ni[0] || vi[0] != ni[0])) {
            System.out.println("Warning: MDL face idxs don't match normal idxs; ignoring");
            faceIdxWarningGiven = true;
        }
        mesh.addFace(vi);
        normalIdxs[k++] = ni[0];
        normalIdxs[k++] = ni[1];
        normalIdxs[k++] = ni[2];
    }
    mesh.setNormals(vn, normalIdxs);
    return mesh;
}
Also used : Vector3d(maspack.matrix.Vector3d) Point3d(maspack.matrix.Point3d) ReaderTokenizer(maspack.util.ReaderTokenizer) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) IOException(java.io.IOException) PolygonalMesh(maspack.geometry.PolygonalMesh)

Example 84 with PolygonalMesh

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

the class MDLMeshIO method read.

/**
 * Creates a PolygonalMesh based on MDL data contained in a specified file.
 * The node coordinate data can be scaled non-uniformly using an optional
 * parameter giving scale values about the x, y, and z axes.
 *
 * @param fileName
 * path name of the MDL file
 * @param scale
 * if non-null, gives scaling about the x, y, and z axes
 * @return created polygonal mesh
 * @throws IOException
 * if this is a problem reading the file
 */
public static PolygonalMesh read(String fileName, Vector3d scale) throws IOException {
    Reader reader = new FileReader(fileName);
    PolygonalMesh mesh = read(reader, scale);
    reader.close();
    return mesh;
}
Also used : Reader(java.io.Reader) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) FileReader(java.io.FileReader) PolygonalMesh(maspack.geometry.PolygonalMesh)

Example 85 with PolygonalMesh

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

the class ObjToMdl method main.

public static void main(String[] args) {
    if (args.length != 2) {
        System.out.println("usage: java artisynth.models.badin.ObjToMdl inputObjMeshName outputMdlMeshName");
        return;
    }
    try {
        PolygonalMesh mesh = new PolygonalMesh(new File(args[0]));
        PrintWriter pw = new PrintWriter(new File(args[1]));
        MDLMeshIO.write(mesh, args[1], pw, new NumberFormat("%g"));
        pw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : IOException(java.io.IOException) PolygonalMesh(maspack.geometry.PolygonalMesh) File(java.io.File) PrintWriter(java.io.PrintWriter) NumberFormat(maspack.util.NumberFormat)

Aggregations

PolygonalMesh (maspack.geometry.PolygonalMesh)128 Point3d (maspack.matrix.Point3d)30 Vertex3d (maspack.geometry.Vertex3d)24 Vector3d (maspack.matrix.Vector3d)23 RigidTransform3d (maspack.matrix.RigidTransform3d)21 IOException (java.io.IOException)18 ArrayList (java.util.ArrayList)18 File (java.io.File)14 Face (maspack.geometry.Face)14 ContactPoint (artisynth.core.mechmodels.ContactPoint)11 Point (artisynth.core.mechmodels.Point)11 PointParticleAttachment (artisynth.core.mechmodels.PointParticleAttachment)10 Color (java.awt.Color)10 MeshBase (maspack.geometry.MeshBase)10 RigidBody (artisynth.core.mechmodels.RigidBody)9 MechModel (artisynth.core.mechmodels.MechModel)8 BufferedReader (java.io.BufferedReader)8 AxisAngle (maspack.matrix.AxisAngle)8 RenderProps (maspack.render.RenderProps)8 HashMap (java.util.HashMap)7