/******************************************************************************* * 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.awt.event.MouseEvent; import java.util.List; import javax.vecmath.Point3d; import javax.vecmath.Vector3d; import org.simantics.db.Graph; import org.simantics.db.Resource; import org.simantics.proconf.g3d.actions.InteractiveAction; import org.simantics.proconf.g3d.base.MathTools; import org.simantics.proconf.g3d.base.ThreeDimensionalEditorBase; import org.simantics.proconf.g3d.base.VecmathJmeTools; import com.jme.math.Vector3f; import com.jme.renderer.ColorRGBA; import com.jme.scene.Line; import com.jme.scene.state.MaterialState; import com.jme.util.geom.BufferUtils; public class SelectSplitPointAction extends InteractiveAction { Point3d start; Point3d end; Vector3d dir; Line line; boolean activated; SplitPointListener listener; public SelectSplitPointAction(ThreeDimensionalEditorBase parent, SplitPointListener listener) { super(parent); this.listener = listener; } public void setSplit(Point3d start, Point3d end) { this.start = start; this.end = end; dir = new Vector3d(end); dir.sub(start); } @Override public void activate() { if (start == null) throw new RuntimeException("Starting split action without information about range"); // start = new Point3d(); // end = new Point3d(); // PipingTools.getStraightPipeEnds(PipingTools.getPipeline(straight), // straight, startStart, startEnd); line = new Line(); MaterialState ms = parent.getRenderingComponent().getDisplaySystem().getRenderer().createMaterialState(); ms.setEmissive(new ColorRGBA(1.f,1.f,1.f,1.f)); line.setRenderState(ms); parent.getRenderingComponent().getNoShadowRoot().attachChild(line); activated = true; } @Override public boolean usable(Graph graph, List resources) { // if (resources.size() != 1) // return false; // IEntity entity = EntityFactory.create(graph, resources.get(0)); // if (entity.isInstanceOf(ProcessResource.plant3Dresource.VariableLengthInlineComponent)) { // return true; // } // This is not a standalone action return false; } @Override public void deactivate() { line.removeFromParent(); activated = false; start = null; end = null; dir = null; } public boolean active() { return activated; } @Override public void update() { if (!activated) { return; } if (input.mouseClicked() && input.clickButton() == MouseEvent.BUTTON3) { listener.setSplitPoint(null); activated = false; return; } Vector3d o = new Vector3d(); Vector3d d = new Vector3d(); parent.createPickRay(o, d); Vector3d normal = parent.getCamera().getUnNormalizedHeading(); normal.normalize(); normal.negate(); // reference point for selection line Vector3d point = new Vector3d(start); Vector3d currentPoint = new Vector3d(); MathTools.intersectStraightPlane(o, d, point, normal, currentPoint); Point3d startPoint = new Point3d(); double mu[] = new double[2]; MathTools.intersectStraightStraight(start, dir, o, d, startPoint, new Point3d(), mu); // startPoint of branch must be between pipe ends // TODO : take account sizes of elbows (or other components) if (mu[0] < 0.0) startPoint.set(start); else if (mu[0] > 1.0) startPoint.set(end); Vector3f verts[] = new Vector3f[2]; verts[0] = VecmathJmeTools.get(startPoint); verts[1] = VecmathJmeTools.get(currentPoint); line.reconstruct(BufferUtils.createFloatBuffer(verts), null, null, null); parent.setViewChanged(true); if (input.mouseClicked() && input.clickButton() == MouseEvent.BUTTON1) { listener.setSplitPoint(new Point3d(startPoint)); } } }