/******************************************************************************* * Copyright (c) 2007- VTT Technical Research Centre of Finland. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.processeditor.actions; import java.util.List; import javax.vecmath.AxisAngle4f; import javax.vecmath.Point3d; import javax.vecmath.Vector3d; import org.simantics.db.Graph; import org.simantics.db.Resource; import org.simantics.layer0.utils.EntityFactory; import org.simantics.layer0.utils.IEntity; import org.simantics.processeditor.Activator; import org.simantics.processeditor.ProcessResource; import org.simantics.processeditor.common.ControlPointTools; import org.simantics.processeditor.stubs.DirectedControlPoint; import org.simantics.processeditor.stubs.PipeControlPoint; import org.simantics.processeditor.stubs.VariableAngleTurnComponent; import org.simantics.proconf.g3d.actions.ConstrainedTransformAction; import org.simantics.proconf.g3d.actions.TranslateActionConstraints; import org.simantics.proconf.g3d.base.G3DAPI; import org.simantics.proconf.g3d.base.G3DTools; import org.simantics.proconf.g3d.base.MathTools; import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase; import org.simantics.proconf.g3d.gizmo.TransformInlineGizmo; import org.simantics.proconf.g3d.scenegraph.IGraphicsNode; import org.simantics.proconf.g3d.stubs.G3DNode; public class TranslateElbowAction extends ConstrainedTransformAction { TransformInlineGizmo gizmo; List mos; Vector3d dir; Vector3d orgPos; double istep = 10.0; int decimals = 2; private Resource pcpResource; public TranslateElbowAction(ThreeDimensionalEditorBase parent) { super(parent); gizmo = new TransformInlineGizmo(component.getDisplaySystem().getRenderer()); } public void init() { this.setText("Translate directed"); this.setToolTipText("Translate the elbow in connections direction"); this.setImageDescriptor(Activator.imageDescriptorFromPlugin(Activator.PLUGIN_ID, "icons/translate_d.png")); } @Override public boolean usable(Graph graph, List resources) { // TODO : it should be possible to move multiple components on the same straight // TODO : checking against elbow and dcp; these are not correct! if (resources.size() != 1) return false; IEntity r = EntityFactory.create(graph,resources.get(0)); if (r.isInstanceOf(ProcessResource.plant3Dresource.VariableAngleTurnComponent)) { VariableAngleTurnComponent e = new VariableAngleTurnComponent(r); PipeControlPoint pcp = e.getControlPoint(); PipeControlPoint prev = ControlPointTools.findPreviousEnd(pcp); PipeControlPoint next = ControlPointTools.findNextEnd(pcp); DirectedControlPoint dcp = null; int directedCount = 0; if (prev != null && prev.isInstanceOf(ProcessResource.plant3Dresource.DirectedControlPoint)) { directedCount++; dcp = new DirectedControlPoint(prev); } if (next != null && next.isInstanceOf(ProcessResource.plant3Dresource.DirectedControlPoint)) { directedCount++; dcp = new DirectedControlPoint(next); } if (directedCount == 1) { orgPos = G3DTools.getVector(dcp.getWorldPosition()); dir = ControlPointTools.getDirectedControlPointDirection(dcp); pcpResource = pcp.getResource(); return true; } } return false; } @Override public void deactivate() { parent.setGizmo(null); super.deactivate(); } @Override public void activate() { parent.setGizmo(gizmo); String text = ""; mos = parent.getSelectionAdapter().getSelectedObjects(); // for (IGraphicsNode mo : mos) { // text += GraphicsNodeTools.getWorldTranslation(mo.getGraphicsNode());//mo.getWorldPosition() + " "; // } mos.iterator().next().getGroup().attachChild(gizmo.getNode()); setInfoText(text); Vector3d front = new Vector3d(1.0,0.0,0.0); Vector3d current = new Vector3d(dir); float angle = (float)current.angle(front); AxisAngle4f aa; if (angle < 0.01 || (Math.PI - angle) < 0.01) { aa = new AxisAngle4f(); } else { current.normalize(); Vector3d right = new Vector3d(); right.cross(front, current); right.normalize(); if (right.lengthSquared() < 0.01) { aa = new AxisAngle4f(); } else { aa = new AxisAngle4f((float) right.x, (float) right.y, (float) right.z, angle); } } gizmo.setRotation(aa); updateGizmo(); TranslateActionConstraints.addConstraints(new Resource[]{pcpResource}, detector); parent.setViewChanged(true); } Vector3d getTranslate() { Vector3d translate = new Vector3d(); Vector3d o = new Vector3d(); Vector3d d = new Vector3d(); parent.createPickRay(o, d); //Vector3d p = gizmo.getPosition(); if (gizmo.isSelected()) { double s[] = new double[1]; Vector3d i1 = new Vector3d(); Vector3d i2 = new Vector3d(); s = new double[2]; MathTools.intersectStraightStraight(orgPos, dir,o, d, i2, i1,s); translate.set(dir); if (s[0] < 0.0) s[0] = 0.0; translate.scale(s[0]); translate.add(orgPos); if (useConstraints) { Vector3d t = new Vector3d(translate); // FIXME : snapped point may be outside of proper range Point3d snap = detector.getPointSnap2(t, dir); if (snap != null) { translate = new Vector3d(snap); } } return translate; } return null; } Vector3d prevTranslate = new Vector3d(); @Override public void doChanges(Graph g) throws Exception { if (input.mousePressed()) { //prevTranslate = getTranslate(); } if (input.mouseClicked()) { end(); return; } if (!input.mouseDragged()) { parent.getDefaultAction().update(); return; } parent.setViewChanged(true); List mos = parent.getSelectionAdapter().getSelectedObjects(); Vector3d translate = getTranslate(); if (translate == null) { //cameraRotateAction.update(); parent.getDefaultAction().update(); updateGizmo(); return; } String text = ""; for (IGraphicsNode mo : mos) { G3DNode node = mo.getG3DNode(g); G3DAPI.setWorldPosition(node, translate); //G3DTools.setTuple3(node.getW, translation) //G3DTools.setLocalTranslation(node,translate); // mo.setLocalTranslation(translate); text += G3DTools.getVector(node.getWorldPosition()) + " " + translate;// mo.getWorldPosition() + " " + } setInfoText(text); updateGizmo(); } protected void updateGizmo() { gizmo.update(camera.getCameraPos(),component); } public void setInfoText(String text) { } }