BrunoLevy / geogram

a programming library with geometric algorithms
Other
1.8k stars 122 forks source link

Missing features and bugs in compute_CSG #113

Open BrunoLevy opened 10 months ago

BrunoLevy commented 10 months ago

compute_CSG does not work on all file inputs

file exact_nt expansion_nt
example001.csg OK OK
example002.csg OK OK
example003.csg OK OK
example004.csg OK OK
example005.csg OK OK
example006.csg OK OK
example007.csg OK OK
example008.csg OK OK
example009.csg OK OK
example010.csg OK OK
example011.csg OK OK
example012.csg OK OK
example013.csg OK OK
example014.csg OK OK
example015.csg OK OK
example016.csg OK OK
example017.csg OK OK
example018.csg OK OK
example019.csg OK OK
example020.csg the "snake" around the scene lacks resolution idem
example021.csg OK unstable (sometimes OK, sometimes radial sort errors)
example022.csg OK radial sort error
example023.csg OK OK
example024.csg OK radial sort error
BrunoLevy commented 10 months ago

done Still unclear to me the difference between union() and group() in a CSG file (so I'm doing a union in each group, but it may cost something ...). Anyway, need to add meshwide bbox test to avoid doing too many intersection tests (example006.csg now takes twice the time, for nothing...)

BrunoLevy commented 10 months ago

done OK, back to the initial timing by keeping bboxes with objects, and using them to detect that there is no need of computing intersections if they do not overlap.

BrunoLevy commented 10 months ago

done Implemented modifiers (!,#,%,*). '#' required changing the configuration of stb_c_lexer (by default, it removes preprocessor directives !)

BrunoLevy commented 10 months ago

done Something crazy happens with this file (pirate ship), especially with the coplanar triangles in the sails. Need to investigate. And also, OpenSCAD is faster on this one !

Problem was in computing approximate points / snap rounding: since points are in homogeneous coordinates, the same point can be represented in different ways and snapped to different floating-point numbers !!

BrunoLevy commented 10 months ago

done but to be understood Sometimes radial sort reports coplanar facets, which should not occur since radial sort is done after intersection. It happens in brio_splitter_round.stl from thingi10K, and also in pirateship.1.csg during compute_CSG.

Let us take a closer look at brio_splitter_round.stl

We are here: one of the previous phases of the algorithm creates a facet with three perfectly aligned vertices. So for the next step, we just need to check for such degenerate facets at different checkpoints in the algorithm.

BrunoLevy commented 9 months ago

done but to be understood sotc.csg gives a different result in geogram and in OpenSCAD ( I get two spurious floating rings of cubes above and below the cylinder).

Did not see that problem again.

BrunoLevy commented 9 months ago

TODO infinite loop in connected component classification by ray-tracing

BrunoLevy commented 9 months ago

WIPs:

BrunoLevy commented 9 months ago

dragon7.csg creates a mesh with 0 facet in it, it is weird, it is a union (of three small meshes, probably cubes or something like that). It is in fact cubes with size 0, it is used as an input to hull right after (ooohhh that's not elegant).

BrunoLevy commented 9 months ago

fractal_sierpinsky_spiral_4.csg has a 2D classification problem right at the end (assertion fail v1 != -1), we are not completely done with CoplanarFacets it seems !!

BrunoLevy commented 9 months ago

2D boolean operations start to fly, but we have some problems:

BrunoLevy commented 9 months ago

done (but todo: double check interval_nt for FPEs) seems that I get sometimes an infinite loop in CDT2d with;

So I extracted from Idler_gear.csg a minimalistic example with a small number of polygons: weird_wing_2d_simple.csg It seems that it generates veeerry small numbers. It triggers FPE in interval arithmetics. I deactivated the arithmetic filters in orient2d() and incircle2d(): still takes forever, and still triggers an FPE, much later, but this time in exact_nt::estimate() this one is easy to understand and to fix: (PCK::approximate(vec2HEx) -> ldexp() with too small exponent -> N.exp() -= D.exp(); D.exp() = 0.0).

With the filters deactivated, in debug mode, obtained the correct result (but took a super long amount of time). I guess that we are manipulating super long numbers under the hood.

Now works with weird_wing_2d_simple.csg with the filters activated. It is weird, because the only other change I've made is in PCK::approximate(), and the computation occurs before. But well, I could get a result before, and it was not looking correct, so it probably explains (result was already correct, but it is PCK::approximate() that was underflowing).

Also works with Idler_gear.csg, takes a HUGE amount of time (1256 seconds for just a tiny gear !!). I suspect mix() to be the culprit, with its D-N factor. Let's try to balance exponents between D and N -> nope (anyway we do things with N-D and things with D...)

Still need to understand where it is spending all the time ! (track very long numbers in exact_nt). sysprof says that exact_nt product is eating up all the time !

OK, Numeric::optimize_number_representation(Vec2HEx) was not implemented (stupid me).

And it is also better to take the original extremities of the constraints rather than the edge vertices passed to create_intersection().

BrunoLevy commented 9 months ago

fixed Problems in evaluating number of segments, examples:

BrunoLevy commented 9 months ago

understood surface was not closed, tiny mismatch between some vertices on sharp creases FAT_KNOB.stl, simplify_coplanar_facets kills too many facets !!

BrunoLevy commented 9 months ago

TODO sphere discretization is not exactly the same as in OpenSCAD: OpenSCAD does not generate poles, it generates a small polygon around each pole (shifted by half a period as compared to geogram).

BrunoLevy commented 9 months ago

Fixed incircle2d with homogeneous coordinates with expansions and different w's seems to be broken (for instance, example005.csg, triangulation of the ceiling is obviously non-Delaunay). It may come from:

Investigating:

Stats with simple example extracted from example005.csg:

Predicate stats for: incircle_2d_SOS_with_lengths(vec2HE)         
                         invocations :         7170                                     
                          filter hit :         6364 (88.76 %)                           
                               exact :          806 (11.24 %)                           
                                 SOS :          271 (3.78 %)  
BrunoLevy commented 9 months ago

Bug Sometimes connected component classification by raytracing enters an infinite loop (example022.csg). Behavior also reported by Cedric. Happens with both expansion_nt and exact_nt

BrunoLevy commented 9 months ago

Bug With expansion_nt, behavior is not reproducible, sometimes I get radial sort errors, sometimes not, on the same input files, depending also on whether I activate multithread or not.

BrunoLevy commented 9 months ago

Fixed simplify_coplanar_facets() stops at chart boundary, we could do that through charts. To do that, we need to re-connect the facets incident to manifold edges right after classification. ... but I'm calling mesh.facets.connect() and the end of classify(), so there is something weird here ! (maybe orientation is not correct). To figure out, save the result right at the end of classify...

Ah, OK, it is not classify(), it is remove_internal_facets() (and mesh_.facets.connect() was missing in there)

BrunoLevy commented 9 months ago

Bug 2D classification error after CDT in simplify_coplanar_facets() with:

Investigation strategy:

Rewrote more elaborate data structure with 2D CIEL Found something:

Understood something Some meshes have pairs of triangles sharing the same three vertices, and then Mesh.facets.find_adjacent(f1,f2) is ambiguous -> replaced it with Mesh.facets.find_edge(f,v1,v2).

Understood something else Sometimes I got triangles that are coplanar, but that have opposite orientation, then they should not be inserted in the same facet group. Note: this should not happen !!, because it means the result of the intersection still has intersecting triangles, this needs more investigation.

I think I sometimes also get sets of triangles considered to be coplanar along some edges and not along some other edges...