vector equal vector

Need help, or want to share a macro? Post here!
E Guerra
Posts: 8
Joined: Tue Sep 08, 2015 4:50 pm

vector equal vector

Postby E Guerra » Tue Feb 09, 2016 2:24 pm

hello
is there no vector0 = vector1 function?
using vector0.x, vector0.y, vector0.z, = vector1.x, vector1.y, vector1.z
User avatar
DeepSOIC
Posts: 7600
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: vector equal vector

Postby DeepSOIC » Tue Feb 09, 2016 3:24 pm

Hi! Are you asking about assignment or comparison?

Code: Select all

vector2 = vector1 # <--- assignment
if vector2 == vector1:  # <----comparison
Assuming you mean comparison, this is not as trivial as it sounds. When comparing vectors, like with floating-point numbers, one must generally compare with tolerance. For example:

Code: Select all

if (vector1 - vector2).Length <= tolerance:
PS. Quick testing revealed that FreeCAD actually includes tolerance when comparing vectors directly:

Code: Select all

>>> vector1 = App.Vector()
>>> vector2 = App.Vector()
>>> vector1 == vector2
True
>>> vector2.x = 0.1
>>> vector1 == vector2
False
>>> vector2.x = 1e-8
>>> vector1 == vector2
True
E Guerra
Posts: 8
Joined: Tue Sep 08, 2015 4:50 pm

Re: vector equal vector

Postby E Guerra » Wed Feb 10, 2016 12:55 pm

thanks for response
vector2 = vector1 # <--- assignment
does not seem to work
using 0.15, for mac
User avatar
DeepSOIC
Posts: 7600
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: vector equal vector

Postby DeepSOIC » Wed Feb 10, 2016 1:29 pm

E Guerra wrote:vector2 = vector1 # <--- assignment
does not seem to work
Please explain, how it does not work for you. What exactly do you do, what you expect, and what you get instead.

You may be interested in reading this: Is Python call-by-value or call-by-reference? Neither.
E Guerra
Posts: 8
Joined: Tue Sep 08, 2015 4:50 pm

Re: vector equal vector

Postby E Guerra » Wed Feb 10, 2016 2:33 pm

hi
here's most of the code in question

Code: Select all

PORTS = {
....
'M10x1.0': ([0.0, 12.00], [05.00, 12.00], [05.00, 02.31], [05.21, 02.10], [05.55, 00.50], [08.00, 00.50], [08.00, 0.0]),
....
}
....
        self.details = PORTS['M10x1.0']
        vector0, vector1 = App.Vector(0.0, 0.0, 0.0), App.Vector(0.0, 0.0, 0.0)
        for key, value in enumerate(self.details):
            vector1.x, vector1.y = value
            App.ActiveDocument.Sketch.addGeometry(Part.Line(vector0, vector1))
            vector0.x, vector0.y = vector1.x, vector1.y
        App.ActiveDocument.Sketch.addGeometry(Part.Line(vector0, App.Vector(0.0, 0.0, 0.0))
....
basically generates a closed sketch
works with above code
if vector0 = vector1 is used instead of vector0.x, vector0.y = vector1.x, vector1.y
only one line is generated: [0.0, 0.0] to [0.0, 12.00]
Last edited by wmayer on Wed Feb 10, 2016 2:52 pm, edited 1 time in total.
Reason: Added code tags
User avatar
looo
Posts: 3301
Joined: Mon Nov 11, 2013 5:29 pm

Re: vector equal vector

Postby looo » Wed Feb 10, 2016 3:07 pm

It seems to me that this is your problem: (b points to a)

Code: Select all

>>> a = App.Vector(1,2,3)
>>> b = a
>>> a.x = 10
>>> a
Vector (10.0, 2.0, 3.0)
>>> b
Vector (10.0, 2.0, 3.0)
this way it should work: (all elements are copied)

Code: Select all

>>> a  = App.Vector(0,1,2)
>>> b = App.Vector(a)
>>> a.x = 10
>>> b
Vector (0.0, 1.0, 2.0)
>>> a
Vector (10.0, 1.0, 2.0)
wmayer
Site Admin
Posts: 15755
Joined: Thu Feb 19, 2009 10:32 am

Re: vector equal vector

Postby wmayer » Wed Feb 10, 2016 3:47 pm

Additionally to what looo said the problem is that after the first iteration you have added two (equal) lines to the sketch.
When doing a

Code: Select all

vector0 = vector1
results into the behaviour that vector0 and vector1 are the one and the same object. In the second iteration the creation of the line fails because the two passed points vector0 and vector1 are equal because they are still the same object.
User avatar
bernd
Posts: 9859
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: vector equal vector

Postby bernd » Tue Jan 16, 2018 7:25 pm

DeepSOIC wrote:
Tue Feb 09, 2016 3:24 pm
When comparing vectors, like with floating-point numbers, one must generally compare with tolerance. For example:

Code: Select all

if (vector1 - vector2).Length <= tolerance:
PS. Quick testing revealed that FreeCAD actually includes tolerance when comparing vectors directly:

Code: Select all

>>> vector1 = App.Vector()
>>> vector2 = App.Vector()
>>> vector1 == vector2
True
>>> vector2.x = 0.1
>>> vector1 == vector2
False
>>> vector2.x = 1e-8
>>> vector1 == vector2
True
Could anyone confirm this is really an intended approach? How big is the tolerance in such a comparison. I need to compare the direction of two edges. For the edge direction I use

Code: Select all

e = theedgeobject
direction = e.Vertexes[1].Point - e.Vertexes[0].Point
wmayer
Site Admin
Posts: 15755
Joined: Thu Feb 19, 2009 10:32 am

Re: vector equal vector

Postby wmayer » Tue Jan 16, 2018 8:17 pm

Could anyone confirm this is really an intended approach? How big is the tolerance in such a comparison. I need to compare the direction of two edges. For the edge direction I use
When you execute the above code snippet then you will see that

Code: Select all

vector1 = App.Vector()
vector2 = App.Vector()
vector1 == vector2
vector2.x = 1e-8
vector1 == vector2
the second comparison also returns false with v0.17. In the past the tolerance was much higher (FLT_EPSILON=1.192092896e-07) but now we use a much stricter tolerance (DBL_EPSILON=2.2204460492503131e-016) because the Base::Vector3d internally uses double, too.

So, with

Code: Select all

vector2.x = 1e-15
vector1 == vector2
it still returns false and with

Code: Select all

vector2.x = 1e-16
vector1 == vector2
it returns true.

Now have a look at occ's Precision class. There is a function Angular() that returns the threshold of what occ considers to be parallel. This limit is 1e-12.

So with the internal limit we use in the vector class you shouldn't run into problems because of that.
Btw, maybe it makes sense to also expose the Precision functions to Python.
User avatar
DeepSOIC
Posts: 7600
Joined: Fri Aug 29, 2014 12:45 am
Location: Saint-Petersburg, Russia

Re: vector equal vector

Postby DeepSOIC » Tue Jan 16, 2018 8:22 pm

btw, congratulations bernd!
bernd-5000.png
bernd-5000.png (92.98 KiB) Viewed 716 times