12 #include <vtkChartXY.h> 13 #include <vtkContextScene.h> 14 #include <vtkContextView.h> 15 #include <vtkDataArray.h> 16 #include <vtkDelimitedTextWriter.h> 17 #include <vtkDoubleArray.h> 18 #include <vtkInformation.h> 19 #include <vtkMultiBlockDataSet.h> 20 #include <vtkPNGWriter.h> 22 #include <vtkPlotBar.h> 23 #include <vtkPlotLine.h> 24 #include <vtkPlotPoints.h> 25 #include <vtkRenderWindow.h> 26 #include <vtkRenderer.h> 28 #include <vtkWindowToImageFilter.h> 32 const std::string& yAxisText,
37 const std::string& outputImageFilename)
39 vtkSmartPointer<vtkContextView> view = vtkSmartPointer<vtkContextView>::New();
40 view->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
41 vtkSmartPointer<vtkChartXY> chart = vtkSmartPointer<vtkChartXY>::New();
42 view->GetScene()->AddItem(chart);
44 vtkPlotPoints* scatterPlot = vtkPlotPoints::SafeDownCast(chart->AddPlot(vtkChart::POINTS));
45 scatterPlot->SetInputData(&inputTable, xColumnIndex, yColumnIndex);
46 scatterPlot->SetColor(0, 0, 1);
47 scatterPlot->SetMarkerStyle(vtkPlotPoints::CROSS);
49 chart->SetShowLegend(
false);
59 const std::string& yAxisText,
65 const std::string& outputImageFilename)
67 vtkSmartPointer<vtkContextView> view = vtkSmartPointer<vtkContextView>::New();
68 view->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
69 vtkSmartPointer<vtkChartXY> chart = vtkSmartPointer<vtkChartXY>::New();
70 view->GetScene()->AddItem(chart);
72 if (y1ColumnIndex >= 0)
74 vtkPlotLine* linePlot = vtkPlotLine::SafeDownCast(chart->AddPlot(vtkChart::LINE));
75 linePlot->SetInputData(&inputTable, xColumnIndex, y1ColumnIndex);
79 if (y2ColumnIndex >= 0)
81 vtkPlotLine* linePlot = vtkPlotLine::SafeDownCast(chart->AddPlot(vtkChart::LINE));
82 linePlot->SetInputData(&inputTable, xColumnIndex, y2ColumnIndex);
86 chart->SetShowLegend(
true);
102 const std::string& outputImageFilename)
104 vtkSmartPointer<vtkContextView> view = vtkSmartPointer<vtkContextView>::New();
105 view->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
106 vtkSmartPointer<vtkChartXY> chart = vtkSmartPointer<vtkChartXY>::New();
107 view->GetScene()->AddItem(chart);
109 vtkSmartPointer<vtkTable> histogramTable = vtkSmartPointer<vtkTable>::New();
115 vtkPlotBar* barPlot = vtkPlotBar::SafeDownCast(chart->AddPlot(vtkChart::BAR));
116 barPlot->SetInputData(histogramTable, 0 , 1 );
117 barPlot->SetColor(0, 0, 1);
119 chart->SetShowLegend(
false);
121 chart->GetAxis(
vtkAxis::LEFT)->SetTitle(
"Number of samples");
130 const std::string& outputImageFilename)
132 vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
133 renderWindow->AddRenderer(view.GetRenderer());
134 renderWindow->SetSize(imageSize);
135 renderWindow->OffScreenRenderingOn();
137 vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter = vtkSmartPointer<vtkWindowToImageFilter>::New();
138 windowToImageFilter->SetInput(renderWindow);
139 windowToImageFilter->Update();
141 vtkSmartPointer<vtkPNGWriter> writer = vtkSmartPointer<vtkPNGWriter>::New();
142 writer->SetFileName(outputImageFilename.c_str());
143 writer->SetInputData(windowToImageFilter->GetOutput());
151 int inputColumnIndex,
152 vtkTable& resultTable,
153 double valueRangeMin,
154 double valueRangeMax,
155 unsigned int numberOfBins)
157 vtkDataArray* inputArray = vtkDataArray::SafeDownCast(inputTable.GetColumn(inputColumnIndex));
158 if (inputArray == NULL)
160 LOG_ERROR(
"PlusMath::ComputeHistogram failed: cannot find a valid data in column " << inputColumnIndex);
163 std::vector<int> histogramBins(numberOfBins, 0);
164 int numberOfValues = inputArray->GetNumberOfTuples();
165 const double binSize = (valueRangeMax - valueRangeMin) / numberOfBins;
166 for (
int i = 0;
i < numberOfValues;
i++)
168 int histogramBinIndex = static_cast<int>(floor((inputArray->GetTuple(
i)[0] - valueRangeMin) / binSize));
169 if (histogramBinIndex < 0)
171 histogramBinIndex = 0;
174 unsigned int histogramBinIndexUint = static_cast<unsigned int>(histogramBinIndex);
175 if (histogramBinIndexUint >= numberOfBins)
177 histogramBinIndexUint = numberOfBins - 1;
179 ++histogramBins[histogramBinIndexUint];
183 while (resultTable.GetNumberOfColumns() > 0)
185 resultTable.RemoveColumn(0);
189 vtkSmartPointer<vtkDoubleArray> arrX = vtkSmartPointer<vtkDoubleArray>::New();
190 resultTable.AddColumn(arrX);
191 arrX->SetName(
"Histogram bin center value");
192 vtkSmartPointer<vtkDoubleArray> arrY = vtkSmartPointer<vtkDoubleArray>::New();
193 resultTable.AddColumn(arrY);
194 arrY->SetName(
"Number of items");
195 resultTable.SetNumberOfRows(numberOfBins);
196 for (
unsigned int i = 0;
i < numberOfBins; ++
i)
198 resultTable.SetValue(
i, 0, valueRangeMin + (
i + 0.5)*binSize);
199 resultTable.SetValue(
i, 1, histogramBins[
i]);
208 LOG_TRACE(
"PlusPlotter::WriteTableToFile");
210 if (filename.empty())
212 LOG_ERROR(
"Failed to dump table to file - Input filename is empty!");
216 vtkSmartPointer<vtkDelimitedTextWriter> textWriter = vtkSmartPointer<vtkDelimitedTextWriter>::New();
217 textWriter->SetFieldDelimiter(
"\t");
218 textWriter->SetUseStringDelimiter(
false);
219 textWriter->SetFileName(filename.c_str());
220 textWriter->SetInputData(&table);
221 textWriter->Update();
static PlusStatus WriteTableToFile(vtkTable &table, const std::string &filename)
static PlusStatus ComputeHistogram(vtkTable &inputTable, int inputColumnIndex, vtkTable &resultTable, double valueRangeMin, double valueRangeMax, unsigned int numberOfBins)
static PlusStatus WriteLineChartToFile(const std::string &chartTitle, const std::string &yAxisText, vtkTable &inputTable, int xColumnIndex, int y1ColumnIndex, int y2ColumnIndex, int imageSize[2], const std::string &outputImageFilename)
static PlusStatus WriteChartToFile(vtkContextView &view, int imageSize[2], const std::string &outputImageFilename)
static PlusStatus WriteHistogramChartToFile(const std::string &chartTitle, vtkTable &inputTable, int inputColumnIndex, double valueRangeMin, double valueRangeMax, int numberOfBins, int imageSize[2], const std::string &outputImageFilename)
static PlusStatus WriteScatterChartToFile(const std::string &chartTitle, const std::string &yAxisText, vtkTable &inputTable, int xColumnIndex, int yColumnIndex, int imageSize[2], const std::string &outputImageFilename)