Search in sources :

Example 51 with RenderProps

use of maspack.render.RenderProps in project artisynth_core by artisynth.

the class FemMuscleHeart method build.

// Model builder
@Override
public void build(String[] args) throws IOException {
    super.build(args);
    setMaxStepSize(0.005);
    // Root mechanical model
    MechModel mech = new MechModel("mech");
    mech.setGravity(0, 0, -9.8);
    addModel(mech);
    // -------------------------------------------------------------
    // HEART LOAD / ADD GEOMETRY
    // -------------------------------------------------------------
    // Heart surface mesh, with texture
    String heartFile = ArtisynthPath.getSrcRelativePath(this, "data/HumanHeart.obj");
    WavefrontReader wfr = new WavefrontReader(new File(heartFile));
    PolygonalMesh heartMesh = new PolygonalMesh();
    wfr.readMesh(heartMesh);
    // triangulate for interaction
    heartMesh.triangulate();
    // FEM heart:
    // - FEM mesh of heart convex hull
    // - embedded heart surface geometry
    FemMuscleModel heart = new FemMuscleModel("heart");
    TetGenReader.read(heart, ArtisynthPath.getSrcRelativePath(this, "data/HumanHeartHull.node"), ArtisynthPath.getSrcRelativePath(this, "data/HumanHeartHull.ele"));
    // add real-looking mesh
    FemMeshComp embeddedHeart = heart.addMesh(heartMesh);
    embeddedHeart.setName("embedded");
    // Allow inverted elements (poor quality mesh)
    heart.setWarnOnInvertedElements(false);
    heart.setAbortOnInvertedElements(false);
    // Convert unites to metres (original was cm)
    heart.scaleDistance(0.01);
    heart.setGravity(0, 0, -9.8);
    heart.setStiffnessDamping(0.02);
    // Set material properties
    heart.setDensity(1000);
    FemMaterial femMat = new LinearMaterial(2500, 0.33, true);
    // simple muscle
    MuscleMaterial muscleMat = new SimpleForceMuscle(500.0);
    heart.setMaterial(femMat);
    // Add heart to model
    mech.addModel(heart);
    // -------------------------------------------------------------
    // MUSCLE BUNDLES
    // -------------------------------------------------------------
    // One "long" direction muscle bundle
    // One "radial" muscle bundle
    // LONG BUNDLE
    // Compute the "long" direction of the heart
    PolygonalMesh hull = heart.getSurfaceMesh();
    RigidTransform3d trans = hull.computePrincipalAxes();
    Vector3d longAxis = new Vector3d();
    // first column of rotation
    trans.R.getColumn(0, longAxis);
    // Create the long axis muscle bundle
    MuscleBundle longBundle = new MuscleBundle("long");
    for (FemElement3d elem : heart.getElements()) {
        longBundle.addElement(elem, longAxis);
    }
    longBundle.setMuscleMaterial(muscleMat);
    heart.addMuscleBundle(longBundle);
    // RADIAL BUNDLE
    // Compute a plane through centre of heart
    Plane plane = new Plane(longAxis, new Point3d(trans.p));
    Point3d centroid = new Point3d();
    Vector3d radialDir = new Vector3d();
    // Create the radial muscle bundle
    MuscleBundle radialBundle = new MuscleBundle("radial");
    for (FemElement3d elem : heart.getElements()) {
        elem.computeCentroid(centroid);
        // project to plane and compute radial direction
        plane.project(centroid, centroid);
        radialDir.sub(centroid, trans.p);
        radialDir.normalize();
        radialBundle.addElement(elem, radialDir);
    }
    radialBundle.setMuscleMaterial(muscleMat);
    heart.addMuscleBundle(radialBundle);
    // -------------------------------------------------------------
    // RIGID TABLE AND COLLISION
    // -------------------------------------------------------------
    // Create a rigid box for the heart to fall on
    RigidBody box = RigidBody.createBox("box", 0.2, 0.2, 0.02, 0, /*addnormals*/
    true);
    box.setPose(new RigidTransform3d(new Vector3d(0, 0, -0.2), AxisAngle.IDENTITY));
    box.setDynamic(false);
    mech.addRigidBody(box);
    // Enable collisions between the heart and table
    mech.setCollisionBehavior(heart, box, true);
    // -------------------------------------------------------------
    // RENDER PROPERTIES
    // -------------------------------------------------------------
    // Hide elements and nodes
    RenderProps.setVisible(heart.getElements(), false);
    RenderProps.setVisible(heart.getNodes(), false);
    RenderProps.setLineColor(radialBundle, Color.BLUE);
    RenderProps.setLineColor(longBundle, Color.RED);
    radialBundle.setDirectionRenderLen(0.1);
    longBundle.setDirectionRenderLen(0.1);
    RenderProps.setVisible(radialBundle, false);
    RenderProps.setVisible(longBundle, false);
    RenderProps.setVisible(heart.getSurfaceMeshComp(), false);
    // adjust table render properties
    RenderProps.setShading(box, Shading.METAL);
    RenderProps.setSpecular(box, new Color(0.8f, 0.8f, 0.8f));
    // adjust heart mesh render properties
    RenderProps rprops = embeddedHeart.getRenderProps();
    rprops.getBumpMap().setScaling(0.01f);
    // don't modify specular
    rprops.getColorMap().setSpecularColoring(false);
    rprops.setShading(Shading.SMOOTH);
    rprops.setFaceColor(new Color(0.8f, 0.8f, 0.8f));
    rprops.getColorMap().setColorMixing(ColorMixing.MODULATE);
    rprops.setSpecular(new Color(0.4f, 0.4f, 0.4f));
    rprops.setShininess(128);
    // -------------------------------------------------------------
    // INPUT PROBES
    // -------------------------------------------------------------
    // Add heart probe
    addHeartProbe(longBundle, radialBundle);
}
Also used : WavefrontReader(maspack.geometry.io.WavefrontReader) SimpleForceMuscle(artisynth.core.materials.SimpleForceMuscle) RigidTransform3d(maspack.matrix.RigidTransform3d) FemElement3d(artisynth.core.femmodels.FemElement3d) Plane(maspack.matrix.Plane) FemMeshComp(artisynth.core.femmodels.FemMeshComp) FemMaterial(artisynth.core.materials.FemMaterial) MuscleMaterial(artisynth.core.materials.MuscleMaterial) Color(java.awt.Color) RenderProps(maspack.render.RenderProps) LinearMaterial(artisynth.core.materials.LinearMaterial) PolygonalMesh(maspack.geometry.PolygonalMesh) MechModel(artisynth.core.mechmodels.MechModel) MuscleBundle(artisynth.core.femmodels.MuscleBundle) Vector3d(maspack.matrix.Vector3d) Point3d(maspack.matrix.Point3d) FemMuscleModel(artisynth.core.femmodels.FemMuscleModel) RigidBody(artisynth.core.mechmodels.RigidBody) File(java.io.File)

