使用图形函数时的分段错误(vtk 6.1,Qt5.3,Ubuntu)

时间:2015-01-07 03:12:13

标签: c++ qt vtk qvtkwidget

更新:调试信息

我正在研究Qt 5.3项目,需要在2D和3D坐标系中绘制数据。我一直在研究vtk 6.1,因为它看起来非常强大,我还需要在稍后的时间点可视化图像数据。我在这个例子中顺利使用了Qvtkwidget

Qvtkwidget example

#include "mainwindow.h"
#include <QApplication>
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL);

#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkImageViewer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleImage.h>
#include <vtkRenderer.h>
#include <vtkJPEGReader.h>

#include <QVTKWidget.h>

int main(int argc, char** argv)
{
  QApplication app(argc, argv);

  QVTKWidget widget;
  widget.resize(256,256);

  // Setup sphere
  vtkSmartPointer<vtkSphereSource> sphereSource =
      vtkSmartPointer<vtkSphereSource>::New();
  sphereSource->Update();
  vtkSmartPointer<vtkPolyDataMapper> sphereMapper =
      vtkSmartPointer<vtkPolyDataMapper>::New();
  sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
  vtkSmartPointer<vtkActor> sphereActor =
      vtkSmartPointer<vtkActor>::New();
  sphereActor->SetMapper(sphereMapper);

  // Setup window
  vtkSmartPointer<vtkRenderWindow> renderWindow =
      vtkSmartPointer<vtkRenderWindow>::New();

  // Setup renderer
  vtkSmartPointer<vtkRenderer> renderer =
      vtkSmartPointer<vtkRenderer>::New();
  renderWindow->AddRenderer(renderer);

  renderer->AddActor(sphereActor);
  renderer->ResetCamera();

  widget.SetRenderWindow(renderWindow);
  widget.show();

  app.exec();

  return EXIT_SUCCESS;
}

但是当我尝试实现图形示例(/Examples/Charts/Cxx/QChartTable.cxx)时,程序会显示Segmentation Fault错误。

#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL)
#include "vtkFloatArray.h"
#include "vtkMath.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"

#include "vtkSmartPointer.h"

#include "vtkContextView.h"
#include "vtkContextScene.h"
#include "vtkChartXY.h"
#include "vtkPlot.h"
#include "vtkTable.h"

#include "vtkTimerLog.h"

#include <QApplication>
#include <QWidget>
#include <QMainWindow>
#include <QHBoxLayout>

#include "QVTKWidget.h"
#include "vtkQtTableView.h"

#define VTK_CREATE(type, name) \
  vtkSmartPointer<type> name = vtkSmartPointer<type>::New()

int main(int argc, char** argv)
{


  // Qt initialization
  QApplication app(argc, argv);
  QMainWindow mainWindow;
  mainWindow.setGeometry(0, 0, 1150, 600);

  // QVTK set up and initialization
  QVTKWidget *qvtkWidget = new QVTKWidget(&mainWindow);

  // Set up my 2D world...
  VTK_CREATE(vtkContextView, view); // This contains a chart object
  view->SetInteractor(qvtkWidget->GetInteractor());
  qvtkWidget->SetRenderWindow(view->GetRenderWindow());

  // Create a table with some points in it...
  VTK_CREATE(vtkTable, table);
  VTK_CREATE(vtkFloatArray, arrX);
  arrX->SetName("X Axis");
  table->AddColumn(arrX);
  VTK_CREATE(vtkFloatArray, arrC);
  arrC->SetName("Cosine");
  table->AddColumn(arrC);
  VTK_CREATE(vtkFloatArray, arrS);
  arrS->SetName("Sine");
  table->AddColumn(arrS);

  // Make a timer object - need to get some frame rates/render times
  VTK_CREATE(vtkTimerLog, timer);

  // Test charting with a few more points...
  int numPoints = 29;
  float inc = 7.0 / (numPoints-1);
  table->SetNumberOfRows(numPoints);
  for (int i = 0; i < numPoints; ++i)
    {
    table->SetValue(i, 0, i * inc);
    table->SetValue(i, 1, cos(i * inc) + 0.0);
    table->SetValue(i, 2, sin(i * inc) + 0.0);
    }

//   table->Update();

  // Add multiple line plots, setting the colors etc
  vtkSmartPointer<vtkChartXY> chart = vtkSmartPointer<vtkChartXY>::New();
  view->GetScene()->AddItem(chart);
  vtkPlot *line = chart->AddPlot(vtkChart::LINE);
  line->SetInputData(table, 0, 1);
  line->SetColor(255, 0, 0, 255);
  line = chart->AddPlot(vtkChart::LINE);
  line->SetInputData(table, 0, 2);
  line->SetColor(0, 255, 0, 255);
  line->SetWidth(2.0);

  // Instantiate a vtkQtChart and use that too
/*  vtkQtChart *qtChart = new vtkQtChart;
  chart = qtChart->chart();
  line = chart->AddPlot(vtkChart::LINE);
  line->SetTable(table, 0, 1);
  line->SetColor(255, 0, 0, 255);
  line = chart->AddPlot(vtkChart::LINE);
  line->SetTable(table, 0, 2);
  line->SetColor(0, 255, 0, 255);
  line->SetWidth(2.0);
*/
  // Now lets try to add a table view
  QWidget *widget = new QWidget(&mainWindow);
  QHBoxLayout *layout = new QHBoxLayout(widget);
  VTK_CREATE(vtkQtTableView, tableView);
  tableView->SetSplitMultiComponentColumns(true);
  tableView->AddRepresentationFromInput(table);
  tableView->Update();
  layout->addWidget(qvtkWidget, 2);
  //layout->addWidget(qtChart, 2);
  layout->addWidget(tableView->GetWidget());
  mainWindow.setCentralWidget(widget);

  // Now show the application and start the event loop
  mainWindow.show();

  return app.exec();
}

