ArrayCalculator
VTKEx/Cxx/Utilities/ArrayCalculator
Question
If you have a simple question about this example contact us at VTKExProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum
Code¶
ArrayCalculator.cxx
#include <vtkArrayCalculator.h>
#include <vtkDoubleArray.h>
#include <vtkPointData.h>
#include <vtkPolyData.h>
#include <vtkSmartPointer.h>
int main(int, char *[])
{
vtkSmartPointer<vtkPoints> points =
vtkSmartPointer<vtkPoints>::New();
points->InsertNextPoint(1.0, 0.0, 0.0);
points->InsertNextPoint(2.0, 0.0, 0.0);
points->InsertNextPoint(3.0, 0.0, 0.0);
vtkSmartPointer<vtkDoubleArray> array =
vtkSmartPointer<vtkDoubleArray>::New();
array->SetName("orig");
array->InsertNextValue(1.0);
array->InsertNextValue(2.0);
array->InsertNextValue(3.0);
vtkSmartPointer<vtkPolyData> polydata =
vtkSmartPointer<vtkPolyData>::New();
polydata->SetPoints(points);
polydata->GetPointData()->AddArray(array);
vtkSmartPointer<vtkArrayCalculator> calc1 =
vtkSmartPointer<vtkArrayCalculator>::New();
calc1->SetInputData(polydata);
calc1->AddScalarArrayName("orig");
calc1->SetFunction("orig+1");
calc1->SetResultArrayName("orig");
calc1->Update();
vtkSmartPointer<vtkDoubleArray> output1 =
dynamic_cast<vtkDoubleArray*>(
calc1->GetPolyDataOutput()->GetPointData()->GetArray("orig"));
for(vtkIdType i = 0; i < output1->GetNumberOfTuples(); i++)
{
double val = output1->GetValue(i);
cout << "output1 value " << i << ": " << val << endl;
}
vtkSmartPointer<vtkArrayCalculator> calc2 =
vtkSmartPointer<vtkArrayCalculator>::New();
calc2->SetInputData(polydata);
calc2->AddScalarArrayName("orig");
calc2->SetFunction("if(orig=2,1,orig)");
calc2->SetResultArrayName("new");
calc2->Update();
vtkSmartPointer<vtkDoubleArray> output2 =
dynamic_cast<vtkDoubleArray*>(
calc2->GetPolyDataOutput()->GetPointData()->GetArray("new"));
for(vtkIdType i = 0; i < output2->GetNumberOfTuples(); i++)
{
double val = output2->GetValue(i);
cout << "output2 value " << i << ": " << val << endl;
}
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR)
project(ArrayCalculator)
find_package(VTK COMPONENTS
vtkvtkCommonCore
vtkvtkCommonDataModel
vtkvtkFiltersCore QUIET)
if (NOT VTK_FOUND)
message("Skipping ArrayCalculator: ${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(ArrayCalculator MACOSX_BUNDLE ArrayCalculator.cxx )
target_link_libraries(ArrayCalculator PRIVATE ${VTK_LIBRARIES})
else ()
# include all components
add_executable(ArrayCalculator MACOSX_BUNDLE ArrayCalculator.cxx )
target_link_libraries(ArrayCalculator PRIVATE ${VTK_LIBRARIES})
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS ArrayCalculator
MODULES ${VTK_LIBRARIES}
)
endif ()
Download and Build ArrayCalculator¶
Click here to download ArrayCalculator and its CMakeLists.txt file. Once the tarball ArrayCalculator.tar has been downloaded and extracted,
cd ArrayCalculator/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:
./ArrayCalculator
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.