From 95c5f67fd62229c761fd322ff7505114d3fc986a Mon Sep 17 00:00:00 2001 From: Reino Ruusu Date: Thu, 20 Feb 2020 16:28:50 +0200 Subject: [PATCH] Let root node determine up direction for rotation angle calculations gitlab #85 Change-Id: I549af4f0b93cbf3867e334165db8c2dacee12968 --- .../plant3d/scenegraph/P3DRootNode.java | 8 +++++++- .../controlpoint/PipeControlPoint.java | 17 ++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/P3DRootNode.java b/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/P3DRootNode.java index 27b1d471..7a5ce551 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/P3DRootNode.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/P3DRootNode.java @@ -27,7 +27,9 @@ import vtk.vtkProp; @GraphType(Plant3D.URIs.Plant) public class P3DRootNode extends ParentNode implements IG3DNode, NodeMapProvider { - + // Vertical direction that determines the interpretation of rotation angle origin + protected Vector3d upVector = new Vector3d(0.0, 1.0, 0.0); + @RelatedElementsAdd(Plant3D.URIs.children) public void addChild(INode node) { //public void addChild(IP3DVisualNode node) { @@ -183,4 +185,8 @@ public class P3DRootNode extends ParentNode implements IG3DNode, NodeMapP public TurnComponent createTurn() { return new TurnComponent(); } + + public Vector3d getUpVector() { + return upVector; + } } diff --git a/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/PipeControlPoint.java b/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/PipeControlPoint.java index 2905d26c..72ef4e68 100644 --- a/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/PipeControlPoint.java +++ b/org.simantics.plant3d/src/org/simantics/plant3d/scenegraph/controlpoint/PipeControlPoint.java @@ -15,6 +15,7 @@ import javax.vecmath.Vector3d; import org.simantics.g3d.math.MathTools; import org.simantics.g3d.property.annotations.GetPropertyValue; import org.simantics.g3d.scenegraph.G3DNode; +import org.simantics.g3d.scenegraph.base.INode; import org.simantics.plant3d.scenegraph.IP3DNode; import org.simantics.plant3d.scenegraph.Nozzle; import org.simantics.plant3d.scenegraph.P3DRootNode; @@ -581,15 +582,24 @@ public class PipeControlPoint extends G3DNode implements IP3DNode { if (dir == null || dir.lengthSquared() < MathTools.NEAR_ZERO) return MathTools.getIdentityQuat(); - Vector3d up = new Vector3d(0.0, 1.0, 0.0); + final P3DRootNode root = getRoot(); + Vector3d up = root != null ? new Vector3d(root.getUpVector()) : new Vector3d(0.0, 1.0, 0.0); double a = up.angle(getPathLegEndpointVector()); if (a < 0.1 || (Math.PI - a) < 0.1) { - up.set(1.0, 0.0, 0.0); + // Rotate components + up.set(up.getY(), up.getZ(), up.getX()); } return getControlPointOrientationQuat(dir, up, angle); } + public P3DRootNode getRoot() { + INode n = getParent(); + while (n != null && !(n instanceof P3DRootNode)) + n = n.getParent(); + return (P3DRootNode) n; + } + public static Quat4d getControlPointOrientationQuat(Vector3d dir, Vector3d up, double angle) { if (dir == null || dir.lengthSquared() < MathTools.NEAR_ZERO) return MathTools.getIdentityQuat(); @@ -600,7 +610,8 @@ public class PipeControlPoint extends G3DNode implements IP3DNode { Vector3d right = new Vector3d(); - + + up = new Vector3d(up); right.cross(dir, up); up.cross(right, dir); right.normalize(); -- 2.47.1