IsoparametricCellsDemo

VTKExamples/Cxx/GeometricObjects/IsoparametricCellsDemo

Description¶

This example shows the isoparametric cells supported by the VTK. These cells are nonlinear and contain one or more mid-side vertices. Isoparametric elements are typically used in finite element analysis. The term isoparametric is derived from the use of the same shape functions (or interpolation functions) to define the element's geometric shape as are used to define the displacements within the element.

This example illustrates each cell's representation using its parametric coordinates (pcoords) as the vertices of the cell. In practice, the vertices will correspond to physical points in a finite element model. Use vtkTessellatorFilter to better see the shape of the cell. See for example, QuadraticHexahedronDemo and QuadraticTetraDemo.

Other Languages

See (Python)

Question

Code¶

IsoparametricCellsDemo.cxx

#include <vtkActor.h>
#include <vtkActor2D.h>
#include <vtkCamera.h>
#include <vtkCellArray.h>
#include <vtkDataSetMapper.h>
#include <vtkGlyph3DMapper.h>
#include <vtkLabeledDataMapper.h>
#include <vtkNamedColors.h>
#include <vtkPoints.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkTextMapper.h>
#include <vtkTextProperty.h>
#include <vtkTextProperty.h>
#include <vtkUnstructuredGrid.h>

#include <vtkCubicLine.h>

#include <algorithm>
#include <array>
#include <cstdlib>
#include <string>
#include <vector>

// These functions return a vtkUnstructured grid corresponding to the object.
namespace
{
template<typename T> vtkSmartPointer<vtkUnstructuredGrid> MakeUnstructuredGrid(vtkSmartPointer<T>);
}

