# Visualise 3D finite element data by slice

**URL:** https://discourse.vtk.org/t/visualise-3d-finite-element-data-by-slice/1900
**Category:** Support
**Created:** [October 10, 2019, 3:07am UTC](https://discourse.vtk.org/t/visualise-3d-finite-element-data-by-slice/1900 "2019-10-10T03:07:57Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![gib](https://discourse.vtk.org/user_avatar/discourse.vtk.org/gib/32/9201_2.png) [@gib](https://discourse.vtk.org/u/gib)
#### Post date: [October 10, 2019, 3:07am UTC](https://discourse.vtk.org/t/visualise-3d-finite-element-data-by-slice/1900/1 "2019-10-10T03:07:57Z")

</div>

I have a program that solves for scalar fields on a 3D finite element mesh, and I I want to be able to view slices, e.g. 2D field at a given value of z. Further, I want to display this within a Qt application. I assume that if I can do it in a VTK program it can be done in Qt. Is there an example that does this?

I can see that from the intersection of a plane with the 3D tet mesh I can generate a set of 2D points with their associated values, and from the points, using vtkDelaunay2d I can create a 2D unstructured triangle mesh. Not sure what should come next.

---

<div class="post-metadata">

### Author: ![gib](https://discourse.vtk.org/user_avatar/discourse.vtk.org/gib/32/9201_2.png) [@gib](https://discourse.vtk.org/u/gib)
#### Post date: [October 11, 2019, 6:18am UTC](https://discourse.vtk.org/t/visualise-3d-finite-element-data-by-slice/1900/2 "2019-10-11T06:18:35Z")

</div>

I have something working, but it seems rather contrived and more complicated than it needs to be.  
The first part, creating a set of points and values where the selected plane intersects the 3D mesh is straightforward (because the elements are all tetrahedrons.) Putting this aside, I wanted to find out how to make a 2D display of an unstructured grid with values at the nodes.  
Using one of the examples as a guide, I do this:  
Create a set of (x,y,z) points (the z value will be used to provide the value, i.e. the color.)  
vtkPoints \*points = vtkPoints::New();  
for (int i = 0; i\<npts; i++) {  
points-\>InsertNextPoint(x[i], y[i], z[i]);  
}  
vtkPolyData \*inputPolyData = vtkPolyData::New();  
inputPolyData-\>SetPoints(points);  
// Triangulate the grid points  
vtkDelaunay2D \*delaunay = vtkDelaunay2D::New();  
delaunay-\>SetInput(inputPolyData);  
delaunay-\>Update();  
vtkPolyData \*outputPolyData = delaunay-\>GetOutput();  
colors-\>Initialize();  
for(int i = 0; i \< outputPolyData-\>GetNumberOfPoints(); i++) {  
double \*p = outputPolyData-\>GetPoint(i);  
double dcolor[3];  
colorLookupTable-\>GetColor(p[2],dcolor);  
p[2] = 0;  
outputPolyData-\>GetPoints()-\>SetPoint(i,p); // flatten the surface  
unsigned char \*color = new unsigned char[3];  
for(int j = 0; j \< 3; j++) {  
color[j] = (unsigned char)( 255 \* dcolor[j] );  
}  
colors-\>InsertNextTuple3(color[0], color[1], color[2] );  
}  
outputPolyData-\>GetPointData()-\>SetScalars(colors);

```
vtkPolyDataMapper *mapper = vtkPolyDataMapper::New();
mapper->SetInputConnection(outputPolyData->GetProducerPort());

```

Then an actor is created, using mapper, and rendered in the usual way.  
This works, but I can’t believe it’s the best way to do it. If I don’t flatten the surface by setting p[2] = 0 the z value at a point is used to create a terrain image, which I don’t want, I just want the coloring.

I’d be grateful if somebody could tell me a better way to make a “heat map” picture of an unstructured 2D grid of points with values.

---

<div class="post-metadata">

### Author: ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)
#### Post date: [November 7, 2019, 12:07pm UTC](https://discourse.vtk.org/t/visualise-3d-finite-element-data-by-slice/1900/3 "2019-11-07T12:07:14Z")

</div>

This is implemented in ParaView and 3D Slicer (see [description](https://blog.kitware.com/3d-slicer-adds-volumetric-mesh-support/) and [implementation](https://github.com/Slicer/Slicer/blob/master/Libs/MRML/DisplayableManager/vtkMRMLModelSliceDisplayableManager.cxx)). You can use/extend/customize these applications (I would recommend this) or copy-paste code from these applications into yours (may sound like a good idea at first but in the long term you spend a lot more time with keep copying code and maintaining it).
