]> gerrit.simantics Code Review - simantics/3d.git/commitdiff
Tools for fitting contents to view in Plant3D editor 69/3969/1
authorReino Ruusu <reino.ruusu@semantum.fi>
Thu, 5 Mar 2020 13:58:25 +0000 (15:58 +0200)
committerReino Ruusu <reino.ruusu@semantum.fi>
Thu, 5 Mar 2020 13:58:25 +0000 (15:58 +0200)
gitlab #100

Also fixes clipping in parallel projection mode by keeping scale and
camera distance at values that match the camera view angle.

Change-Id: I82ad77bdc339fa0ad7814c31fcac97512c440972

org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/handlers/ParallelPerspectiveHandler.java
org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/swt/vtkCameraAndSelectorAction.java
org.simantics.plant3d/plugin.xml
org.simantics.plant3d/src/org/simantics/plant3d/editor/Plant3DEditor.java

index 43ec97a090dda0c78c4c2b6d6bf205d7b569373b..59f94c0b638707d689893245505a88bcc7fb321a 100644 (file)
  *******************************************************************************/
 package org.simantics.g3d.vtk.handlers;
 
-import javax.vecmath.Vector3d;
-
 import org.eclipse.core.commands.AbstractHandler;
 import org.eclipse.core.commands.Command;
 import org.eclipse.core.commands.ExecutionEvent;
 import org.eclipse.core.commands.ExecutionException;
 import org.eclipse.ui.IWorkbenchPart;
 import org.eclipse.ui.handlers.HandlerUtil;
-import org.simantics.g3d.math.MathTools;
 import org.simantics.g3d.vtk.common.VtkView;
 import org.simantics.utils.threads.AWTThread;
 import org.simantics.utils.threads.ThreadUtils;
@@ -46,37 +43,15 @@ public class ParallelPerspectiveHandler extends AbstractHandler {
                            vtkRenderer ren = panel.getRenderer();
                            vtkCamera cam = ren.GetActiveCamera();
                                if (activate){
-
-                                       Vector3d focal = new Vector3d(panel.getRenderer().GetActiveCamera().GetFocalPoint());
-                                       Vector3d pos = new Vector3d(panel.getRenderer().GetActiveCamera().GetPosition());
-                                       double dist = MathTools.distance(pos, focal);
-                                       cam.SetParallelScale(dist/4.0);
-                                       // camera must be moved backwards, or graphics get clipped when parallel view is zoomed out
-                                       // TODO : is there a better way to do this?
-                                       Vector3d dir = new Vector3d(pos);
-                                       dir.sub(focal);
-                                       dir.normalize();
-                                       dir.scale(100);
-                                       dir.add(focal);
-                                       cam.SetPosition(dir.x, dir.y, dir.z);
-
+                                       double distance = cam.GetDistance();
+                                       double angle = cam.GetViewAngle();
+                                       double scale = Math.tan(Math.toRadians(angle / 2)) * distance;
+                                       cam.SetParallelScale(scale);
                                        cam.SetParallelProjection(1);
                                        ren.ResetCameraClippingRange();
                                } else {
-                                   
-                                   double scale = cam.GetParallelScale();
                                        cam.SetParallelProjection(0);
-                                       
-                                       Vector3d focal = new Vector3d(panel.getRenderer().GetActiveCamera().GetFocalPoint());
-                    Vector3d pos = new Vector3d(panel.getRenderer().GetActiveCamera().GetPosition());
-                    Vector3d dir = new Vector3d(pos);
-                    dir.sub(focal);
-                    dir.normalize();
-                    dir.scale(scale*4.0);
-                    dir.add(focal);
-                    cam.SetPosition(dir.x, dir.y, dir.z);
                                        ren.ResetCameraClippingRange();
-
                                }
                                panel.refresh();
 
index 7a6f0d894173ad29e8eebb48be2c18da39ad20e6..a5ea614978ee803a6df23c252205ad583f0a1622 100644 (file)
@@ -18,6 +18,8 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
 
+import javax.vecmath.Vector3d;
+
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.ISelectionChangedListener;
 import org.eclipse.jface.viewers.ISelectionProvider;
@@ -28,6 +30,7 @@ import org.simantics.g3d.tools.AdaptationUtils;
 
 import vtk.vtkCamera;
 import vtk.vtkProp;
+import vtk.vtkProp3D;
 import vtk.vtkRenderWindow;
 import vtk.vtkRenderer;
 
@@ -167,16 +170,13 @@ public class vtkCameraAndSelectorAction extends vtkSwtAction implements ISelecti
                // zoom
                if (this.InteractionMode == 3) {
                        double zoomFactor;
-                       // double clippingRange[];
-
                        zoomFactor = Math.pow(1.02, (y - lastY));
+                       cam.Dolly(zoomFactor);
+                       
                        if (cam.GetParallelProjection() == 1) {
-                               cam.SetParallelScale(cam.GetParallelScale() / zoomFactor);
-                               resetCameraClippingRange();
-                       } else {
-                               cam.Dolly(zoomFactor);
-                               resetCameraClippingRange();
+                               updateParallelScale();
                        }
+                       resetCameraClippingRange();
                }
                lastX = x;
                lastY = y;
@@ -208,16 +208,21 @@ public class vtkCameraAndSelectorAction extends vtkSwtAction implements ISelecti
                double zoomFactor;
                // double clippingRange[];
                zoomFactor = Math.pow(1.02, (e.getWheelRotation()));
-               if (cam.GetParallelProjection() == 1) {
-                       cam.SetParallelScale(cam.GetParallelScale() / zoomFactor);
-               } else {
-                       cam.Dolly(zoomFactor);
-                       resetCameraClippingRange();
-               }
+               cam.Dolly(zoomFactor);
+               updateParallelScale();
+               resetCameraClippingRange();
                panel.refresh();
                return true;
        }
 
