Centerline between lines not working

Discussions about the development of the TechDraw workbench
edi
Posts: 294
Joined: Fri Jan 17, 2020 1:32 pm

Re: Centerline between lines not working

Postby edi » Sun May 02, 2021 4:26 pm

The problem has been discussed in https://forum.freecadweb.org/viewtopic. ... 9&start=30.
The programming problem - flip ends automatic - is solved in TechDrawTools.FCMacro.
I am no C++ programmer, but if there is anyone who has the knowledge to adopt TechDraw in C++ please contact me. I will explain how the algorithm works.
The whole code in Python is:

Code: Select all

    def getPoints(View,ObjectList):
        '''sort startpoints and endpoints of two lines belonging together'''
        def circulation(A,B,C):
            '''returns True if circulation ABC is positive'''
            M = app.Matrix(A.x, A.y, 1, 0,
                           B.x, B.y, 1, 0,
          	 	   C.x, C.y, 1, 0,
                      	   0, 0, 0, 1)
            if M.determinant() > 0.0: return True
        
        if len(ObjectList) > 1:
            Edge1 = View.getEdgeBySelection(ObjectList[0])
            Edge2 = View.getEdgeBySelection(ObjectList[1])
            if Edge1.Curve.TypeId == 'Part::GeomLine' and Edge2.Curve.TypeId == 'Part::GeomLine':
                Start1, Start2 = Edge1.Vertexes[0].Point, Edge2.Vertexes[0].Point
                End1, End2 = Edge1.Vertexes[1].Point, Edge2.Vertexes[1].Point
                if circulation(Start1,End1,Start2) != circulation(End1,End2,Start2):
                    Help1, Help2 = Start1, End1
                    Start1, End1 = Help2, Help1
            return (Start1,End1,Start2,End2)
User avatar
uwestoehr
Posts: 2734
Joined: Sun Jan 27, 2019 3:21 am
Location: Germany

Re: Centerline between lines not working

Postby uwestoehr » Tue May 11, 2021 11:48 pm

I am working to fix some of the centerline bugs and I even found a crash bug:

- open this example file and select there the orange centerline:
pi_terminal_baseplate.FCStd
(28.43 KiB) Downloaded 32 times
- open the centerline dialog and change now subsequently the orientation

result: crash:
ig8UdAHatt.gif
ig8UdAHatt.gif (136.17 KiB) Viewed 1704 times
chrisb
Posts: 35270
Joined: Tue Mar 17, 2015 9:14 am

Re: Centerline between lines not working

Postby chrisb » Wed May 12, 2021 8:48 am

Confirmed. PLease add your FreeCAD infos too.

OS: macOS 10.16
Word size of FreeCAD: 64-bit
Version: 0.20.24825 (Git)
Build type: Release
Branch: master
Hash: ccc4151b3020969450325466e385850783795325
Python version: 3.9.2
Qt version: 5.12.9
Coin version: 4.0.0
OCC version: 7.5.1
Locale: C/Default (C)
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
User avatar
uwestoehr
Posts: 2734
Joined: Sun Jan 27, 2019 3:21 am
Location: Germany

Re: Centerline between lines not working

Postby uwestoehr » Wed May 12, 2021 5:32 pm

chrisb wrote: Wed May 12, 2021 8:48 am Confirmed. PLease add your FreeCAD infos too.
As I wrote I am already working to fix bugs and also the crash. Meanwhile I found even more crashes. It turns out that the implementation of centerlines has a design bug that inexisting storage will be accessed under some circumstances.
User avatar
uwestoehr
Posts: 2734
Joined: Sun Jan 27, 2019 3:21 am
Location: Germany

Re: Centerline between lines not working

Postby uwestoehr » Fri May 14, 2021 4:55 am

edi wrote: Sun May 02, 2021 4:26 pm I am no C++ programmer, but if there is anyone who has the knowledge to adopt TechDraw in C++ please contact me.
Here I am.

I spent now some time trying to fix the centerline bugs and it turned out that the calculation itself is erroneous. Take for example the file from this post: https://forum.freecadweb.org/viewtopic. ... 20#p503220
- then select the centerline and open the task dialog to edit it
- in the dialog just turn on then off the Flip checkbox a few times

-> crash

