Skip to content


# SurfacePlot

VTKEx/Cxx/Plotting/SurfacePlot

### Description

This examples creates a vtkChartXYZ that contains a vtkPlotSurface.

Other languages

See (Python)

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

SurfacePlot.cxx

    #include <vtkNew.h>
    #include <vtkPlotSurface.h>

    #include <vtkNamedColors.h>
    #include <vtkChartXYZ.h>
    #include <vtkPen.h>
    #include <vtkContextMouseEvent.h>
    #include <vtkContextView.h>
    #include <vtkContextScene.h>
    #include <vtkFloatArray.h>
    #include <vtkRenderer.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkTable.h>
    #include <vtkRegressionTestImage.h>
    #include <vtkUnsignedCharArray.h>
    #include <vtkVector.h>

    int main(int , char * [])
    {
      vtkNew<vtkNamedColors> colors;

      vtkNew<vtkChartXYZ> chart;
      chart->SetGeometry(vtkRectf(10.0, 10.0, 630, 470));

      vtkNew<vtkPlotSurface> plot;

      vtkNew<vtkContextView> view;
      view->GetRenderer()->SetBackground(colors->GetColor3d("Silver").GetData());
      view->GetRenderWindow()->SetSize(640, 480);
      view->GetScene()->AddItem(chart);

      // Create a surface
      vtkNew<vtkTable> table;
      vtkIdType numPoints = 70;
      float inc = 9.424778 / (numPoints - 1);
      for (vtkIdType i = 0; i < numPoints; ++i)
      {
        vtkNew<vtkFloatArray> arr;
        table->AddColumn(arr);
      }

      table->SetNumberOfRows(static_cast<vtkIdType>(numPoints));
      for (vtkIdType i = 0; i < numPoints; ++i)
      {
        float x = i * inc;
        for (vtkIdType j = 0; j < numPoints; ++j)
        {
          float y  = j * inc;
          table->SetValue(i, j, sin(sqrt(x*x + y*y)));
        }
      }

      // Set up the surface plot we wish to visualize and add it to the chart.
      plot->SetXRange(0, 10.0);
      plot->SetYRange(0, 10.0);
      plot->SetInputData(table);
      plot->GetPen()->SetColorF(colors->GetColor3d("Tomato").GetData());
      chart->AddPlot(plot);

      view->GetRenderWindow()->SetMultiSamples(0);
      view->GetInteractor()->Initialize();
      view->GetRenderWindow()->Render();

      // rotate
      vtkContextMouseEvent mouseEvent;
      mouseEvent.SetInteractor(view->GetInteractor());

      vtkVector2i pos;

      vtkVector2i lastPos;
      mouseEvent.SetButton(vtkContextMouseEvent::LEFT_BUTTON);
      lastPos.Set(100, 50);
      mouseEvent.SetLastScreenPos(lastPos);
      pos.Set(150, 100);
      mouseEvent.SetScreenPos(pos);

      vtkVector2d sP(pos.Cast<double>().GetData());
      vtkVector2d lSP(lastPos.Cast<double>().GetData());
      vtkVector2d screenPos(mouseEvent.GetScreenPos().Cast<double>().GetData());
      vtkVector2d lastScreenPos(mouseEvent.GetLastScreenPos().Cast<double>().GetData());

      chart->MouseMoveEvent(mouseEvent);

      view->GetInteractor()->Start();

      return EXIT_SUCCESS;
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(SurfacePlot)

    find_package(VTK COMPONENTS
      vtkChartsCore
      vtkCommonColor
      vtkCommonCore
      vtkCommonDataModel
      vtkInteractionStyle
      vtkRenderingContext2D
      vtkRenderingContextOpenGL2
      vtkRenderingCore
      vtkRenderingFreeType
      vtkRenderingGL2PSOpenGL2
      vtkRenderingOpenGL2
      vtkTestingRendering
      vtkViewsContext2D QUIET)
    if (NOT VTK_FOUND)
      message("Skipping SurfacePlot: ${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(SurfacePlot MACOSX_BUNDLE SurfacePlot.cxx )
      target_link_libraries(SurfacePlot PRIVATE ${VTK_LIBRARIES})
    else ()
      # include all components
      add_executable(SurfacePlot MACOSX_BUNDLE SurfacePlot.cxx )
      target_link_libraries(SurfacePlot PRIVATE ${VTK_LIBRARIES})
      # vtk_module_autoinit is needed
      vtk_module_autoinit(
        TARGETS SurfacePlot
        MODULES ${VTK_LIBRARIES}
        )
    endif ()

### Download and Build SurfacePlot

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

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

    ./SurfacePlot

WINDOWS USERS

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