Apply Rotation to a set of cords

Need help, or want to share a macro? Post here!
Forum rules
Be nice to others! Respect the FreeCAD code of conduct!
Post Reply
Vasten
Posts: 23
Joined: Wed May 18, 2022 9:00 pm

Apply Rotation to a set of cords

Post by Vasten »

I just want to apply a quaternion rotation a set of cords

is there simple way to do that without actually doing it a hacky way, like rotating an object and checking vertex cords

eg.new cords=qrotate(cords,quat)
User avatar
onekk
Veteran
Posts: 6222
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Apply Rotation to a set of cords

Post by onekk »

Please put always the info requested:

http://forum.freecadweb.org/viewtopic.php?f=3&t=2264


or at least what version of FC you are using.

and speaking of Python scripting and macro, use the </> button to insert code.

As you are writing on a code section, "speak with code", it is more clear and not ambigous.

New vertex coordinates probably could be obtained using transformations, matrix, but without code is difficult to answer as it depend on what objects are you using and how it is constructed.

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/
Vasten
Posts: 23
Joined: Wed May 18, 2022 9:00 pm

Re: Apply Rotation to a set of cords

Post by Vasten »

I just want to perform a quat rotation on a coordinate and get an output coordinate

Eg

Result coords = quaternion-rotate ( [x,y,z] , [w,x,y,z] ]

I dont want to perform it on an object,

Xyz in xyz out
User avatar
onekk
Veteran
Posts: 6222
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Apply Rotation to a set of cords

Post by onekk »

Vasten wrote:

I don't know what version of FC are you using.

I've tested it on 0.20R29076 (Git)

Please always put your FC info as this could be important to give you a correct answer.

Rotation could be expressed as quaternion.

Code: Select all

iimport FreeCAD

from FreeCAD import Placement, Rotation, Vector

pt = Part.Point(Vector(10, 10, 0))
a = Part.Vertex(pt)

Part.show(a, "VTX_a")


c = Part.Vertex(pt)
c.Placement = FreeCAD.Placement(Vector(0,0,0), Rotation(45.0, 0.0, 0.0))

qc = c.Placement.Rotation.Q

Part.show(c, "VTX_c")


for idx in range(0,5):
    d = Part.Vertex(pt)
    angle = 45.0
    rot = FreeCAD.Rotation(angle * idx, 0.0, 0.0)
    rotq = rot.Q
    d.Placement = FreeCAD.Placement(Vector(0,0,0), Rotation(*rotq))
    Part.show(d, f"VTX_d{idx}")
See if code above will help

rot is returned already as quaternion, at least in Python Console, but as there is the Q property, best is to use it.

Note that when assigning the property to Rotation the python expansion operator * as Q is returned as tuple and Rotation needs four floats to specify a Quaternion.

I don't know because I'm not very proficient in math if there is a way to "manipulate quaternions" in FC with some methods.


You could have more info writing in Python Console:

Code: Select all

print(FreeCAD.Rotation.__doc__)

or maybe:

help(FreeCAD.Rotation)


Hope it helps.

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/
edwilliams16
Veteran
Posts: 3191
Joined: Thu Sep 24, 2020 10:31 pm
Location: Hawaii
Contact:

Re: Apply Rotation to a set of cords

Post by edwilliams16 »

You can get the constructors for App.Rotation by typing into the python console and referring to the tooltip. To create the rotation represented by the quaternion q = w +xi +yj +zj use

Code: Select all

rot = App.Rotation(x, y, z, w)
and to apply it to a vector (u, v, w)

Code: Select all

rotated_vector = rot.multVec(App.Vector(u, v, w))
or more compactly, using the overloaded * operator (which also works for compounding rotations):

Code: Select all

rotated_vector = rot * App.Vector(u, v, w)
Vasten
Posts: 23
Joined: Wed May 18, 2022 9:00 pm

Re: Apply Rotation to a set of cords

Post by Vasten »

edwilliams16 wrote: Fri Jun 17, 2022 4:52 pm You can get the constructors for App.Rotation by typing into the python console and referring to the tooltip. To create the rotation represented by the quaternion q = w +xi +yj +zj use

Code: Select all

rot = App.Rotation(x, y, z, w)
and to apply it to a vector (u, v, w)

Code: Select all

rotated_vector = rot.multVec(App.Vector(u, v, w))
or more compactly, using the overloaded * operator (which also works for compounding rotations):

Code: Select all

rotated_vector = rot * App.Vector(u, v, w)

Perfect.. exactly what I needed.. Thank YOU :)
User avatar
dprojects
Posts: 722
Joined: Mon Mar 06, 2017 6:02 pm
Location: Poland
Contact:

Re: Apply Rotation to a set of cords

Post by dprojects »

But how to rotate object around the edge? mission impossible at FreeCAD? ;-)

Thanks
Darek
github.com/dprojects

workbench for woodworking is available at: github.com/dprojects/Woodworking
User avatar
onekk
Veteran
Posts: 6222
Joined: Sat Jan 17, 2015 7:48 am
Contact:

Re: Apply Rotation to a set of cords

Post by onekk »

dprojects wrote: Sun Jun 19, 2022 1:35 pm But how to rotate object around the edge? mission impossible at FreeCAD? ;-)
Peivably using Placement specification with:

Code: Select all

Placement(VT, rot, c_rot)

VT = translation vector

rot = rotation specification

c_rot = center if rotation

It usually work as it is specified also in the wiki page.

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/
Post Reply