Converting matrices into transforms
External file formats might store transformation data as matrices. glTF, for example, can store the transform of a node as the position, rotation, and scale, or as a single 4x4 matrix. To make the transform code robust, you need to be able to convert matrices to transforms.
Converting a matrix to a transform is more difficult than converting a transform to a matrix. Extracting the rotation of the matrix is simple; you have already implemented a function to turn a 4x4 matrix into a quaternion. Extracting the position is also simple; copy the last column of the matrix into a vector. Extracting the scale is more difficult.
Recall that the order of operations for a transform is to scale, rotate, and then translate. This means that if you had three matrices—S, R, and T—that represent scale, rotation, and translation, respectively, they would combine into a transform matrix, M, as follows:
M = SRT
To find the scale, first, ignore...