Centerline between lines not working

Discussions about the development of the TechDraw workbench
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
User avatar
uwestoehr
Veteran
Posts: 4961
Joined: Sun Jan 27, 2019 3:21 am
Location: Germany
Contact:

Re: Centerline between lines not working

Post by uwestoehr »

edi wrote: Thu May 20, 2021 5:07 pm 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.

Well, as I wrote, I already integrated your solution: https://github.com/FreeCAD/FreeCAD/pull ... 57afaf308f

But I don't understand it. I have never heard about "ciculation of a triangle" and can also not find anything via Google:
https://www.google.com/search?q=circula ... CA0&uact=5

How did you derive your set of equations? Why is the determinant twice the triangle area?
User avatar
uwestoehr
Veteran
Posts: 4961
Joined: Sun Jan 27, 2019 3:21 am
Location: Germany
Contact:

Re: Centerline between lines not working

Post by uwestoehr »

I tried to understand your solution but I am still stuck. Please help me to understand how you derived it.

Another question. Take this example:
l1p1.x = 1, .y = 3
l1p2.x = 1, .y = 1
l2p1.x = 0.5, .y = 3
l2p2.x = 3, .y = 3.5

The determinant (l1p1, l1p2, l2p1) is -1
The determinant (l1p1, l2p2, l2p1) is 0.25
So your algorithm is tho change line 1 in this case.

Then the centerline is between the points (0.75; 2) and (2; 3.25) without the change it would be between (0.75; 3) and (2; 2.25). So is the change correct in this case?
edi
Posts: 482
Joined: Fri Jan 17, 2020 1:32 pm

Re: Centerline between lines not working

Post by edi »

1) ciculation of a triangle:
TechDraw12.png
TechDraw12.png (9.87 KiB) Viewed 1731 times
- In the left triangle, if you run arround A-B-C the "circulation" is math. positive (counter clockwise)

- In the right triangle, if you run arround A-B-C the "circulation" is math. negative (clockwise)

I do not know (and could not find in dictionaries) the correct english word. Maybe a native speaker can tell me. In german I would say: Umlaufsinn.

2) How it works: (I hope I can explain it)
TechDraw10.png
TechDraw10.png (25.56 KiB) Viewed 1731 times
Left picture:
There are two lines (a and e) having both the direction from left to right (startponts l1p1 and l2p1, endpoints l1p2 and l2p2).
Using the lines b,c and d two triangles are created. The first one (a-b-c or l1p1-l1p2-l2p1) has positve "circulation". Also the second one (d-e-b or l1p2-l2p2-l2p1) has positve "circulation".
If you mirror the whole figure, both triangles become negative "circulation". Because both triangles always have the same "circulation" no swap has is necessary.

Right picture:
There are two lines (a and e) having different directions. Line 1 (bottom) from left to right, line2 (top) from right to left. We connect the same points (two have another position) creating the lines b,c and d. The first triangle (a-b-c or l1p1-l1p2-l2p1) has positve "circulation". The second triangle (d-e-b or l1p2-l2p2-l2p1) has negative "circulation". If you mirror the whole figure, both triangles change the sense of their circulation. Because the two triangles always have different "circulation" a swap is necessary.

3) Triangle area:

There are many ways to calculate the area of a triangle. The one I used is:
TechDraw13.png
TechDraw13.png (18.07 KiB) Viewed 1731 times
where 1,2,3 are the vertexes. If the "ciculation" of the triangle is math. negative, the formula returns a negative result.
(For mathematicians: changing the "circulation" means swaping two vertexes. In the matrix two lines are swaped, which switches the sign of the determinat)

In my last post I calculated the determinants manually, which leads to very short results, because the third column of the matrix is always 1.

In my Python example I used a 4x4 matrix because FreeCAD supports only 4x4 matrixes. There is only needed a 3x3 matrix. So if you update TechDraw in github (in future), you either can reduce the matrix to a 3x3 by deleting the 4th column and the 4th line, or use the the calculated determinats from the last post.


To your example:
uwestoehr wrote: Fri May 21, 2021 11:32 pm Another question. Take this example:
l1p1.x = 1, .y = 3
l1p2.x = 1, .y = 1
l2p1.x = 0.5, .y = 3
l2p2.x = 3, .y = 3.5
Where would you (as human being, not in a computer program) create the centerline ?

