There seems to be a mistake in the conditional check where you determine the direction of the normal. You're only flipping the normal of the last face computed in the loop, not all the faces that have normals pointing in the wrong direction.
The check for flipping the normals should occur within the loop where you're iterating over each face. Also, you should be comparing the dot product of each normal with the corresponding face center, not all the centers at once.
function faceNormals = patchnormals(vertices,faces)
normals = zeros(size(faces, 1), 3);
centers = calculateCenters(vertices,faces);
v1 = vertices(faces(i, 1), :);
v2 = vertices(faces(i, 2), :);
v3 = vertices(faces(i, 3), :);
normal = cross(edge1, edge2);
normal = normal / norm(normal);
if dot(normal, centers(i, :) - v1) > 0
function centers = calculateCenters(vertices,faces)
num_faces = size(faces, 1);
centers = zeros(num_faces, 3);
center = (v1 + v2 + v3) / 3;
- The direction check if dot(normal, centers(i, :) - v1) > 0 is inside the loop. This check calculates the dot product of the normal and the vector from the first vertex of the triangle to the center of the face. If the dot product is positive, it means the normal is pointing towards the outside, and if it's negative, it points towards the inside.
- If the normal points towards the outside (which is not what you want), it gets flipped by multiplying by -1.
The code assumes that the mesh is consistently oriented, meaning all the triangles are wound in the same order (clockwise or counter-clockwise). If the mesh is not consistently oriented, the above method won't work correctly, and you would need to preprocess the mesh to ensure consistent orientation or use a more advanced algorithm to determine the correct direction of the normals.
Additionally, it's important to note that if your geometry is not closed (i.e., it has boundaries), the approach of flipping normals based on the center might not work as expected, because there isn't a clear inside or outside.
If you're dealing with a closed surface and you find that the normals are still not consistently pointing outward, you might be dealing with a non-manifold geometry or other issues with the mesh that are causing the inconsistencies. In such cases, you may need to clean up the mesh or use mesh repair tools before recalculating normals.
If you have a visualization tool or software, it's often helpful to visually inspect the normals after computation to verify their directions. If only a few normals are incorrect and you know their indices, you can flip them manually. However, if the issue is widespread, you should check for mesh consistency and consider using a mesh processing library that can handle these issues more robustly.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
- Technical Services and Consulting
- Embedded Systems | Firmware Developement | Simulations
- Electrical and Electronics Engineering
Feel free to contact me.