About the normal of a mesh triangle

About the normal of a mesh triangle

GZ1178
Participant Participant
2,891 Views
4 Replies
Message 1 of 5

About the normal of a mesh triangle

GZ1178
Participant
Participant

Dear all,

 

Current I tried to use a series of triangle meshs  to form a polyhedron as an approximation of a very complcated solid. Therefore I triangulated each face of the solid by face.Triangulate() method and collect those triangle meshes. However, I found that the triangle meshes don't have normals. I am wondering how to get the correct normal of each triangle that points outside the polyhedron.

 

Thank you so much!

 

With kind regards

 

Guo Zeng

0 Likes
Accepted solutions (1)
2,892 Views
4 Replies
Replies (4)
Message 2 of 5

cwaluga
Advocate
Advocate

Number each triangles vertices clockwise from 1 to 3, then take something like (x3-x1).CrossProduct(x2-x1). Of course, this local numbering should be consistently enforced globally in the provided mesh data to fix the sign, otherwise you would have some pointing inward, some outward of your polyhedron.

0 Likes
Message 3 of 5

GZ1178
Participant
Participant

Dear Cwaluga,

 

Thank you so much for your reply.

 

I am wondering how to determine the "right" clockwise or counter-clockwise of a triangle. As is shown in the figure below, the vertice of triangle 1 are arranged counterclockwisely and the normal can be calculated correctly(right-hand rule is applied); however, those of triangle 2 are also arranged counterclockwisely but the normal is incorrect.


Figures.jpg

 

if every face of a solid is a planar face that is not a problem because the triangles can directly get the face normal, but in my case some faces are curved ones, therefore it is not easy to get the right normal directions.

 

Thanks again and I'm looking forward to your reply.

 

With kind regards,

 

GZ1178

 

0 Likes
Message 4 of 5

FAIR59
Advisor
Advisor
Accepted solution

You can calculate the normal on any face with the  face.ComputeNormal() method.

 

			Face f;
			XYZ MeshPt; // x1, x2 or x3
			XYZ NormalFromPoints; //  (x3-x1).CrossProduct(x2-x1)
			UV UVpt = f.Project(MeshPt).UVPoint;
			XYZ face_normal = f.ComputeNormal(UVpt);
			XYZ surface_normal = f.OrientationMatchesSurfaceOrientation? face_normal : face_normal.Negate();
			
			XYZ MeshNormal = surface_normal.DotProduct(NormalFromPoints)> 0? NormalFromPoints : NormalFromPoints.Negate();
0 Likes
Message 5 of 5

GZ1178
Participant
Participant

Dear FAIR59,

 

Thank you very much for helping me again.

 

I'm afraid I cannot find the method "OrientationMatchesSurfaceOrientation" maybe because my API is from Revit 2017.

 

I test the code without the method and found that the normal calculated in this method:

 

var tri = mesh.get_Triangle(i);
XYZ pt0 = tri.get_Vertex(0);
XYZ pt1 = tri.get_Vertex(1);
XYZ pt2 = tri.get_Vertex(2);
XYZ vec1 = pt1 - pt0;
XYZ vec2 = pt2 - pt0;
XYZ normal = vec1.CrossProduct(vec2);

 

can always return the same direction as the face.project method does. is that sufficient to say the Revit software have taken the direction of the vertices of the mesh into consideration?

 

Thank you again.

 

Best Regards,

 

0 Likes