]> gerrit.simantics Code Review - simantics/3d.git/commitdiff
Allow selection of vertical direction in Plant3DEditor 83/3883/1
authorReino Ruusu <reino.ruusu@semantum.fi>
Wed, 19 Feb 2020 15:04:06 +0000 (17:04 +0200)
committerReino Ruusu <reino.ruusu@semantum.fi>
Wed, 19 Feb 2020 15:04:43 +0000 (17:04 +0200)
gitlab #85

Change-Id: Ib5491a4ec233a9516a4087f270343194f28f7d96

org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/swt/vtkCameraAndSelectorAction.java
org.simantics.plant3d/src/org/simantics/plant3d/editor/Plant3DEditor.java

index 9c444b4f7eac40aff2e6d5fe9af0f8a92c4681c3..ebdfb245219a43990271074a2d8f253ffdf774cf 100644 (file)
@@ -43,6 +43,7 @@ public class vtkCameraAndSelectorAction extends vtkSwtAction implements ISelecti
        protected double activeRate = 5.0;
        protected double passiveRate = 0.01;
        protected boolean doNotRotate = true;
+       private double[] upDirection = new double[] { 0, 1, 0 };
 
        public vtkCameraAndSelectorAction(InteractiveVtkComposite panel) {
                super(panel);
@@ -50,6 +51,10 @@ public class vtkCameraAndSelectorAction extends vtkSwtAction implements ISelecti
                this.rw = panel.getRenderWindow();
                this.cam = ren.GetActiveCamera();
        }
+       
+       public void setUpDirection(double[] upDirection) {
+               this.upDirection = upDirection;
+       }
 
        public void Lock() {
                panel.lock();
@@ -116,7 +121,7 @@ public class vtkCameraAndSelectorAction extends vtkSwtAction implements ISelecti
                        cam.Azimuth(lastX - x);
                        cam.Elevation(y - lastY);
                        if (doNotRotate)
-                               cam.SetRoll(0);
+                               cam.SetViewUp(upDirection);
                        cam.OrthogonalizeViewUp();
                        resetCameraClippingRange();
                        // panel.UpdateLight();
@@ -125,9 +130,8 @@ public class vtkCameraAndSelectorAction extends vtkSwtAction implements ISelecti
                if (this.InteractionMode == 2) {
                        double FPoint[];
                        double PPoint[];
-                       double APoint[] = new double[3];
+                       double APoint[];
                        double RPoint[];
-                       double focalDepth;
 
                        // get the current focal point and position
                        FPoint = cam.GetFocalPoint();
@@ -136,14 +140,15 @@ public class vtkCameraAndSelectorAction extends vtkSwtAction implements ISelecti
                        // calculate the focal depth since we'll be using it a lot
                        ren.SetWorldPoint(FPoint[0], FPoint[1], FPoint[2], 1.0);
                        ren.WorldToDisplay();
-                       focalDepth = ren.GetDisplayPoint()[2];
-
-                       APoint[0] = rw.GetSize()[0] / 2.0 + (x - lastX);
-                       APoint[1] = rw.GetSize()[1] / 2.0 - (y - lastY);
-                       APoint[2] = focalDepth;
+                       APoint = ren.GetDisplayPoint();
+                       
+//                     int[] size = rw.GetSize();
+                       APoint[0] -= x - lastX;
+                       APoint[1] += y - lastY;
                        ren.SetDisplayPoint(APoint);
                        ren.DisplayToWorld();
                        RPoint = ren.GetWorldPoint();
+                       
                        if (RPoint[3] != 0.0) {
                                RPoint[0] = RPoint[0] / RPoint[3];
                                RPoint[1] = RPoint[1] / RPoint[3];
@@ -154,10 +159,9 @@ public class vtkCameraAndSelectorAction extends vtkSwtAction implements ISelecti
                         * Compute a translation vector, moving everything 1/2 the distance to the
                         * cursor. (Arbitrary scale factor)
                         */
-                       cam.SetFocalPoint((FPoint[0] - RPoint[0]) / 2.0 + FPoint[0], (FPoint[1] - RPoint[1]) / 2.0 + FPoint[1],
-                                       (FPoint[2] - RPoint[2]) / 2.0 + FPoint[2]);
-                       cam.SetPosition((FPoint[0] - RPoint[0]) / 2.0 + PPoint[0], (FPoint[1] - RPoint[1]) / 2.0 + PPoint[1],
-                                       (FPoint[2] - RPoint[2]) / 2.0 + PPoint[2]);
+                       cam.SetFocalPoint(RPoint);
+                       cam.SetPosition((RPoint[0] - FPoint[0]) + PPoint[0], (RPoint[1] - FPoint[1]) + PPoint[1],
+                                       (RPoint[2] - FPoint[2]) + PPoint[2]);
                        resetCameraClippingRange();
                }
                // zoom
@@ -379,7 +383,7 @@ public class vtkCameraAndSelectorAction extends vtkSwtAction implements ISelecti
                Lock();
                cam.SetFocalPoint(x, y, z);
                if (doNotRotate)
-                       cam.SetRoll(0);
+                       cam.SetViewUp(upDirection);
                cam.OrthogonalizeViewUp();
                resetCameraClippingRange();
                // panel.UpdateLight();
index a97be312f5e3605947f3a45a80c2c2a735bce984..2c626c95a00dd91b46eaff7f98b808bbf694ec34 100644 (file)
@@ -16,7 +16,6 @@ import org.eclipse.jface.viewers.SelectionChangedEvent;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.DisposeEvent;
 import org.eclipse.swt.events.DisposeListener;
-import org.eclipse.swt.layout.FillLayout;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Menu;
 import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
@@ -120,6 +119,11 @@ public class Plant3DEditor extends ResourceEditorPart {
        
        private P3DNodeMap nodeMap;
        
+       /** Constants for selecting the up-direction */
+       public static final int X = 0, Y = 1, Z = 2;
+       
+       protected int upDirection = 1;
+       
        @Override
        public void createPartControl(Composite parent) {
                this.parent = parent;
@@ -162,6 +166,18 @@ public class Plant3DEditor extends ResourceEditorPart {
                new ContextMenuListener(panel, contextMenu);
                
                cameraAction = createCameraAction();
+               switch (upDirection) {
+               case 0:
+                       cameraAction.setUpDirection(new double[] { 1, 0, 0 });
+                       break;
+               case 1:
+                       cameraAction.setUpDirection(new double[] { 0, 1, 0 });
+                       break;
+               case 2:
+                       cameraAction.setUpDirection(new double[] { 0, 0, 1 });
+                       break;
+               }
+               
                panel.setDefaultAction(cameraAction);
                panel.useDefaultAction();
                panel.setPickType(4);
@@ -240,6 +256,10 @@ public class Plant3DEditor extends ResourceEditorPart {
                
        }
        
+       public void setUpDirection(int upDirection) {
+               this.upDirection = upDirection;
+       }
+
        protected vtkCameraAndSelectorAction createCameraAction() {
            return new vtkCameraAndSelectorAction(panel); 
        }
@@ -378,7 +398,8 @@ public class Plant3DEditor extends ResourceEditorPart {
                ren1.SetGradientBackground(true);
 
                // vtkActor grid = vtkShape.createGridActor(8,1.0,1|2|4);
-               vtkActor grid = vtkShape.createGridActor(8,1.0, 2 );
+               int dir = 1 << upDirection;
+               vtkActor grid = vtkShape.createGridActor(8, 1.0, dir);
                grid.SetPickable(0);
                ren1.AddActor(grid);
                panel.addDeletable(grid);