James Potota
James Potota

Reputation: 157

VtkRenderWindowInteractor doesn't start and cause program freezes [java]

I recently have implemented clipping in my VTK Java program. I used BoxWidget to control what should be clipped. However, i'm having an issue with vtkRenderWindowInteractor that attached to BoxWidget. The program freezes at the renderWindowInteractor.Start() statement (I've remarked it in my code).

This is my re-simulate code :

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import vtk.*;

public class VTKWindowInteractor extends JPanel {

    static {
        if (!vtkNativeLibrary.LoadAllNativeLibraries()) {
            for (vtkNativeLibrary lib : vtkNativeLibrary.values()) {
                if (!lib.IsLoaded()) {
                    System.out.println(lib.GetLibraryName() + " not loaded");
                }
            }
            System.out.println("Make sure the search path is correct: ");
            System.out.println(System.getProperty("java.library.path"));
        }
        vtkNativeLibrary.DisableOutputWindow(null);
    }

    private vtkPanel renWin;
    private vtkRenderWindowInteractor renderWindowInteractor;
    private vtkPolyDataMapper mapper;
    private vtkActor coneActor;
    private vtkPlanes planes;
    private vtkBoxWidget boxWidget;

    public VTKWindowInteractor() {
        setLayout(new BorderLayout());
        renWin = new vtkPanel();
        add(renWin, BorderLayout.CENTER);
        renWin.setMinimumSize(new Dimension(50, 50));
        renWin.GetRenderer().SetBackground(0, 0, 0); // black
        renWin.GetRenderWindow().AddRenderer(renWin.GetRenderer());
    }

    public void render() {

        mapper = new vtkPolyDataMapper();
        vtkConeSource cone = new vtkConeSource();
        cone.SetHeight(3.0);
        cone.SetRadius(1.0);
        cone.SetResolution(10);

        mapper.SetInputConnection(cone.GetOutputPort());
        coneActor = new vtkActor();
        coneActor.SetMapper(mapper);

        renWin.GetRenderer().AddActor(coneActor);

        planes = new vtkPlanes();
        renderWindowInteractor = new vtkRenderWindowInteractor();
        renderWindowInteractor.SetRenderWindow(renWin.GetRenderWindow());
        boxWidget = new vtkBoxWidget();
        boxWidget.SetInteractor(renderWindowInteractor);
        boxWidget.SetPlaceFactor(1.25);
        boxWidget.PlaceWidget(coneActor.GetBounds());
        boxWidget.AddObserver("InteractionEvent", this, "executeClipping");
        renderWindowInteractor.Initialize();
        boxWidget.On();
        renWin.Render();
        renWin.resetCamera();
        /**************************************/
        // This is where the freeze come from //
        //                                   //
        /************************************/
        renderWindowInteractor.Start(); // if i comment out this line, the program works but the boxWidget cannot be resized or rescale or moved
    }

    public void executeClipping() {
        planes = new vtkPlanes();
        boxWidget.GetPlanes(planes);
        mapper.SetClippingPlanes(planes);
        planes.Delete();
    }

    public static final int WINDOW_WIDTH = 1000;
    public static final int WINDOW_HEIGHT = 500;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                VTKWindowInteractor _vtkRendererPanel = new VTKWindowInteractor();

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setTitle("......");
                frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
                frame.setVisible(true);
                frame.setLayout(new BorderLayout());
                frame.add(_vtkRendererPanel);
                _vtkRendererPanel.render();
            }
        });
    }
}

I have been finding my mistake for hours and hours and frustrated hence come here to seek for help. If anyone have experienced this or know what did I do wrong please correct me. Thanks !!!

VTK version : 6.2.0

Upvotes: 2

Views: 634

Answers (1)

James Potota
James Potota

Reputation: 157

OK. I finally solved the problem. The follows is the quote from vtkPanel.java

/* *
* Java AWT component that encapsulate vtkRenderWindow, vtkRenderer, vtkCamera,
* vtkLight.
*
* If a vtkInteractor is needed, use vtkCanvas instead. This is necessary when
* Widget and Picker are used.
*
* @author Kitware */

so I changed my

vtkPanel

to

vtkCanvas

&

renderWindowInteractor = new vtkRenderWindowInteractor();

to

renderWindowInteractor = renWin.getRenderWindowInteractor(); and it solves the problem.

Thanks and this is for anyone who are going to face similiar problem as me in the future.

Upvotes: 1

Related Questions