X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=vtk%2Fsrc%2Fvtk%2FvtkPanel.java;h=aaa340ed4b5bed846762350ada3a43c48543b96a;hb=efb52587babd1b858c8e77fedc44a5b4ef0c1088;hp=601a949f17864a71bc97e0e3b25b304e09fe4e2c;hpb=471c46e9a685e20de958d77166461aebc7a7b6ee;p=simantics%2F3d.git diff --git a/vtk/src/vtk/vtkPanel.java b/vtk/src/vtk/vtkPanel.java index 601a949f..aaa340ed 100644 --- a/vtk/src/vtk/vtkPanel.java +++ b/vtk/src/vtk/vtkPanel.java @@ -8,6 +8,8 @@ import java.awt.event.KeyListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.event.MouseMotionListener; +import java.awt.event.MouseWheelEvent; +import java.awt.event.MouseWheelListener; import javax.swing.SwingUtilities; @@ -20,442 +22,410 @@ import javax.swing.SwingUtilities; * * @author Kitware */ -public class vtkPanel extends Canvas implements MouseListener, MouseMotionListener, KeyListener { - private static final long serialVersionUID = 1L; - protected vtkRenderWindow rw = new vtkRenderWindow(); - protected vtkRenderer ren = new vtkRenderer(); - protected vtkCamera cam = null; - protected vtkLight lgt = new vtkLight(); - protected int lastX; - protected int lastY; - protected int windowset = 0; - protected int lightingset = 0; - protected int LightFollowCamera = 1; - protected int InteractionMode = 1; - protected boolean rendering = false; - - static { - System.loadLibrary("vtksys"); - System.loadLibrary("vtkCommon"); - System.loadLibrary("vtkCommonJava"); - System.loadLibrary("vtkFiltering"); - System.loadLibrary("vtkexpat"); - System.loadLibrary("vtkjpeg"); - System.loadLibrary("vtkzlib"); - System.loadLibrary("vtktiff"); - System.loadLibrary("vtkpng"); - System.loadLibrary("vtkmetaio"); - System.loadLibrary("vtkhdf5"); - System.loadLibrary("vtkhdf5_hl"); - System.loadLibrary("vtkNetCDF"); - System.loadLibrary("vtkNetCDF_cxx"); - System.loadLibrary("vtkDICOMParser"); - System.loadLibrary("vtkFilteringJava"); - System.loadLibrary("LSDyna"); - System.loadLibrary("vtkIO"); - System.loadLibrary("vtkIOJava"); - System.loadLibrary("vtkImaging"); -// System.loadLibrary("jawt"); - System.loadLibrary("vtkImagingJava"); - System.loadLibrary("vtkverdict"); - System.loadLibrary("vtkGraphics"); - System.loadLibrary("vtkfreetype"); - System.loadLibrary("vtkftgl"); - System.loadLibrary("vtkGraphicsJava"); - System.loadLibrary("vtkRendering"); - System.loadLibrary("vtkRenderingJava"); - vtkNativeLibrary.COMMON.LoadLibrary(); - vtkNativeLibrary.FILTERING.LoadLibrary(); - vtkNativeLibrary.IO.LoadLibrary(); - vtkNativeLibrary.IMAGING.LoadLibrary(); - vtkNativeLibrary.GRAPHICS.LoadLibrary(); - vtkNativeLibrary.RENDERING.LoadLibrary(); - try { - System.loadLibrary("vtkexoIIc"); - System.loadLibrary("vtkHybrid"); - System.loadLibrary("vtkHybridJava"); - } catch (UnsatisfiedLinkError e) { - System.out.println("cannot load vtkHybrid, skipping..."); - } - try { - System.loadLibrary("vtkVolumeRendering"); - System.loadLibrary("vtkVolumeRenderingJava"); - vtkNativeLibrary.VOLUME_RENDERING.LoadLibrary(); - } catch (Throwable e) { - System.out.println("cannot load vtkVolumeRendering, skipping..."); - } - System.loadLibrary("vtkWidgets"); - System.loadLibrary("vtkWidgetsJava"); - } - - // Allow access to display lock() and unlock(). - // Call these whenever you call something that causes - // a Render(). - // e.g. - // panel.lock(); - // // Code that causes a render - // panel.unlock(); - public void lock() { +public class vtkPanel extends Canvas implements MouseListener, MouseMotionListener, MouseWheelListener, KeyListener { + private static final long serialVersionUID = 1L; + protected vtkRenderWindow rw = new vtkRenderWindow(); + protected vtkRenderer ren = new vtkRenderer(); + protected vtkCamera cam = null; + protected vtkLight lgt = new vtkLight(); + protected int lastX; + protected int lastY; + protected int windowset = 0; + protected int lightingset = 0; + protected int LightFollowCamera = 1; + protected int InteractionMode = 1; + protected boolean rendering = false; + + static { + vtkJavaDep.load(); + } + + // Allow access to display lock() and unlock(). + // Call these whenever you call something that causes + // a Render(). + // e.g. + // panel.lock(); + // // Code that causes a render + // panel.unlock(); + public void lock() { + Lock(); + } + + public void unlock() { + UnLock(); + } + + public void Delete() { + if(rendering) { + return; + } + rendering = true; + // We prevent any further rendering + + if (this.getParent() != null) { + this.getParent().remove(this); + } + // Free internal VTK objects + ren = null; + cam = null; + lgt = null; + // On linux we prefer to have a memory leak instead of a crash + if(!rw.GetClassName().equals("vtkXOpenGLRenderWindow")) { + rw = null; + } else { + System.out.println("The renderwindow has been kept around to prevent a crash"); + } + } + + protected native int RenderCreate(vtkRenderWindow id0); + + protected native int Lock(); + + protected native int UnLock(); + + public vtkPanel() { + + rw.AddRenderer(ren); + addMouseListener(this); + addMouseMotionListener(this); + addMouseWheelListener(this); + addKeyListener(this); + super.setSize(200, 200); + rw.SetSize(200, 200); + } + + public vtkPanel(vtkRenderWindow renwin) { + rw = renwin; + rw.AddRenderer(ren); + addMouseListener(this); + addMouseMotionListener(this); + addKeyListener(this); + super.setSize(200, 200); + rw.SetSize(200, 200); + } + + public void Report() { + + // must be performed on awt event thread + Runnable updateAComponent = new Runnable() { + public void run() { Lock(); - } - - public void unlock() { + System.out.println("direct rendering = " + (rw.IsDirect() == 1)); + System.out.println("opengl supported = " + (rw.SupportsOpenGL() == 1)); + System.out.println("report = " + rw.ReportCapabilities()); UnLock(); - } - - public void Delete() { - if(rendering) { - return; - } - rendering = true; - // We prevent any further rendering - - if (this.getParent() != null) { - this.getParent().remove(this); - } - // Free internal VTK objects - ren = null; - cam = null; - lgt = null; - // On linux we prefer to have a memory leak instead of a crash - if(!rw.GetClassName().equals("vtkXOpenGLRenderWindow")) { - rw = null; - } else { - System.out.println("The renderwindow has been kept arount to prevent a crash"); - } - } - - protected native int RenderCreate(vtkRenderWindow id0); - - protected native int Lock(); - - protected native int UnLock(); - - public vtkPanel() { - rw.AddRenderer(ren); - addMouseListener(this); - addMouseMotionListener(this); - addKeyListener(this); - super.setSize(200, 200); - rw.SetSize(200, 200); - } - - public vtkPanel(vtkRenderWindow renwin) { - rw = renwin; - rw.AddRenderer(ren); - addMouseListener(this); - addMouseMotionListener(this); - addKeyListener(this); - super.setSize(200, 200); - rw.SetSize(200, 200); - } - - public void Report() { - - // must be performed on awt event thread - Runnable updateAComponent = new Runnable() { - public void run() { - Lock(); - System.out.println("direct rendering = " + (rw.IsDirect() == 1)); - System.out.println("opengl supported = " + (rw.SupportsOpenGL() == 1)); - System.out.println("report = " + rw.ReportCapabilities()); - UnLock(); - } - }; - - SwingUtilities.invokeLater(updateAComponent); - - } - - public vtkRenderer GetRenderer() { - return ren; - } - - public vtkRenderWindow GetRenderWindow() { - return rw; - } - - public void setSize(int x, int y) { - super.setSize(x, y); - if (windowset == 1) { - Lock(); - rw.SetSize(x, y); - UnLock(); - } - } - - public void addNotify() { - super.addNotify(); - windowset = 0; - rw.SetForceMakeCurrent(); + } + }; + + SwingUtilities.invokeLater(updateAComponent); + + } + + public vtkRenderer GetRenderer() { + return ren; + } + + public vtkRenderWindow GetRenderWindow() { + return rw; + } + + public void setSize(int x, int y) { + super.setSize(x, y); + if (windowset == 1) { + Lock(); + rw.SetSize(x, y); + UnLock(); + } + } + + public void addNotify() { + super.addNotify(); + windowset = 0; + rw.SetForceMakeCurrent(); + rendering = false; + } + + public void removeNotify() { + rendering = true; + super.removeNotify(); + } + + public synchronized void Render() { + if (!rendering) { + rendering = true; + if (ren.VisibleActorCount() == 0) { rendering = false; - } - - public void removeNotify() { - rendering = true; - super.removeNotify(); - } - - public synchronized void Render() { - if (!rendering) { - rendering = true; - if (ren.VisibleActorCount() == 0) { - rendering = false; - return; - } - if (rw != null) { - if (windowset == 0) { - // set the window id and the active camera - cam = ren.GetActiveCamera(); - if (lightingset == 0) { - ren.AddLight(lgt); - lgt.SetPosition(cam.GetPosition()); - lgt.SetFocalPoint(cam.GetFocalPoint()); - lightingset = 1; - } - RenderCreate(rw); - Lock(); - rw.SetSize(getWidth(), getHeight()); - UnLock(); - windowset = 1; - this.setSize(getWidth(), getHeight()); - } - Lock(); - rw.Render(); - UnLock(); - rendering = false; - } + return; + } + if (rw != null) { + if (windowset == 0) { + // set the window id and the active camera + cam = ren.GetActiveCamera(); + if (lightingset == 0) { + ren.AddLight(lgt); + lgt.SetPosition(cam.GetPosition()); + lgt.SetFocalPoint(cam.GetFocalPoint()); + lightingset = 1; + } + RenderCreate(rw); + Lock(); + rw.SetSize(getWidth(), getHeight()); + UnLock(); + windowset = 1; + this.setSize(getWidth(), getHeight()); } - } - - public boolean isWindowSet() { - return (this.windowset == 1); - } - - public void paint(Graphics g) { - this.Render(); - } - - public void update(Graphics g) { - paint(g); - } - - public void LightFollowCameraOn() { - this.LightFollowCamera = 1; - } - - public void LightFollowCameraOff() { - this.LightFollowCamera = 0; - } - - public void InteractionModeRotate() { - this.InteractionMode = 1; - } - - public void InteractionModeTranslate() { - this.InteractionMode = 2; - } - - public void InteractionModeZoom() { - this.InteractionMode = 3; - } - - public void UpdateLight() { - lgt.SetPosition(cam.GetPosition()); - lgt.SetFocalPoint(cam.GetFocalPoint()); - } - - public void resetCameraClippingRange() { - Lock(); - ren.ResetCameraClippingRange(); - UnLock(); - } - - public void resetCamera() { - Lock(); - ren.ResetCamera(); - UnLock(); - } - - public void mouseClicked(MouseEvent e) { - - } - - public void mousePressed(MouseEvent e) { - - if (ren.VisibleActorCount() == 0) - return; - rw.SetDesiredUpdateRate(5.0); - 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(0.01); - } - - public void mouseEntered(MouseEvent e) { - this.requestFocus(); - } - - public void mouseExited(MouseEvent e) { - } - - public void mouseMoved(MouseEvent e) { - lastX = e.getX(); - lastY = e.getY(); - } - - 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); - cam.OrthogonalizeViewUp(); - resetCameraClippingRange(); - if (this.LightFollowCamera == 1) { - lgt.SetPosition(cam.GetPosition()); - lgt.SetFocalPoint(cam.GetFocalPoint()); - } - } - // 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; - 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; - this.Render(); - } - - public void keyTyped(KeyEvent e) { - } - - public void keyPressed(KeyEvent e) { - if (ren.VisibleActorCount() == 0) - return; - char keyChar = e.getKeyChar(); - - if ('r' == keyChar) { - resetCamera(); - this.Render(); - } - if ('u' == keyChar) { - pickActor(lastX, lastY); - } - if ('w' == keyChar) { - vtkActorCollection ac; - vtkActor anActor; - int i; - - ac = ren.GetActors(); - ac.InitTraversal(); - for (i = 0; i < ac.GetNumberOfItems(); i++) { - anActor = ac.GetNextActor(); - anActor.GetProperty().SetRepresentationToWireframe(); - } - this.Render(); - } - if ('s' == keyChar) { - vtkActorCollection ac; - vtkActor anActor; - int i; - - ac = ren.GetActors(); - ac.InitTraversal(); - for (i = 0; i < ac.GetNumberOfItems(); i++) { - anActor = ac.GetNextActor(); - anActor.GetProperty().SetRepresentationToSurface(); - } - this.Render(); - } - } - - public void HardCopy(String filename, int mag) { - - Lock(); - - vtkWindowToImageFilter w2if = new vtkWindowToImageFilter(); - w2if.SetInput(rw); - - w2if.SetMagnification(mag); - w2if.Update(); - - vtkTIFFWriter writer = new vtkTIFFWriter(); - writer.SetInput(w2if.GetOutput()); - writer.SetFileName(filename); - writer.Write(); - - UnLock(); - } - - public void pickActor(int x, int y) { - - vtkPropPicker picker = new vtkPropPicker(); - Lock(); - picker.PickProp(x, rw.GetSize()[1] - y, ren); + rw.Render(); UnLock(); - - if (picker.GetActor() != null) - System.out.println(picker.GetActor().GetClassName()); - } - - public void keyReleased(KeyEvent e) { - } + rendering = false; + } + } + } + + public boolean isWindowSet() { + return (this.windowset == 1); + } + + public void paint(Graphics g) { + this.Render(); + } + + public void update(Graphics g) { + paint(g); + } + + public void LightFollowCameraOn() { + this.LightFollowCamera = 1; + } + + public void LightFollowCameraOff() { + this.LightFollowCamera = 0; + } + + public void InteractionModeRotate() { + this.InteractionMode = 1; + } + + public void InteractionModeTranslate() { + this.InteractionMode = 2; + } + + public void InteractionModeZoom() { + this.InteractionMode = 3; + } + + public void UpdateLight() { + lgt.SetPosition(cam.GetPosition()); + lgt.SetFocalPoint(cam.GetFocalPoint()); + } + + public void resetCameraClippingRange() { + Lock(); + ren.ResetCameraClippingRange(); + UnLock(); + } + + public void resetCamera() { + Lock(); + ren.ResetCamera(); + UnLock(); + } + + public void mouseClicked(MouseEvent e) { + + } + + public void mousePressed(MouseEvent e) { + + if (ren.VisibleActorCount() == 0) + return; + rw.SetDesiredUpdateRate(5.0); + 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(0.01); + } + + public void mouseEntered(MouseEvent e) { + this.requestFocus(); + } + + public void mouseExited(MouseEvent e) { + } + + public void mouseMoved(MouseEvent e) { + lastX = e.getX(); + lastY = e.getY(); + } + + 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); + cam.OrthogonalizeViewUp(); + resetCameraClippingRange(); + if (this.LightFollowCamera == 1) { + lgt.SetPosition(cam.GetPosition()); + lgt.SetFocalPoint(cam.GetFocalPoint()); + } + } + // 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; + 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; + this.Render(); + } + + public void mouseWheelMoved(MouseWheelEvent e) { + if (ren.VisibleActorCount() == 0 || e.getWheelRotation() == 0) + return; + int exponent; + exponent = -10 * e.getWheelRotation() / Math.abs(e.getWheelRotation()); + double zoomFactor; + zoomFactor = Math.pow(1.02, exponent); + if (cam.GetParallelProjection() == 1) { + cam.SetParallelScale(cam.GetParallelScale() / zoomFactor); + } else { + cam.Dolly(zoomFactor); + resetCameraClippingRange(); + } + this.Render(); + } + + public void keyTyped(KeyEvent e) { + } + + public void keyPressed(KeyEvent e) { + if (ren.VisibleActorCount() == 0) + return; + char keyChar = e.getKeyChar(); + + if ('r' == keyChar) { + resetCamera(); + this.Render(); + } + if ('u' == keyChar) { + pickActor(lastX, lastY); + } + if ('w' == keyChar) { + vtkActorCollection ac; + vtkActor anActor; + int i; + + ac = ren.GetActors(); + ac.InitTraversal(); + for (i = 0; i < ac.GetNumberOfItems(); i++) { + anActor = ac.GetNextActor(); + anActor.GetProperty().SetRepresentationToWireframe(); + } + this.Render(); + } + if ('s' == keyChar) { + vtkActorCollection ac; + vtkActor anActor; + int i; + + ac = ren.GetActors(); + ac.InitTraversal(); + for (i = 0; i < ac.GetNumberOfItems(); i++) { + anActor = ac.GetNextActor(); + anActor.GetProperty().SetRepresentationToSurface(); + } + this.Render(); + } + } + + public void HardCopy(String filename, int mag) { + + Lock(); + + vtkWindowToImageFilter w2if = new vtkWindowToImageFilter(); + w2if.SetInput(rw); + + w2if.SetScale(mag); + w2if.Update(); + + vtkTIFFWriter writer = new vtkTIFFWriter(); + writer.SetInputConnection(w2if.GetOutputPort()); + writer.SetFileName(filename); + writer.Write(); + + UnLock(); + } + + public void pickActor(int x, int y) { + + vtkPropPicker picker = new vtkPropPicker(); + + Lock(); + picker.PickProp(x, rw.GetSize()[1] - y, ren); + UnLock(); + + if (picker.GetActor() != null) + System.out.println(picker.GetActor().GetClassName()); + } + + public void keyReleased(KeyEvent e) { + } }