GH - Same Mesh instance - How to check?

I’m trying to check for equality of meshes in two ways:

  1. Same (not only similar) Object instance and, at other times I want to check for
  2. Similar Geometry

Geometric similarity is often given if Vertex.Count and Faces.Count are the same for both instances but at the language level the function Object.ReferenceEquals(A, A2) comparing the same instance in Rhino, returns false. That was not expected.

So, why are the same Rhino instances considered different instances in Grasshopper?

The code which is returning the above results:

    A_EQ_A2 = Object.ReferenceEquals(A, A2); // Huh?, false(!)    
    //A_EQ_A2 = A.Vertices.Count == A2.Vertices.Count && A.Faces.Count == A2.Faces.Count;
    
    A_EQ_B = Object.ReferenceEquals(A, B); // Expected to be false.
    //A_EQ_B = A.Vertices.Count == B.Vertices.Count && A.Faces.Count == B.Faces.Count;

A_EQ_A2 = A.Equals(A2); also returns false for the same (Rh) instance as in the picture above. :frowning:

Best way to check for equality between two RhinoObject mesh instances?

// Rolf

The mesh is duplicated in memory when it is imported from Rhino to avoid unwanted changes percolating either way. There’s also some casting involved when dealing specifically with c# components, because the assumption is that scripters will be even less worried about directly modifying the input data than component developers.

I understand, but what is the best way to check for (instance) equality?

// Rolf

ReferenceEquals is the best way to check for instance equality. Value equality is much less well defined. Do the meshes have to be exactly alike, or can for example the vertex ordering be different?

If you’re trying to find out whether two meshes have both been referenced from the same Rhino mesh, then you can check their ReferenceIds. But note this information will probably be stripped as soon as the meshes are modified in any way.

Yes, I had already tried comparing the Guids for the Rhino references but it only returns something like “00000-0000-0000”…

ReferenceEquals works fine once I’ve got them inside the component (but then it’s too late in my case)

The equality check from the inputs doesn’t have to be super cheap (it’s done only once for a solutions which takes ~70ms anyway). Could running through the vertices in some kind of “non-commutative” way somehow identify the mesh as unique?

One can of course just compare the vertice lists. If they are different one can at least quit the comparison earlier than if they’re not… :slight_smile:

// Rolf

I haven’t tested yet, but RH6 has this:

https://developer.rhino3d.com/api/RhinoCommon/html/M_Rhino_Geometry_GeometryBase_GeometryEquals.htm

If doesn’t work for you, then yes, to compare if two mesh values are equal, you have to do a lot of conditionals and in case they’re the same you’ll have compared all their shape data.

1 Like

If you want to compare everything, as in, “these meshes must be the same in every way”, you can always serialise them to byte-arrays using GH_Convert.CommonObjectToByteArray() and compare those. Pretty heavy handed though…

For my purposes I think it’s enough if TopologyVertices are equal.

// Rolf