Merge or group the triangles of model which have close normal and compute edge

Hi guys,I wanna handle the model geometry in order to select the region of irregular Polygons. In other application like blender and visiual
component, they can merge faces and compute edge. So I can select the region instead of triangle.

The component snap in VC:


In order to snap edge and points. I use EdgesGeometry:


But I encountered some difficulties in snap face. I wanna classify triangles according to normal , and set a threshold. But it has a poor performance especially in no-Index BufferGeometry. I run my code in editor, and it has a long running time:


And I upload the model and code to my OSS. You can run in three editor.If you have some better function or you are Interested in this topic, please reply me, thanks

model_url: https://oss-yanfeiyu.oss-cn-hangzhou.aliyuncs.com/test_model/IRB1200H_7-705-90_IRC5_Base_Standard_rev0.STL

code_url: https://oss-yanfeiyu.oss-cn-hangzhou.aliyuncs.com/test_model/mesh_classifiction.js

There some things that can be optimized. Here are a few that I see from a first glance, maybe there are more:

  • You scan all triangles and make a separate mesh for each of them – I’m not sure whether this is actually necessary
  • In the classification loop you scan all triangle meshes by looking for their name – this is very slow operation as it pushes the time complexity to O(n2).
  • New instances of vectors a, b and c are created for each triangle (in the second loop) – would it be possible to reuse a, b and c created once?
  • It is not needed to use Math.floor(… * 1000) / 1000) for the threshold, rounding to some precision gives no value in this case
  • Similarly, instead of converting to radians with acos, it might be possible to compare the raw dot product with the cosine of the threshold
  • Imagine there are three non-connected consecutive triangles with the same normals … will your algorithm combine them in one mesh?

1 Like

Thanks to your reply , it was very helpful to me. What method is there to determine whether two triangles are adjacent to solve the problem that there are three non-connected consecutive triangles with the same normal.

I know this is a very old forum, but i there is one thing that could help someone who does need the help.

So first it is definitely O(n^2), we get all the triangles.
iterate through all the triangles

  • for each triangle we iterate through all the other triangles to check if there are any traingles that have the same normal
  • after we find a triangle with the same normal, we use this:
    • we set a plane from the triangle, and check if the other triangle is on the same plane (coplanar), after we find out its the same plane, then we add it to an array.
    • also note that i would use a set so we dont iterate over the same triangle
const trianglePlane =
              new THREE.Plane().setFromNormalAndCoplanarPoint(
                triangles[i].normals,
                triangles[i].vertices[0]
              );
 if (trianglePlane.distanceToPoint(triangles[j].vertices[0]) === 0) {
              group.triangles.push(triangles[j]);
              used.add(j);
            }
1 Like