]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.proconf.processeditor/src/org/simantics/processeditor/actions/TranslateElbowAction.java
Set copyright texts for java files in the latest development branches.
[simantics/3d.git] / org.simantics.proconf.processeditor / src / org / simantics / processeditor / actions / TranslateElbowAction.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007- VTT Technical Research Centre of Finland.\r
3  * All rights reserved. This program and the accompanying materials\r
4  * are made available under the terms of the Eclipse Public License v1.0\r
5  * which accompanies this distribution, and is available at\r
6  * http://www.eclipse.org/legal/epl-v10.html\r
7  *\r
8  * Contributors:\r
9  *     VTT Technical Research Centre of Finland - initial API and implementation\r
10  *******************************************************************************/\r
11 package org.simantics.processeditor.actions;\r
12 \r
13 import java.util.List;\r
14 \r
15 import javax.vecmath.AxisAngle4f;\r
16 import javax.vecmath.Point3d;\r
17 import javax.vecmath.Vector3d;\r
18 \r
19 import org.simantics.db.Graph;\r
20 import org.simantics.db.Resource;\r
21 import org.simantics.layer0.utils.EntityFactory;\r
22 import org.simantics.layer0.utils.IEntity;\r
23 import org.simantics.processeditor.Activator;\r
24 import org.simantics.processeditor.ProcessResource;\r
25 import org.simantics.processeditor.common.ControlPointTools;\r
26 import org.simantics.processeditor.stubs.DirectedControlPoint;\r
27 import org.simantics.processeditor.stubs.PipeControlPoint;\r
28 import org.simantics.processeditor.stubs.VariableAngleTurnComponent;\r
29 import org.simantics.proconf.g3d.actions.ConstrainedTransformAction;\r
30 import org.simantics.proconf.g3d.actions.TranslateActionConstraints;\r
31 import org.simantics.proconf.g3d.base.G3DAPI;\r
32 import org.simantics.proconf.g3d.base.G3DTools;\r
33 import org.simantics.proconf.g3d.base.MathTools;\r
34 import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase;\r
35 import org.simantics.proconf.g3d.gizmo.TransformInlineGizmo;\r
36 import org.simantics.proconf.g3d.scenegraph.IGraphicsNode;\r
37 import org.simantics.proconf.g3d.stubs.G3DNode;\r
38 \r
39 \r
40 public class TranslateElbowAction extends ConstrainedTransformAction {\r
41 \r
42     TransformInlineGizmo gizmo;\r
43     List<IGraphicsNode> mos;\r
44     Vector3d dir;\r
45     Vector3d orgPos;\r
46     double istep = 10.0;\r
47     int decimals = 2;\r
48     \r
49     private Resource pcpResource;\r
50     \r
51     public TranslateElbowAction(ThreeDimensionalEditorBase parent) {\r
52         super(parent);\r
53         gizmo = new TransformInlineGizmo(component.getDisplaySystem().getRenderer());\r
54     \r
55         \r
56     }\r
57     \r
58     public void init() {\r
59         this.setText("Translate directed");\r
60         this.setToolTipText("Translate the elbow in connections direction");\r
61         this.setImageDescriptor(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/translate_d.png"));\r
62     }\r
63     \r
64     \r
65     \r
66     @Override\r
67     public boolean usable(Graph graph, List<Resource> resources) {\r
68         // TODO : it should be possible to move multiple components on the same straight\r
69         // TODO : checking against elbow and dcp; these are not correct!\r
70         if (resources.size() != 1)\r
71             return false;\r
72         IEntity r = EntityFactory.create(graph,resources.get(0));\r
73         if (r.isInstanceOf(ProcessResource.plant3Dresource.VariableAngleTurnComponent)) {\r
74                 VariableAngleTurnComponent e = new VariableAngleTurnComponent(r);\r
75                 PipeControlPoint pcp = e.getControlPoint();\r
76                 PipeControlPoint prev = ControlPointTools.findPreviousEnd(pcp);\r
77                 PipeControlPoint next = ControlPointTools.findNextEnd(pcp);\r
78                 DirectedControlPoint dcp = null;\r
79                 int directedCount = 0;\r
80                 if (prev != null && prev.isInstanceOf(ProcessResource.plant3Dresource.DirectedControlPoint)) {\r
81                         directedCount++;\r
82                         dcp = new DirectedControlPoint(prev);\r
83                 }\r
84                 if (next != null && next.isInstanceOf(ProcessResource.plant3Dresource.DirectedControlPoint)) {\r
85                         directedCount++;\r
86                         dcp = new DirectedControlPoint(next);\r
87                 }\r
88                 if (directedCount == 1) {\r
89                         orgPos = G3DTools.getVector(dcp.getWorldPosition());\r
90                         dir = ControlPointTools.getDirectedControlPointDirection(dcp);\r
91                         pcpResource = pcp.getResource();\r
92                         return true;\r
93                 }\r
94                 } \r
95                 return false;\r
96 \r
97     }\r
98     \r
99     @Override\r
100     public void deactivate() {\r
101         parent.setGizmo(null);\r
102         super.deactivate();\r
103     }\r
104 \r
105     @Override\r
106     public void activate() {\r
107         parent.setGizmo(gizmo);\r
108 \r
109         String text = "";\r
110         mos = parent.getSelectionAdapter().getSelectedObjects();\r
111 //        for (IGraphicsNode mo : mos) {\r
112 //            text += GraphicsNodeTools.getWorldTranslation(mo.getGraphicsNode());//mo.getWorldPosition() + " ";\r
113 //        }\r
114 \r
115         mos.iterator().next().getGroup().attachChild(gizmo.getNode());\r
116         setInfoText(text);    \r
117            \r
118         Vector3d front = new Vector3d(1.0,0.0,0.0);\r
119         Vector3d current = new Vector3d(dir);\r
120         float angle = (float)current.angle(front);\r
121         AxisAngle4f aa;\r
122         if (angle < 0.01 || (Math.PI - angle) < 0.01) {\r
123             aa = new AxisAngle4f();\r
124         } else {\r
125             current.normalize();\r
126             Vector3d right = new Vector3d();\r
127             right.cross(front, current);\r
128 \r
129             right.normalize();\r
130             if (right.lengthSquared() < 0.01) {\r
131                 aa = new AxisAngle4f();\r
132             } else {\r
133                 aa = new AxisAngle4f((float) right.x, (float) right.y, (float) right.z, angle);\r
134             }\r
135         }\r
136         gizmo.setRotation(aa);\r
137         \r
138         updateGizmo();\r
139         TranslateActionConstraints.addConstraints(new Resource[]{pcpResource}, detector);\r
140         parent.setViewChanged(true);\r
141     }\r
142     \r
143     \r
144     \r
145     Vector3d getTranslate() {\r
146         Vector3d translate = new Vector3d();\r
147         Vector3d o = new Vector3d();\r
148         Vector3d d = new Vector3d();\r
149         parent.createPickRay(o, d);\r
150         //Vector3d p = gizmo.getPosition();\r
151         if (gizmo.isSelected()) {\r
152             double s[] = new double[1];\r
153            \r
154             Vector3d i1 = new Vector3d();\r
155             Vector3d i2 = new Vector3d();\r
156             s = new double[2];\r
157             MathTools.intersectStraightStraight(orgPos, dir,o, d, i2, i1,s);\r
158             translate.set(dir);\r
159             if (s[0] < 0.0)\r
160                 s[0] = 0.0;\r
161             \r
162             translate.scale(s[0]);\r
163             translate.add(orgPos);\r
164             \r
165             if (useConstraints) {\r
166                 Vector3d t = new Vector3d(translate);\r
167 //              FIXME : snapped point may be outside of proper range\r
168                 Point3d snap = detector.getPointSnap2(t, dir);\r
169                 if (snap != null) {\r
170                     translate = new Vector3d(snap);\r
171                 }\r
172             }\r
173         \r
174             return translate;\r
175         }\r
176         return null;\r
177     }\r
178     \r
179     Vector3d prevTranslate = new Vector3d();\r
180     \r
181     @Override\r
182     public void doChanges(Graph g) throws Exception {\r
183         if (input.mousePressed()) {\r
184             //prevTranslate = getTranslate();\r
185             \r
186         }\r
187         if (input.mouseClicked()) {\r
188             end();\r
189             return;\r
190         }\r
191         if (!input.mouseDragged()) {\r
192             parent.getDefaultAction().update();\r
193             return;\r
194         }   \r
195         parent.setViewChanged(true);\r
196         \r
197         \r
198         List<IGraphicsNode> mos = parent.getSelectionAdapter().getSelectedObjects();\r
199         Vector3d translate = getTranslate();\r
200        \r
201         if (translate == null) {\r
202             //cameraRotateAction.update();\r
203             parent.getDefaultAction().update();\r
204             updateGizmo();\r
205             return;\r
206         }\r
207 \r
208         String text = "";\r
209                 for (IGraphicsNode mo : mos) {\r
210                         G3DNode node = mo.getG3DNode(g);\r
211                         G3DAPI.setWorldPosition(node, translate);\r
212                         //G3DTools.setTuple3(node.getW, translation)\r
213                         //G3DTools.setLocalTranslation(node,translate);\r
214                         // mo.setLocalTranslation(translate);\r
215                         text += G3DTools.getVector(node.getWorldPosition()) + " " + translate;// mo.getWorldPosition() + " " +\r
216                                                                         \r
217                 }\r
218                 setInfoText(text);\r
219 \r
220         updateGizmo();\r
221 \r
222     }\r
223     \r
224     protected void updateGizmo() {\r
225         gizmo.update(camera.getCameraPos(),component);\r
226     }\r
227     \r
228     public void setInfoText(String text) {\r
229         \r
230     }\r
231 \r
232 }