Search in sources :

Example 51 with RigidTransform3d

use of maspack.matrix.RigidTransform3d in project artisynth_core by artisynth.

the class MultiViewerTesterBase method loadStanfordBunny.

protected static PolygonalMesh loadStanfordBunny() {
    // read Standford bunny directly
    String bunnyURL = "http://graphics.stanford.edu/~mdfisher/Data/Meshes/bunny.obj";
    // bunny
    File bunnyFile = new File("tmp/data/stanford_bunny.obj");
    PolygonalMesh bunny = null;
    try {
        if (!bunnyFile.exists()) {
            bunnyFile.getParentFile().mkdirs();
            // read file directly from remote
            FileCacher cacher = new FileCacher();
            cacher.initialize();
            cacher.cache(new URIx(bunnyURL), bunnyFile);
            cacher.release();
        }
        WavefrontReader reader = new WavefrontReader(bunnyFile);
        bunny = new PolygonalMesh();
        reader.readMesh(bunny);
        // bunny.computeVertexNormals();
        // normalize bunny
        double r = bunny.computeRadius();
        Vector3d c = new Vector3d();
        bunny.computeCentroid(c);
        c.negate();
        bunny.scale(1.0 / r);
        c.z -= 0.5;
        bunny.transform(new RigidTransform3d(c, new AxisAngle(1, 0, 0, Math.PI / 2)));
        reader.close();
    } catch (IOException e1) {
        e1.printStackTrace();
        System.out.println("Unable to load stanford bunny... requires internet connection");
        bunny = null;
    }
    return bunny;
}
Also used : WavefrontReader(maspack.geometry.io.WavefrontReader) RigidTransform3d(maspack.matrix.RigidTransform3d) AxisAngle(maspack.matrix.AxisAngle) Vector3d(maspack.matrix.Vector3d) FileCacher(maspack.fileutil.FileCacher) URIx(maspack.fileutil.uri.URIx) IOException(java.io.IOException) File(java.io.File) PolygonalMesh(maspack.geometry.PolygonalMesh)

Example 52 with RigidTransform3d

use of maspack.matrix.RigidTransform3d in project artisynth_core by artisynth.

the class TextTester method addContent.

