zToVector function

Array zToVector(
  1. Vector3 vector
)

Implementation

Array zToVector(Vector3 vector) {
  if (vector.normSquared() == 0.0) {
    return Array.identity(3);
  }

  var v = vector.normalize();
  var phi = acos(v.z);
  var theta = 0.0;

  if (v.x != 0.0 || v.y != 0) {
    // projection of vector to unit circle
    var axisProj = v.withCoords(z: 0).normalize();
    theta = acos(axisProj.x);

    if (axisProj.y < 0) {
      theta *= -1;
    }
  }

  var phiDown = Array(values: [
    [cos(phi), 0, sin(phi)],
    [0, 1, 0],
    [-sin(phi), 0, cos(phi)],
  ]);

  return rotationAboutZ(theta).matMul(phiDown);
}