Example usage for javax.media.j3d Transform3D transform

List of usage examples for javax.media.j3d Transform3D transform

Introduction

In this page you can find the example usage for javax.media.j3d Transform3D transform.

Prototype

public final void transform(Vector3f normal) 

Source Link

Document

Transforms the normal parameter by this transform and places the value back into normal.

Usage

From source file:Text2DTest.java

/**
 * Process a keyboard event to move or rotate the viewer.
 *//*w  ww  .  j a va  2s .c  o  m*/
void processManualEvent(AWTEvent[] events) {

    for (int i = 0; i < events.length; ++i) {
        if (events[i] instanceof KeyEvent) {
            KeyEvent event = (KeyEvent) events[i];
            if (event.getKeyCode() == KeyEvent.VK_EQUALS) {
                continue;
            }
            Transform3D t = new Transform3D();
            viewTransformGroup.getTransform(t);
            Vector3f viewDir = new Vector3f(0f, 0f, -1f);
            Vector3f translation = new Vector3f();
            t.get(translation);
            t.transform(viewDir);
            if (event.getKeyCode() == KeyEvent.VK_UP) {
                translation.x += viewDir.x;
                translation.y += viewDir.y;
                translation.z += viewDir.z;
            } else if (event.getKeyCode() == KeyEvent.VK_DOWN) {
                translation.x -= viewDir.x;
                translation.y -= viewDir.y;
                translation.z -= viewDir.z;
            } else if (event.getKeyCode() == KeyEvent.VK_RIGHT) {
                rotation += -.1;
            } else if (event.getKeyCode() == KeyEvent.VK_LEFT) {
                rotation += .1;
            }
            t.rotY(rotation);
            t.setTranslation(translation);
            viewTransformGroup.setTransform(t);
        }
    }
}

From source file:ffx.ui.MainPanel.java

/**
 * Merge two or more selected FSystem Nodes into one FSystem node. There are
 * a few gotchas that need to be fixed//from   w ww.  j av a  2  s. c om
 *
 * @param nodesToMerge a {@link java.util.ArrayList} object.
 */
public void merge(ArrayList<MSNode> nodesToMerge) {
    ArrayList<MSNode> activeNodes = new ArrayList<MSNode>();
    for (MSNode node : nodesToMerge) {
        if (node != null && !(node instanceof MSRoot)) {
            activeNodes.add(node);
        }
    }
    if (activeNodes.size() <= 1) {
        return;
    }
    // Set up a structure to hold the new system
    FFXSystem active = hierarchy.getActive();
    File file = SystemFilter.version(hierarchy.getActive().getFile());
    FFXSystem system = new FFXSystem(file, "Merge Result", active.getProperties());
    system.setKeyFile(active.getKeyFile());
    system.setKeywords(KeyFilter.open(active.getKeyFile()));
    // Fill arrays with the atoms and bonds from the systems to be combined
    ArrayList<Atom> mergedAtoms = new ArrayList<Atom>();
    ArrayList<Bond> mergedBonds = new ArrayList<Bond>();
    ArrayList<FFXSystem> systems = new ArrayList<FFXSystem>();
    TransformGroup parentTransformGroup = null;
    FFXSystem parentSystem;
    Transform3D parentTransform3D = new Transform3D();
    Vector3d parentPosition = new Vector3d();
    Vector3d atomPosition = new Vector3d();
    // TINKER Atom Numbers start at 1
    int atomNum = 1;
    Vector3d zero = new Vector3d(0.0, 0.0, 0.0);
    for (MSNode m : activeNodes) {
        parentSystem = (FFXSystem) m.getMSNode(FFXSystem.class);
        if (parentSystem == null) {
            return;
        }
        if (!systems.contains(parentSystem)) {
            graphicsCanvas.updateSceneWait(parentSystem, false, true, RendererCache.ViewModel.WIREFRAME, false,
                    null);
            systems.add(parentSystem);
        }
        // Move each atom into the global frame by applying the System
        // Transform to
        // relative atomic position
        parentTransformGroup = parentSystem.getOriginToRot();
        parentTransformGroup.getTransform(parentTransform3D);
        parentTransform3D.get(parentPosition);
        parentTransform3D.setTranslation(zero);
        // parentTransform3D.setScale(1.0d);
        ArrayList<Atom> atoms = m.getAtomList();
        ArrayList<ROLS> bonds = m.getBondList();
        for (Atom atom : atoms) {
            atom.removeFromParent();
            atom.setXYZIndex(atomNum++);
            mergedAtoms.add(atom);
            atom.getV3D(atomPosition);
            parentTransform3D.transform(atomPosition);
            atomPosition.add(parentPosition);
            atom.moveTo(atomPosition);
        }
        for (ROLS msm : bonds) {
            Bond bond = (Bond) msm;
            bond.removeFromParent();
            mergedBonds.add((Bond) msm);
        }
    }
    for (FFXSystem sys : systems) {
        close(sys);
    }
    MergeFilter mergeFilter = new MergeFilter(system, mergedAtoms, mergedBonds);
    UIFileOpener fileOpener = new UIFileOpener(mergeFilter, this);
    if (fileOpenerThreads > 0) {
        fileOpener.setNThreads(fileOpenerThreads);
    }
    Thread thread = new Thread(fileOpener);
    thread.start();
}

