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;
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.simantics.g3d.math.MathTools;
25 import org.simantics.g3d.math.Ray;
26 import org.simantics.g3d.scenegraph.IG3DNode;
27 import org.simantics.g3d.scenegraph.structural.IStructuralNode;
28 import org.simantics.g3d.vtk.Activator;
29 import org.simantics.g3d.vtk.common.VTKNodeMap;
30 import org.simantics.g3d.vtk.gizmo.TranslateAxisGizmo;
31 import org.simantics.g3d.vtk.utils.vtkUtil;
32 import org.simantics.utils.threads.AWTThread;
33 import org.simantics.utils.threads.ThreadUtils;
37 public class TranslateAction extends vtkAwtAction{
39 public static final int X = 0;
40 public static final int Y = 1;
41 public static final int Z = 2;
42 public static final int XY = 3;
43 public static final int XZ = 4;
44 public static final int YZ = 5;
45 public static final int P = 6;
47 private VTKNodeMap nodeMap;
48 //private TranslateGizmo gizmo = new TranslateGizmo();
49 private TranslateAxisGizmo gizmo = new TranslateAxisGizmo();
50 protected IG3DNode node;
54 private Cursor activeCursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
55 private Cursor dragCursor = Cursor.getPredefinedCursor(Cursor.MOVE_CURSOR);
57 public void setNode(IG3DNode node) {
59 if ((node instanceof IStructuralNode) && ((IStructuralNode)node).isPartOfInstantiatedModel() && !((IStructuralNode)node).isInstantiatedModelRoot()) {
66 public IG3DNode getNode() {
70 public TranslateAction(InteractiveVtkPanel panel, VTKNodeMap nodeMap) {
72 setImageDescriptor(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/arrow_out.png"));
74 this.nodeMap = nodeMap;
77 public void attach() {
82 ThreadUtils.asyncExec(AWTThread.getThreadAccess(), new Runnable() {
93 public void deattach() {
96 nodeMap.commit("Translate");
102 private void attachUI() {
103 panel.setCursor(activeCursor);
107 private void deattachUI() {
108 panel.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
113 public void keyPressed(KeyEvent e) {
114 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
115 panel.useDefaultAction();
118 if (e.getKeyCode() == KeyEvent.VK_X) {
124 if (e.getKeyCode() == KeyEvent.VK_Y) {
130 if (e.getKeyCode() == KeyEvent.VK_Z) {
136 if (e.getKeyCode() == KeyEvent.VK_G) {
137 worldCoord = !worldCoord;
139 gizmo.setType(index);
146 public void keyReleased(KeyEvent e) {
151 public void keyTyped(KeyEvent e) {
156 public void mouseClicked(MouseEvent e) {
157 if (e.getClickCount() > 1) {
161 panel.useDefaultAction();
163 //if(!gizmo.isPartOf(actor))
164 // 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])))
181 public void mouseEntered(MouseEvent e) {
186 public void mouseExited(MouseEvent e) {
191 protected boolean valid = false;
192 private boolean worldCoord = true;
193 private AxisAngle4d aa = null;
194 private Quat4d q = null;
197 public void setWorldCoord(boolean b) {
206 protected void update() {
210 gizmo.setRotation(new AxisAngle4d());
214 aa = new AxisAngle4d();
215 aa.set(((IG3DNode)node.getParent()).getWorldOrientation());
216 gizmo.setRotation(aa);
218 MathTools.getQuat(aa, q);
221 Vector3d nodePos = node.getWorldPosition();
222 //System.out.println(nodePos);
223 gizmo.setPosition(nodePos);
226 Point3d camPos = new Point3d(panel.GetRenderer().GetActiveCamera().GetPosition());
227 Vector3d p = new Vector3d(nodePos);
231 Quat4d qi = new Quat4d(q);
233 MathTools.rotate(q, p, p);
235 if (panel.GetRenderer().GetActiveCamera().GetParallelProjection() == 0) {
236 double distance = p.length();
238 double fov = panel.GetRenderer().GetActiveCamera().GetViewAngle();
239 float s = (float) (Math.sin(fov) * distance * 0.1);
241 Vector3d scale = new Vector3d(1., 1., 1.);
250 gizmo.setScale(scale);
253 Vector3d scale = new Vector3d(1.f, 1.f, 1.f);
254 double s = panel.GetRenderer().GetActiveCamera().GetParallelScale() / 5.;
262 gizmo.setScale(scale);
269 protected Vector3d prevTranslate = null;
272 public void mousePressed(MouseEvent e) {
273 if (e.getButton() == MouseEvent.BUTTON1) {
276 prevTranslate = getTranslate(e.getX(), e.getY());
278 panel.setCursor(dragCursor);
281 getDefaultAction().mousePressed(e);
282 panel.setCursor(activeCursor);
285 getDefaultAction().mousePressed(e);
287 //index = gizmo.getTranslateAxis(actor);
290 // panel.getDefaultAction().mousePressed(e);
294 //prevTranslate = getTranslate(e.getX(), e.getY());
295 //System.out.println("start translate " + prevTranslate);
301 public void mouseReleased(MouseEvent e) {
302 if (e.getButton() == MouseEvent.BUTTON1) {
304 prevTranslate = null;
305 panel.setCursor(activeCursor);
307 getDefaultAction().mouseReleased(e);
312 public void mouseDragged(MouseEvent e) {
313 if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) > 0 && valid) {
315 Vector3d translate = getTranslate(e.getX(), e.getY(), prevTranslate);
316 //System.out.println("translate " + translate);
317 if (translate == null)
319 boolean step = ((e.getModifiers() & MouseEvent.CTRL_MASK) > 0);
321 Vector3d pos = new Vector3d(node.getWorldPosition());
323 pos = constaints(pos, step);
326 Vector3d pos = new Vector3d(node.getPosition());
328 pos = constaints(pos, step);
331 //mapping.rangeModified(node);
333 //nodeMap.modified(node);
336 getDefaultAction().mouseDragged(e);
341 protected void setPos(Vector3d pos) {
342 node.setPosition(pos);
345 protected void setWorldPos(Vector3d pos) {
346 node.setWorldPosition(pos);
349 private double istep = 10.0;
350 private int decimals = 2;
352 protected Vector3d constaints(Vector3d p, boolean step) {
357 p.x = Math.round(istep * p.x) / istep;
358 BigDecimal bx = new BigDecimal(p.x);
359 bx.setScale(decimals, BigDecimal.ROUND_HALF_UP);
360 p.x = bx.doubleValue();
363 p.y = Math.round(istep * p.y) / istep;
364 BigDecimal by = new BigDecimal(p.y);
365 by.setScale(decimals, BigDecimal.ROUND_HALF_UP);
366 p.y = by.doubleValue();
370 p.z = Math.round(istep * p.z) / istep;
371 BigDecimal bz = new BigDecimal(p.z);
372 bz.setScale(decimals, BigDecimal.ROUND_HALF_UP);
373 p.z = bz.doubleValue();
380 public void mouseMoved(MouseEvent e) {
381 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);