+       private void updateParallelScale() {
+               // Make height of parallel projection match the distance
+               double distance = cam.GetDistance();
+               double angle = cam.GetViewAngle();
+               double scale = Math.tan(Math.toRadians(angle / 2)) * distance;
+               cam.SetParallelScale(scale);
+       }
+
        protected List<vtkProp> selectActors = new ArrayList<vtkProp>();
        protected List<vtkProp> hoverActor = new ArrayList<vtkProp>();
 
@@ -415,5 +420,61 @@ public class vtkCameraAndSelectorAction extends vtkSwtAction implements ISelecti
                // panel.UpdateLight();
                UnLock();
        }
+       
+       public void fitToView(Collection<vtkProp3D> props) {
+               if (props.isEmpty())
+                       return;
+               
+               double[] bounds = new double[] { Double.MAX_VALUE, -Double.MAX_VALUE,  Double.MAX_VALUE, -Double.MAX_VALUE,  Double.MAX_VALUE, -Double.MAX_VALUE }, b = new double[6];
+               for (vtkProp3D prop : props) {
+                       prop.GetBounds(b);
+                       for (int i = 0; i < 6; i+=2)
+                               bounds[i] = Math.min(bounds[i], b[i]);
+                       for (int i = 1; i < 6; i+=2)
+                               bounds[i] = Math.max(bounds[i], b[i]);
+               }
+               
+               fitToView(bounds);
+       }
+
+       public void fitToView(double[] bounds) {
+               Vector3d center = new Vector3d((bounds[0] + bounds[1])/2, (bounds[2] + bounds[3])/2, (bounds[4] + bounds[5])/2);
+               Vector3d viewDir = new Vector3d(cam.GetDirectionOfProjection());
+               Vector3d upDir = new Vector3d(cam.GetViewUp());
+               viewDir.normalize();
+               upDir.normalize();
+               Vector3d sideDir = new Vector3d();
+               sideDir.cross(viewDir, upDir);
+               sideDir.normalize();
+               
+               double width = getBoxWidth(bounds, sideDir);
+               double height = getBoxWidth(bounds, upDir);
+               double depth = getBoxWidth(bounds, viewDir);
+               
+               int[] size = rw.GetActualSize();
+               
+               double distance1 = height / 2 / Math.tan(Math.toRadians(cam.GetViewAngle()) / 2);
+               double distance2 = distance1 * (width / size[0] / (height / size[1]));
+               
+               double distance = Math.max(distance1, distance2) + depth / 2;
+               
+               viewDir.scale(-distance);
+               viewDir.add(center);
+               
+               cam.SetPosition(viewDir.x, viewDir.y, viewDir.z);
+               focus(center.x, center.y, center.z);
+               
+               if (cam.GetParallelProjection() == 1) {
+                       cam.SetParallelScale(Math.max(height, width * size[1] / size[0]) / 2);
+               }
+       }
+
+       private static double getBoxWidth(double[] bounds, Vector3d dir) {
+               double dx = bounds[1] - bounds[0];
+               double dy = bounds[3] - bounds[2];
+               double dz = bounds[5] - bounds[4];
+               
+               return Math.abs(dx * dir.x) + Math.abs(dy * dir.y) + Math.abs(dz * dir.z);
+       }
 
 }
