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. (Here the Z-axis is taken as is, the X-axis is orthogonalized and the Y axis is ignored.)

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.

Vectors in 2D
FreeCAD has a subsystem for creating 2D objects in the Part.Geom2d module. It uses a two component vector class App.Base.Vector2d. 3D vectors are in App.Vector See here for some usage information of Part.Geom2d.

Placement
A Placement combines rotation and translation into a single operator. is the translation vector. is the rotation. The combined operation translates the object, rotated about its origin, by the translation vector. (See Rigid Transformation Math for the representation of placements as 4 x 4 matrices.) The placement relates the local coordinate system (LCS) of the object to that of its container coordinate system (CCS). The tooltip for  shows the many options for its constructor. An example, rotating by 90 degrees about the x-axis and translating 20 in the z-direction, would be

A placement acting on (multiplying) a coordinate vector in the LCS gives the coordinates in the CCS. (see Vertex Coordinates)

We can test of two placements are equal within numerical tolerance with:

Compound placement
The document macroplacement, whose tree-view is illustrated above, contains two objects, Body and Cube. The Part container's location in the document's global coordinate system (GCS) is given by its Placement property. Likewise, Part001's location Part's LCS is given by its Placement. The global placements of the objects are determined by multiplying the chain of placements, starting from the root. Note that the features inside the Body container (Pad) have the placement of the Body. FCstd file is here.

Manipulating global Placement.
Changing any of the placements in the chain will change the global placements of the children. Suppose we wish to change the global placement of Body by changing the placement of Part001. We express the desired change in global placement of Body as

See also forum discussion and getting getGlobalPlacement for linked objects

Interpolating Placements.
Just as we can interpolate rotations using slerp, we can interpolate Placements using sclerp. The object takes a helical path between two placements, simultaneously rotating and translating.

Interpolation is a continuous motion along a helical path, made of equal transforms if discretized. t = 0.0 - return self. t = 1.0 - return placement2. t can also be outside of 0..1 range, for extrapolation. If the quaternions of rotations of the two placements differ in sign, the interpolation will take a long path. If 'shorten' is true, the signs are harmonized before interpolation, and the interpolation takes the shorter path.

Vertex Coordinates
Selecting a vertex in the 3D view displays its global coordinates and its name. We can obtain the local coordinates as follows.

Part workbench objects behave differently from others for historical reasons. Point for Part objects includes their placement. See Forum discussion

Using the Gui, on selecting a vertex:

Possible code correcting for placement of part objects