int main(int, char *[])
{
std::vector<std::string> titles;
std::vector<vtkSmartPointer<vtkTextMapper> > textMappers;
std::vector<vtkSmartPointer<vtkActor2D> > textActors;

std::vector<vtkSmartPointer<vtkUnstructuredGrid> > uGrids;
std::vector<vtkSmartPointer<vtkDataSetMapper> > mappers;
std::vector<vtkSmartPointer<vtkActor> > actors;
std::vector<vtkSmartPointer<vtkRenderer> > renderers;

uGrids.push_back(MakeUnstructuredGrid(

uGrids.push_back(MakeUnstructuredGrid(

uGrids.push_back(MakeUnstructuredGrid(

uGrids.push_back(MakeUnstructuredGrid(

uGrids.push_back(MakeUnstructuredGrid(

uGrids.push_back(MakeUnstructuredGrid(

uGrids.push_back(MakeUnstructuredGrid(

uGrids.push_back(MakeUnstructuredGrid(

uGrids.push_back(MakeUnstructuredGrid(

uGrids.push_back(MakeUnstructuredGrid(

uGrids.push_back(MakeUnstructuredGrid(

uGrids.push_back(MakeUnstructuredGrid(

uGrids.push_back(MakeUnstructuredGrid(

uGrids.push_back(MakeUnstructuredGrid(

uGrids.push_back(MakeUnstructuredGrid(
vtkSmartPointer<vtkCubicLine>::New()));
titles.push_back("VTK_CUBIC_LINE (= 35)");

vtkSmartPointer<vtkNamedColors> colors =
vtkSmartPointer<vtkNamedColors>::New();

vtkSmartPointer<vtkRenderWindow> renWin =
vtkSmartPointer<vtkRenderWindow>::New();
renWin->SetSize(600, 600);
renWin->SetWindowName("Isoparametric Cell");

vtkSmartPointer<vtkRenderWindowInteractor> iRen =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
iRen->SetRenderWindow(renWin);

// Create one sphere for all
vtkSmartPointer<vtkSphereSource> sphere =
vtkSmartPointer<vtkSphereSource>::New();
sphere->SetPhiResolution(21);
sphere->SetThetaResolution(21);

// Create one text property for all
vtkSmartPointer<vtkTextProperty> textProperty =
vtkSmartPointer<vtkTextProperty>::New();
textProperty->SetFontSize(10);
textProperty->SetJustificationToCentered();

// Create and link the mappers actors and renderers together.
for (unsigned int i = 0; i < uGrids.size(); ++i)
{
std::cout << "Creating: " << titles[i] << std::endl;
textMappers.push_back(vtkSmartPointer<vtkTextMapper>::New());
textActors.push_back(vtkSmartPointer<vtkActor2D>::New());

textMappers[i]->GetTextProperty()->SetFontSize(10);
mappers.push_back(vtkSmartPointer<vtkDataSetMapper>::New());
actors.push_back(vtkSmartPointer<vtkActor>::New());
renderers.push_back(vtkSmartPointer<vtkRenderer>::New());
mappers[i]->SetInputData(uGrids[i]);
actors[i]->SetMapper(mappers[i]);
actors[i]->GetProperty()->SetColor(colors->GetColor3d("Tomato").GetData());
actors[i]->GetProperty()->EdgeVisibilityOn();
actors[i]->GetProperty()->SetLineWidth(3);
actors[i]->GetProperty()->SetOpacity(.5);

textMappers[i]->SetInput(titles[i].c_str());
textActors[i]->SetMapper(textMappers[i]);
textActors[i]->SetPosition(50, 10);

// Label the points
vtkSmartPointer<vtkLabeledDataMapper> labelMapper =
vtkSmartPointer<vtkLabeledDataMapper>::New();
labelMapper->SetInputData(uGrids[i]);
vtkSmartPointer<vtkActor2D> labelActor =
vtkSmartPointer<vtkActor2D>::New();
labelActor->SetMapper(labelMapper);

// Glyph the points
vtkSmartPointer<vtkGlyph3DMapper> pointMapper =
vtkSmartPointer<vtkGlyph3DMapper>::New();
pointMapper->SetInputData(uGrids[i]);
pointMapper->SetSourceConnection(sphere->GetOutputPort());
pointMapper->ScalingOff();
pointMapper->ScalarVisibilityOff();

vtkSmartPointer<vtkActor> pointActor =
vtkSmartPointer<vtkActor>::New();
pointActor->SetMapper(pointMapper);
pointActor->GetProperty()->SetDiffuseColor(colors->GetColor3d("Banana").GetData());
pointActor->GetProperty()->SetSpecular(.6);
pointActor->GetProperty()->SetSpecularColor(1.0,1.0,1.0);
pointActor->GetProperty()->SetSpecularPower(100);

}

// Setup the viewports
int xGridDimensions = 4;
int yGridDimensions = 4;
int rendererSize = 240;

renWin->SetSize(
rendererSize * xGridDimensions, rendererSize * yGridDimensions);

for(int row = 0; row < yGridDimensions; row++)
{
for(int col = 0; col < xGridDimensions; col++)
{
int index = row * xGridDimensions + col;

// (xmin, ymin, xmax, ymax)
// (xmin, ymin, xmax, ymax)
double viewport[4] = {
static_cast<double>(col) / xGridDimensions,
static_cast<double>(yGridDimensions - (row + 1))/ yGridDimensions,
static_cast<double>(col + 1) / xGridDimensions,
static_cast<double>(yGridDimensions - row) / yGridDimensions
};

if(index > int(actors.size()) - 1)
{
// Add a renderer even if there is no actor.
// This makes the render window background all the same color.
vtkSmartPointer<vtkRenderer> ren = vtkSmartPointer<vtkRenderer>::New();
ren->SetBackground(colors->GetColor3d("SlateGray").GetData());
ren->SetViewport(viewport);
continue;
}

renderers[index]->SetViewport(viewport);
renderers[index]->SetBackground(colors->GetColor3d("SlateGray").GetData());
renderers[index]->ResetCamera();
renderers[index]->GetActiveCamera()->Azimuth(30);
renderers[index]->GetActiveCamera()->Elevation(-30);
renderers[index]->ResetCameraClippingRange();
}
}

iRen->Initialize();

renWin->Render();

iRen->Start();

return EXIT_SUCCESS;
}

namespace
{
template<typename T> vtkSmartPointer<vtkUnstructuredGrid> MakeUnstructuredGrid(vtkSmartPointer<T> aCell)
{
double *pcoords = aCell->GetParametricCoords();
for (int i = 0; i < aCell->GetNumberOfPoints(); ++i)
{
aCell->GetPointIds()->SetId(i, i);
aCell->GetPoints()->SetPoint(i,
*(pcoords + 3 * i),
*(pcoords + 3 * i + 1),
*(pcoords + 3 * i + 2));
}

vtkSmartPointer<vtkUnstructuredGrid> ug =
vtkSmartPointer<vtkUnstructuredGrid>::New();
ug->SetPoints(aCell->GetPoints());
ug->InsertNextCell(aCell->GetCellType(),aCell->GetPointIds());
return ug;
}

{

vtkSmartPointer<vtkUnstructuredGrid> ug =
vtkSmartPointer<vtkUnstructuredGrid>::New();
return ug;
}
}

CMakeLists.txt¶

cmake_minimum_required(VERSION 3.3 FATAL_ERROR)

project(IsoparametricCellsDemo)

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

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

./IsoparametricCellsDemo

WINDOWS USERS

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