index e935e5e9cb6154772766f6b2ed00b5332c9d839b..7d6b9042023bfd46db81dea08077cf56a08ec246 100644 (file)
                   type="push">
                <parameter
                      name="org.simantics.g3d.viewDirection"
-                     value="-0.57735026918962576450914878050196,-0.57735026918962576450914878050196,0.57735026918962576450914878050196">
+                     value="-0.57735026918962576450914878050196,0.57735026918962576450914878050196,-0.57735026918962576450914878050196">
                </parameter>
             </command>
       <command
index a7559f8009b35a599469a6d7d45cc7845b698a43..96789ff11770b871412d5e2d9aaff09bdee0579b 100644 (file)
@@ -1,13 +1,16 @@
 package org.simantics.plant3d.editor;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
+import org.eclipse.jface.action.Action;
 import org.eclipse.jface.action.IMenuListener;
 import org.eclipse.jface.action.IMenuManager;
 import org.eclipse.jface.action.MenuManager;
+import org.eclipse.jface.action.Separator;
 import org.eclipse.jface.layout.GridDataFactory;
 import org.eclipse.jface.layout.GridLayoutFactory;
 import org.eclipse.jface.viewers.ISelectionChangedListener;
@@ -28,12 +31,12 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.g3d.scenegraph.NodeMap;
 import org.simantics.g3d.scenegraph.RenderListener;
 import org.simantics.g3d.scenegraph.base.INode;
+import org.simantics.g3d.scenegraph.base.ParentNode;
 import org.simantics.g3d.toolbar.ToolComposite;
 import org.simantics.g3d.vtk.action.RemoveAction;
 import org.simantics.g3d.vtk.common.HoverHighlighter;
 import org.simantics.g3d.vtk.common.NodeSelectionProvider2;
 import org.simantics.g3d.vtk.common.SelectionHighlighter;
-import org.simantics.g3d.vtk.common.VTKSelectionItem;
 import org.simantics.g3d.vtk.common.VtkView;
 import org.simantics.g3d.vtk.shape.vtkShape;
 import org.simantics.g3d.vtk.swt.ContextMenuListener;
@@ -58,6 +61,7 @@ import org.simantics.plant3d.ontology.Plant3D;
 import org.simantics.plant3d.scenegraph.EndComponent;
 import org.simantics.plant3d.scenegraph.Equipment;
 import org.simantics.plant3d.scenegraph.IP3DNode;
+import org.simantics.plant3d.scenegraph.IP3DVisualNode;
 import org.simantics.plant3d.scenegraph.InlineComponent;
 import org.simantics.plant3d.scenegraph.Nozzle;
 import org.simantics.plant3d.scenegraph.P3DRootNode;
@@ -82,7 +86,7 @@ import vtk.vtkCameraPass;
 import vtk.vtkDefaultPass;
 import vtk.vtkGaussianBlurPass;
 import vtk.vtkLightsPass;
-import vtk.vtkProp;
+import vtk.vtkProp3D;
 import vtk.vtkRenderPassCollection;
 import vtk.vtkRenderer;
 import vtk.vtkSSAAPass;
@@ -254,7 +258,6 @@ public class Plant3DEditor extends ResourceEditorPart {
                }
                
                createActions();
