wo80 / Triangle.NET

C# / .NET version of Jonathan Shewchuk's Triangle mesh generator.
442 stars 81 forks source link

Mesh not constrained inside the polygon boundaries #40

Closed mustbau closed 9 months ago

mustbau commented 9 months ago

Hi Iam trying to create a mesh, which should constrain inside a polygon or confirms to the polygon boundary. Unfortunately the output mesh doesnt constrain in the polygon boundaries. I am using segments to define Polygon. Here is my code:

' // creat 2d vertices from the modelline points<XYZ> and add to  a List<Vertex> outerbound
            for (int i = 0;i<boundarypoints.Count;i++) 
            {
                vert = new TriangleNet.Geometry.Vertex(boundary[i].X, boundary[i].Y, 1);
                outerbound.Add(vert);
            }

// now create segments using the vertices in the list<Vertex> outerbound and add them to polygon object
            for (int i = 0; i < outerbound.Count; i++) 
            {
                if(i<outerbound.Count-1)
                {
                    polygon.Add(new TriangleNet.Geometry.Segment(outerbound[i], outerbound[i + 1], 1), 0);
                }
                else if(i == outerbound.Count - 1)
                {
                    polygon.Add(new TriangleNet.Geometry.Segment(outerbound[i], outerbound[0], 1), 0);
                }

            }

   // now add inner pts to the polygon object
            for (int i = 0; i < Innerpts.Count; i++)
            {
                polygon.Add(new TriangleNet.Geometry.Vertex(boreholepts[i].X, boreholepts[i].Y, 1));
            }

            var triQuality = new TriangleNet.Meshing.QualityOptions() { MinimumAngle = 20.0 };
            var options = new ConstraintOptions() { ConformingDelaunay = true };
            // var mesh = p.Triangulate(new ConstraintOptions() { Convex = true });
            var mesh = p.Triangulate(options,triQuality);
            var vertices = mesh.Vertices.ToList();
            var triangles = mesh.Triangles.ToList();
'
wo80 commented 9 months ago

Instead of the first two for loops, write something like

polygon.Add(new Contour(boundarypoints.Select(p => new Vertex(p.X, p.Y, 1)), 1));

Now, the third loop looks suspicious:

for (int i = 0; i < Innerpts.Count; i++)
{
    polygon.Add(new TriangleNet.Geometry.Vertex(boreholepts[i].X, boreholepts[i].Y, 1));
}

What is Innerpts? Shouldn't it be boreholepts.Count? Instead of the loop, just write

polygon.Points.AddRange(boreholepts.Select(p => new Vertex(p.X, p.Y, 1)));

This is the only thing I can spot right now. Please post the complete, working example code if you need more help.

mustbau commented 9 months ago

Thanks for your reply: Yes i tried using the contours as you suggested in above code. But the traingles still dont constrain to the polygon. as for the third for loop you are right, it should be '''boreholepts.Count'''. i changed it to '''innerpts''' here in the issue just for better understanding. At the last two lines of code i take out traingles and vertices of mesh and render them Here is my new code :

  var p = new TriangleNet.Geometry.Polygon();
     p.Add(new Contour(boundary.Select(px => new Vertex(px.X, px.Y, 1)), 1));
     p.Points.AddRange(boreholepts.Select(qy => new Vertex(qy.X, qy.Y, 1)));

     var triQuality = new TriangleNet.Meshing.QualityOptions() { MinimumAngle = 20.0 };
     var options = new ConstraintOptions() { ConformingDelaunay = true };

     var mesh = p.Triangulate(options, triQuality);

    // use the triangles in the mesh object to render it later  
     var vertices = mesh.Vertices.ToList();
     var triangles = mesh.Triangles.ToList();

so in the last two lines of code i take out the triangles of the mesh and render them: Below two images of the rendered mesh (in brown) and polygon (blue lines)

OuterPolygonAndMeshTringles2 OuterPolygonAndMeshTringles3

wo80 commented 9 months ago