From source file:FourByFour.java

public void processStimulus(Enumeration criteria) {
    WakeupCriterion wakeup;//  w  ww . j av  a 2  s .co  m
    AWTEvent[] event;
    int id;
    int dx, dy;

    while (criteria.hasMoreElements()) {
        wakeup = (WakeupCriterion) criteria.nextElement();
        if (wakeup instanceof WakeupOnAWTEvent) {
            event = ((WakeupOnAWTEvent) wakeup).getAWTEvent();
            for (int i = 0; i < event.length; i++) {
                id = event[i].getID();
                if (id == MouseEvent.MOUSE_DRAGGED) {

                    x = ((MouseEvent) event[i]).getX();
                    y = ((MouseEvent) event[i]).getY();

                    dx = x - x_last;
                    dy = y - y_last;

                    x_angle = dy * y_factor;
                    y_angle = dx * x_factor;

                    transformX.rotX(x_angle);
                    transformY.rotY(y_angle);

                    modelTrans.mul(transformX, modelTrans);
                    modelTrans.mul(transformY, modelTrans);

                    transformGroup.setTransform(modelTrans);

                    x_last = x;
                    y_last = y;
                } else if (id == MouseEvent.MOUSE_PRESSED) {

                    x = x_last = ((MouseEvent) event[i]).getX();
                    y = y_last = ((MouseEvent) event[i]).getY();

                    Point3d eyePos = new Point3d();
                    canvas3D.getCenterEyeInImagePlate(eyePos);

                    Point3d mousePos = new Point3d();
                    canvas3D.getPixelLocationInImagePlate(x, y, mousePos);

                    Transform3D transform3D = new Transform3D();
                    canvas3D.getImagePlateToVworld(transform3D);

                    transform3D.transform(eyePos);
                    transform3D.transform(mousePos);

                    Vector3d mouseVec;
                    if (parallel) {
                        mouseVec = new Vector3d(0.f, 0.f, -1.f);
                    } else {
                        mouseVec = new Vector3d();
                        mouseVec.sub(mousePos, eyePos);
                        mouseVec.normalize();
                    }

                    pickRay.set(mousePos, mouseVec);
                    sceneGraphPath = branchGroup.pickAllSorted(pickRay);

                    if (sceneGraphPath != null) {
                        for (int j = 0; j < sceneGraphPath.length; j++) {
                            if (sceneGraphPath[j] != null) {
                                Node node = sceneGraphPath[j].getObject();
                                if (node instanceof Shape3D) {
                                    try {
                                        ID posID = (ID) node.getUserData();
                                        if (posID != null) {
                                            int pos = posID.get();
                                            positions.set(pos, Positions.HUMAN);
                                            canvas2D.repaint();
                                            break;
                                        }
                                    } catch (CapabilityNotSetException e) {
                                        // Catch all CapabilityNotSet
                                        // exceptions and
                                        // throw them away, prevents
                                        // renderer from
                                        // locking up when encountering
                                        // "non-selectable"
                                        // objects.
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    wakeupOn(mouseCriterion);
}

From source file:edu.uci.ics.jung.visualization3d.VisualizationViewer.java

public BranchGroup createSceneGraph(final Canvas3D canvas) {

    objRoot = new BranchGroup();
    objRoot.setCapability(Group.ALLOW_CHILDREN_EXTEND);
    objRoot.setCapability(Group.ALLOW_CHILDREN_WRITE);

    TransformGroup objScale = new TransformGroup();
    Transform3D t3d = new Transform3D();
    //      t3d.setScale(0.05);
    objScale.setTransform(t3d);//from   w  w  w  . jav a 2s  . c o m
    objRoot.addChild(objScale);

    Transform3D tt = new Transform3D();
    tt.setScale(.05);
    tt.setTranslation(new Vector3f(0, 0, -30.f));
    objTrans = new TransformGroup(tt);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
    objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
    objTrans.setCapability(TransformGroup.ALLOW_CHILDREN_EXTEND);
    objScale.addChild(objTrans);
    //      objRoot.addChild(objTrans);

    // Create Colors, Materials,  and Appearances.
    Appearance look = new Appearance();
    Color3f objColor = new Color3f(0.7f, 0.7f, 0.7f);
    Color3f black = new Color3f(0.f, 0.f, 0.f);
    Color3f white = new Color3f(1.0f, 1.0f, 0.6f);
    Color3f gray = new Color3f(.2f, .2f, .2f);
    Color3f red = new Color3f(1.0f, 0, 0);
    Color3f yellow = new Color3f(1, 1, 0);

    Material objMaterial = new Material(objColor, black, objColor, white, 100.0f);
    Material blackMaterial = new Material(objColor, black, black, objColor, 10.0f);
    Material whiteMaterial = new Material(white, white, white, white, 100.0f);
    Material grayMaterial = new Material(gray, black, gray, gray, 100.0f);

    Material redMaterial = new Material(red, black, red, red, 100.0f);
    Material yellowMaterial = new Material(yellow, black, yellow, yellow, 100);

    look.setMaterial(new Material(objColor, black, objColor, white, 100.0f));
    Appearance blackLook = new Appearance();
    blackLook.setMaterial(blackMaterial);

    Appearance whiteLook = new Appearance();
    whiteLook.setMaterial(whiteMaterial);

    Appearance grayLook = new Appearance();
    grayLook.setMaterial(grayMaterial);
    grayLook.setCapability(Appearance.ALLOW_MATERIAL_READ);
    grayLook.setCapability(Appearance.ALLOW_MATERIAL_WRITE);

    final Appearance redLook = new Appearance();
    redLook.setMaterial(redMaterial);
    //      vertexLook = redLook;

    Appearance objLook = new Appearance();
    objLook.setMaterial(objMaterial);
    grayLook = objLook;
    final Appearance yellowLook = new Appearance();
    yellowLook.setMaterial(yellowMaterial);
    Bounds bounds = new BoundingSphere(new Point3d(), 300);

    MouseRotate behavior1 = new MouseRotate();
    behavior1.setTransformGroup(objTrans);
    objTrans.addChild(behavior1);
    behavior1.setSchedulingBounds(bounds);

    MouseWheelZoom behavior2 = new MouseWheelZoom();
    behavior2.setTransformGroup(objTrans);
    //      behavior2.setFactor(10);
    objTrans.addChild(behavior2);
    behavior2.setSchedulingBounds(bounds);

    MouseTranslate behavior3 = new MouseTranslate();
    behavior3.setTransformGroup(objTrans);
    objTrans.addChild(behavior3);
    behavior3.setSchedulingBounds(bounds);

    PickTranslateBehavior ptb = new PickTranslateBehavior(objRoot, canvas, bounds, PickTool.GEOMETRY);
    ptb.setSchedulingBounds(bounds);
    //      objTrans.addChild(ptb);
    ptb.setupCallback(new PickingCallback() {

        public void transformChanged(int type, TransformGroup tg) {
            if (tg == null)
                return;
            Transform3D t3d = new Transform3D();
            tg.getTransform(t3d);
            //            System.err.println(tg+" transformChanged \n"+t3d);
            Point3f p1 = new Point3f();
            V v = vertexMap.getKey(tg);
            //            Transform3D lvw = new Transform3D();
            //            tg.getLocalToVworld(lvw);
            //            System.err.println("lvw = \n"+lvw);
            //            lvw.invert();
            //            System.err.println("invert lvw = \n"+lvw);
            Point3f p0 = layout.transform(v);
            //            Transform3D vwip = new Transform3D();
            //            canvas.getVworldToImagePlate(vwip);
            //            System.err.println("vwip=\n"+vwip);
            //            t3d.mul(lvw);
            t3d.transform(p1);
            //            scale.transform(p1);
            System.err.println(
                    "change location for vertex " + v + ", transformGroup " + tg + " from " + p0 + " to " + p1);
            //            p1.set(p1.getX()*2,p1.getY()*2,p1.getZ()*2);
            //            layout.setLocation(v, p1);

        }
    });

    PickSphereBehavior psb = new PickSphereBehavior(objRoot, canvas, bounds);

    PickVertexBehavior pvb = new PickVertexBehavior(objRoot, canvas, bounds,
            renderContext.getPickedVertexState());
    objTrans.addChild(pvb);
    pvb.addChangeListener(new ChangeListener() {

        public void stateChanged(ChangeEvent e) {
            for (V v : graph.getVertices()) {
                VertexGroup<V> vg = vertexMap.get(v);
                Appearance look = redLook;
                if (renderContext.getPickedVertexState().isPicked(v)) {
                    look = yellowLook;
                }
                Node node = vg.getShape();
                if (node instanceof Primitive) {
                    ((Primitive) node).setAppearance(look);
                }
            }

        }
    });

    //Shine it with two colored lights.
    Color3f lColor1 = new Color3f(.5f, .5f, .5f);
    Color3f lColor2 = new Color3f(1.0f, 1.0f, 1.0f);
    Vector3f lDir2 = new Vector3f(-1.0f, 0.0f, -1.0f);
    DirectionalLight lgt2 = new DirectionalLight(lColor2, lDir2);
    AmbientLight ambient = new AmbientLight(lColor1);
    lgt2.setInfluencingBounds(bounds);
    ambient.setInfluencingBounds(bounds);
    objRoot.addChild(lgt2);
    objRoot.addChild(ambient);

    // Let Java 3D perform optimizations on this scene graph.
    objRoot.compile();

    //      VisRunner runner = new VisRunner((IterativeContext)elayout);
    //      runner.relax();

    return objRoot;
}

From source file:pl.edu.icm.visnow.geometries.viewer3d.Display3DPanel.java

private void processCameraKeys(KeyEvent evt) {
    double dAngle = Math.PI / 144;
    double dScale = 129. / 128.;
    if (storingFrames) {
        dAngle = Math.PI / 360;//from   w ww .jav a  2 s  .c  o m
        dScale = 513. / 512.;
    }
    if ((evt.getModifiers() & KeyEvent.CTRL_MASK) != 0) {
        dAngle = Math.PI / 2;
    }

    TransformGroup cam = universe.getViewingPlatform().getViewPlatformTransform();
    Transform3D camTr;
    Transform3D cRot;
    Transform3D tmpTr;
    Vector3d cMov;

    if (cameraUpKeyPressed) {
        camTr = new Transform3D();
        cam.getTransform(camTr);
        cRot = new Transform3D();
        cRot.rotX(-dAngle);
        camTr.mul(cRot);
        cam.setTransform(camTr);
    }

    if (cameraDownKeyPressed) {
        camTr = new Transform3D();
        cam.getTransform(camTr);
        cRot = new Transform3D();
        cRot.rotX(dAngle);
        camTr.mul(cRot);
        cam.setTransform(camTr);
    }

    if (cameraRightKeyPressed) {
        camTr = new Transform3D();
        cam.getTransform(camTr);
        cRot = new Transform3D();
        cRot.rotY(-dAngle);
        camTr.mul(cRot);
        cam.setTransform(camTr);
    }

    if (cameraLeftKeyPressed) {
        camTr = new Transform3D();
        cam.getTransform(camTr);
        cRot = new Transform3D();
        cRot.rotY(dAngle);
        camTr.mul(cRot);
        cam.setTransform(camTr);
    }

    if (cameraForwardKeyPressed) {
        camTr = new Transform3D();
        cam.getTransform(camTr);
        cMov = new Vector3d();
        cMov.x = 0;
        cMov.y = 0;
        cMov.z = -(dScale / 50);

        tmpTr = new Transform3D();
        cam.getTransform(tmpTr);
        tmpTr.set(new Vector3d());
        tmpTr.transform(cMov);

        tmpTr.set(cMov);
        camTr.mul(tmpTr);
        cam.setTransform(camTr);
    }

    if (cameraBackwardKeyPressed) {
        camTr = new Transform3D();
        cam.getTransform(camTr);
        cMov = new Vector3d();
        cMov.x = 0;
        cMov.y = 0;
        cMov.z = (dScale / 50);

        tmpTr = new Transform3D();
        cam.getTransform(tmpTr);
        tmpTr.set(new Vector3d());
        tmpTr.transform(cMov);

        tmpTr.set(cMov);
        camTr.mul(tmpTr);
        cam.setTransform(camTr);
    }

}