LibreCAD / LibreCAD

LibreCAD is a cross-platform 2D CAD program written in C++17. It can read DXF/DWG files and can write DXF/PDF/SVG files. It supports point/line/circle/ellipse/parabola/spline primitives. The user interface is highly customizable, and has dozens of translations.
http://librecad.org/
Other
4.38k stars 1k forks source link

App stops responding after zooming in/out to a specific zone #1610

Closed mich-j closed 1 year ago

mich-j commented 1 year ago

Expected behavior

LibreCAD allows to zoom in and out to every part of a drawing.

Observed behavior

LibreCAD stops responding, when I zoom in and zoom out a few times to a specific zone in my drawing. File that I'm working on is a section cut from an assembly drawing, created in FreeCAD TechDraw Workbench and exported to .dxf. I deleted by "trial and error" lines which did not cause any freezing, which left me with a something like minimal working example, which is included in this issue ticket (drawing.zip). Unfortunately, I am not able to pinpoint which lines exactly caused this problem.

I had an opportunity to import this file to GStarCAD on someone else's PC and it indeed prompted that some of the lines are broken. It said that it repaired them, but did not inform which ones were faulty. I imported this 'repaired' file to LibreCAD again, but it unfortunately did not help in the freezing issue.

Linking similar issues: #1000, #891

Steps to reproduce or sample file

drawing.zip

Operating System and LibreCAD version info

Both crashed in a similar manner.

dxli commented 1 year ago

This is a problem of splines. I will be working on this one.

dxli commented 1 year ago

Some background on the math part: Quadratic Spline Curve Length

We are using a dimensionless quadratic curve length function $$\int 2\sqrt{1+v^2}dv=v\sqrt{1+v^2}+ \ln(v+\sqrt{1+v^2})$$

The issue is due to negative v, when $v<-10^{10}$, $v$ and $\sqrt{1+v^2}$ are close in absolute values but of different signs, and direct adding $v + \sqrt{1+v^2}$ gets unstable numerically with loss significant digits due to floating point rounding up.

The fix, as usual, to avoid summation of floating points of different signs, for negative $v$, $$v+\sqrt{1+v^2} = \frac{1}{\sqrt{1+v^2}-v}$$ Therefore, for $v \ge 0$, $$\ln({v+\sqrt{1+v^2} })= \ln({v+\sqrt{1+v^2} }) $$ for $v<0$, $$\ln({v+\sqrt{1+v^2} }) = - \ln({\sqrt{1+v^2} - v}) $$

mich-j commented 1 year ago

With your commits, on LibreCAD:

Version: 2.2.1_alpha-9-g8c2e3ce6
Compiler: GNU GCC 11.3.0
Compiled on: May 21 2023
Qt Version: 5.15.3
Boost Version: 1.74.0
System: Ubuntu 22.04.2 LTS

I observed no freezing on the sample file. Also I tested BancadaCozinha-Vistas.dxf.gz file from #1000 and it also turned out OK.

Unfortunately on file Transistors.dxf.zip from #891 freezing was still observed.

So @dxli, I think that this issue and #1000 can be closed. I leave this decision for you. If you need more testing, I can help out as much as I can.

dxli commented 1 year ago

891 is a separate issue specific for polyline bulge handling.

1000 is a duplicate of this one due to SplinePoints length numerical stability.

Successfully tested.