If you need more help, please also post the input data and the output mesh. You can use

TriangleNet.IO.FileProcessor.Write(IPolygon polygon, string filename)
TriangleNet.IO.FileProcessor.Write(IMesh mesh, string filename)

Triangle.NET also offers some help troubleshooting:

mustbau commented 9 months ago

Hi, Thank you for your guideance and help, i have saved the input polygon and the output mesh files. Boundary Polygon: '''

18 2 0 0
0 125.440710048282 -129.625686939517
1 104.841007216555 -194.185276790119
2 67.1727790671414 -193.425549312091
3 10.9605598288596 -301.449993892171
4 -103.403891139292 -240.575270476172
5 -109.891796128172 -87.1943157152896
6 -33.593252198118 -111.506665034748
7 -2.19672204675081 -196.108044429279
8 -94.9067344826639 -328.762402339435
9 -134.742378545997 -165.68159215945
10 -23.993372113709 -81.9441931042491
11 -55.4504593182355 -108.016076117754
12 -15.1223753280938 -145.596784777939
13 88.9120734892786 -164.941272962838
14 -87.6286089234054 -320.363517060876
15 -54.3943569567055 -196.090879265219
16 -6.49376640468836 -236.715879265219
17 -55.1689632553607 -294.654527559876
11 0
0 0 1
1 1 2
2 2 3
3 3 4
4 4 5
5 5 6
6 6 7
7 7 8
8 8 9
9 9 10
10 10 0
0

'''

Mesh.Poly:

'''

60 2 0 1
0 125.440710048282 -129.625686939517 1
1 104.841007216555 -194.185276790119 1
2 67.1727790671414 -193.425549312091 1
3 10.9605598288596 -301.449993892171 1
4 -103.403891139292 -240.575270476172 1
5 -109.891796128172 -87.1943157152896 1
6 -33.593252198118 -111.506665034748 1
7 -2.19672204675081 -196.108044429279 1
8 -94.9067344826639 -328.762402339435 1
9 -134.742378545997 -165.68159215945 1
10 -23.993372113709 -81.9441931042491 1
11 -55.4504593182355 -108.016076117754 1
12 -15.1223753280938 -145.596784777939 1
13 88.9120734892786 -164.941272962838 1
14 -87.6286089234054 -320.363517060876 1
15 -54.3943569567055 -196.090879265219 1
16 -6.49376640468836 -236.715879265219 1
17 -55.1689632553607 -294.654527559876 1
18 -52.2891834398718 -267.782970660544 1
19 -107.444882997891 -145.041937217562 1
20 -54.3460145950416 -104.893846719365 1
21 115.140858632418 -161.905481864818 1
22 39.0666694480005 -247.437771602131 1
23 -17.8949871224344 -153.807354732013 1
24 -27.2429527433113 -231.945507544911 1
25 -114.82455651433 -247.221997249443 1
26 -106.092515129247 -177.01334790192 1
27 -41.583484443559 -95.2440889953334 1
28 -79.8710748980068 -124.193362167427 1
29 50.7236689672863 -105.784940021883 1
30 -25.7441196602762 -132.65700988338 1
31 -124.783467530164 -206.451794704446 1
32 -84.8355334281679 -95.1784341535269 1
33 -67.1085447465242 -114.543604443396 1
34 -21.8195533913553 -143.232182307697 1
35 -104.748203134269 -208.794309189046 1
36 -69.5907740116048 -100.036140436446 1
37 -60.7272796707829 -109.71872558138 1
38 -19.8572702568949 -148.519768519855 1
39 -61.9683943033232 -102.464993577905 1
40 -57.5366471329122 -107.306286150372 1
41 -58.1572044491824 -103.679420148635 1
42 -55.9413308639769 -106.100066434868 1
43 -56.251609522112 -104.286633434 1
44 -56.5564316949416 -113.616704298411 0
45 -46.72363488676 -107.322699860824 1
46 -47.9647495193003 -100.068967857349 1
47 -33.1201396781348 -156.449675442003 0
48 -34.4575583301568 -143.866631079586 0
49 -7.37896381400299 -128.411157310408 0
50 5.60943542662428 -157.171094614475 0
51 -59.2100489112201 -138.75233829353 0
52 -77.8465372895818 -254.179120568358 1
53 -104.865645498497 -287.992199794439 1
54 -76.5755895320795 -302.5332368018 1
55 -104.080075073614 -224.589565133993 1
56 -119.804012022247 -226.836895976945 1
57 -109.845101006414 -267.607098521941 1
58 -10.0458545845926 -174.957699580646 1
59 -72.1467785339139 -222.70196686534 0
49 1
0 0 21 1
1 1 2 1
2 2 22 1
3 3 18 1
4 19 5 1
5 20 45 1
6 23 58 1
7 52 4 1
8 24 18 1
9 18 54 1
10 8 53 1
11 19 26 1
12 9 19 1
13 32 5 1
14 46 20 1
15 19 28 1
16 10 29 1
17 21 1 1
18 22 3 1
19 23 38 1
20 24 7 1
21 25 56 1
22 26 35 1
23 27 10 1
24 28 33 1
25 29 0 1
26 30 6 1
27 31 9 1
28 32 36 1
29 33 37 1
30 34 30 1
31 35 55 1
32 36 39 1
33 37 40 1
34 38 34 1
35 39 41 1
36 40 42 1
37 41 43 1
38 42 20 1
39 43 20 1
40 45 6 1
41 46 27 1
42 52 18 1
43 53 57 1
44 54 8 1
45 55 4 1
46 56 31 1
47 57 25 1
48 58 7 1
0

'''

