X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.g3d.vtk%2Fsrc%2Forg%2Fsimantics%2Fg3d%2Fvtk%2Fawt%2FvtkCameraAndSelectorAction.java;h=0ce1acd2b69de2f631ce20f1a77d253795ed5102;hb=34cb7e694463fb4463a9bde610a1fd3bfd330644;hp=f1e6f32443bf7d1079c1f00b4b5004a0dc415533;hpb=84132a1d750c45f9161afbd58b78572964e50d26;p=simantics%2F3d.git diff --git a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/awt/vtkCameraAndSelectorAction.java b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/awt/vtkCameraAndSelectorAction.java index f1e6f324..0ce1acd2 100644 --- a/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/awt/vtkCameraAndSelectorAction.java +++ b/org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/awt/vtkCameraAndSelectorAction.java @@ -17,6 +17,10 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import javax.vecmath.Point2d; +import javax.vecmath.Point2i; +import javax.vecmath.Point3d; + import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.ISelectionChangedListener; import org.eclipse.jface.viewers.ISelectionProvider; @@ -59,154 +63,156 @@ public class vtkCameraAndSelectorAction extends vtkAwtAction implements ISelecti } public void InteractionModeRotate() - { - this.InteractionMode = 1; - } - - public void InteractionModeTranslate() - { - this.InteractionMode = 2; - } - - public void InteractionModeZoom() - { - this.InteractionMode = 3; - } - + { + this.InteractionMode = 1; + } + + public void InteractionModeTranslate() + { + this.InteractionMode = 2; + } + + public void InteractionModeZoom() + { + this.InteractionMode = 3; + } + public void resetCameraClippingRange() { - Lock(); - ren.ResetCameraClippingRange(); - UnLock(); - } + Lock(); + ren.ResetCameraClippingRange(); + UnLock(); + } - public void resetCamera() { - Lock(); - ren.ResetCamera(); - UnLock(); - } - - public void mousePressed(MouseEvent e) - { - - if (ren.VisibleActorCount() == 0) return; - rw.SetDesiredUpdateRate(activeRate); - lastX = e.getX(); - lastY = e.getY(); - if ((e.getModifiers()==InputEvent.BUTTON2_MASK) || - (e.getModifiers()==(InputEvent.BUTTON1_MASK | InputEvent.SHIFT_MASK))) - { - InteractionModeTranslate(); - } - else if (e.getModifiers()==InputEvent.BUTTON3_MASK) - { - InteractionModeZoom(); - } - else - { - InteractionModeRotate(); - } - } - - public void mouseReleased(MouseEvent e) - { - rw.SetDesiredUpdateRate(passiveRate); - } - + public void resetCamera() { + Lock(); + ren.ResetCamera(); + UnLock(); + } + + public void mousePressed(MouseEvent e) + { + + if (ren.VisibleActorCount() == 0) return; + rw.SetDesiredUpdateRate(activeRate); + lastX = e.getX(); + lastY = e.getY(); + if ((e.getModifiers()==InputEvent.BUTTON2_MASK) || + (e.getModifiers()==(InputEvent.BUTTON1_MASK | InputEvent.SHIFT_MASK))) + { + InteractionModeTranslate(); + } + else if (e.getModifiers()==InputEvent.BUTTON3_MASK) + { + InteractionModeZoom(); + } + else + { + InteractionModeRotate(); + } + } + + public void mouseReleased(MouseEvent e) + { + rw.SetDesiredUpdateRate(passiveRate); + } + - - public void mouseDragged(MouseEvent e) - { - if (ren.VisibleActorCount() == 0) return; - int x = e.getX(); - int y = e.getY(); - // rotate - if (this.InteractionMode == 1) - { - cam.Azimuth(lastX - x); - cam.Elevation(y - lastY); - if (doNotRotate) - cam.SetRoll(0); - cam.OrthogonalizeViewUp(); - resetCameraClippingRange(); - panel.UpdateLight(); - } - // translate - if (this.InteractionMode == 2) - { - double FPoint[]; - double PPoint[]; - double APoint[] = new double[3]; - double RPoint[]; - double focalDepth; - - // get the current focal point and position - FPoint = cam.GetFocalPoint(); - PPoint = cam.GetPosition(); - - // 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; - 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]; - RPoint[2] = RPoint[2]/RPoint[3]; - } - - /* - * 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]); - resetCameraClippingRange(); - } - // zoom - if (this.InteractionMode == 3) - { - double zoomFactor; - //double clippingRange[]; - - zoomFactor = Math.pow(1.02,(y - lastY)); - if (cam.GetParallelProjection() == 1) - { - cam.SetParallelScale(cam.GetParallelScale()/zoomFactor); - } - else - { - cam.Dolly(zoomFactor); - resetCameraClippingRange(); - } - } - lastX = x; - lastY = y; - panel.Render(); - } + + public void mouseDragged(MouseEvent e) + { + if (ren.VisibleActorCount() == 0) return; + int x = e.getX(); + int y = e.getY(); + // rotate + if (this.InteractionMode == 1) + { + cam.Azimuth(lastX - x); + cam.Elevation(y - lastY); + if (doNotRotate) + cam.SetRoll(0); + cam.OrthogonalizeViewUp(); + resetCameraClippingRange(); + panel.UpdateLight(); + } + // translate + if (this.InteractionMode == 2) + { + double FPoint[]; + double PPoint[]; + double APoint[] = new double[3]; + double RPoint[]; + double focalDepth; + + // get the current focal point and position + FPoint = cam.GetFocalPoint(); + PPoint = cam.GetPosition(); + + // 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; + 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]; + RPoint[2] = RPoint[2]/RPoint[3]; + } + + /* + * 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]); + resetCameraClippingRange(); + } + // zoom + if (this.InteractionMode == 3) + { + double zoomFactor; + //double clippingRange[]; + + zoomFactor = Math.pow(1.02,(y - lastY)); + if (cam.GetParallelProjection() == 1) + { + cam.SetParallelScale(cam.GetParallelScale()/zoomFactor); + } + else + { + cam.Dolly(zoomFactor); + resetCameraClippingRange(); + } + } + lastX = x; + lastY = y; + panel.Render(); + } - + private List selectActors = new ArrayList(); private List hoverActor = new ArrayList(); - + @Override public void mouseClicked(MouseEvent e) { if (!panel.isFocusOwner()) return; if (e.getButton() != MouseEvent.BUTTON1) return; + lastX = e.getX(); + lastY = e.getY(); vtkProp spick[] = panel.pick(e.getX(), e.getY()); if (spick != null && spick.length > 0) { for (vtkProp selectActor : spick) { @@ -271,8 +277,8 @@ public class vtkCameraAndSelectorAction extends vtkAwtAction implements ISelecti @Override public void mouseMoved(MouseEvent e) { lastX = e.getX(); - lastY = e.getY(); - + lastY = e.getY(); + if (!panel.isFocusOwner()) return; List prevHover = new ArrayList(); @@ -289,6 +295,22 @@ public class vtkCameraAndSelectorAction extends vtkAwtAction implements ISelecti } } + /** + * Returns mouse position in AWT screen coordinates. + * @return + */ + public Point2i getMousePositionAWT() { + return new Point2i(lastX, lastY); + } + + /** + * Returns mouse position in VTK screen coordinates. + * @return + */ + public Point2d getMousePosition() { + return new Point2d(lastX, rw.GetSize()[1]-lastY); + } + public List getSelectActor() { return selectActors; } @@ -298,7 +320,7 @@ public class vtkCameraAndSelectorAction extends vtkAwtAction implements ISelecti } private List selectionListeners = new ArrayList(); - + @Override public void addSelectionChangedListener(ISelectionChangedListener listener) { selectionListeners.add(listener); @@ -346,7 +368,7 @@ public class vtkCameraAndSelectorAction extends vtkAwtAction implements ISelecti private List hoverListeners = new ArrayList(); - + public void addHoverChangedListener(ISelectionChangedListener listener) { hoverListeners.add(listener); @@ -380,16 +402,51 @@ public class vtkCameraAndSelectorAction extends vtkAwtAction implements ISelecti }); } - public void focus(double x, double y, double z) { + public vtkCamera getCamera() { + return cam; + } + + public Point3d getPosition() { + double pos[] = cam.GetPosition(); + return new Point3d(pos); + } + + public Point3d getFocus() { + double pos[] = cam.GetFocalPoint(); + return new Point3d(pos); + } + + public void setFocus(Point3d focus) { + setFocus(focus.x,focus.y,focus.z); + } + + public void setFocus(double x, double y, double z) { Lock(); cam.SetFocalPoint(x, y, z); if (doNotRotate) - cam.SetRoll(0); - cam.OrthogonalizeViewUp(); - resetCameraClippingRange(); - panel.UpdateLight(); + cam.SetRoll(0); + cam.OrthogonalizeViewUp(); + ren.ResetCameraClippingRange(); + panel.UpdateLight(); + UnLock(); + } + + public void setPosition(Point3d position) { + setPosition(position.x,position.y,position.z); + } + + public void setPosition(double x, double y, double z) { + Lock(); + cam.SetPosition(x, y, z); + if (doNotRotate) + cam.SetRoll(0); + cam.OrthogonalizeViewUp(); + ren.ResetCameraClippingRange(); + panel.UpdateLight(); UnLock(); } + + }