我不知道这个错误是怎么发生的。

我已在调试模式下重新编译VTK并获得更多调试信息。这是该计划的回溯。在这个截图中显示问题可能来自&#34;这个&#34;指针&#34;这个&#34;应该是vtkTextRenderer *但是&#34;这个&#34;指向空指针。我想这可能是VTK 6.1中的一个错误,但不知道如何修复它。请给我一些建议。感谢

我是新手,仍然无法发布图片,所以我将其上传到此链接

Debug Screenshot

0   vtkTextRenderer::GetBoundingBox vtkTextRenderer.h   136 0x7ffff0fbda58  
1   vtkTextRendererStringToImage::GetBounds vtkTextRendererStringToImage.cxx    62  0x7ffff0fbd68b  
2   vtkOpenGLContextDevice2D::ComputeStringBounds   vtkOpenGLContextDevice2D.cxx    885 0x7ffff650d338  
3   vtkOpenGLContextDevice2D::ComputeStringBounds   vtkOpenGLContextDevice2D.cxx    809 0x7ffff650cab4  
4   vtkContext2D::ComputeStringBounds   vtkContext2D.cxx    619 0x7ffff64f24dd  
5   vtkAxis::GetBoundingRect    vtkAxis.cxx 871 0x7ffff7a9d66d  
6   vtkChartXY::UpdateLayout    vtkChartXY.cxx  762 0x7ffff7ac6064  
7   vtkChartXY::Paint   vtkChartXY.cxx  325 0x7ffff7ac4450  
8   vtkContextScenePrivate::PaintItems  vtkContextScenePrivate.h    80  0x7ffff64ea83a  
9   vtkContextScene::Paint  vtkContextScene.cxx 120 0x7ffff64fc2f2  
10  vtkContextActor::RenderOverlay  vtkContextActor.cxx 239 0x7ffff64f5787  
11  vtkRenderer::UpdateGeometry vtkRenderer.cxx 585 0x7ffff3518bc3  
12  vtkOpenGLRenderer::DeviceRender vtkOpenGLRenderer.cxx   270 0x7ffff61102c0  
13  vtkRenderer::Render vtkRenderer.cxx 292 0x7ffff3517dfb  
14  vtkRendererCollection::Render   vtkRendererCollection.cxx   51  0x7ffff35164d3  
15  vtkRenderWindow::DoStereoRender vtkRenderWindow.cxx 759 0x7ffff352bc36  
16  vtkRenderWindow::DoFDRender vtkRenderWindow.cxx 728 0x7ffff352bb46  
17  vtkRenderWindow::DoAARender vtkRenderWindow.cxx 607 0x7ffff352b4f2  
18  vtkRenderWindow::Render vtkRenderWindow.cxx 423 0x7ffff352aac2  
19  vtkXOpenGLRenderWindow::Render  vtkXOpenGLRenderWindow.cxx  1831    0x7ffff61abc51  
20  vtkRenderWindowInteractor::Render   vtkRenderWindowInteractor.cxx   168 0x7ffff3537751  
... <More>              

2 个答案:

答案 0 :(得分:2)

这不是一个错误。对象vtkTextRenderer指向null,因为尚未实现Text Font模块。它应该使用vtkRenderingFreeType实现。 请尝试在代码中包含此代码段:

#include "vtkAutoInit.h"
VTK_MODULE_INIT(vtkRenderingFreeType);

可在此处找到更多详细信息:http://www.vtk.org/Wiki/VTK/VTK_6_Migration/Factories_now_require_defines

答案 1 :(得分:0)

我已在调试模式下重新编译VTK并获得更多调试信息。这是该计划的回溯。在这个截图中显示问题可能来自“this”指针“this”应该是vtkTextRenderer *但是“this”指向null指针。我想这可能是VTK 6.1中的一个错误,但不知道如何修复它。请给我一些建议。感谢

在第一篇文章中更新调试信息。