Mesh.ele

''' 7 31 35 26 8 15 7 58 9 23 38 47 10 47 58 23 11 11 45 20 12 51 19 26 13 20 45 46 14 13 1 21 15 56 4 55 16 23 58 50 17 7 16 22 18 22 2 7 19 19 28 5 20 16 24 18 21 7 15 24 22 49 50 29 23 6 49 10 24 1 13 2 25 13 21 0 26 30 49 6 27 29 50 13 28 7 2 50 29 51 15 47 30 9 26 19 31 55 59 35 32 35 15 26 33 24 15 59 34 6 10 27 35 32 5 28 36 51 48 44 37 13 0 29 38 44 6 45 39 30 34 49 40 26 9 31 41 33 36 28 42 32 28 36 43 37 33 44 44 48 47 38 45 49 34 12 46 35 59 15 47 56 35 31 48 33 37 36 49 39 36 37 50 40 37 44 51 38 34 48 52 12 34 38 53 40 41 37 54 39 37 41 55 11 42 40 56 42 43 40 57 41 40 43 58 11 20 42 59 42 20 43 60 51 33 28 61 6 44 30 62 44 45 11 63 6 27 45 64 45 27 46 65 48 34 30 66 47 48 51 67 30 44 48 68 44 33 51 69 12 23 50 70 10 49 29 71 50 2 13 72 49 12 50 73 19 51 28 74 15 51 26 75 52 54 18 76 59 18 24 77 52 57 53 78 53 8 54 79 4 56 25 80 52 59 4 81 35 56 55 82 15 58 47 83 4 57 52 84 7 50 58 85 4 59 55 86 59 52 18

'''

wo80 commented 9 months ago

This is how the input polygon and mesh renders. Notice that the polygon constraints don't look the same as in your graphics, so there's either something wrong with your polygon setup or your rendering of the polygon:

issue40-1

mustbau commented 9 months ago

Hi. thank you very much for the render result on your side. i will check the polygon once again, and give feedback, Cheers,

mustbau commented 9 months ago

Hi, i ran an ordering algorithm on the lines which form the polygon, and then input it to the Triangle, and.. Voila!! i get a quality mesh constrained in boundaries. I was also able to add inner points to the mesh. Thank you very much for your help. Iam closing this issue now

Here an image of the final mesh:

image