How to rotate one vector to the another?

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
stefankorisnik3
Posts: 101
Joined: Sun Jul 24, 2022 12:49 pm

How to rotate one vector to the another?

Post by stefankorisnik3 »

I want to rotate the vector App.Vector(1,1,1) to the App.Vector(1,1,0) like on the next image:
Image (if image doesn't work: https://imgur.com/z3GJ3QR)

I tried the following but it doesn't work:

Code: Select all

>>v = App.Vector(1,1,1)
>>angle = 35.26
>>rot = App.Rotation(App.Vector(0,0,1), angle)
>>rot.multVec(v)
Vector (0.23925309980019827, 1.393828523971294, 0.9999999999999999)
Can you tell me how can i get the vector (1,1,0) by rotation of the vector App.Vector(1,1,1) like on the picture?
edwilliams16
Veteran
Posts: 3106
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: How to rotate one vector to the another?

Post by edwilliams16 »

App.Rotation(v1, v2) rotates v1 into v2 in the v1 - v2 plane. (There are an infinity of other possible rotation axes)
Just type help(App.Rotation) into the Python console. Excerpt below:

Code: Select all

>>> help(App.Rotation)
Help on class Rotation:

class Rotation(builtins.PyObjectBase)
 |  Base.Rotation class.
 |  
 |  A Rotation using a quaternion.
 |  
 |  The following constructors are supported:
 |  
 |  Rotation()
 |  Empty constructor.
 |  
 |  Rotation(rotation)
 |  Copy constructor.
 |  
 |  Rotation(Axis, Radian)
 |  Rotation(Axis, Degree)
 |  Define from an axis and an angle (in radians or degrees according to the keyword).
 |  Axis : Base.Vector
 |  Radian : float
 |  Degree : float
 |  
 |  Rotation(vector_start, vector_end)
 |  Define from two vectors (rotation from/to vector).
 |  vector_start : Base.Vector
 |  vector_end : Base.Vector
 |  
 |  Rotation(angle1, angle2, angle3)
 |  Define from three floats (Euler angles) as yaw-pitch-roll in XY'Z'' convention.
 |  angle1 : float
 |  angle2 : float
 |  angle3 : float
 |  
 |  Rotation(seq, angle1, angle2, angle3)
 |  Define from one string and three floats (Euler angles) as Euler rotation
 |  of a given type. Call toEulerAngles() for supported sequence types.
 |  seq : str
 |  angle1 : float
 |  angle2 : float
 |  angle3 : float
 |  
 |  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
 |  
 |  Rotation(dir1, dir2, dir3, seq)
 |  Define from three vectors that define rotated axes directions plus an optional
 |  3-characher string of capital letters 'X', 'Y', 'Z' that sets the order of 
 |  importance of the axes (e.g., 'ZXY' means z direction is followed strictly,
 |  x is used but corrected if necessary, y is ignored).
 |  dir1 : Base.Vector
 |  dir2 : Base.Vector
 |  dir3 : Base.Vector
 |  seq : str
 |  
 |  Rotation(matrix)
 |  Define from a matrix rotation in the 4D representation.
 |  matrix : Base.Matrix
 |  
 |  Rotation(*coef)
 |  Define from 16 or 9 elements which represent the rotation in the 4D matrix
 |  representation or in the 3D matrix representation, respectively.
 |  coef : sequence of float
 |  
 
Post Reply