Skip to content


# QuadraticHexahedron

VTKEx/Cxx/GeometricObjects/QuadraticHexahedron

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

QuadraticHexahedron.cxx

    #include <vtkActor.h>
    #include <vtkDataSetMapper.h>
    #include <vtkGlyph3D.h>
    #include <vtkMinimalStandardRandomSequence.h>
    #include <vtkNamedColors.h>
    #include <vtkPoints.h>
    #include <vtkProperty.h>
    #include <vtkQuadraticHexahedron.h>
    #include <vtkRenderer.h>
    #include <vtkRenderWindow.h>
    #include <vtkRenderWindowInteractor.h>
    #include <vtkSmartPointer.h>
    #include <vtkSphereSource.h>
    #include <vtkTessellatorFilter.h>
    #include <vtkUnstructuredGrid.h>

    namespace
    {
    vtkSmartPointer<vtkUnstructuredGrid> MakeQuadraticHexahedron();
    }

    int main (int, char *[])
    {
      vtkSmartPointer<vtkNamedColors> namedColors =
        vtkSmartPointer<vtkNamedColors>::New();

      vtkSmartPointer<vtkUnstructuredGrid> uGrid =
        MakeQuadraticHexahedron();

      vtkSmartPointer<vtkTessellatorFilter> tessellate =
        vtkSmartPointer<vtkTessellatorFilter>::New();
      tessellate->SetInputData(uGrid);
      tessellate->SetMaximumNumberOfSubdivisions(2);

      vtkSmartPointer<vtkDataSetMapper> mapper =
        vtkSmartPointer<vtkDataSetMapper>::New();
      mapper->SetInputConnection(tessellate->GetOutputPort());
      mapper->ScalarVisibilityOff();

      // Create an actor for the grid
      vtkSmartPointer<vtkActor> actor =
        vtkSmartPointer<vtkActor>::New();
      actor->SetMapper(mapper);
      actor->GetProperty()->SetDiffuseColor(
        namedColors->GetColor3d("Tomato").GetData());
      actor->GetProperty()->SetEdgeColor(
        namedColors->GetColor3d("IvoryBlack").GetData());
      actor->GetProperty()->EdgeVisibilityOn();

      vtkSmartPointer<vtkSphereSource> sphereSource =
        vtkSmartPointer<vtkSphereSource>::New();
      sphereSource->SetRadius(0.02);

      vtkSmartPointer<vtkGlyph3D> glyph3D =
        vtkSmartPointer<vtkGlyph3D>::New();
      glyph3D->SetInputData(uGrid);
      glyph3D->SetSourceConnection(sphereSource->GetOutputPort());
      glyph3D->ScalingOff();
      glyph3D->Update();

      vtkSmartPointer<vtkDataSetMapper> glyph3DMapper =
        vtkSmartPointer<vtkDataSetMapper>::New();
      glyph3DMapper->SetInputConnection(glyph3D->GetOutputPort());
      glyph3DMapper->ScalarVisibilityOff();

      vtkSmartPointer<vtkActor> glyph3DActor =
        vtkSmartPointer<vtkActor>::New();
      glyph3DActor->SetMapper(glyph3DMapper);
      glyph3DActor->GetProperty()->SetColor(
        namedColors->GetColor3d("Banana").GetData());

      // Visualize
      vtkSmartPointer<vtkRenderer> renderer =
        vtkSmartPointer<vtkRenderer>::New();
      vtkSmartPointer<vtkRenderWindow> renderWindow =
        vtkSmartPointer<vtkRenderWindow>::New();
      renderWindow->SetWindowName("Quadratic Hexahedron");
      renderWindow->AddRenderer(renderer);
      vtkSmartPointer<vtkRenderWindowInteractor> interactor =
        vtkSmartPointer<vtkRenderWindowInteractor>::New();
      interactor->SetRenderWindow(renderWindow);

      renderer->AddActor(actor);
      renderer->AddActor(glyph3DActor);
      renderer->SetBackground(namedColors->GetColor3d("SlateGray").GetData());

      renderWindow->Render();
      interactor->Start();

      return EXIT_SUCCESS;
    }

    namespace
    {
    vtkSmartPointer<vtkUnstructuredGrid> MakeQuadraticHexahedron()
    {
      vtkSmartPointer<vtkQuadraticHexahedron> aHexahedron =
        vtkSmartPointer<vtkQuadraticHexahedron>::New();
      vtkSmartPointer<vtkPoints> points =
        vtkSmartPointer<vtkPoints>::New();

      double *pcoords = aHexahedron->GetParametricCoords();
      vtkSmartPointer<vtkMinimalStandardRandomSequence> rng =
        vtkSmartPointer<vtkMinimalStandardRandomSequence>::New();
      points->SetNumberOfPoints(aHexahedron->GetNumberOfPoints());
      rng->SetSeed(5070); // for testing
      for (auto i = 0; i < aHexahedron->GetNumberOfPoints(); ++i)
      {
        double perturbation[3];
        for (auto j = 0; j < 3; ++j)
        {
          rng->Next();
          perturbation[j] = rng->GetRangeValue(-0.1, 0.1);
        }
        aHexahedron->GetPointIds()->SetId(i, i);
        points->SetPoint(i,
                         *(pcoords + 3 * i) + perturbation[0],
                         *(pcoords + 3 * i + 1) + perturbation[1],
                         *(pcoords + 3 * i + 2) + perturbation[2]);
      }

      // Add the points and hexahedron to an unstructured grid
      vtkSmartPointer<vtkUnstructuredGrid> uGrid =
        vtkSmartPointer<vtkUnstructuredGrid>::New();
      uGrid->SetPoints(points);
      uGrid->InsertNextCell(aHexahedron->GetCellType(), aHexahedron->GetPointIds());

      return uGrid;
    }
    }

### CMakeLists.txt

    cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

    project(QuadraticHexahedron)

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

### Download and Build QuadraticHexahedron

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

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

    ./QuadraticHexahedron

WINDOWS USERS

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