meshpro / pygalmesh

:spider_web: A Python interface to CGAL's meshing tools
GNU General Public License v3.0
597 stars 57 forks source link

Issues with pygalmesh.generate_2d #113

Closed IgnacioEG closed 3 years ago

IgnacioEG commented 4 years ago

Hello,

I have been using pygalmesh to generate 2D meshes from a polygon defined by points, Also pygalmesh.Extrude to do the same but for extruding and generate a volumen.

When I try some simple polygons it is OK. However,when I try to do so with some more complex polygons (polygons that do not form a convex hull for example) I get some errors and the STL created can not be read by third party softwares.

Is there any restriction on the polygon shape? (For example it has to be convex, etc...)

Regards

nschloe commented 4 years ago

Perhaps, you'll have to ask the cgal folks. Without and MWE, I can't say in anything.

IgnacioEG commented 4 years ago

Hello Nico,

Thanks for your quick response. Please find below an example:

import numpy as np import pygalmesh import pyvista as pv

points=np.array([[-69.5297,-10.9517],[-68.7862,-11.0364],[-68.2713,-11.0145],[-68.0482,-10.7121],[-67.1738,-10.3068],[-66.6469,-9.93133],[-65.3384,-9.76199],[-65.4448,-10.5115],[-65.3219,-10.8959],[-65.4023,-11.5663],[-64.3164,-12.462],[-63.1965,-12.627],[-62.8031,-13.0007],[-62.1271,-13.1988],[-61.7132,-13.4892],[-61.0841,-13.4794],[-60.5033,-13.776],[-60.4592,-14.354],[-60.2643,-14.646],[-60.2511,-15.0772],[-60.543,-15.0939],[-60.1584,-16.2583],[-58.2412,-16.2996],[-58.3881,-16.8771],[-58.2808,-17.2717],[-57.7346,-17.5525],[-57.4984,-18.1742],[-57.676,-18.9618],[-57.95,-19.4],[-57.8538,-19.97],[-58.1664,-20.1767],[-58.1835,-19.8684],[-59.115,-19.3569],[-60.0436,-19.3427],[-61.7863,-19.6337],[-62.266,-20.5137],[-62.2912,-21.0516],[-62.6851,-22.249],[-62.8465,-22.035],[-63.9868,-21.9936],[-64.377,-22.7981],[-64.9649,-22.0759],[-66.2733,-21.8323],[-67.1067,-22.7359],[-67.8282,-22.8729],[-68.2199,-21.4943],[-68.7572,-20.3727],[-68.4422,-19.4051],[-68.9668,-18.9817],[-69.1002,-18.2601],[-69.5904,-17.58],[-68.9596,-16.5007],[-69.3898,-15.6601],[-69.1603,-15.324],[-69.3395,-14.9532],[-68.9489,-14.4536],[-68.9292,-13.6027],[-68.8801,-12.8997],[-68.6651,-12.5613]])

v1=np.arange(0,len(points[:,0]+1)) v2=np.arange(1,len(points[:,0]+1)) v2=np.append(v2,0) constraints=np.hstack((v1[:,np.newaxis],v2[:,np.newaxis])) cons=np.ndarray.tolist(constraints) mesh = pygalmesh.generate_2d(points, cons, cell_size=0.25, num_lloyd_steps=10) mesh.write('example_surface.vtk') mesh2 = pv.read('example_surface.vtk') cpos = mesh2.plot(show_edges=True, color=True)

p = pygalmesh.Polygon2D(points) edge_size = 0.1 domain = pygalmesh.Extrude(p, [0.0, 0.0, 1.0], 0, edge_size) mesh = pygalmesh.generate_mesh( domain, cell_size=0.25, edge_size=edge_size, verbose=False ) mesh.write('example_volume.stl')

mesh3 = pv.read('example_volume.stl')

cpos = mesh3.plot(show_edges=True, color=True)

nschloe commented 4 years ago

Please use code fences. Also, this example isn't minimal. Try to boil it down to much fewer points to still see the error.

IgnacioEG commented 4 years ago

Hello again,

This is the maximum that I reduce the code:

import numpy as np
import pygalmesh

points=np.array([[-69.5297,-10.9517],[-68.7862,-11.0364],[-68.2713,-11.0145],[-68.0482,-10.7121],[-67.1738,-10.3068],[-66.6469,-9.93133],[-65.3384,-9.76199],[-65.4448,-10.5115],[-65.3219,-10.8959],[-65.4023,-11.5663],[-64.3164,-12.462],[-63.1965,-12.627],[-62.8031,-13.0007],[-62.1271,-13.1988],[-61.7132,-13.4892],[-61.0841,-13.4794],[-60.5033,-13.776],[-60.4592,-14.354],[-60.2643,-14.646],[-60.2511,-15.0772],[-60.543,-15.0939],[-60.1584,-16.2583],[-58.2412,-16.2996],[-58.3881,-16.8771],[-58.2808,-17.2717],[-57.7346,-17.5525],[-57.4984,-18.1742],[-57.676,-18.9618],[-57.95,-19.4],[-57.8538,-19.97],[-58.1664,-20.1767],[-58.1835,-19.8684],[-59.115,-19.3569],[-60.0436,-19.3427],[-61.7863,-19.6337],[-62.266,-20.5137],[-62.2912,-21.0516],[-62.6851,-22.249],[-62.8465,-22.035],[-63.9868,-21.9936],[-64.377,-22.7981],[-64.9649,-22.0759],[-66.2733,-21.8323],[-67.1067,-22.7359],[-67.8282,-22.8729],[-68.2199,-21.4943],[-68.7572,-20.3727],[-68.4422,-19.4051],[-68.9668,-18.9817],[-69.1002,-18.2601],[-69.5904,-17.58],[-68.9596,-16.5007],[-69.3898,-15.6601],[-69.1603,-15.324],[-69.3395,-14.9532],[-68.9489,-14.4536],[-68.9292,-13.6027],[-68.8801,-12.8997],[-68.6651,-12.5613]])

