Triangular Elements. Barycentric Coordinates.
For each point , in this triangle one can define the barycentric coordinades of this point using the value of the 2D shape functions defined for this triangle : satisfying . This way, one can express any point using three coordinates (barycentric) . In particular . The barycentric coordinates satisfy . Moreover unless the point is outside of the triangle.
(see https://en.wikipedia.org/wiki/Barycentric_coordinate_system )
Compute Barycentric Coordinates
Given a triangle defined by vertices , compute the barycentric coordinates of point . Verify that .
vertices=[1,1; %define the triangle vertex coordinates
vplot=[vertices;vertices(1,:)]; %just to plot a closed triangle, we add the first point at the end
Compute shape functions
Psi1 = @(x,y) c1(1)+c1(2)*x+c1(3)*y;
Psi2 = @(x,y) c2(1)+c2(2)*x+c2(3)*y;
Psi3 = @(x,y) c3(1)+c3(2)*x+c3(3)*y;
% Compute barycentric coordinates
alpha1 = Psi1(p(1),p(2));
alpha2 = Psi2(p(1),p(2));
alpha3 = Psi3(p(1),p(2));
alpha1+alpha2+alpha3-1 %check error
Exercise 1: Build baryCoord function
Build a function file that returns the barycentric coordinates of a point with respect to a triangle. It must return a flag value isInside with value 1 if the point is inside the triangle and 0 if it is outside.
Use the previous example to check if it works.
Exemple: Correspondence with the Reference Triangle
Usually the reference triangular element, , is the triangle defined by the vertices
We can use barycentric coordinates to make a correspondence between the reference triangle and every other triangle. Let's consider the preivous exemple triangle
Assuming that each vertex has its correspondence , using the barycentric coordinates we obtain the point correspondences: .
verticesTR=[0,0; %define the reference triangle (TR) vertex coordinates
%compute the corresponding point on the reference triangle
vplotTR=[verticesTR;verticesTR(1,:)]; %just to plot a closed triangle, we add the first point at the end
Exercise 1.1: Point correspondence
Consider now the obtained point on the Reference Triangle pTR=(0.4,0.2) and compute the associated point on the Original Triangle. Observe that the barycentric coordinates are the same and the point obtained is (2,2)
Exercise 2: Several Triangles
Let's consider now four triangles adding a central vertex (as it is shown in the figure). We can define these polygons by the coordinates of the vertices and the triangles defined by these vertices.
plotElements(vertex,triang,1); %find this file at *Numerical Factory*
Exercise 2.1: Several Triangles
Use the function build in the previous exercise
to decide if the point belongs to each triangle
Solution= It belongs to triangle 2.
ShapeFunctions2D Pract: Interpolate Temperature in a Triangle Mesh
A triangle mesh is defined by a set of nodes (the vertices coordinates) and a set of elements (the triangles) defined by the indices corresponding to the vertices belonging to each triangle (connectivity matrix).
The usual format is
nodes=[ x1,y1; x2,y2; ......; xN,yN]; a Nx2 matrix for N vertices
elements =[ i1,j1,k1; i2,j2,k2; .....; iM,jM,kM]; a Mx3 matrix for M triangles
Compute the temperature at the point
Get the meshFiles file from Numerical Factory
eval('meshHole'); %load data from the file mesHole.m: nodes and elements
assing a simple temperature value for each vertex. For now Temp=numNode this way the first nodes are cooler than the last ones.
Final step: Internal point interpolation
Finally find the triangle where the point p belongs to (using the baryCoord function) and interpolate the temperature value.
The idea of the code can be:
for all the elements
compute the barycentric coordinates
if (isInside >= 1), pick the element number; break; end;
Take this element and perform the interpolation using the temperatures of the nodes and barycentric coord (see theory presentation).
Solution: Temp= 104.0420; element= 189; nodes=[109, 112, 92];
(c)Numerical Factory 2020