Does Matlab calculate abnormal surface normals?

I have a large FEM model, from where I can get the “surface” of the model, say, the elements and vertex that define the surface of this FEM model. For the purpose of construction (nice stories are always a victory!) I want to do it beautifully. My approach is to use

lungs.Vertex=vtx; lungs.Faces=fcs; patch(lungs,'facecolor','r','edgecolor','none') 

NOTE. I need edgecolor none, since this is 4D data and different FEM have different triangulation, if the graph is plotted, the user will be dizzy.

enter image description here

However, this will output everything in a really simple red color, which is not very nice (since it cannot show the complexity of the figure, which is light, for attention to detail).

so I decided to use ligthing:

 camlight; camlight(-80,-10); lighting phong; 

But then again, this is not entirely correct. Actually, it seems that the nromals fixes are incorrectly calculated by Matlab.

enter image description here

My assumption is that, perhaps, patches are not always defined counterclockwise, and so some normals go in the wrong direction. However, this is not so easy to verify.

Does anyone have a similar problem, or a hint of how I should treat this problem in order to have a good surface here?

EDIT

Just to shake the graph, here is the result obtained using @magnetometer answer:

enter image description here

+5
source share
1 answer

If your model gives you appearance-oriented normals, you can re-sort the faces of your model so that Matlab can correctly calculate its own normals. The following function works if you have triangular faces and external oriented normals:

 function [FaceCor,nnew]=SortFaces(Faces,Normals,Vertices) FaceCor=Faces; nnew=Normals*0; for jj=1:size(Faces,1) v1=Vertices(Faces(jj,3),:)-Vertices(Faces(jj,2),:); v2=Vertices(Faces(jj,2),:)-Vertices(Faces(jj,1),:); nvek=cross(v2,v1); %calculate normal vectors nvek=nvek/norm(nvek); nnew(jj,:)=nvek; if dot(nvek,Normals(jj,:))<0 FaceCor(jj,:)=[Faces(jj,3) Faces(jj,2) Faces(jj,1)]; nnew(jj,:)=-nvek; end end 

If your FEM model does not give you outward oriented normals, one way would be to reconstruct the surface using, for example, a peel algorithm that gives you directionally directed or correctly oriented patches.

EDIT: Since you have no normals, the only solution that comes to my mind is to restore the surface. This implementation of the cortex algorithm worked well for me in the past. All you have to do is:

 [FacesNew,NormalsNew]=MyRobustCrust(Vertices); 

If I remember correctly, FacesNew is not counterclockwise oriented yet, but you can use the SortFaces algorithm that I wrote above to fix this, since now you have correctly oriented face normals, i.e. run:

 [FaceCor,~]=SortFaces(FacesNew,NormalsNew,Vertices) 

If you use Matlab reducepatch (e.g. reducedmodel=reducepatch(fullmodel,reduction); ) to reduce the number of vertices, you will have to restore the surface again, since reducepatch does not seem to maintain the correct orientation of the patches.

+3
source

Source: https://habr.com/ru/post/1203149/


All Articles