]> gerrit.simantics Code Review - simantics/3d.git/commitdiff
Get inline component direction irrespectively of connectivity 02/3702/1
authorReino Ruusu <reino.ruusu@semantum.fi>
Tue, 10 Dec 2019 12:59:27 +0000 (14:59 +0200)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Wed, 11 Dec 2019 15:58:43 +0000 (15:58 +0000)
gitlab #35

Change-Id: Iea337594e8220c91deff469b4f80d7df12a82266
(cherry picked from commit 0702c03a4c063b77af27ea492fafdd05c426bb62)

org.simantics.plant3d/src/org/simantics/plant3d/actions/RoutePipeAction.java
org.simantics.plant3d/src/org/simantics/plant3d/actions/TranslateFreeVariableLengthAction.java
org.simantics.plant3d/src/org/simantics/plant3d/actions/TranslateInlineAction.java
org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/PipeControlPoint.java

index c7c7234ef0f679a4b038253aa6a73b4c1be14a7c..6a1a8292d1028f0d834ca83450659d4f573df014 100644 (file)
@@ -390,7 +390,8 @@ public class RoutePipeAction extends vtkSwtAction {
                        lockForced = true;
                } else if (startComponent instanceof PipelineComponent){
                        if (startComponent instanceof InlineComponent) {
-                               direction = startComponent.getControlPoint().getPathLegDirection(reversed ? Direction.PREVIOUS : Direction.NEXT);
+                               direction = startComponent.getControlPoint().getInlineDir();
+                               if (reversed) direction.negate();
                                lock = LockType.CUSTOM;
                                lockForced = true;
                                if (((InlineComponent) startComponent).isVariableLength()) {
index c9bdea171ec8b452571a5d53952bacdc5077db8a..1088a894347af1ab908d26083059e1220ebfdc5c 100644 (file)
@@ -92,7 +92,8 @@ public class TranslateFreeVariableLengthAction extends RoutePipeAction{
             lock = LockType.CUSTOM;
         } else if (startComponent instanceof PipelineComponent){
             if (startComponent instanceof InlineComponent) {
-                direction = startComponent.getControlPoint().getPathLegDirection(reversed ? Direction.PREVIOUS : Direction.NEXT);
+                direction = startComponent.getControlPoint().getInlineDir();
+                if (reversed) direction.negate();
                 lock = LockType.CUSTOM;
                 if (((InlineComponent) startComponent).isVariableLength()) {
                     direction = null;
index 3aac1fd99b005f50473c46c7ee0d025360900023..91cc3ceae233e2c68f2f300ed2a612f158ba5620 100644 (file)
@@ -43,7 +43,7 @@ public class TranslateInlineAction extends TranslateAction{
                        } else if (comp.getNext() == null || comp.getPrevious() == null) {
                 setEnabled(true);
                 inline = false;
-                dir = comp.getControlPoint().getPathLegDirection(Direction.NEXT);
+                dir = comp.getControlPoint().getInlineDir();
                 dir.normalize();
             } else {
                                setEnabled(true);
@@ -66,8 +66,7 @@ public class TranslateInlineAction extends TranslateAction{
                                Point3d pe = new Point3d();
                                next.getEnds(ns, ne);
                                prev.getEnds(ps, pe);
-                               dir = comp.getControlPoint().getPathLegDirection(Direction.NEXT);
-                               dir.normalize();
+                               dir = comp.getControlPoint().getInlineDir();
                                // We may have offsets in the path leg, hence we have to project the coordinates.
                                Vector3d wp = node.getWorldPosition();
                                if (prev.getControlPoint().isVariableLength())
index eec074a68b88693812f7c8f686a08036ea72a548..493f3b894702d8f86606c08e744f75afdffedf9c 100644 (file)
@@ -518,14 +518,10 @@ public class PipeControlPoint extends G3DNode implements IP3DNode {
 
                if (turnAxis == null) {
                        Vector3d dir = getPathLegDirection(Direction.NEXT);
-                       if (dir.lengthSquared() > MathTools.NEAR_ZERO)
-                               dir.normalize();
                        return getControlPointOrientationQuat(dir, angle);
                } else {
                        Vector3d dir = getPathLegDirection(Direction.PREVIOUS);
-                       dir.negate();
-                       if (dir.lengthSquared() > MathTools.NEAR_ZERO)
-                               dir.normalize();
+                       if (dir != null) dir.negate();
                        return getControlPointOrientationQuat(dir, turnAxis, angle);
                }
        }
@@ -563,7 +559,7 @@ public class PipeControlPoint extends G3DNode implements IP3DNode {
 
 
        public static Quat4d getControlPointOrientationQuat(Vector3d dir, double angle) {
-               if (dir.lengthSquared() < MathTools.NEAR_ZERO)
+               if (dir == null || dir.lengthSquared() < MathTools.NEAR_ZERO)
                        return MathTools.getIdentityQuat();
 
 
@@ -578,7 +574,7 @@ public class PipeControlPoint extends G3DNode implements IP3DNode {
        }
 
        public static Quat4d getControlPointOrientationQuat(Vector3d dir, Vector3d up,  double angle) {
-               if (dir.lengthSquared() < MathTools.NEAR_ZERO)
+               if (dir == null || dir.lengthSquared() < MathTools.NEAR_ZERO)
                        return MathTools.getIdentityQuat();
 
                final Vector3d front = new Vector3d(1.0,0.0,0.0);
@@ -925,9 +921,8 @@ public class PipeControlPoint extends G3DNode implements IP3DNode {
 
                PipeControlPoint sub = isAxial() ? this : getDualSub();
                Vector3d pos = getWorldPosition(), pos2 = sub == this ? pos : sub.getWorldPosition();
-               Vector3d dir = sub.getPathLegDirection(Direction.NEXT);
+               Vector3d dir = sub.getInlineDir();
                
-               dir.normalize();
                dir.scale(length * 0.5);
                p1.set(pos);
                p2.set(pos2);
@@ -939,12 +934,16 @@ public class PipeControlPoint extends G3DNode implements IP3DNode {
                PipeControlPoint sub = isAxial() || isDirected() || isTurn() ? this : getChildPoints().get(0);
                Vector3d pos = getWorldPosition(), pos2 = sub == this ? pos : sub.getWorldPosition();
                
-               Vector3d dir1 = getPathLegDirection(Direction.PREVIOUS);
-               Vector3d dir2 = sub.getPathLegDirection(Direction.NEXT);
+               Vector3d dir1;
+               Vector3d dir2;
                if (isInline()) {
-                       dir1.scale(length * 0.5);
+                       dir2 = getInlineDir();
                        dir2.scale(length * 0.5);
+                       dir1 = new Vector3d(dir2);
+                       dir1.negate();
                } else {
+                       dir1 = getPathLegDirection(Direction.PREVIOUS);
+                       dir2 = sub.getPathLegDirection(Direction.NEXT);
                        dir1.scale(length);
                        dir2.scale(length);
                }
@@ -954,20 +953,34 @@ public class PipeControlPoint extends G3DNode implements IP3DNode {
                p2.add(dir2);
        }
 
+       /**
+        * Get both path leg directions, with (0,0,0) if no connection exists. The returned vectors are not normalized.
+        * 
+        * @param v1  Set to the direction towards the previous control point on output
+        * @param v2  Set to the direction towards the next control point on output
+        */
        public void getEndDirections(Tuple3d v1, Tuple3d v2) {
                PipeControlPoint sub = isAxial() ? this : getDualSub();
                
                Vector3d dir1 = getPathLegDirection(Direction.PREVIOUS);
                Vector3d dir2 = sub.getPathLegDirection(Direction.NEXT);
-               v1.set(dir1);
-               v2.set(dir2);
+               
+               if (dir1 != null)
+                       v1.set(dir1);
+               else
+                       v1.set(0,0,0);
+               
+               if (dir2 != null)
+                       v2.set(dir2);
+               else
+                       v2.set(0,0,0);
        }
 
        public void getInlineControlPointEnds(Tuple3d p1, Tuple3d p2, Vector3d dir) {
                assert (isInline());
 
                Vector3d pos = getWorldPosition();
-               dir.set(getPathLegDirection(Direction.NEXT));
+               dir.set(getInlineDir());
                dir.normalize();
                dir.scale(length * 0.5);
                p1.set(pos);
@@ -981,7 +994,7 @@ public class PipeControlPoint extends G3DNode implements IP3DNode {
 
                Vector3d pos = getWorldPosition();
                center.set(pos);
-               dir.set(getPathLegDirection(Direction.NEXT));
+               dir.set(getInlineDir());
                dir.normalize();
                dir.scale(length * 0.5);
                p1.set(pos);
@@ -990,6 +1003,22 @@ public class PipeControlPoint extends G3DNode implements IP3DNode {
                p2.add(dir);
        }
 
+       public Vector3d getInlineDir() {
+               Vector3d dir = getPathLegDirection(Direction.NEXT);
+               if (dir == null) {
+                       dir = getPathLegDirection(Direction.PREVIOUS);
+                       if (dir != null) {
+                               // Use reverse direction
+                               dir.scale(-1.0);
+                       } else {
+                               // Control point is not connected at all, use current orientation
+                               dir = new Vector3d(1,0,0);
+                               MathTools.rotate(getWorldOrientation(), dir, dir);
+                       }
+               }
+               return dir;
+       }
+
        public double getInlineLength() {
                if (type == PointType.TURN)
                        return length;
@@ -998,22 +1027,27 @@ public class PipeControlPoint extends G3DNode implements IP3DNode {
                return 0;
        }
 
+       /**
+        * Return the position indicated by the argument. If the indicated direction is not connected, the
+        * control point's wolrd position is returned instead.
+        * 
+        * @param type  A selector for the position to be returned
+        * @return  The selected position
+        */
        public Vector3d getRealPosition(PositionType type) {
                Vector3d pos = getWorldPosition();
                switch (type) {
                case NEXT: {
-                       Vector3d dir = getPathLegDirection(Direction.NEXT);
+                       Vector3d dir = getInlineDir();
                        double length = getInlineLength();
-                       dir.normalize();
                        dir.scale(length);
                        pos.add(dir);
                        break;
                }
                case PREVIOUS: {
-                       Vector3d dir = getPathLegDirection(Direction.PREVIOUS);
+                       Vector3d dir = getInlineDir();
                        double length = getInlineLength();
-                       dir.normalize();
-                       dir.scale(length);
+                       dir.scale(-length);
                        pos.add(dir);
                        break;
                }