[Bug #4062] Incorrect behavior with Euler angles in Placement dialog

Post here for help on using FreeCAD's graphical user interface (GUI).
Forum rules
and Helpful information
IMPORTANT: Please click here and read this first, before asking for help

Also, be nice to others! Read the FreeCAD code of conduct!
openBrain
Posts: 2372
Joined: Fri Nov 09, 2018 5:38 pm

[Bug #4062] Incorrect behavior with Euler angles in Placement dialog

Postby openBrain » Wed Jul 17, 2019 6:10 pm

Hi all,
I want to report here a bug that has been discussed in the Italian subforum in this thread but I hope to find a highest audience here to confirm and identify.

Steps to reproduce :
  1. Create a blank new model and select Part WB
  2. Add a torus then a cone (default settings)
  3. Select the torus in the tree view and open its Placement dialog
  4. In the Rotation group, choose "Euler angles" type then set Pitch=90° and Roll=90° and validate with OK
  5. Immediately reopen the Placement dialog => :? Roll angle is back to 0° while no change has been done (and no change is displayed)
@carsco published a video about walkthrough => https://streamable.com/iaacj

Notice that I could reproduce this bug with 0.18.2 version on Windows but can't reproduce with both 0.18 & 0.19 on Kubuntu... Other users reproduced with both 0.18 & 0.19 but only on Windows (cf. original thread).

Thanks for confirming (do not forget to add your FC info as it seems to play a role)
carsco wrote:
Mon Jul 15, 2019 1:27 pm
Ping
In case you're interested in following this thread
Last edited by openBrain on Thu Jul 18, 2019 12:38 pm, edited 1 time in total.
kisolre
Posts: 1360
Joined: Wed Nov 21, 2018 1:13 pm

Re: [Bug ?] Incorrect behavior with Euler angles in Placement dialog

Postby kisolre » Wed Jul 17, 2019 6:26 pm

Confirmed here with

OS: Windows 8.1
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.17300 (Git)
Build type: Release
Branch: master
Hash: 07d3423adda83e9bd013a52a1b1bd027ae9fa454
Python version: 3.6.6
Qt version: 5.6.2
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: Bulgarian/Bulgaria (bg_BG)
TheMarkster
Posts: 1061
Joined: Thu Apr 05, 2018 1:53 am

Re: [Bug ?] Incorrect behavior with Euler angles in Placement dialog

Postby TheMarkster » Wed Jul 17, 2019 6:49 pm

Works as expected here:

OS: Ubuntu 18.04.2 LTS (LXDE/LXDE)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.17428 (Git)
Build type: Debug
Branch: master
Hash: 50c8513398fe6c68e30fd2cb1f0be58ccc555e58
Python version: 3.6.8
Qt version: 5.9.5
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: English/UnitedStates (en_US)


But not in Windows. I'll need to edit this to add that information.

Edit:
OS: Windows 10 (10.0)
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.19.17352 (Git)
Build type: Release
Branch: master
Hash: ec0049921b813ee78c1fd4ea304e70904a3208f7
Python version: 3.6.8
Qt version: 5.12.1
Coin version: 4.0.0a
OCC version: 7.3.0
Locale: English/United States (en_US)
openBrain
Posts: 2372
Joined: Fri Nov 09, 2018 5:38 pm

Re: [Bug ?] Incorrect behavior with Euler angles in Placement dialog

Postby openBrain » Thu Jul 18, 2019 12:37 pm

TheMarkster
Posts: 1061
Joined: Thu Apr 05, 2018 1:53 am

Re: [Bug #4062] Incorrect behavior with Euler angles in Placement dialog

Postby TheMarkster » Sat Jul 20, 2019 7:13 am

FreeCAD does not store they yaw/pitch/roll values, but rather they get calculated based on quaternions. The code is using arctan2(x,y) to get the yaw/pitch/roll values converted, but the problem is when pitch = 90 and roll = 90, what's getting called is arctan2(0,0), which is indeterminate. And according to the wikipedia article, as x and y approach 0 the floating point rounding errors expand.

https://en.wikipedia.org/wiki/Atan2

It seems arctan2() is not a trig function, it's an algorithm. It calls some variation on arctan() depending on the values of x and y. It might return arctan(y/x) or arctan(y/x)+pi or arctan(y/x)-pi or pi/2 or -pi/2 or undefined, all depending on the values of x and y compared to 0.

So, why the difference in windows/linux? Where x = 0 and y > 0 arctan2(x,y) returns Pi/2 (90 degrees).

In VS2017 it appears Windows is rounding numbers, so we get some of the q values = 0.2500000000000 whereas in Ubuntu one of the q values might be 0.249999999999989. (This is in Base/Rotation.cpp in getYawPitchRoll().) The upshot of this is what's getting called in windows is arctan2(0,0) and in ubuntu it's arctan2(2.2e-16,0). So, y > 0 in ubuntu, even if only by .000000000000000022, it's still y > 0 and so we're getting Pi/2 as the return value. Intuitively, I think windows is getting it right as these values should be 0,0, even though ubuntu, perhaps by lucky accident, is getting the correct answer.

edit: it's atan2(y,x) not arctan2(x,y)
kisolre
Posts: 1360
Joined: Wed Nov 21, 2018 1:13 pm

Re: [Bug #4062] Incorrect behavior with Euler angles in Placement dialog

Postby kisolre » Sat Jul 20, 2019 7:30 am

So should it need corner case treatment or just a documentation as a weird artifact? :D
TheMarkster
Posts: 1061
Joined: Thu Apr 05, 2018 1:53 am

Re: [Bug #4062] Incorrect behavior with Euler angles in Placement dialog

Postby TheMarkster » Sat Jul 20, 2019 5:51 pm

We could test the parameters against Base::Vector3d::epsilon() and send a warning to the console if both parameters are less than that.

Code: Select all

	if (2.0*(q01 + q23) < Base::Vector3d::epsilon() && (q00 + q33) - (q11 + q22) < Base::Vector3d::epsilon()) {
		Base::Console().Warning("Yaw value indeterminate\n");
	}

	if (2.0*(q12 + q03) < Base::Vector3d::epsilon() && (q22 + q33) - (q00 + q11) < Base::Vector3d::epsilon()) {
		Base::Console().Warning("Roll value indeterminate\n");
	}
But I'm not sure if sometimes it can be valid that both parameters are negative.
openBrain
Posts: 2372
Joined: Fri Nov 09, 2018 5:38 pm

Re: [Bug #4062] Incorrect behavior with Euler angles in Placement dialog

Postby openBrain » Sun Jul 21, 2019 7:42 pm

Actually the best would be to introduce management of north/south pole singularities in the code.
Unfortunately ATM I can't find the right code to manage this with the yaw/pitch/roll in Euler XY'Z'' used in FC. ;)
openBrain
Posts: 2372
Joined: Fri Nov 09, 2018 5:38 pm

Re: [Bug #4062] Incorrect behavior with Euler angles in Placement dialog

Postby openBrain » Mon Jul 22, 2019 12:52 pm

After some extra searching, I found that equations used in FC are the one described in this Wikipedia page. They are not strictly identical but quite easily linked to.
The main problem is that the page pointed in the Wikipedia article about patching singularities doesn't use the same equations, which thus makes tough to introduce singularities management in FC.
For direct reference, link to corresponding FC code.
TheMarkster wrote: Ping @previous_poster
microelly2 wrote: Ping @apparently comfortable with quaternions :)
wmayer wrote: Ping @code author
Guys, do you know/are one that could be able to deal with that ?
wmayer
Site Admin
Posts: 14992
Joined: Thu Feb 19, 2009 10:32 am

Re: [Bug #4062] Incorrect behavior with Euler angles in Placement dialog

Postby wmayer » Mon Jul 22, 2019 1:46 pm

They are not strictly identical but quite easily linked to.
The code on the WP does exactly the same as the FC code. On the WP side the quaternion uses qw, qx, qy, qz while in FreeCAD it's q0, q1, q2, q3 where q0 corresponds to qx, q1 to qy, q2 to qz and q3 to qw.

When comparing the formulas then you will see that they are identical to get the quaternion from the Euler angles. The formulas to get the Euler angles from the quaternion look slightly different but when you remember that the quaternions are normalized, i.e. qw**2 + qx**2 + qy**2 + qz**2 = 1 you can again show that the formulas are identical.