gpt4 book ai didi

java - VtkRenderWindowInteractor 不启动并导致程序卡住 [java]

转载 作者:行者123 更新时间:2023-11-30 10:59:47 31 4
gpt4 key购买 nike

我最近在我的 VTK Java 程序中实现了裁剪。我使用 BoxWidget 来控制应该剪裁的内容。但是,我遇到了附加到 BoxWidget 的 vtkRenderWindowInteractor 的问题。程序在 renderWindowInteractor.Start() 语句处卡住(我已在我的代码中注明)。

这是我重新模拟的代码:

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();
}
});
}
}

几个小时以来,我一直在寻找自己的错误,并且感到沮丧,因此来这里寻求帮助。如果有人遇到过这种情况或知道我做错了什么,请纠正我。谢谢!!!

VTK 版本:6.2.0

最佳答案

好的。我终于解决了这个问题。以下是引用自 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 */

所以我改变了我的

vtk面板

vtkCanvas

&

renderWindowInteractor = new vtkRenderWindowInteractor();

renderWindowInteractor = renWin.getRenderWindowInteractor();它解决了这个问题。

谢谢,这是为将来会遇到与我类似问题的任何人准备的。

关于java - VtkRenderWindowInteractor 不启动并导致程序卡住 [java],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31802089/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com