Could there be a .ToDoubleArray methods in the Mesh.Vertices?

And may I ask what is this ON_Mesh type?
Does this mean I need to import the opennurbs lib on my cpp side?

OK, I installed the Rhino7 SDK, but I cannot find any tips on adding the SDK to my existing cpp project here: C/C++ Guides with C/C++

Could anyone point out how to do it?

Hi @xliotx,

Maybe this helps?

public static class MeshExtensions
{
  public static double[] ToDoubleArray(this Mesh mesh)
  {
    if (!mesh.Vertices.UseDoublePrecisionVertices)
      return new double[0];

    var rc = new double[mesh.Vertices.Count * 3];
    int index = 0;
    for (var i = 0; i < mesh.Vertices.Count; i++)
    {
      var pt = mesh.Vertices.Point3dAt(i);
      rc[index++] = pt.X;
      rc[index++] = pt.Y;
      rc[index++] = pt.Z;
    }
    return rc;
  }
}

You can use like this:

protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
  var filter = ObjectType.Mesh;
  var rc = RhinoGet.GetOneObject("Select mesh", false, filter, out var objref);
  if (rc != Rhino.Commands.Result.Success || null == objref)
    return rc;

  var mesh = objref.Mesh();
  if (null == mesh || !mesh.Vertices.UseDoublePrecisionVertices)
    return Result.Cancel;

  var doubles = mesh.ToDoubleArray();
  foreach (var d in doubles)
    RhinoApp.WriteLine(d.ToString());

  return Result.Success;
}

– Dale

@dale
Thanks.

Where should I put the MeshExtension class?

I tried to put it outside my namespace where the conversion of mesh vertices happens, but it doesn’t work.

BTW, I guess you’re the man who is in charge of the opennurbs?
I tried to build it and use it in my project somehow, but the compilation works in debug mode, not the release mode, saying

LNK1181 cannot open input file '<OPENNURBS_PUBLIC_LIBS_DIR>\bin\x64\Debug.obj'

Not sure why the release mode will try to find a Debug.obj file…

I’m writing a cpp+c# plugin for Grasshopper, so not sure how the example can be applied to a mesh loaded through Grasshopper…

I’d put the extension class inside your namespace.

More on extension methods:

If this is the case, then you must link with the version of openNURBS that comes with Rhino, not build and link some other version into your DLL. The easiest way to do this is to build a Rhino-dependent DLL.

– Dale

You could use the MeshUnsafeLock class if you want to directly stream the arrays to C++

Is there an example to use it?
Check the docs, but I’m a bit new to C#…
image

Mesh msh = .......; // Some mesh
using var meshRawData = msh.GetUnsafeLock();
var ptrToP3d = meshRawData.VertexPoint3dArray(out var verticeCnt);
...

But pay attention ptrToP3d is a Point3d* rather than Point3d[]. You probably want to memcpy later.

Pay attention on what you want to do here. Raw memory manipulation is quite dangerous in C# and may be slow than expected. I’d recommend C++/CLI if you are unfamliar with C#.

Thanks for the tips.
I’m using cpp, but need to bring the mesh I get from Grasshopper into the C++ world first.
And that’s what I’m actually doing…

OK, problem solved, thank you everyone!

Two approaches are working:

  1. Could there be a .ToDoubleArray methods in the Mesh.Vertices? - #6 by dale
    Build the first class method, and then do
double[] tar = MeshExtensions.ToDoubleArray(my mesh)
  1. Could there be a .ToDoubleArray methods in the Mesh.Vertices? - #12 by gankeyu
    And process on the point array.

I would use reflection to call Mesh's .(Non)ConstPointer() which can be converted to the C++ ON_Mesh object.

This will require to integrate opennurbs into my cpp project, which after trying yesterday, didn’t succeed…
The doc on the website is not very clearly written and if I follow the process, differen’t kinds of errors always occur. For instance, this:

@menno Is there a similar approach to get the pointer of C# containers, like List<Point3d>?

I assume the corresponding openNURBS class should be ON_3dPointArray, but cannot find the command to do the conversion.

To be honest, I don’t exactly know. Maybe you can get a pointer, but you need to make sure that the garbage collector doesn’t touch it, so you need to pin the pointer until you ar done in c++.
If you search for terms like marshalling and pointer pinning, maybe something useful turns up.

Another thing to look at is what rhino provides in the Rhino.Runtime.Interop class and the interop wrappers

1 Like

Ah, thanks for pointing out the InterpoWrappers. I didn’t know that.
Yes, that namespace contains all I need.

Problem solved.

1 Like

How come the Rhino.Runtime.InteropWrappers have SimpleArrayPoint3d, but not SimpleArrayVector3d ???

I think that is because they are memory-wise identical, a list of (x,y,z) tuples.

Yes, I understand now.
But wouldn’t it be helpful to provide such a wrapper, rather than relying on the user to do the transition?

Hi @xliotx,

The wrappers you find in Rhino.Runtime.InteropWrappers are what are used. That is, you don’t see a SimpleArrayVector3d because we haven’t needed one.

Make sense?

– Dale