Getting Started Coding/VTK/Table

From FarsightWiki
Jump to: navigation, search

Return to:

VTK examples

Getting started

In this example, we're starting off with the most fundamental concepts of the Visualization Toolkit i.e. vtkTable


Standard vtkTable

 
 #include <vtkTable.h>
 #include <vtkVariant.h>
 #include <vtkVariantArray.h>
 #include <vtkSmartPointer.h>
 
 int main(int, char *[])
 {
 
  vtkSmartPointer<vtkTable> table = vtkSmartPointer<vtkTable>::New();                              /* Initializes vtk Smart Pointer (i.e table) to vtkTable */
 
  for (unsigned int i = 0; i<3; i++)                                                               /* Constructs the no.of columns of the initialized vtkTable (Unsigned as table can't have negative indexes)  */
    {
         vtkSmartPointer<vtkVariantArray> col = vtkSmartPointer<vtkVariantArray>::New();           /* Initializes vtk Smart Pointer (i.e col) to vtkVariantArray which is an array of vtk Variants  */     
 
         col->InsertNextValue ( vtkVariant ( 0.0 ) );                                              /* Expand the vtkVariantArray by one, where InsertNextValue inserts value (Type doesn't matter) to the next column */
         col->InsertNextValue ( vtkVariant ( 0.0 ) );                   
         col->InsertNextValue ( vtkVariant ( 0.0 ) );                
         table->AddColumn ( col );                                                                 /* Adds the vtkVariantArray (having 3 rows of value 0.0) to the vtkTable  */
    }                                                                  
 
                                                                                                   /*
                                                                                                       X  X  X   when i = 1 (loop end), table has 3 rows, 1 column   (AddColumn adds the array col as a new column)   
                                                                                                       X  X  X   when i = 2 (loop end), table has 3 rows, 2 columns
                                                                                                       X  X  X   when i = 3 (loop end), table has 3 rows, 3 columns  (where X = 0.0, can also be char!)
 
                                                                                                   */
 
  unsigned int counter = 0;                                                                        /* Initializes the starting point of values for the vtkTable */
  for(vtkIdType r = 0; r < table->GetNumberOfRows(); r++ )                                         /* table now */
    {                                                                                              /* Inputs the values into the vtkTable */
         for(vtkIdType c = 0; c < table->GetNumberOfColumns(); c++ )
          {
                 table->SetValue ( r,c, vtkVariant ( counter ) );
                 counter++;
          }
    }
 
  std::cout << "Number of Rows: " << table->GetNumberOfRows() << std::endl;                        /* Prints the no.of rows to the Console Window */
  std::cout << "Number of Columns: " << table->GetNumberOfColumns() << std::endl;                  /* Prints the no.of columns to the Console Window */
 
  table->Dump ( 3 );                                                                               /* Prints the entire vtkTable to the Console Window */
 
  return EXIT_SUCCESS;
 }

Table1.jpg

Exporting vtkTable to file

 
#include <iostream>
#include <fstream>
 
#include <vtkTable.h>
#include <vtkVariant.h>
#include <vtkVariantArray.h>
#include <vtkSmartPointer.h>
 
 
int main(int, char *[])
{
 
  vtkSmartPointer<vtkTable> table = vtkSmartPointer<vtkTable>::New();
 
  ofstream Dumped_Table;                                                                      // Creates an output stream
  Dumped_Table.open ("Dumped_Table.xls");                                                     // Creates the file 'Dumped_Table.xls' in the '.sln' directory
 
  unsigned int noh;
 
  std::cout << "Enter the no.of headers : " ;                                                 
  std::cin >> noh ;                                                                           // User inputs the no.of header tags
  std::cout << "Enter the names of the headers " << "\n" << "\n";
 
  std::string headers;  
 
  for ( unsigned int i = 0; i < noh; i++ )
    {
        vtkSmartPointer<vtkVariantArray> col = vtkSmartPointer<vtkVariantArray>::New();
 
        col->InsertNextValue ( vtkVariant ( 0.0 ) );
        col->InsertNextValue ( vtkVariant ( 0.0 ) );
        col->InsertNextValue ( vtkVariant ( 0.0 ) );
 
        std::cin >> headers;
	col->SetName(headers.c_str());                                                        // User inputs the names of the header tags
	Dumped_Table << headers << "\t";                                                      // Dumps the values of the header tags to file
 
        table->AddColumn ( col );
    }
 
  Dumped_Table << endl << "\n";
 
  unsigned int counter = 0;
  for(vtkIdType r = 0; r < table->GetNumberOfRows(); r++ )
   {
 
    for(vtkIdType c = 0; c < table->GetNumberOfColumns(); c++ )
      {
      table->SetValue ( r,c, vtkVariant ( counter ) );
      counter++;
      }
   }
 
  table->Dump ( 3 );
 
  for(vtkIdType r = 0; r < table->GetNumberOfRows(); r++ )
  {
       for(vtkIdType c = 0; c < table->GetNumberOfColumns(); c++ )
         {
             vtkVariant v = table->GetValue( r,c);
	     Dumped_Table << v << "\t";                                                          // Dumps the values of the vtkTable to file
         }
 
       Dumped_Table << endl;
  }
 
  return EXIT_SUCCESS;
}

Table2.jpg Table2 IO.jpg


                                                                                                                                                                                                                                                                        Author : Ripan Sarkar