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.structural.IStructuralNode;
30 import org.simantics.g3d.vtk.Activator;
31 import org.simantics.g3d.vtk.common.VTKNodeMap;
32 import org.simantics.g3d.vtk.gizmo.RotateAxisGizmo;
33 import org.simantics.g3d.vtk.utils.vtkUtil;
34 import org.simantics.utils.threads.AWTThread;
35 import org.simantics.utils.threads.ThreadUtils;
39 * FIXME: complete rewrite.
41 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
44 public class RotateAction extends vtkAwtAction{
46 public static final int X = 0;
47 public static final int Y = 1;
48 public static final int Z = 2;
49 public static final int P = 3;
51 private VTKNodeMap nodeMap;
52 //private TranslateGizmo gizmo = new TranslateGizmo();
53 private RotateAxisGizmo gizmo = new RotateAxisGizmo();
54 private IG3DNode node;
58 private Cursor activeCursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
59 private Cursor dragCursor = Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR);
63 Order order = Order.YXZ;
66 private double angles[];
69 boolean valid = false;
70 private boolean worldCoord = true;
71 //private AxisAngle4d aa = null;
72 private Quat4d parentWorldOrientation = null;
74 //AxisAngle4d rotation = new AxisAngle4d();
75 Quat4d worldOrientation = new Quat4d();
77 public void setNode(IG3DNode node) {
79 if ((node instanceof IStructuralNode) && ((IStructuralNode)node).isPartOfInstantiatedModel() && !((IStructuralNode)node).isInstantiatedModelRoot()) {
85 String set = org.simantics.g3d.Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.ORIENTATION_PRESENTATION);
86 if (set.equals("aa")) {
88 } else if (set.equals("euler")){
90 String eulerOrder = org.simantics.g3d.Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.EULER_ANGLE_ORDER);
92 order = Order.valueOf(eulerOrder);
93 } catch (Exception e) {
101 public IG3DNode getNode() {
105 public RotateAction(InteractiveVtkPanel panel, VTKNodeMap nodeMap) {
107 setImageDescriptor(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/arrow_rotate_clockwise.png"));
109 this.nodeMap = nodeMap;
113 angles = new double[steps+1];
114 for (int i = 0; i < angles.length; i++) {
115 angles[i] = - Math.PI + (Math.PI * i * 2.0 / steps);
119 public void attach() {
124 ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {
135 public void deattach() {
138 nodeMap.commit("Rotate");
144 private void attachUI() {
145 panel.setCursor(activeCursor);
149 private void deattachUI() {
150 panel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
155 public void keyPressed(KeyEvent e) {
156 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
157 panel.useDefaultAction();
160 if (e.getKeyCode() == KeyEvent.VK_X) {
166 if (e.getKeyCode() == KeyEvent.VK_Y) {
172 if (e.getKeyCode() == KeyEvent.VK_Z) {
178 if (e.getKeyCode() == KeyEvent.VK_G) {
179 worldCoord = !worldCoord;
181 gizmo.setType(index);
186 public void keyReleased(KeyEvent e) {
193 public void mouseClicked(MouseEvent e) {
194 if (e.getClickCount() > 1) {
198 panel.useDefaultAction();
199 //if(!gizmo.isPartOf(actor))
200 // panel.useDefaultAction();
206 public void mouseEntered(MouseEvent e) {
211 public void mouseExited(MouseEvent e) {
219 public void setWorldCoord(boolean b) {
228 private void update() {
229 Vector3d nodePos = node.getWorldPosition();
230 System.out.println(nodePos);
231 gizmo.setPosition(nodePos);
233 gizmo.setRotation(new AxisAngle4d());
234 parentWorldOrientation = null;
236 AxisAngle4d aa = new AxisAngle4d();
237 parentWorldOrientation = ((IG3DNode)node.getParent()).getWorldOrientation();
238 aa.set(parentWorldOrientation);
239 gizmo.setRotation(aa);
242 Point3d camPos = new Point3d(panel.GetRenderer().GetActiveCamera().GetPosition());
243 Vector3d p = new Vector3d(nodePos);
246 if (parentWorldOrientation != null) {
247 Quat4d qi = new Quat4d(parentWorldOrientation);
249 MathTools.rotate(parentWorldOrientation, p, p);
251 if (panel.GetRenderer().GetActiveCamera().GetParallelProjection() == 0) {
252 double distance = p.length();
254 double fov = panel.GetRenderer().GetActiveCamera().GetViewAngle();
255 float s = (float) (Math.sin(fov) * distance * 0.1);
257 Vector3d scale = new Vector3d(1., 1., 1.);
266 gizmo.setScale(scale);
269 Vector3d scale = new Vector3d(1.f, 1.f, 1.f);
270 double s = panel.GetRenderer().GetActiveCamera().GetParallelScale() / 5.;
278 gizmo.setScale(scale);
284 private boolean isOverNode(MouseEvent e) {
285 vtkProp picked[] = panel.pick(e.getX(), e.getY());
287 for (int i = 0; i < picked.length; i++) {
288 if (node.equals(nodeMap.getNode(picked[i])))
298 public void mousePressed(MouseEvent e) {
299 if (e.getButton() == MouseEvent.BUTTON1) {
304 if ((e.getModifiers() & MouseEvent.CTRL_MASK) > 0) {
309 worldOrientation = node.getWorldOrientation();
310 doChanges(true, e.getX(), e.getY());
312 panel.setCursor(dragCursor);
315 getDefaultAction().mousePressed(e);
316 panel.setCursor(activeCursor);
319 getDefaultAction().mousePressed(e);
326 public void mouseReleased(MouseEvent e) {
327 if (e.getButton() == MouseEvent.BUTTON1) {
329 worldOrientation = null;
330 panel.setCursor(activeCursor);
332 getDefaultAction().mouseReleased(e);
337 public void mouseDragged(MouseEvent e) {
338 if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) > 0 && valid) {
339 if ((e.getModifiers() & MouseEvent.CTRL_MASK) > 0) {
344 doChanges(false, e.getX(), e.getY());
346 //nodeMap.modified(node);
349 getDefaultAction().mouseDragged(e);
354 Vector3d axis = null;
357 public void keyTyped(KeyEvent e) {
358 if (e.getKeyCode() == KeyEvent.VK_LEFT) {
359 inputType = InputType.KEY;
360 axis = new Vector3d(0.0,1.0,0.0);
361 } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
362 inputType = InputType.KEY;
363 axis = new Vector3d(0.0,-1.0,0.0);
364 } else if (e.getKeyCode() ==KeyEvent.VK_UP) {
365 inputType = InputType.KEY;
366 axis = new Vector3d(1.0,0.0,0.0);
367 } else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
368 inputType = InputType.KEY;
369 axis = new Vector3d(-1.0,0.0,0.0);
373 public void doChanges(boolean pressed, int x, int y) {
374 Ray ray = vtkUtil.createMouseRay(panel.GetRenderer(),x, y);
375 Vector3d p = node.getWorldPosition();
378 Vector3d axis = getRotationAxis();
381 MathTools.rotate(parentWorldOrientation, axis, axis);
385 double s[] = new double[2];
386 Vector3d i2 = new Vector3d();
388 boolean intersect = MathTools.intersectStraightPlane(ray.pos, ray.dir, p, axis, i2, s);
389 double dot = Math.abs(ray.dir.dot(axis));
390 if (intersect && dot > 0.4)
391 inputType = InputType.INTERSECT;
393 inputType = InputType.NONINTERSECT;
396 if (inputType == InputType.INTERSECT) {
397 // picking ray and plane defined by gizmo's center point and
398 // rotation axis can intersect
399 // vector from center point to intersection point
401 // creating vectors i and j that are lying on the plane and
403 // vectors are used to calculate polar coordinate for
404 // intersection point
407 System.out.println("I,J " + i + " " + j);
408 double angleI = i2.angle(i);
409 double angleJ = i2.angle(j);
410 prevAngle = Math.atan2(Math.cos(angleJ), Math.cos(angleI));
412 // picking ray and plane defined by gizmo's center point and
413 // rotation axis are parallel,
414 // so we'll use cross product of rotation axis and picking
415 // ray to detect amount of rotation
416 i.cross(ray.dir, axis);
417 MathTools.intersectStraightStraight(ray.pos, ray.dir, p, i, new Vector3d(), new Vector3d(), s);
425 if (inputType != InputType.KEY)
426 axis = getRotationAxis();
430 Vector3d taxis = null;
432 taxis = new Vector3d(axis);
433 MathTools.rotate(parentWorldOrientation, axis, axis);
435 System.out.println(inputType);
436 if (inputType == InputType.INTERSECT) {
438 double s[] = new double[2];
439 Vector3d i2 = new Vector3d();
440 MathTools.intersectStraightPlane(ray.pos, ray.dir, p, axis, i2, s);
442 double angleI = i2.angle(i);
443 double angleJ = i2.angle(j);
444 double angle = Math.atan2(Math.cos(angleJ), Math.cos(angleI));
445 System.out.println("Angle " + angle + " i " + angleI + " j " + angleJ + " prev " + prevAngle);
450 //setOrientation(MathTools.getQuat(rotation));
451 AxisAngle4d rot = new AxisAngle4d(axis,angle-prevAngle);
452 Quat4d qrot = new Quat4d();
453 MathTools.getQuat(rot, qrot);
455 qrot.mulInverse(worldOrientation);
458 if (stepMethod == 0) {
460 rot.angle = roundAngle(rot.angle);
462 MathTools.getQuat(rot,qrot);
463 setOrientation(qrot);
464 } else if (stepMethod == 1){
466 //Vector3d euler = MathTools.getEuler(qrot);
467 Vector3d euler = EulerTools.getEulerFromQuat(order, qrot);
468 euler.x = roundAngle(euler.x);
469 euler.y = roundAngle(euler.y);
470 euler.z = roundAngle(euler.z);
471 //Quat4d q = MathTools.getQuat(euler);
472 Quat4d q = EulerTools.getQuatFromEuler(order, euler);
474 System.out.println(" (" + MathTools.radToDeg(euler.x) + " " + MathTools.radToDeg(euler.y) + " " + MathTools.radToDeg(euler.z) + ") " + qrot + " "+ q);
476 setOrientation(qrot);
481 //G3DTools.multiplyOrientation(mo.getG3DNode(graph).getWorldOrientation(), new AxisAngle4d(axis,angle-prevAngle));
482 AxisAngle4d aa = MathTools.getAxisAngle(node.getWorldOrientation());
483 AxisAngle4d rot = new AxisAngle4d(axis,angle-prevAngle);
484 MathTools.multiplyOrientation(aa, rot);
485 setWorldOrientation(MathTools.getQuat(rot));
487 AxisAngle4d aa = MathTools.getAxisAngle(node.getOrientation());
488 AxisAngle4d rot = new AxisAngle4d(axis,angle-prevAngle);
489 MathTools.multiplyOrientation(aa, rot);
490 setOrientation(MathTools.getQuat(rot));
491 //G3DTools.multiplyOrientation(mo.getG3DNode(graph).getLocalOrientation(), new AxisAngle4d(axis,angle-prevAngle));
496 } else if (inputType == InputType.NONINTERSECT){
498 double s[] = new double[2];
499 MathTools.intersectStraightStraight(ray.pos, ray.dir, p, i, new Vector3d(), new Vector3d(), s);
503 //setOrientation(MathTools.getQuat(rotation));
504 AxisAngle4d rot = new AxisAngle4d(axis,s[1] - prevS);
506 Quat4d qrot = new Quat4d();
508 MathTools.getQuat(rot, qrot);
510 qrot.mulInverse(worldOrientation);
513 if (stepMethod == 0) {
515 rot.angle = roundAngle(rot.angle);
517 MathTools.getQuat(rot,qrot);
518 setOrientation(qrot);
519 } else if (stepMethod == 1){
521 //Vector3d euler = MathTools.getEuler(qrot);
522 Vector3d euler = EulerTools.getEulerFromQuat(order, qrot);
523 euler.x = roundAngle(euler.x);
524 euler.y = roundAngle(euler.y);
525 euler.z = roundAngle(euler.z);
526 //Quat4d q = MathTools.getQuat(euler);
527 Quat4d q = EulerTools.getQuatFromEuler(order, euler);
529 System.out.println(" (" + MathTools.radToDeg(euler.x) + " " + MathTools.radToDeg(euler.y) + " " + MathTools.radToDeg(euler.z) + ") " + qrot + " "+ q);
531 setOrientation(qrot);
535 // G3DTools.setOrientation(mo.getG3DNode(graph).getLocalOrientation(), rotations.get(mo));
536 // G3DTools.multiplyOrientation(mo.getG3DNode(graph).getWorldOrientation(), new AxisAngle4d(axis,s[1] - prevS));
537 // AxisAngle4d aa = G3DTools.getOrientation(mo.getG3DNode(graph).getLocalOrientation());
538 // rotations.put(mo, aa);
539 // Vector3d euler = MathTools.getEuler(aa);
540 // euler.x = roundAngle(euler.x);
541 // euler.y = roundAngle(euler.y);
542 // euler.z = roundAngle(euler.z);
543 // aa = MathTools.getFromEuler2(euler);
545 // G3DTools.setOrientation(mo.getG3DNode(graph).getLocalOrientation(), aa);
546 // Vector3d e = MathTools.getEuler(G3DTools.getOrientation(mo.getG3DNode(graph).getLocalOrientation()));
547 // e.scale(180.0/Math.PI);
548 // text += G3DTools.getOrientation(mo.getG3DNode(graph).getLocalOrientation()) + " " + e + " ";
553 AxisAngle4d aa = MathTools.getAxisAngle(node.getWorldOrientation());
554 AxisAngle4d rot = new AxisAngle4d(axis,s[1] - prevS);
555 MathTools.multiplyOrientation(aa, rot);
556 setWorldOrientation(MathTools.getQuat(rot));
557 //G3DTools.multiplyOrientation(mo.getG3DNode(graph).getWorldOrientation(), new AxisAngle4d(axis,s[1] - prevS));
559 AxisAngle4d aa = MathTools.getAxisAngle(node.getOrientation());
560 AxisAngle4d rot = new AxisAngle4d(axis,s[1] - prevS);
561 MathTools.multiplyOrientation(aa, rot);
562 setOrientation(MathTools.getQuat(rot));
563 //G3DTools.multiplyOrientation(mo.getG3DNode(graph).getLocalOrientation(), new AxisAngle4d(axis,s[1] - prevS));
565 //text += G3DTools.getOrientation(mo.getG3DNode(graph).getLocalOrientation()) + " " + MathTools.getEuler(G3DTools.getOrientation(mo.getG3DNode(graph).getLocalOrientation())) + " ";
572 AxisAngle4d aa = MathTools.getAxisAngle(node.getWorldOrientation());
573 AxisAngle4d rot = new AxisAngle4d(axis,Math.PI * 0.5);
574 MathTools.multiplyOrientation(aa, rot);
575 setWorldOrientation(MathTools.getQuat(rot));
576 //G3DTools.multiplyOrientation(mo.getG3DNode(graph).getWorldOrientation(), new AxisAngle4d(axis,Math.PI * 0.5));
578 AxisAngle4d aa = MathTools.getAxisAngle(node.getOrientation());
579 AxisAngle4d rot = new AxisAngle4d(axis,Math.PI * 0.5);
580 MathTools.multiplyOrientation(aa, rot);
581 setOrientation(MathTools.getQuat(rot));
582 //G3DTools.multiplyOrientation(mo.getG3DNode(graph).getLocalOrientation(), new AxisAngle4d(axis,Math.PI * 0.5));
584 // text += G3DTools.getOrientation(mo.getG3DNode(graph).getLocalOrientation()) + " " + MathTools.getEuler(G3DTools.getOrientation(mo.getG3DNode(graph).getLocalOrientation())) + " ";
591 protected void setOrientation(Quat4d q) {
592 node.setOrientation(q);
595 protected void setWorldOrientation(Quat4d q) {
596 node.setWorldOrientation(q);
600 public void mouseMoved(MouseEvent e) {
601 getDefaultAction().mouseMoved(e);
604 private Vector3d getRotationAxis() {
607 return new Vector3d(1.0, 0.0, 0.0);
609 return new Vector3d(0.0, 1.0, 0.0);
611 return new Vector3d(0.0, 0.0, 1.0);
613 Vector3d axis = new Vector3d(panel.GetRenderer().GetActiveCamera()
614 .GetDirectionOfProjection());
622 private double prevS = 0.0;
624 private Vector3d i = new Vector3d();
625 private Vector3d j = new Vector3d();
626 private double prevAngle = 0;
628 enum InputType{INTERSECT,NONINTERSECT,KEY,NONE};
630 private boolean useStep = false;
634 private double roundAngle(double angle) {
635 while (angle < - Math.PI)
636 angle += Math.PI*2.0;
637 while (angle > Math.PI)
638 angle -= Math.PI*2.0;
642 while (angle > angles[index])
647 double d = angle - angles[index - 1];
648 double d2 = angles[index] - angle;
650 angle = angles[index - 1];
652 angle = angles[index];