1 /*******************************************************************************
\r
2 * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.
\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
9 * VTT Technical Research Centre of Finland - initial API and implementation
\r
10 *******************************************************************************/
\r
11 package org.simantics.processeditor.actions;
\r
13 import java.util.List;
\r
15 import javax.vecmath.AxisAngle4f;
\r
16 import javax.vecmath.Point3d;
\r
17 import javax.vecmath.Vector3d;
\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
40 public class TranslateElbowAction extends ConstrainedTransformAction {
\r
42 TransformInlineGizmo gizmo;
\r
43 List<IGraphicsNode> mos;
\r
46 double istep = 10.0;
\r
49 private Resource pcpResource;
\r
51 public TranslateElbowAction(ThreeDimensionalEditorBase parent) {
\r
53 gizmo = new TransformInlineGizmo(component.getDisplaySystem().getRenderer());
\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
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
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
82 dcp = new DirectedControlPoint(prev);
\r
84 if (next != null && next.isInstanceOf(ProcessResource.plant3Dresource.DirectedControlPoint)) {
\r
86 dcp = new DirectedControlPoint(next);
\r
88 if (directedCount == 1) {
\r
89 orgPos = G3DTools.getVector(dcp.getWorldPosition());
\r
90 dir = ControlPointTools.getDirectedControlPointDirection(dcp);
\r
91 pcpResource = pcp.getResource();
\r
100 public void deactivate() {
\r
101 parent.setGizmo(null);
\r
102 super.deactivate();
\r
106 public void activate() {
\r
107 parent.setGizmo(gizmo);
\r
110 mos = parent.getSelectionAdapter().getSelectedObjects();
\r
111 // for (IGraphicsNode mo : mos) {
\r
112 // text += GraphicsNodeTools.getWorldTranslation(mo.getGraphicsNode());//mo.getWorldPosition() + " ";
\r
115 mos.iterator().next().getGroup().attachChild(gizmo.getNode());
\r
116 setInfoText(text);
\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
122 if (angle < 0.01 || (Math.PI - angle) < 0.01) {
\r
123 aa = new AxisAngle4f();
\r
125 current.normalize();
\r
126 Vector3d right = new Vector3d();
\r
127 right.cross(front, current);
\r
130 if (right.lengthSquared() < 0.01) {
\r
131 aa = new AxisAngle4f();
\r
133 aa = new AxisAngle4f((float) right.x, (float) right.y, (float) right.z, angle);
\r
136 gizmo.setRotation(aa);
\r
139 TranslateActionConstraints.addConstraints(new Resource[]{pcpResource}, detector);
\r
140 parent.setViewChanged(true);
\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
154 Vector3d i1 = new Vector3d();
\r
155 Vector3d i2 = new Vector3d();
\r
157 MathTools.intersectStraightStraight(orgPos, dir,o, d, i2, i1,s);
\r
158 translate.set(dir);
\r
162 translate.scale(s[0]);
\r
163 translate.add(orgPos);
\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
179 Vector3d prevTranslate = new Vector3d();
\r
182 public void doChanges(Graph g) throws Exception {
\r
183 if (input.mousePressed()) {
\r
184 //prevTranslate = getTranslate();
\r
187 if (input.mouseClicked()) {
\r
191 if (!input.mouseDragged()) {
\r
192 parent.getDefaultAction().update();
\r
195 parent.setViewChanged(true);
\r
198 List<IGraphicsNode> mos = parent.getSelectionAdapter().getSelectedObjects();
\r
199 Vector3d translate = getTranslate();
\r
201 if (translate == null) {
\r
202 //cameraRotateAction.update();
\r
203 parent.getDefaultAction().update();
\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
224 protected void updateGizmo() {
\r
225 gizmo.update(camera.getCameraPos(),component);
\r
228 public void setInfoText(String text) {
\r