@Override
protected void addContent(MultiViewer mv) {
    final HashMap<String, Font> fontMap = new HashMap<>();
    for (Font font : GraphicsEnvironment.getLocalGraphicsEnvironment().getAllFonts()) {
        fontMap.put(font.getName(), font);
    }
    mv.addRenderable(new SimpleSelectable() {

        JFrame debugframe = null;

        @Override
        public void updateBounds(Vector3d pmin, Vector3d pmax) {
            Point3d p1 = new Point3d(-3, -3, -3);
            Point3d p2 = new Point3d(3, 3, 3);
            p1.updateBounds(pmin, pmax);
            p2.updateBounds(pmin, pmax);
        }

        @Override
        public void render(Renderer renderer, int flags) {
            if (debugframe == null) {
                TextImageStore store = ((GLViewer) renderer).getTextRenderer().getImageStore();
                debugframe = TextImageStore.createDisplayFrame(store);
                debugframe.setVisible(true);
            }
            renderer.setShading(Shading.FLAT);
            renderer.setColor(Color.WHITE);
            renderer.setFaceStyle(FaceStyle.FRONT_AND_BACK);
            // renderer.drawSphere (Point3d.ZERO, 0.01);
            Font font = new Font(Font.SANS_SERIF, 0, 54);
            renderer.drawText(font, "Hello world! And goodnight to all.", new float[] { 0.4f, 0.3f, 0.3f }, 0.25);
            renderer.setColor(Color.ORANGE);
            String text = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
            renderer.drawText(font, text, new float[] { -2f, 0f, 0f }, 0.25);
            Font comic = fontMap.get("Comic Sans MS");
            if (comic == null) {
                comic = new Font(Font.MONOSPACED, Font.BOLD, 32);
            } else {
                comic = comic.deriveFont(Font.BOLD, 32);
            }
            renderer.setColor(Color.CYAN);
            text = "Cowabunga";
            Rectangle2D rect = renderer.getTextBounds(comic, text, 0.25);
            renderer.drawText(comic, text, new float[] { -(float) (rect.getWidth()), 0.5f, 0f }, 0.25);
            renderer.setColor(Color.MAGENTA);
            text = "Flip me!";
            font = new Font(Font.SERIF, Font.PLAIN, 64);
            rect = renderer.getTextBounds(font, text, 0.3);
            renderer.pushModelMatrix();
            RigidTransform3d trans = new RigidTransform3d(new Vector3d(0, -0.4, 0), new AxisAngle(0, 1, 0, Math.toRadians(30)));
            renderer.mulModelMatrix(trans);
            renderer.setFaceStyle(FaceStyle.FRONT);
            renderer.drawText(font, text, Point3d.ZERO, 0.3);
            trans = new RigidTransform3d(Vector3d.ZERO, new AxisAngle(0, 1, 0, Math.PI));
            renderer.mulModelMatrix(trans);
            renderer.drawText(font, text, new Point3d(-rect.getWidth(), 0, 0), 0.3);
            trans = new RigidTransform3d(Vector3d.ZERO, new AxisAngle(0.1, 1, 0, Math.PI * 1));
            renderer.mulModelMatrix(trans);
            renderer.setShading(Shading.NONE);
            renderer.setFaceStyle(FaceStyle.FRONT_AND_BACK);
            renderer.setColor(Color.GREEN);
            text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sagittis hendrerit consequat. Duis id purus neque. Nulla luctus pretium lorem vel convallis. Praesent non ex eu risus porttitor scelerisque vel et lorem. Maecenas ultricies imperdiet tortor, ac varius enim. Integer sagittis dui lacus. Nulla vitae egestas orci. Phasellus vel metus vitae nibh imperdiet imperdiet ut a urna.";
            drawMultiline(renderer, font, text, new float[] { -1.75f, -0.2f, -0.5f }, 0.1, 2);
            renderer.popModelMatrix();
        }

        public void drawMultiline(Renderer renderer, Font font, String text, float[] loc, double size, double linelength) {
            float left = loc[0];
            float[] nloc = { loc[0], loc[1], loc[2] };
            Rectangle2D rect = renderer.getTextBounds(font, text, size);
            float lineheight = (float) (rect.getHeight());
            String[] words = text.split(" ");
            int w = 0;
            while (w < words.length) {
                String word = words[w];
                nloc[0] += renderer.drawText(font, word, nloc, size);
                ++w;
                while (w < words.length && nloc[0] < left + linelength) {
                    word = " " + words[w];
                    rect = renderer.getTextBounds(font, word, size);
                    double ll = nloc[0] + rect.getWidth();
                    if (ll < left + linelength) {
                        renderer.drawText(font, word, nloc, size);
                        ++w;
                    }
                    nloc[0] = (float) ll;
                }
                // newline
                nloc[0] = left;
                nloc[1] -= lineheight;
            }
        }

        @Override
        public void prerender(RenderList list) {
        }

        @Override
        public int getRenderHints() {
            return 0;
        }

        @Override
        public int numSelectionQueriesNeeded() {
            return 0;
        }

        @Override
        public boolean isSelectable() {
            return false;
        }

        @Override
        public void getSelection(LinkedList<Object> list, int qid) {
        }

        @Override
        public void setSelected(boolean set) {
        }

        @Override
        public boolean isSelected() {
            return false;
        }
    });
    mv.setAxialView(AxisAlignedRotation.X_Y);
}
Also used : RigidTransform3d(maspack.matrix.RigidTransform3d) HashMap(java.util.HashMap) Rectangle2D(java.awt.geom.Rectangle2D) Font(java.awt.Font) RenderList(maspack.render.RenderList) GLViewer(maspack.render.GL.GLViewer) AxisAngle(maspack.matrix.AxisAngle) SimpleSelectable(maspack.test.GL.MultiViewer.SimpleSelectable) JFrame(javax.swing.JFrame) Vector3d(maspack.matrix.Vector3d) Point3d(maspack.matrix.Point3d) Renderer(maspack.render.Renderer) TextImageStore(maspack.render.TextImageStore)

Example 53 with RigidTransform3d

use of maspack.matrix.RigidTransform3d in project artisynth_core by artisynth.

the class TextureEgyptianTest method addContent.

