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;
14 import java.awt.event.KeyEvent;
15 import java.awt.event.MouseEvent;
16 import java.awt.event.MouseWheelEvent;
17 import java.math.BigDecimal;
19 import javax.vecmath.AxisAngle4d;
20 import javax.vecmath.Point3d;
21 import javax.vecmath.Quat4d;
22 import javax.vecmath.Vector3d;
24 import org.eclipse.swt.SWT;
25 import org.eclipse.swt.graphics.Cursor;
26 import org.eclipse.swt.widgets.Display;
27 import org.simantics.g3d.math.MathTools;
28 import org.simantics.g3d.math.Ray;
29 import org.simantics.g3d.scenegraph.IG3DNode;
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.TranslateAxisGizmo;
34 import org.simantics.g3d.vtk.utils.vtkUtil;
35 import org.simantics.utils.threads.AWTThread;
36 import org.simantics.utils.threads.ThreadUtils;
40 public class TranslateAction extends vtkSwtAction{
42 public static final int X = 0;
43 public static final int Y = 1;
44 public static final int Z = 2;
45 public static final int XY = 3;
46 public static final int XZ = 4;
47 public static final int YZ = 5;
48 public static final int P = 6;
50 private VTKNodeMap nodeMap;
51 //private TranslateGizmo gizmo = new TranslateGizmo();
52 private TranslateAxisGizmo gizmo = new TranslateAxisGizmo();
53 protected IG3DNode node;
57 private Cursor activeCursor;// = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
58 private Cursor dragCursor;// = Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR);
60 public void setNode(IG3DNode node) {
62 if ((node instanceof IStructuralNode) && ((IStructuralNode)node).isPartOfInstantiatedModel() && !((IStructuralNode)node).isInstantiatedModelRoot()) {
69 public IG3DNode getNode() {
73 public TranslateAction(InteractiveVtkComposite panel, VTKNodeMap nodeMap) {
75 setImageDescriptor(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/arrow_out.png"));
77 this.nodeMap = nodeMap;
79 activeCursor = Display.getCurrent().getSystemCursor(SWT.CURSOR_HAND);
80 dragCursor = Display.getCurrent().getSystemCursor(SWT.CURSOR_SIZEALL);
83 public void attach() {
88 ThreadUtils.asyncExec(panel.getThreadQueue(), new Runnable() {
99 public void deattach() {
108 private void attachUI() {
109 panel.getComponent().setCursor(activeCursor);
113 private void deattachUI() {
114 panel.getComponent().setCursor(Display.getCurrent().getSystemCursor(SWT.CURSOR_ARROW));
119 public boolean keyPressed(KeyEvent e) {
120 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
121 panel.useDefaultAction();
124 if (e.getKeyCode() == KeyEvent.VK_X) {
130 if (e.getKeyCode() == KeyEvent.VK_Y) {
136 if (e.getKeyCode() == KeyEvent.VK_Z) {
142 if (e.getKeyCode() == KeyEvent.VK_G) {
143 worldCoord = !worldCoord;
145 gizmo.setType(index);
155 public boolean mouseClicked(MouseEvent e) {
156 if (e.getClickCount() > 1) {
160 panel.useDefaultAction();
162 //if(!gizmo.isPartOf(actor))
163 // panel.useDefaultAction();
169 private boolean isOverNode(MouseEvent e) {
170 vtkProp picked[] = panel.pick(e.getX(), e.getY());
172 for (int i = 0; i < picked.length; i++) {
173 if (node.equals(nodeMap.getNode(picked[i])))
182 protected boolean valid = false;
183 private boolean worldCoord = true;
184 private AxisAngle4d aa = null;
185 private Quat4d q = null;
188 public void setWorldCoord(boolean b) {
197 protected void update() {
201 gizmo.setRotation(new AxisAngle4d());
205 aa = new AxisAngle4d();
206 aa.set(((IG3DNode)node.getParent()).getWorldOrientation());
207 gizmo.setRotation(aa);
209 MathTools.getQuat(aa, q);
212 Vector3d nodePos = node.getWorldPosition();
213 //System.out.println(nodePos);
214 gizmo.setPosition(nodePos);
217 Point3d camPos = new Point3d(panel.getRenderer().GetActiveCamera().GetPosition());
218 Vector3d p = new Vector3d(nodePos);
222 Quat4d qi = new Quat4d(q);
224 MathTools.rotate(q, p, p);
226 if (panel.getRenderer().GetActiveCamera().GetParallelProjection() == 0) {
227 double distance = p.length();
229 double fov = panel.getRenderer().GetActiveCamera().GetViewAngle();
230 float s = (float) (Math.sin(fov) * distance * 0.1);
232 Vector3d scale = new Vector3d(1., 1., 1.);
241 gizmo.setScale(scale);
244 Vector3d scale = new Vector3d(1.f, 1.f, 1.f);
245 double s = panel.getRenderer().GetActiveCamera().GetParallelScale() / 5.;
253 gizmo.setScale(scale);
260 protected Vector3d prevTranslate = null;
263 public boolean mousePressed(MouseEvent e) {
264 if (e.getButton() == MouseEvent.BUTTON1) {
267 prevTranslate = getTranslate(e.getX(), e.getY());
269 panel.getComponent().setCursor(dragCursor);
272 getDefaultAction().mousePressed(e);
273 panel.getComponent().setCursor(activeCursor);
276 getDefaultAction().mousePressed(e);
279 //index = gizmo.getTranslateAxis(actor);
282 // panel.getDefaultAction().mousePressed(e);
286 //prevTranslate = getTranslate(e.getX(), e.getY());
287 //System.out.println("start translate " + prevTranslate);
293 public boolean mouseReleased(MouseEvent e) {
294 if (e.getButton() == MouseEvent.BUTTON1) {
296 prevTranslate = null;
297 panel.getComponent().setCursor(activeCursor);
299 getDefaultAction().mouseReleased(e);
305 public boolean mouseDragged(MouseEvent e) {
306 if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) > 0 && valid) {
308 Vector3d translate = getTranslate(e.getX(), e.getY(), prevTranslate);
309 //System.out.println("translate " + translate);
310 if (translate == null)
312 boolean step = ((e.getModifiers() & MouseEvent.CTRL_MASK) > 0);
314 Vector3d pos = new Vector3d(node.getWorldPosition());
316 pos = constaints(pos, step);
319 Vector3d pos = new Vector3d(node.getPosition());
321 pos = constaints(pos, step);
324 //mapping.rangeModified(node);
326 //nodeMap.modified(node);
329 getDefaultAction().mouseDragged(e);
336 public boolean mouseWheelMoved(MouseWheelEvent e) {
337 return getDefaultAction().mouseWheelMoved(e);
340 protected void setPos(Vector3d pos) {
341 node.setPosition(pos);
344 protected void setWorldPos(Vector3d pos) {
345 node.setWorldPosition(pos);
348 private double istep = 10.0;
349 private int decimals = 2;
351 protected Vector3d constaints(Vector3d p, boolean step) {
356 p.x = Math.round(istep * p.x) / istep;
357 BigDecimal bx = new BigDecimal(p.x);
358 bx.setScale(decimals, BigDecimal.ROUND_HALF_UP);
359 p.x = bx.doubleValue();
362 p.y = Math.round(istep * p.y) / istep;
363 BigDecimal by = new BigDecimal(p.y);
364 by.setScale(decimals, BigDecimal.ROUND_HALF_UP);
365 p.y = by.doubleValue();
369 p.z = Math.round(istep * p.z) / istep;
370 BigDecimal bz = new BigDecimal(p.z);
371 bz.setScale(decimals, BigDecimal.ROUND_HALF_UP);
372 p.z = bz.doubleValue();
379 public boolean mouseMoved(MouseEvent e) {
380 getDefaultAction().mouseMoved(e);
384 protected Vector3d getTranslate(double x, double y) {
385 return getTranslate(x, y, new Vector3d());
388 protected Vector3d getTranslate(double x, double y, Vector3d offset) {
389 Vector3d translate = new Vector3d();
391 Ray ray = vtkUtil.createMouseRay(panel.getRenderer(),x, y);
393 Vector3d p = node.getWorldPosition();
398 Vector3d normal = new Vector3d(panel.getRenderer().GetActiveCamera().GetDirectionOfProjection());
400 MathTools.rotate(q, normal, normal);
403 double s[] = new double[1];
404 Vector3d r = new Vector3d();
405 if (MathTools.intersectStraightPlane(ray.pos, ray.dir, p, normal, r)) {
414 dir = new Vector3d(1.0,0.0,0.0);
416 MathTools.rotate(q, dir, dir);
417 Vector3d i1 = new Vector3d();
418 Vector3d i2 = new Vector3d();
420 MathTools.intersectStraightStraight( p, dir,ray.pos, ray.dir, i2, i1,s);
425 dir = new Vector3d(0.0,1.0,0.0);
427 MathTools.rotate(q, dir, dir);
431 MathTools.intersectStraightStraight( p, dir,ray.pos, ray.dir, i2, i1,s);
435 dir = new Vector3d(0.0,0.0,1.0);
437 MathTools.rotate(q, dir, dir);
441 MathTools.intersectStraightStraight( p, dir,ray.pos, ray.dir, i2, i1,s);
445 normal = new Vector3d(0.0,0.0,1.0);
447 MathTools.rotate(q, normal, normal);
449 if (MathTools.intersectStraightPlane(ray.pos, ray.dir, p, normal, r)) {
456 normal = new Vector3d(0.0,1.0,0.0);
458 MathTools.rotate(q, normal, normal);
460 if (MathTools.intersectStraightPlane(ray.pos, ray.dir, p, normal, r)) {
467 normal = new Vector3d(1.0,0.0,0.0);
469 MathTools.rotate(q, normal, normal);
471 if (MathTools.intersectStraightPlane(ray.pos, ray.dir, p, normal, r)) {
481 translate.sub(offset);