# IsoparametricCellsDemo

vtk-examples/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

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

### 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 <vtkNew.h>
#include <vtkPoints.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkSmartPointer.h>
#include <vtkSphereSource.h>
#include <vtkTextMapper.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>);
} // namespace

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(

uGrids.push_back(

uGrids.push_back(

uGrids.push_back(

uGrids.push_back(

uGrids.push_back(

uGrids.push_back(

uGrids.push_back(

uGrids.push_back(

uGrids.push_back(

uGrids.push_back(

uGrids.push_back(MakeUnstructuredGrid(

uGrids.push_back(MakeUnstructuredGrid(

uGrids.push_back(

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

vtkNew<vtkNamedColors> colors;

vtkNew<vtkRenderWindow> renWin;
renWin->SetSize(600, 600);
renWin->SetWindowName("Isoparametric Cell");

vtkNew<vtkRenderWindowInteractor> iRen;
iRen->SetRenderWindow(renWin);

// Create one sphere for all
vtkNew<vtkSphereSource> sphere;
sphere->SetPhiResolution(21);
sphere->SetThetaResolution(21);

// Create one text property for all
vtkNew<vtkTextProperty> textProperty;
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;
}

{

quadraticPolygon->GetPoints()->SetPoint(0, 0.0, 0.0, 0.0);
quadraticPolygon->GetPoints()->SetPoint(1, 2.0, 0.0, 0.0);
quadraticPolygon->GetPoints()->SetPoint(2, 2.0, 2.0, 0.0);
quadraticPolygon->GetPoints()->SetPoint(3, 0.0, 2.0, 0.0);
quadraticPolygon->GetPoints()->SetPoint(4, 1.0, 0.0, 0.0);
quadraticPolygon->GetPoints()->SetPoint(5, 2.0, 1.0, 0.0);
quadraticPolygon->GetPoints()->SetPoint(6, 1.0, 2.0, 0.0);
quadraticPolygon->GetPoints()->SetPoint(7, 0.0, 1.0, 0.0);
quadraticPolygon->GetPoints()->SetPoint(5, 3.0, 1.0, 0.0);

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


### CMakeLists.txt¶

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(IsoparametricCellsDemo)

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

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


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.