File: JoglConeRendering.java

package info (click to toggle)
vtk7 7.1.1%2Bdfsg2-8
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 127,396 kB
  • sloc: cpp: 1,539,584; ansic: 124,382; python: 78,038; tcl: 47,013; xml: 8,142; yacc: 5,040; java: 4,439; perl: 3,132; lex: 1,926; sh: 1,500; makefile: 126; objc: 83
file content (178 lines) | stat: -rw-r--r-- 6,467 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package vtk.sample.rendering;

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.util.Arrays;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import vtk.vtkActor;
import vtk.vtkBoxRepresentation;
import vtk.vtkBoxWidget2;
import vtk.vtkCell;
import vtk.vtkCellPicker;
import vtk.vtkConeSource;
import vtk.vtkLookupTable;
import vtk.vtkNativeLibrary;
import vtk.vtkPolyDataMapper;
import vtk.vtkScalarBarRepresentation;
import vtk.vtkScalarBarWidget;
import vtk.vtkTransform;
import vtk.rendering.vtkAbstractEventInterceptor;
import vtk.rendering.vtkEventInterceptor;
import vtk.rendering.jogl.vtkAbstractJoglComponent;
import vtk.rendering.jogl.vtkJoglCanvasComponent;
import vtk.rendering.jogl.vtkJoglPanelComponent;

public class JoglConeRendering {
  // -----------------------------------------------------------------
  // Load VTK library and print which library was not properly loaded

  static {
    if (!vtkNativeLibrary.LoadAllNativeLibraries()) {
      for (vtkNativeLibrary lib : vtkNativeLibrary.values()) {
        if (!lib.IsLoaded()) {
          System.out.println(lib.GetLibraryName() + " not loaded");
        }
      }
    }
    vtkNativeLibrary.DisableOutputWindow(null);
  }

  public static void main(String[] args) {
    final boolean usePanel = Boolean.getBoolean("usePanel");

    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
        // build VTK Pipeline
        vtkConeSource cone = new vtkConeSource();
        cone.SetResolution(8);
        cone.Update();

        vtkPolyDataMapper coneMapper = new vtkPolyDataMapper();
        coneMapper.SetInputConnection(cone.GetOutputPort());

        final vtkActor coneActor = new vtkActor();
        coneActor.SetMapper(coneMapper);

        // VTK rendering part
        final vtkAbstractJoglComponent<?> joglWidget = usePanel ? new vtkJoglPanelComponent() : new vtkJoglCanvasComponent();
        System.out.println("We are using " + joglWidget.getComponent().getClass().getName() + " for the rendering.");

        joglWidget.getRenderer().AddActor(coneActor);

        // Add orientation axes
        vtkAbstractJoglComponent.attachOrientationAxes(joglWidget);

        // Add Scalar bar widget
        vtkLookupTable lut = new vtkLookupTable();
        lut.SetHueRange(.66, 0);
        lut.Build();
        vtkScalarBarWidget scalarBar = new vtkScalarBarWidget();
        scalarBar.SetInteractor(joglWidget.getRenderWindowInteractor());

        scalarBar.GetScalarBarActor().SetTitle("Example");
        scalarBar.GetScalarBarActor().SetLookupTable(lut);
        scalarBar.GetScalarBarActor().SetOrientationToHorizontal();
        scalarBar.GetScalarBarActor().SetTextPositionToPrecedeScalarBar();
        vtkScalarBarRepresentation srep = (vtkScalarBarRepresentation) scalarBar.GetRepresentation();
        srep.SetPosition(0.5, 0.053796);
        srep.SetPosition2(0.33, 0.106455);
        //scalarBar.ProcessEventsOff();
        scalarBar.EnabledOn();
        scalarBar.RepositionableOn();

        // Add interactive 3D Widget
        final vtkBoxRepresentation representation = new vtkBoxRepresentation();
        representation.SetPlaceFactor(1.25);
        representation.PlaceWidget(cone.GetOutput().GetBounds());

        final vtkBoxWidget2 boxWidget = new vtkBoxWidget2();
        boxWidget.SetRepresentation(representation);
        boxWidget.SetInteractor(joglWidget.getRenderWindowInteractor());
        boxWidget.SetPriority(1);

        final Runnable callback = new Runnable() {
          vtkTransform trasform = new vtkTransform();

          public void run() {
            vtkBoxRepresentation rep = (vtkBoxRepresentation) boxWidget.GetRepresentation();
            rep.GetTransform(trasform);
            coneActor.SetUserTransform(trasform);
          }
        };

        // Bind widget
        boxWidget.AddObserver("InteractionEvent", callback, "run");
        representation.VisibilityOn();
        representation.HandlesOn();
        boxWidget.SetEnabled(1);
        boxWidget.SetMoveFacesEnabled(1);

        // Add cell picker
        final vtkCellPicker picker = new vtkCellPicker();
        Runnable pickerCallback = new Runnable() {
          public void run() {
            if(picker.GetCellId() != -1) {
              vtkCell cell = picker.GetDataSet().GetCell(picker.GetCellId());
              System.out.println("Pick cell: " +  picker.GetCellId() + " - Bounds: " + Arrays.toString(cell.GetBounds()));
            }
          }
        };
        joglWidget.getRenderWindowInteractor().SetPicker(picker);
        picker.AddObserver("EndPickEvent", pickerCallback, "run");

        // Bind pick action to double-click
        joglWidget.getInteractorForwarder().setEventInterceptor(new vtkAbstractEventInterceptor() {

          public boolean mouseClicked(MouseEvent e) {
            // Request picking action on double-click
            final double[] position = {e.getX(), joglWidget.getComponent().getHeight() - e.getY(), 0};
            if(e.getClickCount() == 2) {
              System.out.println("Click trigger the picking (" + position[0] + ", " +position[1] + ")");
              picker.Pick(position, joglWidget.getRenderer());
            }

            // We let the InteractionStyle process the event anyway
            return false;
          }
        });

        // UI part
        JFrame frame = new JFrame("SimpleVTK");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new BorderLayout());
        frame.getContentPane().add(joglWidget.getComponent(),
            BorderLayout.CENTER);
        frame.setSize(400, 400);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        joglWidget.resetCamera();
        joglWidget.getComponent().requestFocus();

        // Add r:ResetCamera and q:Quit key binding
        joglWidget.getComponent().addKeyListener(new KeyListener() {
          @Override
          public void keyTyped(KeyEvent e) {
            if (e.getKeyChar() == 'r') {
              joglWidget.resetCamera();
            } else if (e.getKeyChar() == 'q') {
              System.exit(0);
            }
          }

          @Override
          public void keyReleased(KeyEvent e) {
          }

          @Override
          public void keyPressed(KeyEvent e) {
          }
        });
      }
    });
  }
}