ConstructTable
vtk-examples/Cxx/Utilities/ConstructTable
Description¶
This example creates a 3x3 table. The columns MUST be added first - if you attempt to call AddRow type functions, they will not do anything (or produce errors) because the table still has 0 columns until you tell it otherwise. This example adds 3 empty columns to the table, then goes back and fills them in.
Warning: the table does not copy the data, so if you add the SAME column to the table each time, i.e.
``` vtkSmartPointer vtkVariantArray> col = vtkSmartPointer vtkVariantArray>::New();
col->InsertNextValue vtkVariant(0.0)); col->InsertNextValue vtkVariant(0.0)); col->InsertNextValue vtkVariant(0.0)); table->AddColumn(Col); table->AddColumn(Col); table->AddColumn(Col);
If you modify one of the entries in the table with `Table->SetValue (r, c, [vtkVariant](https://www.vtk.org/doc/nightly/html/classvtkVariant.html#details)(counter));` after they have all been set to the same column, the rth entry in EVERY column will be changed.
!!! question
If you have a question about this example, please use the [VTK Discourse Forum](https://discourse.vtk.org/)
###Code
**ConstructTable.cxx**
``` c++ hl_lines="1 2 3 4 9 13 15 16 17 23 25 27 40 42 44"
#include <vtkNew.h>
#include <vtkTable.h>
#include <vtkVariant.h>
#include <vtkVariantArray.h>
int main(int, char*[])
{
// construct an empty table
vtkNew<vtkTable> table;
for (unsigned int i = 0; i < 3; i++)
{
vtkNew<vtkVariantArray> col;
col->InsertNextValue(vtkVariant(0.0));
col->InsertNextValue(vtkVariant(0.0));
col->InsertNextValue(vtkVariant(0.0));
table->AddColumn(col);
}
// fill the table with values
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++;
}
}
// print information about the table
std::cout << "NumRows: " << table->GetNumberOfRows() << std::endl;
std::cout << "NumCols: " << table->GetNumberOfColumns() << std::endl;
// display the table
table->Dump(3);
// access elements of the table
for (vtkIdType r = 0; r < table->GetNumberOfRows(); r++)
{
for (vtkIdType c = 0; c < table->GetNumberOfColumns(); c++)
{
vtkVariant v = table->GetValue(r, c);
std::cout << "(r,c) = (" << r << "," << c << ") = " << v << std::endl;
}
}
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(ConstructTable)
find_package(VTK COMPONENTS
vtkCommonCore
vtkCommonDataModel
QUIET
)
if (NOT VTK_FOUND)
message("Skipping ConstructTable: ${VTK_NOT_FOUND_MESSAGE}")
return ()
endif()
message (STATUS "VTK_VERSION: ${VTK_VERSION}")
if (VTK_VERSION VERSION_LESS "8.90.0")
# old system
include(${VTK_USE_FILE})
add_executable(ConstructTable MACOSX_BUNDLE ConstructTable.cxx )
target_link_libraries(ConstructTable PRIVATE ${VTK_LIBRARIES})
else ()
# include all components
add_executable(ConstructTable MACOSX_BUNDLE ConstructTable.cxx )
target_link_libraries(ConstructTable PRIVATE ${VTK_LIBRARIES})
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS ConstructTable
MODULES ${VTK_LIBRARIES}
)
endif ()
Download and Build ConstructTable¶
Click here to download ConstructTable and its CMakeLists.txt file. Once the tarball ConstructTable.tar has been downloaded and extracted,
cd ConstructTable/build
If VTK is installed:
cmake ..
If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:
cmake -DVTK_DIR:PATH=/home/me/vtk_build ..
Build the project:
make
and run it:
./ConstructTable
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.