1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.g3d.vtk.swt;
15 import java.awt.event.KeyEvent;
16 import java.awt.event.MouseEvent;
18 import javax.vecmath.AxisAngle4d;
19 import javax.vecmath.Point3d;
20 import javax.vecmath.Quat4d;
21 import javax.vecmath.Vector3d;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.SelectionAdapter;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.graphics.Cursor;
27 import org.eclipse.swt.widgets.Button;
28 import org.eclipse.swt.widgets.Combo;
29 import org.eclipse.swt.widgets.Display;
30 import org.eclipse.swt.widgets.Label;
31 import org.simantics.g3d.math.EulerTools;
32 import org.simantics.g3d.math.EulerTools.Order;
33 import org.simantics.g3d.math.MathTools;
34 import org.simantics.g3d.math.Ray;
35 import org.simantics.g3d.preferences.PreferenceConstants;
36 import org.simantics.g3d.scenegraph.IG3DNode;
37 import org.simantics.g3d.scenegraph.base.INode;
38 import org.simantics.g3d.scenegraph.structural.IStructuralNode;
39 import org.simantics.g3d.toolbar.ToolComposite;
40 import org.simantics.g3d.vtk.Activator;
41 import org.simantics.g3d.vtk.common.VTKNodeMap;
42 import org.simantics.g3d.vtk.gizmo.RotateAxisGizmo;
43 import org.simantics.g3d.vtk.utils.vtkUtil;
44 import org.simantics.utils.threads.ThreadUtils;
48 * FIXME: complete rewrite.
50 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
53 public class RotateAction extends vtkSwtAction{
55 public static final int X = 0;
56 public static final int Y = 1;
57 public static final int Z = 2;
58 public static final int P = 3;
60 private VTKNodeMap<?, ? extends INode> nodeMap;
61 //private TranslateGizmo gizmo = new TranslateGizmo();
62 private RotateAxisGizmo gizmo = new RotateAxisGizmo();
63 private IG3DNode node;
67 private Cursor activeCursor;// = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
68 private Cursor dragCursor;// = Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR);
72 Order order = Order.YXZ;
75 private double angles[];
78 boolean valid = false;
79 private boolean worldCoord = true;
80 //private AxisAngle4d aa = null;
81 private Quat4d parentWorldOrientation = null;
83 //AxisAngle4d rotation = new AxisAngle4d();
84 Quat4d worldOrientation = new Quat4d();
86 protected ToolComposite toolComposite;
87 protected Combo axisCombo;
89 public void setNode(IG3DNode node) {
91 if ((node instanceof IStructuralNode) && ((IStructuralNode)node).isPartOfInstantiatedModel() && !((IStructuralNode)node).isInstantiatedModelRoot()) {
97 String set = org.simantics.g3d.Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.ORIENTATION_PRESENTATION);
98 if (set.equals("aa")) {
100 } else if (set.equals("euler")){
102 String eulerOrder = org.simantics.g3d.Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.EULER_ANGLE_ORDER);
104 order = Order.valueOf(eulerOrder);
105 } catch (Exception e) {
113 public IG3DNode getNode() {
117 public RotateAction(InteractiveVtkComposite panel, VTKNodeMap<?, ? extends INode> nodeMap, ToolComposite toolComposite) {
119 setImageDescriptor(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/arrow_rotate_clockwise.png"));
121 this.nodeMap = nodeMap;
122 this.toolComposite = toolComposite;
125 angles = new double[steps+1];
126 for (int i = 0; i < angles.length; i++) {
127 angles[i] = - Math.PI + (Math.PI * i * 2.0 / steps);
130 activeCursor = Display.getCurrent().getSystemCursor(SWT.CURSOR_HAND);
131 dragCursor = Display.getCurrent().getSystemCursor(SWT.CURSOR_CROSS);
134 protected void createTools(ToolComposite toolComposite) {
135 Label label = new Label(toolComposite, SWT.READ_ONLY);
136 label.setText("Rotate axis:");
137 axisCombo = new Combo(toolComposite, SWT.READ_ONLY);
141 axisCombo.add("Camera");
142 axisCombo.addSelectionListener(new SelectionAdapter() {
144 public void widgetSelected(SelectionEvent e) {
145 Combo c = (Combo)e.getSource();
146 index = c.getSelectionIndex();
148 panel.getComponent().setFocus();
151 axisCombo.select(index);
153 Button close = new Button(toolComposite, SWT.PUSH);
154 close.setText("Close");
155 close.addSelectionListener(new SelectionAdapter() {
156 public void widgetSelected(SelectionEvent e) {
157 panel.useDefaultAction();
160 toolComposite.relayout();
163 public void attach() {
166 if (toolComposite != null) {
167 createTools(toolComposite);
170 ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
181 public void deattach() {
183 if (toolComposite != null) {
184 toolComposite.clear();
188 nodeMap.commit("Rotate");
194 private void attachUI() {
195 panel.getComponent().setCursor(activeCursor);
199 private void deattachUI() {
200 panel.getComponent().setCursor(Display.getCurrent().getSystemCursor(SWT.CURSOR_ARROW));
205 public boolean keyPressed(KeyEvent e) {
206 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
207 panel.useDefaultAction();
210 if (e.getKeyCode() == KeyEvent.VK_X) {
216 if (e.getKeyCode() == KeyEvent.VK_Y) {
222 if (e.getKeyCode() == KeyEvent.VK_Z) {
228 if (e.getKeyCode() == KeyEvent.VK_G) {
229 worldCoord = !worldCoord;
237 private void updateLock() {
238 gizmo.setType(index);
239 if (axisCombo != null)
240 axisCombo.select(index);
245 public boolean keyReleased(KeyEvent e) {
252 public boolean mouseClicked(MouseEvent e) {
253 if (e.getClickCount() > 1) {
257 panel.useDefaultAction();
258 //if(!gizmo.isPartOf(actor))
259 // panel.useDefaultAction();
271 public void setWorldCoord(boolean b) {
280 private void update() {
281 Vector3d nodePos = node.getWorldPosition();
282 // System.out.println(nodePos);
283 gizmo.setPosition(nodePos);
285 gizmo.setRotation(new AxisAngle4d());
286 parentWorldOrientation = null;
288 AxisAngle4d aa = new AxisAngle4d();
289 parentWorldOrientation = ((IG3DNode)node.getParent()).getWorldOrientation();
290 aa.set(parentWorldOrientation);
291 gizmo.setRotation(aa);
294 Point3d camPos = new Point3d(panel.getRenderer().GetActiveCamera().GetPosition());
295 Vector3d p = new Vector3d(nodePos);
298 if (parentWorldOrientation != null) {
299 Quat4d qi = new Quat4d(parentWorldOrientation);
301 MathTools.rotate(parentWorldOrientation, p, p);
303 if (panel.getRenderer().GetActiveCamera().GetParallelProjection() == 0) {
304 double distance = p.length();
306 double fov = panel.getRenderer().GetActiveCamera().GetViewAngle();
307 float s = (float) (Math.sin(fov) * distance * 0.1);
309 Vector3d scale = new Vector3d(1., 1., 1.);
318 gizmo.setScale(scale);
321 Vector3d scale = new Vector3d(1.f, 1.f, 1.f);
322 double s = panel.getRenderer().GetActiveCamera().GetParallelScale() / 5.;
330 gizmo.setScale(scale);
336 private boolean isOverNode(MouseEvent e) {
337 vtkProp picked[] = panel.pick(e.getX(), e.getY());
339 for (int i = 0; i < picked.length; i++) {
340 if (node.equals(nodeMap.getNode(picked[i])))
350 public boolean mousePressed(MouseEvent e) {
351 if (e.getButton() == MouseEvent.BUTTON1) {
356 if ((e.getModifiers() & MouseEvent.CTRL_MASK) > 0) {
361 worldOrientation = node.getWorldOrientation();
362 doChanges(true, e.getX(), e.getY());
364 panel.getComponent().setCursor(dragCursor);
367 getDefaultAction().mousePressed(e);
368 panel.getComponent().setCursor(activeCursor);
371 getDefaultAction().mousePressed(e);
379 public boolean mouseReleased(MouseEvent e) {
380 if (e.getButton() == MouseEvent.BUTTON1) {
382 worldOrientation = null;
383 panel.getComponent().setCursor(activeCursor);
385 getDefaultAction().mouseReleased(e);
391 public boolean mouseDragged(MouseEvent e) {
392 if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) > 0 && valid) {
393 if ((e.getModifiers() & MouseEvent.CTRL_MASK) > 0) {
398 doChanges(false, e.getX(), e.getY());
400 //nodeMap.modified(node);
403 getDefaultAction().mouseDragged(e);
409 Vector3d axis = null;
412 public boolean keyTyped(KeyEvent e) {
413 if (e.getKeyCode() == KeyEvent.VK_LEFT) {
414 inputType = InputType.KEY;
415 axis = new Vector3d(0.0,1.0,0.0);
416 } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
417 inputType = InputType.KEY;
418 axis = new Vector3d(0.0,-1.0,0.0);
419 } else if (e.getKeyCode() ==KeyEvent.VK_UP) {
420 inputType = InputType.KEY;
421 axis = new Vector3d(1.0,0.0,0.0);
422 } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
423 inputType = InputType.KEY;
424 axis = new Vector3d(-1.0,0.0,0.0);
429 public void doChanges(boolean pressed, int x, int y) {
430 Ray ray = vtkUtil.createMouseRay(panel.getRenderer(),x, y);
431 Vector3d p = node.getWorldPosition();
434 Vector3d axis = getRotationAxis();
437 MathTools.rotate(parentWorldOrientation, axis, axis);
441 double s[] = new double[2];
442 Vector3d i2 = new Vector3d();
444 boolean intersect = MathTools.intersectStraightPlane(ray.pos, ray.dir, p, axis, i2, s);
445 double dot = Math.abs(ray.dir.dot(axis));
446 if (intersect && dot > 0.4)
447 inputType = InputType.INTERSECT;
449 inputType = InputType.NONINTERSECT;
452 if (inputType == InputType.INTERSECT) {
453 // picking ray and plane defined by gizmo's center point and
454 // rotation axis can intersect
455 // vector from center point to intersection point
457 // creating vectors i and j that are lying on the plane and
459 // vectors are used to calculate polar coordinate for
460 // intersection point
463 // System.out.println("I,J " + i + " " + j);
464 double angleI = i2.angle(i);
465 double angleJ = i2.angle(j);
466 prevAngle = Math.atan2(Math.cos(angleJ), Math.cos(angleI));
468 // picking ray and plane defined by gizmo's center point and
469 // rotation axis are parallel,
470 // so we'll use cross product of rotation axis and picking
471 // ray to detect amount of rotation
472 i.cross(ray.dir, axis);
473 MathTools.intersectStraightStraight(ray.pos, ray.dir, p, i, new Vector3d(), new Vector3d(), s);
481 if (inputType != InputType.KEY)
482 axis = getRotationAxis();
486 Vector3d taxis = null;
488 taxis = new Vector3d(axis);
489 MathTools.rotate(parentWorldOrientation, axis, axis);
491 // System.out.println(inputType);
492 if (inputType == InputType.INTERSECT) {
494 double s[] = new double[2];
495 Vector3d i2 = new Vector3d();
496 MathTools.intersectStraightPlane(ray.pos, ray.dir, p, axis, i2, s);
498 double angleI = i2.angle(i);
499 double angleJ = i2.angle(j);
500 double angle = Math.atan2(Math.cos(angleJ), Math.cos(angleI));
501 // System.out.println("Angle " + angle + " i " + angleI + " j " + angleJ + " prev " + prevAngle);
506 //setOrientation(MathTools.getQuat(rotation));
507 AxisAngle4d rot = new AxisAngle4d(axis,angle-prevAngle);
508 Quat4d qrot = new Quat4d();
509 MathTools.getQuat(rot, qrot);
511 qrot.mulInverse(worldOrientation);
514 if (stepMethod == 0) {
516 rot.angle = roundAngle(rot.angle);
518 MathTools.getQuat(rot,qrot);
519 setOrientation(qrot);
520 } else if (stepMethod == 1){
522 //Vector3d euler = MathTools.getEuler(qrot);
523 Vector3d euler = EulerTools.getEulerFromQuat(order, qrot);
524 euler.x = roundAngle(euler.x);
525 euler.y = roundAngle(euler.y);
526 euler.z = roundAngle(euler.z);
527 //Quat4d q = MathTools.getQuat(euler);
528 Quat4d q = EulerTools.getQuatFromEuler(order, euler);
530 // System.out.println(" (" + MathTools.radToDeg(euler.x) + " " + MathTools.radToDeg(euler.y) + " " + MathTools.radToDeg(euler.z) + ") " + qrot + " "+ q);
532 setOrientation(qrot);
537 //G3DTools.multiplyOrientation(mo.getG3DNode(graph).getWorldOrientation(), new AxisAngle4d(axis,angle-prevAngle));
538 AxisAngle4d aa = MathTools.getAxisAngle(node.getWorldOrientation());
539 AxisAngle4d rot = new AxisAngle4d(axis,angle-prevAngle);
540 MathTools.multiplyOrientation(aa, rot);
541 setWorldOrientation(MathTools.getQuat(rot));
543 AxisAngle4d aa = MathTools.getAxisAngle(node.getOrientation());
544 AxisAngle4d rot = new AxisAngle4d(axis,angle-prevAngle);
545 MathTools.multiplyOrientation(aa, rot);
546 setOrientation(MathTools.getQuat(rot));
547 //G3DTools.multiplyOrientation(mo.getG3DNode(graph).getLocalOrientation(), new AxisAngle4d(axis,angle-prevAngle));
552 } else if (inputType == InputType.NONINTERSECT){
554 double s[] = new double[2];
555 MathTools.intersectStraightStraight(ray.pos, ray.dir, p, i, new Vector3d(), new Vector3d(), s);
559 //setOrientation(MathTools.getQuat(rotation));
560 AxisAngle4d rot = new AxisAngle4d(axis,s[1] - prevS);
562 Quat4d qrot = new Quat4d();
564 MathTools.getQuat(rot, qrot);
566 qrot.mulInverse(worldOrientation);
569 if (stepMethod == 0) {
571 rot.angle = roundAngle(rot.angle);
573 MathTools.getQuat(rot,qrot);
574 setOrientation(qrot);
575 } else if (stepMethod == 1){
577 //Vector3d euler = MathTools.getEuler(qrot);
578 Vector3d euler = EulerTools.getEulerFromQuat(order, qrot);
579 euler.x = roundAngle(euler.x);
580 euler.y = roundAngle(euler.y);
581 euler.z = roundAngle(euler.z);
582 //Quat4d q = MathTools.getQuat(euler);
583 Quat4d q = EulerTools.getQuatFromEuler(order, euler);
585 // System.out.println(" (" + MathTools.radToDeg(euler.x) + " " + MathTools.radToDeg(euler.y) + " " + MathTools.radToDeg(euler.z) + ") " + qrot + " "+ q);
587 setOrientation(qrot);
591 // G3DTools.setOrientation(mo.getG3DNode(graph).getLocalOrientation(), rotations.get(mo));
592 // G3DTools.multiplyOrientation(mo.getG3DNode(graph).getWorldOrientation(), new AxisAngle4d(axis,s[1] - prevS));
593 // AxisAngle4d aa = G3DTools.getOrientation(mo.getG3DNode(graph).getLocalOrientation());
594 // rotations.put(mo, aa);
595 // Vector3d euler = MathTools.getEuler(aa);
596 // euler.x = roundAngle(euler.x);
597 // euler.y = roundAngle(euler.y);
598 // euler.z = roundAngle(euler.z);
599 // aa = MathTools.getFromEuler2(euler);
601 // G3DTools.setOrientation(mo.getG3DNode(graph).getLocalOrientation(), aa);
602 // Vector3d e = MathTools.getEuler(G3DTools.getOrientation(mo.getG3DNode(graph).getLocalOrientation()));
603 // e.scale(180.0/Math.PI);
604 // text += G3DTools.getOrientation(mo.getG3DNode(graph).getLocalOrientation()) + " " + e + " ";
609 AxisAngle4d aa = MathTools.getAxisAngle(node.getWorldOrientation());
610 AxisAngle4d rot = new AxisAngle4d(axis,s[1] - prevS);
611 MathTools.multiplyOrientation(aa, rot);
612 setWorldOrientation(MathTools.getQuat(rot));
613 //G3DTools.multiplyOrientation(mo.getG3DNode(graph).getWorldOrientation(), new AxisAngle4d(axis,s[1] - prevS));
615 AxisAngle4d aa = MathTools.getAxisAngle(node.getOrientation());
616 AxisAngle4d rot = new AxisAngle4d(axis,s[1] - prevS);
617 MathTools.multiplyOrientation(aa, rot);
618 setOrientation(MathTools.getQuat(rot));
619 //G3DTools.multiplyOrientation(mo.getG3DNode(graph).getLocalOrientation(), new AxisAngle4d(axis,s[1] - prevS));
621 //text += G3DTools.getOrientation(mo.getG3DNode(graph).getLocalOrientation()) + " " + MathTools.getEuler(G3DTools.getOrientation(mo.getG3DNode(graph).getLocalOrientation())) + " ";
628 AxisAngle4d aa = MathTools.getAxisAngle(node.getWorldOrientation());
629 AxisAngle4d rot = new AxisAngle4d(axis,Math.PI * 0.5);
630 MathTools.multiplyOrientation(aa, rot);
631 setWorldOrientation(MathTools.getQuat(rot));
632 //G3DTools.multiplyOrientation(mo.getG3DNode(graph).getWorldOrientation(), new AxisAngle4d(axis,Math.PI * 0.5));
634 AxisAngle4d aa = MathTools.getAxisAngle(node.getOrientation());
635 AxisAngle4d rot = new AxisAngle4d(axis,Math.PI * 0.5);
636 MathTools.multiplyOrientation(aa, rot);
637 setOrientation(MathTools.getQuat(rot));
638 //G3DTools.multiplyOrientation(mo.getG3DNode(graph).getLocalOrientation(), new AxisAngle4d(axis,Math.PI * 0.5));
640 // text += G3DTools.getOrientation(mo.getG3DNode(graph).getLocalOrientation()) + " " + MathTools.getEuler(G3DTools.getOrientation(mo.getG3DNode(graph).getLocalOrientation())) + " ";
647 protected void setOrientation(Quat4d q) {
648 node.setOrientation(q);
651 protected void setWorldOrientation(Quat4d q) {
652 node.setWorldOrientation(q);
656 public boolean mouseMoved(MouseEvent e) {
657 return getDefaultAction().mouseMoved(e);
660 private Vector3d getRotationAxis() {
663 return new Vector3d(1.0, 0.0, 0.0);
665 return new Vector3d(0.0, 1.0, 0.0);
667 return new Vector3d(0.0, 0.0, 1.0);
669 Vector3d axis = new Vector3d(panel.getRenderer().GetActiveCamera().GetDirectionOfProjection());
677 private double prevS = 0.0;
679 private Vector3d i = new Vector3d();
680 private Vector3d j = new Vector3d();
681 private double prevAngle = 0;
683 enum InputType{INTERSECT,NONINTERSECT,KEY,NONE};
685 private boolean useStep = false;
689 private double roundAngle(double angle) {
690 while (angle < - Math.PI)
691 angle += Math.PI*2.0;
692 while (angle > Math.PI)
693 angle -= Math.PI*2.0;
697 while (angle > angles[index])
702 double d = angle - angles[index - 1];
703 double d2 = angles[index] - angle;
705 angle = angles[index - 1];
707 angle = angles[index];