jimy-byerley / pymadcad

Simple yet powerful CAD (Computer Aided Design) library, written with Python.
https://madcad.netlify.app/
GNU Lesser General Public License v3.0
205 stars 15 forks source link

Issue with 'difference' Function - Incorrect Object Removal and Polygon Closure #87

Open Tommamgo opened 10 months ago

Tommamgo commented 10 months ago

Hello,

I hope you're doing well. I've come across an issue while testing the 'difference' function. During the process of subtracting an STL file from another, I observed that while the object's orientation was correctly subtracted, the polygons were not closed properly.

Code:

from madcad import *

#open
read_mesh1 = io.read("stl1.stl")
read_mesh2 = io.read("stl2.stl")
read_mesh1.mergeclose()
read_mesh2.mergeclose()

# Remove the volume of the second to the first
diff = difference(read_mesh2, read_mesh1)

# Write
io.write(diff, "mesh.stl")

Result:

grafik

This should be the Result:

grafik

stl1 and stl2:

stl.zip

jimy-byerley commented 10 months ago

I cannot reproduce the issue on my side with the master branch Which version of pymadcad do you use ?

Tommamgo commented 10 months ago

Thank you for the prompt response.

Unfortunately, I had already deleted the library. With the understanding that it could have been due to an incorrect version of the library, I tried again with the same code example and the library version.

This time, I didn't encounter any faulty output files.

However, this time I faced issues with different test .stl files:

Traceback (most recent call last): File "/home/linus/PycharmProjects/madCAD/main.py", line 30, in diff = difference(read_mesh2, read_mesh1) File "/home/linus/anaconda3/envs/madCAD/lib/python3.10/site-packages/madcad/boolean.py", line 638, in difference return boolean(a,b, (False,True)) File "/home/linus/anaconda3/envs/madCAD/lib/python3.10/site-packages/madcad/boolean.py", line 620, in boolean return op(a, b, sides, prec) File "/home/linus/anaconda3/envs/madCAD/lib/python3.10/site-packages/madcad/boolean.py", line 254, in boolean_mesh mc1 = pierce_mesh(m1, m2, sides[0], prec) File "/home/linus/anaconda3/envs/madCAD/lib/python3.10/site-packages/madcad/boolean.py", line 166, in pierce_mesh m1, frontier = cut_mesh(m1, m2, prec) File "/home/linus/anaconda3/envs/madCAD/lib/python3.10/site-packages/madcad/boolean.py", line 147, in cut_mesh flat = triangulation.triangulation_closest(segts, normal, prec) File "/home/linus/anaconda3/envs/madCAD/lib/python3.10/site-packages/madcad/triangulation.py", line 759, in triangulation_closest result += triangulation_outline(loop, z, prec) File "/home/linus/anaconda3/envs/madCAD/lib/python3.10/site-packages/madcad/triangulation.py", line 294, in triangulation_outline raise TriangulationError("no more feasible triangles (algorithm failure or bad input outline)", [outline.indices[i] for i in hole]) madcad.triangulation.TriangulationError: ('no more feasible triangles (algorithm failure or bad input outline)', [120, 127, 125, 92])

I used these two .stl files: files.zip

System: Date: Wed Aug 30 18:20:02 2023 CEST

            OS : Linux
        CPU(s) : 4
       Machine : x86_64
  Architecture : 64bit
   Environment : Python
   GPU Details : error

MathText Support : False

PIP: appdirs==1.4.4 arrex==0.5.2 blessed==1.19.1 distlib==0.3.7 flyingcircus==0.1.4.0 glcontext==2.4.0 Jinja2==3.1.2 MarkupSafe==2.1.3 moderngl==5.8.2 nose==1.3.7 numpy==1.25.2 numpy-stl==3.0.1 packaging==21.3 Pillow==10.0.0 plyfile==1.0.1 PyGLM==2.7.0 pymadcad==0.15.1 pynsist==2.8 PyQt5==5.15.9 PyQt5-Qt5==5.15.2 PyQt5-sip==12.12.2 pyserial==3.5 python-utils==3.7.0 pytk==0.0.2.1 PyWavefront==1.3.3 PyYAML==6.0.1 requests_download==0.1.2 scipy==1.11.2 setuptools-scm==6.4.2 tomli==2.0.1 typing_extensions==4.7.1 wcwidth==0.2.5 yarg==0.1.9


from madcad import *

#open
read_mesh1 = io.read("stl1.stl")
read_mesh2 = io.read("stl2.stl")
read_mesh1.mergeclose()
read_mesh2.mergeclose()

# Remove the volume of the second to the first
diff = difference(read_mesh2, read_mesh1)

# Write
io.write(diff, "mesh.stl")

Did I do something wrong in my setup?

Thank you.

jimy-byerley commented 10 months ago

This discussion should be a different issue then

I don't know exactly what is stl1.stl and stl2.stl in your example since your zip file only contains Basis.stl and Gemin.stl However that seems to be a floating point precision issue: I could avoid the problem with a slight shift of one of your meshes

difference(basis, gemein.transform(0.001*vec3(1)))
Tommamgo commented 10 months ago

Thank you for the quick response.

I apologize for the incorrect file naming.

stl1.stl = Basis.stl stl2.stl = Gemin.stl

With your changes, I was able to compile it and achieved the desired results.

Thanks!

jimy-byerley commented 10 months ago

Happy that it worked for you ! I will keep this issue opened since the floating point precision issue is a bug that needs to be solved Thanks for reporting