I solved it in a different way. As I wanted to planarize ngon mesh, but only polygons with 4 vertices.
I added this function to my planarize function.
The original problem was that I removed faces n valence mesh from all ngons.
The mesh looked like extruded hex extruded grid and caps at top and bottom were hexes.
After deleting them I wanted again to fill ngons with faces. But this was a bit reverse engineering and took too much time to calculate.
To resurrect this, often Ngons are defined just with a vertex loop, and the triangulation is done on-the-fly. Libraries such as Carve only give you a vertex loop and no faces to work with.
Are there any helper functions to create an Ngon from a vertex loop WITHOUT faces? Or is the best way at the moment to create a Polyline from the vertex loop, triangulate that, then add that as an Ngon? I imagine this to be a bit inefficient, but kind of works for now… ?
void CreateNgon(Mesh m, int[] verts)
{
var pts = verts.Select(x => new Point3d(m.Vertices[x])).ToList();
Polyline pl = new Polyline(pts);
pl.Add(pl[0]);
MeshFace[] mfs = pl.TriangulateClosedPolyline();
for (int i = 0; i < mfs.Length; ++i)
{
mfs[i].A = verts[mfs[i].A];
mfs[i].B = verts[mfs[i].B];
mfs[i].C = verts[mfs[i].C];
mfs[i].D = verts[mfs[i].D];
}
int[] fis = m.Faces.AddFaces(mfs);
m.Ngons.AddNgon(MeshNgon.Create(verts, fis));
}
This is polyline class with that triangulation method .net.
Now it would be good to find code what is exactly that unsafe method and where is the source in native language.
Same stuff in cpp sdk RhinoTriangulate3dPolygon and I think it is not shared via github what is inside. Also other google quires shows examples that requires x and y coords for triangulation calculation so i think you cannot do triangulation without knowing location of vertices.
I adapted an ear clipping algorithm I found, and it seems to work quite alright. I’m sure there is lots of room for optimization, and it could probably be threaded, etc. for making a large amount of Ngons.
It needs to be changed so that you can input a Mesh and give it a list of vertex indices, but that should be pretty straight-forward.
Would be good to make a benchmark sometime to see if ‘new Polyline -> Polyline.TriangulateClosedPolyline()’ is a big slowdown or not, in comparison to a raw meshing implementation, such as the basic ear clipping. I haven’t run into a situation where it’s a huge bottleneck yet, but I’m sure it will happen
Until we get some more Ngon love in R6 and more optimized helper functions, this might be the easiest way to make Ngons from scratch
In any case, maybe I’ll try to whack this into Carverino, to see if we can make use of the Ngon support in Carve.
Yes, exactly. Really just an optimisation, though, I haven’t done any serious testing.
I think the requirement of Ngons to have pre-existing mesh faces to use should be looked at closer, as one of the key advantages AFAIK of Ngons is the flexibility of defining polygons without triangulating them, and only triangulating them when required, and in a way that is optimal for that particular occurrence. Otherwise we might as well just have face groups, similar to smoothing groups in other programs…
Yes, in rhinocommon ngon is just an additional property to track collection of mesh faces.
And when it comes doing something with ngons you always need to have some sort of mapping to jump between triangular mesh properties and ngons.
While in half-edge mesh data structure you do not need any triangulation.
But half-edge has its own issues too.