# Colouring Tetramesh - Element wise

17 views (last 30 days)
Commented: ADSW121365 on 27 Jan 2022
I'm working with tetrahedral finite elements. I can happily set up a mesh and work with said mesh. A really simple example code then gives the following with 72 elements:
clear,close all; clc; dbstop if error;
Nx = 4; Ny =3; Nz = 3; %No. Nodes Nx*Ny Ny = odd
%Variables to create Mesh:
a = 1; b = 1; c = 2;
X = linspace(0,a,Nx); Y = linspace(0,c,Ny); Z = linspace(0,a,Nz);
[X,Y,Z] = meshgrid(X,Y,Z); X = X(:); Y = Y(:); Z=Z(:);
%Meshing:
TRI = delaunay(X,Y,Z);
N = [X,Y,Z]; %Node Coordinates Vector
%figure; triplot(TRI,X,Y);
figure; tetramesh(TRI,[X(:),Y(:),Z(:)]), xlabel('x'),ylabel('y'),zlabel('z');
As a part of my model, each individual element is assumed to be homogoenous and as such has a material property associated with it which for the example here is given by:
NE = size(TRI,1); %Number of Elements
Elements = 1:NE;
Rho_e = zeros(NE,1);
Rho_e(Elements(sum(Y(TRI) <= b,2)==3)) = rho;
When I'm working with triangles in 2D. I can make use of trisurf and create a plot of the material properties:
Nx = 13; Ny =13; %No. Nodes
%Variables to create Mesh:
a = 1; b = 1; c = 2;
X = linspace(0,a,Nx); Y = linspace(0,c,Ny);
[X,Y] = meshgrid(X,Y); X = X(:); Y = Y(:);
TRI = delaunay(X,Y); %Simple Mesh
figure;
trisurf(TRI,X,Y,0*X,Rho_e,'edgecolor','k','facecolor','interp');
I'm currently trying then to create this plot for a 3D mesh of delauney tetrahedra as set up in the previous code. My current attempt looks like:
tetramesh(TRI,[X,Y,Z],Rho_e,'facecolor','interp');
view(2);
axis equal; colorbar; xlim([0,a]); ylim([0,c]); zlim([0,a]);
Which results in the following error message:
Warning: Error creating or updating Patch
Error in value of property FaceVertexCData
Number of colors must equal number of vertices
> In defaulterrorcallback (line 12)
In Oct8_3DFEM (line 56)
My guess is that I somehow need to manipulate my Rho vector from reprsenting individual elements to individual vertices. However for physical purposes it is important I find Rho_e first. The final image I'm searching for will essentially be a 3D projection of the 2D one attached above. Any thoughts, advice or other suggestions would be greatly appreciated
Update:
figure;
tetramesh(TRI,[X Y Z],Rho_e);
Using this syntax, I get the following figure:
The error message then arises when I try to set it to interpolate the colours
figure;
tetramesh(TRI,[X Y Z],Rho_e,'facecolor','interp');
which I assume means this is a syntax error.

darova on 21 Oct 2019
Manipulate with faceVertexCData property if you want to assing some colors
get(p)
%%
FaceVertexCData = [ (72 by 1) double array]
%%
Vertices = [ (169 by 3) double array]
Example
p1 = tetramesh(TRI,[X,Y,Z]);
set(p1,'facevertexcdata',jet(numel(X)),'facecolor','interp');
It does work in producing the image I'm interested in, just not as nicely as the tetramesh function
:
The difference being tetramesh plots the whole tetrahedron as a single solid element with transparency automatically chosen to be able to see all the elements, whilst patch seems to construct a series of solid 2D shapes to represent the tetrahedra.

Alessandro Sicilia on 27 Jan 2022
Maybe you no longer working on this, but i have found a faster and simpler way to fulfill the problem.
While the 'tetramesh' function is very slow when you use a high number of elements, the 'patch' function is amazing: furthermore it fills any element shape, regardless its solid or plane nature, guaranteeing also the color interpolation.
It should work as follow:
patch('Faces',TRI,'Vertices',[X Y Z],'FaceVertexCData',Rho_e,'FaceColor','interp')
Anyway, you can refer to the 'patch' function HelpPage in the Matlab documentation.
Hoping to have been useful, have a good day
This is the process I now use, but hopefully this comment will help others. My variant uses a modified version of the plotcube function which is available on file exchange to plot all cubes in a single step (opposed to looping through over all individual cubes).
One key note is this becomes problematic (on my system at least) at resolutions above 200x200x200 voxels and much of the interacterability of the figure is lost. [128GB DDR4 4000MhZ + OC'd Ryzen 5950X suggest this isn't a hardware issue]

### Categories

Find more on Graphics Performance in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!