I find the whole flip feature senseless since it is not necessary. The centerline should be calculated using the line vectors and not the middle point of lines connecting the endpoints of the lines.
However, before I change this, I would like to hear what your solution is. Is it to determine the optimal flip state or do you calculate the centerline?
User avatar
uwestoehr
Posts: 2734
Joined: Sun Jan 27, 2019 3:21 am
Location: Germany

Re: Centerline between lines not working

Postby uwestoehr » Fri May 14, 2021 9:58 pm

Here is the PR that fixes most of the known issues: https://github.com/FreeCAD/FreeCAD/pull/4793/

- fixes all known crashes
- adds preview when creating new centerlines
- allows to Cancel centerline creation or editing
- getting rid of the erroneous and misleading flip sides option by using @edi's sorting algorithm

Todo for a further PR is the record the editing of a centerline for Undo.

@edi, can you please explain me what your algorithm exactly does? It works nicely but I haven't yet understood why the sign of the determinant is the correct way to decide if one has to sort or not.
chrisb
Posts: 35270
Joined: Tue Mar 17, 2015 9:14 am

Re: Centerline between lines not working

Postby chrisb » Sat May 15, 2021 5:14 am

uwestoehr wrote: Fri May 14, 2021 9:58 pm Here is the PR that fixes most of the known issues
Thanks!
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
User avatar
uwestoehr
Posts: 2734
Joined: Sun Jan 27, 2019 3:21 am
Location: Germany

Re: Centerline between lines not working

Postby uwestoehr » Tue May 18, 2021 7:11 pm

uwestoehr wrote: Fri May 14, 2021 9:58 pm Here is the PR that fixes most of the known issues: https://github.com/FreeCAD/FreeCAD/pull/4793/
It had been merged today. Please test the next FC 0.20 build and report back what further centerline issues you encounter or in case in introduced a regression.
edi
Posts: 294
Joined: Fri Jan 17, 2020 1:32 pm

Re: Centerline between lines not working

Postby edi » Thu May 20, 2021 5:07 pm

uwestoehr wrote: Fri May 14, 2021 9:58 pm @edi, can you please explain me what your algorithm exactly does? It works nicely but I haven't yet understood why the sign of the determinant is the correct way to decide if one has to sort or not.
file: ...Mod/TechDraw/App/Cosmetic.cpp

function: calcEndPoints2Lines

Code: Select all

....

Base::Vector3d l1p1 = edges.front()->getStartPoint();
Base::Vector3d l1p2 = edges.front()->getEndPoint()
Base::Vector3d l2p1 = edges.back()->getStartPoint();
Base::Vector3d l2p2 = edges.back()->getEndPoint();

// replace the lines:

    if (flip) {             //reverse line 2
        Base::Vector3d temp;
        temp = l2p1;
        l2p1 = l2p2;
        l2p2 = temp;
    }
    
// with the lines
    
    float Circ1, Circ2;
    Circ1 = l1p1.x*l1p2.y+l1p1.y*l2p2.x+l1p2.x*l2p2.y-l2p2.x*l1p2.y-l2p2.y*l1p1.x-l1p2.x*l1p1.y;
    Circ2 = l1p2.x*l2p2.y+l1p2.y*l2p1.x+l2p2.x*l2p1.y-l2p1.x*l2p2.y-l2p1.y*l1p2.x-l2p2.x*l1p2.y;
    if signbit(Circ1) != signbit(Circ2){ //reverse line 2
    Base::Vector3d temp;
    temp = l2p1;
    l2p1 = l2p2;
    l2p2 = temp;
    }

The algorithm checks automatic whether line 2 has to be reversed or not.

How it works: The two triangles l1p1-l1p2-l2p1 and l1p2-l2p2-l2p1 are analysed. If they have different circulations one line has to be reversed.

Math: The determinant of the matrix returns twice the area of the triangle. The value is positive if the circulation is math. positive, otherwise negative.

The boolean variable flip is not needed any more. The algorithm is used and tested in TechDrawTools.FCMacro

I am sure my C++ syntax is not OK, please correct it.

If you have questions, please ask.
domad
Posts: 440
Joined: Mon Jun 22, 2020 12:16 pm

Re: Centerline between lines not working

Postby domad » Fri May 21, 2021 6:56 am

Hi edi
Excellent found for conditional control based on the result of two 1st degree equations.
The result "+" or "-" determines the condition. There is nothing more to say: simple and brilliant! ;)