0

Basically, I have written the following code to try to take a testdata file with comma seperated values in order to put the individual values into a multidimensional array and then later put them into VTK's table (Visualisation Toolkit). up until where i have put the mass of slashes, if i take that code (without the rest underneath and the vtk includes at the top). the initial 'while(getline(inputData, dataStore))' works fine, but when i put it in with the VTK code, the while loop doesn't execute, any ideas why? Thanks.

The testfile ('testdata2') looks like this:

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

The code looks like this:

#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;
}
Daniel Takyi
  • 159
  • 1
  • 9

1 Answers1

0

try to replace

getline(iss, eachrow, '/')

with

std::getline(iss, eachrow, '/')
user1767754
  • 23,311
  • 18
  • 141
  • 164