- }
-
- private PipelineComponent getLast() {
- if (added.size() == 0)
- return startComponent;
- return added.get(added.size()-1);
- }
-
-
- /**
- * Removes last point from pipeline
- */
- public void removePoint() {
- if (added.size() < 3)
- return;
- InlineComponent straight = (InlineComponent)added.remove(added.size()-1);
- TurnComponent turn = (TurnComponent)added.remove(added.size()-1);
- straight.getControlPoint().remove();
- turn.getControlPoint().remove();
- if (added.size() > 1) {
- setPreviousPosition(added.get(added.size()-2).getWorldPosition());
- } else {
- setPreviousPosition(startComponent.getWorldPosition());
- if (direction != null)
- setLockType(LockType.CUSTOM, true);
- }
-
- }
-
- private void endPiping() throws Exception {
- state = ToolState.NOT_ACTIVE;
-
- if (endTo != null) {
- PipeControlPoint endCP = endTo.getControlPoint();
- if (endType == null || endType == PositionType.NEXT || endType == PositionType.PREVIOUS) {
-
- PipelineComponent current = getLast();
- PipeControlPoint currentCP = current.getControlPoint();
-
- boolean requiresReverse = false;
- if (!reversed && endCP.getPrevious() != null) {
- requiresReverse = true;
- } else if (reversed && endCP.getNext() != null) {
- requiresReverse = true;
- }
- PipeRun other = endCP.getPipeRun();
- boolean mergeRuns = pipeRun.equalSpecs(other);
-
- if (requiresReverse) {
- // Pipe line must be traversible with next/previous relations without direction change.
- // Now the component, where we are connecting the created pipeline is defined in different order.
- PipingRules.reverse(other);
-
- }
- if (mergeRuns) {
- // Runs have compatible specs and must be merged
- if (pipeRun != other) // FIXME: temporary workaround.
- PipingRules.merge(pipeRun, other);
- if (!reversed) {
- currentCP.setNext(endCP);
- endCP.setPrevious(currentCP);
- } else {
- currentCP.setPrevious(endCP);
- endCP.setNext(currentCP);
- }
- } else {
- // Runs do not have compatible specs, and a reducer must be attached in between.
- InlineComponent reducer = ComponentUtils.createReducer(root);
- PipeControlPoint pcp = reducer.getControlPoint();
- PipeControlPoint ocp = pcp.getSubPoint().get(0);
-
- Vector3d endPos = endCP.getWorldPosition();
- Vector3d currentPos = currentCP.getWorldPosition();
- Vector3d v = new Vector3d(endPos);
- v.sub(currentPos);
- v.scale(0.5);
- v.add(currentPos);
-
- PipingRules.addSizeChange(reversed, pipeRun, other, reducer, currentCP, endCP);
-
- pcp.setWorldPosition(v);
- reducer.updateParameters();
- }
-
- } else if (endType == PositionType.SPLIT) {
- InlineComponent branchSplit = createBranchSplit((InlineComponent)endTo, currentPosition);
- PipeControlPoint branchSplitCP = branchSplit.getControlPoint();
- PipeControlPoint pcp = new PipeControlPoint(branchSplit,pipeRun);
- branchSplitCP.children.add(pcp);
- pcp.parent = branchSplitCP;
- pcp.setWorldOrientation(branchSplitCP.getWorldOrientation());
- pcp.setWorldPosition(branchSplitCP.getWorldPosition());
-
- PipelineComponent current = getLast();
- PipeControlPoint currentCP = current.getControlPoint();
-
-
- if(!reversed) {
- pcp.setPrevious(currentCP);
- currentCP.setNext(pcp);
- } else {
- pcp.setNext(currentCP);
- currentCP.setPrevious(pcp);
- }
-
- }
- PipingRules.positionUpdate(endCP);
- }
- panel.useDefaultAction();
- }
+
+ }
+ return PositionType.SPLIT;
+
+ }
+
+ private PipeControlPoint endingToComponent(INode componentNode, Vector3d o, Vector3d d) {
+ PipelineComponent component = (PipelineComponent)componentNode;
+ PipeControlPoint pcp = component.getControlPoint();
+ if (component instanceof EndComponent) {
+ if (pcp.getNext() != null || pcp.getPrevious() != null)
+ return null;
+ return pcp;
+ } else if (component instanceof TurnComponent) {
+ if (pcp.getNext() == null || pcp.getPrevious() == null)
+ return pcp;
+ return null;
+ } else if (component instanceof InlineComponent) {
+ // TODO : scan all empty pcps of the component and select closest one.
+ if (pcp.getNext() == null || pcp.getPrevious() == null)
+ return pcp;
+ return null;
+ }
+
+ return null;
+ }
+
+ private PositionType endingLockToStraight(INode straightNode, double mu[]) {
+ InlineComponent s = (InlineComponent)straightNode;
+ Point3d sStart = new Point3d();
+ Point3d sEnd = new Point3d();
+ s.getControlPoint().getInlineControlPointEnds(sStart, sEnd);
+ Vector3d sDir = new Vector3d(sEnd);
+ sDir.sub(sStart);
+ Vector3d dir = new Vector3d(currentPosition);
+ Point3d prev = new Point3d(previousPosition);
+ dir.sub(prev);
+ // intersection point in pipe where branch would be inserted to
+ Vector3d branchPoint = new Vector3d();
+ // intersection point in straight pipe that is currently routed
+ Vector3d routePoint = new Vector3d();
+ MathTools.intersectStraightStraight(sStart, sDir, new Vector3d(prev), dir, branchPoint, routePoint, mu);
+ routePoint.sub(branchPoint);
+ // startPoint of branch must be between pipe ends
+ // TODO : take account sizes of elbows (or other components)
+ // branch point must be between pipe ends and intersection points must be quite close to each other
+ if (mu[0] > 0.0 && mu[0] < 1.0 && routePoint.lengthSquared() < BRANCH_SNAP_DISTANCE) {
+ currentPosition.set(branchPoint);
+
+ updateCurrentPoint();
+
+ setInfoText("Make branch (l) :" + currentPosition + " " + Math.max(0.0, Math.min(mu[0], 1.0)) + " " + routePoint.lengthSquared());
+ return PositionType.SPLIT;
+ }
+ return null;
+ }
+
+ private boolean endingLockToNozzle(INode nozzleNode) {
+ Nozzle nozzle = (Nozzle)nozzleNode;
+ Vector3d dir = new Vector3d(currentPosition);
+ Point3d prev = new Point3d(previousPosition);
+ dir.sub(prev);
+ Vector3d nozzleLoc = nozzle.getWorldPosition();
+ double u[] = new double[1];
+ Vector3d closest = MathTools.closestPointOnStraight(new Point3d(nozzleLoc), new Point3d(prev), new Vector3d(dir), u);
+ double dist = MathTools.distanceSquared(nozzleLoc,closest);
+ if (dist < BRANCH_SNAP_DISTANCE) {
+ // FIXME : directions should be checked (insert an elbow)
+ currentPosition.set(nozzleLoc);
+ updateCurrentPoint();
+ setInfoText("Connect to nozzle (l) :" + currentPosition);
+ return true;
+ }
+ //System.out.println(u[0]);
+ return false;
+ }
+
+ private PipeControlPoint endingLockToComponent(INode componentNode) {
+ // we'll must scan all free pcp's and their direction to accept the connection.
+ return null;
+ }
+
+ private void addPoint() throws Exception {
+ InlineComponent previous = (InlineComponent)getLast();
+ PipeControlPoint previousCP = previous.getControlPoint();
+ TurnComponent turn = ComponentUtils.createTurn(root);
+ InlineComponent straight = ComponentUtils.createStraight(root);
+ PipeControlPoint turnCP = turn.getControlPoint();
+ PipeControlPoint straightCP = straight.getControlPoint();
+ straight.setName(pipeRun.getUniqueName("Pipe"));
+ turn.setName(pipeRun.getUniqueName("Elbow"));
+ pipeRun.addChild(turn);
+ pipeRun.addChild(straight);
+ added.add(turn);
+ added.add(straight);
+
+ turnCP.setDeletable(false); // mark turnCP nonDeletable so that PipingRules won't delete it immediately.
+
+ if (!reversed) {
+ previousCP.setNext(turnCP);
+ turnCP.setPrevious(previousCP);
+ turnCP.setNext(straightCP);
+ straightCP.setPrevious(turnCP);
+ } else {
+ previousCP.setPrevious(turnCP);
+ turnCP.setNext(previousCP);
+ turnCP.setPrevious(straightCP);
+ straightCP.setNext(turnCP);
+ }
+
+ turnCP.setWorldPosition(currentPosition);
+ turnCP.setTurnAngle(0.0);
+ turnCP.setLength(0.0);
+ straightCP.setWorldPosition(currentPosition);
+ straightCP.setLength(0.0);
+
+ setPreviousPosition(currentPosition);
+ updateCurrentPoint();
+
+
+
+ }
+
+ /**
+ * Updates tool graphics for current point
+ */
+ private void updateCurrentPoint() {
+ InlineComponent straight = (InlineComponent)added.get(added.size()-1);
+ // TODO: the inline length is from previous update step.
+ double l;
+ if (!reversed)
+ l = straight.getPrevious().getControlPoint().getInlineLength();
+ else
+ l = straight.getNext().getControlPoint().getInlineLength();
+ Vector3d v = new Vector3d();
+ v.sub(currentPosition, previousPosition);
+ double length = v.length();
+ if (length > MathTools.NEAR_ZERO) {
+ v.scale(1.0/length);
+ v.scale(0.5*(length+l));
+ v.add(previousPosition);
+ straight.getControlPoint().setWorldPosition(v);
+ straight.getControlPoint().setLength(length);
+ }
+ try {
+ PipingRules.positionUpdate(straight.getControlPoint(),false);
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ private PipelineComponent getLast() {
+ if (added.size() == 0)
+ return startComponent;
+ return added.get(added.size()-1);
+ }
+
+
+ /**
+ * Removes last point from pipeline
+ */
+ public void removePoint() {
+ if (added.size() < 3)
+ return;
+ InlineComponent straight = (InlineComponent)added.remove(added.size()-1);
+ TurnComponent turn = (TurnComponent)added.remove(added.size()-1);
+ straight.getControlPoint().remove();
+ turn.getControlPoint().remove();
+ if (added.size() > 1) {
+ setPreviousPosition(added.get(added.size()-2).getWorldPosition());
+ } else {
+ setPreviousPosition(startComponent.getWorldPosition());
+ if (direction != null)
+ setLockType(LockType.CUSTOM, true);
+ }
+
+ }
+
+ private void endPiping() throws Exception {
+ state = ToolState.NOT_ACTIVE;
+
+ if (endTo != null) {
+ ComponentUtils.connect(getLast(), endTo, endType, currentPosition);
+ }
+ panel.useDefaultAction();
+ }