Skip to content

VisualizeStructuredGridCells

Repository source: VisualizeStructuredGridCells


Question

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

Code

VisualizeStructuredGridCells.cxx

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkDataSetMapper.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkShrinkFilter.h>
#include <vtkStructuredGrid.h>
#include <vtkUnstructuredGrid.h>

#include <iostream>
#include <string>

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

  // Create a grid
  vtkNew<vtkStructuredGrid> structuredGrid;

  vtkNew<vtkPoints> points;
  unsigned int numi = 3;
  unsigned int numj = 4;
  unsigned int numk = 5;

  for (unsigned int k = 0; k < numk; k++)
  {
    for (unsigned int j = 0; j < numj; j++)
    {
      for (unsigned int i = 0; i < numi; i++)
      {
        points->InsertNextPoint(i, j, k);
      }
    }
  }

  // Specify the dimensions of the grid
  structuredGrid->SetDimensions(numi, numj, numk);
  structuredGrid->SetPoints(points);

  std::cout << "There are " << structuredGrid->GetNumberOfPoints()
            << " points before shrinking." << std::endl;
  std::cout << "There are " << structuredGrid->GetNumberOfCells()
            << " cells before shrinking." << std::endl;

  vtkNew<vtkShrinkFilter> shrinkFilter;
  shrinkFilter->SetInputData(structuredGrid);
  shrinkFilter->SetShrinkFactor(.8);
  shrinkFilter->Update();

  std::cout << "There are " << shrinkFilter->GetOutput()->GetNumberOfPoints()
            << " points after shrinking." << std::endl;
  std::cout << "There are " << shrinkFilter->GetOutput()->GetNumberOfCells()
            << " cells after shrinking." << std::endl;

  // Note: there are more points after shrinking because cells no longer share
  // points.

  // Create a mapper and actor
  vtkNew<vtkDataSetMapper> mapper;
  mapper->SetInputConnection(shrinkFilter->GetOutputPort());
  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);
  actor->GetProperty()->SetColor(colors->GetColor3d("MistyRose").GetData());

  // Create a renderer, render window, and interactor
  vtkNew<vtkRenderer> renderer;
  vtkNew<vtkRenderWindow> renderWindow;
  renderWindow->AddRenderer(renderer);
  renderWindow->SetWindowName("VisualizeStructuredGridCells");

  vtkNew<vtkRenderWindowInteractor> renderWindowInteractor;
  renderWindowInteractor->SetRenderWindow(renderWindow);

  // Add the actor to the scene
  renderer->AddActor(actor);
  renderer->SetBackground(colors->GetColor3d("SteelBlue").GetData());

  // Render and interact
  renderWindow->Render();

  auto camera = renderer->GetActiveCamera();
  camera->SetPosition(9.56681, 4.14093, 5.85709);
  camera->SetFocalPoint(1, 1.5, 2);
  camera->SetViewUp(-0.172781, 0.94846, -0.26565);
  camera->SetDistance(9.7592);
  camera->SetClippingRange(5.84116, 14.7147);

  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(VisualizeStructuredGridCells)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  CommonDataModel
  FiltersGeneral
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "VisualizeStructuredGridCells: 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(VisualizeStructuredGridCells MACOSX_BUNDLE VisualizeStructuredGridCells.cxx )
  target_link_libraries(VisualizeStructuredGridCells PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS VisualizeStructuredGridCells
  MODULES ${VTK_LIBRARIES}
)

Download and Build VisualizeStructuredGridCells

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

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

./VisualizeStructuredGridCells

WINDOWS USERS

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