Skip to content

ExponentialCosine

vtk-examples/Cxx/VisualizationAlgorithms/ExponentialCosine


Description

Visualization of an exponential cosine function. Function values are indicated by surface displacement. Colors indicate derivative values.

Other languages

See (Python)

Question

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

Code

ExponentialCosine.cxx

//
// Brute force computation of Bessel functions. Might be better to create a
// filter (or source) object. Might also consider vtkSampleFunction.

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkDataSetMapper.h>
#include <vtkDoubleArray.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPlaneSource.h>
#include <vtkPointData.h>
#include <vtkPoints.h>
#include <vtkPolyData.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkTransform.h>
#include <vtkTransformPolyDataFilter.h>
#include <vtkWarpScalar.h>

int main(int, char*[])
{

  vtkNew<vtkNamedColors> colors;

  vtkNew<vtkRenderer> ren;
  vtkNew<vtkRenderWindow> renWin;
  renWin->AddRenderer(ren);

  vtkNew<vtkRenderWindowInteractor> iren;
  iren->SetRenderWindow(renWin);

  // Create plane to warp.
  vtkNew<vtkPlaneSource> plane;
  plane->SetResolution(300, 300);

  vtkNew<vtkTransform> transform;
  transform->Scale(10.0, 10.0, 1.0);

  vtkNew<vtkTransformPolyDataFilter> transF;
  transF->SetInputConnection(plane->GetOutputPort());
  transF->SetTransform(transform);
  transF->Update();

  // Compute Bessel function and derivatives. This portion could be
  // encapsulated into source or filter object.
  //
  auto input = transF->GetOutput();
  auto numPts = input->GetNumberOfPoints();

  vtkNew<vtkPoints> newPts;
  newPts->SetNumberOfPoints(numPts);

  vtkNew<vtkDoubleArray> derivs;
  derivs->SetNumberOfTuples(numPts);

  vtkNew<vtkPolyData> bessel;
  bessel->CopyStructure(input);
  bessel->SetPoints(newPts);
  bessel->GetPointData()->SetScalars(derivs);

  double x[3];

  for (auto i = 0; i < numPts; i++)
  {
    input->GetPoint(i, x);
    auto r = sqrt(static_cast<double>(x[0] * x[0]) + x[1] * x[1]);
    x[2] = exp(-r) * cos(10.0 * r);
    newPts->SetPoint(i, x);
    auto deriv = -exp(-r) * (cos(10.0 * r) + 10.0 * sin(10.0 * r));
    derivs->SetValue(i, deriv);
  }

  // Warp plane.
  vtkNew<vtkWarpScalar> warp;
  warp->SetInputData(bessel);
  warp->XYPlaneOn();
  warp->SetScaleFactor(0.5);

  // Mapper and actor.
  vtkNew<vtkDataSetMapper> mapper;
  mapper->SetInputConnection(warp->GetOutputPort());
  double tmp[2];
  bessel->GetScalarRange(tmp);
  mapper->SetScalarRange(tmp[0], tmp[1]);

  vtkNew<vtkActor> carpet;
  carpet->SetMapper(mapper);

  // Assign our actor to the renderer.
  ren->AddActor(carpet);
  ren->SetBackground(colors->GetColor3d("Beige").GetData());
  renWin->SetSize(640, 480);
  renWin->SetWindowName("ExponentialCosine");

  // Draw the resulting scene.
  ren->ResetCamera();
  ren->GetActiveCamera()->Zoom(1.35);
  ren->GetActiveCamera()->Elevation(-55);
  ren->GetActiveCamera()->Azimuth(25);
  ren->ResetCameraClippingRange();
  renWin->Render();

  iren->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(ExponentialCosine)

find_package(VTK COMPONENTS 
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "ExponentialCosine: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(ExponentialCosine MACOSX_BUNDLE ExponentialCosine.cxx )
  target_link_libraries(ExponentialCosine PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS ExponentialCosine
  MODULES ${VTK_LIBRARIES}
)

Download and Build ExponentialCosine

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

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

./ExponentialCosine

WINDOWS USERS

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