Hexahedron
VTKExamples/Cxx/GeometricObjects/Hexahedron
Description¶
The hexahedron is a primary three-dimensional cell consisting of six quadrilateral faces, twelve edges, and eight vertices. The hexahedron is defined by an ordered list of eight points. The faces and edges must not intersect any other faces and edges, and the hexahedron must be convex.
Code¶
Hexahedron.cxx
#include <vtkActor.h> #include <vtkCamera.h> #include <vtkCellArray.h> #include <vtkDataSetMapper.h> #include <vtkHexahedron.h> #include <vtkNamedColors.h> #include <vtkPoints.h> #include <vtkProperty.h> #include <vtkRenderWindow.h> #include <vtkRenderWindowInteractor.h> #include <vtkRenderer.h> #include <vtkSmartPointer.h> #include <vtkUnstructuredGrid.h> #include <array> #include <vector> int main(int, char* []) { vtkSmartPointer<vtkNamedColors> colors = vtkSmartPointer<vtkNamedColors>::New(); // Set the background color. std::array<unsigned char , 4> bkg{{51, 77, 102, 255}}; colors->SetColor("BkgColor", bkg.data()); // For the hexahedron; setup the coordinates of eight points. // The two faces must be in counter clockwise order as viewed from the // outside. std::vector<std::array<double, 3>> pointCoordinates; pointCoordinates.push_back({{0.0, 0.0, 0.0}}); // Face 1 pointCoordinates.push_back({{1.0, 0.0, 0.0}}); pointCoordinates.push_back({{1.0, 1.0, 0.0}}); pointCoordinates.push_back({{0.0, 1.0, 0.0}}); pointCoordinates.push_back({{0.0, 0.0, 1.0}}); // Face 2 pointCoordinates.push_back({{1.0, 0.0, 1.0}}); pointCoordinates.push_back({{1.0, 1.0, 1.0}}); pointCoordinates.push_back({{0.0, 1.0, 1.0}}); // Create the points. vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New(); // Create a hexahedron from the points. vtkSmartPointer<vtkHexahedron> hex = vtkSmartPointer<vtkHexahedron>::New(); for (auto i = 0; i < pointCoordinates.size(); ++i) { points->InsertNextPoint(pointCoordinates[i].data()); hex->GetPointIds()->SetId(i, i); } // Add the hexahedron to a cell array. vtkSmartPointer<vtkCellArray> hexs = vtkSmartPointer<vtkCellArray>::New(); hexs->InsertNextCell(hex); // Add the points and hexahedron to an unstructured grid. vtkSmartPointer<vtkUnstructuredGrid> uGrid = vtkSmartPointer<vtkUnstructuredGrid>::New(); uGrid->SetPoints(points); uGrid->InsertNextCell(hex->GetCellType(), hex->GetPointIds()); // Visualize. vtkSmartPointer<vtkDataSetMapper> mapper = vtkSmartPointer<vtkDataSetMapper>::New(); mapper->SetInputData(uGrid); vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New(); actor->GetProperty()->SetColor(colors->GetColor3d("Cornsilk").GetData()); actor->SetMapper(mapper); vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New(); vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New(); renderWindow->SetWindowName("Hexahedron"); renderWindow->AddRenderer(renderer); vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New(); renderWindowInteractor->SetRenderWindow(renderWindow); renderer->AddActor(actor); renderer->SetBackground(colors->GetColor3d("BkgColor").GetData()); renderer->ResetCamera(); renderer->GetActiveCamera()->Azimuth(30); renderer->GetActiveCamera()->Elevation(30); renderWindow->Render(); renderWindowInteractor->Start(); return EXIT_SUCCESS; }
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.3 FATAL_ERROR) project(Hexahedron) find_package(VTK COMPONENTS vtkCommonColor vtkCommonCore vtkCommonDataModel vtkInteractionStyle vtkRenderingCore vtkRenderingFreeType vtkRenderingOpenGL2 QUIET) if (NOT VTK_FOUND) message("Skipping Hexahedron: ${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(Hexahedron MACOSX_BUNDLE Hexahedron.cxx ) target_link_libraries(Hexahedron PRIVATE ${VTK_LIBRARIES}) else () # include all components add_executable(Hexahedron MACOSX_BUNDLE Hexahedron.cxx ) target_link_libraries(Hexahedron PRIVATE ${VTK_LIBRARIES}) # vtk_module_autoinit is needed vtk_module_autoinit( TARGETS Hexahedron MODULES ${VTK_LIBRARIES} ) endif ()
Download and Build Hexahedron¶
Click here to download Hexahedron and its CMakeLists.txt file. Once the tarball Hexahedron.tar has been downloaded and extracted,
cd Hexahedron/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:
./Hexahedron
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.