How to use arcs.py arcFrom2Pts

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
drmacro
Veteran
Posts: 8984
Joined: Sun Mar 02, 2014 4:35 pm

How to use arcs.py arcFrom2Pts

Post by drmacro »

In src/Mod/Draft/draftgeoutils/arcs.py > arcFrom2Pts() it is preparing to normalize and executes this:

Code: Select all

thirdPt = App.Vector(firstPt.sub(center).add(lastPt).sub(center))
I'm guessing .sub(vector) subtracts the argument (a vector) from the vector.

I don't understand why in with one set of vectors thirdPt is 0,0,0 and fails with a null vector error when .normalize id executed.

But, with another set of values it succeeds.

The values are confirmed to be different (i.e. firstPt and LastPt) and the center is the calculated value of the midpoint of a line between firstPt & lastPt returned by findMidpoint()

I assume it's me not doing something right, but don't get why it works on one set of points and not on another.
:oops:
Last edited by drmacro on Sun May 02, 2021 1:15 pm, edited 1 time in total.
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
edwilliams16
Veteran
Posts: 3180
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: [possible bug] in arcs.py arcFrom2Pts

Post by edwilliams16 »

I would expect the function to always return null.

If first_point = x, last_point= y, then center = (x + y)/2

in which case

App.Vector(firstPt.sub(center).add(lastPt).sub(center)) = x - (x + y)/2 + y - (x+y)/2 which is always zero.


EDIT Looking at the code, center is the center of the arc, not the mid-point of first and last! I
Last edited by edwilliams16 on Sat May 01, 2021 9:46 pm, edited 1 time in total.
drmacro
Veteran
Posts: 8984
Joined: Sun Mar 02, 2014 4:35 pm

Re: [possible bug] in arcs.py arcFrom2Pts

Post by drmacro »

I agree with the logic you describe, yet I have 2 sets of data with the center calculated the same way, one works, one doesn't. :?

I guess I'll need to look at it some more in debug to see if I'm fooling myself in some way. :roll:
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
edwilliams16
Veteran
Posts: 3180
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: [possible bug] in arcs.py arcFrom2Pts

Post by edwilliams16 »

See EDIT above.

I think the function will fail when the center is the mid-point. In that case the plane of the circle is unknown.


EDIT: It should fail when the center is the mid-point, because the answer to the problem is undefined. (unknown plane for the semi-circle). With exact arithmetic, it will fail on .normalize() With finite precision, it will produce a semi circle on the error side of the line. The function should have returned an exception when asked to perform the impossible, within numerical error. These edge cases are always the most problematic when you can't rely on the caller to provide legal arguments.
drmacro
Veteran
Posts: 8984
Joined: Sun Mar 02, 2014 4:35 pm

Re: [possible bug] in arcs.py arcFrom2Pts

Post by drmacro »

soooo... center is the midpoint of the circumference of the arc, not the centerpoint of the circle that makes the arc?
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
edwilliams16
Veteran
Posts: 3180
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: [possible bug] in arcs.py arcFrom2Pts

Post by edwilliams16 »

drmacro wrote: Sat May 01, 2021 11:10 pm soooo... center is the midpoint of the circumference of the arc, not the centerpoint of the circle that makes the arc?
In the arcFrom2Pts function center is the center of the circle defining the arc. firstPt and lastPt are the ends of the arc and must be equidistant from center. If you choose center to be the mid-point of the chord between firstPt and lastPt, the function should fail because the answer is undefined. It may not, because of finite precision.

The function works by constructing thirdPoint, the mid-point along the arc, then calling

Code: Select all

Part.Arc(firstPt, thirdPt, lastPt)
which constructs the arc, given three points on it, in order.
edwilliams16
Veteran
Posts: 3180
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: [possible bug] in arcs.py arcFrom2Pts

Post by edwilliams16 »

I see you have another thread on the same subject...

arcFrom2pts is designed work in 3D. If you are working, say, in the sketcher, you are confined to the z = 0 plane, and then the case of a semi-circular arc is no longer undefined, as the plane is specified and you should use a dedicated routine, that will handle this case.
drmacro
Veteran
Posts: 8984
Joined: Sun Mar 02, 2014 4:35 pm

Re: [possible bug] in arcs.py arcFrom2Pts

Post by drmacro »

