PR #2475: Expression syntax extension

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

Re: PR #2475: Expression syntax extension

Post by wmayer »

git commit 44354b808be (and possibly earlier commits) cause some memory leaks because instances of Py classes are created on the heap and passed as argument to Py::Object() so that the reference counter is incremented twice.

Example:
Create a sphere and try this code snippet:

Code: Select all

o=App.ActiveDocument.ActiveObject
o.evalExpression("minvert(Sphere.Shape.Placement)")
When running it in debug mode set a breakpoint to the constructor and destructor of PlacmentPy. The constructor is accessed twice but the destructor only once.
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: PR #2475: Expression syntax extension

Post by vocx »

wmayer wrote: Mon Oct 07, 2019 10:14 pm ...The constructor is accessed twice but the destructor only once.
I don't know if this is related but I've noticed a problem that makes me think of this. Ocassionaly when creating an object from a base object, two objects are created at once, like if the command was given twice. Do you think this is related? It doesn't seem to be a big problem other than you have to delete the second object.

I saw this when I was using the Dodo workbench (external workbenches) that is used to create pipe and frame objects. For example, I would place a wire, then select it, and hit a button to create a pipe. Instead of getting one pipe, I would get two. Of course, the problem could be in the Dodo workbench itself, but I wonder if this is not something more people have experienced.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
realthunder
Veteran
Posts: 2190
Joined: Tue Jan 03, 2017 10:55 am

Re: PR #2475: Expression syntax extension

Post by realthunder »

wmayer wrote: Mon Oct 07, 2019 10:14 pm git commit 44354b808be (and possibly earlier commits) cause some memory leaks because instances of Py classes are created on the heap and passed as argument to Py::Object() so that the reference counter is incremented twice.
Oops, this is not the first time I made this mistake. I searched around and fixed some similar ones. PR is here.
Try Assembly3 with my custom build of FreeCAD at here.
And if you'd like to show your support, you can donate through patreon, liberapay, or paypal
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: PR #2475: Expression syntax extension

Post by wmayer »

vocx wrote: Mon Oct 07, 2019 10:38 pm
wmayer wrote: Mon Oct 07, 2019 10:14 pm ...The constructor is accessed twice but the destructor only once.
I don't know if this is related but I've noticed a problem that makes me think of this. Ocassionaly when creating an object from a base object, two objects are created at once, like if the command was given twice. Do you think this is related? It doesn't seem to be a big problem other than you have to delete the second object.
If I understand this right then it's a different thing. What I am talking about is to handle the reference counter of Python objects where it happens that the counter is incremented one time too often so that when releasing it to the interpreter it will never become zero and thus the allocated memory won't be freed any more.
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: PR #2475: Expression syntax extension

Post by wmayer »

realthunder wrote: Tue Oct 08, 2019 1:23 am
wmayer wrote: Mon Oct 07, 2019 10:14 pm git commit 44354b808be (and possibly earlier commits) cause some memory leaks because instances of Py classes are created on the heap and passed as argument to Py::Object() so that the reference counter is incremented twice.
Oops, this is not the first time I made this mistake. I searched around and fixed some similar ones. PR is here.
Thanks!
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: PR #2475: Expression syntax extension

Post by wmayer »

DeepSOIC wrote: Mon Sep 30, 2019 2:29 pm
wmayer wrote: Mon Sep 30, 2019 12:22 pm
This is true for numbers (1/X = X^-1), but there is a rigorous mathematical notion behind it, that allows to define X^-3 or X^(1/2), but for matrices or Placements, what would mean :
On Wikipedia you can find some information for square roots of a matrix but it's only well-defined for positive-definite matrices while for arbitrary matrices it causes ambiguities. But what when using 1/3 as exponent instead? Here I couldn't find a valid definition. So, a power function for arbitrary exponents makes probably not much sense (especially not in a CAD program) and it should be limited to integers only as done by the current implementation.
I've been actually wanting this arbitrary exponent as a way to interpolate between placements.
You don't have to define, what ^(1/3) means, as 1/3 can't be precisely represented by a float anyway. A fractional part of float is a sum of fractions:
bit1*1/2 + bit2*1/4 + bit3*1/8 +...
Power-of-two fractional powers can be easily calculated by multiplying M^(1/2) by itself.
Do you have any concrete ideas/suggestions how to implement this? You may know the function "slerp" for quaternions which is about this but the idea could be extended for placements. I wonder whether we should use a real function name for this instead of using the pow() function.
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: PR #2475: Expression syntax extension

Post by DeepSOIC »

Given scipy's matrix square rooting function (scipy.linalg.sqrtm), I can give it a shot...
Last edited by DeepSOIC on Tue Oct 08, 2019 7:27 pm, edited 1 time in total.
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: PR #2475: Expression syntax extension

Post by DeepSOIC »

Bad news.
square root of a placement matrix is not a placement matrix.

Code: Select all

>>> rtm # the square root of matrix of a placement with some translation and some rotation
Matrix ((0.935334,0.241336,0.258664,7.09003),(0.258664,0.965346,0.0346543,8.62005),(0.241336,0.0993202,0.965346,3.10023),(0,0,0,1))
>>> rtplm.toMatrix() # if matrix survives being converted into placement, we are good
Matrix ((0.999922,-0.00851867,0.0088085,7.09003),(0.0088085,0.99942,-0.0322941,8.62005),(-0.00851867,0.0323718,0.99942,3.10023),(0,0,0,1))
>>> #no, matrix of placement is different. Bummer!
wmayer
Founder
Posts: 20243
Joined: Thu Feb 19, 2009 10:32 am
Contact:

Re: PR #2475: Expression syntax extension

Post by wmayer »

Have you ever heard of dual quaternions? For quaternions we have a spherical linear interpolation called slerp and when searching for an extension to also handle translation I stumbled across dual quaternions: https://community.khronos.org/t/dual-qu ... lerp/72496

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.

OCCT provides the class NCollection_Lerp<gp_Trsf> (see header gp_TrsfNLerp.hxx) which handles rotation and translation independently but that may cause some unnatural movements.
User avatar
DeepSOIC
Veteran
Posts: 7896
Joined: Fri Aug 29, 2014 12:45 am
Location: used to be Saint-Petersburg, Russia

Re: PR #2475: Expression syntax extension

Post by DeepSOIC »

wmayer wrote: Tue Oct 08, 2019 8:22 pm Have you ever heard of dual quaternions?
Nope :roll:

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.
reading,,,
Post Reply