Sandbox:Edwilliams16

Manipulating Vectors in FreeCAD
Vectors are ubiquitous in FreeCAD, describing point locations and displacements.

creates a vector  whose x-, y-, and z-components are 1, 2, 3, respectively.

The result of adding the two vectors, tip to tail is given by

but much more conveniently by:

because the + operator has been overloaded, resulting in the vector

We can also subtract vectors, and multiply or divide them by scalars, so we can write:

obtaining

The length of the vector v1 is given by Pythagoras' theorem (in 3D):

or much more conveniently by the builtin method

We can use a vector to represent a direction in space. For this purpose, since the (non-zero!) length of the vector is then immaterial, it is customary to use unit vectors, whose length is 1. (In FreeCAD dialogs for axes, you can use un-normalized directions. The code normalizes them for you.) We can create a unit vector by normalizing any vector in the desired direction:

This gives us alternative way of creating vectors. If we'd like, for instance, to create a vector in the direction of  with the length of , we could use:

Dot and cross products of vectors
Other than addition and subtraction, there are other geometrically meaningful ways of combining two vectors.

One is the dot product

In terms of components, if  and , then   Alternatively, this can be shown to be equal to the product of their two lengths with the cosine of the angle between them. It is thus, in some sense, the projection of one vector on the other. It can be used to calculate the angle between the two (non-zero) vectors:

giving the angle in radians.

this method is built in to FreeCAD as

The cross product of two vectors  and   creates a third vector, perpendicular to both of them, that is, normal to the plane containing   and In component form, Its length is given by the product of the lengths of  and   with the sine of the angle between them. It thus vanishes if  and   are parallel.

Another way of visualizing this is that the length of  is the area of the parallelogram defined by   and. Note that and   have opposite signs, but   and  are equal.

A test to check if, within numerical error, two (non-zero) vectors are parallel could be written:

Similarly,

tests for orthogonality.

These tests both return  if either vector has zero length. If you require, replace   with.

Rotations
Another operation we might want to perform on a vector is to rotate it. There are many ways to specify a rotation.

-- Rotation object-- four floats (a quaternion)

FreeCAD's internal representation of rotations (a, b, c, d) = a i + b j + c k + d

where d = cos(theta/2), (a,b,c) = sin(theta/2)*unit_vector

represents a rotation of theta about the unit_vector axis. It is unlikely you will need to work with these directly.

-- three floats (yaw, pitch, roll)

Uses Euler Angles. (see below)

​ https://wiki.freecadweb.org/Placement

-- Vector (rotation axis) and float (rotation angle)

see below

-- two Vectors (two axes)

rotates  into   in the v1 - v2 plane. (There are an infinity of other possible rotation axes)

-- Matrix object -- 16 floats (4x4 matrix)

The 3x3 submatrix in the top-left is the rotation part. The rest represents (unused) translation.

-- 9 floats (3x3 matrix)

See https://en.wikipedia.org/wiki/Rotation_matrix#In_three_dimensions

-- 3 vectors + optional string

For example, rotates x-&gt;y, y-&gt;-x and z-&gt;z. Because of potential numerical error, the target triad may not be exactly orthogonal, the optional string 'ZXY' gives the priority order of the calculation.

Of these, axis and angle is the most commonly used rotation constructor. To rotate  around the axis given by   by 30 degrees, we would use:

Note that we input the angle in degrees, but if we query it with  we get the internal value in radians.

Successive rotations and Euler Angles
See https://wiki.freecadweb.org/Placement#Position_and_Yaw.2C_Pitch_and_Roll

One of the properties of quaternions that makes them so useful is that the product of two quaternions represents the result of succesive rotations to which they correspond. The order matters! The result of two rotations is generally not the same if the rotations are made in the reverse order.

Yaw, pitch and roll through three Euler angles is a decomposition of a general rotation into three successive rotations about coordinate axes. This is commonly used in aerodynamics and rigid body mechanics.

-- First we rotate by the yaw angle about the z-axis.

-- Then pitch up by pitch angle about the new y-axis.

-- Finally, we roll about the new x axis by the roll angle. In FreeCAD

What is easily confused here is that in above case the successive rotations are about the new rotated axes. If instead, we make successive rotations about the fixed coordinate axes, not following the body, the multiplications are made in the opposite order!

Axis cube prior to rotation.

After rz - 90 degrees around global z axis After rzx - 90 degrees around global z axis, followed by 90 degrees around global x-axis.

Note that since rotation axes are stored normalized,  returns   This is likewise the case for the property of a Placement. When you enter an axis into a placement rotation dialog, it need not be normalized. The code replaces your entry with the normalized version.

Note that the  method tests   if   and   create the same result. For instance,,  and   test   with  even though their   and  properties differ, and are stored as created.

slerp (Spherical Linear Interpolation.)
This is a function widely used in animation. Suppose you wanted to smoothly rotate an object from one orientation,  to another,. You would need to create a series of intermediate orientations. creates a rotation 30% of the way between the two. As a simple example:

This example is simple because  and   happen to have the same axis. Slerp works in the general case. 

Offset rotations
represents rotations about the direction  through the origin. What if we wish to rotate a point  about an axis offset from the origin by the Vector  ? We break our Vector  into the two parts   and. The first part remains fixed, the second rotates, resulting in

Some other Vector Methods
Let v, v0, v1, v2 etc. be Vectors

This returns the perpendicular distance to the extended line passing though v1 in the direction v2

The name and tooltip are misleading here. This function returns the vector to the closest point on the line segment that extends from  and. It is along the perpendicular if that meets the line segment, otherwise it is to the nearest endpoint.

The plane is defined by, any point on it, and  , the direction of the normal to the plane. The method returns the shortest distance to the plane - positive if  is on the side of the plane pointed to by its normal , negative otherwise.