# Scalars

VTKExamples/Cxx/VTKConcepts/Scalars

### Code

Scalars.cxx

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

    void TypeSpecific();
    void NonTypeSpecific();

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

      return EXIT_SUCCESS;
    }

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

      vtkSmartPointer<vtkPolyData> polydata =
        vtkSmartPointer<vtkPolyData>::New();
      polydata->SetPoints(points);

      vtkSmartPointer<vtkDoubleArray> weights =
        vtkSmartPointer<vtkDoubleArray>::New();
      weights->SetNumberOfValues(2);
      weights->SetValue(0, 1);
      weights->SetValue(1, 2);

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

      double weight = vtkDoubleArray::SafeDownCast(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 = vtkFloatArray::SafeDownCast(polydata->GetPointData()->GetScalars())->GetValue(0);
      std::cout << "float weight: " << weightf << std::endl;
      */
    }

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

      vtkSmartPointer<vtkPolyData> polydata =
        vtkSmartPointer<vtkPolyData>::New();
      polydata->SetPoints(points);

      vtkSmartPointer<vtkDoubleArray> weights =
        vtkSmartPointer<vtkDoubleArray>::New();
      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.