Seriously the algorithm works only in reasonable situations, e.g. if both lines are (nearly) parallel and have similar length. If the two lines form a X or a T (as in the example) the algorithm will produce nonsense. But the lines are selected by a (sensible) user.

If there are open questions, please ask.
User avatar
uwestoehr
Veteran
Posts: 4961
Joined: Sun Jan 27, 2019 3:21 am
Location: Germany
Contact:

Re: Centerline between lines not working

Post by uwestoehr »

edi wrote: Sat May 22, 2021 1:52 pm I do not know (and could not find in dictionaries) the correct english word. Maybe a native speaker can tell me. In german I would say: Umlaufsinn.
Thanks! Now I get it. The problem was when googling for "triangle circulation" you only find the Stokes law for a curl of a vector field along a triangle path.
I would say a better term is "direction of rotation" when looking at https://en.wikipedia.org/wiki/Clockwise ... -clockwise or maybe "direction of circulation".

2) How it works: (I hope I can explain it)
Thanks, I will reference this in the code as comment because as the code it, developers would not understand what the code does and why.

3) Triangle area:
There are many ways to calculate the area of a triangle. The one I used is:
OK, I got this now too, it is this method:
https://en.wikipedia.org/wiki/Triangle# ... oordinates

In my Python example I used a 4x4 matrix because FreeCAD supports only 4x4 matrixes. There is only needed a 3x3 matrix.
I am currently traveling, when I am back I will have a look to reduce the matrix to a 3x3 matrix.

To your example:
Where would you (as human being, not in a computer program) create the centerline ?
The first option, without the line flipping, that is why I asked if the flipping is correct.

Seriously the algorithm works only in reasonable situations, e.g. if both lines are (nearly) parallel and have similar length. If the two lines form a X or a T (as in the example) the algorithm will produce nonsense. But the lines are selected by a (sensible) user.
The point is that personally, I use the centerline feature also for rectangles. So for example I select two connecting edges of the rectangle to get the diagonal as "centerline". With your algorithm, I get now in 50% of the cases a diagonal intersecting both edges. I was looking for a way to calculate the centerline that works for all cases, but apparently there is no such algorithm.

However, thanks to your algorithm, we could now get rid of the swap option, that only lead to confusions.
Many thanks for your work!
edi
Posts: 482
Joined: Fri Jan 17, 2020 1:32 pm

Re: Centerline between lines not working

Post by edi »

uwestoehr wrote: Sat May 22, 2021 9:20 pm OK, I got this now too, it is this method:
https://en.wikipedia.org/wiki/Triangle# ... oordinates
Yes, you found the formula I used in wikipedia.
If you transpose a quadratic matrix (transpose is mirror along the main diagonal from top-left to bottom-right) the value of the determinant keeps unchanged. I used the transposed matrix.
In wikipedia the absolut value is taken to receive the area. Thats correct. If you do not take the absolut value, you get a signed value and the sign shows the "direction of rotation".
uwestoehr wrote: Sat May 22, 2021 9:20 pm The point is that personally, I use the centerline feature also for rectangles. So for example I select two connecting edges of the rectangle to get the diagonal as "centerline". With your algorithm, I get now in 50% of the cases a diagonal intersecting both edges. I was looking for a way to calculate the centerline that works for all cases, but apparently there is no such algorithm.
Thats correct. It happened also using the "old" algorithm using the swap option.
But, if wished, I can add a tool "create diagonals of a quadrangle" in TechDrawTools.FCMacro https://forum.freecadweb.org/viewtopic.php?f=35&t=55029
User avatar
uwestoehr
Veteran
Posts: 4961
Joined: Sun Jan 27, 2019 3:21 am
Location: Germany
Contact:

Re: Centerline between lines not working

Post by uwestoehr »

edi wrote: Sun May 23, 2021 7:38 am But, if wished, I can add a tool "create diagonals of a quadrangle" in TechDrawTools.FCMacro https://forum.freecadweb.org/viewtopic.php?f=35&t=55029
Such an option would be nice.

Meanwhile I made a PR to properly document the changes I have already made using your method: https://github.com/FreeCAD/FreeCAD/pull/4830
Post Reply