Advertisement
Guest User

VTK code

a guest
Aug 4th, 2015
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.24 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.Dimension;
  3. import javax.swing.JPanel;
  4. import javax.swing.JFrame;
  5. import javax.swing.SwingUtilities;
  6. import vtk.*;
  7.  
  8. public class VTKWindowInteractor extends JPanel {
  9.  
  10.     static {
  11.         if (!vtkNativeLibrary.LoadAllNativeLibraries()) {
  12.             for (vtkNativeLibrary lib : vtkNativeLibrary.values()) {
  13.                 if (!lib.IsLoaded()) {
  14.                     System.out.println(lib.GetLibraryName() + " not loaded");
  15.                 }
  16.             }
  17.             System.out.println("Make sure the search path is correct: ");
  18.             System.out.println(System.getProperty("java.library.path"));
  19.         }
  20.         vtkNativeLibrary.DisableOutputWindow(null);
  21.     }
  22.  
  23.     private vtkPanel renWin;
  24.     private vtkRenderWindowInteractor renderWindowInteractor;
  25.     private vtkPolyDataMapper mapper;
  26.     private vtkActor coneActor;
  27.     private vtkPlanes planes;
  28.     private vtkBoxWidget boxWidget;
  29.  
  30.     public VTKWindowInteractor() {
  31.         setLayout(new BorderLayout());
  32.         renWin = new vtkPanel();
  33.         add(renWin, BorderLayout.CENTER);
  34.         renWin.setMinimumSize(new Dimension(50, 50));
  35.         renWin.GetRenderer().SetBackground(0, 0, 0); // black
  36.         renWin.GetRenderWindow().AddRenderer(renWin.GetRenderer());
  37.     }
  38.  
  39.     public void render() {
  40.  
  41.         mapper = new vtkPolyDataMapper();
  42.         vtkConeSource cone = new vtkConeSource();
  43.         cone.SetHeight(3.0);
  44.         cone.SetRadius(1.0);
  45.         cone.SetResolution(10);
  46.  
  47.         mapper.SetInputConnection(cone.GetOutputPort());
  48.         coneActor = new vtkActor();
  49.         coneActor.SetMapper(mapper);
  50.  
  51.         renWin.GetRenderer().AddActor(coneActor);
  52.        
  53.         planes = new vtkPlanes();
  54.         renderWindowInteractor = new vtkRenderWindowInteractor();
  55.         renderWindowInteractor.SetRenderWindow(renWin.GetRenderWindow());
  56.         boxWidget = new vtkBoxWidget();
  57.         boxWidget.SetInteractor(renderWindowInteractor);
  58.         boxWidget.SetPlaceFactor(1.25);
  59.         boxWidget.PlaceWidget(coneActor.GetBounds());
  60.         boxWidget.AddObserver("InteractionEvent", this, "executeClipping");
  61.         renderWindowInteractor.Initialize();
  62.         boxWidget.On();
  63.         renWin.Render();
  64.         renWin.resetCamera();
  65.         renderWindowInteractor.Start();
  66.     }
  67.  
  68.     public void executeClipping() {
  69.         planes = new vtkPlanes();
  70.         boxWidget.GetPlanes(planes);
  71.         mapper.SetClippingPlanes(planes);
  72.         planes.Delete();
  73.     }
  74.  
  75.     public static final int WINDOW_WIDTH = 1000;
  76.     public static final int WINDOW_HEIGHT = 500;
  77.  
  78.     public static void main(String[] args) {
  79.         SwingUtilities.invokeLater(new Runnable() {
  80.             public void run() {
  81.                 VTKWindowInteractor _vtkRendererPanel = new VTKWindowInteractor();
  82.  
  83.                 JFrame frame = new JFrame();
  84.                 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  85.                 frame.setTitle("......");
  86.                 frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
  87.                 frame.setVisible(true);
  88.                 frame.setLayout(new BorderLayout());
  89.                 frame.add(_vtkRendererPanel);
  90.                 _vtkRendererPanel.render();
  91.             }
  92.         });
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement