radixvtkerrorbars.cc 4.77 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingContextOpenGL2)
VTK_MODULE_INIT(vtkRenderingOpenGL2)
VTK_MODULE_INIT(vtkInteractionStyle)
VTK_MODULE_INIT(vtkRenderingFreeType)

#include <QApplication>
#include <QGridLayout>
#include <QMainWindow>
#include <QWidget>

#include "vtkFloatArray.h"
#include "vtkTable.h"

15
#include "radixwidgets/vtkPlotErrorBars.hh"
16
#include "radixwidgets/vtkPlotLineErrorBars.hh"
17
#include "radixwidgets/vtkPlotPointsErrorBars.hh"
18
19
#include "radixwidgets/vtkchartwidget.hh"

20
21
#include "radixvtkerrorbars.hh"

22
23
24
25
26
vtkChartMainWindow::vtkChartMainWindow(QWidget* parent)
    : QMainWindow(parent)
{
  setGeometry(400, 250, 542, 390);
  // needed to ensure appropriate OpenGL context is created for VTK rendering.
27
  QSurfaceFormat::setDefaultFormat(QVTKWidget_t::defaultFormat());
28
29
30
31
32
33
34
35
36
37

  // Test widget
  QWidget* widget                       = new QWidget(this);
  QGridLayout* layout                   = new QGridLayout(widget);
  radix::VTKChartWidget* vtkChartWidget = new radix::VTKChartWidget(this);
  layout->addWidget(vtkChartWidget, 0, 0);
  setCentralWidget(widget);

  // Create a table with some points in it
  vtkNew<vtkTable> table;
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
  vtkNew<vtkFloatArray> xArray;
  xArray->SetName("x");
  table->AddColumn(xArray);
  vtkNew<vtkFloatArray> sinArray;
  sinArray->SetName("sin");
  table->AddColumn(sinArray);
  vtkNew<vtkFloatArray> cosArray;
  cosArray->SetName("cos");
  table->AddColumn(cosArray);
  vtkNew<vtkFloatArray> sumArray;
  sumArray->SetName("sum");
  table->AddColumn(sumArray);
  vtkNew<vtkFloatArray> subArray;
  subArray->SetName("subtract");
  table->AddColumn(subArray);
  vtkNew<vtkFloatArray> multArray;
  multArray->SetName("multiply");
  table->AddColumn(multArray);
  vtkNew<vtkFloatArray> xErrorArray;
  xErrorArray->SetName("x error");
  table->AddColumn(xErrorArray);
  vtkNew<vtkFloatArray> cosErrorArray;
  cosErrorArray->SetName("cos error");
  table->AddColumn(cosErrorArray);
  vtkNew<vtkFloatArray> sumErrorArray;
  sumErrorArray->SetName("sum error");
  table->AddColumn(sumErrorArray);
  vtkNew<vtkFloatArray> subErrorArray;
  subErrorArray->SetName("sub error");
  table->AddColumn(subErrorArray);

  // Add some data points to the chart
  int numPoints = 100;
  float maxX = 15.0f;
72
73
74
  table->SetNumberOfRows(numPoints);
  for (int i = 0; i < numPoints; ++i)
  {
75
76
77
78
79
80
81
82
83
84
85
    float num = i * maxX / numPoints;
    table->SetValue(i, 0, num);
    table->SetValue(i, 1, sin(num));
    table->SetValue(i, 2, cos(num) + 1.0);
    table->SetValue(i, 3, sin(num) + cos(num) + 3.0);
    table->SetValue(i, 4, sin(num) - cos(num) + 4.0);
    table->SetValue(i, 5, sin(num) * cos(num) + 5.0);
    table->SetValue(i, 6, abs(num / 10.0));
    table->SetValue(i, 7, abs(cos(num) / 3.0));
    table->SetValue(i, 8, abs((sin(num) + cos(num)) / 3.0));
    table->SetValue(i, 9, abs((sin(num) - cos(num)) / 3.0));
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
  // Add a line plot without errors (sin)
  vtkPlotLineErrorBars* sinPlot = vtkPlotLineErrorBars::New();
  vtkChartWidget->chartXY()->AddPlot(sinPlot);
  sinPlot->SetInputData(table, 0, 1);
  sinPlot->SetColor(0, 0, 0, 255);
  sinPlot->SetWidth(1.0);

   // Add a points plot with errors (cos)
   vtkPlotPointsErrorBars* cosPlot = vtkPlotPointsErrorBars::New();
   vtkChartWidget->chartXY()->AddPlot(cosPlot);
   cosPlot->SetInputData(table, 0, 2, 6, 7);
   cosPlot->SetPlotErrorBars(1);
   cosPlot->SetColor(0, 255, 0, 255);
   cosPlot->SetWidth(2.0);

   // Add a points plot with errors, but don't plot the errors (sum)
   vtkPlotPointsErrorBars* sumPlot = vtkPlotPointsErrorBars::New();
   vtkChartWidget->chartXY()->AddPlot(sumPlot);
   sumPlot->SetInputData(table, 0, 3, 6, 8);
   sumPlot->SetPlotErrorBars(0);
   sumPlot->SetColor(0, 0, 255, 255);
   sumPlot->SetWidth(2.0);

   // Add a line plot with errors (subtract)
   vtkPlotLineErrorBars* subPlot = vtkPlotLineErrorBars::New();
   vtkChartWidget->chartXY()->AddPlot(subPlot);
   subPlot->SetInputData(table, 0, 4, 6, 9);
   subPlot->SetPlotErrorBars(1);
   subPlot->SetColor(255, 0, 0, 255);
   subPlot->SetWidth(1.0);

  // Add a points plot without errors, but set PlotErrorBars = true (multiply)
  // (no error bars should be plotted)
   vtkPlotPointsErrorBars* multPlot = vtkPlotPointsErrorBars::New();
   vtkChartWidget->chartXY()->AddPlot(multPlot);
   multPlot->SetInputData(table, 0, 5);
   multPlot->SetPlotErrorBars(1);
   multPlot->SetColor(255, 0, 255, 255);
   multPlot->SetWidth(2.0);
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
}

vtkChartMainWindow::~vtkChartMainWindow() {}

/******************************************************************************/
/******************************** MAIN PROGRAM ********************************/
/******************************************************************************/

int main(int argc, char** argv)
{
  QApplication app(argc, argv);
  vtkChartMainWindow mainWindow;
  mainWindow.show();
  mainWindow.raise();

  return app.exec();
}