PlotLine3D

VTKExamples/Cxx/Plotting/PlotLine3D


Description

This example illustrates plotting 3D points using vtkPlotLine3D. It plots the solution to the Lorenz Attractor.

Warning

The current vtkPlotLine3D produces the warning, vtkOpenGLContextDevice3D (): a line width has been requested that is larger than your system supports" which appears to have no effect on the results.

Code

PlotLine3D.cxx

#include <vtkNew.h>

#include <vtkChartXYZ.h>
#include <vtkPen.h>
#include <vtkPlotLine3D.h>

#include <vtkNamedColors.h>
#include <vtkRenderWindow.h>
#include <vtkTable.h>
#include <vtkFloatArray.h>
#include <vtkContextView.h>
#include <vtkContextScene.h>
#include <vtkContext3D.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkCamera.h>

// Plot the solution to the Lorenz attractor.
// http://en.wikipedia.org/wiki/Lorenz_system
namespace
{
void lorenz(const float * varX, float * varXDerivative)
{
  const float sigma = 10.f;
  const float rho = 28.f;
  const float beta = 2.66666666666f;

  varXDerivative[0] = sigma * (varX[1] - varX[0]);
  varXDerivative[1] = varX[0] * (rho - varX[2]) - varX[1];
  varXDerivative[2] = varX[0] * varX[1] - beta * varX[2];
}
} // end anonymous namespace

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

  // Create the data.
  vtkNew<vtkTable> varXSolution;

  vtkNew<vtkFloatArray> arrX0;
  arrX0->SetName("X");
  varXSolution->AddColumn(arrX0);

  vtkNew<vtkFloatArray> arrX1;
  arrX1->SetName("Y");
  varXSolution->AddColumn(arrX1);

  vtkNew<vtkFloatArray> arrX2;
  arrX2->SetName("Z");
  varXSolution->AddColumn(arrX2);

  const unsigned int numberOfTimePoints = 1000;
  varXSolution->SetNumberOfRows(numberOfTimePoints);
  float varX[3];
  varX[0] = 0.0f;
  varX[1] = 1.0f;
  varX[2] = 1.05f;

  float varXDerivative[3];
  const float deltaT = 0.01f;
  for (unsigned int ii = 0; ii < numberOfTimePoints; ++ii)
  {
    varXSolution->SetValue(ii, 0, varX[0]);
    varXSolution->SetValue(ii, 1, varX[1]);
    varXSolution->SetValue(ii, 2, varX[2]);
    lorenz(varX, varXDerivative);
    varX[0] += varXDerivative[0] * deltaT;
    varX[1] += varXDerivative[1] * deltaT;
    varX[2] += varXDerivative[2] * deltaT;
  }

  // Set up a 3D scene and add an XYZ chart to it.
  vtkNew<vtkContextView> view;
  view->GetRenderWindow()->SetSize(640, 480);

  vtkNew<vtkChartXYZ> chart;
  chart->SetGeometry(vtkRectf(5.0, 5.0, 635.0, 475.0));
  view->GetScene()->AddItem(chart);

  // Add a line plot.
  vtkNew<vtkPlotLine3D> plot;
  plot->SetInputData(varXSolution);
  plot->GetPen()->SetColorF(colors->GetColor3d("Tomato").GetData());
  view->GetRenderWindow()->SetMultiSamples(0);
  plot->GetPen()->SetWidth(6.0);
  chart->AddPlot(plot);

  // Finally render the scene.
  view->GetRenderer()->SetBackground(colors->GetColor3d("Mint").GetData());
  view->GetInteractor()->Initialize();
  view->GetInteractor()->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(PlotLine3D)

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

Download and Build PlotLine3D

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

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

./PlotLine3D

WINDOWS USERS

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