Steve,
I went back and checked the timing on using AddObject which copies to the heap only:
time1 = chrono::steady_clock::now();
ON_3dmObjectAttributes attribs;
pDoc->GetDefaultObjectAttributes(attribs);
CRhinoMeshObject *meshObject = new
CRhinoMeshObject(attribs);
meshObject->SetMesh(mesh);
if (!pDoc->AddObject(meshObject)) { delete meshObject; meshObject = NULL; }
else { mesh_serial_number = meshObject ? meshObject->RuntimeSerialNumber():0; }
time2 = chrono::steady_clock::now();
d1 = (int32_t)chrono::duration_cast<chrono::microseconds> (time2-time1).count();
vs using AddMeshObject which copies to the stack and then to the heap:
time1 = chrono::steady_clock::now();
CRhinoMeshObject *meshObject = pDoc->AddMeshObject(cmesh);
mesh_serial_number = meshObject ? meshObject->RuntimeSerialNumber():0;
time2 = chrono::steady_clock::now();
d1 = (int32_t)chrono::duration_cast<chrono::microseconds> (time2-time1).count();
Here is a graph of the slowdown caused by using AddMeshObject vs AddObject:
I created the constant mesh cmesh, used with the AddMeshObject method, before the timed section above using this (which has been edited based upon your post below):
const ON_Mesh &cmesh = *mesh;
Thus it appears that using the simpler, perhaps more reliable, AddMeshObject is well more than 2X slower from these timings. This seems to be different than what you expected and is more than the 20% difference I measured in the past.
The difference now is that I precompute the face normals and vertex normals before adding the mesh to the document. If a mesh without these is added, then Rhino automatically computes them. But this takes 4-5X longer than copying the data. Thus the timing difference between the methods shrinks considerably when normals are not precomputed.
Since my C++ code with parallelism computes the normals 4X to 10X faster than Rhino, it saves time to pre-compute them. In fact face-area weighted vertex normals can be computed with little overhead. With the normals all set to go, then all that is left is for Rhino to copy the data to the document, which it does quickly at 4.4 GB/sec on my 4 GHz machine. If the copy is done twice, then it is going to take twice as long at least.
Thus with pre-computed normals, AddObject will always be more than 2X faster than AddMeshObject.
From this exercise with pre-computed normals, I now know that Rhino does virtually nothing to the mesh data when it is added to the document as there is essentially no time left after the copy operation. This seems strange since Rhino will not add invalid meshes with degenerate faces. But there is not enough time to do the copy and check for degenerate faces. It is a mystery to me where Rhino gets this time. But that’s a topic for another day.
Regards,
Terry.