+ boolean connectPrev = false;
+ boolean connectNext = false;
+ boolean branch = false;
+ if (mu[0] < 0.1) {
+ connectPrev = true;
+ }
+ else if (mu[0] > 0.9) {
+ connectNext = true;
+ }
+
+
+ if (connectPrev) {
+ PipeControlPoint pcp = s.getControlPoint();
+ if (pcp.getPrevious() != null)
+ connectPrev = false;
+ } else if (connectNext) {
+ PipeControlPoint pcp = s.getControlPoint();
+ if (pcp.getNext() != null)
+ connectNext = false;
+ } else {
+ Vector3d dir = s.getControlPoint().getPathLegDirection(Direction.NEXT);
+ Vector3d currDir = getLast().getControlPoint().getPathLegDirection(Direction.NEXT);
+ dir.normalize();
+ currDir.normalize();
+ double dot = dir.dot(currDir);
+ System.out.println(dot + " " + currDir + " " + dir);
+ if (dot > BRANCH_DOT_PRODUCT || dot < -BRANCH_DOT_PRODUCT) {
+ // pipes are almost in the same direction, creating a branch is not feasible.
+ branch = false;
+ } else {
+ branch = true;
+ }
+ }
+
+
+ if (connectNext || connectPrev)
+ info += "Connect pipes :";
+ else if (branch)
+ info += "Create a Branch :";
+
+ setInfoText(info + currentPosition + " " + Math.max(0.0, Math.min(mu[0], 1.0)));
+ if (connectNext) {
+ currentPosition.set(sEnd);
+ updateCurrentPoint();
+ return PositionType.NEXT;
+ } else if (connectPrev){
+ currentPosition.set(sStart);
+ updateCurrentPoint();
+ return PositionType.PREVIOUS;
+ } else if (branch && allowBranches) {
+ return PositionType.SPLIT;
+ } else {
+ currentPosition.set(currentPipePoint);
+ updateCurrentPoint();
+ return null;
+ }
+ }
+
+ private PipeControlPoint endingToComponent(INode componentNode, Vector3d o, Vector3d d) {
+ PipelineComponent component = (PipelineComponent)componentNode;
+ PipeControlPoint pcp = component.getControlPoint();
+ PipeControlPoint connect = null;
+ if (component instanceof EndComponent) {
+ if (pcp.getNext() != null || pcp.getPrevious() != null)
+ return null;
+ connect = pcp;
+ } else if (component instanceof TurnComponent) {
+ if (pcp.getNext() == null || pcp.getPrevious() == null)
+ connect = pcp;
+ else
+ 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)
+ connect = pcp;
+ else
+ return null;
+ }
+ if (connect != null) {
+ currentPosition.set(connect.getWorldPosition());
+ updateCurrentPoint();
+ setInfoText("Connect to " + component.getName());
+ return connect;
+ }
+ return null;
+ }
+
+ private PositionType endingLockToStraight(INode straightNode, double mu[], Point3d currentPipePoint) {
+// if (!allowBranches) {
+// updateCurrentPoint();
+// return null;
+// }
+ 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 (routePoint.lengthSquared() > BRANCH_SNAP_DISTANCE)
+ return null;
+
+ return endingToStraight(mu, s, sStart, sEnd, currentPipePoint);
+
+// if (mu[0] > 0.0 && mu[0] < 1.0 && routePoint.lengthSquared() < BRANCH_SNAP_DISTANCE) {
+// currentPosition.set(branchPoint);
+//
+// updateCurrentPoint();
+//
+// setInfoText("Create a 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;
+ }
+
+ protected void addTurn() throws Exception{
+ InlineComponent previous = (InlineComponent)getLast();
+ PipeControlPoint previousCP = previous.getControlPoint();
+ TurnComponent turn = ComponentUtils.createTurn(root);
+ PipeControlPoint turnCP = turn.getControlPoint();
+ turn.setName(pipeRun.getUniqueName("Elbow"));
+ pipeRun.addChild(turn);
+ added.add(turn);
+ turnCP.setDeletable(false); // mark turnCP nonDeletable so that PipingRules won't delete it immediately.
+ if (!reversed) {
+ previousCP.setNext(turnCP);
+ turnCP.setPrevious(previousCP);
+ } else {
+ previousCP.setPrevious(turnCP);
+ turnCP.setNext(previousCP);
+ }
+
+ turnCP.setWorldPosition(currentPosition);
+ turnCP.setTurnAngle(0.0);
+ turnCP.setLength(0.0);
+
+ }
+
+ protected void addStraight() throws Exception{
+ TurnComponent turn = (TurnComponent)getLast();
+ PipeControlPoint turnCP = turn.getControlPoint();
+
+ InlineComponent straight = ComponentUtils.createStraight(root);
+
+ PipeControlPoint straightCP = straight.getControlPoint();
+ straight.setName(pipeRun.getUniqueName("Pipe"));
+
+
+ pipeRun.addChild(straight);
+
+ added.add(straight);
+
+
+
+ if (!reversed) {
+ turnCP.setNext(straightCP);
+ straightCP.setPrevious(turnCP);
+ } else {
+ turnCP.setPrevious(straightCP);
+ straightCP.setNext(turnCP);
+ }
+
+ turnCP.setWorldPosition(currentPosition);
+ turnCP.setTurnAngle(0.0);
+ turnCP.setLength(0.0);
+ straightCP.setWorldPosition(currentPosition);
+ straightCP.setLength(0.0);