-               
        }
        
        public void setUpDirection(int upDirection) {
@@ -309,19 +312,21 @@ public class Plant3DEditor extends ResourceEditorPart {
                     
                     @Override
                     public void postRender() {
+                        panel.removeListener(this);
+                        
                         try {
                             P3DUtil.finalizeDBLoad2(rootNode);
                             if (nodeMap.isRangeModified());
                                 nodeMap.commit("Load sync");
                         } catch (Exception e) {
                             ExceptionUtils.logAndShowError("Failed to load model correctly", e);
-                            //throw new DatabaseException(e);
                         }
-                        panel.removeListener(this);  
-                        
+                                       
+                                       List<vtkProp3D> props = new ArrayList<vtkProp3D>();
+                                       collectProps(rootNode, props);
+                                       fitToWindow(props);
                     }
                 });
-                               
                        }
                });
                
@@ -342,7 +347,7 @@ public class Plant3DEditor extends ResourceEditorPart {
                panel.getComponent().setFocus();
        }
        
-       private void createScene() {
+       protected void createScene() {
                vtkRenderer ren1 = panel.getRenderer();
                
                boolean multiPass = false;
@@ -407,7 +412,6 @@ public class Plant3DEditor extends ResourceEditorPart {
                
                AxesDisplay axesDisplay = new AxesDisplay(panel);
                axesDisplay.show();
-               
        }
        
        protected Menu contextMenu;
@@ -426,19 +430,11 @@ public class Plant3DEditor extends ResourceEditorPart {
        
        protected void createContextMenu(IMenuManager m) {
                List<INode> selected = selectionProvider.getSelectedNodes();
-               List<VTKSelectionItem<Resource>> selectedItems = selectionProvider.getSelectedItems();
-               if (selectedItems.size() > 0) {
-                   List<vtkProp> props = new ArrayList<>();
-                   for (VTKSelectionItem<Resource> i : selectedItems) {
-                       vtkProp p = (vtkProp)i.getAdapter(vtkProp.class);
-                       if (p != null)
-                           props.add(p);
-                   }
-                   if (props.size() > 0) {
-                       focusAction.setProps(props);
-                       m.add(focusAction);
-                   }
-               }
+               
+               createFocusMenu(m, selected);
+
+               m.add(new Separator());
+               
                try {
                        if (selected.size() == 0) {
                            m.add(new AddEquipmentAction(rootNode, getLibraryUri()));
@@ -518,6 +514,31 @@ public class Plant3DEditor extends ResourceEditorPart {
                        ExceptionUtils.logAndShowError(e);
                }
        }
+
+       protected void createFocusMenu(IMenuManager m, List<INode> selected) {
+               m.add(new Action("Fit to Window") {
+                       @Override
+                       public void run() {
+                               List<vtkProp3D> props = new ArrayList<>();
+                               final Collection<INode> collection = !selected.isEmpty() ? selected : getRootNode().getChild();
+                               for (INode n : collection)
+                                       collectProps(n, props);
+                               
+                               fitToWindow(props);
+                               getPanel().refresh();
+                       }
+               });
+
+               if (!selected.isEmpty()) {
+                       List<vtkProp3D> actors = new ArrayList<>();
+                       for (INode n : selected)
+                               collectProps(n, actors);
+                       if (actors.size() > 0) {
+                               focusAction.setProps(new ArrayList<>(actors));
+                               m.add(focusAction);
+                       }
+               }
+       }
        
        private IContentOutlinePage createOutline() {
                if (rootNode == null || selectionProvider == null)
@@ -585,4 +606,18 @@ public class Plant3DEditor extends ResourceEditorPart {
        public P3DNodeMap getNodeMap() {
         return nodeMap;
     }
+
+       public void fitToWindow(Collection<vtkProp3D> props) {
+               cameraAction.fitToView(props);
+       }
+
+       protected static void collectProps(INode node, List<vtkProp3D> props) {
+               if (node instanceof IP3DVisualNode)
+                       props.addAll(((IP3DVisualNode) node).getActors());
+               
+               if (node instanceof ParentNode)
+               for (INode n : ((ParentNode<?>) node).getNodes()) {
+                               collectProps(n, props);
+               }
+       }
 }