Example 52 with RenderProps

use of maspack.render.RenderProps in project artisynth_core by artisynth.

the class IntersectionTester method createRenderProps.

@Override
public RenderProps createRenderProps() {
    RenderProps props = super.createRenderProps();
    props.setFaceStyle(FaceStyle.FRONT);
    return props;
}
Also used : RenderProps(maspack.render.RenderProps)

Example 53 with RenderProps

use of maspack.render.RenderProps in project artisynth_core by artisynth.

the class MeshDemo method main.

public static void main(String[] args) {
    // create a simple box
    double wx = 9;
    double wy = 4;
    double wz = 1;
    PolygonalMesh myMesh = new PolygonalMesh();
    myMesh.addVertex(new Point3d(wx / 2, wy / 2, wz / 2));
    myMesh.addVertex(new Point3d(wx / 2, wy / 2, -wz / 2));
    myMesh.addVertex(new Point3d(-wx / 2, wy / 2, -wz / 2));
    myMesh.addVertex(new Point3d(-wx / 2, wy / 2, wz / 2));
    myMesh.addVertex(new Point3d(wx / 2, -wy / 2, wz / 2));
    myMesh.addVertex(new Point3d(wx / 2, -wy / 2, -wz / 2));
    myMesh.addVertex(new Point3d(-wx / 2, -wy / 2, -wz / 2));
    myMesh.addVertex(new Point3d(-wx / 2, -wy / 2, wz / 2));
    myMesh.addFace(new int[] { 0, 1, 2, 3 });
    myMesh.addFace(new int[] { 0, 3, 7, 4 });
    myMesh.addFace(new int[] { 1, 0, 4, 5 });
    myMesh.addFace(new int[] { 2, 1, 5, 6 });
    myMesh.addFace(new int[] { 3, 2, 6, 7 });
    myMesh.addFace(new int[] { 4, 7, 6, 5 });
    RenderProps props = myMesh.createRenderProps();
    props.setDrawEdges(true);
    // gold
    props.setFaceColor(new Color(0.93f, 0.8f, 0.063f));
    myMesh.setRenderProps(props);
    GLViewerFrame frame = new GLViewerFrame("MeshDemo", 400, 400);
    frame.getViewer().addRenderable(myMesh);
    frame.getViewer().autoFitPerspective();
    frame.setVisible(true);
}
Also used : GLViewerFrame(maspack.render.GL.GLViewerFrame) Color(java.awt.Color) RenderProps(maspack.render.RenderProps)

Example 54 with RenderProps

use of maspack.render.RenderProps in project artisynth_core by artisynth.

the class NURBSCurveBase method createRenderProps.

/**
 * {@inheritDoc}
 */
public RenderProps createRenderProps() {
    RenderProps props = new PointEdgeRenderProps();
    props.setDrawEdges(true);
    props.setPointSize(3);
    props.setLineWidth(2);
    props.setLineColor(Color.WHITE);
    props.setEdgeColor(Color.BLUE);
    props.setPointColor(Color.GREEN);
    return props;
}
Also used : PointEdgeRenderProps(maspack.render.PointEdgeRenderProps) RenderProps(maspack.render.RenderProps) PointEdgeRenderProps(maspack.render.PointEdgeRenderProps)

Example 55 with RenderProps

use of maspack.render.RenderProps 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)

Aggregations

RenderProps (maspack.render.RenderProps)86 Color (java.awt.Color)17 Vector3d (maspack.matrix.Vector3d)17 RigidBody (artisynth.core.mechmodels.RigidBody)11 RigidTransform3d (maspack.matrix.RigidTransform3d)11 Shading (maspack.render.Renderer.Shading)10 PolygonalMesh (maspack.geometry.PolygonalMesh)8 Point3d (maspack.matrix.Point3d)8 RenderObject (maspack.render.RenderObject)7 FrameMarker (artisynth.core.mechmodels.FrameMarker)6 File (java.io.File)6 IOException (java.io.IOException)6 Vertex3d (maspack.geometry.Vertex3d)6 AffineTransform3d (maspack.matrix.AffineTransform3d)4 ColorMapProps (maspack.render.ColorMapProps)4 HasRenderProps (maspack.render.HasRenderProps)4 LineRenderProps (maspack.render.LineRenderProps)4 AxialSpring (artisynth.core.mechmodels.AxialSpring)3 Muscle (artisynth.core.mechmodels.Muscle)3 LinkedList (java.util.LinkedList)3