PR #2475: Expression syntax extension

Merged, abandoned or rejected pull requests are moved here to clear the main Pull Requests forum.
wmayer
Site Admin
Posts: 14788
Joined: Thu Feb 19, 2009 10:32 am

Re: PR #2475: Expression syntax extension

Postby wmayer » Tue Oct 08, 2019 9:43 pm

DeepSOIC wrote:
Tue Oct 08, 2019 7:15 pm
Bad news.
square root of a placement matrix is not a placement matrix.
As said above the square root of an arbitrary matrix is ambiguous and it's hard to say what the scipy implementation actually does.

But:
When you have placement with rotation R and translation t and apply it twice, i.e. get the square you have:
y = R * (R * x + t) + t
y = R * (R * x) + R * t + t
y= (R * R) * x + (R + I) * t

this means you have the rotation Q = R * R and the translation s = (R + I) * t.

Now if you consider a placement with rotation Q and translation s you can easily get the square root of Q (by doing only the half rotation) but for the translation part you have to compute t = (R + I)^-1 * s and the question is if this matrix is always invertible.

Example:

Code: Select all

import math
# arbitrarily rotate the object
p=App.ActiveDocument.ActiveObject.Placement
p.Base # Vector (-4.642491320876232, 2.744713842009025, 9.827212698728683)
p.Rotation.Q # (0.14145656219842426, -0.6950597415393837, -0.4084936490538904, 0.5744692640906436)

q=App.Rotation(p.Rotation.Axis, math.degrees(p.Rotation.Angle/2))
# compare the axes and angles
p.Rotation.Axis
q.Axis
p.Rotation.Angle
q.Angle # = p.Rotation.Angle/2

# get q as matrix
r=q.toMatrix()
r.determinant() # = 1.0 ok
m = r + App.Matrix() # (R + I) from above
m_inv = m.inverse()
s = p.Base
t = m_inv.multVec(s)
p_root = App.Placement(t, q)

# p_root * p_root gives the same as p
p_root.multiply(p_root) # Placement [Pos=(-4.64249,2.74471,9.82721), Yaw-Pitch-Roll=(-114.246,-43.0795,90.4892)]
p # Placement [Pos=(-4.64249,2.74471,9.82721), Yaw-Pitch-Roll=(-114.246,-43.0795,90.4892)]

# the same for their matrix representations
p.toMatrix()
p_root.toMatrix().multiply(p_root.toMatrix())
So, it's possible at least in this case to get the square root of a placement or matrix but the question is whether R + I is always invertible.
User avatar
DeepSOIC
Posts: 6967
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: PR #2475: Expression syntax extension

Postby DeepSOIC » Tue Oct 08, 2019 11:20 pm

wmayer wrote:
Tue Oct 08, 2019 8:22 pm
This references an interesting paper about the math and some source code:
http://www.xbdev.net/misc_demos/demos/d ... /paper.pdf

That's exactly what we need.
Well, I have read it, and mostly understood it. I think I feel like trying to implement it in FreeCAD. Maybe prototype it in python first.

BTW, I noticed funny correlations between 'dual numbers' thing and "DeriVector2" I've made up for sketcher solver.
User avatar
yorik
Site Admin
Posts: 11514
Joined: Tue Feb 17, 2009 9:16 pm
Location: São Paulo, Brazil
Contact:

Re: PR #2475: Expression syntax extension

Postby yorik » Wed Oct 09, 2019 1:23 pm

These high-level math discussions are cool :)
User avatar
DeepSOIC
Posts: 6967
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: PR #2475: Expression syntax extension

Postby DeepSOIC » Wed Oct 09, 2019 1:46 pm

BTW, I've prototyped it in python, and will post a demo in a separate thread. That thing is definitely working.
toralf
Posts: 25
Joined: Fri May 03, 2019 3:54 am

Re: PR #2475: Expression syntax extension

Postby toralf » Thu Oct 10, 2019 7:48 pm

Could you please provide a link to the thread? I could not find it. But maybe you have not posted it yet.
User avatar
DeepSOIC
Posts: 6967
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: PR #2475: Expression syntax extension

Postby DeepSOIC » Thu Oct 10, 2019 7:54 pm

toralf wrote:
Thu Oct 10, 2019 7:48 pm
Could you please provide a link to the thread? I could not find it. But maybe you have not posted it yet.
I haven't :oops: jumped straight to implementing it into FC.
toralf
Posts: 25
Joined: Fri May 03, 2019 3:54 am

Re: PR #2475: Expression syntax extension

Postby toralf » Thu Oct 10, 2019 8:10 pm

Ok, don’t worry. Go ahead.
User avatar
DeepSOIC
Posts: 6967
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: PR #2475: Expression syntax extension

Postby DeepSOIC » Fri Oct 11, 2019 8:25 pm

toralf wrote:
Thu Oct 10, 2019 7:48 pm
Could you please provide a link to the thread? I could not find it. But maybe you have not posted it yet.
Here! https://forum.freecadweb.org/viewtopic. ... 54#p340354
wmayer
Site Admin
Posts: 14788
Joined: Thu Feb 19, 2009 10:32 am

Re: PR #2475: Expression syntax extension

Postby wmayer » Sat Oct 12, 2019 10:39 am

So, it's possible at least in this case to get the square root of a placement or matrix but the question is whether R + I is always invertible.
Small addition: I found an example where R + I is not invertible. For every rotation with an angle of 180 deg the determinant of R + I becomes zero and thus is not invertible.
wmayer
Site Admin
Posts: 14788
Joined: Thu Feb 19, 2009 10:32 am

Re: PR #2475: Expression syntax extension

Postby wmayer » Sat Oct 12, 2019 2:48 pm

Here is short example to demonstrate the new sclerp() function of the placement:

Code: Select all

def animate(box, p1, p2, p3, steps=100):
  for i in range(steps):
    t=(i+1)/steps
    p=p1.sclerp(t,p2)
    box.Placement=p
    Gui.updateGui()
  
  for i in range(steps):
    t=(i+1)/steps
    p=p2.sclerp(t,p3)
    box.Placement=p
    Gui.updateGui()


box=App.ActiveDocument.Box
p1=box.Placement
p2=box.Placement # after changing manually
p3=box.Placement # after changing manually

animate(box, p1, p2, p3, 100)
p1, p2 and p3 define three different placements and with this script you can nicely see how the interpolation works.