Skip to content

Scalars

vtk-examples/Cxx/VTKConcepts/Scalars

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

Scalars.cxx

#include <vtkDoubleArray.h>
#include <vtkFloatArray.h>
#include <vtkNew.h>
#include <vtkPointData.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>

void TypeSpecific();
void NonTypeSpecific();

int main(int, char*[])
{
  TypeSpecific();
  NonTypeSpecific();

  return EXIT_SUCCESS;
}

void TypeSpecific()
{
  // Create a point set
  vtkNew<vtkPoints> points;
  points->InsertNextPoint(0, 0, 0);
  points->InsertNextPoint(1, 0, 0);

  vtkNew<vtkPolyData> polydata;
  polydata->SetPoints(points);

  vtkNew<vtkDoubleArray> weights;
  weights->SetNumberOfValues(2);
  weights->SetValue(0, 1);
  weights->SetValue(1, 2);

  polydata->GetPointData()->SetScalars(weights);

  double weight =
      dynamic_cast<vtkDoubleArray*>(polydata->GetPointData()->GetScalars())
          ->GetValue(0);
  std::cout << "double weight: " << weight << std::endl;

  /*
  // This causes a crash because the array is not a vtkFloatArray
  double weightf =
  dynamic_cast<vtkFloatArray*>(polydata->GetPointData()->GetScalars())->GetValue(0);
  std::cout << "float weight: " << weightf << std::endl;
  */
}

void NonTypeSpecific()
{
  // Create a point set
  vtkNew<vtkPoints> points;
  points->InsertNextPoint(0, 0, 0);
  points->InsertNextPoint(1, 0, 0);

  vtkNew<vtkPolyData> polydata;
  polydata->SetPoints(points);

  vtkNew<vtkDoubleArray> weights;
  weights->SetNumberOfValues(2);
  weights->SetValue(0, 1);
  weights->SetValue(1, 2);

  polydata->GetPointData()->SetScalars(weights);

  double weight = polydata->GetPointData()->GetScalars()->GetComponent(0, 0);
  std::cout << "double weight: " << weight << std::endl;

  double weightf = polydata->GetPointData()->GetScalars()->GetComponent(0, 0);
  std::cout << "float weight: " << weightf << std::endl;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(Scalars)

find_package(VTK COMPONENTS 
  vtkCommonCore
  vtkCommonDataModel
  QUIET
)

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

Download and Build Scalars

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

cd Scalars/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:

./Scalars

WINDOWS USERS

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