Fast MeshMesh intersection

Hi all.

Rhino 7.

I’m doing hundreds of Mesh-Mesh intersections to find quickly collisions/clashes…
I’m using MeshMeshFast() method, but i’m getting a:

Rhino.Geometry.Intersect.Intersection.MeshMeshFast(Rhino.Geometry.Mesh, Rhino.Geometry.Mesh)’ is obsolete: ‘Use the MeshMesh() method.’

warning.

I tried MeshMeshFast, but it is actually much slower!


This execute in 8ms :

Mesh ma = Mesh.CreateFromBox(new Box(Plane.WorldXY, new Interval(0, 2), new Interval(0, 2), new Interval(0, 2)), 3, 3, 3);
Mesh mb = Mesh.CreateFromBox(new Box(Plane.WorldXY, new Interval(1, 3), new Interval(1, 3), new Interval(1, 3)), 3, 3, 3);

for(int i = 0;i < 1000;i++){
   Line[] lines = Rhino.Geometry.Intersect.Intersection.MeshMeshFast(ma, mb);
}

And this takes 270ms :

Mesh ma = Mesh.CreateFromBox(new Box(Plane.WorldXY, new Interval(0, 2), new Interval(0, 2), new Interval(0, 2)), 3, 3, 3);
Mesh mb = Mesh.CreateFromBox(new Box(Plane.WorldXY, new Interval(1, 3), new Interval(1, 3), new Interval(1, 3)), 3, 3, 3);

Mesh[] meshes = new Mesh[2];
meshes[0] = ma;
meshes[1] = mb;
Polyline[] intersections;
Polyline[] overlaps;
Mesh overlapmesh;

for(int i = 0;i < 1000;i++){
  Rhino.Geometry.Intersect.Intersection.MeshMesh(meshes, 0.005, out intersections,
  false, out overlaps, false, out overlapmesh, null, System.Threading.CancellationToken.None, null);
}

meshmeshfast intersection.gh (4.9 KB)

Is this expected/normal? There are better/faster methods?

MeshMeshAccurate() seems to be as fast as (or as slow as) MeshMesh() .

1 Like

I think there is a bit of managing a cache going on in fast the first time.

MeshMesh = 254 ms

The Fast Mesh runs about 215 ms first time, then subsequent MeshMeshFast = ~15ms

There are a lot of changes to the mesh intersectors in both speed and accuracy. We will need to look back a ways to see how to best to deal with this.

1 Like

I’m not experiencing that difference from the first execution to the subsequent.


Anyway, I know in last years the mesh collider was remade for accuracy and cleaner output…

My point is, I don’t actually need the Line[] output of MeshMeshFast.
I’m just checking IF 2 meshes collide. (Are there better methods for this?)
(my meshes are simple hitboxes, few faces and vertexes)

Currently, on Rhino 7, MeshMeshFast is good, perfect… but marked as obsolete.
Will that method stop working in future releases of Rhino?
Maybe consider keeping that method or renaming it.

There are the MeshClash methods:

1 Like

Interesting, never saw those!


still… MeshMeshFast is 5x faster than MeshClash, and 15x faster than MeshMesh… or more.
My meshes are quite simple (much simpler than those 2 spheres) and are just a lone mesh clashing with another lone mesh.

I don’t know how the “obsolete” MeshMeshFast method work, but please don’t trash it.


Script for testing:


meshmeshfast intersection 2.gh (10.1 KB)
(move the MD slider…)

Code:
 private void RunScript(int method, Mesh ma, Mesh mb, int n, double t, ref object R)
  {

    if(method == 0){
      for(int i = 0;i < n;i++){
        Line[] lines = Rhino.Geometry.Intersect.Intersection.MeshMeshFast(ma, mb);
        R = lines;
      }
    }

    if(method == 1){
      Mesh[] meshes = new Mesh[2];
      meshes[0] = ma;
      meshes[1] = mb;
      Polyline[] intersections;
      Polyline[] overlaps;
      Mesh overlapmesh;
      for(int i = 0;i < n;i++){
        Rhino.Geometry.Intersect.Intersection.MeshMesh(meshes, t, out intersections,
          false, out overlaps, false, out overlapmesh, null, System.Threading.CancellationToken.None, null);
        if(intersections != null) R = intersections;
      }
    }

    if(method == 2){
      for(int i = 0;i < n;i++){
        Rhino.Geometry.Intersect.MeshClash[] mcs = Rhino.Geometry.Intersect.MeshClash.Search(ma, mb, t, 1);
        List<Point3d> pts = new List<Point3d>();

        foreach(Rhino.Geometry.Intersect.MeshClash mc in mcs){
          if(mc.ClashPoint.IsValid){
            pts.Add(mc.ClashPoint);
          }
        }
        R = pts;
      }
    }

  }

Is the performance of the MeshClash the same with the native MeshClash Grasshopper component. I am under the impression that it would be the fastest method for this type of stuff. And it expects Lists of Meshes as inputs which is nice.

Mesh clash wants two all the meshes at once and it looks for intersections. It does this very roughly.

Running two meshes for each test can work, but it bypasses the optimization that may help with larger groups of meshes.

I understand.

But that’s what I’m doing, simple clashes between just two simple meshes.
(i’m changing the relative orientation of the two meshes, doing it many times iteratively to find the best fitting position)

How should I use MeshClash to have better performance than MeshMeshFast? Is it possible?

Hey @maje90

if you can upgrade to Rhino 8, maybe there are alternatives. Especially if you re-use some of the meshes.

Thanks,

Giulio


Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com