Mesh Vertices Precision Weirdness

I might be missing something obvious, but I have several meshes where the following code:

A = myMesh.Vertices[0];

B = myMesh.Vertices.ToPoint3dArray()[0];

produces two different results:

A: {838541.125, 832133.25, 14.661953}
B: {838541.115259, 832133.273086, 14.661953}

Note that the X coordinate is rounded in the case of A, and not rounded in the case of B. I wish I could upload the offending mesh too, but it’s not mine to share. This causes later down the line various inconsistencies, resulting in a bad mesh :crying_cat_face:

Not sure whether this is something to avoid entirely, but on the surface you’d expect them to yield similar results.

Hi @idid,
myMesh.Vertices[0] is returning the Point3f for that vertex (https://developer.rhino3d.com/api/RhinoCommon/html/T_Rhino_Geometry_Collections_MeshVertexList.htm)
while ToPoint3dArray is converting those points to Point3d

1 Like

Hi @idid

myMesh.Vertices is an IEnumerable<Point3f>, so the item you are accessing is of type Point3f

ToPoint3dArray() copies all vertices to a new array of [Point3d], so the item you are accesing becomes of type Point3d

You could either cast the first one to (Point3d) or use ToPoint3fArray() on the second one to keep things uniform

1 Like

ah. @DanielPiker was faster :slight_smile:

Sorry for the crosspost! You actually provided a solution though :slight_smile:

Ha! thanks guys! I should’ve checked the types more carefully. On first read I was… is Daniel crazy? then noticed the bold emphasis :wink:

1 Like

I did wonder at first if setting UseDoublePrecisionVertices to True might help here
https://developer.rhino3d.com/api/RhinoCommon/html/P_Rhino_Geometry_Collections_MeshVertexList_UseDoublePrecisionVertices.htm
but it seems that if you do this then retrieve a vertex position using Mesh.Vertices[0], it still returns a Point3f
Maybe someone else from McNeel can clarify how this works. Does it only affect vertices which are added or moved after switching it on? and does Mesh.Vertices[0] then return a Point3d?

As far as I understand it, traditionally a Mesh would only have single precision vertices. To keep this API going forward, but also be able to support double precision vertices, the support for this has been “bolted on” (not meant in a negative way).
So the default mesh.Vertices collection will still use single precision, but if UseDoublePrecisionVertices has been set to true, the storage will be in double precision.

To access in double precision, you can use mesh.Vertices.SetVertex(int, Point3d) or mesh.Vertices.Add(Point3d) to set/add a vertex in double precision, and mesh.Vertices.Point3dAt(int) to get a double precision vertex. This also works if UseDoublePrecisionVertices is not set to true, but then the storage will be in single precision.

7 Likes

Thanks @menno, really helpful explanation