3DArray

VTKExamples/Cxx/Utilities/3DArray

Code

3DArray.cxx

#include <vtkSmartPointer.h>
#include <vtkDenseArray.h>

int main(int, char *[])
{
  // Create an N-D array
  vtkSmartPointer<vtkDenseArray<double> > array = 
      vtkSmartPointer<vtkDenseArray<double> >::New();

  // Resize the array to 4x5x3
  array->Resize(4,5,3);

  // Set a value
  int i = 0; int j = 0; int k = 0;
  double value = 3.0;
  array->SetValue(i, j, k, value);

  // Get a value
  std::cout << array->GetValue(i,j,k) << std::endl;

  // Get size (bounds) of whole array
  cout << array->GetExtents() << endl;

  // Get size (bounds) of array dimensions
  std::cout << array->GetExtents()[0] << std::endl;
  std::cout << array->GetExtents()[1] << std::endl;
  std::cout << array->GetExtents()[2] << std::endl;

  // Get the bounds of the 0th dimension
  std::cout << array->GetExtents()[0].GetBegin() << std::endl;
  std::cout << array->GetExtents()[0].GetEnd() << std::endl;

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(3DArray)

find_package(VTK COMPONENTS 
  vtkCommonCore QUIET)
if (NOT VTK_FOUND)
  message("Skipping 3DArray: ${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(3DArray MACOSX_BUNDLE 3DArray.cxx )
  target_link_libraries(3DArray PRIVATE ${VTK_LIBRARIES})
else ()
  # include all components
  add_executable(3DArray MACOSX_BUNDLE 3DArray.cxx )
  target_link_libraries(3DArray PRIVATE ${VTK_LIBRARIES})
  # vtk_module_autoinit is needed
  vtk_module_autoinit(
    TARGETS 3DArray
    MODULES ${VTK_LIBRARIES}
    )
endif () 

Download and Build 3DArray

Click here to download 3DArray and its CMakeLists.txt file. Once the tarball 3DArray.tar has been downloaded and extracted,

cd 3DArray/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:

./3DArray

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.