# Quaternion to Rotate Rigid Frame

I have arbitrary planes defined by coefficients A,B,C,D such that Ax + By + Cz + D = 0.

I want to fit a solid box over each plane such that one short edge of the box is perpendicular to the normal of the plane, with the other two edge lengths being longer, and with one of the other two edges parallel to the horizontal XY plane. I've been able to construct the position of the box but making the orientation has been tricky.

The coefficients given above for the plane make it really easy to get the normal vector to the plane N = (A,B,C). The way I've been trying to compose the orientation of the box is:

- Create a vector V normal to the global X axis and to the plane's normal vector by taking their cross product
- Using a dot product determine the angle theta between the global X axis and the plane normal N
- Create a quaternion using theta and V as the angle and rotation axis.

Using that quaternion for the object orientation gets me a box with its X-axis normal to the plane, but doesn't account for rotation of the box about its own X-axis. To get one of the edges (here its Y-axis) of the box parallel to the ground XY plane, I've been trying:

- Rotate the global Y-axis by the previously made quaternion using tf::quatRotate to produce the intermediate local Y-axis Y'
- Take the cross product of N and the global Z-axis to get a vector E parallel to the target plane and to the XY plane (E is the desired local box Y-axis)
- Determine the angle between the E and Y' vectors
- Use that angle and N to make a second quaternion, multiple this with the first quaternion to get the final box orientation

I haven't actually been able to get the second part to completely work yet, although I think the logic makes sense? **But I was wondering if there were any utilities in TF that I've overlooked which might make this whole process easier / anywhere this is already implemented.**

Fundamentally I just want to find the quaternion necessary to rotate a pair of perpendicular axes to a desired orientation, from a given start orientation, where the start and end axes are all defined as vectors.