Expression doesn't update property correctly

Have some feature requests, feedback, cool stuff to share, or want to know where FreeCAD is going? This is the place.
Forum rules
Be nice to others! Read the FreeCAD code of conduct!
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Expression doesn't update property correctly

Post by onekk »

adrianinsaval wrote: Wed Dec 07, 2022 11:51 am
onekk wrote: Wed Dec 07, 2022 9:43 am
If you look at the values shown at the Placement line there's still something weird going on.
You intend the screenshot, it is relative to a 0 degree angle, so it differs from the 90 angle example below, I know, but I have the screenshot done, to show the expressions and the rotation part that is rounded.

But I'm not very skilled in Vector math, even if I have done some "weird things" and having not found problems, in replicating rotations, like in:

https://forum.freecadweb.org/viewtopic. ... 63#p636163

I have seen that in output:

Code: Select all

obj.Placement.Rotation
Rotation (0.5, 0.5, 0.0, 0.7071067811865476)
That seems to add a 0.0 in between values, and probably is not using a quaternion, is this what you are referring?

But as my vector math is derived mainly from @edwilliams16 teaching, probably he has some better explanations. :D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Expression doesn't update property correctly

Post by onekk »

Jolbas wrote: Wed Dec 07, 2022 12:46 pm @onekk Do you get expected results from python commands above?
Maybe you could you make some test code, to load and run, that probably could be useful if a problem is here maybe to keep under the cushion for future references?

Now I'm rather busy to put together some code, but I will be happy to download and run and post the results.

But probably even poking @wmayer will be useful, as if I remember weel something is changed in the code about rawAxis, but as usual my memory is not very reliable.

Regards

Carlo D.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: Expression doesn't update property correctly

Post by adrianinsaval »

onekk wrote: Wed Dec 07, 2022 3:52 pm You intend the screenshot, it is relative to a 0 degree angle, so it differs from the 90 angle example below, I know, but I have the screenshot done, to show the expressions and the rotation part that is rounded.
I mean that these do not match up:
Captura de pantalla 2022-12-07 140240.png
Captura de pantalla 2022-12-07 140240.png (19.05 KiB) Viewed 852 times
I don't know what to make of that.
User avatar
Jolbas
Posts: 327
Joined: Sat Mar 26, 2022 7:48 am
Location: Sweden

Re: Expression doesn't update property correctly

Post by Jolbas »

onekk wrote: Wed Dec 07, 2022 3:57 pm Maybe you could you make some test code, to load and run, that probably could be useful if a problem is here maybe to keep under the cushion for future references?
Short test code ta paste in python console:

Code: Select all

import math
rot = App.Rotation()
rot.Axis.x = math.sqrt(0.5)
rot.Axis.y = math.sqrt(0.5)
rot.Axis.z = 0
print(rot.RawAxis)

My result: Vector (0.4714045207910317, 0.5773502691896257, 0.0)
Expected result: Vector (0.7071067811865476, 0.7071067811865476, 0.0)
User avatar
onekk
Veteran
Posts: 6144
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Expression doesn't update property correctly

Post by onekk »

Code: Select all

import math
rot = App.Rotation()
rot.Axis.x = math.sqrt(0.5)
rot.Axis.y = math.sqrt(0.5)
rot.Axis.z = 0
print(rot.Axis)
print(rot.RawAxis)

see:

Code: Select all

18:28:34  Vector (0.6324555320336759, 0.7745966692414833, 0.0)
18:28:34  Vector (0.4714045207910317, 0.5773502691896257, 0.0)
consistent with your.

But:
| Rotation(x, y, z, w)
| Define from four floats (quaternion) where the quaternion is specified as:
| q = xi+yj+zk+w, i.e. the last parameter is the real part.
| x : float
| y : float
| z : float
| w : float
so when you issue print(rot) you obtain the 4 values, so probably is correct, adding an angle modify the results,

Another test:

Code: Select all

rot = App.Rotation()
rot.setYawPitchRoll(45,45,0)
print("Angle: ", rot.Angle)
print("Axis: ", rot.Axis)
print("RawAxis: ", rot.RawAxis)
print("YawPitchRoll: ", rot.getYawPitchRoll())
print("Rotation: ", rot)
print("Quaternion: ", rot.Q)
it give:

Code: Select all

Angle:  1.0960568152406256
Axis:  Vector (-0.2810846377148203, 0.6785983445458471, 0.6785983445458471)
RawAxis:  Vector (-0.28108463771482023, 0.678598344545847, 0.678598344545847)
YawPitchRoll:  (45.0, 44.99999999999999, 4.497983566394945e-15)
Rotation:  Rotation (-0.14644660940672624, 0.3535533905932738, 0.3535533905932738, 0.8535533905932737)
Quaternion:  (-0.14644660940672624, 0.3535533905932738, 0.3535533905932738, 0.8535533905932737)
a part for (IEEE floating error), setting Yaw Pitch and Roll seems correct.

