Signed Point-To-Line Distance Constraints

About the development of the Part Design module/workbench. PLEASE DO NOT POST HELP REQUESTS HERE!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
milobrandt
Posts: 2
Joined: Sat Dec 11, 2021 5:06 pm

Signed Point-To-Line Distance Constraints

Post by milobrandt »

I'm a fairly new user of FreeCAD, but am interested in implementing a change to make point-to-line (P2L) constraints preserve the orientation/sign of the distance. The fact that it doesn't has already caused me some annoyance (and an incorrect cut on a CNC router) and it looks like it's been discussed years ago and at least some times more recently, as well as having this behavior called out in some tutorials on the sketcher that I've read - along with the workaround of adding 180 degree angle constraints (but which feels cumbersome to have to use for a very common construction). It looks like it's a trivial change to the back-end of the sketcher, so I think it would be worthwhile unless more experienced people in this community know a good reason to preserve the existing behavior.

As far as I can tell, making the P2L constraint signed just requires remove the call to std::abs in double ConstraintP2LDistance::error() and removing the lines

Code: Select all

if (area < 0)
	deriv *= -1;
from double ConstraintP2LDistance::grad(double*) - at least, after making those changes and recompiling, the sketcher had the expected behavior that the sign of the constraint controlled which side of the line the point would be constrained on. This is basically all the technical feature - I think the main question for me is how to intuitively let the user take advantage of this.

I would think that a reasonable user interface for this would be
  • Add a property to P2L constraints signifying whether it is signed/oriented - modify error/grad calculations as above when this is true.
  • Upon creating (or adding signed behavior to) a P2L constraint, choose orientations/signs such that positive distances correspond to the side of the line that the point is already on. Allow the user some way in the interface to flip the orientations of a constraint if they prefer the opposite convention.
Some of the changes would also potentially apply to tangencies between lines and circles/arcs, which are internally represented in the same way. I believe that the P2L constraint is the only one which artificially has an absolute value added - and I believe that the horizontal/vertical distance already work similarly to this (but I haven't fully traced through how those work). Backwards compatibility should be reasonably easy to maintain by just disabling signed behavior for older files - but it would need to save new properties, so would probably break forwards compatibility.

Is this a reasonable change to make? Are there any things to consider further about it?
chrisb
Veteran
Posts: 54293
Joined: Tue Mar 17, 2015 9:14 am

Re: Signed Point-To-Line Distance Constraints

Post by chrisb »

This is very reasonable from my point of view, and I have made this proposal previously. You may search the old discussion, because Abdullah has made some of his always valuable comments there.

An important thing to consider is the backwards compatibility, so it may be sensible to add the directed lines as a new geometric element.
A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
chrisb
Veteran
Posts: 54293
Joined: Tue Mar 17, 2015 9:14 am

Re: Signed Point-To-Line Distance Constraints

Post by chrisb »

A Sketcher Lecture with in-depth information is available in English, auf Deutsch, en français, en español.
openBrain
Veteran
Posts: 9041
Joined: Fri Nov 09, 2018 5:38 pm
Contact:

Re: Signed Point-To-Line Distance Constraints

Post by openBrain »

I'd be interested in trying this.
IMO considering the risk if integrating such a feature breaks the code, it is important that this is proposed as an optional/alternative feature so that user can fallback to original one if needed.
milobrandt
Posts: 2
Joined: Sat Dec 11, 2021 5:06 pm

Re: Signed Point-To-Line Distance Constraints

Post by milobrandt »

I'll try adding a new tool. It looks like that's the preferred solution in the discussions I've now read (and it should be safer for issues of backwards compatibility/not confusing new users/not confusing old users). I spent an hour hacking at the code today and made a rudimentary working implementation of a new command and constraint, and it seems to work happily with the solver and be easy to use - maybe I'll try to implement it more properly next weekend.
Post Reply