edwilliams16 wrote: Sun May 02, 2021 12:13 am I see you have another thread on the same subject...

arcFrom2pts is designed work in 3D. If you are working, say, in the sketcher, you are confined to the z = 0 plane, and then the case of a semi-circular arc is no longer undefined, as the plane is specified and you should use a dedicated routine, that will handle this case.
In this case I am in Sketcher and thus z is by definition, i guess, =0
But the vectors are setting z=0 somshould it matter?

I'm sure there are dedicated routines, but, my frustration is finding them.

And when I do, as in this case, the documentation is sparse. I suppose, it was obvious to someone that an argument called center was not the center point of the arc...maybe I'm getting too old for this. ;)

I'm been beating on creating this particular arc for so long, I now forget why I thought I need to draw a centerpoint arc.
:mrgreen:
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
edwilliams16
Veteran
Posts: 3180
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: [possible bug] in arcs.py arcFrom2Pts

Post by edwilliams16 »

drmacro wrote: Sun May 02, 2021 12:55 am
And when I do, as in this case, the documentation is sparse. I suppose, it was obvious to someone that an argument called center was not the center point of the arc...maybe I'm getting too old for this. ;)
I'm not sure you are reading what I said carefully.
  • The center argument of the arcFrom2Pts function is the center of the circle of which the arc is part.
  • This function, because it is 3D, will randomly fail in one case - when the required arc is a semicircle. Equivalently, it fails when the center is at the mid-point of the chord between the arc endpoints.
I can easily write a function that will create an arc given two endpoints and the arc center, but it needs additional specification.
  • Which arc is desired? The short/long one? Counter/Clockwise from point 1 to point 2?
  • To handle the case where we have a semi-circle, we need (1) the plane in which the semi-circle is to be created (XY, if you require z=0 in the inputs, or more generally we need the normal to the circle plane) (2) Which of the two semi-circular arcs do you want in this case?
For me, it is simpler to write the required function from scratch than to find and debug existing code.
https://wiki.freecadweb.org/Sandbox:Edwilliams16 is about Vector algebra in FreeCAD.
drmacro
Veteran
Posts: 8984
Joined: Sun Mar 02, 2014 4:35 pm

Re: [possible bug] in arcs.py arcFrom2Pts

Post by drmacro »

edwilliams16 wrote: Sun May 02, 2021 1:27 am ...
I'm not sure you are reading what I said carefully.
More like reading the words and misinterpreting based on my preconceived notions. Thus, my problem not yours. :oops:
And I appreciate your patience.
  • The center argument of the arcFrom2Pts function is the center of the circle of which the arc is part.
  • This function, because it is 3D, will randomly fail in one case - when the required arc is a semicircle. Equivalently, it fails when the center is at the mid-point of the chord between the arc endpoints.
I see that now and other things in this routine continue to become clear. It seems in my glee of finding a routine that I perceived, by the name, to be the thing I needed, my use case happens to be a failure mode. :roll:
I can easily write a function that will create an arc given two endpoints and the arc center, but it needs additional specification.
  • Which arc is desired? The short/long one? Counter/Clockwise from point 1 to point 2?
  • To handle the case where we have a semi-circle, we need (1) the plane in which the semi-circle is to be created (XY, if you require z=0 in the inputs, or more generally we need the normal to the circle plane) (2) Which of the two semi-circular arcs do you want in this case?
For me, it is simpler to write the required function from scratch than to find and debug existing code.
I, maybe naively, expect that most of the basic geometry creation helpers would be readily available in a 20 year old piece of CAD code. I'm loath to recreate a wheel that exists.

As for the direction of the arc, is that not what the axis argument addresses? I.e. (0,0,1) is CCW and (0,0,-1) being CW
Again, my ignorance... :oops:

This all started because, figuring it would be a good learning exercise..., I wanted to create a generic script that would allow the user to enter various parameters and it would produce the following two arcs:
AddArcs2Sausage.gif
AddArcs2Sausage.gif (514.03 KiB) Viewed 1198 times
Of course I need to make the ends tangent and other constraints so the user ends up with something that can be dragged around if desired.
https://wiki.freecadweb.org/Sandbox:Edwilliams16 is about Vector algebra in FreeCAD.
I have this bookmarked and will be studying it. Thank you.
Star Trek II: The Wrath of Khan: Spock: "...His pattern indicates two-dimensional thinking."
Post Reply