@Override
protected void addContent(MultiViewer mv) {
    JFrame frame = new JFrame();
    JPanel controls = new JPanel();
    frame.add(controls);
    PolygonalMesh plane = MeshFactory.createRectangle(4000, 400, 32, 8, /*texture=*/
    true);
    RenderProps rprops = plane.getRenderProps();
    if (rprops == null) {
        rprops = new RenderProps();
    }
    rprops.setShading(Shading.SMOOTH);
    rprops.setShininess(20);
    rprops.setFaceColor(new Color(155, 196, 30));
    rprops.setSpecular(new Color(255, 113, 0));
    rprops.setFaceStyle(FaceStyle.FRONT_AND_BACK);
    plane.transform(new RigidTransform3d(Vector3d.ZERO, AxisAngle.ROT_X_90));
    String srcDir = PathFinder.findSourceDir(this);
    ColorMapProps dprops = new ColorMapProps();
    dprops.setFileName(srcDir + "/data/specular_map.jpg");
    dprops.setColorMixing(ColorMixing.MODULATE);
    dprops.setEnabled(true);
    NormalMapProps normalProps = new NormalMapProps();
    normalProps.setFileName(srcDir + "/data/foil_normal_map2.png");
    normalProps.setScaling(0.3f);
    normalProps.setEnabled(true);
    BumpMapProps bumpProps = new BumpMapProps();
    bumpProps.setFileName(srcDir + "/data/egyptian_friz_2.png");
    bumpProps.setScaling(2.5f);
    bumpProps.setEnabled(true);
    rprops.setColorMap(dprops);
    rprops.setNormalMap(normalProps);
    rprops.setBumpMap(bumpProps);
    // FixedMeshBody fm = new FixedMeshBody(plane);
    // fm.setRenderProps(rprops);
    mv.addRenderable(plane);
    if (false) {
        mv.addRenderable(new IsRenderable() {

            @Override
            public void updateBounds(Vector3d pmin, Vector3d pmax) {
                Point3d.X_UNIT.updateBounds(pmin, pmax);
                Point3d.Y_UNIT.updateBounds(pmin, pmax);
                Point3d.Z_UNIT.updateBounds(pmin, pmax);
                Point3d.NEG_X_UNIT.updateBounds(pmin, pmax);
                Point3d.NEG_Y_UNIT.updateBounds(pmin, pmax);
                Point3d.NEG_Z_UNIT.updateBounds(pmin, pmax);
            }

            @Override
            public void render(Renderer renderer, int flags) {
                renderer.setColor(Color.CYAN);
                renderer.setFaceStyle(FaceStyle.FRONT_AND_BACK);
                renderer.drawSphere(Point3d.ZERO, 1);
            }

            @Override
            public void prerender(RenderList list) {
            // TODO Auto-generated method stub
            }

            @Override
            public int getRenderHints() {
                // TODO Auto-generated method stub
                return 0;
            }
        });
    }
    mv.autoFitViewers();
    LabeledComponentBase base = PropertyWidget.create("Color texture", rprops.getColorMap(), "enabled");
    controls.add(base);
    base = PropertyWidget.create("Normal map", rprops.getNormalMap(), "enabled");
    controls.add(base);
    base = PropertyWidget.create("Bump map", rprops.getBumpMap(), "enabled");
    controls.add(base);
    base = PropertyWidget.create("Specular", rprops.getColorMap(), "specularColoring");
    controls.add(base);
    base = PropertyWidget.create("Bump map scale", rprops.getBumpMap(), "scaling");
    controls.add(base);
    base = PropertyWidget.create("Normal map scale", rprops.getNormalMap(), "scaling");
    controls.add(base);
    frame.pack();
    frame.setVisible(true);
}
Also used : JPanel(javax.swing.JPanel) RigidTransform3d(maspack.matrix.RigidTransform3d) Color(java.awt.Color) ColorMapProps(maspack.render.ColorMapProps) RenderProps(maspack.render.RenderProps) PolygonalMesh(maspack.geometry.PolygonalMesh) RenderList(maspack.render.RenderList) BumpMapProps(maspack.render.BumpMapProps) IsRenderable(maspack.render.IsRenderable) JFrame(javax.swing.JFrame) Vector3d(maspack.matrix.Vector3d) Renderer(maspack.render.Renderer) LabeledComponentBase(maspack.widgets.LabeledComponentBase) NormalMapProps(maspack.render.NormalMapProps)

Example 54 with RigidTransform3d

use of maspack.matrix.RigidTransform3d in project artisynth_core by artisynth.

the class RigidTransformWidget method updateInternalValue.

/**
 * Updates the internal representation of the value, updates any result
 * holders, and returns true if the new value differs from the old value.
 */
