+1

MeshGeometryHelper Cut Method

Anonymous 10 years ago 0
This discussion was imported from CodePlex

badgerbadger wrote at 2012-07-26 16:50:

I would like to modify the MeshGeometryHelper.Cut method so that it (also) meshes the opening created by the planar cut (i.e. currently the mesh is open at the location of the plane, I need a closed surface)

Can anyone figure out a neat way of achieving this in the Cut method? (Objo? :) )  (maybe uncommenting some of the commented-code in the Cut method achieves this..?)

Otherwise I guess I will have to get a contour of the planar mesh, attempt to mesh that surface and add to the Cut mesh - but a bit messy.


objo wrote at 2012-08-09 00:59:

Yes, you could change the cut method to also provide the contour(s). See the GetContourSegments...

When you have the contour(s), you can use the MeshBuilder.AddPolygonByCuttingEars to triangulate by the cutting/ears method (easy algorithm, but not very efficient).


badgerbadger wrote at 2012-08-09 10:41:

Many thanks Objo, yes I ended up doing something similar but used the AddPolygon instead of CuttingEars version (which I'll now investigate). I meant to post my hack but moved on to the next problem. Here is the mk1 code ( meshOrig is the original mesh while cutMesh is after the plane cut has been applied; plane is the cut plane).

I should add my code was not required to produce a nice mesh, it is only an intermediate step towards a bigger problem involving segmenting a mesh, so if others use it, it is worth investigating replacing AddPolygon (which applies a fan mesh)..

        private static MeshGeometry3D MeshPlaneCut(MeshGeometry3D meshCut, MeshGeometry3D meshOrig, Plane3D plane)
        {
            //Store the positions on the cut plane
            var segments = MeshGeometryHelper.GetContourSegments(meshOrig, plane.Position, plane.Normal).ToList();

            //assumes largest contour is the outer contour!
            IList<Point3D> vertexPoints = MeshGeometryHelper.CombineSegments(segments, 1e-6).ToList().OrderByDescending(x=>x.Count).First();

            //meshCut the polygon opening and add to existing cut mesh
            var builder = new MeshBuilder(false,false);
            builder.Append(meshCut.Positions, meshCut.TriangleIndices);
            builder.AddPolygon(vertexPoints);
           
            MeshGeometry3D mg3D = builder.ToMesh();

            return mg3D;
        }


ulissespi wrote at 2012-12-24 08:06:

I'm also interested in having a MeshGeometryHelper.Cut that as result creates a solid (non hollow) mesh.

I changed the ApplyCuttingPlanesToModel method in the CuttingPlaneGroup.cs file, replacing the call   

g = MeshGeometryHelper.Cut(g, p, n);

by    

g = MeshGeometryHelper.MeshPlaneCut(g, p, n);   

to use the method described in the previous post, but obviously it didn't work as the parameters are not the same.

Could you please give a bit more detail on how do you call your new method?

(maybe have a piece of the calling code) 

 

Or if someone else found another way of resolving this issue, other code examplesare wellcome.


badgerbadger wrote at 2012-12-24 11:39:

Hi Ulissepi - I should point out that my problem wasn't concerned with drawing a cut (and closed) mesh, only in calculating the mesh so that I could apply a contour algorithm to it. If you need to display the visual then something more sophisticated will be required.

I've tried to pick out the relevant bits from my code. It's been a while so hopefully I have included everything relevant. Hope it helps!

Geometry3D originalGeometry = model.Geometry;
//create a copy of the mesh
var g = originalGeometry as MeshGeometry3D;
MeshGeometry3D gOrig = g.Clone();

//Cut the Mesh and then apply the cutplane

//cp, p & n are the cut plane (Plane3D) and the position (Point3D) and the normal (Vector3D) of the plane

g = MeshGeometryHelper.Cut(g, p, n);

g = MeshPlaneCut(g, gOrig, cp);

GeometryModel3D gm3d = model.Clone();
 gm3d.Geometry = g;