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.awt;
14 import java.awt.Cursor;
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.simantics.g3d.math.EulerTools;
24 import org.simantics.g3d.math.EulerTools.Order;
25 import org.simantics.g3d.math.MathTools;
26 import org.simantics.g3d.math.Ray;
27 import org.simantics.g3d.preferences.PreferenceConstants;
28 import org.simantics.g3d.scenegraph.IG3DNode;
29 import org.simantics.g3d.scenegraph.base.INode;
30 import org.simantics.g3d.scenegraph.structural.IStructuralNode;
31 import org.simantics.g3d.vtk.Activator;
32 import org.simantics.g3d.vtk.common.VTKNodeMap;
33 import org.simantics.g3d.vtk.gizmo.RotateAxisGizmo;
34 import org.simantics.g3d.vtk.utils.vtkUtil;
35 import org.simantics.utils.threads.AWTThread;
36 import org.simantics.utils.threads.ThreadUtils;
40 * FIXME: complete rewrite.
42 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
45 public class RotateAction extends vtkAwtAction{
47 public static final int X = 0;
48 public static final int Y = 1;
49 public static final int Z = 2;
50 public static final int P = 3;
52 private VTKNodeMap<?,? extends INode> nodeMap;
53 //private TranslateGizmo gizmo = new TranslateGizmo();
54 private RotateAxisGizmo gizmo = new RotateAxisGizmo();
55 private IG3DNode node;
59 private Cursor activeCursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
60 private Cursor dragCursor = Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR);
64 Order order = Order.YXZ;
67 private double angles[];
70 boolean valid = false;
71 private boolean worldCoord = true;
72 //private AxisAngle4d aa = null;
73 private Quat4d parentWorldOrientation = null;
75 //AxisAngle4d rotation = new AxisAngle4d();
76 Quat4d worldOrientation = new Quat4d();
78 public void setNode(IG3DNode node) {
80 if ((node instanceof IStructuralNode) && ((IStructuralNode)node).isPartOfInstantiatedModel() && !((IStructuralNode)node).isInstantiatedModelRoot()) {
86 String set = org.simantics.g3d.Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.ORIENTATION_PRESENTATION);
87 if (set.equals("aa")) {
89 } else if (set.equals("euler")){
91 String eulerOrder = org.simantics.g3d.Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.EULER_ANGLE_ORDER);
93 order = Order.valueOf(eulerOrder);
94 } catch (Exception e) {
102 public IG3DNode getNode() {
106 public RotateAction(InteractiveVtkPanel panel, VTKNodeMap<?,? extends INode> nodeMap) {
108 setImageDescriptor(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/arrow_rotate_clockwise.png"));
110 this.nodeMap = nodeMap;
114 angles = new double[steps+1];
115 for (int i = 0; i < angles.length; i++) {
116 angles[i] = - Math.PI + (Math.PI * i * 2.0 / steps);
120 public void attach() {
125 ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {
136 public void deattach() {
139 nodeMap.commit("Rotate");
145 private void attachUI() {
146 panel.setCursor(activeCursor);
150 private void deattachUI() {
151 panel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
156 public void keyPressed(KeyEvent e) {
157 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
158 panel.useDefaultAction();
161 if (e.getKeyCode() == KeyEvent.VK_X) {
167 if (e.getKeyCode() == KeyEvent.VK_Y) {
173 if (e.getKeyCode() == KeyEvent.VK_Z) {
179 if (e.getKeyCode() == KeyEvent.VK_G) {
180 worldCoord = !worldCoord;
182 gizmo.setType(index);
187 public void keyReleased(KeyEvent e) {
194 public void mouseClicked(MouseEvent e) {
195 if (e.getClickCount() > 1) {
199 panel.useDefaultAction();
200 //if(!gizmo.isPartOf(actor))
201 // panel.useDefaultAction();
207 public void mouseEntered(MouseEvent e) {
212 public void mouseExited(MouseEvent e) {
220 public void setWorldCoord(boolean b) {
229 private void update() {
230 Vector3d nodePos = node.getWorldPosition();
231 System.out.println(nodePos);
232 gizmo.setPosition(nodePos);
234 gizmo.setRotation(new AxisAngle4d());
235 parentWorldOrientation = null;
237 AxisAngle4d aa = new AxisAngle4d();
238 parentWorldOrientation = ((IG3DNode)node.getParent()).getWorldOrientation();
239 aa.set(parentWorldOrientation);
240 gizmo.setRotation(aa);
243 Point3d camPos = new Point3d(panel.GetRenderer().GetActiveCamera().GetPosition());
244 Vector3d p = new Vector3d(nodePos);
247 if (parentWorldOrientation != null) {
248 Quat4d qi = new Quat4d(parentWorldOrientation);
250 MathTools.rotate(parentWorldOrientation, p, p);
252 if (panel.GetRenderer().GetActiveCamera().GetParallelProjection() == 0) {
253 double distance = p.length();
255 double fov = panel.GetRenderer().GetActiveCamera().GetViewAngle();
256 float s = (float) (Math.sin(fov) * distance * 0.1);
258 Vector3d scale = new Vector3d(1., 1., 1.);
267 gizmo.setScale(scale);
270 Vector3d scale = new Vector3d(1.f, 1.f, 1.f);
271 double s = panel.GetRenderer().GetActiveCamera().GetParallelScale() / 5.;
279 gizmo.setScale(scale);
285 private boolean isOverNode(MouseEvent e) {
286 vtkProp picked[] = panel.pick(e.getX(), e.getY());
288 for (int i = 0; i < picked.length; i++) {
289 if (node.equals(nodeMap.getNode(picked[i])))
299 public void mousePressed(MouseEvent e) {
300 if (e.getButton() == MouseEvent.BUTTON1) {
305 if ((e.getModifiers() & MouseEvent.CTRL_MASK) > 0) {
310 worldOrientation = node.getWorldOrientation();
311 doChanges(true, e.getX(), e.getY());
313 panel.setCursor(dragCursor);
316 getDefaultAction().mousePressed(e);
317 panel.setCursor(activeCursor);
320 getDefaultAction().mousePressed(e);
327 public void mouseReleased(MouseEvent e) {
328 if (e.getButton() == MouseEvent.BUTTON1) {
330 worldOrientation = null;
331 panel.setCursor(activeCursor);
333 getDefaultAction().mouseReleased(e);
338 public void mouseDragged(MouseEvent e) {
339 if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) > 0 && valid) {
340 if ((e.getModifiers() & MouseEvent.CTRL_MASK) > 0) {
345 doChanges(false, e.getX(), e.getY());
347 //nodeMap.modified(node);
350 getDefaultAction().mouseDragged(e);
355 Vector3d axis = null;
358 public void keyTyped(KeyEvent e) {
359 if (e.getKeyCode() == KeyEvent.VK_LEFT) {
360 inputType = InputType.KEY;
361 axis = new Vector3d(0.0,1.0,0.0);
362 } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
363 inputType = InputType.KEY;
364 axis = new Vector3d(0.0,-1.0,0.0);
365 } else if (e.getKeyCode() ==KeyEvent.VK_UP) {
366 inputType = InputType.KEY;
367 axis = new Vector3d(1.0,0.0,0.0);
368 } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
369 inputType = InputType.KEY;
370 axis = new Vector3d(-1.0,0.0,0.0);
374 public void doChanges(boolean pressed, int x, int y) {
375 Ray ray = vtkUtil.createMouseRay(panel.GetRenderer(),x, y);
376 Vector3d p = node.getWorldPosition();
379 Vector3d axis = getRotationAxis();
382 MathTools.rotate(parentWorldOrientation, axis, axis);
386 double s[] = new double[2];
387 Vector3d i2 = new Vector3d();
389 boolean intersect = MathTools.intersectStraightPlane(ray.pos, ray.dir, p, axis, i2, s);
390 double dot = Math.abs(ray.dir.dot(axis));
391 if (intersect && dot > 0.4)
392 inputType = InputType.INTERSECT;
394 inputType = InputType.NONINTERSECT;
397 if (inputType == InputType.INTERSECT) {
398 // picking ray and plane defined by gizmo's center point and
399 // rotation axis can intersect
400 // vector from center point to intersection point
402 // creating vectors i and j that are lying on the plane and
404 // vectors are used to calculate polar coordinate for
405 // intersection point
408 System.out.println("I,J " + i + " " + j);
409 double angleI = i2.angle(i);
410 double angleJ = i2.angle(j);
411 prevAngle = Math.atan2(Math.cos(angleJ), Math.cos(angleI));
413 // picking ray and plane defined by gizmo's center point and
414 // rotation axis are parallel,
415 // so we'll use cross product of rotation axis and picking
416 // ray to detect amount of rotation
417 i.cross(ray.dir, axis);
418 MathTools.intersectStraightStraight(ray.pos, ray.dir, p, i, new Vector3d(), new Vector3d(), s);
426 if (inputType != InputType.KEY)
427 axis = getRotationAxis();
431 Vector3d taxis = null;
433 taxis = new Vector3d(axis);
434 MathTools.rotate(parentWorldOrientation, axis, axis);
436 System.out.println(inputType);
437 if (inputType == InputType.INTERSECT) {
439 double s[] = new double[2];
440 Vector3d i2 = new Vector3d();
441 MathTools.intersectStraightPlane(ray.pos, ray.dir, p, axis, i2, s);
443 double angleI = i2.angle(i);
444 double angleJ = i2.angle(j);
445 double angle = Math.atan2(Math.cos(angleJ), Math.cos(angleI));
446 System.out.println("Angle " + angle + " i " + angleI + " j " + angleJ + " prev " + prevAngle);
451 //setOrientation(MathTools.getQuat(rotation));
452 AxisAngle4d rot = new AxisAngle4d(axis,angle-prevAngle);
453 Quat4d qrot = new Quat4d();
454 MathTools.getQuat(rot, qrot);
456 qrot.mulInverse(worldOrientation);
459 if (stepMethod == 0) {
461 rot.angle = roundAngle(rot.angle);
463 MathTools.getQuat(rot,qrot);
464 setOrientation(qrot);
465 } else if (stepMethod == 1){
467 //Vector3d euler = MathTools.getEuler(qrot);
468 Vector3d euler = EulerTools.getEulerFromQuat(order, qrot);
469 euler.x = roundAngle(euler.x);
470 euler.y = roundAngle(euler.y);
471 euler.z = roundAngle(euler.z);
472 //Quat4d q = MathTools.getQuat(euler);
473 Quat4d q = EulerTools.getQuatFromEuler(order, euler);
475 System.out.println(" (" + MathTools.radToDeg(euler.x) + " " + MathTools.radToDeg(euler.y) + " " + MathTools.radToDeg(euler.z) + ") " + qrot + " "+ q);
477 setOrientation(qrot);
482 //G3DTools.multiplyOrientation(mo.getG3DNode(graph).getWorldOrientation(), new AxisAngle4d(axis,angle-prevAngle));
483 AxisAngle4d aa = MathTools.getAxisAngle(node.getWorldOrientation());
484 AxisAngle4d rot = new AxisAngle4d(axis,angle-prevAngle);
485 MathTools.multiplyOrientation(aa, rot);
486 setWorldOrientation(MathTools.getQuat(rot));
488 AxisAngle4d aa = MathTools.getAxisAngle(node.getOrientation());
489 AxisAngle4d rot = new AxisAngle4d(axis,angle-prevAngle);
490 MathTools.multiplyOrientation(aa, rot);
491 setOrientation(MathTools.getQuat(rot));
492 //G3DTools.multiplyOrientation(mo.getG3DNode(graph).getLocalOrientation(), new AxisAngle4d(axis,angle-prevAngle));
497 } else if (inputType == InputType.NONINTERSECT){
499 double s[] = new double[2];
500 MathTools.intersectStraightStraight(ray.pos, ray.dir, p, i, new Vector3d(), new Vector3d(), s);
504 //setOrientation(MathTools.getQuat(rotation));
505 AxisAngle4d rot = new AxisAngle4d(axis,s[1] - prevS);
507 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);
536 // G3DTools.setOrientation(mo.getG3DNode(graph).getLocalOrientation(), rotations.get(mo));
537 // G3DTools.multiplyOrientation(mo.getG3DNode(graph).getWorldOrientation(), new AxisAngle4d(axis,s[1] - prevS));
538 // AxisAngle4d aa = G3DTools.getOrientation(mo.getG3DNode(graph).getLocalOrientation());
539 // rotations.put(mo, aa);
540 // Vector3d euler = MathTools.getEuler(aa);
541 // euler.x = roundAngle(euler.x);
542 // euler.y = roundAngle(euler.y);
543 // euler.z = roundAngle(euler.z);
544 // aa = MathTools.getFromEuler2(euler);
546 // G3DTools.setOrientation(mo.getG3DNode(graph).getLocalOrientation(), aa);
547 // Vector3d e = MathTools.getEuler(G3DTools.getOrientation(mo.getG3DNode(graph).getLocalOrientation()));
548 // e.scale(180.0/Math.PI);
549 // text += G3DTools.getOrientation(mo.getG3DNode(graph).getLocalOrientation()) + " " + e + " ";
554 AxisAngle4d aa = MathTools.getAxisAngle(node.getWorldOrientation());
555 AxisAngle4d rot = new AxisAngle4d(axis,s[1] - prevS);
556 MathTools.multiplyOrientation(aa, rot);
557 setWorldOrientation(MathTools.getQuat(rot));
558 //G3DTools.multiplyOrientation(mo.getG3DNode(graph).getWorldOrientation(), new AxisAngle4d(axis,s[1] - prevS));
560 AxisAngle4d aa = MathTools.getAxisAngle(node.getOrientation());
561 AxisAngle4d rot = new AxisAngle4d(axis,s[1] - prevS);
562 MathTools.multiplyOrientation(aa, rot);
563 setOrientation(MathTools.getQuat(rot));
564 //G3DTools.multiplyOrientation(mo.getG3DNode(graph).getLocalOrientation(), new AxisAngle4d(axis,s[1] - prevS));
566 //text += G3DTools.getOrientation(mo.getG3DNode(graph).getLocalOrientation()) + " " + MathTools.getEuler(G3DTools.getOrientation(mo.getG3DNode(graph).getLocalOrientation())) + " ";
573 AxisAngle4d aa = MathTools.getAxisAngle(node.getWorldOrientation());
574 AxisAngle4d rot = new AxisAngle4d(axis,Math.PI * 0.5);
575 MathTools.multiplyOrientation(aa, rot);
576 setWorldOrientation(MathTools.getQuat(rot));
577 //G3DTools.multiplyOrientation(mo.getG3DNode(graph).getWorldOrientation(), new AxisAngle4d(axis,Math.PI * 0.5));
579 AxisAngle4d aa = MathTools.getAxisAngle(node.getOrientation());
580 AxisAngle4d rot = new AxisAngle4d(axis,Math.PI * 0.5);
581 MathTools.multiplyOrientation(aa, rot);
582 setOrientation(MathTools.getQuat(rot));
583 //G3DTools.multiplyOrientation(mo.getG3DNode(graph).getLocalOrientation(), new AxisAngle4d(axis,Math.PI * 0.5));
585 // text += G3DTools.getOrientation(mo.getG3DNode(graph).getLocalOrientation()) + " " + MathTools.getEuler(G3DTools.getOrientation(mo.getG3DNode(graph).getLocalOrientation())) + " ";
592 protected void setOrientation(Quat4d q) {
593 node.setOrientation(q);
596 protected void setWorldOrientation(Quat4d q) {
597 node.setWorldOrientation(q);
601 public void mouseMoved(MouseEvent e) {
602 getDefaultAction().mouseMoved(e);
605 private Vector3d getRotationAxis() {
608 return new Vector3d(1.0, 0.0, 0.0);
610 return new Vector3d(0.0, 1.0, 0.0);
612 return new Vector3d(0.0, 0.0, 1.0);
614 Vector3d axis = new Vector3d(panel.GetRenderer().GetActiveCamera()
615 .GetDirectionOfProjection());
623 private double prevS = 0.0;
625 private Vector3d i = new Vector3d();
626 private Vector3d j = new Vector3d();
627 private double prevAngle = 0;
629 enum InputType{INTERSECT,NONINTERSECT,KEY,NONE};
631 private boolean useStep = false;
635 private double roundAngle(double angle) {
636 while (angle < - Math.PI)
637 angle += Math.PI*2.0;
638 while (angle > Math.PI)
639 angle -= Math.PI*2.0;
643 while (angle > angles[index])
648 double d = angle - angles[index - 1];
649 double d2 = angles[index] - angle;
651 angle = angles[index - 1];
653 angle = angles[index];