1 package org.simantics.plant3d.actions;
3 import java.awt.event.KeyEvent;
4 import java.awt.event.MouseEvent;
6 import javax.vecmath.Point3d;
7 import javax.vecmath.Vector3d;
9 import org.simantics.g3d.math.MathTools;
10 import org.simantics.g3d.math.Ray;
11 import org.simantics.g3d.scenegraph.IG3DNode;
12 import org.simantics.g3d.vtk.action.TranslateAction;
13 import org.simantics.g3d.vtk.common.InteractiveVtkPanel;
14 import org.simantics.g3d.vtk.common.VTKNodeMap;
15 import org.simantics.g3d.vtk.utils.vtkUtil;
16 import org.simantics.plant3d.Activator;
17 import org.simantics.plant3d.scenegraph.InlineComponent;
18 import org.simantics.plant3d.scenegraph.PipelineComponent;
19 import org.simantics.plant3d.scenegraph.controlpoint.PipeControlPoint.Direction;
21 public class TranslateInlineAction extends TranslateAction{
27 public TranslateInlineAction(InteractiveVtkPanel panel, VTKNodeMap nodeMap) {
28 super(panel, nodeMap);
29 setImageDescriptor(Activator.imageDescriptorFromPlugin("com.famfamfam.silk", "icons/arrow_refresh.png"));
33 public void setNode(IG3DNode node) {
35 if (node instanceof InlineComponent) {
36 InlineComponent comp = (InlineComponent)node;
37 if (comp.isVariableLength()) {
42 PipelineComponent prev = comp.getPrevious();
43 PipelineComponent next = comp.getNext();
44 if (prev == null || next == null) {
45 // TODO : we should support open ended translation (when translated component is the first or last of the run).
49 if (prev.getControlPoint().isInline() && !prev.getControlPoint().isFixed())
50 prev = prev.getPrevious();
51 if (next.getControlPoint().isInline() && !next.getControlPoint().isFixed()) {
52 next = next.getNext();
54 Point3d ns = new Point3d();
55 Point3d ne = new Point3d();
56 Point3d ps = new Point3d();
57 Point3d pe = new Point3d();
58 next.getControlPointEnds(ns, ne);
59 prev.getControlPointEnds(ps, pe);
60 dir = comp.getControlPoint().getPathLegDirection(Direction.NEXT);
62 // We may have offsets in the path leg, hence we have to project the coordinates.
63 Vector3d wp = node.getWorldPosition();
64 s = MathTools.closestPointOnStraight(pe, wp, dir);
65 e = MathTools.closestPointOnStraight(ns, wp, dir);
66 // Remove component's own space from end points to get actual movement range
67 double l = comp.getControlPoint().getInlineLength();
68 Vector3d ld = new Vector3d(dir);
79 public void keyPressed(KeyEvent e) {
80 if (e.getKeyCode() == KeyEvent.VK_ESCAPE)
81 panel.useDefaultAction();
89 public void setWorldCoord(boolean b) {
94 public void mouseDragged(MouseEvent e) {
95 if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) > 0 && valid) {
97 Vector3d translate = getTranslate(e.getX(), e.getY(), prevTranslate);
98 //System.out.println("translate " + translate);
99 if (translate == null)
101 //boolean step = ((e.getModifiers() & MouseEvent.CTRL_MASK) > 0);
102 Vector3d pos = new Vector3d(node.getWorldPosition());
103 //pos.add(translate);
105 //pos = constaints(pos, step);
108 //mapping.rangeModified(node);
110 //nodeMap.modified(node);
113 panel.getDefaultAction().mouseDragged(e);
120 protected Vector3d getTranslate(double x, double y, Vector3d offset) {
122 Ray ray = vtkUtil.createMouseRay(panel.GetRenderer(),x, y);
124 Vector3d p = node.getWorldPosition();
126 Vector3d i1 = new Vector3d();
127 Vector3d i2 = new Vector3d();
129 double mu[] = new double[2];
130 MathTools.intersectStraightStraight( p, dir,ray.pos, ray.dir, i2, i1,mu);
132 Vector3d t = MathTools.closestPointOnEdge(i1, s, e);