基本上,我编写了以下代码来尝试使用逗号分隔值来获取testdata文件,以便将各个值放入多维数组中,然后将它们放入VTK表(Visualization Toolkit)中。直到我把大量的斜线,如果我采取该代码(没有其余的下面,vtk包括在顶部)。最初的'while(getline(inputData,dataStore))'工作正常,但当我把它放入VTK代码时,while循环不执行,任何想法为什么?感谢。
testfile('testdata2')如下所示:
2,4,6,8,10,12,14
1,3,5,7,9,11,13
2.3,2.4,2.5,2.6,2.7,2.8,2.9
代码如下所示:
#include <vtkVersion.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderWindow.h>
#include <vtkSmartPointer.h>
#include <vtkChartXY.h>
#include <vtkTable.h>
#include <vtkPlot.h>
#include <vtkFloatArray.h>
#include <vtkContextView.h>
#include <vtkContextScene.h>
#include <vtkPen.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <fstream>
#include <string>
#include <stdio.h>
#include <string.h>
#include <sstream>
using namespace std;
int main(int, char *[])
{
int number_of_lines = 0;
string dataStore;
string collectedData = "";
char delim = ',';
int commaCount = 0;
string columnData;
char exit;
ifstream inputData;
inputData.open("testdata2");
//if(inputData.is_open()){
while(getline(inputData, dataStore)){
++number_of_lines;
commaCount = count(dataStore.begin(), dataStore.end(), ',');
collectedData += dataStore + "/" ;
}
cout << commaCount << endl;
cout << number_of_lines << endl;
float dataTable[(number_of_lines)][(commaCount+1)];
stringstream iss(collectedData);
string eachrow;
int levelOneCounter = 0;
int levelTwoCOunter = 0;
while(getline(iss, eachrow, '/')){
stringstream innerIss(eachrow);
string eachValue;
while(getline(innerIss, eachValue, ',')){
dataTable[levelOneCounter][levelTwoCOunter] = atof(eachValue.c_str());
levelTwoCOunter++;
}
levelTwoCOunter = 0;
levelOneCounter++;
}
//} ////////////////////////////////////////////////////////////////////////////
// Create a table with some points in it
vtkSmartPointer<vtkTable> table = //initializes a new instance of a VTKTable (using VTKSmartPointer to avoid memory leakage
vtkSmartPointer<vtkTable>::New();
vtkSmartPointer<vtkFloatArray> arrX =
vtkSmartPointer<vtkFloatArray>::New(); //initializes a new instance of a VTKFloatArray to store X values
arrX->SetName("First");
table->AddColumn(arrX); //X array is added to the table
vtkSmartPointer<vtkFloatArray> arrC =
vtkSmartPointer<vtkFloatArray>::New();
arrC->SetName("Second"); //Cosine array is added to the table
table->AddColumn(arrC);
vtkSmartPointer<vtkFloatArray> arrS =
vtkSmartPointer<vtkFloatArray>::New();
arrS->SetName("Third"); //Sine array is added to the table
table->AddColumn(arrS);
//while(getline(inputData, dataStore)){
//++number_of_lines;
//}
// Fill in the table with some example values (part i have to put values i nwith
// int numPoints = number_of_lines; // initialize no. of points
//table->SetNumberOfRows(commaCount+1); // Sets the number of points of the table
table->SetNumberOfRows(7);
for (int j = 0; j <= 6; ++j) // for loop to put in values into table
{
table->SetValue(j, 0, 1);
table->SetValue(j, 1, 2);
table->SetValue(j, 2, 3);
//for(int i = 0; i < number_of_lines; ++i){
//table->SetValue(j, i, dataTable[i][j]);
//}
}
// Set up the view
vtkSmartPointer<vtkContextView> view = //creates view for visualisation to be shown in
vtkSmartPointer<vtkContextView>::New();
view->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
// Add multiple line plots, setting the colors etc
vtkSmartPointer<vtkChartXY> chart = //creates new chartXY
vtkSmartPointer<vtkChartXY>::New();
view->GetScene()->AddItem(chart); //access view's scene via GetScene method and add the created chart above
vtkPlot *line = chart->AddPlot(vtkChart::LINE); // Add new vtkPlot to the created chart through addplot method
#if VTK_MAJOR_VERSION <= 5
line->SetInput(table, 0, 1); // puts table into 1st line plot (old versions)
#else
line->SetInputData(table, 0, 1); // puts table into 1st line plot (new versions)
#endif
line->SetColor(0, 255, 0, 255); // colour of 1st line plot
line->SetWidth(1.0); // width of 1st line plot
line = chart->AddPlot(vtkChart::LINE); // add a 2nd line plot
#if VTK_MAJOR_VERSION <= 5
line->SetInput(table, 0, 2);
#else
line->SetInputData(table, 0, 2);
#endif
line->SetColor(255, 0, 0, 255);
line->SetWidth(5.0);
line->GetPen()->SetLineType(2);//For dotted line, can be from 2 to 5 for different dot patterns
//view->GetRenderWindow()->SetMultiSamples(0);
// Start interactor
view->GetInteractor()->Initialize();
view->GetInteractor()->Start();
return EXIT_SUCCESS;
}
答案 0 :(得分:0)
尝试替换
getline(iss, eachrow, '/')
与
std::getline(iss, eachrow, '/')