UGrid
Repository source: UGrid
Description¶
Creation of an unstructured grid.
Info
See Figure 5-21 in Chapter 05 the VTK Textbook.
Other languages
See (Python)
Question
If you have a question about this example, please use the VTK Discourse Forum
Code¶
UGrid.cxx
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkDataSetMapper.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPoints.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkUnstructuredGrid.h>
int main(int, char*[])
{
int i;
static double x[27][3] = {
{0, 0, 0}, {1, 0, 0}, {2, 0, 0}, {0, 1, 0}, {1, 1, 0}, {2, 1, 0},
{0, 0, 1}, {1, 0, 1}, {2, 0, 1}, {0, 1, 1}, {1, 1, 1}, {2, 1, 1},
{0, 1, 2}, {1, 1, 2}, {2, 1, 2}, {0, 1, 3}, {1, 1, 3}, {2, 1, 3},
{0, 1, 4}, {1, 1, 4}, {2, 1, 4}, {0, 1, 5}, {1, 1, 5}, {2, 1, 5},
{0, 1, 6}, {1, 1, 6}, {2, 1, 6}};
static vtkIdType pts[12][8] = {
{0, 1, 4, 3, 6, 7, 10, 9}, {1, 2, 5, 4, 7, 8, 11, 10},
{6, 10, 9, 12, 0, 0, 0, 0}, {8, 11, 10, 14, 0, 0, 0, 0},
{16, 17, 14, 13, 12, 15, 0, 0}, {18, 15, 19, 16, 20, 17, 0, 0},
{22, 23, 20, 19, 0, 0, 0, 0}, {21, 22, 18, 0, 0, 0, 0, 0},
{22, 19, 18, 0, 0, 0, 0, 0}, {23, 26, 0, 0, 0, 0, 0, 0},
{21, 24, 0, 0, 0, 0, 0, 0}, {25, 0, 0, 0, 0, 0, 0, 0}};
vtkNew<vtkNamedColors> colors;
vtkNew<vtkRenderer> renderer;
vtkNew<vtkRenderWindow> renWin;
renWin->AddRenderer(renderer);
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renWin);
vtkNew<vtkPoints> points;
for (i = 0; i < 27; i++) points->InsertPoint(i, x[i]);
vtkNew<vtkUnstructuredGrid> ugrid;
ugrid->Allocate(100);
ugrid->InsertNextCell(VTK_HEXAHEDRON, 8, pts[0]);
ugrid->InsertNextCell(VTK_HEXAHEDRON, 8, pts[1]);
ugrid->InsertNextCell(VTK_TETRA, 4, pts[2]);
ugrid->InsertNextCell(VTK_TETRA, 4, pts[3]);
ugrid->InsertNextCell(VTK_POLYGON, 6, pts[4]);
ugrid->InsertNextCell(VTK_TRIANGLE_STRIP, 6, pts[5]);
ugrid->InsertNextCell(VTK_QUAD, 4, pts[6]);
ugrid->InsertNextCell(VTK_TRIANGLE, 3, pts[7]);
ugrid->InsertNextCell(VTK_TRIANGLE, 3, pts[8]);
ugrid->InsertNextCell(VTK_LINE, 2, pts[9]);
ugrid->InsertNextCell(VTK_LINE, 2, pts[10]);
ugrid->InsertNextCell(VTK_VERTEX, 1, pts[11]);
ugrid->SetPoints(points);
vtkNew<vtkDataSetMapper> ugridMapper;
ugridMapper->SetInputData(ugrid);
vtkNew<vtkActor> ugridActor;
ugridActor->SetMapper(ugridMapper);
ugridActor->GetProperty()->SetColor(colors->GetColor3d("Peacock").GetData());
ugridActor->GetProperty()->EdgeVisibilityOn();
renderer->AddActor(ugridActor);
renderer->SetBackground(colors->GetColor3d("Beige").GetData());
renderer->ResetCamera();
renderer->GetActiveCamera()->Elevation(60.0);
renderer->GetActiveCamera()->Azimuth(30.0);
renderer->GetActiveCamera()->Dolly(1.2);
renWin->SetSize(640, 480);
renWin->SetWindowName("UGrid)");
// interact with data
renWin->Render();
iren->Start();
return EXIT_SUCCESS;
}
CMakeLists.txt¶
cmake_minimum_required(VERSION 3.12 FATAL_ERROR)
project(UGrid)
find_package(VTK COMPONENTS
CommonColor
CommonCore
CommonDataModel
InteractionStyle
RenderingContextOpenGL2
RenderingCore
RenderingFreeType
RenderingGL2PSOpenGL2
RenderingOpenGL2
)
if (NOT VTK_FOUND)
message(FATAL_ERROR "UGrid: 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(UGrid MACOSX_BUNDLE UGrid.cxx )
target_link_libraries(UGrid PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
TARGETS UGrid
MODULES ${VTK_LIBRARIES}
)
Download and Build UGrid¶
Click here to download UGrid and its CMakeLists.txt file. Once the tarball UGrid.tar has been downloaded and extracted,
cd UGrid/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:
./UGrid
WINDOWS USERS
Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.