p = pygalmesh.Polygon2D(points)
domain = pygalmesh.Extrude(p, [0.0, 0.0, 1.0], 0, edge_size = 0.1)
mesh = pygalmesh.generate_mesh(domain, cell_size=0.25, edge_size=0.1, verbose=False)
mesh.write('example_volume.stl')
nschloe commented 4 years ago

Use triple code fences for code blocks. Also, I tried running your code and got no error. screenshot

IgnacioEG commented 4 years ago

Hello,

I don't get an error message either. However, when I try to import the STL with other software (I have tried CST Microwave Studio and also CURA for 3D printing) they say that the STL is corrupt. It seems that the mesh has holes in it. message2 message3 message1

Please see figures.

nschloe commented 4 years ago

Can you produce an example where the holes are more obvious?

IgnacioEG commented 4 years ago

Yes, in the same example, if you change the cell_size to 2.0 you get this:

larger_holes

nschloe commented 4 years ago

Indeed. I also noticed that you can remove the last point and still get the hole. Please make the example minimal this way, perhaps the issue will jump at us then.

IgnacioEG commented 4 years ago

I made it minimal, I still cannot find any issue. Am I doing something wrong or it is a bug in the code?

minimal

import numpy as np
import pygalmesh
points=np.array([[-69.5297,-10.9517],[-62.8031,-13.0007],[-60.543,-15.0939],[-58.1664,-20.1767],[-66.2733,-21.8323],[-68.9489,-14.4536]])

p = pygalmesh.Polygon2D(points)
domain = pygalmesh.Extrude(p, [0.0, 0.0, 1.0], 0, edge_size = 0.5)
mesh = pygalmesh.generate_mesh(domain, cell_size=2.0, edge_size=0.5, verbose=False)
mesh.write('example_volume.stl')
nschloe commented 4 years ago

Again, I can remove the last point and still get the error. Please be more diligent when creating an MWE.

nschloe commented 4 years ago

Also, please try to reproduce the issue with "normal" values, something between 0 and 1 perhaps. I'm sure it'll get much easier.

IgnacioEG commented 4 years ago

Reducing to 4 points and normalizing the coordinates, the problem is still there. Have you experienced something similar before?

import numpy as np
import pygalmesh

points=np.array([[-69.5297,-10.9517],[-60.543,-15.0939],[-58.1664,-20.1767],[-66.2733,-21.8323]])
points[:,0]=points[:,0]-np.min(points[:,0])
points[:,0]=points[:,0]/np.max(points[:,0])
points[:,1]=points[:,1]-np.min(points[:,1])
points[:,1]=points[:,1]/np.max(points[:,1])

p = pygalmesh.Polygon2D(points)
domain = pygalmesh.Extrude(p, [0.0, 0.0, 0.1]*1, 0, edge_size = 0.02)
mesh = pygalmesh.generate_mesh(domain, cell_size=0.02, edge_size=0.02, verbose=False)
mesh.write('example_volume.stl')
nschloe commented 4 years ago

Now it's more complicated then before!

nschloe commented 4 years ago

Make the code simple and easy!

IgnacioEG commented 4 years ago

The example is simpler than before (as it has only 4 points). The code has a couple more of lines now in order to normalize the values (as you requested).

My comment was wether I am doing something wrong or the method has a bug. Best regards

nschloe commented 4 years ago

I didn't request normalization, I requested easier-to-read points. I'm sure it can all be reproduced with [0.0, 0.0], [1.0, 0.7], ... etc.

nschloe commented 4 years ago

I cannot tell if there's an issue without doing some work. If you want me to work for you, you can at least provide a minimal example that's easy to read. It's impolite to just drop your code and expect anyone to plow through it. That said, you're doing a better job than most, I'm just guiding you to create something super simple that everyone can understand at first sight.

IgnacioEG commented 4 years ago

I get your point. Actually I was not expecting from you to work in my code. I just wanted an answer such us: 'actually I found some similar problem in the past with that method' or 'I have tested that method several times and it works, you must do something wrong'. I tried some really simple examples and they do work, but whenever I use some representative example of my problem is when I got troubles, that is why I asked if there was some limitations such as convex envelopes and so.

Althought I do not get the same issue, just with the simple points below, the mesh does something weird in one corner

points=np.array([[0,0],[0.1,0.75],[0.35,0.35],[1.0,0.0]])
nschloe commented 4 years ago

the mesh does something weird in one corner

Absolutely. screenshot

I haven't seen anything like this before, so I'll forward this to the CGAL devs. The example now really does look minimal, thanks.

nschloe commented 3 years ago

This has been fixed in CGAL, cf. https://github.com/CGAL/cgal/pull/5251.

IgnacioEG commented 3 years ago

Thanks! What should I do to get it work? I have tried to update to the latest version of pygalmesh via conda or pip but I still get the same result

nschloe commented 3 years ago

Install CGAL from master or wait for their next release.