protected boolean updateInternalValue(Object value) {
    if (!valuesEqual(value, myValue)) {
        myTranslationField.maskValueChangeListeners(true);
        myRotationField.maskValueChangeListeners(true);
        if (value instanceof RigidTransform3d) {
            RigidTransform3d newX = (RigidTransform3d) value;
            if (myTranslationField.valueIsVoid() || !newX.p.equals(getTranslation())) {
                myTranslationField.setValue(newX.p);
            }
            RotationMatrix3d R = new RotationMatrix3d();
            if (!myRotationField.valueIsVoid()) {
                R.setAxisAngle(getRotation());
            }
            if (myRotationField.valueIsVoid() || !newX.R.equals(R)) {
                AxisAngle newAxisAngle = new AxisAngle();
                newX.R.getAxisAngle(newAxisAngle);
                myRotationField.setValue(newAxisAngle);
            }
            value = new RigidTransform3d(newX);
        } else if (value == Property.VoidValue) {
            myTranslationField.setValue(Property.VoidValue);
            myRotationField.setValue(Property.VoidValue);
        }
        myTranslationField.maskValueChangeListeners(false);
        myRotationField.maskValueChangeListeners(false);
        myValue = value;
        return true;
    } else {
        return false;
    }
}
Also used : RigidTransform3d(maspack.matrix.RigidTransform3d) AxisAngle(maspack.matrix.AxisAngle) RotationMatrix3d(maspack.matrix.RotationMatrix3d)

Example 55 with RigidTransform3d

use of maspack.matrix.RigidTransform3d in project artisynth_core by artisynth.

the class RigidTransformWidget method initialize.

private void initialize(String fmtStr) {
    myTranslationField = new VectorField("Pos:", 3);
    myTranslationField.setVoidValueEnabled(true);
    myRotationField = new AxisAngleField("Rot:");
    myRotationField.setVoidValueEnabled(true);
    myPanel = new LabeledComponentPanel();
    myTranslationField.addValueChangeListener(new ValueChangeListener() {

        public void valueChange(ValueChangeEvent e) {
            if (!myRotationField.valueIsVoid()) {
                RigidTransform3d X = new RigidTransform3d();
                X.p.set(getTranslation());
                X.R.setAxisAngle(getRotation());
                updateValue(X);
            }
        }
    });
    myRotationField.addValueChangeListener(new ValueChangeListener() {

        public void valueChange(ValueChangeEvent e) {
            if (!myTranslationField.valueIsVoid()) {
                RigidTransform3d X = new RigidTransform3d();
                X.p.set(getTranslation());
                X.R.setAxisAngle(getRotation());
                updateValue(X);
            }
        }
    });
    myPanel.addWidget(myTranslationField);
    myPanel.addWidget(myRotationField);
    if (myTranslationField.getColumns() > myRotationField.getColumns()) {
        myRotationField.setColumns(myTranslationField.getColumns());
    } else {
        myTranslationField.setColumns(myRotationField.getColumns());
    }
    myTranslationField.setFormat(fmtStr);
    addMajorComponent(myPanel);
}
Also used : RigidTransform3d(maspack.matrix.RigidTransform3d)

Aggregations

RigidTransform3d (maspack.matrix.RigidTransform3d)206 Vector3d (maspack.matrix.Vector3d)56 Point3d (maspack.matrix.Point3d)48 PolygonalMesh (maspack.geometry.PolygonalMesh)21 MechModel (artisynth.core.mechmodels.MechModel)19 RigidBody (artisynth.core.mechmodels.RigidBody)18 AxisAngle (maspack.matrix.AxisAngle)18 RotationMatrix3d (maspack.matrix.RotationMatrix3d)17 AffineTransform3d (maspack.matrix.AffineTransform3d)13 FemModel3d (artisynth.core.femmodels.FemModel3d)11 RenderProps (maspack.render.RenderProps)11 FemNode3d (artisynth.core.femmodels.FemNode3d)9 Color (java.awt.Color)7 Point (java.awt.Point)7 Matrix3d (maspack.matrix.Matrix3d)7 Shading (maspack.render.Renderer.Shading)7 LinearMaterial (artisynth.core.materials.LinearMaterial)6 Renderable (maspack.render.Renderable)6 ArrayList (java.util.ArrayList)5 Vector2d (maspack.matrix.Vector2d)5