Maybe if someone will explain the math behind, as using rotations it work as expected only the visualization is difficult to catch. at least for me.

Regards

Carlo D.

EDITED Post: sorry for any inconvenience.
GitHub page: https://github.com/onekk/freecad-doc.
- In deep articles on FreeCAD.
- Learning how to model with scripting.
- Various other stuffs.

Blog: https://okkmkblog.wordpress.com/
User avatar
adrianinsaval
Veteran
Posts: 5541
Joined: Thu Apr 05, 2018 5:15 pm

Re: Expression doesn't update property correctly

Post by adrianinsaval »

sounds to me like we want RawAxis to be exposed to the user rather than Axis, in python it looks like you can't assign values to RawAxis
User avatar
Jolbas
Posts: 327
Joined: Sat Mar 26, 2022 7:48 am
Location: Sweden

Re: Expression doesn't update property correctly

Post by Jolbas »

Yes. As if for examle the setter for Axis.x currently works like this:

Code: Select all

def setAxisX(self, newX):
    oldAxis = self.RawAxis.normalized()
    newAxis = Vector(newX, oldAxis.y, oldAxis.z)
    self.RawAxis = newAxis
I have looked at the code in Base/Rotation.cpp etc but I don't fully understand the code so that I can propose a fix. But I agree with @adrianinsaval that the x, y and z setter should modify the RawAxis. That's what is done when editing the properties in Property View. More like:

Code: Select all

def setAxisX(self, newX):
    oldAxis = self.RawAxis
    newAxis = Vector(newX, oldAxis.y, oldAxis.z)
    self.RawAxis = newAxis
edwilliams16
Veteran
Posts: 3106
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Expression doesn't update property correctly

Post by edwilliams16 »

The python interface does what I expect

Code: Select all

>>> obj.Placement.Rotation.Axis = App.Vector(1,1,1)
>>> obj.Placement.Rotation.RawAxis
Vector (1.0, 1.0, 1.0)
>>> obj.Placement.Rotation.Axis
Vector (0.5773502691896258, 0.5773502691896258, 0.5773502691896258)
>>> obj.Placement.Rotation.Axis.x =2
>>> obj.Placement.Rotation.Axis
Vector (0.9258200997725515, 0.26726124191242445, 0.26726124191242445)
>>> obj.Placement.Rotation.RawAxis
Vector (2.0, 0.5773502691896258, 0.5773502691896258)
>>> 
When I execute obj.Placement.Rotation.Axis.x =2, it first does exactly that, setting its x-component to 2, becoming Vector (2.0, 0.5773502691896258, 0.5773502691896258).
But, Axis is stored normalized, so it becomes Vector (0.9258200997725515, 0.26726124191242445, 0.26726124191242445), leaving the intermediate unnormalized value in RawAxis.

If I make successive changes of Axis.x, Axis.y and Axis.z, I'll obviously get a different result than from setting them simultaneously with

Code: Select all

obj.Placement.Rotation.Axis = App.Vector( x, y, z)
because of the intermediate normalizations.

However, there IS a problem with expressions. If I use expressions to set Axis.x, Axis.y, Axis.z separately, the Placement.Axis and the Axis components get out of sync, as has already been noted. It's a related issue, in that if, I set the rotation Axis by expression, not component-wise it works fine.

Note that if I change Axis.x etc. in the Properties window (not by expression) , it behaves properly - but what is displayed and modified is the RawAxis. So maybe the problem is that the expressions are driving Axis not RawAxis.
Screen Shot 2022-12-07 at 2.24.44 PM.png
Screen Shot 2022-12-07 at 2.24.44 PM.png (38.71 KiB) Viewed 750 times
User avatar
Jolbas
Posts: 327
Joined: Sat Mar 26, 2022 7:48 am
Location: Sweden

Re: Expression doesn't update property correctly

Post by Jolbas »

edwilliams16 wrote: Thu Dec 08, 2022 12:50 am But, Axis is stored normalized, so it becomes Vector (0.9258200997725515, 0.26726124191242445, 0.26726124191242445), leaving the intermediate unnormalized value in RawAxis.
Actually the normalized Axis property is calculated on every call and is not stored in memory. When saved to disk it is normalized but as long as the document is not closed and reopened it is the RawAxis that keeps track of the value.
Anyway I can't see any practical use of the current behaviour in either expressions or in a python script. I see more potential in being able to set the subvalues of RawAxis. Similar to how it works in Property view.
wmayer
Founder
Posts: 20241
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: Expression doesn't update property correctly

Post by wmayer »

I found an issue with expressions on some Placement properties.
That's a known issue/limitation. The underlying problem is that changing the coordinates of the rotation axis is not an atomic operation where the whole vector is set as a whole but the coordinates are set separately. When changing the coordinates separately it happens that the partially updated rotation axis is recomputed and returns a totally wrong vector.

This issue was brought up three years ago and the workaround was to allow to directly set the whole rotation axis of the placement.
See also: https://forum.freecadweb.org/viewtopic.php?f=3&t=41960
Post Reply