C++ Mesh weld and convert quads to triangles


I have a simple basic question considering C++ Rhino SDK.

I would like to as how can I properly call these two methods RhinoWeldMesh() and ConvertQuadsToTriangles(), I want to replicate RhinoCommand WeldVertices and convert mesh quads to triangles.

I am a bit confused by RhinoWeldMesh it seems that it returns a mesh and it has third input as out mesh also I need somehow to convert const Mesh to non-const:
ON_Mesh* RhinoWeldMesh(const ON_Mesh& MeshIn, double angle_tol, ON_Mesh* pMeshOut = 0);

This is my code that I do not know if I write it correctly:

CRhinoGetObject go;
go.SetCommandPrompt(L"Select 2 meshes");
go.GetObjects(2, 2);

if (go.CommandResult() == CRhinoCommand::success) {

    //Once pointcloud is selected get parameters

    const ON_Mesh* meshRhino0 =  go.Object(0).Mesh();
    const ON_Mesh* meshRhino1 =  go.Object(1).Mesh();

    ON_Mesh* meshRhino0_ = RhinoWeldMesh(*meshRhino0,0.01);

Weld does not seem to work the way I write, I tried also this:

ON_Mesh* meshRhino0_;
meshRhino0_ = RhinoWeldMesh(*meshRhino0,0.01,meshRhino0_);

I think a correct answer is this:

      ` ON_Mesh* meshRhino0_=RhinoWeldMesh(*meshRhino0,3.14159265359);`

Yes, if you want to weld also those edges that connect faces in 90 degree angle (as in the box edges) then you have to set the angle_tol parameter to at least 90 degrees (half a pi). Passing in pi (180 degrees) means you basically want to weld all edges regardless of the angle.

The third parameter is optional. If you pass in a pointer to an ON_Mesh object then that ON_Mesh will be modified to store the result of the welding and pointer to that object will be returned. If that parameter is null - as it is by default - then a new ON_Mesh object is allocated that will store the result of the welding.

Thanks for the explanation it helps a lot.

In the later case when optional mesh is NULL do I need to delete the result mesh to free the memory, or it will be removed from stack automatically when function ends?

Yes, you need to delete it - it is on heap.

ON_Mesh* pWeldedMesh = RhinoWeldMesh(*pMesh, 3.14);
// Use the pWeldedMesh for something
delete pWeldedMesh;

Or if you want to use stack then

ON_Mesh weldedMesh; // This is in stack
RhinoWeldMesh(*pMesh, 3.14, &weldedMesh); // This will modify the weldedMesh
// Do something with weldedMesh
1 Like

Thank you