]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/handlers/ParallelPerspectiveHandler.java
Tools for fitting contents to view in Plant3D editor
[simantics/3d.git] / org.simantics.g3d.vtk / src / org / simantics / g3d / vtk / handlers / ParallelPerspectiveHandler.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();